ccGLMatrix.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_GL_MATRIX_HEADER
  18. #define CC_GL_MATRIX_HEADER
  19. //Local
  20. #include "ccGLMatrixTpl.h"
  21. //CCCoreLib
  22. #include <SquareMatrix.h>
  23. //! Float version of ccGLMatrixTpl
  24. class QCC_DB_LIB_API ccGLMatrix : public ccGLMatrixTpl<float>
  25. {
  26. public:
  27. //! Default constructor
  28. /** Matrix is set to identity (see ccGLMatrixTpl::toIdentity) by default.
  29. **/
  30. ccGLMatrix() : ccGLMatrixTpl<float>() {}
  31. //! Copy constructor from a ccGLMatrixTpl
  32. /** \param mat matrix
  33. **/
  34. ccGLMatrix(const ccGLMatrixTpl<float>& mat) : ccGLMatrixTpl<float>(mat) {}
  35. //! Constructor from a float GL matrix array
  36. /** \param mat16f a 16 elements array (column major order)
  37. **/
  38. explicit ccGLMatrix(const float* mat16f) : ccGLMatrixTpl<float>(mat16f) {}
  39. //! Constructor from a double GL matrix array
  40. /** \warning Will implicitly cast the elements to float!
  41. \param mat16d a 16 elements array (column major order)
  42. **/
  43. explicit ccGLMatrix(const double* mat16d) : ccGLMatrixTpl<float>(mat16d) {}
  44. //! Constructor from 4 columns (X,Y,Z,Tr)
  45. /** \param X 3 first elements of the 1st column (last one is 0)
  46. \param Y 3 first elements of the 2nd column (last one is 0)
  47. \param Z 3 first elements of the 3rd column (last one is 0)
  48. \param Tr 3 first elements of the last column (last one is 1)
  49. **/
  50. ccGLMatrix(const Vector3Tpl<float>& X, const Vector3Tpl<float>& Y, const Vector3Tpl<float>& Z, const Vector3Tpl<float>& Tr) : ccGLMatrixTpl<float>(X,Y,Z,Tr) {}
  51. };
  52. //! Double version of ccGLMatrixTpl
  53. class QCC_DB_LIB_API ccGLMatrixd : public ccGLMatrixTpl<double>
  54. {
  55. public:
  56. //! Default constructor
  57. /** Matrix is set to identity (see ccGLMatrixTpl::toIdentity) by default.
  58. **/
  59. ccGLMatrixd() : ccGLMatrixTpl<double>() {}
  60. //! Copy constructor from a ccGLMatrixTpl
  61. /** \param mat matrix
  62. **/
  63. ccGLMatrixd(const ccGLMatrixTpl<double>& mat) : ccGLMatrixTpl<double>(mat) {}
  64. //! Constructor from a float GL matrix array
  65. /** \param mat16f a 16 elements array (column major order)
  66. **/
  67. explicit ccGLMatrixd(const float* mat16f) : ccGLMatrixTpl<double>(mat16f) {}
  68. //! Constructor from a double GL matrix array
  69. /** \param mat16d a 16 elements array (column major order)
  70. **/
  71. explicit ccGLMatrixd(const double* mat16d) : ccGLMatrixTpl<double>(mat16d) {}
  72. //! Constructor from 4 columns (X,Y,Z,Tr)
  73. /** \param X 3 first elements of the 1st column (last one is 0)
  74. \param Y 3 first elements of the 2nd column (last one is 0)
  75. \param Z 3 first elements of the 3rd column (last one is 0)
  76. \param Tr 3 first elements of the last column (last one is 1)
  77. **/
  78. ccGLMatrixd(const Vector3Tpl<double>& X, const Vector3Tpl<double>& Y, const Vector3Tpl<double>& Z, const Vector3Tpl<double>& Tr) : ccGLMatrixTpl<double>(X,Y,Z,Tr) {}
  79. };
  80. /*** Helpers ***/
  81. //! Constructor from a 3x3 rotation matrix R and a vector Tr
  82. template <typename Tin, typename Tout> ccGLMatrixTpl<Tout> FromCCLibMatrix(const CCCoreLib::SquareMatrixTpl<Tin>& R, const Vector3Tpl<Tin>& Tr)
  83. {
  84. ccGLMatrixTpl<Tout> outputMat;
  85. //outputMat.toIdentity(); //already done in the constructor
  86. if (R.isValid() && R.size() == 3)
  87. {
  88. //we copy each column
  89. Tout* mat = outputMat.data();
  90. for (unsigned j=0; j<3; ++j)
  91. {
  92. *mat++ = static_cast<Tout>(R.m_values[0][j]);
  93. *mat++ = static_cast<Tout>(R.m_values[1][j]);
  94. *mat++ = static_cast<Tout>(R.m_values[2][j]);
  95. mat++;
  96. }
  97. }
  98. outputMat.setTranslation( Vector3Tpl<Tout>( static_cast<Tout>(Tr.x),
  99. static_cast<Tout>(Tr.y),
  100. static_cast<Tout>(Tr.z) ));
  101. return outputMat;
  102. }
  103. //! Constructor from a 3x3 rotation matrix R, a vector Tr, a scale S
  104. template <typename Tin, typename Tout> ccGLMatrixTpl<Tout> FromCCLibMatrix(const CCCoreLib::SquareMatrixTpl<Tin>& R, const Vector3Tpl<Tin>& Tr, Tin S)
  105. {
  106. ccGLMatrixTpl<Tout> outputMat;
  107. //outputMat.toIdentity(); //already done in the constructor
  108. if (R.isValid() && R.size() == 3)
  109. {
  110. //we copy each column
  111. Tout* mat = outputMat.data();
  112. for (unsigned j = 0; j < 3; ++j)
  113. {
  114. *mat++ = static_cast<Tout>(R.m_values[0][j] * S);
  115. *mat++ = static_cast<Tout>(R.m_values[1][j] * S);
  116. *mat++ = static_cast<Tout>(R.m_values[2][j] * S);
  117. mat++;
  118. }
  119. }
  120. outputMat.setTranslation( Vector3Tpl<Tout>( static_cast<Tout>(Tr.x),
  121. static_cast<Tout>(Tr.y),
  122. static_cast<Tout>(Tr.z) ));
  123. return outputMat;
  124. }
  125. //! Constructor from a rotation center G, a 3x3 rotation matrix R and a vector Tr
  126. template <typename Tin, typename Tout> ccGLMatrixTpl<Tout> FromCCLibMatrix(const CCCoreLib::SquareMatrixTpl<Tin>& R, const Vector3Tpl<Tin>& Tr, const Vector3Tpl<Tin>& rotCenter)
  127. {
  128. ccGLMatrixTpl<Tout> outputMat = FromCCLibMatrix<Tin,Tout>(R,Tr);
  129. outputMat.shiftRotationCenter(rotCenter);
  130. return outputMat;
  131. }
  132. #endif //CC_GL_MATRIX_HEADER