OpenLB 1.7
Searching...
No Matches
olb::STLtriangle< T > Struct Template Reference

`#include <stlReader.h>`

Collaboration diagram for olb::STLtriangle< T >:

## Public Member Functions

bool testRayIntersect (const Vector< T, 3 > &pt, const Vector< T, 3 > &dir, Vector< T, 3 > &q, T &alpha, const T &rad=T(), bool print=false)
Test intersection between ray and triangle.

Vector< T, 3 > closestPtPointTriangle (const Vector< T, 3 > &pt) const
computes closest Point in a triangle to another point.

STLtriangle ()
Constructor constructs.

STLtriangle (STLtriangle< T > const &tri)
CopyConstructor copies.

STLtriangle< T > & operator= (STLtriangle< T > const &tri)
Operator= equals.

~STLtriangle ()

void init ()
Initializes triangle and precomputes member variables.

Vector< T, 3 > & getNormal ()

const Vector< T, 3 > & getNormal () const

Vector< T, 3 > getCenter ()
Returns center.

std::vector< T > getE0 ()
Returns Pt0-Pt1.

std::vector< T > getE1 ()
Returns Pt0-Pt2.

bool isPointInside (const PhysR< T, 3 > &pt) const
Check whether a point is inside a triangle.

## Public Attributes

std::vector< Vertex< T, 3 > > point
A triangle contains 3 Points.

Vector< T, 3 > normal
normal of triangle

Vector< T, 3 > uBeta
variables explained in http://www.uninformativ.de/bin/RaytracingSchnitttests-76a577a-CC-BY.pdf page 7-12 precomputed for speedup

Vector< T, 3 > uGamma

d

kBeta

kGamma

## Detailed Description

template<typename T>
struct olb::STLtriangle< T >

Definition at line 79 of file stlReader.h.

## ◆ STLtriangle() [1/2]

template<typename T >
 olb::STLtriangle< T >::STLtriangle ( )
inline

Constructor constructs.

Definition at line 104 of file stlReader.h.

104:point(3, Vertex<T,3>()), normal(T()), uBeta(T()), uGamma(T()), d(T()), kBeta(T()), kGamma(T()) {};
Vector< T, 3 > uBeta
variables explained in http://www.uninformativ.de/bin/RaytracingSchnitttests-76a577a-CC-BY....
Vector< T, 3 > normal
normal of triangle
Vector< T, 3 > uGamma
std::vector< Vertex< T, 3 > > point
A triangle contains 3 Points.

## ◆ STLtriangle() [2/2]

template<typename T >
 olb::STLtriangle< T >::STLtriangle ( STLtriangle< T > const & tri )
inline

CopyConstructor copies.

Definition at line 106 of file stlReader.h.

106:point(tri.point), normal(tri.normal), uBeta(tri.uBeta), uGamma(tri.uGamma), d(tri.d), kBeta(tri.kBeta), kGamma(tri.kGamma) {};

## ◆ ~STLtriangle()

template<typename T >
 olb::STLtriangle< T >::~STLtriangle ( )
inline

Definition at line 120 of file stlReader.h.

120{};

## ◆ closestPtPointTriangle()

template<typename T >
 Vector< T, 3 > olb::STLtriangle< T >::closestPtPointTriangle ( const Vector< T, 3 > & pt ) const

computes closest Point in a triangle to another point.

source: Real-Time Collision Detection. Christer Ericson. ISBN-10: 1558607323

Definition at line 261 of file stlReader.hh.

