canonical_quaternions.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_CANONICAL_QUATERNIONS_H
  9. #define IGL_CANONICAL_QUATERNIONS_H
  10. #include "igl_inline.h"
  11. /// @file canonical_quaternions
  12. ///
  13. /// Define some canonical quaternions for floats and doubles
  14. /// A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  15. /// such that q = x*i + y*j + z*k + w.
  16. ///
  17. /// \see snap_to_canonical_view_quat
  18. namespace igl
  19. {
  20. // Float versions
  21. // This will get undef'd below
  22. #define SQRT_2_OVER_2 0.707106781f
  23. // Identity quaternion
  24. const float IDENTITY_QUAT_F[4] = {0,0,0,1};
  25. // The following match the Matlab canonical views
  26. // X point right, Y pointing up and Z point out
  27. const float XY_PLANE_QUAT_F[4] = {0,0,0,1};
  28. // X points right, Y points *in* and Z points up
  29. const float XZ_PLANE_QUAT_F[4] = {-SQRT_2_OVER_2,0,0,SQRT_2_OVER_2};
  30. // X points out, Y points right, and Z points up
  31. const float YZ_PLANE_QUAT_F[4] = {-0.5,-0.5,-0.5,0.5};
  32. const float CANONICAL_VIEW_QUAT_F[][4] =
  33. {
  34. { 0, 0, 0, 1}, // 0
  35. { 0, 0, SQRT_2_OVER_2, SQRT_2_OVER_2}, // 1
  36. { 0, 0, 1, 0}, // 2
  37. { 0, 0, SQRT_2_OVER_2,-SQRT_2_OVER_2}, // 3
  38. { 0, -1, 0, 0}, // 4
  39. {-SQRT_2_OVER_2, SQRT_2_OVER_2, 0, 0}, // 5
  40. { -1, 0, 0, 0}, // 6
  41. {-SQRT_2_OVER_2,-SQRT_2_OVER_2, 0, 0}, // 7
  42. { -0.5, -0.5, -0.5, 0.5}, // 8
  43. { 0,-SQRT_2_OVER_2, 0, SQRT_2_OVER_2}, // 9
  44. { 0.5, -0.5, 0.5, 0.5}, // 10
  45. { SQRT_2_OVER_2, 0, SQRT_2_OVER_2, 0}, // 11
  46. { SQRT_2_OVER_2, 0,-SQRT_2_OVER_2, 0}, // 12
  47. { 0.5, 0.5, -0.5, 0.5}, // 13
  48. { 0, SQRT_2_OVER_2, 0, SQRT_2_OVER_2}, // 14
  49. { -0.5, 0.5, 0.5, 0.5}, // 15
  50. { 0, SQRT_2_OVER_2, SQRT_2_OVER_2, 0}, // 16
  51. { -0.5, 0.5, 0.5, -0.5}, // 17
  52. {-SQRT_2_OVER_2, 0, 0,-SQRT_2_OVER_2}, // 18
  53. { -0.5, -0.5, -0.5, -0.5}, // 19
  54. {-SQRT_2_OVER_2, 0, 0, SQRT_2_OVER_2}, // 20
  55. { -0.5, -0.5, 0.5, 0.5}, // 21
  56. { 0,-SQRT_2_OVER_2, SQRT_2_OVER_2, 0}, // 22
  57. { 0.5, -0.5, 0.5, -0.5} // 23
  58. };
  59. #undef SQRT_2_OVER_2
  60. // Double versions
  61. #define SQRT_2_OVER_2 0.70710678118654757
  62. // Identity
  63. const double IDENTITY_QUAT_D[4] = {0,0,0,1};
  64. // The following match the Matlab canonical views
  65. // X point right, Y pointing up and Z point out
  66. const double XY_PLANE_QUAT_D[4] = {0,0,0,1};
  67. // X points right, Y points *in* and Z points up
  68. const double XZ_PLANE_QUAT_D[4] = {-SQRT_2_OVER_2,0,0,SQRT_2_OVER_2};
  69. // X points out, Y points right, and Z points up
  70. const double YZ_PLANE_QUAT_D[4] = {-0.5,-0.5,-0.5,0.5};
  71. const double CANONICAL_VIEW_QUAT_D[][4] =
  72. {
  73. { 0, 0, 0, 1},
  74. { 0, 0, SQRT_2_OVER_2, SQRT_2_OVER_2},
  75. { 0, 0, 1, 0},
  76. { 0, 0, SQRT_2_OVER_2,-SQRT_2_OVER_2},
  77. { 0, -1, 0, 0},
  78. {-SQRT_2_OVER_2, SQRT_2_OVER_2, 0, 0},
  79. { -1, 0, 0, 0},
  80. {-SQRT_2_OVER_2,-SQRT_2_OVER_2, 0, 0},
  81. { -0.5, -0.5, -0.5, 0.5},
  82. { 0,-SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  83. { 0.5, -0.5, 0.5, 0.5},
  84. { SQRT_2_OVER_2, 0, SQRT_2_OVER_2, 0},
  85. { SQRT_2_OVER_2, 0,-SQRT_2_OVER_2, 0},
  86. { 0.5, 0.5, -0.5, 0.5},
  87. { 0, SQRT_2_OVER_2, 0, SQRT_2_OVER_2},
  88. { -0.5, 0.5, 0.5, 0.5},
  89. { 0, SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  90. { -0.5, 0.5, 0.5, -0.5},
  91. {-SQRT_2_OVER_2, 0, 0,-SQRT_2_OVER_2},
  92. { -0.5, -0.5, -0.5, -0.5},
  93. {-SQRT_2_OVER_2, 0, 0, SQRT_2_OVER_2},
  94. { -0.5, -0.5, 0.5, 0.5},
  95. { 0,-SQRT_2_OVER_2, SQRT_2_OVER_2, 0},
  96. { 0.5, -0.5, 0.5, -0.5}
  97. };
  98. #undef SQRT_2_OVER_2
  99. #define NUM_CANONICAL_VIEW_QUAT 24
  100. // NOTE: I want to rather be able to return a Q_type[][] but C++ is not
  101. // making it easy. So instead I've written a per-element accessor
  102. // Return element [i][j] of the corresponding CANONICAL_VIEW_QUAT_* of the
  103. // given templated type
  104. // Inputs:
  105. // i index of quaternion
  106. // j index of coordinate in quaternion i
  107. // Returns values of CANONICAL_VIEW_QUAT_*[i][j]
  108. template <typename Q_type>
  109. IGL_INLINE Q_type CANONICAL_VIEW_QUAT(int i, int j);
  110. // Template specializations for float and double
  111. template <>
  112. IGL_INLINE float CANONICAL_VIEW_QUAT<float>(int i, int j);
  113. template <>
  114. IGL_INLINE double CANONICAL_VIEW_QUAT<double>(int i, int j);
  115. }
  116. #ifndef IGL_STATIC_LIBRARY
  117. # include "canonical_quaternions.cpp"
  118. #endif
  119. #endif