pgl
PrimitiveOpenGL3Dprimitivelibrary
All Classes Files Functions Variables
math.h
Go to the documentation of this file.
1 
22 #ifndef PGL_MATH_H_
23 #define PGL_MATH_H_
24 
25 #include <ostream>
26 
27 #include <string.h>
28 #include <math.h>
29 
30 namespace pgl {
31 
43 class Vector3
44 {
45  public:
46  union
47  {
48  double data[3];
49  struct
50  {
51  double x, y, z;
52  };
53  };
54 
55  public:
56  Vector3(float _data[3])
57  {
58  data[0] = (double)_data[0];
59  data[1] = (double)_data[1];
60  data[2] = (double)_data[2];
61  }
62 
63  Vector3(double _data[3])
64  {
65  data[0] = _data[0];
66  data[1] = _data[1];
67  data[2] = _data[2];
68  }
69 
70  Vector3() { }
71  Vector3(double _x, double _y, double _z) : data{_x, _y, _z} { }
72  Vector3(const Vector3 &rhs) : data{rhs.x, rhs.y, rhs.z} { }
73 
74  double &operator[](const unsigned int idx)
75  {
76  return data[idx];
77  }
78 
79  double operator[](const unsigned int idx) const
80  {
81  return data[idx];
82  }
83 
84  Vector3 operator-() const
85  {
86  return Vector3(-x, -y, -z);
87  }
88 
89  Vector3 operator+(const Vector3 &rhs) const
90  {
91  return Vector3(x+rhs.x, y+rhs.y, z+rhs.z);
92  }
93 
94  Vector3 operator-(const Vector3 &rhs) const
95  {
96  return Vector3(x-rhs.x, y-rhs.y, z-rhs.z);
97  }
98 
99  Vector3 operator*(const double &rhs) const
100  {
101  return Vector3(x*rhs, y*rhs, z*rhs);
102  }
103 
105  Vector3 operator*(const Vector3 &rhs) const
106  {
107  return Vector3(x*rhs.x, y*rhs.y, z*rhs.z);
108  }
109 
110  Vector3 operator/(const double &rhs) const
111  {
112  return Vector3(x/rhs, y/rhs, z/rhs);
113  }
114 
115  Vector3 operator/(const Vector3 &rhs) const
116  {
117  return Vector3(x/rhs.x, y/rhs.y, z/rhs.z);
118  }
119 
121  Vector3 operator^(const double &rhs) const
122  {
123  return Vector3(pow(x, rhs), pow(y, rhs), pow(z, rhs));
124  }
125 
126  Vector3 cross(const Vector3 &rhs) const
127  {
128  return Vector3(y*rhs.z - z*rhs.y, z*rhs.x-x*rhs.z, x*rhs.y - y*rhs.x);
129  }
130 
131  double norm() const
132  {
133  return sqrt(normsq());
134  }
135 
136  double normsq() const
137  {
138  return x*x+y*y+z*z;
139  }
140 
141  friend std::ostream &operator<<(std::ostream &os, const Vector3 &obj)
142  {
143  os << "[" << obj.x << ", " << obj.y << ", " << obj.z << "]";
144  return os;
145  }
146 };
147 
165 {
166  public:
167  union
168  {
169  double data[16];
170  struct
171  {
172  double __dummy[12];
173  double x, y, z;
174  double __dummy2;
175  };
176  };
177 
178  public:
179  Transform() { }
180 
181  Transform(double _data[16])
182  {
183  set(_data);
184  }
185 
187  Transform(const Vector3 &rotation, const Vector3 &translation)
188  {
189  set(rotation, translation);
190  }
191 
193  Transform(const Vector3 &axis, const double &angle, const Vector3 &translation)
194  {
195  set(axis, angle, translation);
196  }
197 
198  void set(double _data[16])
199  {
200  memcpy(data, _data, 16*sizeof(double));
201  }
202 
203  void set(const Vector3 &axis, const double &angle, const Vector3 &translation)
204  {
205  double s = sin(angle), c = cos(angle), c1 = 1-c;
206  double x = axis.x, y = axis.y, z = axis.z;
207 
208  data[0] = c + x*x*c1; data[4] = x*y*c1 - z*s; data[ 8] = x*z*c1 + y*s;
209  data[1] = y*x*c1 + z*s; data[5] = c + y*y*c1; data[ 9] = y*z*c1 - x*s;
210  data[2] = z*x*c1 - y*s; data[6] = z*y*c1 + x*s; data[10] = c + z*z*c1;
211  data[3] = 0; data[7] = 0; data[11] = 0;
212 
213  this->x = translation.x;
214  this->y = translation.y;
215  this->z = translation.z;
216  data[15] = 1;
217  }
218 
219  void set(const Vector3 &rotation, const Vector3 &translation)
220  {
221  double sa = sin(rotation[2]), ca = cos(rotation[2]);
222  double sb = sin(rotation[1]), cb = cos(rotation[1]);
223  double sg = sin(rotation[0]), cg = cos(rotation[0]);
224 
225  // Intrinsic roll-pitch-yaw
226  data[0] = ca*cb; data[4] = ca*sb*sg - sa*cg; data[8] = ca*sb*cg + sa*sg;
227  data[1] = sa*cb; data[5] = sa*sb*sg + ca*cg; data[9] = sa*sb*cg - ca*sg;
228  data[2] = -sb; data[6] = cb*sg; data[10] = cb*cg;
229  data[3] = 0; data[7] = 0; data[11] = 0;
230 
231  x = translation.x;
232  y = translation.y;
233  z = translation.z;
234  data[15] = 1;
235  }
236 
237  double &operator[](const unsigned int idx)
238  {
239  return data[idx];
240  }
241 
242  double operator[](const unsigned int idx) const
243  {
244  return data[idx];
245  }
246 
247  Transform operator*(const Transform &rhs) const
248  {
249  Transform result;
250 
251  for (unsigned char ii = 0; ii < 4; ++ii)
252  for (unsigned char jj = 0; jj < 4; ++jj)
253  {
254  double sum = 0;
255  for (unsigned char kk = 0; kk < 4; ++kk)
256  sum += data[ii+kk*4]*rhs.data[jj*4+kk];
257  result[ii+jj*4] = sum;
258  }
259 
260  return result;
261  }
262 
263  Vector3 operator*(const Vector3 &rhs) const
264  {
265  return Vector3(data[0]*rhs.x + data[1]*rhs.y + data[ 2]*rhs.z,
266  data[4]*rhs.x + data[5]*rhs.y + data[ 6]*rhs.z,
267  data[8]*rhs.x + data[9]*rhs.y + data[10]*rhs.z);
268  }
269 
270  friend std::ostream &operator<<(std::ostream &os, const Transform &obj)
271  {
272  os << "[" << obj.data[0] << ", " << obj.data[4] << ", " << obj.data[8] << ", " << obj.data[12] << std::endl
273  << " " << obj.data[1] << ", " << obj.data[5] << ", " << obj.data[9] << ", " << obj.data[13] << std::endl
274  << " " << obj.data[2] << ", " << obj.data[6] << ", " << obj.data[10] << ", " << obj.data[14] << std::endl
275  << " " << obj.data[3] << ", " << obj.data[7] << ", " << obj.data[11] << ", " << obj.data[15] << "]" << std::endl;
276  return os;
277  }
278 
279 };
280 
282 class Rotation : public Transform
283 {
284  public:
285  Rotation(const Vector3 &rotation) : Transform(rotation, {0, 0, 0}) { }
286 };
287 
289 class Translation : public Transform
290 {
291  public:
292  Translation(const Vector3 &translation) : Transform({0, 0, 0}, translation) { }
293 };
294 
295 }
296 
297 #endif // PGL_MATH_H_
Transform(const Vector3 &axis, const double &angle, const Vector3 &translation)
Specifies transform through axis-angle and translation.
Definition: math.h:193
Vector3 operator*(const Vector3 &rhs) const
Elementwise product.
Definition: math.h:105
Vector3 operator^(const double &rhs) const
Power.
Definition: math.h:121
Definition: controller.h:27
Transform with zero translation.
Definition: math.h:282
3-component vector.
Definition: math.h:43
Transform(const Vector3 &rotation, const Vector3 &translation)
Specifies transform through intrinsict roll-pitch-yaw and translation.
Definition: math.h:187
Transform with identity rotation.
Definition: math.h:289
Homogeneous coordinate transform.
Definition: math.h:164