Berserk
Transformf.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_TRANSFORMF_H
29 #define BERSERK_TRANSFORMF_H
30 
31 #include <core/Config.hpp>
33 #include <core/math/TMatMxN.hpp>
34 #include <core/math/TQuat.hpp>
35 #include <core/math/TVecN.hpp>
36 
38 
58 class Transformf {
59 public:
60  Transformf() = default;
61 
62  Transformf &RotateX(float angleRad) {
63  return Rotate(Vec3f::AxisX(), angleRad);
64  }
65 
66  Transformf &RotateY(float angleRad) {
67  return Rotate(Vec3f::AxisY(), angleRad);
68  }
69 
70  Transformf &RotateZ(float angleRad) {
71  return Rotate(Vec3f::AxisZ(), angleRad);
72  }
73 
74  Transformf &Rotate(const Vec3f &axis, float angleRad) {
75  mRotation = mRotation * Quatf(axis, angleRad);
76  return *this;
77  }
78 
79  Transformf &ScaleX(float scale) {
80  mScale[INDEX_X] *= scale;
81  return *this;
82  }
83 
84  Transformf &ScaleY(float scale) {
85  mScale[INDEX_Y] *= scale;
86  return *this;
87  }
88 
89  Transformf &ScaleZ(float scale) {
90  mScale[INDEX_Z] *= scale;
91  return *this;
92  }
93 
94  Transformf &Scale(const Vec3f &scale) {
95  mScale *= scale;
96  return *this;
97  }
98 
99  Transformf &Scale(float scale) {
100  mScale *= scale;
101  return *this;
102  }
103 
105  mOffset += t;
106  return *this;
107  }
108 
109  Vec3f Transform(const Vec3f &v) const {
110  auto rotated = mRotation.Rotate(v);
111  auto scaled = rotated * mScale;
112  auto translated = scaled + mOffset;
113  return translated;
114  }
115 
116  Vec3f TransformAsNormal(const Vec3f &v) const {
117  auto rotated = mRotation.Rotate(v);
118  auto scaled = rotated * (Vec3f(1.0f, 1.0f, 1.0f) / mScale);
119  return scaled;
120  }
121 
124  auto rotation = mRotation.AsMatrix();
125  auto scale = MathUtils3d::Scale(mScale);
126  auto translation = MathUtils3d::Translate(mOffset);
127  return translation * scale * rotation;
128  }
129 
135  auto rotation = mRotation.Conjugate().AsMatrix();
136  auto scale = MathUtils3d::Scale(Vec3f(1.0f, 1.0f, 1.0f) / mScale);
137  auto translation = MathUtils3d::Translate(-mOffset);
138  return rotation * scale * translation;
139  }
140 
143  auto rotation = mRotation.AsMatrix();
144  auto scale = MathUtils3d::Scale(Vec3f(1.0f, 1.0f, 1.0f) / mScale);
145  return scale * rotation;
146  }
147 
148  const Quatf &GetRotation() const { return mRotation; }
149  const Vec3f &GetOffset() const { return mOffset; }
150  const Vec3f &GetScale() const { return mScale; }
151 
152  Quatf &GetRotation() { return mRotation; }
153  Vec3f &GetOffset() { return mOffset; }
154  Vec3f &GetScale() { return mScale; }
155 
156  void GetBasisRotation(Vec3f &x, Vec3f &y, Vec3f &z) const {
157  x = mRotation.Rotate(Vec3f::AxisX());
158  y = mRotation.Rotate(Vec3f::AxisY());
159  z = mRotation.Rotate(Vec3f::AxisY());
160  }
161 
171  static Transformf LookAt(const Vec3f &eye, const Vec3f &dir, const Vec3f &up) {
172  Transformf t;
173  t.mOffset = -eye;
174 
175  auto Z = (-dir).Normalized();
176  auto X = Vec3f::Cross(up, Z).Normalized();
177  auto Y = Vec3f::Cross(Z, X);
178 
179  Mat4x4f rotation(
180  X[INDEX_X], X[INDEX_Y], X[INDEX_Z], 0.0f,
181  Y[INDEX_X], Y[INDEX_Y], Y[INDEX_Z], 0.0f,
182  Z[INDEX_X], Z[INDEX_Y], Z[INDEX_Z], 0.0f,
183  0.0f, 0.0f, 0.0f, 1.0f);
184 
185  t.mRotation = Quatf(rotation);
186 
187  return t;
188  }
189 
190 private:
191  static const uint32 INDEX_X = 0;
192  static const uint32 INDEX_Y = 1;
193  static const uint32 INDEX_Z = 2;
194 
195  Quatf mRotation;
196  Vec3f mOffset = Vec3f(0.0f, 0.0f, 0.0f);
197  Vec3f mScale = Vec3f(1.0f, 1.0f, 1.0f);
198 };
199 
205 
206 #endif//BERSERK_TRANSFORMF_H
#define BRK_NS_END
Definition: Config.hpp:48
std::uint32_t uint32
Definition: Typedefs.hpp:44
static Mat4x4f Scale(const Vec3f &scale)
Definition: MathUtils3d.hpp:71
static Mat4x4f Translate(const Vec3f &t)
Translation in the direction of vec t.
Definition: MathUtils3d.hpp:86
Mat AsMatrix() const
Definition: TQuat.hpp:240
Vec Rotate(const Vec &v) const
Definition: TQuat.hpp:212
TQuat Conjugate() const
Definition: TQuat.hpp:208
static TVecN Cross(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:375
static TVecN AxisZ()
Definition: TVecN.hpp:590
static TVecN AxisX()
Definition: TVecN.hpp:582
static TVecN AxisY()
Definition: TVecN.hpp:586
TVecN Normalized() const
Definition: TVecN.hpp:520
3d space transform wrapper class
Definition: Transformf.hpp:58
Transformf & ScaleZ(float scale)
Definition: Transformf.hpp:89
Transformf & Rotate(const Vec3f &axis, float angleRad)
Definition: Transformf.hpp:74
Transformf & ScaleX(float scale)
Definition: Transformf.hpp:79
Transformf & ScaleY(float scale)
Definition: Transformf.hpp:84
static Transformf LookAt(const Vec3f &eye, const Vec3f &dir, const Vec3f &up)
Definition: Transformf.hpp:171
const Vec3f & GetScale() const
Definition: Transformf.hpp:150
Mat4x4f ToTransformMat() const
Definition: Transformf.hpp:123
Transformf()=default
Transformf & Scale(float scale)
Definition: Transformf.hpp:99
Quatf & GetRotation()
Definition: Transformf.hpp:152
Mat4x4f ToNormalMat() const
Definition: Transformf.hpp:142
Transformf & RotateZ(float angleRad)
Definition: Transformf.hpp:70
const Vec3f & GetOffset() const
Definition: Transformf.hpp:149
Mat4x4f ToInverseTransformMat() const
Definition: Transformf.hpp:134
Transformf & Scale(const Vec3f &scale)
Definition: Transformf.hpp:94
Vec3f & GetOffset()
Definition: Transformf.hpp:153
Vec3f TransformAsNormal(const Vec3f &v) const
Definition: Transformf.hpp:116
Transformf & RotateX(float angleRad)
Definition: Transformf.hpp:62
Vec3f & GetScale()
Definition: Transformf.hpp:154
const Quatf & GetRotation() const
Definition: Transformf.hpp:148
Transformf & RotateY(float angleRad)
Definition: Transformf.hpp:66
Vec3f Transform(const Vec3f &v) const
Definition: Transformf.hpp:109
Transformf & Translate(const Vec3f &t)
Definition: Transformf.hpp:104
void GetBasisRotation(Vec3f &x, Vec3f &y, Vec3f &z) const
Definition: Transformf.hpp:156
TQuat< float > Quatf
Definition: TQuat.hpp:423
TVecN< float, 3 > Vec3f
Definition: TVecN.hpp:599
Definition: GLDevice.cpp:46