canonical_quaternions.h 5.6 KB

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