TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CoordinateFrame.h
Go to the documentation of this file.
1 
13 #ifndef G3D_CFrame_h
14 #define G3D_CFrame_h
15 
16 #include "G3D/platform.h"
17 #include "G3D/Vector3.h"
18 #include "G3D/Vector4.h"
19 #include "G3D/Matrix3.h"
20 #include "G3D/Array.h"
21 #include <math.h>
22 #include <string>
23 #include <stdio.h>
24 #include <cstdarg>
25 #include <assert.h>
26 
27 #ifdef _MSC_VER
28 // Turn off "conditional expression is constant" warning; MSVC generates this
29 // for debug assertions in inlined methods.
30 # pragma warning (disable : 4127)
31 #endif
32 
33 
34 namespace G3D {
35 class Any;
36 class Frustum;
37 
60 public:
61 
64 
67 
75  CoordinateFrame(const Any& any);
76 
78  Any toAny() const;
79 
80  inline bool operator==(const CoordinateFrame& other) const {
81  return (translation == other.translation) && (rotation == other.rotation);
82  }
83 
84  inline bool operator!=(const CoordinateFrame& other) const {
85  return !(*this == other);
86  }
87 
88  bool fuzzyEq(const CoordinateFrame& other) const;
89 
90  bool fuzzyIsIdentity() const;
91 
92  bool isIdentity() const;
93 
98 
99  CoordinateFrame(const Point3& _translation) :
100  rotation(Matrix3::identity()), translation(_translation) {
101  }
102 
103  CoordinateFrame(const Matrix3& rotation, const Point3& translation) :
104  rotation(rotation), translation(translation) {
105  }
106 
107  CoordinateFrame(const Matrix3& rotation) :
108  rotation(rotation), translation(Point3::zero()) {
109  }
110 
111  CoordinateFrame(const class UprightFrame& f);
112 
113  static CoordinateFrame fromXYZYPRRadians(float x, float y, float z, float yaw = 0.0f, float pitch = 0.0f, float roll = 0.0f);
114 
115  std::string toXYZYPRDegreesString() const;
116 
122  static CoordinateFrame fromXYZYPRDegrees(float x, float y, float z, float yaw = 0.0f, float pitch = 0.0f, float roll = 0.0f);
123 
124  CoordinateFrame(class BinaryInput& b);
125 
126  void deserialize(class BinaryInput& b);
127 
128  void serialize(class BinaryOutput& b) const;
129 
131  rotation(other.rotation), translation(other.translation) {}
132 
136  inline CoordinateFrame inverse() const {
137  CoordinateFrame out;
138  out.rotation = rotation.transpose();
139  out.translation = -out.rotation * translation;
140  return out;
141  }
142 
143  inline ~CoordinateFrame() {}
144 
146  class Matrix4 toMatrix4() const;
147 
148  void getXYZYPRRadians(float& x, float& y, float& z, float& yaw, float& pitch, float& roll) const;
149  void getXYZYPRDegrees(float& x, float& y, float& z, float& yaw, float& pitch, float& roll) const;
150 
151 
156  std::string toXML() const;
157 
166  inline float getHeading() const {
167  Vector3 look = rotation.column(2);
168  float angle = -(float) atan2(-look.x, look.z);
169  return angle;
170  }
171 
177  return this->inverse() * c;
178  }
179 
180  inline Vector4 toObjectSpace(const Vector4& v) const {
181  return this->inverse().toWorldSpace(v);
182  }
183 
184  inline Vector4 toWorldSpace(const Vector4& v) const {
185  return Vector4(rotation * Vector3(v.x, v.y, v.z) + translation * v.w, v.w);
186  }
187 
191  inline Point3 pointToWorldSpace(const Point3& v) const {
192  return Point3
193  (rotation[0][0] * v[0] + rotation[0][1] * v[1] + rotation[0][2] * v[2] + translation[0],
194  rotation[1][0] * v[0] + rotation[1][1] * v[1] + rotation[1][2] * v[2] + translation[1],
195  rotation[2][0] * v[0] + rotation[2][1] * v[1] + rotation[2][2] * v[2] + translation[2]);
196  }
197 
201  inline Point3 pointToObjectSpace(const Point3& v) const {
202  float p[3];
203  p[0] = v[0] - translation[0];
204  p[1] = v[1] - translation[1];
205  p[2] = v[2] - translation[2];
206  debugAssert(G3D::fuzzyEq(fabsf(rotation.determinant()), 1.0f));
207  return Point3(rotation[0][0] * p[0] + rotation[1][0] * p[1] + rotation[2][0] * p[2],
208  rotation[0][1] * p[0] + rotation[1][1] * p[1] + rotation[2][1] * p[2],
209  rotation[0][2] * p[0] + rotation[1][2] * p[1] + rotation[2][2] * p[2]);
210  }
211 
215  inline Vector3 vectorToWorldSpace(const Vector3& v) const {
216  return rotation * v;
217  }
218 
219  inline Vector3 normalToWorldSpace(const Vector3& v) const {
220  return rotation * v;
221  }
222 
223  class Ray toObjectSpace(const Ray& r) const;
224 
225  Ray toWorldSpace(const Ray& r) const;
226 
227  Frustum toWorldSpace(const Frustum& f) const;
228 
232  inline Vector3 vectorToObjectSpace(const Vector3 &v) const {
233  // Multiply on the left (same as rotation.transpose() * v)
234  return v * rotation;
235  }
236 
237  inline Vector3 normalToObjectSpace(const Vector3 &v) const {
238  // Multiply on the left (same as rotation.transpose() * v)
239  return v * rotation;
240  }
241 
242  void pointToWorldSpace(const Array<Point3>& v, Array<Point3>& vout) const;
243 
244  void normalToWorldSpace(const Array<Vector3>& v, Array<Vector3>& vout) const;
245 
246  void vectorToWorldSpace(const Array<Vector3>& v, Array<Vector3>& vout) const;
247 
248  void pointToObjectSpace(const Array<Point3>& v, Array<Point3>& vout) const;
249 
250  void normalToObjectSpace(const Array<Vector3>& v, Array<Vector3>& vout) const;
251 
252  void vectorToObjectSpace(const Array<Vector3>& v, Array<Vector3>& vout) const;
253 
254  void toWorldSpace(const class AABox& b, class AABox& result) const;
255 
256  class Box toWorldSpace(const class AABox& b) const;
257 
258  class Box toWorldSpace(const class Box& b) const;
259 
260  class Cylinder toWorldSpace(const class Cylinder& b) const;
261 
262  class Capsule toWorldSpace(const class Capsule& b) const;
263 
264  class Plane toWorldSpace(const class Plane& p) const;
265 
266  class Sphere toWorldSpace(const class Sphere& b) const;
267 
268  class Triangle toWorldSpace(const class Triangle& t) const;
269 
270  class Box toObjectSpace(const AABox& b) const;
271 
272  class Box toObjectSpace(const Box& b) const;
273 
274  class Plane toObjectSpace(const Plane& p) const;
275 
276  class Sphere toObjectSpace(const Sphere& b) const;
277 
278  Triangle toObjectSpace(const Triangle& t) const;
279 
282  return CoordinateFrame(rotation * other.rotation,
284  }
285 
287  return CoordinateFrame(rotation, translation + v);
288  }
289 
291  return CoordinateFrame(rotation, translation - v);
292  }
293 
294 
307  void moveTowards(const CoordinateFrame& goal, float maxTranslation, float maxRotation);
308 
309  void lookAt(const Point3& target);
310 
311  void lookAt
312  (const Point3& target,
313  Vector3 up);
314 
316  inline Vector3 lookVector() const {
317  return -rotation.column(2);
318  }
319 
321  class Ray lookRay() const;
322 
324  inline Vector3 upVector() const {
325  return rotation.column(1);
326  }
327 
328  inline Vector3 rightVector() const {
329  return rotation.column(0);
330  }
331 
336  inline Vector3 leftVector() const {
337  return -rotation.column(0);
338  }
339 
347  (const CoordinateFrame& other,
348  float alpha) const;
349 
350 };
351 
352 typedef CoordinateFrame CFrame;
353 
354 } // namespace
355 
356 #endif
CoordinateFrame(const CoordinateFrame &other)
Definition: CoordinateFrame.h:130
std::string toXML() const
Definition: CoordinateFrame.cpp:200
float x
Definition: Vector3.h:62
Definition: Frustum.h:24
Vector3 upVector() const
Definition: CoordinateFrame.h:324
Definition: Plane.h:25
CoordinateFrame lerp(const CoordinateFrame &other, float alpha) const
Definition: CoordinateFrame.cpp:419
CoordinateFrame toObjectSpace(const CoordinateFrame &c) const
Definition: CoordinateFrame.h:176
bool isIdentity() const
Definition: CoordinateFrame.cpp:188
static CoordinateFrame fromXYZYPRDegrees(float x, float y, float z, float yaw=0.0f, float pitch=0.0f, float roll=0.0f)
Definition: CoordinateFrame.cpp:142
bool operator!=(const CoordinateFrame &other) const
Definition: CoordinateFrame.h:84
Definition: BinaryInput.h:69
Vector3 rightVector() const
Definition: CoordinateFrame.h:328
float w
Definition: Vector4.h:74
A rigid body RT (rotation-translation) transformation.
Definition: CoordinateFrame.h:59
~CoordinateFrame()
Definition: CoordinateFrame.h:143
class CoordinateFrame CFrame
Definition: Box2D.h:22
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
bool any(float x)
Definition: g3dmath.h:424
bool fuzzyIsIdentity() const
Definition: CoordinateFrame.cpp:170
Matrix3 rotation
Definition: CoordinateFrame.h:63
Vector4 toObjectSpace(const Vector4 &v) const
Definition: CoordinateFrame.h:180
bool operator==(const CoordinateFrame &other) const
Definition: CoordinateFrame.h:80
void getXYZYPRRadians(float &x, float &y, float &z, float &yaw, float &pitch, float &roll) const
Definition: CoordinateFrame.cpp:123
An arbitrary (oriented) 3D box, useful as a bounding box.
Definition: Box.h:35
float x
Definition: Vector4.h:74
Definition: Vector3.h:58
Vector3 leftVector() const
Definition: CoordinateFrame.h:336
CoordinateFrame(const Matrix3 &rotation)
Definition: CoordinateFrame.h:107
void moveTowards(const CoordinateFrame &goal, float maxTranslation, float maxRotation)
Definition: CoordinateFrame.cpp:411
CoordinateFrame operator+(const Vector3 &v) const
Definition: CoordinateFrame.h:286
Definition: Sphere.h:24
Definition: Matrix4.h:36
Easy loading and saving of human-readable configuration files.
Definition: Any.h:184
class Ray lookRay() const
Definition: CoordinateFrame.cpp:148
float getHeading() const
Definition: CoordinateFrame.h:166
void lookAt(const Point3 &target)
Definition: CoordinateFrame.cpp:377
CoordinateFrame()
Definition: CoordinateFrame.cpp:110
CoordinateFrame inverse() const
Definition: CoordinateFrame.h:136
#define debugAssert(exp)
Definition: debugAssert.h:160
Definition: Cylinder.h:27
G3D::int16 z
Definition: Vector3int16.h:46
Definition: Capsule.h:27
Vector4 toWorldSpace(const Vector4 &v) const
Definition: CoordinateFrame.h:184
CoordinateFrame(const Matrix3 &rotation, const Point3 &translation)
Definition: CoordinateFrame.h:103
void deserialize(class BinaryInput &b)
Definition: CoordinateFrame.cpp:345
G3D::int16 y
Definition: Vector2int16.h:38
Definition: Vector4.h:39
Vector3 lookVector() const
Definition: CoordinateFrame.h:316
CoordinateFrame(const Point3 &_translation)
Definition: CoordinateFrame.h:99
Definition: Ray.h:24
Definition: AABox.h:32
class Matrix4 toMatrix4() const
Definition: CoordinateFrame.cpp:195
float z
Definition: Vector4.h:74
Definition: Matrix3.h:37
Vector3 normalToObjectSpace(const Vector3 &v) const
Definition: CoordinateFrame.h:237
static void transpose(const Matrix3 &A, Matrix3 &out)
Definition: Matrix3.h:233
Vector3 vectorToObjectSpace(const Vector3 &v) const
Definition: CoordinateFrame.h:232
CoordinateFrame operator*(const CoordinateFrame &other) const
Definition: CoordinateFrame.h:281
Vector3 column(int c) const
Definition: Matrix3.cpp:203
float z
Definition: Vector3.h:62
std::string toXYZYPRDegreesString() const
Definition: CoordinateFrame.cpp:36
static unorm16 zero()
Definition: unorm16.h:82
Definition: BinaryOutput.h:52
void serialize(class BinaryOutput &b) const
Definition: CoordinateFrame.cpp:351
Any toAny() const
Definition: CoordinateFrame.cpp:87
static CoordinateFrame fromXYZYPRRadians(float x, float y, float z, float yaw=0.0f, float pitch=0.0f, float roll=0.0f)
Definition: CoordinateFrame.cpp:114
float y
Definition: Vector4.h:74
CoordinateFrame operator-(const Vector3 &v) const
Definition: CoordinateFrame.h:290
#define const
Definition: zconf.h:217
G3D::int16 x
Definition: Vector2int16.h:37
Coordinate frame expressed in Euler angles. Unlike a G3D::Quat, UprightFrame always keeps the referen...
Definition: UprightFrame.h:23
void getXYZYPRDegrees(float &x, float &y, float &z, float &yaw, float &pitch, float &roll) const
Definition: CoordinateFrame.cpp:133
Vector3 vectorToWorldSpace(const Vector3 &v) const
Definition: CoordinateFrame.h:215
bool fuzzyEq(const CoordinateFrame &other) const
Definition: CoordinateFrame.cpp:153
Vector3 Point3
Definition: Vector3.h:820
Definition: Triangle.h:34
Point3 pointToWorldSpace(const Point3 &v) const
Definition: CoordinateFrame.h:191
bool fuzzyEq(double a, double b)
Definition: g3dmath.h:857
float determinant() const
Definition: Matrix3.cpp:453
Point3 pointToObjectSpace(const Point3 &v) const
Definition: CoordinateFrame.h:201
Point3 translation
Definition: CoordinateFrame.h:66
Vector3 normalToWorldSpace(const Vector3 &v) const
Definition: CoordinateFrame.h:219