My Project
geometry.h
1 #ifndef GEOMETRY_INCLUDED
2 #define GEOMETRY_INCLUDED
3
4 #define RAYEPS 1e-10
5
6
7 #define PI 3.1415926535897932384
8
9
11 class Point2D{
12 public:
14  double p[2];
15  Point2D(void);
16  Point2D(const double& x,const double& y);
17
19  double& operator[] (const int& index);
21  double& index(const int& i);
22
24  double dot(const Point2D& pt) const;
25
27  double length(void) const;
29  double squareNorm(void) const;
30
32  Point2D unit(void) const;
33
35  Point2D negate(void) const;
37  Point2D operator- (void) const;
38
40  Point2D scale(double scl) const;
42  Point2D operator* (double scale) const;
44  Point2D& operator*=(double scale);
45
47  Point2D operator/ (double scale) const;
49  Point2D& operator/=(double scale);
50
52  Point2D add(const Point2D& pt) const;
54  Point2D operator+ (const Point2D& pt) const;
56  Point2D& operator+=(const Point2D& pt);
57
59  Point2D subtract(const Point2D& pt) const;
61  Point2D operator- (const Point2D& pt) const;
63  Point2D& operator-=(const Point2D& pt);
64
66  Point2D mult(const Point2D& pt) const;
68  Point2D operator* (const Point2D& pt) const;
70  Point2D& operator*=(const Point2D& pt);
71
73  Point2D div(const Point2D& pt) const;
75  Point2D operator/ (const Point2D& pt) const;
77  Point2D& operator/=(const Point2D& pt);
78 };
79
81 class Point3D{
82 public:
84  double p[3];
85  Point3D(void);
86  Point3D(const double& x,const double& y,const double& z);
87
89  double& operator[] (const int& index);
91  double& index(const int& i);
92
94  double dot(const Point3D& pt) const;
95
97  double length(void) const;
99  double squareNorm(void) const;
100
102  Point3D unit(void) const;
103
105  Point3D negate(void) const;
107  Point3D operator- (void) const;
108
110  Point3D scale(double scale) const;
112  Point3D operator* (double scale) const;
114  Point3D& operator*=(double scale);
115
117  Point3D operator/ (double scale) const;
119  Point3D& operator/=(double scale);
120
122  Point3D add(const Point3D& pt) const;
124  Point3D operator+ (const Point3D& pt) const;
126  Point3D& operator+=(const Point3D& pt);
127
129  Point3D subtract(const Point3D& pt) const;
131  Point3D operator- (const Point3D& pt) const;
133  Point3D& operator-=(const Point3D& pt);
134
136  Point3D mult(const Point3D& pt) const;
138  Point3D operator* (const Point3D& pt) const;
140  Point3D& operator*=(const Point3D& pt);
141
143  Point3D div(const Point3D& pt) const;
145  Point3D operator/ (const Point3D& pt) const;
147  Point3D& operator/=(const Point3D& pt);
148
150  Point3D crossProduct(const Point3D& pt) const;
152  Point3D operator^ (const Point3D& pt) const;
154  Point3D& operator^=(const Point3D& pt);
155 };
156
157
159 class Plane3D{
160 public:
164  double distance;
165
166  Plane3D(void);
168  Plane3D(const Point3D& normal,const Point3D& pt);
170  Plane3D(const Point3D& p1,const Point3D& p2,const Point3D& p3);
171
173  double evaluate(const Point3D& p) const;
175  double operator()(const Point3D& p) const;
176
178  void makePositive(const Point3D& p);
179 };
180
182 class Ray3D{
183 public:
188
189  Ray3D(void);
190  Ray3D(const Point3D& pos,const Point3D& dir);
191
193  Ray3D add(const Point3D& p) const;
195  Ray3D operator+ (const Point3D& p) const;
197  Ray3D& operator+=(const Point3D& p);
199  Ray3D subtract(const Point3D& p) const;
201  Ray3D operator- (const Point3D& p) const;
203  Ray3D& operator-=(const Point3D& p);
204
206  Point3D operator() (double t) const;
207 };
208
210 class Matrix3D{
211 public:
213  double m[3][3];
214
216  Matrix3D(void);
218  Matrix3D(const Point3D& eulerAngles);
220  Matrix3D(const class Quaternion& q);
222  Matrix3D(const class Matrix4D& m);
223
225  double& operator() (int col,int row);
227  double& index(int col,int row);
228
230  double det(void) const;
231
233  Matrix3D operator- (void) const;
234
236  Matrix3D operator* (const Matrix3D& m) const;
238  Matrix3D& operator*=(const Matrix3D& m);
239
241  Matrix3D operator+ (const Matrix3D& m) const;
243  Matrix3D& operator+=(const Matrix3D& m);
244
246  Matrix3D operator- (const Matrix3D& m) const;
248  Matrix3D& operator-=(const Matrix3D& m);
249
251  Matrix3D operator* (double f) const;
253  Matrix3D& operator*=(double f);
255  Matrix3D operator/ (double f) const;
257  Matrix3D& operator/=(double f);
258
260  Matrix3D transpose(void) const;
261
263  Matrix3D invert(void) const;
264
266  static int Invert(const Matrix3D& in,Matrix3D& out);
267
269  Point3D operator*(const Point3D& p) const;
270
272  Ray3D operator* (const Ray3D& ray) const;
273
275  static Matrix3D IdentityMatrix(void);
276
278  Point3D toEuler(void) const;
281  Point3D toEuler(const Point3D& euler) const;
282
284  double squareNorm(void) const;
286  static double SquareL2Difference(const Matrix3D& m1,const Matrix3D& m2);
287
289  static Matrix3D Exp(const Matrix3D& m,int iter=100);
291  static Matrix3D Log(const Matrix3D& m,double eps=0.0001);
293  static Matrix3D SquareRoot(const Matrix3D& m,double eps=0.000001);
294
297  void SVD(Matrix3D& r1,Matrix3D& diagonal,Matrix3D& r2) const;
298
300  void Factor(Matrix3D& rot,Matrix3D& sym) const;
301
303  Matrix3D closestRotation(void) const;
304
306  Matrix3D symmetrize(void) const;
308  Matrix3D skewSymmetrize(void) const;
309 };
310
312 class Matrix4D{
313  double subDet(int col1,int row1,int col2,int row2) const;
314  double subDet(int col,int row) const;
315 public:
317  double m[4][4];
318
320  Matrix4D(void);
322  Matrix4D(const Matrix3D& m);
325  Matrix4D(const Matrix3D& m,const Point3D& p);
326
328  double& operator() (int col,int row);
330  double& index(int col,int row);
331
333  double det(void) const;
334
336  Matrix4D operator- (void) const;
337
339  Matrix4D mult(const Matrix4D& m) const;
341  Matrix4D operator* (const Matrix4D& m) const;
343  Matrix4D& operator*=(const Matrix4D& m);
344
347  Matrix4D add(const Matrix4D& m) const;
349  Matrix4D operator+ (const Matrix4D& m) const;
351  Matrix4D& operator+=(const Matrix4D& m);
352
353  // Subtracts two matrics
355  Matrix4D subtract(const Matrix4D& m) const;
357  Matrix4D operator- (const Matrix4D& m) const;
359  Matrix4D& operator-=(const Matrix4D& m);
360
362  Matrix4D mult(double f) const;
364  Matrix4D operator* (double f) const;
366  Matrix4D& operator*=(double f);
368  Matrix4D div(double f) const;
370  Matrix4D operator/ (double f) const;
372  Matrix4D& operator/=(double f);
373
375  Matrix4D transpose(void) const;
376
378  Matrix4D invert(void) const;
379
381  static int Invert(const Matrix4D& in,Matrix4D& out);
382
384  Point3D operator*(const Point3D& p) const;
386  Point3D multPosition(const Point3D& position) const;
387
389  Point3D multDirection(const Point3D& direction) const;
390
392  Point3D multNormal(const Point3D& normal) const;
393
394
396  Ray3D mult(const Ray3D& ray) const;
398  Ray3D operator* (const Ray3D& ray) const;
399
401  static Matrix4D IdentityMatrix(void);
402
404  Point3D toEuler(void) const;
405
407  double squareNorm(void) const;
409  static double SquareL2Difference(const Matrix4D& m1,const Matrix4D& m2);
410 };
411
412
413 /*
414  * This class represents a bounding box object that surrounds a shape.
415  * The box is assumed to be lined up with the coordinate axis and is
416  * defined by (any) two antipodal points on the box. The constructors
417  * make sure that the the (x,y,z) coordinates of p[0] are all less than
418  * or equal to the (x,y,z) coordinates of p[1]
419  */
422 public:
424  Point3D p[2];
425
426  BoundingBox3D(void);
428  BoundingBox3D(const Point3D& p1,const Point3D& p2);
429
431  BoundingBox3D(const Point3D* pList,const int pSize);
432
436  BoundingBox3D operator+ (const BoundingBox3D& b) const;
441
444  BoundingBox3D transform(const Matrix4D& m) const;
445
448  double intersect(const Ray3D& ray) const;
449
450 };
451
454 public:
456  double real;
459
461  Quaternion(void);
463  Quaternion(double r,const Point3D& i);
465  Quaternion(const Matrix3D& rot);
467  Quaternion(const Matrix3D& rot,const Quaternion& quat);
469  Quaternion(const Point3D& eulerAngle,const Quaternion& quat);
470
472  double dot(const Quaternion& q) const;
473
475  double length(void) const;
477  double squareNorm(void) const;
478
480  Quaternion unit(void) const;
481
483  Quaternion operator- (void) const;
484
486  Quaternion conjugate(void) const;
488  Quaternion invert(void) const;
489
491  Quaternion operator* (double scale) const;
493  Quaternion& operator*=(double scale);
494
496  Quaternion operator/ (double scale) const;
498  Quaternion& operator/=(double scale);
499
501  Quaternion operator+ (const Quaternion& q) const;
503  Quaternion& operator+=(const Quaternion& q);
504
506  Quaternion operator- (const Quaternion& q) const;
508  Quaternion& operator-=(const Quaternion& q);
509
511  Quaternion operator* (const Quaternion& q) const;
513  Quaternion& operator*=(const Quaternion& q);
514
516  Quaternion operator/ (const Quaternion& q) const;
518  Quaternion& operator/=(const Quaternion& q);
519 };
520
521
522
523
526 public:
531
537
540
545
550
555
560 };
563 public:
568
572
575
580
585
590
595 };
598 public:
603
609
612
617
622
627
632 };
635 public:
640
646
649
654
659
664
669 };
670 #endif // GEOMETRY_INCLUDED
Definition: geometry.h:597
Matrix3D skewSymmetric
Definition: geometry.h:600
Point3D translate
Definition: geometry.h:602
Definition: geometry.h:562
Definition: geometry.h:421
Definition: geometry.h:453
Point2D & operator*=(double scale)
Definition: geometry.cpp:26
Point2D & operator-=(const Point2D &pt)
Definition: geometry.cpp:52
Point2D & operator+=(const Point2D &pt)
Definition: geometry.cpp:44
Point2D scale(double scl) const
Definition: geometry.cpp:23
Definition: geometry.h:525
Point3D eulerAngles
Definition: geometry.h:528
Point2D operator*(double scale) const
Definition: geometry.cpp:24
Point3D imag
Definition: geometry.h:458
Definition: geometry.cpp:42
Point3D translate
Definition: geometry.h:567
Point3D translate
Definition: geometry.h:639
Point3D translate
Definition: geometry.h:530
Definition: geometry.h:81
Definition: geometry.h:11
Point2D mult(const Point2D &pt) const
Definition: geometry.cpp:58
Point2D negate(void) const
Definition: geometry.cpp:20
Point2D operator-(void) const
Definition: geometry.cpp:21
Quaternion quaternion
Definition: geometry.h:637
Point2D & operator/=(double scale)
Definition: geometry.cpp:31
double real
Definition: geometry.h:456
Point2D subtract(const Point2D &pt) const
Definition: geometry.cpp:50
Point2D unit(void) const
Definition: geometry.cpp:19
double squareNorm(void) const
Definition: geometry.cpp:39
Point3D direction
Definition: geometry.h:187
Point3D position
Definition: geometry.h:185
Point2D div(const Point2D &pt) const
Definition: geometry.cpp:66
Point3D normal
Definition: geometry.h:162
double & operator[](const int &index)
Definition: geometry.cpp:17
Definition: geometry.h:182
double dot(const Point2D &pt) const
Definition: geometry.cpp:37
double distance
Definition: geometry.h:164
Definition: geometry.h:210
Definition: geometry.h:312
Point2D operator/(double scale) const
Definition: geometry.cpp:25
double p[2]
Definition: geometry.h:14
Point2D operator+(const Point2D &pt) const
Definition: geometry.cpp:43
Matrix3D rotation
Definition: geometry.h:565
Definition: geometry.h:634
Definition: geometry.h:159
double length(void) const
Definition: geometry.cpp:38
double & index(const int &i)
Definition: geometry.cpp:18