llcoordframe.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file llcoordframe.h
  3. * @brief LLCoordFrame class header file.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_COORDFRAME_H
  33. #define LL_COORDFRAME_H
  34. #include "llvector3.h"
  35. #include "llvector4.h"
  36. #include "llerror.h"
  37. // The constructors of the LLCoordFrame class assume that all vectors and
  38. // quaternions being passed as arguments are normalized, and all matrix
  39. // arguments are unitary. VERY BAD things will happen if these assumptions
  40. // fail. Also, segfault hazzards exist in methods that accept F32* arguments.
  41. class LLCoordFrame
  42. {
  43. protected:
  44. LOG_CLASS(LLCoordFrame);
  45. public:
  46. // Inits at zero with identity rotation
  47. LL_INLINE LLCoordFrame() noexcept
  48. : mXAxis(1.f, 0.f, 0.f),
  49. mYAxis(0.f, 1.f, 0.f),
  50. mZAxis(0.f, 0.f, 1.f)
  51. {
  52. }
  53. // Sets origin, and inits rotation = Identity
  54. explicit LLCoordFrame(const LLVector3& origin) noexcept;
  55. // Sets coordinate axes and inits origin at zero
  56. LLCoordFrame(const LLVector3& x_axis, const LLVector3& y_axis,
  57. const LLVector3& z_axis) noexcept;
  58. // Sets the origin and coordinate axes
  59. LLCoordFrame(const LLVector3& origin, const LLVector3& x_axis,
  60. const LLVector3& y_axis, const LLVector3& z_axis) noexcept;
  61. // Sets axes to 3x3 matrix
  62. LLCoordFrame(const LLVector3& origin, const LLMatrix3& rotation) noexcept;
  63. // Sets origin and calls lookDir(direction)
  64. LLCoordFrame(const LLVector3& origin, const LLVector3& direction) noexcept;
  65. // Sets axes using q and inits mOrigin to zero
  66. explicit LLCoordFrame(const LLQuaternion& q) noexcept;
  67. // Uses quaternion to init axes
  68. LLCoordFrame(const LLVector3& origin, const LLQuaternion& q) noexcept;
  69. // Extracts frame from a 4x4 matrix
  70. explicit LLCoordFrame(const LLMatrix4& mat) noexcept;
  71. // Allow the use of the default C++11 move constructor and assignation
  72. LLCoordFrame(LLCoordFrame&& other) noexcept = default;
  73. LLCoordFrame& operator=(LLCoordFrame&& other) noexcept = default;
  74. LLCoordFrame(const LLCoordFrame& other) = default;
  75. LLCoordFrame& operator=(const LLCoordFrame& other) = default;
  76. #if 0 // The folowing two constructors are dangerous due to implicit casting
  77. // and have been disabled - SJB
  78. // Assumes "origin" is 1x3 and "rotation" is 1x9 array
  79. LLCoordFrame(const F32* origin, const F32* rotation) noexcept;
  80. // Assumes "origin_and_rotation" is 1x12 array
  81. LLCoordFrame(const F32* origin_and_rotation) noexcept;
  82. #endif
  83. virtual ~LLCoordFrame() = default;
  84. LL_INLINE bool isFinite()
  85. {
  86. return mOrigin.isFinite() &&
  87. mXAxis.isFinite() && mYAxis.isFinite() && mZAxis.isFinite();
  88. }
  89. void reset();
  90. void resetAxes();
  91. void setOrigin(F32 x, F32 y, F32 z); // Set mOrigin
  92. void setOrigin(const LLVector3& origin);
  93. void setOrigin(const F32* origin);
  94. void setOrigin(const LLCoordFrame &frame);
  95. LL_INLINE void setOriginX(F32 x) { mOrigin.mV[VX] = x; }
  96. LL_INLINE void setOriginY(F32 y) { mOrigin.mV[VY] = y; }
  97. LL_INLINE void setOriginZ(F32 z) { mOrigin.mV[VZ] = z; }
  98. void setAxes(const LLVector3& x_axis, const LLVector3& y_axis,
  99. const LLVector3& z_axis);
  100. void setAxes(const LLMatrix3 &rotation_matrix);
  101. void setAxes(const LLQuaternion& q);
  102. void setAxes(const F32* rotation_matrix);
  103. void setAxes(const LLCoordFrame &frame);
  104. void translate(F32 x, F32 y, F32 z); // Move mOrgin
  105. void translate(const LLVector3& v);
  106. void translate(const F32* origin);
  107. void rotate(F32 angle, F32 x, F32 y, F32 z); // Move axes
  108. void rotate(F32 angle, const LLVector3& rotation_axis);
  109. void rotate(const LLQuaternion& q);
  110. void rotate(const LLMatrix3 &m);
  111. void orthonormalize(); // Makes sure axes are unitary and orthogonal.
  112. // These methods allow rotations in the LLCoordFrame's frame
  113. void roll(F32 angle); // RH rotation about mXAxis, radians
  114. void pitch(F32 angle); // RH rotation about mYAxis, radians
  115. void yaw(F32 angle); // RH rotation about mZAxis, radians
  116. LL_INLINE const LLVector3& getOrigin() const { return mOrigin; }
  117. LL_INLINE const LLVector3& getXAxis() const { return mXAxis; }
  118. LL_INLINE const LLVector3& getYAxis() const { return mYAxis; }
  119. LL_INLINE const LLVector3& getZAxis() const { return mZAxis; }
  120. LL_INLINE const LLVector3& getAtAxis() const { return mXAxis; }
  121. LL_INLINE const LLVector3& getLeftAxis() const { return mYAxis; }
  122. LL_INLINE const LLVector3& getUpAxis() const { return mZAxis; }
  123. // These return representations of the rotation or orientation of the
  124. // LLFrame it its absolute frame. That is, these rotations acting on the
  125. // X-axis { 1, 0, 0 } will produce the mXAxis.
  126. #if 0
  127. LLMatrix3 getMatrix3() const; // Returns axes in 3x3 matrix
  128. #endif
  129. LLQuaternion getQuaternion() const; // Returns axes in quaternion form
  130. #if 0
  131. // Same as above, except it also includes the translation of the LLFrame
  132. LLMatrix4 getMatrix4() const; // Returns position and axes in 4x4 matrix
  133. #endif
  134. // Returns matrix which expresses point in local frame in the parent frame
  135. void getMatrixToParent(LLMatrix4& mat) const;
  136. // Returns matrix which expresses point in parent frame in the local frame
  137. void getMatrixToLocal(LLMatrix4& mat) const;
  138. void getRotMatrixToParent(LLMatrix4& mat) const;
  139. // Copies mOrigin, then the three axes to buffer, returns number of bytes
  140. // copied.
  141. size_t writeOrientation(char* buffer) const;
  142. // Copies mOrigin, then the three axes from buffer, returns the number of
  143. // bytes copied. Assumes the data in buffer is correct.
  144. size_t readOrientation(const char* buffer);
  145. LLVector3 rotateToLocal(const LLVector3& v) const; // Returns v' rotated to local
  146. LLVector4 rotateToLocal(const LLVector4& v) const; // Returns v' rotated to local
  147. LLVector3 rotateToAbsolute(const LLVector3& v) const; // Returns v' rotated to absolute
  148. LLVector4 rotateToAbsolute(const LLVector4& v) const; // Returns v' rotated to absolute
  149. LLVector3 transformToLocal(const LLVector3& v) const; // Returns v' in local coord
  150. LLVector4 transformToLocal(const LLVector4& v) const; // Returns v' in local coord
  151. LLVector3 transformToAbsolute(const LLVector3& v) const; // Returns v' in absolute coord
  152. LLVector4 transformToAbsolute(const LLVector4& v) const; // Returns v' in absolute coord
  153. // Write coord frame orientation into provided array in OpenGL matrix
  154. // format.
  155. void getOpenGLTranslation(F32* ogl_matrix) const;
  156. void getOpenGLRotation(F32* ogl_matrix) const;
  157. void getOpenGLTransform(F32* ogl_matrix) const;
  158. // lookDir orients to (xuv, presumed normalized) and does not affect origin
  159. void lookDir(const LLVector3& xuv, const LLVector3& up);
  160. void lookDir(const LLVector3& xuv); // up = 0,0,1
  161. // lookAt orients to (point_of_interest - origin) and sets origin
  162. void lookAt(const LLVector3& origin, const LLVector3& point_of_interest,
  163. const LLVector3& up);
  164. // up = 0,0,1
  165. void lookAt(const LLVector3& origin, const LLVector3& point_of_interest);
  166. // deprecated
  167. LL_INLINE void setOriginAndLookAt(const LLVector3& origin,
  168. const LLVector3& up,
  169. const LLVector3& point_of_interest)
  170. {
  171. lookAt(origin, point_of_interest, up);
  172. }
  173. friend std::ostream& operator<<(std::ostream &s, const LLCoordFrame &C);
  174. public:
  175. // These vectors are in absolute frame
  176. LLVector3 mOrigin;
  177. LLVector3 mXAxis;
  178. LLVector3 mYAxis;
  179. LLVector3 mZAxis;
  180. };
  181. #endif // LL_COORDFRAME_H