Gorgon Game Engine
Transform3D.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <iostream>
5 #include <cstring>
6 
7 #include "Point3D.h"
8 #include "Point.h"
9 
10 namespace Gorgon { namespace Geometry {
11  template<class T_>
13  public:
15  LoadIdentity();
16  }
17 
18  basic_Transform3D(std::initializer_list<std::array<T_, 4>> init) {
19  int i=0;
20  for(auto r : init) {
21  for(int j=0; j<4; j++) {
22  mat[i][j] = r[j];
23  }
24  i+=1;
25  if(i==4) break;
26  }
27  }
28 
29  void LoadIdentity() {
30  std::memset(&mat[0], 0, 4*4*sizeof(T_));
31 
32  for(int i=0; i<4; i++) {
33  mat[i][i] = 1;
34  }
35  }
36 
37  T_ Get(int row, int col) const { return mat[row][col]; }
38 
39  T_ &operator()(int row, int col) { return mat[row][col]; }
40 
42  auto f = p.X * mat[3][0] + p.Y * mat[3][1] + p.Z * mat[3][2] + mat[3][3];
43 
44  return{
45  (p.X * mat[0][0] + p.Y * mat[0][1] + p.Z * mat[0][2] + mat[0][3]) / f,
46  (p.X * mat[1][0] + p.Y * mat[1][1] + p.Z * mat[1][2] + mat[1][3]) / f,
47  (p.X * mat[2][0] + p.Y * mat[2][1] + p.Z * mat[2][2] + mat[2][3]) / f
48  };
49  }
50 
52  auto f = p.X * mat[3][0] + p.Y * mat[3][1] + mat[3][3];
53 
54  return{
55  (p.X * mat[0][0] + p.Y * mat[0][1] + mat[0][3]) / f,
56  (p.X * mat[1][0] + p.Y * mat[1][1] + mat[1][3]) / f
57  };
58  }
59 
61  basic_Transform3D o = *this;
62  for(int i=0; i<4; i++)
63  for(int j=0; j<4; j++)
64  mat[i][j] = o.mat[i][0] * t.mat[0][j] + o.mat[i][1] * t.mat[1][j] + o.mat[i][2] * t.mat[2][j] + o.mat[i][3] * t.mat[3][j];
65 
66  return *this;
67  }
68 
69  void Translate(const basic_Point3D<T_> &p) {
71  {1, 0, 0, p.X},
72  {0, 1, 0, p.Y},
73  {0, 0, 1, p.Z},
74  {0, 0, 0, 1}
75  });
76 
77  (*this)*=t;
78  }
79 
80  void Translate(T_ x, T_ y, T_ z = 0) {
82  {1, 0, 0, x},
83  {0, 1, 0, y},
84  {0, 0, 1, z},
85  {0, 0, 0, 1}
86  });
87 
88  (*this)*=t;
89  }
90 
91  void Scale(T_ x, T_ y, T_ z = 1) {
93  {x, 0, 0, 0},
94  {0, y, 0, 0},
95  {0, 0, z, 0},
96  {0, 0, 0, 1}
97  });
98 
99  (*this)*=t;
100  }
101 
102  void Scale(T_ f) {
104  {f, 0, 0, 0},
105  {0, f, 0, 0},
106  {0, 0, f, 0},
107  {0, 0, 0, 1}
108  });
109 
110  (*this)*=t;
111  }
112 
113  void Rotate(T_ x, T_ y, T_ z) {
114  T_ ca = std::cos(z), sa = std::sin(z);
115  T_ cb = std::cos(y), sb = std::sin(y);
116  T_ cc = std::cos(x), sc = std::sin(x);
117 
119  {ca*cb, ca*sb*sc - sa*cc, ca*sb*cc + sa*sc, 0},
120  {sa*cb, sa*cb*sc + ca*cc, sa*sb*cc - ca*sc, 0},
121  {-sb , cb*sc , cb*cc , 0},
122  {0 , 0 , 0 , 1}
123  });
124 
125  (*this)*=t;
126  }
127 
129  void Rotate(const basic_Point3D<T_> &vec, T_ ang) {
130  T_ c = std::cos(ang), s = std::sin(ang);
131  T_ C = 1 - c;
132 
134  {vec.X*vec.X + (vec.Y*vec.Y + vec.Z*vec.Z)*c, vec.X*vec.Y*C - vec.Z*s , vec.Z*vec.X*C + vec.Y*s , 0},
135  {vec.X*vec.Y*C + vec.Z*s , vec.Y*vec.Y + (vec.X*vec.X + vec.Z*vec.Z)*c, vec.Z*vec.Y*C + vec.X*s , 0},
136  {vec.X*vec.Z*C - vec.Y*s , vec.Z*vec.Y*C + vec.X*s , vec.Z*vec.Z + (vec.X*vec.X + vec.Y*vec.Y)*c, 0},
137  {0 , 0 , 0 , 1}
138  });
139 
140  (*this)*=t;
141  }
142 
144  void Transpose() {
145  *this = {
146  {mat[0][0], mat[1][0], mat[2][0], mat[0][3]},
147  {mat[0][1], mat[1][1], mat[2][1], mat[1][3]},
148  {mat[0][2], mat[1][2], mat[2][2], mat[2][3]},
149  {mat[3][0], mat[3][1], mat[3][2], mat[3][3]}
150  };
151  }
152 
153  T_ *Data() {
154  return vec;
155  }
156 
157  private:
158  union {
159  T_ mat[4][4];
160  T_ vec[16];
161  };
162  };
163 
164  template<class T_>
165  std::ostream &operator <<(std::ostream &out, const basic_Transform3D<T_> &transform) {
166  for(int i=0; i<4; i++) {
167  for(int j=0; j<4; j++) {
168  out<<transform.Get(i, j)<<"\t";
169  }
170  out<<std::endl;
171  }
172 
173  return out;
174  }
175 
177 } }
Gorgon::Geometry::basic_Transform3D::operator()
T_ & operator()(int row, int col)
Definition: Transform3D.h:39
Gorgon::Geometry::basic_Transform3D::Rotate
void Rotate(T_ x, T_ y, T_ z)
Definition: Transform3D.h:113
Gorgon::Geometry::basic_Point3D::Y
T_ Y
Definition: Point3D.h:67
Gorgon::Geometry::basic_Transform3D::Translate
void Translate(const basic_Point3D< T_ > &p)
Definition: Transform3D.h:69
Gorgon::Geometry::basic_Transform3D::Rotate
void Rotate(const basic_Point3D< T_ > &vec, T_ ang)
Rotates around the given plane, vec should be a unit vector.
Definition: Transform3D.h:129
Point.h
contains point class.
Gorgon::Geometry::basic_Point3D::Z
T_ Z
Definition: Point3D.h:68
Gorgon::Geometry::basic_Point::X
T_ X
X coordinate.
Definition: Point.h:368
Gorgon::Geometry::basic_Transform3D::vec
T_ vec[16]
Definition: Transform3D.h:160
Gorgon::WindowManager::init
void init()
Definition: X11.cpp:143
Gorgon::Geometry::basic_Point3D
Definition: Point3D.h:12
Gorgon::Geometry::basic_Transform3D::Get
T_ Get(int row, int col) const
Definition: Transform3D.h:37
Gorgon::Geometry::basic_Transform3D::Data
T_ * Data()
Definition: Transform3D.h:153
Gorgon::Geometry::basic_Transform3D::Scale
void Scale(T_ x, T_ y, T_ z=1)
Definition: Transform3D.h:91
Gorgon::Geometry::basic_Transform3D
Definition: Transform3D.h:12
Gorgon::Geometry::basic_Transform3D::LoadIdentity
void LoadIdentity()
Definition: Transform3D.h:29
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Transform3D::basic_Transform3D
basic_Transform3D(std::initializer_list< std::array< T_, 4 >> init)
Definition: Transform3D.h:18
Gorgon::Geometry::basic_Point3D::X
T_ X
Definition: Point3D.h:66
Gorgon::Geometry::basic_Transform3D::mat
T_ mat[4][4]
Definition: Transform3D.h:159
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Geometry::basic_Transform3D::operator*
basic_Point3D< T_ > operator*(const basic_Point3D< T_ > &p) const
Definition: Transform3D.h:41
Gorgon::Geometry::basic_Transform3D::Translate
void Translate(T_ x, T_ y, T_ z=0)
Definition: Transform3D.h:80
Gorgon::Geometry::basic_Transform3D::Scale
void Scale(T_ f)
Definition: Transform3D.h:102
Gorgon::Geometry::basic_Transform3D::basic_Transform3D
basic_Transform3D()
Definition: Transform3D.h:14
Gorgon::Input::Keyboard::Keycodes::C
constexpr Key C
Definition: Keyboard.h:82
Gorgon::Geometry::basic_Point::Y
T_ Y
Y coordinate.
Definition: Point.h:371
Gorgon::Geometry::operator<<
std::ostream & operator<<(std::ostream &out, const basic_Bounds< T_ > &bounds)
Allows streaming of bounds.
Definition: Bounds.h:415
Point3D.h
Gorgon::Geometry::basic_Transform3D::Transpose
void Transpose()
This function transposes only 3x3 portion of the matrix.
Definition: Transform3D.h:144
Gorgon::Geometry::basic_Transform3D::operator*=
basic_Transform3D & operator*=(const basic_Transform3D &t)
Definition: Transform3D.h:60