PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 27#ifndef LL_COORDFRAME_H
 28#define LL_COORDFRAME_H
 29
 30#include "v3math.h"
 31#include "v4math.h"
 32#include "llerror.h"
 33
 34// XXX : The constructors of the LLCoordFrame class assume that all vectors
 35//		 and quaternion being passed as arguments are normalized, and all matrix 
 36// 		 arguments are unitary.  VERY BAD things will happen if these assumptions fail.
 37//		 Also, segfault hazzards exist in methods that accept F32* arguments.
 38
 39
 40class LLCoordFrame 
 41{
 42public:
 43	LLCoordFrame();											// Inits at zero with identity rotation
 44	explicit LLCoordFrame(const LLVector3 &origin);			// Sets origin, and inits rotation = Identity
 45	LLCoordFrame(const LLVector3 &x_axis, 
 46				 const LLVector3 &y_axis, 
 47				 const LLVector3 &z_axis);					// Sets coordinate axes and inits origin at zero
 48	LLCoordFrame(const LLVector3 &origin, 
 49				 const LLVector3 &x_axis, 
 50				 const LLVector3 &y_axis, 
 51				 const LLVector3 &z_axis);					// Sets the origin and coordinate axes
 52	LLCoordFrame(const LLVector3 &origin, 
 53				 const LLMatrix3 &rotation);				// Sets axes to 3x3 matrix
 54	LLCoordFrame(const LLVector3 &origin, 
 55				 const LLVector3 &direction);				// Sets origin and calls lookDir(direction)
 56	explicit LLCoordFrame(const LLQuaternion &q);			// Sets axes using q and inits mOrigin to zero 
 57	LLCoordFrame(const LLVector3 &origin, 
 58				 const LLQuaternion &q);					// Uses quaternion to init axes
 59	explicit LLCoordFrame(const LLMatrix4 &mat);			// Extracts frame from a 4x4 matrix
 60	// The folowing two constructors are dangerous due to implicit casting and have been disabled - SJB
 61	//LLCoordFrame(const F32 *origin, const F32 *rotation);	// Assumes "origin" is 1x3 and "rotation" is 1x9 array
 62	//LLCoordFrame(const F32 *origin_and_rotation);			// Assumes "origin_and_rotation" is 1x12 array
 63
 64	BOOL isFinite() { return mOrigin.isFinite() && mXAxis.isFinite() && mYAxis.isFinite() && mZAxis.isFinite(); }
 65
 66	void reset();
 67	void resetAxes();
 68
 69	void setOrigin(F32 x, F32 y, F32 z);					// Set mOrigin
 70	void setOrigin(const LLVector3 &origin);
 71	void setOrigin(const F32 *origin);
 72	void setOrigin(const LLCoordFrame &frame);
 73
 74	inline void setOriginX(F32 x) { mOrigin.mV[VX] = x; }
 75	inline void setOriginY(F32 y) { mOrigin.mV[VY] = y; }
 76	inline void setOriginZ(F32 z) { mOrigin.mV[VZ] = z; }
 77
 78	void setAxes(const LLVector3 &x_axis, 					// Set axes
 79				 const LLVector3 &y_axis, 
 80				 const LLVector3 &z_axis);
 81	void setAxes(const LLMatrix3 &rotation_matrix);
 82	void setAxes(const LLQuaternion &q);
 83	void setAxes(const F32 *rotation_matrix);
 84	void setAxes(const LLCoordFrame &frame);
 85
 86	void translate(F32 x, F32 y, F32 z);					// Move mOrgin
 87	void translate(const LLVector3 &v);
 88	void translate(const F32 *origin);
 89
 90	void rotate(F32 angle, F32 x, F32 y, F32 z);			// Move axes
 91	void rotate(F32 angle, const LLVector3 &rotation_axis);
 92	void rotate(const LLQuaternion &q);
 93	void rotate(const LLMatrix3 &m);
 94
 95	void orthonormalize();	// Makes sure axes are unitary and orthogonal.
 96
 97	// These methods allow rotations in the LLCoordFrame's frame
 98	void roll(F32 angle);		// RH rotation about mXAxis, radians
 99	void pitch(F32 angle);		// RH rotation about mYAxis, radians
100	void yaw(F32 angle);		// RH rotation about mZAxis, radians
101
102	inline const LLVector3 &getOrigin() const { return mOrigin; }
103
104	inline const LLVector3 &getXAxis() const  { return mXAxis; }
105	inline const LLVector3 &getYAxis() const  { return mYAxis; }
106	inline const LLVector3 &getZAxis() const  { return mZAxis; }
107
108	inline const LLVector3 &getAtAxis() const   { return mXAxis; }
109	inline const LLVector3 &getLeftAxis() const { return mYAxis; }
110	inline const LLVector3 &getUpAxis() const   { return mZAxis; }
111	
112	// These return representations of the rotation or orientation of the LLFrame
113	// it its absolute frame.  That is, these rotations acting on the X-axis {1,0,0}
114	// will produce the mXAxis.
115	//		LLMatrix3 getMatrix3() const;				// Returns axes in 3x3 matrix 
116	LLQuaternion getQuaternion() const;			// Returns axes in quaternion form
117
118	// Same as above, except it also includes the translation of the LLFrame
119	//		LLMatrix4 getMatrix4() const;				// Returns position and axes in 4x4 matrix
120
121	// Returns matrix which expresses point in local frame in the parent frame
122	void getMatrixToParent(LLMatrix4 &mat) const;
123	// Returns matrix which expresses point in parent frame in the local frame
124	void getMatrixToLocal(LLMatrix4 &mat) const; // Returns matrix which expresses point in parent frame in the local frame
125
126	void getRotMatrixToParent(LLMatrix4 &mat) const;
127
128	// Copies mOrigin, then the three axes to buffer, returns number of bytes copied.
129	size_t writeOrientation(char *buffer) const;
130		
131	// Copies mOrigin, then the three axes from buffer, returns the number of bytes copied.
132	// Assumes the data in buffer is correct.
133	size_t readOrientation(const char *buffer);
134
135	LLVector3 rotateToLocal(const LLVector3 &v) const;		// Returns v' rotated to local
136	LLVector4 rotateToLocal(const LLVector4 &v) const;		// Returns v' rotated to local
137	LLVector3 rotateToAbsolute(const LLVector3 &v) const;	// Returns v' rotated to absolute
138	LLVector4 rotateToAbsolute(const LLVector4 &v) const;	// Returns v' rotated to absolute
139
140	LLVector3 transformToLocal(const LLVector3 &v) const;		// Returns v' in local coord
141	LLVector4 transformToLocal(const LLVector4 &v) const;		// Returns v' in local coord
142	LLVector3 transformToAbsolute(const LLVector3 &v) const;	// Returns v' in absolute coord
143	LLVector4 transformToAbsolute(const LLVector4 &v) const;	// Returns v' in absolute coord
144
145	// Write coord frame orientation into provided array in OpenGL matrix format.
146	void getOpenGLTranslation(F32 *ogl_matrix) const;
147	void getOpenGLRotation(F32 *ogl_matrix) const;
148	void getOpenGLTransform(F32 *ogl_matrix) const;
149
150	// lookDir orients to (xuv, presumed normalized) and does not affect origin
151	void lookDir(const LLVector3 &xuv, const LLVector3 &up);
152	void lookDir(const LLVector3 &xuv); // up = 0,0,1
153	// lookAt orients to (point_of_interest - origin) and sets origin
154	void lookAt(const LLVector3 &origin, const LLVector3 &point_of_interest, const LLVector3 &up);
155	void lookAt(const LLVector3 &origin, const LLVector3 &point_of_interest); // up = 0,0,1
156
157	// deprecated
158	void setOriginAndLookAt(const LLVector3 &origin, const LLVector3 &up, const LLVector3 &point_of_interest)
159	{
160		lookAt(origin, point_of_interest, up);
161	}
162	
163	friend std::ostream& operator<<(std::ostream &s, const LLCoordFrame &C);
164
165	// These vectors are in absolute frame
166	LLVector3 mOrigin;
167	LLVector3 mXAxis;
168	LLVector3 mYAxis;
169	LLVector3 mZAxis;
170};
171
172
173#endif
174