PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
|
This namespace contains functions to answer closest point queries. More...
Functions | |
template<mini::CMatrix TMatrixX, mini::CMatrix TMatrixP, mini::CMatrix TMatrixN> | |
PBAT_HOST_DEVICE auto | PointOnPlane (TMatrixX const &X, TMatrixP const &P, TMatrixN const &n) -> mini::SVector< typename TMatrixX::ScalarType, TMatrixX::kRows > |
Obtain the point on the plane (P,n) closest to the point X. | |
template<mini::CMatrix TMatrixX, mini::CMatrix TMatrixP, mini::CMatrix TMatrixQ> | |
PBAT_HOST_DEVICE auto | PointOnLineSegment (TMatrixX const &X, TMatrixP const &P, TMatrixQ const &Q) -> mini::SVector< typename TMatrixX::ScalarType, TMatrixX::kRows > |
Obtain the point on the line segment PQ closest to the point X. | |
template<mini::CMatrix TMatrixX, mini::CMatrix TMatrixL, mini::CMatrix TMatrixU> | |
PBAT_HOST_DEVICE auto | PointOnAxisAlignedBoundingBox (TMatrixX const &X, TMatrixL const &L, TMatrixU const &U) -> mini::SVector< typename TMatrixX::ScalarType, TMatrixX::kRows > |
Obtain the point on the axis-aligned bounding box (AABB) defined by the lower and upper corners closest to the point X. | |
template<mini::CMatrix TMatrixP, mini::CMatrix TMatrixA, mini::CMatrix TMatrixB, mini::CMatrix TMatrixC> | |
PBAT_HOST_DEVICE auto | UvwPointInTriangle (TMatrixP const &P, TMatrixA const &A, TMatrixB const &B, TMatrixC const &C) -> mini::SVector< typename TMatrixP::ScalarType, 3 > |
Obtain the point on the triangle ABC closest to the point P in barycentric coordinates. | |
template<mini::CMatrix TMatrixP, mini::CMatrix TMatrixA, mini::CMatrix TMatrixB, mini::CMatrix TMatrixC> | |
PBAT_HOST_DEVICE auto | PointInTriangle (TMatrixP const &P, TMatrixA const &A, TMatrixB const &B, TMatrixC const &C) -> mini::SVector< typename TMatrixP::ScalarType, TMatrixP::kRows > |
Obtain the point on the triangle ABC closest to the point P. | |
template<mini::CMatrix TMatrixP, mini::CMatrix TMatrixA, mini::CMatrix TMatrixB, mini::CMatrix TMatrixC, mini::CMatrix TMatrixD> | |
PBAT_HOST_DEVICE auto | PointInTetrahedron (TMatrixP const &P, TMatrixA const &A, TMatrixB const &B, TMatrixC const &C, TMatrixD const &D) -> mini::SVector< typename TMatrixP::ScalarType, TMatrixP::kRows > |
Obtain the point in the tetrahedron ABCD closest to the point P. The order of ABCD must be such that all faces ABC, ACD, ADB and BDC are oriented with outwards pointing normals when viewed from outside the tetrahedron. | |
template<mini::CMatrix TMatrixP1, mini::CMatrix TMatrixQ1, mini::CMatrix TMatrixP2, mini::CMatrix TMatrixQ2, class TScalar = typename TMatrixP1::ScalarType> | |
PBAT_HOST_DEVICE auto | Lines (TMatrixP1 const &P1, TMatrixQ1 const &Q1, TMatrixP2 const &P2, TMatrixQ2 const &Q2, TScalar eps=std::numeric_limits< TScalar >::min()) -> mini::SVector< TScalar, 2 > |
Find closest points on two lines defined by points P1, Q1 and P2, Q2. | |
This namespace contains functions to answer closest point queries.
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::Lines | ( | TMatrixP1 const & | P1, |
TMatrixQ1 const & | Q1, | ||
TMatrixP2 const & | P2, | ||
TMatrixQ2 const & | Q2, | ||
TScalar | eps = std::numeric_limits<TScalar>::min() ) -> mini::SVector<TScalar, 2> |
Find closest points on two lines defined by points P1, Q1 and P2, Q2.
We find closest points by minimizing
\[\begin{align*} f(\alpha, \beta) &= | (P1 + \alpha d_1) - (P2 + \beta d_2) |^2 \\ &= | (P1 - P2) + (\alpha d_1 - \beta d_2) |^2 , \end{align*} \]
where \( d_1 = Q1 - P1 \) and \( d_2 = Q2 - P2 \).
We can expand \( f \) to obtain
\[\begin{align*} f(\alpha,\beta) &= |P1-P2|^2 + 2 (P1-P2)^T (\alpha d_1 - \beta d_2) + | (\alpha d_1 - \beta d_2) |^2 \\ &= |P1-P2|^2 + 2 (P1-P2)^T (\alpha d_1 - \beta d_2) + \alpha^2 |d_1|^2 - 2 \alpha \beta d_1^T d_2 + \beta^2 |d_2|^2 . \end{align*} \]
The gradients are thus
\[\begin{align*} \frac{\partial f}{\partial \alpha} &= 2 (P1-P2)^T d_1 + 2 \alpha |d_1|^2 - 2 \beta d_1^T d_2 \\ \frac{\partial f}{\partial \beta} &= -2 (P1-P2)^T d_2 - 2 \alpha d_1^T d_2 + 2 \beta |d_2|^2 \end{align*} \]
Setting the gradients to zero, we obtain the following equations
\[\begin{align*} \beta &= \frac{(P1-P2)^T d_2 + \alpha d_1^T d_2}{|d_2|^2} \\ &= \frac{(P1-P2+\alpha d_1)^T d_2}{|d_2|^2} \\ \alpha &= \frac{\beta d_1^T d_2 - (P1-P2)^T d_1}{|d_1|^2} . \end{align*} \]
Injecting the first equation into the second, we obtain
\[\begin{align*} \alpha &= \frac{-(P1-P2)^T d_1}{|d_1|^2} + \frac{(P1-P2)^T d_2 + \alpha d_1^T d_2}{|d_1|^2 |d_2|^2} d_1^T d_2 \\ \alpha &= \frac{-(P1-P2)^T d_1}{|d_1|^2} + \frac{(P1-P2)^T d_2}{|d_1|^2 |d_2|^2} d_1^T d_2 + \alpha \frac{(d_1^T d_2)^2}{|d_1|^2 |d_2|^2} \\ (1 - \frac{(d_1^T d_2)^2}{|d_1|^2 |d_2|^2}) \alpha &= \frac{-(P1-P2)^T d_1}{|d_1|^2} + \frac{(P1-P2)^T d_2}{|d_1|^2 |d_2|^2} d_1^T d_2 \\ (1 - \frac{(d_1^T d_2)^2}{|d_1|^2 |d_2|^2}) \alpha &= \frac{(P1-P2)^T (d_2 d_1^T d_2 - |d_2|^2 d_1)}{|d_1|^2 |d_2|^2} \\ (|d_1|^2 |d_2|^2 - (d_1^T d_2)^2) \alpha &= (P1-P2)^T (d_2 d_1^T d_2 - |d_2|^2 d_1) . \end{align*} \]
which allows solving for \( \alpha \) first, then \( \beta \).
Note that if the lines are parallel, then by definition,
\[\begin{align*} d_1^T d_2 &= |d_1| |d_2| \cos(\theta) \\ &= |d_1| |d_2| \\ (d_1^T d_2)^2 &= |d_1|^2 |d_2|^2 \\ |d_1|^2 |d_2|^2 - (d_1^T d_2)^2 &= 0 . \end{align*} \]
In that case, we choose \( \alpha = 0 \) and return the corresponding \( \beta \). If any line \( d_i = 0 \), it is degenerated into a point. In that case, we choose the closest point on the other line. If both lines are degenerated into points, the solution is \( \alpha, \beta = 0 \).
TMatrixP1 | Type of the input matrix P1 |
TMatrixQ1 | Type of the input matrix Q1 |
TMatrixP2 | Type of the input matrix P2 |
TMatrixQ2 | Type of the input matrix Q2 |
TScalar | Type of the scalar |
P1 | Point 1 on line 1 |
Q1 | Point 2 on line 1 |
P2 | Point 1 on line 2 |
Q2 | Point 2 on line 2 |
eps | Numerical error tolerance for zero checks |
eps >= 0
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::PointInTetrahedron | ( | TMatrixP const & | P, |
TMatrixA const & | A, | ||
TMatrixB const & | B, | ||
TMatrixC const & | C, | ||
TMatrixD const & | D ) -> mini::SVector<typename TMatrixP::ScalarType, TMatrixP::kRows> |
Obtain the point in the tetrahedron ABCD closest to the point P. The order of ABCD must be such that all faces ABC, ACD, ADB and BDC are oriented with outwards pointing normals when viewed from outside the tetrahedron.
TMatrixP | Query point matrix type |
TMatrixA | Vertex A of the tetrahedron matrix type |
TMatrixB | Vertex B of the tetrahedron matrix type |
TMatrixC | Vertex C of the tetrahedron matrix type |
TMatrixD | Vertex D of the tetrahedron matrix type |
P | Query point |
A | Vertex A of the tetrahedron |
B | Vertex B of the tetrahedron |
C | Vertex C of the tetrahedron |
D | Vertex D of the tetrahedron |
See [5] section 5.16
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::PointInTriangle | ( | TMatrixP const & | P, |
TMatrixA const & | A, | ||
TMatrixB const & | B, | ||
TMatrixC const & | C ) -> mini::SVector<typename TMatrixP::ScalarType, TMatrixP::kRows> |
Obtain the point on the triangle ABC closest to the point P.
TMatrixP | Query point matrix type |
TMatrixA | Vertex A of the triangle matrix type |
TMatrixB | Vertex B of the triangle matrix type |
TMatrixC | Vertex C of the triangle matrix type |
P | Query point |
A | Vertex A of the triangle |
B | Vertex B of the triangle |
C | Vertex C of the triangle |
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::PointOnAxisAlignedBoundingBox | ( | TMatrixX const & | X, |
TMatrixL const & | L, | ||
TMatrixU const & | U ) -> mini::SVector<typename TMatrixX::ScalarType, TMatrixX::kRows> |
Obtain the point on the axis-aligned bounding box (AABB) defined by the lower and upper corners closest to the point X.
TMatrixX | Query point matrix type |
TMatrixL | Lower corner of the AABB matrix type |
TMatrixU | Upper corner of the AABB matrix type |
X | Query point |
L | Lower corner of the AABB |
U | Upper corner of the AABB |
See [5] section 5.13
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::PointOnLineSegment | ( | TMatrixX const & | X, |
TMatrixP const & | P, | ||
TMatrixQ const & | Q ) -> mini::SVector<typename TMatrixX::ScalarType, TMatrixX::kRows> |
Obtain the point on the line segment PQ closest to the point X.
TMatrixX | Query point matrix type |
TMatrixP | Start point of the line segment matrix type |
TMatrixQ | End point of the line segment matrix type |
X | Query point |
P | Start point of the line segment |
Q | End point of the line segment |
See [5] section 5.12
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::PointOnPlane | ( | TMatrixX const & | X, |
TMatrixP const & | P, | ||
TMatrixN const & | n ) -> mini::SVector<typename TMatrixX::ScalarType, TMatrixX::kRows> |
Obtain the point on the plane (P,n) closest to the point X.
TMatrixX | Query point matrix type |
TMatrixP | Point on the plane matrix type |
TMatrixN | Normal of the plane matrix type |
X | Query point |
P | Point on the plane |
n | Normal of the plane |
See [5] section 5.11
PBAT_HOST_DEVICE auto pbat::geometry::ClosestPointQueries::UvwPointInTriangle | ( | TMatrixP const & | P, |
TMatrixA const & | A, | ||
TMatrixB const & | B, | ||
TMatrixC const & | C ) -> mini::SVector<typename TMatrixP::ScalarType, 3> |
Obtain the point on the triangle ABC closest to the point P in barycentric coordinates.
TMatrixP | Query point matrix type |
TMatrixA | Vertex A of the triangle matrix type |
TMatrixB | Vertex B of the triangle matrix type |
TMatrixC | Vertex C of the triangle matrix type |
P | Query point |
A | Vertex A of the triangle |
B | Vertex B of the triangle |
C | Vertex C of the triangle |
See [5] section 5.15