PageRenderTime 36ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llcoordframe.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 174 lines | 92 code | 33 blank | 49 comment | 3 complexity | 0319763dc9330031a6d90549f0cbbd00 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llcoordframe.h
  3. * @brief LLCoordFrame class header file.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_COORDFRAME_H
  27. #define LL_COORDFRAME_H
  28. #include "v3math.h"
  29. #include "v4math.h"
  30. #include "llerror.h"
  31. // XXX : The constructors of the LLCoordFrame class assume that all vectors
  32. // and quaternion being passed as arguments are normalized, and all matrix
  33. // arguments are unitary. VERY BAD things will happen if these assumptions fail.
  34. // Also, segfault hazzards exist in methods that accept F32* arguments.
  35. class LLCoordFrame
  36. {
  37. public:
  38. LLCoordFrame(); // Inits at zero with identity rotation
  39. explicit LLCoordFrame(const LLVector3 &origin); // Sets origin, and inits rotation = Identity
  40. LLCoordFrame(const LLVector3 &x_axis,
  41. const LLVector3 &y_axis,
  42. const LLVector3 &z_axis); // Sets coordinate axes and inits origin at zero
  43. LLCoordFrame(const LLVector3 &origin,
  44. const LLVector3 &x_axis,
  45. const LLVector3 &y_axis,
  46. const LLVector3 &z_axis); // Sets the origin and coordinate axes
  47. LLCoordFrame(const LLVector3 &origin,
  48. const LLMatrix3 &rotation); // Sets axes to 3x3 matrix
  49. LLCoordFrame(const LLVector3 &origin,
  50. const LLVector3 &direction); // Sets origin and calls lookDir(direction)
  51. explicit LLCoordFrame(const LLQuaternion &q); // Sets axes using q and inits mOrigin to zero
  52. LLCoordFrame(const LLVector3 &origin,
  53. const LLQuaternion &q); // Uses quaternion to init axes
  54. explicit LLCoordFrame(const LLMatrix4 &mat); // Extracts frame from a 4x4 matrix
  55. // The folowing two constructors are dangerous due to implicit casting and have been disabled - SJB
  56. //LLCoordFrame(const F32 *origin, const F32 *rotation); // Assumes "origin" is 1x3 and "rotation" is 1x9 array
  57. //LLCoordFrame(const F32 *origin_and_rotation); // Assumes "origin_and_rotation" is 1x12 array
  58. BOOL isFinite() { return mOrigin.isFinite() && mXAxis.isFinite() && mYAxis.isFinite() && mZAxis.isFinite(); }
  59. void reset();
  60. void resetAxes();
  61. void setOrigin(F32 x, F32 y, F32 z); // Set mOrigin
  62. void setOrigin(const LLVector3 &origin);
  63. void setOrigin(const F32 *origin);
  64. void setOrigin(const LLCoordFrame &frame);
  65. inline void setOriginX(F32 x) { mOrigin.mV[VX] = x; }
  66. inline void setOriginY(F32 y) { mOrigin.mV[VY] = y; }
  67. inline void setOriginZ(F32 z) { mOrigin.mV[VZ] = z; }
  68. void setAxes(const LLVector3 &x_axis, // Set axes
  69. const LLVector3 &y_axis,
  70. const LLVector3 &z_axis);
  71. void setAxes(const LLMatrix3 &rotation_matrix);
  72. void setAxes(const LLQuaternion &q);
  73. void setAxes(const F32 *rotation_matrix);
  74. void setAxes(const LLCoordFrame &frame);
  75. void translate(F32 x, F32 y, F32 z); // Move mOrgin
  76. void translate(const LLVector3 &v);
  77. void translate(const F32 *origin);
  78. void rotate(F32 angle, F32 x, F32 y, F32 z); // Move axes
  79. void rotate(F32 angle, const LLVector3 &rotation_axis);
  80. void rotate(const LLQuaternion &q);
  81. void rotate(const LLMatrix3 &m);
  82. void orthonormalize(); // Makes sure axes are unitary and orthogonal.
  83. // These methods allow rotations in the LLCoordFrame's frame
  84. void roll(F32 angle); // RH rotation about mXAxis, radians
  85. void pitch(F32 angle); // RH rotation about mYAxis, radians
  86. void yaw(F32 angle); // RH rotation about mZAxis, radians
  87. inline const LLVector3 &getOrigin() const { return mOrigin; }
  88. inline const LLVector3 &getXAxis() const { return mXAxis; }
  89. inline const LLVector3 &getYAxis() const { return mYAxis; }
  90. inline const LLVector3 &getZAxis() const { return mZAxis; }
  91. inline const LLVector3 &getAtAxis() const { return mXAxis; }
  92. inline const LLVector3 &getLeftAxis() const { return mYAxis; }
  93. inline const LLVector3 &getUpAxis() const { return mZAxis; }
  94. // These return representations of the rotation or orientation of the LLFrame
  95. // it its absolute frame. That is, these rotations acting on the X-axis {1,0,0}
  96. // will produce the mXAxis.
  97. // LLMatrix3 getMatrix3() const; // Returns axes in 3x3 matrix
  98. LLQuaternion getQuaternion() const; // Returns axes in quaternion form
  99. // Same as above, except it also includes the translation of the LLFrame
  100. // LLMatrix4 getMatrix4() const; // Returns position and axes in 4x4 matrix
  101. // Returns matrix which expresses point in local frame in the parent frame
  102. void getMatrixToParent(LLMatrix4 &mat) const;
  103. // Returns matrix which expresses point in parent frame in the local frame
  104. void getMatrixToLocal(LLMatrix4 &mat) const; // Returns matrix which expresses point in parent frame in the local frame
  105. void getRotMatrixToParent(LLMatrix4 &mat) const;
  106. // Copies mOrigin, then the three axes to buffer, returns number of bytes copied.
  107. size_t writeOrientation(char *buffer) const;
  108. // Copies mOrigin, then the three axes from buffer, returns the number of bytes copied.
  109. // Assumes the data in buffer is correct.
  110. size_t readOrientation(const char *buffer);
  111. LLVector3 rotateToLocal(const LLVector3 &v) const; // Returns v' rotated to local
  112. LLVector4 rotateToLocal(const LLVector4 &v) const; // Returns v' rotated to local
  113. LLVector3 rotateToAbsolute(const LLVector3 &v) const; // Returns v' rotated to absolute
  114. LLVector4 rotateToAbsolute(const LLVector4 &v) const; // Returns v' rotated to absolute
  115. LLVector3 transformToLocal(const LLVector3 &v) const; // Returns v' in local coord
  116. LLVector4 transformToLocal(const LLVector4 &v) const; // Returns v' in local coord
  117. LLVector3 transformToAbsolute(const LLVector3 &v) const; // Returns v' in absolute coord
  118. LLVector4 transformToAbsolute(const LLVector4 &v) const; // Returns v' in absolute coord
  119. // Write coord frame orientation into provided array in OpenGL matrix format.
  120. void getOpenGLTranslation(F32 *ogl_matrix) const;
  121. void getOpenGLRotation(F32 *ogl_matrix) const;
  122. void getOpenGLTransform(F32 *ogl_matrix) const;
  123. // lookDir orients to (xuv, presumed normalized) and does not affect origin
  124. void lookDir(const LLVector3 &xuv, const LLVector3 &up);
  125. void lookDir(const LLVector3 &xuv); // up = 0,0,1
  126. // lookAt orients to (point_of_interest - origin) and sets origin
  127. void lookAt(const LLVector3 &origin, const LLVector3 &point_of_interest, const LLVector3 &up);
  128. void lookAt(const LLVector3 &origin, const LLVector3 &point_of_interest); // up = 0,0,1
  129. // deprecated
  130. void setOriginAndLookAt(const LLVector3 &origin, const LLVector3 &up, const LLVector3 &point_of_interest)
  131. {
  132. lookAt(origin, point_of_interest, up);
  133. }
  134. friend std::ostream& operator<<(std::ostream &s, const LLCoordFrame &C);
  135. // These vectors are in absolute frame
  136. LLVector3 mOrigin;
  137. LLVector3 mXAxis;
  138. LLVector3 mYAxis;
  139. LLVector3 mZAxis;
  140. };
  141. #endif