canonical_quaternions.h 5.9 KB

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