263{
264
265 const T nEps = -std::numeric_limits<T>::epsilon();
266 const T Eps = std::numeric_limits<T>::epsilon();
267
268 Vector<T,3> ab = point[1].coords - point[0].coords;
269 Vector<T,3> ac = point[2].coords - point[0].coords;
270 Vector<T,3> bc = point[2].coords - point[1].coords;
271
272 T snom = (pt - point[0].coords)*ab;
273 T sdenom = (pt - point[1].coords)*(point[0].coords - point[1].coords);
274
275 T tnom = (pt - point[0].coords)*ac;
276 T tdenom = (pt - point[2].coords)*(point[0].coords - point[2].coords);
277
278 if (snom < nEps && tnom < nEps) {
279 return point[0].coords;
280 }
281
282 T unom = (pt - point[1].coords)*bc;
283 T udenom = (pt - point[2].coords)*(point[1].coords - point[2].coords);
284
285 if (sdenom < nEps && unom < nEps) {
286 return point[1].coords;
287 }
288 if (tdenom < nEps && udenom < nEps) {
289 return point[2].coords;
290 }
291
292 T vc = normal*crossProduct3D(point[0].coords - pt, point[1].coords - pt);
293
294 if (vc < nEps && snom > Eps && sdenom > Eps) {
295 return point[0].coords + snom / (snom + sdenom) * ab;
296 }
297
298 T va = normal*crossProduct3D(point[1].coords - pt, point[2].coords - pt);
299
300 if (va < nEps && unom > Eps && udenom > Eps) {
301 return point[1].coords + unom / (unom + udenom) * bc;
302 }
303
304 T vb = normal*crossProduct3D(point[2].coords - pt, point[0].coords - pt);
305
306 if (vb < nEps && tnom > Eps && tdenom > Eps) {
307 return point[0].coords + tnom / (tnom + tdenom) * ac;
308 }
309
310 T u = va / (va + vb + vc);
311 T v = vb / (va + vb + vc);
312 T w = 1. - u - v;
313
314 return u * point[0].coords + v * point[1].coords + w * point[2].coords;
315}
constexpr Vector< T, 3 > crossProduct3D(const ScalarVector< T, 3, IMPL > &a, const ScalarVector< T, 3, IMPL_ > &b) any_platform
Definition vector.h:224

References olb::crossProduct3D().

Here is the call graph for this function:

## ◆ getCenter()

template<typename T >
 Vector< T, 3 > olb::STLtriangle< T >::getCenter ( )

Returns center.

Definition at line 91 of file stlReader.hh.

92{
93 Vector<T,3> center( T(0) );
94
95 center[0] = (point[0].coords[0] + point[1].coords[0]
96 + point[2].coords[0]) / 3.;
97 center[1] = (point[0].coords[1] + point[1].coords[1]
98 + point[2].coords[1]) / 3.;
99 center[2] = (point[0].coords[2] + point[1].coords[2]
100 + point[2].coords[2]) / 3.;
101
102 return center;
103}

## ◆ getE0()

template<typename T >
 std::vector< T > olb::STLtriangle< T >::getE0 ( )

Returns Pt0-Pt1.

Definition at line 106 of file stlReader.hh.

107{
108 Vector<T,3> vec;
109 vec[0] = point[0].coords[0] - point[1].coords[0];
110 vec[1] = point[0].coords[1] - point[1].coords[1];
111 vec[2] = point[0].coords[2] - point[1].coords[2];
112 return vec;
113}

## ◆ getE1()

template<typename T >
 std::vector< T > olb::STLtriangle< T >::getE1 ( )

Returns Pt0-Pt2.

Definition at line 116 of file stlReader.hh.

117{
118 Vector<T,3> vec;
119 vec[0] = point[0].coords[0] - point[2].coords[0];
120 vec[1] = point[0].coords[1] - point[2].coords[1];
121 vec[2] = point[0].coords[2] - point[2].coords[2];
122 return vec;
123}

## ◆ getNormal() [1/2]

template<typename T >
 Vector< T, 3 > & olb::STLtriangle< T >::getNormal ( )
inline

Definition at line 125 of file stlReader.h.

126 {
127 return normal;
128 }

References olb::STLtriangle< T >::normal.

Here is the caller graph for this function:

## ◆ getNormal() [2/2]

template<typename T >
 const Vector< T, 3 > & olb::STLtriangle< T >::getNormal ( ) const
inline

Definition at line 130 of file stlReader.h.

131 {
132 return normal;
133 }

References olb::STLtriangle< T >::normal.

## ◆ init()

template<typename T >
 void olb::STLtriangle< T >::init ( )

Initializes triangle and precomputes member variables.

Definition at line 46 of file stlReader.hh.

