fw4spl
SrcLib/core/fwDataTools/include/fwDataTools/TransformationMatrix3D.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "fwDataTools/config.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwData/Point.hpp>
14 #include <fwData/TransformationMatrix3D.hpp>
15 
16 #include <glm/mat4x4.hpp>
17 
18 namespace fwDataTools
19 {
24 {
25 public:
26 
32  FWDATATOOLS_API static bool invert(const ::fwData::TransformationMatrix3D::csptr& _input,
33  ::fwData::TransformationMatrix3D::sptr& _output);
34 
41  FWDATATOOLS_API static void multiply(const ::fwData::TransformationMatrix3D::csptr& _trfA,
42  const ::fwData::TransformationMatrix3D::csptr& _trfB,
43  ::fwData::TransformationMatrix3D::sptr& _output);
44 
49  FWDATATOOLS_API static void identity(::fwData::TransformationMatrix3D::sptr& _trf);
50 
57  FWDATATOOLS_API static void multiply(const ::fwData::TransformationMatrix3D::csptr& _trf,
58  const ::fwData::Point::csptr& _input, ::fwData::Point::sptr& _output);
59 
65  FWDATATOOLS_API static ::glm::dmat4x4 getMatrixFromTF3D(const ::fwData::TransformationMatrix3D::csptr& _trf);
66 
72  FWDATATOOLS_API static void setTF3DFromMatrix(::fwData::TransformationMatrix3D::sptr& _trf,
73  const ::glm::dmat4x4& _input);
74 
81  FWDATATOOLS_API static bool isIdentity(const ::fwData::TransformationMatrix3D::csptr& _trf,
82  const double _epsilon = 1e-12);
83 };
84 
85 // ----------------------------------------------------------------------------
86 
87 inline ::glm::dmat4x4 TransformationMatrix3D::getMatrixFromTF3D(const ::fwData::TransformationMatrix3D::csptr& _trf)
88 {
89  // TransformationMatrix3D is stored row-major
90  // glm matrices are stored column-major
91  const auto& coefs = _trf->getCoefficients();
92 
93  ::glm::dmat4x4 mat(coefs[0], coefs[4], coefs[8], coefs[12],
94  coefs[1], coefs[5], coefs[9], coefs[13],
95  coefs[2], coefs[6], coefs[10], coefs[14],
96  coefs[3], coefs[7], coefs[11], coefs[15]);
97 
98  return mat;
99 }
100 
101 // ----------------------------------------------------------------------------
102 
103 inline void TransformationMatrix3D::setTF3DFromMatrix(::fwData::TransformationMatrix3D::sptr& _trf,
104  const ::glm::dmat4x4& _input)
105 {
106  // TransformationMatrix3D is stored row-major
107  // glm matrices are stored column-major
108  auto& coefs = _trf->getCoefficients();
109  for (size_t i = 0; i < 4; ++i)
110  {
111  const size_t rowDst = i * 4;
112  const ::glm::length_t rowSrc = static_cast< ::glm::length_t>(i);
113  for (size_t j = 0; j < 4; ++j)
114  {
115  const ::glm::length_t colSrc = static_cast< ::glm::length_t>(j);
116  coefs[rowDst + j] = _input[colSrc][rowSrc];
117  }
118  }
119 }
120 } // namespace fwDataTools
static FWDATATOOLS_API void setTF3DFromMatrix(::fwData::TransformationMatrix3D::sptr &_trf, const ::glm::dmat4x4 &_input)
Convert a GLM matrix into a fwData::TransformationMatrix3D.
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
static FWDATATOOLS_API::glm::dmat4x4 getMatrixFromTF3D(const ::fwData::TransformationMatrix3D::csptr &_trf)
Convert a fwData::TransformationMatrix3D into a GLM matrix.
static FWDATATOOLS_API void multiply(const ::fwData::TransformationMatrix3D::csptr &_trfA, const ::fwData::TransformationMatrix3D::csptr &_trfB,::fwData::TransformationMatrix3D::sptr &_output)
Multiply two matrices.
static FWDATATOOLS_API bool isIdentity(const ::fwData::TransformationMatrix3D::csptr &_trf, const double _epsilon=1e-12)
Return whether a fwData::TransformationMatrix3D is an identity matrix.
static FWDATATOOLS_API void identity(::fwData::TransformationMatrix3D::sptr &_trf)
Set the matrix to identity.
static FWDATATOOLS_API bool invert(const ::fwData::TransformationMatrix3D::csptr &_input,::fwData::TransformationMatrix3D::sptr &_output)
Invert a matrix.