Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
geometry.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include "math/const.h"
29 #include "math/func.h"
30 #include "math/point.h"
31 #include "math/matrix.h"
32 #include "math/vector.h"
33 
34 
35 #include <cmath>
36 #include <cstdlib>
37 
38 
39 // Math module namespace
40 namespace Math {
41 
42 
44 inline float MidPoint(const Math::Point &a, const Math::Point &b, float px)
45 {
46  if (IsEqual(a.x, b.x))
47  {
48  if (a.y < b.y)
49  return Math::HUGE_NUM;
50  else
51  return -Math::HUGE_NUM;
52  }
53  return (b.y-a.y) * (px-a.x) / (b.x-a.x) + a.y;
54 }
55 
58 {
59  float n, m;
60 
61  if ( p.x < a.x && p.x < b.x && p.x < c.x ) return false;
62  if ( p.x > a.x && p.x > b.x && p.x > c.x ) return false;
63  if ( p.y < a.y && p.y < b.y && p.y < c.y ) return false;
64  if ( p.y > a.y && p.y > b.y && p.y > c.y ) return false;
65 
66  if ( a.x > b.x ) Swap(a,b);
67  if ( a.x > c.x ) Swap(a,c);
68  if ( c.x < a.x ) Swap(c,a);
69  if ( c.x < b.x ) Swap(c,b);
70 
71  n = MidPoint(a, b, p.x);
72  m = MidPoint(a, c, p.x);
73  if ( (n>p.y||p.y>m) && (n<p.y||p.y<m) ) return false;
74 
75  n = MidPoint(c, b, p.x);
76  m = MidPoint(c, a, p.x);
77  if ( (n>p.y||p.y>m) && (n<p.y||p.y<m) ) return false;
78 
79  return true;
80 }
81 
83 
88 inline Math::Point RotatePoint(const Math::Point &center, float angle, const Math::Point &p)
89 {
90  Math::Point a;
91  a.x = p.x-center.x;
92  a.y = p.y-center.y;
93 
94  Math::Point b;
95  b.x = a.x*cosf(angle) - a.y*sinf(angle);
96  b.y = a.x*sinf(angle) + a.y*cosf(angle);
97 
98  b.x += center.x;
99  b.y += center.y;
100 
101  return b;
102 }
103 
105 
109 inline Math::Point RotatePoint(float angle, const Math::Point &p)
110 {
111  float x = p.x*cosf(angle) - p.y*sinf(angle);
112  float y = p.x*sinf(angle) + p.y*cosf(angle);
113 
114  return Math::Point(x, y);
115 }
116 
118 
122 inline Math::Point RotatePoint(float angle, float dist)
123 {
124  float x = dist*cosf(angle);
125  float y = dist*sinf(angle);
126 
127  return Math::Point(x, y);
128 }
129 
131 
136 inline void RotatePoint(float cx, float cy, float angle, float &px, float &py)
137 {
138  float ax, ay;
139 
140  px -= cx;
141  py -= cy;
142 
143  ax = px*cosf(angle) - py*sinf(angle);
144  ay = px*sinf(angle) + py*cosf(angle);
145 
146  px = cx+ax;
147  py = cy+ay;
148 }
149 
151 
158 inline void RotatePoint(const Math::Vector &center, float angleH, float angleV, Math::Vector &p)
159 {
160  p.x -= center.x;
161  p.y -= center.y;
162  p.z -= center.z;
163 
164  Math::Vector b;
165  b.x = p.x*cosf(angleH) - p.z*sinf(angleH);
166  b.y = p.z*sinf(angleV) + p.y*cosf(angleV);
167  b.z = p.x*sinf(angleH) + p.z*cosf(angleH);
168 
169  p = center + b;
170 }
171 
173 
180 inline void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p)
181 {
182  p.x -= center.x;
183  p.y -= center.y;
184  p.z -= center.z;
185 
186  Math::Vector a;
187  a.x = p.x*cosf(angleH) - p.z*sinf(angleH);
188  a.y = p.y;
189  a.z = p.x*sinf(angleH) + p.z*cosf(angleH);
190 
191  Math::Vector b;
192  b.x = a.x;
193  b.y = a.z*sinf(angleV) + a.y*cosf(angleV);
194  b.z = a.z*cosf(angleV) - a.y*sinf(angleV);
195 
196  p = center + b;
197 }
198 
200 inline float RotateAngle(float x, float y)
201 {
202  if (x == 0.0f && y == 0.0f) return 0.0f;
203 
204  if (x >= 0.0f)
205  {
206  if (y >= 0.0f)
207  {
208  if (x > y) return atanf(y/x);
209  else return PI*0.5f - atanf(x/y);
210  }
211  else
212  {
213  if (x > -y) return PI*2.0f + atanf(y/x);
214  else return PI*1.5f - atanf(x/y);
215  }
216  }
217  else
218  {
219  if (y >= 0.0f)
220  {
221  if (-x > y) return PI*1.0f + atanf(y/x);
222  else return PI*0.5f - atanf(x/y);
223  }
224  else
225  {
226  if (-x > -y) return PI*1.0f + atanf(y/x);
227  else return PI*1.5f - atanf(x/y);
228  }
229  }
230 }
231 
233 
238 inline float RotateAngle(const Math::Point &center, const Math::Point &p1, const Math::Point &p2)
239 {
240  if (PointsEqual(p1, center))
241  return 0;
242 
243  if (PointsEqual(p2, center))
244  return 0;
245 
246  float a1 = asinf((p1.y - center.y) / Distance(p1, center));
247  float a2 = asinf((p2.y - center.y) / Distance(p2, center));
248 
249  if (p1.x < center.x) a1 = PI - a1;
250  if (p2.x < center.x) a2 = PI - a2;
251 
252  float a = a2 - a1;
253  if (a < 0)
254  a += 2.0f*PI;
255 
256  return a;
257 }
258 
260 
266 inline void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from,
267  const Math::Vector &at, const Math::Vector &worldUp)
268 {
269  // Get the z basis vector, which points straight ahead. This is the
270  // difference from the eyepoint to the lookat point.
271  Math::Vector view = at - from;
272 
273  float length = view.Length();
274  assert(! IsZero(length) );
275 
276  // Normalize the z basis vector
277  view /= length;
278 
279  // Get the dot product, and calculate the projection of the z basis
280  // vector onto the up vector. The projection is the y basis vector.
281  float dotProduct = DotProduct(worldUp, view);
282 
283  Math::Vector up = worldUp - dotProduct * view;
284 
285  // If this vector has near-zero length because the input specified a
286  // bogus up vector, let's try a default up vector
287  if ( IsZero(length = up.Length()) )
288  {
289  up = Math::Vector(0.0f, 1.0f, 0.0f) - view.y * view;
290 
291  // If we still have near-zero length, resort to a different axis.
292  if ( IsZero(length = up.Length()) )
293  {
294  up = Math::Vector(0.0f, 0.0f, 1.0f) - view.z * view;
295 
296  assert(! IsZero(up.Length()) );
297  }
298  }
299 
300  // Normalize the y basis vector
301  up /= length;
302 
303  // The x basis vector is found simply with the cross product of the y
304  // and z basis vectors
305  Math::Vector right = CrossProduct(up, view);
306 
307  // Start building the matrix. The first three rows contains the basis
308  // vectors used to rotate the view to point at the lookat point
309  mat.LoadIdentity();
310 
311  /* (1,1) */ mat.m[0 ] = right.x;
312  /* (2,1) */ mat.m[1 ] = up.x;
313  /* (3,1) */ mat.m[2 ] = view.x;
314  /* (1,2) */ mat.m[4 ] = right.y;
315  /* (2,2) */ mat.m[5 ] = up.y;
316  /* (3,2) */ mat.m[6 ] = view.y;
317  /* (1,3) */ mat.m[8 ] = right.z;
318  /* (2,3) */ mat.m[9 ] = up.z;
319  /* (3,3) */ mat.m[10] = view.z;
320 
321  // Do the translation values (rotations are still about the eyepoint)
322  /* (1,4) */ mat.m[12] = -DotProduct(from, right);
323  /* (2,4) */ mat.m[13] = -DotProduct(from, up);
324  /* (3,4) */ mat.m[14] = -DotProduct(from, view);
325 }
326 
328 
335 inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = Math::PI / 2.0f, float aspect = 1.0f,
336  float nearPlane = 1.0f, float farPlane = 1000.0f)
337 {
338  assert(fabs(farPlane - nearPlane) >= 0.01f);
339  assert(fabs(sin(fov / 2)) >= 0.01f);
340 
341  float f = cosf(fov / 2.0f) / sinf(fov / 2.0f);
342 
343  mat.LoadZero();
344 
345  /* (1,1) */ mat.m[0 ] = f / aspect;
346  /* (2,2) */ mat.m[5 ] = f;
347  /* (3,3) */ mat.m[10] = (nearPlane + farPlane) / (nearPlane - farPlane);
348  /* (4,3) */ mat.m[11] = -1.0f;
349  /* (3,4) */ mat.m[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
350 }
351 
353 
359 inline void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right, float bottom, float top,
360  float zNear = -1.0f, float zFar = 1.0f)
361 {
362  mat.LoadIdentity();
363 
364  /* (1,1) */ mat.m[0 ] = 2.0f / (right - left);
365  /* (2,2) */ mat.m[5 ] = 2.0f / (top - bottom);
366  /* (3,3) */ mat.m[10] = -2.0f / (zFar - zNear);
367 
368  /* (1,4) */ mat.m[12] = - (right + left) / (right - left);
369  /* (2,4) */ mat.m[13] = - (top + bottom) / (top - bottom);
370  /* (3,4) */ mat.m[14] = - (zFar + zNear) / (zFar - zNear);
371 }
372 
374 
378 inline void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans)
379 {
380  mat.LoadIdentity();
381  /* (1,4) */ mat.m[12] = trans.x;
382  /* (2,4) */ mat.m[13] = trans.y;
383  /* (3,4) */ mat.m[14] = trans.z;
384 }
385 
387 
391 inline void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale)
392 {
393  mat.LoadIdentity();
394  /* (1,1) */ mat.m[0 ] = scale.x;
395  /* (2,2) */ mat.m[5 ] = scale.y;
396  /* (3,3) */ mat.m[10] = scale.z;
397 }
398 
400 
404 inline void LoadRotationXMatrix(Math::Matrix &mat, float angle)
405 {
406  mat.LoadIdentity();
407  /* (2,2) */ mat.m[5 ] = cosf(angle);
408  /* (3,2) */ mat.m[6 ] = sinf(angle);
409  /* (2,3) */ mat.m[9 ] = -sinf(angle);
410  /* (3,3) */ mat.m[10] = cosf(angle);
411 }
412 
414 
418 inline void LoadRotationYMatrix(Math::Matrix &mat, float angle)
419 {
420  mat.LoadIdentity();
421  /* (1,1) */ mat.m[0 ] = cosf(angle);
422  /* (3,1) */ mat.m[2 ] = -sinf(angle);
423  /* (1,3) */ mat.m[8 ] = sinf(angle);
424  /* (3,3) */ mat.m[10] = cosf(angle);
425 }
426 
428 
432 inline void LoadRotationZMatrix(Math::Matrix &mat, float angle)
433 {
434  mat.LoadIdentity();
435  /* (1,1) */ mat.m[0 ] = cosf(angle);
436  /* (2,1) */ mat.m[1 ] = sinf(angle);
437  /* (1,2) */ mat.m[4 ] = -sinf(angle);
438  /* (2,2) */ mat.m[5 ] = cosf(angle);
439 }
440 
442 
447 inline void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle)
448 {
449  float cos = cosf(angle);
450  float sin = sinf(angle);
451  Math::Vector v = Normalize(dir);
452 
453  mat.LoadIdentity();
454 
455  /* (1,1) */ mat.m[0 ] = (v.x * v.x) * (1.0f - cos) + cos;
456  /* (2,1) */ mat.m[1 ] = (v.x * v.y) * (1.0f - cos) - (v.z * sin);
457  /* (3,1) */ mat.m[2 ] = (v.x * v.z) * (1.0f - cos) + (v.y * sin);
458 
459  /* (1,2) */ mat.m[4 ] = (v.y * v.x) * (1.0f - cos) + (v.z * sin);
460  /* (2,2) */ mat.m[5 ] = (v.y * v.y) * (1.0f - cos) + cos ;
461  /* (3,2) */ mat.m[6 ] = (v.y * v.z) * (1.0f - cos) - (v.x * sin);
462 
463  /* (1,3) */ mat.m[8 ] = (v.z * v.x) * (1.0f - cos) - (v.y * sin);
464  /* (2,3) */ mat.m[9 ] = (v.z * v.y) * (1.0f - cos) + (v.x * sin);
465  /* (3,3) */ mat.m[10] = (v.z * v.z) * (1.0f - cos) + cos;
466 }
467 
469 inline void LoadRotationXZYMatrix(Math::Matrix &mat, const Math::Vector &angles)
470 {
471  Math::Matrix temp;
472  LoadRotationXMatrix(temp, angles.x);
473 
474  LoadRotationZMatrix(mat, angles.z);
475  mat = Math::MultiplyMatrices(temp, mat);
476 
477  LoadRotationYMatrix(temp, angles.y);
478  mat = Math::MultiplyMatrices(temp, mat);
479 }
480 
482 inline void LoadRotationZXYMatrix(Math::Matrix &mat, const Math::Vector &angles)
483 {
484  Math::Matrix temp;
485  LoadRotationZMatrix(temp, angles.z);
486 
487  LoadRotationXMatrix(mat, angles.x);
488  mat = Math::MultiplyMatrices(temp, mat);
489 
490  LoadRotationYMatrix(temp, angles.y);
491  mat = Math::MultiplyMatrices(temp, mat);
492 }
493 
495 inline float DistanceProjected(const Math::Vector &a, const Math::Vector &b)
496 {
497  return sqrtf( (a.x-b.x)*(a.x-b.x) +
498  (a.z-b.z)*(a.z-b.z) );
499 }
500 
502 
505 inline Math::Vector NormalToPlane(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3)
506 {
507  Math::Vector u = p3 - p1;
508  Math::Vector v = p2 - p1;
509 
510  return Normalize(CrossProduct(u, v));
511 }
512 
514 
518 inline Math::Vector SegmentPoint(const Math::Vector &p1, const Math::Vector &p2, float dist)
519 {
520  Math::Vector direction = p2 - p1;
521 
522  direction.Normalize();
523 
524  return p1 + direction * dist;
525 }
526 
528 
532 inline float DistanceToPlane(const Math::Vector &a, const Math::Vector &b,
533  const Math::Vector &c, const Math::Vector &p)
534 {
535  Math::Vector n = NormalToPlane(a, b, c);
536  float d = -(n.x*a.x + n.y*a.y + n.z*a.z);
537 
538  return fabs(n.x*p.x + n.y*p.y + n.z*p.z + d);
539 }
540 
542 
546 inline bool IsSamePlane(const Math::Vector (&plane1)[3], const Math::Vector (&plane2)[3])
547 {
548  Math::Vector n1 = NormalToPlane(plane1[0], plane1[1], plane1[2]);
549  Math::Vector n2 = NormalToPlane(plane2[0], plane2[1], plane2[2]);
550 
551  if ( fabs(n1.x-n2.x) > 0.1f ||
552  fabs(n1.y-n2.y) > 0.1f ||
553  fabs(n1.z-n2.z) > 0.1f )
554  return false;
555 
556  float dist = DistanceToPlane(plane1[0], plane1[1], plane1[2], plane2[0]);
557  if ( dist > 0.1f )
558  return false;
559 
560  return true;
561 }
562 
564 inline bool Intersect(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c,
565  const Math::Vector &d, const Math::Vector &e, Math::Vector &i)
566 {
567  float d1 = (d.x-a.x)*((b.y-a.y)*(c.z-a.z)-(c.y-a.y)*(b.z-a.z)) -
568  (d.y-a.y)*((b.x-a.x)*(c.z-a.z)-(c.x-a.x)*(b.z-a.z)) +
569  (d.z-a.z)*((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
570 
571  float d2 = (d.x-e.x)*((b.y-a.y)*(c.z-a.z)-(c.y-a.y)*(b.z-a.z)) -
572  (d.y-e.y)*((b.x-a.x)*(c.z-a.z)-(c.x-a.x)*(b.z-a.z)) +
573  (d.z-e.z)*((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
574 
575  if (d2 == 0)
576  return false;
577 
578  i.x = d.x + d1/d2*(e.x-d.x);
579  i.y = d.y + d1/d2*(e.y-d.y);
580  i.z = d.z + d1/d2*(e.z-d.z);
581 
582  return true;
583 }
584 
586 
587 inline bool IntersectY(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, Math::Vector &p)
588 {
589  float d = (b.x-a.x)*(c.z-a.z) - (c.x-a.x)*(b.z-a.z);
590  float d1 = (p.x-a.x)*(c.z-a.z) - (c.x-a.x)*(p.z-a.z);
591  float d2 = (b.x-a.x)*(p.z-a.z) - (p.x-a.x)*(b.z-a.z);
592 
593  if (d == 0.0f)
594  return false;
595 
596  p.y = a.y + d1/d*(b.y-a.y) + d2/d*(c.y-a.y);
597 
598  return true;
599 }
600 
602 inline Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length)
603 {
604  Math::Vector lookat = eye;
605  lookat.z += length;
606 
607  RotatePoint(eye, angleH, angleV, lookat);
608 
609  return lookat;
610 }
611 
613 
615 {
616  return MatrixVectorMultiply(m, p);
617 }
618 
620 
624 inline Math::Vector Projection(const Math::Vector &a, const Math::Vector &b, const Math::Vector &p)
625 {
626  float k = DotProduct(b - a, p - a);
627  k /= DotProduct(b - a, b - a);
628 
629  return a + k*(b-a);
630 }
631 
633 inline Math::Vector RotateView(Math::Vector center, float angleH, float angleV, float dist)
634 {
635  Math::Matrix mat1, mat2;
636  LoadRotationZMatrix(mat1, -angleV);
637  LoadRotationYMatrix(mat2, -angleH);
638 
639  Math::Matrix mat = MultiplyMatrices(mat2, mat1);
640 
641  Math::Vector eye;
642  eye.x = 0.0f+dist;
643  eye.y = 0.0f;
644  eye.z = 0.0f;
645  eye = Transform(mat, eye);
646 
647  return eye+center;
648 }
649 
650 
651 } // namespace Math
652 
void LoadRotationXMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the X axis.
Definition: geometry.h:404
void LoadIdentity()
Loads the identity matrix.
Definition: matrix.h:131
float DistanceToPlane(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, const Math::Vector &p)
Returns the distance between given point and a plane.
Definition: geometry.h:532
void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale)
Loads a scaling matrix fom given vector.
Definition: geometry.h:391
void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right, float bottom, float top, float zNear=-1.0f, float zFar=1.0f)
Loads an othogonal projection matrix.
Definition: geometry.h:359
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:46
void LoadRotationZXYMatrix(Math::Matrix &mat, const Math::Vector &angles)
Calculates the matrix to make three rotations in the order Z, X and Y.
Definition: geometry.h:482
void LoadZero()
Loads the zero matrix.
Definition: matrix.h:124
float x
X coord.
Definition: point.h:52
Math::Vector Transform(const Math::Matrix &m, const Math::Vector &p)
Transforms the point p by matrix m.
Definition: geometry.h:614
float MidPoint(const Math::Point &a, const Math::Point &b, float px)
Returns py up on the line a - b.
Definition: geometry.h:44
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:189
Point struct and related functions.
4x4 matrix
Definition: matrix.h:66
Vector Normalize(const Math::Vector &v)
Convenience function for getting normalized vector.
Definition: vector.h:242
void Normalize()
Normalizes the vector.
Definition: vector.h:100
bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math::Point p)
Tests whether the point p is inside the triangle (a,b,c)
Definition: geometry.h:57
float x
X - 1st coord.
Definition: vector.h:55
Math::Vector NormalToPlane(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3)
Returns the normal vector to a plane.
Definition: geometry.h:505
Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right)
Convenience function for multiplying a matrix.
Definition: matrix.h:427
void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle)
Loads a rotation matrix along the given axis.
Definition: geometry.h:447
float y
Y coord.
Definition: point.h:54
float DotProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating dot product.
Definition: vector.h:250
const float PI
PI.
Definition: const.h:47
bool IntersectY(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, Math::Vector &p)
Calculates the intersection of the straight line passing through p (x, z)
Definition: geometry.h:587
void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, const Math::Vector &at, const Math::Vector &worldUp)
Loads view matrix from the given vectors.
Definition: geometry.h:266
bool Intersect(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, const Math::Vector &d, const Math::Vector &e, Math::Vector &i)
Calculates the intersection "i" right "of" the plane "abc" (TODO: ?)
Definition: geometry.h:564
const float HUGE_NUM
Huge number.
Definition: const.h:44
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:40
float RotateAngle(float x, float y)
Returns the angle between point (x,y) and (0,0)
Definition: geometry.h:200
Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide=false)
Calculates the result of multiplying m * v.
Definition: matrix.h:445
void LoadRotationXZYMatrix(Math::Matrix &mat, const Math::Vector &angles)
Calculates the matrix to make three rotations in the order X, Z and Y.
Definition: geometry.h:469
Namespace for (new) math code.
Definition: const.h:32
void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans)
Loads a translation matrix from given vector.
Definition: geometry.h:378
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:104
Matrix struct and related functions.
2D point
Definition: point.h:49
void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p)
Rotates a point around a center in space.
Definition: geometry.h:180
Math::Point RotatePoint(const Math::Point &center, float angle, const Math::Point &p)
Rotates a point around a center.
Definition: geometry.h:88
float z
Z - 3rd coord.
Definition: vector.h:59
Math::Vector RotateView(Math::Vector center, float angleH, float angleV, float dist)
Calculates point of view to look at a center two angles and a distance.
Definition: geometry.h:633
float Length() const
Returns the vector length.
Definition: vector.h:94
void LoadRotationZMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Z axis.
Definition: geometry.h:432
void LoadRotationYMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Y axis.
Definition: geometry.h:418
Constants used in math functions.
Vector struct and related functions.
Common math functions.
bool PointsEqual(const Point &a, const Point &b, float tolerance=TOLERANCE)
Checks if two vectors are equal within given tolerance.
Definition: point.h:173
Math::Vector SegmentPoint(const Math::Vector &p1, const Math::Vector &p2, float dist)
Returns a point on the line p1 - p2, in dist distance from p1.
Definition: geometry.h:518
Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length)
Calculates the end point.
Definition: geometry.h:602
Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating cross product.
Definition: vector.h:256
3D (3x1) vector
Definition: vector.h:52
Math::Vector Projection(const Math::Vector &a, const Math::Vector &b, const Math::Vector &p)
Calculates the projection of the point p on a straight line a to b.
Definition: geometry.h:624
bool IsSamePlane(const Math::Vector(&plane1)[3], const Math::Vector(&plane2)[3])
Checks if two planes defined by three points are the same.
Definition: geometry.h:546
float y
Y - 2nd coord.
Definition: vector.h:57
float m[16]
Matrix values in column-major order.
Definition: matrix.h:69
float DistanceProjected(const Math::Vector &a, const Math::Vector &b)
Returns the distance between projections on XZ plane of two vectors.
Definition: geometry.h:495
void LoadProjectionMatrix(Math::Matrix &mat, float fov=Math::PI/2.0f, float aspect=1.0f, float nearPlane=1.0f, float farPlane=1000.0f)
Loads a perspective projection matrix.
Definition: geometry.h:335