47{
48 Vector<T,3> A = point[0].coords;
49 Vector<T,3> B = point[1].coords;
50 Vector<T,3> C = point[2].coords;
51 Vector<T,3> b, c;
52 T bb = 0., bc = 0., cc = 0.;
53
54 for (int i = 0; i < 3; i++) {
55 b[i] = B[i] - A[i];
56 c[i] = C[i] - A[i];
57 bb += b[i] * b[i];
58 bc += b[i] * c[i];
59 cc += c[i] * c[i];
60 }
61
62 normal[0] = b[1] * c[2] - b[2] * c[1];
63 normal[1] = b[2] * c[0] - b[0] * c[2];
64 normal[2] = b[0] * c[1] - b[1] * c[0];
65
66 T norm = util::sqrt(
67 util::pow(normal[0], 2) + util::pow(normal[1], 2) + util::pow(normal[2], 2));
68 normal[0] /= norm;
69 normal[1] /= norm;
70 normal[2] /= norm;
71
72 T D = 1.0 / (cc * bb - bc * bc);
73 T bbD = bb * D;
74 T bcD = bc * D;
75 T ccD = cc * D;
76
77 kBeta = 0.;
78 kGamma = 0.;
79 d = 0.;
80
81 for (int i = 0; i < 3; i++) {
82 uBeta[i] = b[i] * ccD - c[i] * bcD;
83 uGamma[i] = c[i] * bbD - b[i] * bcD;
84 kBeta -= A[i] * uBeta[i];
85 kGamma -= A[i] * uGamma[i];
86 d += A[i] * normal[i];
87 }
88}
platform_constant int c[Q][D]
cpu::simd::Pack< T > sqrt(cpu::simd::Pack< T > value)
Definition pack.h:100
cpu::simd::Pack< T > pow(cpu::simd::Pack< T > base, cpu::simd::Pack< T > exp)
Definition pack.h:112
constexpr T norm(const ScalarVector< T, D, IMPL > &a)
Euclidean vector norm.

References olb::norm(), olb::util::pow(), and olb::util::sqrt().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ isPointInside()

template<typename T >
 bool olb::STLtriangle< T >::isPointInside ( const PhysR< T, 3 > & pt ) const

Check whether a point is inside a triangle.

Definition at line 126 of file stlReader.hh.

127{
128 // tests with T=double and T=float show that the epsilon must be increased
129 const T epsilon = std::numeric_limits<BaseType<T>>::epsilon()*T(10);
130
131 const T beta = pt * uBeta + kBeta;
132 const T gamma = pt * uGamma + kGamma;
133
134 // check if approximately equal
135 if ( util::nearZero(norm(pt - (point[0].coords + beta*(point[1].coords-point[0].coords) + gamma*(point[2].coords-point[0].coords))), epsilon) ) {
136 const T alpha = T(1) - beta - gamma;
137 return (beta >= T(0) || util::nearZero(beta, epsilon))
138 && (gamma >= T(0) || util::nearZero(gamma, epsilon))
139 && (alpha >= T(0) || util::nearZero(alpha, epsilon));
140 }
141 return false;
142}
bool nearZero(const ADf< T, DIM > &a)

References olb::util::nearZero(), and olb::norm().

Here is the call graph for this function:

## ◆ operator=()

template<typename T >
 STLtriangle< T > & olb::STLtriangle< T >::operator= ( STLtriangle< T > const & tri )
inline

Operator= equals.

Definition at line 108 of file stlReader.h.

109 {
110 point = tri.point;
111 normal = tri.normal;
112 uBeta = tri.uBeta;
113 uGamma = tri.uGamma;
114 d = tri.d;
115 kBeta = tri.kBeta;
116 kGamma = tri.kGamma;
117 return *this;
118 };
static constexpr unsigned d

## ◆ testRayIntersect()

template<typename T >
 bool olb::STLtriangle< T >::testRayIntersect ( const Vector< T, 3 > & pt, const Vector< T, 3 > & dir, Vector< T, 3 > & q, T & alpha, const T & rad = T(), bool print = false )

Test intersection between ray and triangle.

Parameters
 pt Raypoint dir Direction q Point of intersection (if intersection occurs) alpha Explained in http://www.uninformativ.de/bin/RaytracingSchnitttests-76a577a-CC-BY.pdf page 7-12 q = pt + alpha * dir rad It's complicated. Imagine you have a sphere with radius rad moving a long the ray. Then q becomes the first point of the sphere to touch the triangle.

Definition at line 154 of file stlReader.hh.

