canonical_quaternions.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef IGL_CANONICAL_QUATERNIONS_H
  2. #define IGL_CANONICAL_QUATERNIONS_H
  3. // Define some canonical quaternions for floats and doubles
  4. // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  5. // such that q = x*i + y*j + z*k + w
  6. namespace igl
  7. {
  8. # define SQRT_2_OVER_2 0.707106781f
  9. // Float versions
  10. // Identity
  11. const float IDENTITY_QUAT_F[4] = {0,0,0,1};
  12. // The following match the Matlab canonical views
  13. // X point right, Y pointing up and Z point out
  14. const float XY_PLANE_QUAT_F[4] = {0,0,0,1};
  15. // X points right, Y points *in* and Z points up
  16. const float XZ_PLANE_QUAT_F[4] = {-SQRT_2_OVER_2,0,0,SQRT_2_OVER_2};
  17. // X points out, Y points right, and Z points up
  18. const float YZ_PLANE_QUAT_F[4] = {-0.5,-0.5,-0.5,0.5};
  19. const float CANONICAL_VIEW_QUAT_F[][4] =
  20. {
  21. { 0, 0, 0, 1},
  22. { 0, 0, SQRT_2_OVER_2, SQRT_2_OVER_2},
  23. { 0, 0, 1, 0},
  24. { 0, 0, SQRT_2_OVER_2,-SQRT_2_OVER_2},
  25. { 0, -1, 0, 0},
  26. {-SQRT_2_OVER_2, SQRT_2_OVER_2, 0, 0},
  27. { -1, 0, 0, 0},
  28. {-SQRT_2_OVER_2,-SQRT_2_OVER_2, 0, 0},
  29. { -0.5, -0.5, -0.5, 0.5},
  30. { 0,-SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  31. { 0.5, -0.5, 0.5, 0.5},
  32. { SQRT_2_OVER_2, 0, SQRT_2_OVER_2, 0},
  33. { SQRT_2_OVER_2, 0,-SQRT_2_OVER_2, 0},
  34. { 0.5, 0.5, -0.5, 0.5},
  35. { 0, SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  36. { -0.5, 0.5, 0.5, 0.5},
  37. { 0, SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  38. { -0.5, 0.5, 0.5, -0.5},
  39. {-SQRT_2_OVER_2, 0, 0,-SQRT_2_OVER_2},
  40. { -0.5, -0.5, -0.5, -0.5},
  41. {-SQRT_2_OVER_2, 0, 0, SQRT_2_OVER_2},
  42. { -0.5, -0.5, 0.5, 0.5},
  43. { 0,-SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  44. { 0.5, -0.5, 0.5, -0.5}
  45. };
  46. # undef SQRT_2_OVER_2
  47. # define SQRT_2_OVER_2 0.707106781186548f
  48. // Double versions
  49. // Identity
  50. const double IDENTITY_QUAT_D[4] = {0,0,0,1};
  51. // The following match the Matlab canonical views
  52. // X point right, Y pointing up and Z point out
  53. const double XY_PLANE_QUAT_D[4] = {0,0,0,1};
  54. // X points right, Y points *in* and Z points up
  55. const double XZ_PLANE_QUAT_D[4] = {-SQRT_2_OVER_2,0,0,SQRT_2_OVER_2};
  56. // X points out, Y points right, and Z points up
  57. const double YZ_PLANE_QUAT_D[4] = {-0.5,-0.5,-0.5,0.5};
  58. const double CANONICAL_VIEW_QUAT_D[][4] =
  59. {
  60. { 0, 0, 0, 1},
  61. { 0, 0, SQRT_2_OVER_2, SQRT_2_OVER_2},
  62. { 0, 0, 1, 0},
  63. { 0, 0, SQRT_2_OVER_2,-SQRT_2_OVER_2},
  64. { 0, -1, 0, 0},
  65. {-SQRT_2_OVER_2, SQRT_2_OVER_2, 0, 0},
  66. { -1, 0, 0, 0},
  67. {-SQRT_2_OVER_2,-SQRT_2_OVER_2, 0, 0},
  68. { -0.5, -0.5, -0.5, 0.5},
  69. { 0,-SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  70. { 0.5, -0.5, 0.5, 0.5},
  71. { SQRT_2_OVER_2, 0, SQRT_2_OVER_2, 0},
  72. { SQRT_2_OVER_2, 0,-SQRT_2_OVER_2, 0},
  73. { 0.5, 0.5, -0.5, 0.5},
  74. { 0, SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  75. { -0.5, 0.5, 0.5, 0.5},
  76. { 0, SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  77. { -0.5, 0.5, 0.5, -0.5},
  78. {-SQRT_2_OVER_2, 0, 0,-SQRT_2_OVER_2},
  79. { -0.5, -0.5, -0.5, -0.5},
  80. {-SQRT_2_OVER_2, 0, 0, SQRT_2_OVER_2},
  81. { -0.5, -0.5, 0.5, 0.5},
  82. { 0,-SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  83. { 0.5, -0.5, 0.5, -0.5}
  84. };
  85. const size_t NUM_CANONICAL_VIEW_QUAT = 24;
  86. // NOTE: I want to rather be able to return a Q_type[][] but C++ is not
  87. // making it easy. So instead I've written a per-element accessor
  88. // Return element [i][j] of the corresponding CANONICAL_VIEW_QUAT_* of the
  89. // given templated type
  90. // Inputs:
  91. // i index of quaternion
  92. // j index of coordinate in quaternion i
  93. // Returns values of CANONICAL_VIEW_QUAT_*[i][j]
  94. template <typename Q_type>
  95. inline const Q_type CANONICAL_VIEW_QUAT(size_t i, size_t j);
  96. // Template specializations for float and double
  97. template <>
  98. inline const float CANONICAL_VIEW_QUAT<float>(size_t i, size_t j);
  99. template <>
  100. inline const double CANONICAL_VIEW_QUAT<double>(size_t i, size_t j);
  101. # undef SQRT_2_OVER_2
  102. }
  103. // Implementation
  104. template <> inline const float igl::CANONICAL_VIEW_QUAT<float>(size_t i, size_t j)
  105. {
  106. return igl::CANONICAL_VIEW_QUAT_F[i][j];
  107. }
  108. template <> inline const double igl::CANONICAL_VIEW_QUAT<double>(size_t i, size_t j)
  109. {
  110. return igl::CANONICAL_VIEW_QUAT_D[i][j];
  111. }
  112. #endif