RigsofRods
Soft-body Physics Simulation
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 {
49 public:
51 
56  struct TriangleCoord {
57  const struct {
58  Ogre::Real alpha,
59  beta,
61  } barycentric;
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 
126 private:
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 
CartesianToTriangleTransform::CartesianToTriangleTransform
CartesianToTriangleTransform(const Triangle &triangle)
Construct transformation for specified triangle.
Definition: CartesianToTriangleTransform.h:66
Triangle
Represents a triangle in three-dimensional space.
Definition: Triangle.h:35
Triangle.h
CartesianToTriangleTransform::TriangleCoord::distance
const Ogre::Real distance
Shortest (signed) distance to triangle plane.
Definition: CartesianToTriangleTransform.h:62
Triangle::normal
Ogre::Vector3 normal() const
Return normal vector of the triangle.
Definition: Triangle.h:48
Triangle::v
const Ogre::Vector3 v
Span vector v.
Definition: Triangle.h:64
CartesianToTriangleTransform::TriangleCoord::beta
Ogre::Real beta
Barycentric coordinate.
Definition: CartesianToTriangleTransform.h:59
CartesianToTriangleTransform::TriangleCoord::alpha
Ogre::Real alpha
Barycentric coordinate.
Definition: CartesianToTriangleTransform.h:58
CartesianToTriangleTransform::m_matrix
Ogre::Matrix3 m_matrix
Cached transformation matrix.
Definition: CartesianToTriangleTransform.h:142
CartesianToTriangleTransform
Defines a linear transformation from cartesian coordinates to local (barycentric) coordinates of a sp...
Definition: CartesianToTriangleTransform.h:47
Triangle::u
const Ogre::Vector3 u
Span vector u.
Definition: Triangle.h:63
Triangle::c
const Ogre::Vector3 c
Vertex c.
Definition: Triangle.h:61
CartesianToTriangleTransform::TriangleCoord::gamma
Ogre::Real gamma
Barycentric coordinate.
Definition: CartesianToTriangleTransform.h:60
CartesianToTriangleTransform::operator()
TriangleCoord operator()(const Ogre::Vector3 &p) const
Transform point into local triangle coordinates.
Definition: CartesianToTriangleTransform.h:113
CartesianToTriangleTransform::m_triangle
const Triangle m_triangle
The triangle on which the transformation is based.
Definition: CartesianToTriangleTransform.h:140
CartesianToTriangleTransform::InitMatrix
void InitMatrix() const
Initialize the transformation matrix.
Definition: CartesianToTriangleTransform.h:128
CartesianToTriangleTransform::TriangleCoord::barycentric
const struct CartesianToTriangleTransform::TriangleCoord::@9 barycentric
CartesianToTriangleTransform::m_initialized
bool m_initialized
Definition: CartesianToTriangleTransform.h:141
CartesianToTriangleTransform::TriangleCoord
Return type of CartesianToTriangleTransform transformation.
Definition: CartesianToTriangleTransform.h:56