158{
159 T rn = 0.;
160 Vector<T,3> testPt = pt + rad * normal;
161 /* Vector<T,3> help; */
162
163 for (int i = 0; i < 3; i++) {
164 rn += dir[i] * normal[i];
165 }
166#ifdef OLB_DEBUG
167
168 if (print) {
169 std::cout << "Pt: " << pt[0] << " " << pt[1] << " " << pt[2] << std::endl;
170 }
171 if (print)
172 std::cout << "testPt: " << testPt[0] << " " << testPt[1] << " " << testPt[2]
173 << std::endl;
174 if (print)
175 std::cout << "PosNeg: "
176 << normal[0] * testPt[0] + normal[1] * testPt[1] + normal[2] * testPt[2]
177 - d << std::endl;
178 if (print)
179 std::cout << "Normal: " << normal[0] << " " << normal[1] << " " << normal[2]
180 << std::endl;
181#endif
182
183 // Schnitttest Flugrichtung -> Ebene
184 if (util::fabs(rn) < std::numeric_limits<T>::epsilon()) {
185#ifdef OLB_DEBUG
186 if (print) {
187 std::cout << "FALSE 1" << std::endl;
188 }
189#endif
190 return false;
191 }
192 alpha = d - testPt[0] * normal[0] - testPt[1] * normal[1] - testPt[2] * normal[2];
193 // alpha -= testPt[i] * normal[i];
194 alpha /= rn;
195
196 // Abstand Partikel Ebene
197 if (alpha < -std::numeric_limits<T>::epsilon()) {
198#ifdef OLB_DEBUG
199 if (print) {
200 std::cout << "FALSE 2" << std::endl;
201 }
202#endif
203 return false;
204 }
205 for (int i = 0; i < 3; i++) {
206 q[i] = testPt[i] + alpha * dir[i];
207 }
208 T beta = kBeta;
209 for (int i = 0; i < 3; i++) {
210 beta += uBeta[i] * q[i];
211 }
212#ifdef OLB_DEBUG
213 T dist = util::sqrt(
214 util::pow(q[0] - testPt[0], 2) + util::pow(q[1] - testPt[1], 2)
215 + util::pow(q[2] - testPt[2], 2));
216#endif
217
218 // Schnittpunkt q in der Ebene?
219 if (beta < -std::numeric_limits<T>::epsilon()) {
220#ifdef OLB_DEBUG
221
222 if (print) {
223 std::cout << "FALSE 3 BETA " << beta << " DIST " << dist << std::endl;
224 }
225#endif
226 return false;
227 }
228 T gamma = kGamma;
229 for (int i = 0; i < 3; i++) {
230 gamma += uGamma[i] * q[i];
231 }
232 if (gamma < -std::numeric_limits<T>::epsilon()) {
233#ifdef OLB_DEBUG
234 if (print) {
235 std::cout << "FALSE 4 GAMMA " << gamma << " DIST " << dist << std::endl;
236 }
237#endif
238 return false;
239 }
240 if (1. - beta - gamma < -std::numeric_limits<T>::epsilon()) {
241#ifdef OLB_DEBUG
242 if (print)
243 std::cout << "FALSE 5 VAL " << 1 - beta - gamma << " DIST " << dist
244 << std::endl;
245#endif
246 return false;
247 }
248#ifdef OLB_DEBUG
249 if (print) {
250 std::cout << "TRUE" << " GAMMA " << gamma << " BETA " << beta << std::endl;
251 }
252#endif
253 return true;
254}
constexpr int q() any_platform
cpu::simd::Pack< T > fabs(cpu::simd::Pack< T > value)
Definition pack.h:106

References olb::util::fabs(), olb::util::pow(), and olb::util::sqrt().

Here is the call graph for this function:

## ◆ d

template<typename T >
 T olb::STLtriangle< T >::d

Definition at line 100 of file stlReader.h.

## ◆ kBeta

template<typename T >
 T olb::STLtriangle< T >::kBeta

Definition at line 100 of file stlReader.h.

## ◆ kGamma

template<typename T >
 T olb::STLtriangle< T >::kGamma

Definition at line 100 of file stlReader.h.

## ◆ normal

template<typename T >
 Vector olb::STLtriangle< T >::normal

normal of triangle

Definition at line 95 of file stlReader.h.

## ◆ point

template<typename T >
 std::vector > olb::STLtriangle< T >::point

A triangle contains 3 Points.

Definition at line 92 of file stlReader.h.

## ◆ uBeta

template<typename T >
 Vector olb::STLtriangle< T >::uBeta

variables explained in http://www.uninformativ.de/bin/RaytracingSchnitttests-76a577a-CC-BY.pdf page 7-12 precomputed for speedup

Definition at line 99 of file stlReader.h.

## ◆ uGamma

template<typename T >
 Vector olb::STLtriangle< T >::uGamma

Definition at line 99 of file stlReader.h.

The documentation for this struct was generated from the following files: