[191] | 1 | // This file is a part of the Framsticks GDK. |
---|
[271] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[191] | 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 | |
---|
[260] | 13 | |
---|
| 14 | /*Binary literals like 0b010 are standardized only in C++14. We use macros as they are compatible with older compilers too. |
---|
| 15 | |
---|
| 16 | 3-bit numbers are used when iterating through octants in a 3D space. Example: when creating points that cover the surface |
---|
| 17 | of an ellipsoid, the points are only created for the positive octant (x, y, and z coordinates are positive). Points in |
---|
| 18 | the remaining 7 octants are created by reflecting points from the positive octant through the appropriate planes defined by |
---|
| 19 | pairs of axes. |
---|
| 20 | |
---|
| 21 | 2-bit numbers are used for 2D. Example: cylinders are aligned along the x axis so that both bases are parallel to |
---|
| 22 | the yz plane. When points are created along the edge of the base (these will be used later to create points along the side |
---|
| 23 | of the cylinder), only y and z axes are important, so quadrants of the 2D are sufficient. Just as in the 3D example above, |
---|
| 24 | only points for the positive quadrant, QuadrantYZ, are created, and points of the remaining quadrants are created by reflection. |
---|
| 25 | QuadrantXY and QuadrantZX enumerations are never used and are provided only for completeness. |
---|
| 26 | */ |
---|
[236] | 27 | #define b000 0 |
---|
| 28 | #define b01 1 |
---|
| 29 | #define b001 1 |
---|
| 30 | #define b10 2 |
---|
| 31 | #define b010 2 |
---|
| 32 | #define b100 4 |
---|
| 33 | #define b110 6 |
---|
| 34 | |
---|
[260] | 35 | |
---|
| 36 | |
---|
[191] | 37 | namespace CuboidFaces |
---|
| 38 | { |
---|
| 39 | enum Face |
---|
| 40 | { |
---|
| 41 | NEGATIVE_X = 0, |
---|
| 42 | POSITIVE_X = 1, |
---|
| 43 | NEGATIVE_Y = 2, |
---|
| 44 | POSITIVE_Y = 3, |
---|
| 45 | NEGATIVE_Z = 4, |
---|
| 46 | POSITIVE_Z = 5, |
---|
| 47 | FIRST = 0, |
---|
| 48 | NUMBER = 6 |
---|
| 49 | }; |
---|
| 50 | |
---|
[236] | 51 | inline bool isPositive(Face f) { return f & b001; } |
---|
[191] | 52 | inline bool isNegative(Face f) { return !isPositive(f); } |
---|
[236] | 53 | inline bool isX(Face f) { return (f & b110) == b000; } |
---|
| 54 | inline bool isY(Face f) { return (f & b110) == b010; } |
---|
| 55 | inline bool isZ(Face f) { return (f & b110) == b100; } |
---|
[191] | 56 | } |
---|
| 57 | |
---|
| 58 | namespace CylinderBases |
---|
| 59 | { |
---|
| 60 | enum Base |
---|
| 61 | { |
---|
| 62 | NEGATIVE_X = 0, |
---|
| 63 | POSITIVE_X = 1, |
---|
| 64 | FIRST = 0, |
---|
| 65 | NUMBER = 2 |
---|
| 66 | }; |
---|
| 67 | |
---|
[236] | 68 | inline bool isPositive(Base b) { return b & b001; } |
---|
[191] | 69 | inline bool isNegative(Base b) { return !isPositive(b); } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | namespace QuadrantsXY |
---|
| 73 | { |
---|
| 74 | enum QuadrantXY |
---|
| 75 | { |
---|
| 76 | NEGATIVE_X_NEGATIVE_Y = 0, |
---|
| 77 | NEGATIVE_X_POSITIVE_Y = 1, |
---|
| 78 | POSITIVE_X_NEGATIVE_Y = 2, |
---|
| 79 | POSITIVE_X_POSITIVE_Y = 3, |
---|
| 80 | FIRST = 0, |
---|
| 81 | NUMBER = 4 |
---|
| 82 | }; |
---|
| 83 | |
---|
[238] | 84 | inline bool isPositiveX(QuadrantXY q) { return (q & b10) != 0; } |
---|
[191] | 85 | inline bool isNegativeX(QuadrantXY q) { return !isPositiveX(q); } |
---|
[236] | 86 | inline bool isPositiveY(QuadrantXY q) { return q & b01; } |
---|
[191] | 87 | inline bool isNegativeY(QuadrantXY q) { return !isPositiveY(q); } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | namespace QuadrantsYZ |
---|
| 91 | { |
---|
| 92 | enum QuadrantYZ |
---|
| 93 | { |
---|
| 94 | NEGATIVE_Y_NEGATIVE_Z = 0, |
---|
| 95 | NEGATIVE_Y_POSITIVE_Z = 1, |
---|
| 96 | POSITIVE_Y_NEGATIVE_Z = 2, |
---|
| 97 | POSITIVE_Y_POSITIVE_Z = 3, |
---|
| 98 | FIRST = 0, |
---|
| 99 | NUMBER = 4 |
---|
| 100 | }; |
---|
| 101 | |
---|
[238] | 102 | inline bool isPositiveY(QuadrantYZ q) { return (q & b10) != 0; } |
---|
[191] | 103 | inline bool isNegativeY(QuadrantYZ q) { return !isPositiveY(q); } |
---|
[236] | 104 | inline bool isPositiveZ(QuadrantYZ q) { return q & b01; } |
---|
[191] | 105 | inline bool isNegativeZ(QuadrantYZ q) { return !isPositiveZ(q); } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | namespace QuadrantsZX |
---|
| 109 | { |
---|
| 110 | enum QuadrantZX |
---|
| 111 | { |
---|
| 112 | NEGATIVE_Z_NEGATIVE_X = 0, |
---|
| 113 | NEGATIVE_Z_POSITIVE_X = 1, |
---|
| 114 | POSITIVE_Z_NEGATIVE_X = 2, |
---|
| 115 | POSITIVE_Z_POSITIVE_X = 3, |
---|
| 116 | FIRST = 0, |
---|
| 117 | NUMBER = 4 |
---|
| 118 | }; |
---|
| 119 | |
---|
[238] | 120 | inline bool isPositiveZ(QuadrantZX q) { return (q & b10) != 0; } |
---|
[191] | 121 | inline bool isNegativeZ(QuadrantZX q) { return !isPositiveZ(q); } |
---|
[238] | 122 | inline bool isPositiveX(QuadrantZX q) { return (q & b01) != 0; } |
---|
[191] | 123 | inline bool isNegativeX(QuadrantZX q) { return !isPositiveX(q); } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | namespace Octants |
---|
| 127 | { |
---|
| 128 | enum Octant |
---|
| 129 | { |
---|
| 130 | NEGATIVE_X_NEGATIVE_Y_NEGATIVE_Z = 0, |
---|
| 131 | NEGATIVE_X_NEGATIVE_Y_POSITIVE_Z = 1, |
---|
| 132 | NEGATIVE_X_POSITIVE_Y_NEGATIVE_Z = 2, |
---|
| 133 | NEGATIVE_X_POSITIVE_Y_POSITIVE_Z = 3, |
---|
| 134 | POSITIVE_X_NEGATIVE_Y_NEGATIVE_Z = 4, |
---|
| 135 | POSITIVE_X_NEGATIVE_Y_POSITIVE_Z = 5, |
---|
| 136 | POSITIVE_X_POSITIVE_Y_NEGATIVE_Z = 6, |
---|
| 137 | POSITIVE_X_POSITIVE_Y_POSITIVE_Z = 7, |
---|
| 138 | FIRST = 0, |
---|
| 139 | NUMBER = 8 |
---|
| 140 | }; |
---|
| 141 | |
---|
[238] | 142 | inline bool isPositiveX(Octant o) { return (o & b100) != 0; } |
---|
[191] | 143 | inline bool isNegativeX(Octant o) { return !isPositiveX(o); } |
---|
[238] | 144 | inline bool isPositiveY(Octant o) { return (o & b010) != 0; } |
---|
[191] | 145 | inline bool isNegativeY(Octant o) { return !isPositiveY(o); } |
---|
[236] | 146 | inline bool isPositiveZ(Octant o) { return o & b001; } |
---|
[191] | 147 | inline bool isNegativeZ(Octant o) { return !isPositiveZ(o); } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | namespace GeometryUtils |
---|
| 151 | { |
---|
| 152 | double pointPosition(const int pointIndex, const int numberOfPoints); |
---|
| 153 | double pointOnAxis(const double scale, const double position); |
---|
| 154 | double pointOnAxis(const double scale, const int pointIndex, const int numberOfPoints); |
---|
| 155 | double combination(const double value1, const double value2, const double position); |
---|
| 156 | double combination(const double value1, const double value2, const int pointIndex, const int numberOfPoints); |
---|
| 157 | bool isPointInsideModelExcludingPart(const Pt3D &point, const Model *model, const int excludedPartIndex); |
---|
| 158 | bool isPointInsideModel(const Pt3D &point, const Model &model); |
---|
| 159 | bool isPointInsidePart(const Pt3D &point, const Part *part); |
---|
| 160 | bool isPointStrictlyInsidePart(const Pt3D &point, const Part *part); |
---|
| 161 | bool isPointInsideEllipsoid(const Pt3D &point, const Part *part); |
---|
| 162 | bool isPointStrictlyInsideEllipsoid(const Pt3D &point, const Part *part); |
---|
| 163 | bool isPointInsideCuboid(const Pt3D &point, const Part *part); |
---|
| 164 | bool isPointStrictlyInsideCuboid(const Pt3D &point, const Part *part); |
---|
| 165 | bool isPointInsideCylinder(const Pt3D &point, const Part *part); |
---|
| 166 | bool isPointStrictlyInsideCylinder(const Pt3D &point, const Part *part); |
---|
| 167 | void findSizesAndAxesOfPointsGroup(SListTempl<Pt3D> &points, Pt3D &sizes, Orient &axes); |
---|
| 168 | void findSizeAndAxisOfPointsGroup(const SListTempl<Pt3D> &points, double &size, Pt3D &axis); |
---|
| 169 | double findTwoFurthestPoints(const SListTempl<Pt3D> &points, int &index1, int &index2); |
---|
| 170 | void createAxisFromTwoPoints(Pt3D &axis, const Pt3D &point1, const Pt3D &point2); |
---|
| 171 | void orthographicProjectionToPlane(SListTempl<Pt3D> &points, const Pt3D &planeNormalVector); |
---|
| 172 | double pointDistanceToPlane(const Pt3D &point, const Pt3D &planeNormalVector); |
---|
| 173 | void getRectangleApicesFromCuboid(const Part *part, const CuboidFaces::Face face, Pt3D &apex1, Pt3D &apex2, Pt3D &apex3, Pt3D &apex4); |
---|
| 174 | void getRectangleApices(const double width, const double height, const Pt3D &position, const Orient &orient, Pt3D &apex1, Pt3D &apex2, Pt3D &apex3, Pt3D &apex4); |
---|
| 175 | void getNextEllipseSegmentationPoint(const double d, const double a, const double b, double &x, double &y); |
---|
| 176 | double ellipsoidArea(const Pt3D &sizes); |
---|
| 177 | double ellipsoidArea(const double a, const double b, const double c); |
---|
| 178 | double ellipsePerimeter(const double a, const double b); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | #endif |
---|