![]() |
RigsofRods
Soft-body Physics Simulation
|
Defines a linear transformation from cartesian coordinates to local (barycentric) coordinates of a specified triangle. More...
#include <CartesianToTriangleTransform.h>
Data Structures | |
struct | TriangleCoord |
Return type of CartesianToTriangleTransform transformation. More... | |
Public Member Functions | |
CartesianToTriangleTransform (const Triangle &triangle) | |
Construct transformation for specified triangle. More... | |
TriangleCoord | operator() (const Ogre::Vector3 &p) const |
Transform point into local triangle coordinates. More... | |
Private Member Functions | |
void | InitMatrix () const |
Initialize the transformation matrix. More... | |
Private Attributes | |
const Triangle | m_triangle |
The triangle on which the transformation is based. More... | |
bool | m_initialized |
Ogre::Matrix3 | m_matrix |
Cached transformation matrix. More... | |
Defines a linear transformation from cartesian coordinates to local (barycentric) coordinates of a specified triangle.
The barycentric coordinate system of the triangle is defined in terms of its three vertices \((\mathbf{a}, \mathbf{b}, \mathbf{c})\). Since an arbitrary three-dimensional point \(\mathbf{p}\) is not guaranteed to lie within the plane defined by this triangle, the barycentric coordinates \((\alpha, \beta, \gamma)\) are instead determined for the point \(\mathbf{p}'\) which denotes the projection of \(\mathbf{p}\) onto this plane. In addition, the shortest (signed) distance \(d\) between the point \(\mathbf{p}\) and the triangle plane is returned by this transformation.
The transformation is evaluated lazily, i.e. creating new instances of CartesianToTriangleTransform is relatively cheap. Actual calculations are deferred until the transformation is applied to a point for the first time.
Definition at line 47 of file CartesianToTriangleTransform.h.
|
inlineexplicit |
Construct transformation for specified triangle.
Definition at line 66 of file CartesianToTriangleTransform.h.
|
inlineprivate |
Initialize the transformation matrix.
Definition at line 128 of file CartesianToTriangleTransform.h.
|
inline |
Transform point into local triangle coordinates.
The calculation of the barycentric coordinates and the perpendicular distance to the triangle plane for a given point p is explained in the following.
Let \(\mathbf{p}'\) denote the projection of point \(\mathbf{p}\) onto the triangle plane. I.e. \(\mathbf{p} = \mathbf{p}' + d \cdot \mathbf{n}\), where \(\mathbf{n}\) is the normal vector of the plane and \(d\) is the shortest (signed) distance between point \(\mathbf{p}\) and the plane.
The point \(\mathbf{p}'\) can be expressed in barycentric coordinates \((\alpha, \beta, \gamma)\) as follows:
\[ \mathbf{p}' = \alpha \cdot \mathbf{a} + \beta \cdot \mathbf{b} + \gamma \cdot \mathbf{c}, \quad \text{with } \alpha + \beta + \gamma = 1 \]
The variable \(\gamma\) is redundant and can be substituted by \(1-\alpha-\beta\):
\begin{eqnarray*} \mathbf{p}' &=& \alpha \cdot \mathbf{a} + \beta \cdot \mathbf{b} + (1-\alpha -\beta) \cdot \mathbf{c} \\ &=& \alpha \cdot (\mathbf{a} - \mathbf{c}) + \beta \cdot (\mathbf{b} - \mathbf{c}) + \mathbf{c} \end{eqnarray*}
The distance \(d\) between point \(\mathbf{p}\) and the plane is calculated by projecting the vector \((\mathbf{p} - \mathbf{c})\) onto the normal vector \(\mathbf{n}\):
\[ d = \mathbf{n}^T \cdot (\mathbf{p}-\mathbf{c}), \quad \text{with } ||n|| = 1 \]
Combining this with the expression of point \(\mathbf{p}'\) in barycentric coordinates allows to formulate the problem in matrix form:
\[ \mathbf{M} = \begin{bmatrix} \mathbf{u} & \mathbf{v} & \mathbf{n} \end{bmatrix}, \quad \text{with } \mathbf{u} = (\mathbf{a} - \mathbf{c}) \text{ and } \mathbf{v} = (\mathbf{b} - \mathbf{c}) \]
\[ \mathbf{M} \cdot \begin{bmatrix} \alpha \\ \beta \\ d \end{bmatrix} = \mathbf{p}' + \mathbf{n} \cdot d - \mathbf{c} = \mathbf{p} - \mathbf{c} \]
The solution is obtained by inverting matrix \(\mathbf{M}\):
\[ \begin{bmatrix} \alpha \\ \beta \\ d \end{bmatrix} = \mathbf{M}^{-1} \cdot (\mathbf{p} - \mathbf{c}) \]
\(\gamma\) can be immediately calculated from known values \(\alpha\) and \(\beta\) because \(\alpha + \beta + \gamma = 1\) always holds.
Definition at line 113 of file CartesianToTriangleTransform.h.
|
mutableprivate |
Definition at line 141 of file CartesianToTriangleTransform.h.
|
mutableprivate |
Cached transformation matrix.
Definition at line 142 of file CartesianToTriangleTransform.h.
|
private |
The triangle on which the transformation is based.
Definition at line 140 of file CartesianToTriangleTransform.h.