1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #ifndef _GEOMETRYUTILS_H_ |
---|
6 | #define _GEOMETRYUTILS_H_ |
---|
7 | |
---|
8 | #include <frams/model/model.h> |
---|
9 | #include <frams/model/modelparts.h> |
---|
10 | #include <frams/util/3d.h> |
---|
11 | #include <frams/util/list.h> |
---|
12 | |
---|
13 | //binary literals standardized in C++14 |
---|
14 | #define b000 0 |
---|
15 | #define b01 1 |
---|
16 | #define b001 1 |
---|
17 | #define b10 2 |
---|
18 | #define b010 2 |
---|
19 | #define b100 4 |
---|
20 | #define b110 6 |
---|
21 | |
---|
22 | namespace CuboidFaces |
---|
23 | { |
---|
24 | enum Face |
---|
25 | { |
---|
26 | NEGATIVE_X = 0, |
---|
27 | POSITIVE_X = 1, |
---|
28 | NEGATIVE_Y = 2, |
---|
29 | POSITIVE_Y = 3, |
---|
30 | NEGATIVE_Z = 4, |
---|
31 | POSITIVE_Z = 5, |
---|
32 | FIRST = 0, |
---|
33 | NUMBER = 6 |
---|
34 | }; |
---|
35 | |
---|
36 | inline bool isPositive(Face f) { return f & b001; } |
---|
37 | inline bool isNegative(Face f) { return !isPositive(f); } |
---|
38 | inline bool isX(Face f) { return (f & b110) == b000; } |
---|
39 | inline bool isY(Face f) { return (f & b110) == b010; } |
---|
40 | inline bool isZ(Face f) { return (f & b110) == b100; } |
---|
41 | } |
---|
42 | |
---|
43 | namespace CylinderBases |
---|
44 | { |
---|
45 | enum Base |
---|
46 | { |
---|
47 | NEGATIVE_X = 0, |
---|
48 | POSITIVE_X = 1, |
---|
49 | FIRST = 0, |
---|
50 | NUMBER = 2 |
---|
51 | }; |
---|
52 | |
---|
53 | inline bool isPositive(Base b) { return b & b001; } |
---|
54 | inline bool isNegative(Base b) { return !isPositive(b); } |
---|
55 | } |
---|
56 | |
---|
57 | namespace QuadrantsXY |
---|
58 | { |
---|
59 | enum QuadrantXY |
---|
60 | { |
---|
61 | NEGATIVE_X_NEGATIVE_Y = 0, |
---|
62 | NEGATIVE_X_POSITIVE_Y = 1, |
---|
63 | POSITIVE_X_NEGATIVE_Y = 2, |
---|
64 | POSITIVE_X_POSITIVE_Y = 3, |
---|
65 | FIRST = 0, |
---|
66 | NUMBER = 4 |
---|
67 | }; |
---|
68 | |
---|
69 | inline bool isPositiveX(QuadrantXY q) { return (q & b10) != 0; } |
---|
70 | inline bool isNegativeX(QuadrantXY q) { return !isPositiveX(q); } |
---|
71 | inline bool isPositiveY(QuadrantXY q) { return q & b01; } |
---|
72 | inline bool isNegativeY(QuadrantXY q) { return !isPositiveY(q); } |
---|
73 | } |
---|
74 | |
---|
75 | namespace QuadrantsYZ |
---|
76 | { |
---|
77 | enum QuadrantYZ |
---|
78 | { |
---|
79 | NEGATIVE_Y_NEGATIVE_Z = 0, |
---|
80 | NEGATIVE_Y_POSITIVE_Z = 1, |
---|
81 | POSITIVE_Y_NEGATIVE_Z = 2, |
---|
82 | POSITIVE_Y_POSITIVE_Z = 3, |
---|
83 | FIRST = 0, |
---|
84 | NUMBER = 4 |
---|
85 | }; |
---|
86 | |
---|
87 | inline bool isPositiveY(QuadrantYZ q) { return (q & b10) != 0; } |
---|
88 | inline bool isNegativeY(QuadrantYZ q) { return !isPositiveY(q); } |
---|
89 | inline bool isPositiveZ(QuadrantYZ q) { return q & b01; } |
---|
90 | inline bool isNegativeZ(QuadrantYZ q) { return !isPositiveZ(q); } |
---|
91 | } |
---|
92 | |
---|
93 | namespace QuadrantsZX |
---|
94 | { |
---|
95 | enum QuadrantZX |
---|
96 | { |
---|
97 | NEGATIVE_Z_NEGATIVE_X = 0, |
---|
98 | NEGATIVE_Z_POSITIVE_X = 1, |
---|
99 | POSITIVE_Z_NEGATIVE_X = 2, |
---|
100 | POSITIVE_Z_POSITIVE_X = 3, |
---|
101 | FIRST = 0, |
---|
102 | NUMBER = 4 |
---|
103 | }; |
---|
104 | |
---|
105 | inline bool isPositiveZ(QuadrantZX q) { return (q & b10) != 0; } |
---|
106 | inline bool isNegativeZ(QuadrantZX q) { return !isPositiveZ(q); } |
---|
107 | inline bool isPositiveX(QuadrantZX q) { return (q & b01) != 0; } |
---|
108 | inline bool isNegativeX(QuadrantZX q) { return !isPositiveX(q); } |
---|
109 | } |
---|
110 | |
---|
111 | namespace Octants |
---|
112 | { |
---|
113 | enum Octant |
---|
114 | { |
---|
115 | NEGATIVE_X_NEGATIVE_Y_NEGATIVE_Z = 0, |
---|
116 | NEGATIVE_X_NEGATIVE_Y_POSITIVE_Z = 1, |
---|
117 | NEGATIVE_X_POSITIVE_Y_NEGATIVE_Z = 2, |
---|
118 | NEGATIVE_X_POSITIVE_Y_POSITIVE_Z = 3, |
---|
119 | POSITIVE_X_NEGATIVE_Y_NEGATIVE_Z = 4, |
---|
120 | POSITIVE_X_NEGATIVE_Y_POSITIVE_Z = 5, |
---|
121 | POSITIVE_X_POSITIVE_Y_NEGATIVE_Z = 6, |
---|
122 | POSITIVE_X_POSITIVE_Y_POSITIVE_Z = 7, |
---|
123 | FIRST = 0, |
---|
124 | NUMBER = 8 |
---|
125 | }; |
---|
126 | |
---|
127 | inline bool isPositiveX(Octant o) { return (o & b100) != 0; } |
---|
128 | inline bool isNegativeX(Octant o) { return !isPositiveX(o); } |
---|
129 | inline bool isPositiveY(Octant o) { return (o & b010) != 0; } |
---|
130 | inline bool isNegativeY(Octant o) { return !isPositiveY(o); } |
---|
131 | inline bool isPositiveZ(Octant o) { return o & b001; } |
---|
132 | inline bool isNegativeZ(Octant o) { return !isPositiveZ(o); } |
---|
133 | } |
---|
134 | |
---|
135 | namespace GeometryUtils |
---|
136 | { |
---|
137 | double pointPosition(const int pointIndex, const int numberOfPoints); |
---|
138 | double pointOnAxis(const double scale, const double position); |
---|
139 | double pointOnAxis(const double scale, const int pointIndex, const int numberOfPoints); |
---|
140 | double combination(const double value1, const double value2, const double position); |
---|
141 | double combination(const double value1, const double value2, const int pointIndex, const int numberOfPoints); |
---|
142 | bool isPointInsideModelExcludingPart(const Pt3D &point, const Model *model, const int excludedPartIndex); |
---|
143 | bool isPointInsideModel(const Pt3D &point, const Model &model); |
---|
144 | bool isPointInsidePart(const Pt3D &point, const Part *part); |
---|
145 | bool isPointStrictlyInsidePart(const Pt3D &point, const Part *part); |
---|
146 | bool isPointInsideEllipsoid(const Pt3D &point, const Part *part); |
---|
147 | bool isPointStrictlyInsideEllipsoid(const Pt3D &point, const Part *part); |
---|
148 | bool isPointInsideCuboid(const Pt3D &point, const Part *part); |
---|
149 | bool isPointStrictlyInsideCuboid(const Pt3D &point, const Part *part); |
---|
150 | bool isPointInsideCylinder(const Pt3D &point, const Part *part); |
---|
151 | bool isPointStrictlyInsideCylinder(const Pt3D &point, const Part *part); |
---|
152 | void findSizesAndAxesOfPointsGroup(SListTempl<Pt3D> &points, Pt3D &sizes, Orient &axes); |
---|
153 | void findSizeAndAxisOfPointsGroup(const SListTempl<Pt3D> &points, double &size, Pt3D &axis); |
---|
154 | double findTwoFurthestPoints(const SListTempl<Pt3D> &points, int &index1, int &index2); |
---|
155 | void createAxisFromTwoPoints(Pt3D &axis, const Pt3D &point1, const Pt3D &point2); |
---|
156 | void orthographicProjectionToPlane(SListTempl<Pt3D> &points, const Pt3D &planeNormalVector); |
---|
157 | double pointDistanceToPlane(const Pt3D &point, const Pt3D &planeNormalVector); |
---|
158 | void getRectangleApicesFromCuboid(const Part *part, const CuboidFaces::Face face, Pt3D &apex1, Pt3D &apex2, Pt3D &apex3, Pt3D &apex4); |
---|
159 | void getRectangleApices(const double width, const double height, const Pt3D &position, const Orient &orient, Pt3D &apex1, Pt3D &apex2, Pt3D &apex3, Pt3D &apex4); |
---|
160 | void getNextEllipseSegmentationPoint(const double d, const double a, const double b, double &x, double &y); |
---|
161 | double ellipsoidArea(const Pt3D &sizes); |
---|
162 | double ellipsoidArea(const double a, const double b, const double c); |
---|
163 | double ellipsePerimeter(const double a, const double b); |
---|
164 | } |
---|
165 | |
---|
166 | #endif |
---|