ACF $AcfVersion:0$
Geometry.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
6#include <i3d/CVector3d.h>
7#include <i3d/CPlane3d.h>
8#include <i3d/CLine3d.h>
9
10
11namespace i3d
12{
13
14
18namespace Geometry
19{
20
21
28inline double GetAngleBetweenVectors(const CVector3d& v1, const CVector3d& v2)
29{
30 double dot = v1.GetNormalized().GetDotProduct(v2.GetNormalized());
31 // Clamp to avoid numerical issues with acos
32 dot = qMax(-1.0, qMin(1.0, dot));
33 return qAcos(dot);
34}
35
36
44inline double GetSignedAngleBetweenVectors(const CVector3d& v1, const CVector3d& v2, const CVector3d& axis)
45{
46 CVector3d n1 = v1.GetNormalized();
47 CVector3d n2 = v2.GetNormalized();
48
49 double angle = GetAngleBetweenVectors(n1, n2);
50
51 // Determine sign using cross product
52 CVector3d cross = n1.GetCrossProduct(n2);
53 if (cross.GetDotProduct(axis) < 0.0){
54 angle = -angle;
55 }
56
57 return angle;
58}
59
60
64inline double GetDistance(const CVector3d& p1, const CVector3d& p2)
65{
66 return (p2 - p1).GetLength();
67}
68
69
73inline double GetDistanceSq(const CVector3d& p1, const CVector3d& p2)
74{
75 return (p2 - p1).GetLength2();
76}
77
78
82inline double GetDistance(const CVector3d& point, const CPlane3d& plane)
83{
84 return plane.GetDistance(point);
85}
86
87
91inline double GetDistance(const CVector3d& point, const CLine3d& line)
92{
93 return line.GetDistance(point);
94}
95
96
104bool AreIntersecting(const CLine3d& line1, const CLine3d& line2, double tolerance = I_BIG_EPSILON);
105
106
115bool GetIntersection(const CLine3d& line, const CPlane3d& plane, CVector3d& result, double tolerance = I_BIG_EPSILON);
116
117
126double GetClosestPoints(const CLine3d& line1, const CLine3d& line2, CVector3d& point1, CVector3d& point2);
127
128
132inline bool IsOnPositiveSide(const CVector3d& point, const CPlane3d& plane)
133{
134 return plane.GetSignedDistance(point) >= 0.0;
135}
136
137
144inline CVector3d ProjectVectorOntoVector(const CVector3d& vector, const CVector3d& onto)
145{
146 double ontoLengthSq = onto.GetLength2();
147 if (ontoLengthSq < I_BIG_EPSILON){
148 return CVector3d(0.0, 0.0, 0.0);
149 }
150
151 double projection = vector.GetDotProduct(onto) / ontoLengthSq;
152 return onto * projection;
153}
154
155
168 const CVector3d& point,
169 const CVector3d& p1,
170 const CVector3d& p2,
171 const CVector3d& p3,
172 double& u,
173 double& v,
174 double& w);
175
176
186 const CVector3d& point,
187 const CVector3d& p1,
188 const CVector3d& p2,
189 const CVector3d& p3);
190
191
195inline CVector3d GetTriangleNormal(const CVector3d& p1, const CVector3d& p2, const CVector3d& p3)
196{
197 CVector3d edge1 = p2 - p1;
198 CVector3d edge2 = p3 - p1;
199 return edge1.GetCrossProduct(edge2).GetNormalized();
200}
201
202
206inline double GetTriangleArea(const CVector3d& p1, const CVector3d& p2, const CVector3d& p3)
207{
208 CVector3d edge1 = p2 - p1;
209 CVector3d edge2 = p3 - p1;
210 return edge1.GetCrossProduct(edge2).GetLength() * 0.5;
211}
212
213
214} // namespace Geometry
215
216
217} // namespace i3d
218
219
Definition of a line in 3D space.
Definition CLine3d.h:18
double GetDistance(const CVector3d &point) const
Calculate distance from point to line segment.
Definition of a plane in 3D space.
Definition CPlane3d.h:19
double GetSignedDistance(const CVector3d &point) const
Calculate signed distance from point to plane.
Definition CPlane3d.h:139
double GetDistance(const CVector3d &point) const
Calculate absolute distance from point to plane.
Definition CPlane3d.h:145
Represents a position or mathematical vector in 3D space with double precision.
Definition CVector3d.h:74
CVector3d GetNormalized(double length=1.0) const
Return normalized vector with the same direction and specified length.
CVector3d GetCrossProduct(const imath::TVector< 3 > &vector) const
Calculates the cross product of this vector with another vector.
Element GetLength() const
Calculates the Euclidean length (magnitude) of the vector.
Definition TVector.h:667
Element GetDotProduct(const TVector< Size, Element > &vector) const
Calculates the dot product with another vector.
Definition TVector.h:647
Element GetLength2() const
Calculates the squared Euclidean length of the vector.
Definition TVector.h:660
static const double I_BIG_EPSILON
Definition istd.h:26
double GetSignedAngleBetweenVectors(const CVector3d &v1, const CVector3d &v2, const CVector3d &axis)
Calculate signed angle between two vectors around an axis.
Definition Geometry.h:44
double GetAngleBetweenVectors(const CVector3d &v1, const CVector3d &v2)
Calculate angle between two vectors in radians.
Definition Geometry.h:28
bool GetBarycentricCoordinates(const CVector3d &point, const CVector3d &p1, const CVector3d &p2, const CVector3d &p3, double &u, double &v, double &w)
Calculate barycentric coordinates of point relative to triangle.
bool GetIntersection(const CLine3d &line, const CPlane3d &plane, CVector3d &result, double tolerance=I_BIG_EPSILON)
Find intersection point between line and plane.
double GetDistanceSq(const CVector3d &p1, const CVector3d &p2)
Calculate squared distance between two points (faster).
Definition Geometry.h:73
bool IsOnPositiveSide(const CVector3d &point, const CPlane3d &plane)
Check if point is on the same side of plane as the plane's normal.
Definition Geometry.h:132
double GetTriangleArea(const CVector3d &p1, const CVector3d &p2, const CVector3d &p3)
Calculate area of triangle.
Definition Geometry.h:206
CVector3d GetTriangleNormal(const CVector3d &p1, const CVector3d &p2, const CVector3d &p3)
Calculate normal vector of triangle.
Definition Geometry.h:195
CVector3d ProjectVectorOntoVector(const CVector3d &vector, const CVector3d &onto)
Project vector onto another vector.
Definition Geometry.h:144
bool AreIntersecting(const CLine3d &line1, const CLine3d &line2, double tolerance=I_BIG_EPSILON)
Check if two lines intersect in 3D.
bool IsPointInTriangle(const CVector3d &point, const CVector3d &p1, const CVector3d &p2, const CVector3d &p3)
Check if point is inside triangle.
double GetClosestPoints(const CLine3d &line1, const CLine3d &line2, CVector3d &point1, CVector3d &point2)
Find closest points between two lines.
double GetDistance(const CVector3d &p1, const CVector3d &p2)
Calculate distance between two points.
Definition Geometry.h:64
Contains the 3D objects.
Definition CAffine3d.h:12