Loading...
Searching...
No Matches
RectPrism.h
1
19
20#pragma once
21#include "ConvexObj.h"
22#include "Rotation.h"
23
24namespace CommonMath
25{
26
28
33 class RectPrism : public ConvexObj
34 {
35 public:
37
47 RectPrism(Vec3 center, Vec3 sideLengths) : _centerPoint(center), _sideLengths(sideLengths)
48 {
49 // Side lengths must be positive
50 assert(sideLengths.x > 0 && sideLengths.y > 0 && sideLengths.z > 0);
51 }
52
54
63 Boundary GetTangentPlane(Vec3 const testPoint)
64 {
65 // Write testPoint in the local coordinate frame
66 Vec3 p = testPoint - _centerPoint;
67
68 // Compute Euclidean projection onto cube
69 Boundary bound;
70 for (int i = 0; i < 3; i++)
71 {
72 if (p[i] < -_sideLengths[i] / 2)
73 {
74 bound.point[i] = -_sideLengths[i] / 2;
75 }
76 else if (p[i] > _sideLengths[i] / 2)
77 {
78 bound.point[i] = _sideLengths[i] / 2;
79 }
80 else
81 {
82 bound.point[i] = p[i];
83 }
84 }
85
86 // Convert point back to global coordinates and get unit normal
87 bound.point = bound.point + _centerPoint;
88 bound.normal = (testPoint - bound.point).GetUnitVector();
89 return bound;
90 }
91
93
97 bool IsPointInside(Vec3 const testPoint)
98 {
99 // Write testPoint in the local coordinate frame
100 Vec3 p = testPoint - _centerPoint;
101
102 // Check if testPoint is inside the rectangular prism
103 return (fabs(p.x) <= _sideLengths.x / 2) && (fabs(p.y) <= _sideLengths.y / 2) &&
104 (fabs(p.z) <= _sideLengths.z / 2);
105 }
106
107 Vec3 GetRandomPositionInside(std::default_random_engine randomGen)
108 {
109 std::uniform_real_distribution<double> posX(-_sideLengths.x / 2, _sideLengths.x / 2);
110 std::uniform_real_distribution<double> posY(-_sideLengths.y / 2, _sideLengths.y / 2);
111 std::uniform_real_distribution<double> posZ(-_sideLengths.z / 2, _sideLengths.z / 2);
112
113 Vec3 randomPos = Vec3(posX(randomGen), posY(randomGen), posZ(randomGen));
114 return _centerPoint + randomPos;
115 }
116
118 void SetCenterPoint(Vec3 center)
119 {
120 _centerPoint = center;
121 }
122
123 void SetSideLengths(Vec3 sideLen)
124 {
125 _sideLengths = sideLen;
126 }
127
128 private:
129 Vec3 _centerPoint;
130 Vec3 _sideLengths;
131 };
132
133} // namespace CommonMath
An abstract class that defines the required properties of convex objects.
Definition ConvexObj.h:66
Boundary GetTangentPlane(Vec3 const testPoint)
Finds a separating plane between the prism and given point.
Definition RectPrism.h:63
bool IsPointInside(Vec3 const testPoint)
Check whether a given point is inside or on the boundary of the prism.
Definition RectPrism.h:97
RectPrism(Vec3 center, Vec3 sideLengths)
Constructor.
Definition RectPrism.h:47
void SetCenterPoint(Vec3 center)
Sets the location of the center of the prism in global coordinates.
Definition RectPrism.h:118
void SetSideLengths(Vec3 sideLen)
Sets the side lengths of the prism.
Definition RectPrism.h:123
3D vector class with common vector operations.
Definition Vec3.h:36
double z
the three components of the vector
Definition Vec3.h:38
A struct defining the properties of a plane.
Definition ConvexObj.h:31
Vec3 point
A point on the plane.
Definition ConvexObj.h:55
Vec3 normal
A unit vector normal to the plane.
Definition ConvexObj.h:56