Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
CartesianToTriangleTransform.h
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2016 Fabian Killus
4
5 For more information, see http://www.rigsofrods.org/
6
7 Rigs of Rods is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3, as
9 published by the Free Software Foundation.
10
11 Rigs of Rods is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include "Triangle.h"
23
24#include <OgreMatrix3.h>
25#include <OgreVector3.h>
26
29
32
34
48{
49public:
51
57 const struct {
58 Ogre::Real alpha,
62 const Ogre::Real distance;
63 };
64
66 explicit CartesianToTriangleTransform(const Triangle &triangle) : m_triangle{triangle}, m_initialized{false} {}
67
69
113 TriangleCoord operator() (const Ogre::Vector3 &p) const
114 {
115 // lazy initialization of transformation matrix
116 if (!m_initialized) {
117 InitMatrix();
118 m_initialized = true;
119 }
120
121 // apply transformation matrix and extract alpha, beta, gamma and perpendicular offset
122 const Ogre::Vector3 result = m_matrix * (p - m_triangle.c);
123 return {result[0], result[1], (1.f - result[0] - result[1]), result[2]};
124 }
125
126private:
128 void InitMatrix() const {
129 // determine span and normal vectors
130 const Ogre::Vector3 u = m_triangle.u;
131 const Ogre::Vector3 v = m_triangle.v;
132 const Ogre::Vector3 n = m_triangle.normal();
133
134 // construct and invert matrix
135 m_matrix = Ogre::Matrix3{ u[0], v[0], n[0],
136 u[1], v[1], n[1],
137 u[2], v[2], n[2] }.Inverse();
138 }
139
141 mutable bool m_initialized;
142 mutable Ogre::Matrix3 m_matrix;
143};
144
Defines a linear transformation from cartesian coordinates to local (barycentric) coordinates of a sp...
void InitMatrix() const
Initialize the transformation matrix.
TriangleCoord operator()(const Ogre::Vector3 &p) const
Transform point into local triangle coordinates.
const Triangle m_triangle
The triangle on which the transformation is based.
Ogre::Matrix3 m_matrix
Cached transformation matrix.
CartesianToTriangleTransform(const Triangle &triangle)
Construct transformation for specified triangle.
Represents a triangle in three-dimensional space.
Definition Triangle.h:36
const Ogre::Vector3 u
Span vector u.
Definition Triangle.h:63
const Ogre::Vector3 v
Span vector v.
Definition Triangle.h:64
const Ogre::Vector3 c
Vertex c.
Definition Triangle.h:61
Ogre::Vector3 normal() const
Return normal vector of the triangle.
Definition Triangle.h:48
Return type of CartesianToTriangleTransform transformation.
const struct CartesianToTriangleTransform::TriangleCoord::@9 barycentric
const Ogre::Real distance
Shortest (signed) distance to triangle plane.