6 #ifndef UTILS_GEOMETRY_STRUCTURES_H 7 #define UTILS_GEOMETRY_STRUCTURES_H 10 #include <glm/glm.hpp> 11 #include <Utils/ExtGLM.h> 12 #include <Utils/Property.h> 28 static inline glm::vec3 AxisToVec3(
const geom::Axis axis)
33 return glm::vec3(1.0f, 0.0f, 0.0f);
35 return glm::vec3(0.0f, 1.0f, 0.0f);
37 return glm::vec3(0.0f, 0.0f, 1.0f);
39 throw std::invalid_argument(
"Undefined axis symbol.");
43 const static glm::vec3 Vec3X = AxisToVec3(Axis::x);
45 const static glm::vec3 Vec3Y = AxisToVec3(Axis::y);
47 const static glm::vec3 Vec3Z = AxisToVec3(Axis::z);
50 static inline glm::vec4 AxisToVec4(
const geom::Axis axis)
52 return glm::vec4(geom::AxisToVec3(axis), 0.0f);
56 const static glm::vec4 Vec4X = AxisToVec4(Axis::x);
58 const static glm::vec4 Vec4Y = AxisToVec4(Axis::y);
60 const static glm::vec4 Vec4Z = AxisToVec4(Axis::z);
68 using Vec = glm::vec<N, float>;
74 LineSegment(
const Vec &init_point1,
const Vec &init_point2)
75 : point1(init_point1), point2(init_point2)
81 return glm::distance(point1, point2);
87 return glm::distance2(point1, point2);
93 return glm::normalize(point2 - point1);
99 return point2 - point1;
112 LineSegment2(
const Vec &init_point1,
const Vec &init_point2)
122 LineSegment3(
float x1,
float y1,
float z1,
float x2,
float y2,
float z2)
126 LineSegment3(
const Vec &init_point1,
const Vec &init_point2)
135 using Vec = glm::vec<N, float>;
136 Vec point, direction;
138 Line(
const Vec &_point,
const Vec &_direction)
139 : point(_point), direction(glm::normalize(_direction))
155 using Vec = glm::vec<N, float>;
160 Ray(
const Vec &init_origin,
const Vec &init_direction)
161 : origin(init_origin), direction(init_direction)
172 template <
int N,
typename T>
175 glm::vec<N, T> center = glm::vec<N, T>(0);
178 TSphere(
const glm::vec<N, T> &_center,
const T _radius)
179 : center(_center), radius(_radius)
181 T GetDiameter()
const 183 return static_cast<T
>(2) * radius;
185 void SetDiameter(T value)
187 radius = value /
static_cast<T
>(2);
201 using Vec = glm::vec<D, float>;
210 throw std::invalid_argument(
"Triangle index out of range.");
212 return points[point_id];
220 throw std::invalid_argument(
"Triangle index out of range.");
222 return points[point_id];
228 return (points[0] + points[1] + points[2]) / 3.0f;
237 Triangle2(
const glm::vec2 &p0,
const glm::vec2 &p1,
const glm::vec2 &p2)
250 Triangle3(
const glm::vec3 &p0,
const glm::vec3 &p1,
const glm::vec3 &p2)
260 return glm::normalize(glm::cross(points[1] - points[0], points[2] - points[0]));
267 template <
typename T>
270 using Vec2 = glm::tvec2<T>;
272 Vec2 position = Vec2((T)0);
273 Vec2 position2 = Vec2((T)0);
275 TRect() : x(
this), y(
this), width(
this), height(
this), x2(
this), y2(
this), size(
this) {};
276 TRect(
const T x1,
const T y1,
const T x2,
const T y2) :
TRect()
280 TRect(
const Vec2 &point1,
const Vec2 &point2) :
TRect()
285 void SetSize(
const Vec2 &new_size)
287 position2 = position + new_size;
290 TRect(
const Vec2 &top_left_origin,
const T size_width,
const T size_height) :
TRect()
292 position = top_left_origin;
293 SetSize(Vec2(size_width, size_height));
296 void Set(
const TRect &rect)
298 Set(rect.position, rect.position + rect.size);
300 void Set(
const T t_x1,
const T t_y1,
const T t_x2,
const T t_y2)
302 position.x = glm::min(t_x1, t_x2);
303 position.y = glm::min(t_y1, t_y2);
304 position2.x = glm::max(t_x1, t_x2);
305 position2.y = glm::max(t_y1, t_y2);
307 void Set(
const Vec2 &point1,
const Vec2 &point2)
309 Set(point1.x, point1.y, point2.x, point2.y);
314 return position2 - position;
334 return position.x + GetSize().x;
338 return position.y + GetSize().y;
341 void SetX(
const T value)
345 void SetY(
const T value)
349 void SetWidth(
const T value)
351 position2.x = position.x + value;
353 void SetHeight(
const T value)
355 position2.y = position.y + value;
357 void SetX2(
const T value)
361 void SetY2(
const T value)
366 Vec2 Center(
void)
const 368 return (position + position2) / (T)2;
370 bool Contains(
const Vec2 &point)
const 372 return point.x >= position.x && point.x < GetX2() && point.y >= position.y && point.y < GetY2();
376 return (position2.x - position.x) * (position2.y - position.y);
400 template <
int dimensions,
typename Type>
403 using Vec = glm::vec<dimensions, Type, glm::highp>;
404 Vec min = Vec(std::numeric_limits<Type>::max());
405 Vec max = Vec(std::numeric_limits<Type>::lowest());
408 TAabb(
const Vec &corner1,
const Vec &corner2)
410 min = (glm::min)(corner1, corner2);
411 max = (glm::max)(corner1, corner2);
423 return min + GetSize() / Type(2);
429 return (glm::distance2)(min, max);
435 return (glm::distance)(min, max);
441 for (
int i = 0; i < dimensions; i++)
443 if (point[i] < min[i] || point[i] > max[i])
460 min = glm::min(min, point);
461 max = glm::max(max, point);
467 min = glm::min(min, aabb.min);
468 max = glm::max(max, aabb.max);
474 return glm::all(glm::lessThan(min, max));
482 result.min = glm::max(min, aabb.min);
483 result.max = glm::min(max, aabb.max);
492 aabb.min.x < max.x && min.x < aabb.max.x
493 && aabb.min.y < max.y && min.y < aabb.max.y
494 && aabb.min.z < max.z && min.z < aabb.max.z;
501 Type result = dim[0];
502 for (
unsigned char i = 1; i < dimensions; i++)
512 min = Vec(std::numeric_limits<Type>::max());
513 max = Vec(std::numeric_limits<Type>::lowest());
621 float distance{ 0.0f };
623 glm::vec3 normal{ 0.0f, 0.0f, 1.0f };
626 Plane(
const glm::vec3 &origin,
const glm::vec3 &normal)
628 this->normal = glm::normalize(normal);
629 this->distance = -glm::dot(normal, -origin);
631 bool operator==(
const Plane &other)
const 633 return (this->distance == other.
distance) && (this->normal == other.
normal);
635 bool operator!=(
const Plane &other)
const 637 return !(*
this == other);
649 glm::vec3 direction{ glm::vec3(0.0f, 0.0f, 1.0f) };
652 HalfPlane(
const Line3 &border_line,
const glm::vec3 &halfplane_direction)
654 this->border = border_line;
655 this->direction = halfplane_direction;
667 float radius{ 1.0f };
672 : central(line_segment), radius(init_radius)
675 Capsule(
const glm::vec3 &point1,
const glm::vec3 &point2,
float init_radius)
676 : central(point1, point2), radius(init_radius)
688 float radius1{ 1.0f };
690 float radius2{ 1.0f };
695 : central(line_segment), radius1(init_radius1), radius2(init_radius2)
698 ConicalCapsule(
const glm::vec3 point1,
const float init_radius1,
const glm::vec3 point2,
const float init_radius2)
699 : central(point1, point2), radius1(init_radius1), radius2(init_radius2)
711 return (1.f - factor) * radius1 + factor * radius2;
725 void Set(
const glm::vec3 *points,
const size_t num_of_points)
727 for (
size_t i = 0; i < num_of_points; i++)
729 for (
size_t j = 0; j < axes.size(); j++)
731 const auto t = glm::dot(points[i], axes[j].direction);
732 axes[j].min = std::min(axes[j].min, t);
733 axes[j].max = std::max(axes[j].max, t);
741 return !axes.empty() &&
std_ext::AllOf(axes, [](
const auto & a) {
return a.IsValid(); });
747 for (
size_t i = 0; i < axes.size(); i++)
749 if (axes[i].min > other.axes[i].max || axes[i].max < other.axes[i].min)
760 for (
auto &axis : axes)
762 auto t = glm::dot(point, axis.direction);
763 if (axis.min > t || axis.max < t)
773 KDop(
const std::vector<glm::vec3> &dirs)
775 axes.resize(dirs.size());
776 for (
size_t i = 0; i < dirs.size(); i++)
778 axes[i].direction = glm::normalize(dirs[i]);
787 float min{ std::numeric_limits<float>::max() };
788 float max{ std::numeric_limits<float>::min() };
795 std::vector<PrincipalAxis> axes;
799 const static std::vector<glm::vec3> KDOP_3_DIRECTIONS
801 glm::vec3(1.0, 0.0, 0.0),
802 glm::vec3(0.0, 1.0, 0.0),
803 glm::vec3(0.0, 0.0, 1.0),
806 const static std::vector<glm::vec3> KDOP_4_DIRECTIONS
808 glm::vec3(1.0, 1.0, 1.0),
809 glm::vec3(1.0, 1.0, -1.0),
810 glm::vec3(1.0, -1.0, 1.0),
811 glm::vec3(1.0, -1.0, -1.0)
814 const static std::vector<glm::vec3> KDOP_7_DIRECTIONS
816 glm::vec3(1.0, 0.0, 0.0),
817 glm::vec3(0.0, 1.0, 0.0),
818 glm::vec3(0.0, 0.0, 1.0),
820 glm::vec3(1.0, 1.0, 1.0),
821 glm::vec3(1.0, 1.0, -1.0),
822 glm::vec3(1.0, -1.0, 1.0),
823 glm::vec3(1.0, -1.0, -1.0)
826 const static std::vector<glm::vec3> KDOP_13_DIRECTIONS
828 glm::vec3(1.0, 0.0, 0.0),
829 glm::vec3(0.0, 1.0, 0.0),
830 glm::vec3(0.0, 0.0, 1.0),
832 glm::vec3(1.0, 1.0, 1.0),
833 glm::vec3(1.0, 1.0, -1.0),
834 glm::vec3(1.0, -1.0, 1.0),
835 glm::vec3(1.0, -1.0, -1.0),
837 glm::vec3(0.0, 1.0, 1.0),
838 glm::vec3(0.0, 1.0, -1.0),
839 glm::vec3(1.0, 0.0, 1.0),
840 glm::vec3(1.0, 0.0, -1.0),
841 glm::vec3(1.0, 1.0, 0.0),
842 glm::vec3(1.0, -1.0, 0.0),
Vec ToVector() const
Converts the line to the vector from point1 to point2.
Definition: GeometryStructures.h:97
k-DOP bounding volume structure with 3 principal axes. It is essentially AABB.
Definition: GeometryStructures.h:846
void Clear()
Resets min and max to init values.
Definition: GeometryStructures.h:510
void Include(const Vec &point)
Enlarges aabb so that it covers also defined point.
Definition: GeometryStructures.h:458
Definition of a line segment in a 3 dimensional space.
Definition: GeometryStructures.h:118
bool AreIntersected(const TAabb< dimensions, Type > &aabb) const
Checks if aabb has an intersection with defined bounding box.
Definition: GeometryStructures.h:489
k-DOP bounding volume structure with 13 principal axes.
Definition: GeometryStructures.h:873
Type Volume()
Returns volume of bounding box.
Definition: GeometryStructures.h:498
float GetLength() const
Returns length of line segment = distance between point1 and point2.
Definition: GeometryStructures.h:79
Definition of a 2 dimensional triangle.
Definition: GeometryStructures.h:233
float GetInterpolatedRadius(float factor) const
Returns a radius in the specific central part of a capsule.
Definition: GeometryStructures.h:708
Vec GetCenter() const
Returns center point of bounding box.
Definition: GeometryStructures.h:421
void Inflate(float d)
Inflate in all directions.
Definition: GeometryStructures.h:517
Template triangle in an arbitrary dimension.
Definition: GeometryStructures.h:199
float distance
Distance from the origin in the direction of a normal.
Definition: GeometryStructures.h:621
const Vec & operator[](size_t point_id) const
Returns point with a specified index. Calling Triangle[0] is equivalent to Triangle.points[0].
Definition: GeometryStructures.h:216
bool AreIntersected(const KDop &other)
Checks if the k-dop has an intersection with other k-dop.
Definition: GeometryStructures.h:745
Template ray in an arbitrary dimension.
Definition: GeometryStructures.h:153
Vec GetCentroid() const
Returns a centroid of the three triangle points.
Definition: GeometryStructures.h:226
void Include(const TAabb< dimensions, Type > &aabb)
Enlarges aabb so that it covers also area of specified aabb.
Definition: GeometryStructures.h:465
auto AllOf(const C &container, const P &Predicate) -> bool
Simplified wrapper for std::all_of.
Definition: ExtSTD.h:841
Definition of a 3 dimensional capsule with different radiuses at each end.
Definition: GeometryStructures.h:683
bool IsLequal(const glm::vec< N, float > &vec1, const glm::vec< N, float > &vec2, const float epsilon=FLT_EPSILON)
Returns true if all components of vec1 are less or equal then vec2 within defined epsilon threshold...
Definition: ExtGLM.h:364
Line3 border
Line representing border of the half-plane.
Definition: GeometryStructures.h:647
Vec & operator[](size_t point_id)
Returns point with a specified index. Calling Triangle[0] is equivalent to Triangle.points[0].
Definition: GeometryStructures.h:206
Template spheroid of in an arbitrary dimension.
Definition: GeometryStructures.h:173
bool IsValid() const
Checks if structure is valid. Invalid structure may have max < min.
Definition: GeometryStructures.h:472
LineSegment3 central
Central line of a capsule.
Definition: GeometryStructures.h:686
TAabb< dimensions, Type > GetIntersection(const TAabb< dimensions, Type > &aabb) const
Returns smaller bounding box computed as an intersection.
Definition: GeometryStructures.h:478
Definition of a 3 dimensional plane.
Definition: GeometryStructures.h:618
Definition of abstract k-DOP bounding volume structure.
Definition: GeometryStructures.h:718
float GetLengthSqr() const
Returns square length of line segment = square distance between point1 and point2.
Definition: GeometryStructures.h:85
LineSegment3 central
Central line of a capsule.
Definition: GeometryStructures.h:665
Vec GetSize() const
Returns sizing vector.
Definition: GeometryStructures.h:415
bool IsValid() const
Checks if all.
Definition: GeometryStructures.h:739
float GetDiagonalSqr() const
Returns square size of diagonal.
Definition: GeometryStructures.h:427
Definition of a 3 dimensional half-plane.
Definition: GeometryStructures.h:644
Template axis aligned bounding box in an arbitrary dimension.
Definition: GeometryStructures.h:401
Template line in an arbitrary dimension.
Definition: GeometryStructures.h:133
Definition of a line segment in a 2 dimensional space.
Definition: GeometryStructures.h:104
glm::vec3 normal
Orientation of the plane.
Definition: GeometryStructures.h:623
Definition of a 3 dimensional triangle.
Definition: GeometryStructures.h:246
Set of structures for geometry and linear algebra.
Definition: GeometryStructures.h:17
Vec GetDirection() const
Returns direction of the line as a normalized vector from point1 to point2.
Definition: GeometryStructures.h:91
bool IsIncluded(const glm::vec3 &point)
Checks if point is included in range of k-dop.
Definition: GeometryStructures.h:758
genType Clamp01(genType x)
Clamps a value or individual vector components to the range 0-1.
Definition: ExtGLM.h:241
void Set(const glm::vec3 *points, const size_t num_of_points)
Set k-dop so that it covers all specified points.
Definition: GeometryStructures.h:725
glm::vec3 GetNormal() const
Calculates a normal vector of a triangle using counter-clockwise order.
Definition: GeometryStructures.h:258
Axis
Enumeration for axes of a 3 dimensional space.
Definition: GeometryStructures.h:20
k-DOP bounding volume structure with 4 principal axes.
Definition: GeometryStructures.h:855
Template rectangle in an arbitrary dimension.
Definition: GeometryStructures.h:268
Definition of a 3 dimensional capsule.
Definition: GeometryStructures.h:662
k-DOP bounding volume structure with 7 principal axes.
Definition: GeometryStructures.h:864
Template line segment in an arbitrary dimension.
Definition: GeometryStructures.h:66
bool IsIncluded(const Vec &point) const
Checks if point is included in range of bounding box.
Definition: GeometryStructures.h:439
float GetDiagonal() const
Returns size of diagonal.
Definition: GeometryStructures.h:433
bool IsInside(const TAabb &aabb) const
Checks if this bounding box is inside other Aabb.
Definition: GeometryStructures.h:452