gwnavruntime/math/matrix3x3f.h Source File

matrix3x3f.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
25 {
26  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
27 
28 public:
29  // ------------------------------ Init ------------------------------
30 
32  Matrix3x3f() { SetIdentity(); }
33 
36  KyFloat32 Xx, KyFloat32 Xy, KyFloat32 Xz,
37  KyFloat32 Yx, KyFloat32 Yy, KyFloat32 Yz,
38  KyFloat32 Zx, KyFloat32 Zy, KyFloat32 Zz)
39  {
40  Set(Xx, Xy, Xz, Yx, Yy, Yz, Zx, Zy, Zz);
41  }
42 
44  Matrix3x3f(const Vec3f& X_, const Vec3f& Y_, const Vec3f& Z_) { Set(X_, Y_, Z_); }
45 
47  Matrix3x3f(const Vec3f& axis, KyFloat32 angleRad) { SetRotation(axis, angleRad); }
48 
49  void SetIdentity() { Set(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); }
50 
52  void Set(
53  KyFloat32 Xx, KyFloat32 Xy, KyFloat32 Xz,
54  KyFloat32 Yx, KyFloat32 Yy, KyFloat32 Yz,
55  KyFloat32 Zx, KyFloat32 Zy, KyFloat32 Zz)
56  {
57  X.x = Xx; X.y = Xy; X.z = Xz;
58  Y.x = Yx; Y.y = Yy; Y.z = Yz;
59  Z.x = Zx; Z.y = Zy; Z.z = Zz;
60  }
61 
63  void Set(const Vec3f& X_, const Vec3f& Y_, const Vec3f& Z_) { Set(X_.x, X_.y, X_.z, Y_.x, Y_.y, Y_.z, Z_.x, Z_.y, Z_.z); }
64 
66  void SetRotation(const Vec3f& axis, KyFloat32 angleInRadian);
67 
70  void SetYawPitchRoll(KyFloat32 yawInRadian, KyFloat32 pitchInRadian, KyFloat32 rollInRadian);
71 
72  // ------------------------------ Main interface ------------------------------
73 
74  const Vec3f& Axis(KyUInt32 index) const;
75  Vec3f& Axis(KyUInt32 index);
76 
77  Vec3f Transform(const Vec3f& to_transform) const;
78  Matrix3x3f Transform(const Matrix3x3f& to_transform) const;
79 
80  void Transform(const Vec3f& to_transform , Vec3f& transformed) const;
81  void Transform(const Matrix3x3f& to_transform, Matrix3x3f& transformed) const;
82 
83  bool operator==(const Matrix3x3f& other) const { return X == other.X && Y == other.Y && Z == other.Z; }
84  bool operator!=(const Matrix3x3f& other) const { return !operator==(other); }
85 
86  // ------------------------------ Static methods ------------------------------
87 
88  static Matrix3x3f Identity() { return Matrix3x3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); }
89 
92  static Matrix3x3f YawPitchRoll(KyFloat32 yawInRadian, KyFloat32 pitchInRadian, KyFloat32 rollInRadian)
93  {
94  Matrix3x3f M;
95  M.SetYawPitchRoll(yawInRadian, pitchInRadian, rollInRadian);
96  return M;
97  }
98 
100  static Matrix3x3f Rotation(const Vec3f& axis, KyFloat32 angleInRadian) { return Matrix3x3f(axis, angleInRadian); }
101 
102  // ------------------------------ Data ------------------------------
103 
107 };
108 
109 inline void SwapEndianness(Endianness::Target e, Matrix3x3f& self)
110 {
111  SwapEndianness(e, self.X);
112  SwapEndianness(e, self.Y);
113  SwapEndianness(e, self.Z);
114 }
115 
116 template <class OSTREAM>
117 inline OSTREAM& operator<<(OSTREAM& os, const Matrix3x3f& m)
118 {
119  os << "{ X " << m.X << '\n';
120  os << " Y " << m.Y << '\n';
121  os << " Z " << m.Z << " }" << '\n';
122  return os;
123 }
124 
125 inline const Vec3f& Matrix3x3f::Axis(KyUInt32 index) const
126 {
127  KY_ASSERT(index < 3);
128  return *(&X + index);
129 }
130 
132 {
133  KY_ASSERT(index < 3);
134  return *(&X + index);
135 }
136 
137 inline Vec3f Matrix3x3f::Transform(const Vec3f& V) const
138 {
139  return Vec3f(
140  X.x * V.x + Y.x * V.y + Z.x * V.z,
141  X.y * V.x + Y.y * V.y + Z.y * V.z,
142  X.z * V.x + Y.z * V.y + Z.z * V.z);
143 }
144 
145 inline void Matrix3x3f::Transform(const Vec3f& V, Vec3f& transformed) const
146 {
147  transformed.x = X.x * V.x + Y.x * V.y + Z.x * V.z;
148  transformed.y = X.y * V.x + Y.y * V.y + Z.y * V.z;
149  transformed.z = X.z * V.x + Y.z * V.y + Z.z * V.z;
150 }
151 
152 inline Matrix3x3f Matrix3x3f::Transform(const Matrix3x3f& to_transform) const
153 {
154  return Matrix3x3f(
155  Transform(to_transform.X),
156  Transform(to_transform.Y),
157  Transform(to_transform.Z));
158 }
159 
160 inline void Matrix3x3f::Transform(const Matrix3x3f& to_transform, Matrix3x3f& transformed) const
161 {
162  transformed.X = Transform(to_transform.X);
163  transformed.Y = Transform(to_transform.Y);
164  transformed.Z = Transform(to_transform.Z);
165 }
166 
167 }
168 
169 
Vec3f Transform(const Vec3f &to_transform) const
return transformed Vec3f
Definition: matrix3x3f.h:137
Matrix3x3f(KyFloat32 Xx, KyFloat32 Xy, KyFloat32 Xz, KyFloat32 Yx, KyFloat32 Yy, KyFloat32 Yz, KyFloat32 Zx, KyFloat32 Zy, KyFloat32 Zz)
{Xx,Xy,Xz}=Matrix.Transform({1,0,0}), {Yx,Yy,Yz}=Matrix.Transform({0,1,0}), {Zx,Zy,Zz}=Matrix.Transform({0,0,1})
Definition: matrix3x3f.h:35
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
void Set(KyFloat32 Xx, KyFloat32 Xy, KyFloat32 Xz, KyFloat32 Yx, KyFloat32 Yy, KyFloat32 Yz, KyFloat32 Zx, KyFloat32 Zy, KyFloat32 Zz)
{Xx,Xy,Xz}=Matrix.Transform({1,0,0}), {Yx,Yy,Yz}=Matrix.Transform({0,1,0}), {Zx,Zy,Zz}=Matrix.Transform({0,0,1})
Definition: matrix3x3f.h:52
Matrix3x3f()
Set Identity.
Definition: matrix3x3f.h:32
static Matrix3x3f Rotation(const Vec3f &axis, KyFloat32 angleInRadian)
Return Matrix as the rotation around axis.
Definition: matrix3x3f.h:100
Vec3f Y
Matrix.Transfrom(Vec3f(0,1,0))
Definition: matrix3x3f.h:105
Matrix3x3f is defined as 3 Transformed Unit Vectors:
Definition: matrix3x3f.h:24
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Matrix3x3f(const Vec3f &X_, const Vec3f &Y_, const Vec3f &Z_)
X=Matrix.Transform({1,0,0}), Y=Matrix.Transform({0,1,0}), Z=Matrix.Transform({0,0,1})
Definition: matrix3x3f.h:44
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
void Set(const Vec3f &X_, const Vec3f &Y_, const Vec3f &Z_)
X=Matrix.Transform({1,0,0}), Y=Matrix.Transform({0,1,0}), Z=Matrix.Transform({0,0,1})
Definition: matrix3x3f.h:63
void SetYawPitchRoll(KyFloat32 yawInRadian, KyFloat32 pitchInRadian, KyFloat32 rollInRadian)
Return yawRot.Transform(pitchRot.Transform(rollRot)) where yawRot around Z, pitchRot around X...
Definition: matrix3x3f.cpp:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Matrix3x3f(const Vec3f &axis, KyFloat32 angleRad)
Set Matrix as the CCW rotation around axis.
Definition: matrix3x3f.h:47
Vec3f Z
Matrix.Transfrom(Vec3f(0,0,1))
Definition: matrix3x3f.h:106
Matrix3x3f rotation and Vec3f translation.
Definition: transform.h:16
static Matrix3x3f YawPitchRoll(KyFloat32 yawInRadian, KyFloat32 pitchInRadian, KyFloat32 rollInRadian)
Return yawRot.Transform(pitchRot.Transform(rollRot)) where yawRot around Z, pitchRot around X...
Definition: matrix3x3f.h:92
void SetRotation(const Vec3f &axis, KyFloat32 angleInRadian)
Set Matrix as the CCW rotation around axis.
Definition: matrix3x3f.cpp:12
Vec3f X
Matrix.Transfrom(Vec3f(1,0,0))
Definition: matrix3x3f.h:104
const Vec3f & Axis(KyUInt32 index) const
Axis(0) return M.X, Axis(1) return M.Y, Axis(2) return M.Z.
Definition: matrix3x3f.h:125
float KyFloat32
float
Definition: types.h:32
3d vector using 32bits floating points.
Definition: vec3f.h:16