Library of Assembled Shared Sources
intersect_triangle_3d.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_INTERSECT_TRIANGLE_3D_H
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_INTERSECT_TRIANGLE_3D_H
45
47#include "../xyz.h"
48
49namespace lass
50{
51namespace prim
52{
53namespace impl
54{
55
56/** @internal
57 *
58 * @par Algorithm:
59 * @arg Tomas M�ller and Ben Trumbore.
60 * "Fast, minimum storage ray-triangle intersection."
61 * Journal of Graphics Tools, 2(1):21--28, 1997.
62 * http://www.graphics.cornell.edu/pubs/1997/MT97.html
63 *
64 * @arg comp.graphics.algorithms Frequently Asked Questions:
65 * Subject 5.06: "How do I determine the intersection between a ray and a triangle?"
66 * http://www.faqs.org/faqs/graphics/algorithms-faq/ *
67 */
68template <typename Point, typename Vector, typename T> inline
69Result intersectTriangle3D(const Point& iVertex0, const Vector& iEdge1, const Vector& iEdge2,
70 const Point& iSupport, const Vector& iDirection,
71 T& oU, T& oV, T& oT, const T& iMinT)
72{
73 typedef typename Vector::TNumTraits TNumTraits;
74 typedef num::Consistent<T> TConsistent;
75
76 const Vector pvec = cross(iDirection, iEdge2);
77 const T det = dot(pvec, iEdge1);
78
79 if (det == TNumTraits::zero)
80 {
81 return rNone;
82 }
83
84 const T invDet = num::inv(det);
85
86 const Vector tvec = iSupport - iVertex0;
87 const T u = dot(tvec, pvec) * invDet;
88 if (u < TNumTraits::zero || u > TNumTraits::one)
89 {
90 return rNone;
91 }
92
93 const Vector qvec = cross(tvec, iEdge1);
94 const T v = dot(iDirection, qvec) * invDet;
95 if (v < TNumTraits::zero || (u + v) > TNumTraits::one)
96 {
97 return rNone;
98 }
99
100 const TConsistent t = dot(iEdge2, qvec) * invDet;
101 if (t <= iMinT)
102 {
103 return rNone;
104 }
105
106 oU = u;
107 oV = v;
108 oT = t.value();
109 return rOne;
110}
111
112
113
114/** @internal
115 *
116 * @par Algorithm:
117 * @arg Sven Woop, Carsten Benthin, and Ingo Wald.
118 * "Watertight Ray/Triangle Intersection."
119 * Journal of Computer Graphics Techniques (JCGT), 2(1):65--82, 2019.
120 * https://jcgt.org/published/0002/01/05/
121 */
122template <typename Point>
123class IntersectTriangle3DWoop
124{
125public:
126
127 using TPoint3D = Point;
128 using TVector3D = typename Point::TVector;
129 using TValue = typename Point::TValue;
130
131 IntersectTriangle3DWoop(const TPoint3D& support, const TVector3D& direction):
132 support_(support)
133 {
134 kz_ = direction.majorAxis();
135 kx_ = kz_ + 1;
136 ky_ = kx_ + 1;
137 if (direction[kz_] < 0)
138 {
139 std::swap(kx_, ky_);
140 }
141 shear_.z = num::inv(direction[kz_]);
142 shear_.x = -direction[kx_] * shear_.z;
143 shear_.y = -direction[ky_] * shear_.z;
144 }
145
146 Result operator()(
147 const TPoint3D& vertex0, const TPoint3D& vertex1, const TPoint3D& vertex2,
148 TValue b[3], TValue& t, const TValue& tMin) const
149 {
150 const TVector3D v0 = vertex0 - support_;
151 const TVector3D v1 = vertex1 - support_;
152 const TVector3D v2 = vertex2 - support_;
153
154 const TValue v0x = v0[kx_] + v0[kz_] * shear_.x;
155 const TValue v0y = v0[ky_] + v0[kz_] * shear_.y;
156 const TValue v1x = v1[kx_] + v1[kz_] * shear_.x;
157 const TValue v1y = v1[ky_] + v1[kz_] * shear_.y;
158 const TValue v2x = v2[kx_] + v2[kz_] * shear_.x;
159 const TValue v2y = v2[ky_] + v2[kz_] * shear_.y;
160
161 TValue u = v2x * v1y - v2y * v1x;
162 TValue v = v0x * v2y - v0y * v2x;
163 TValue w = v1x * v0y - v1y * v0x;
164
165 using TDouble = typename num::DoublePrecision<TValue>::Type;
166 if constexpr (!std::is_same_v<TValue, TDouble>)
167 {
168 if (u == 0 || v == 0 || w == 0)
169 {
170 // retry with double precision
171 const TDouble v21xy = static_cast<TDouble>(v2x) * static_cast<TDouble>(v1y);
172 const TDouble v21yx = static_cast<TDouble>(v2y) * static_cast<TDouble>(v1x);
173 u = static_cast<TValue>(v21xy - v21yx);
174
175 const TDouble v02xy = static_cast<TDouble>(v0x) * static_cast<TDouble>(v2y);
176 const TDouble v02yx = static_cast<TDouble>(v0y) * static_cast<TDouble>(v2x);
177 v = static_cast<TValue>(v02xy - v02yx);
178
179 const TDouble v10xy = static_cast<TDouble>(v1x) * static_cast<TDouble>(v0y);
180 const TDouble v10yx = static_cast<TDouble>(v1y) * static_cast<TDouble>(v0x);
181 w = static_cast<TValue>(v10xy - v10yx);
182 }
183 }
184
185 if ((u < 0 || v < 0 || w < 0) && (u > 0 || v > 0 || w > 0))
186 {
187 return rNone;
188 }
189
190 const TValue det = u + v + w;
191 if (det == 0)
192 {
193 return rNone;
194 }
195 const TValue invDet = num::inv(det);
196
197 const TValue v0z = v0[kz_] * shear_.z;
198 const TValue v1z = v1[kz_] * shear_.z;
199 const TValue v2z = v2[kz_] * shear_.z;
200 const TValue tt = (v0z * u + v1z * v + v2z * w) * invDet;
201 if (tt <= tMin)
202 {
203 return rNone;
204 }
205
206 b[0] = u * invDet;
207 b[1] = v * invDet;
208 b[2] = w * invDet;
209 t = tt;
210 return rOne;
211 }
212
213private:
214 TPoint3D support_;
215 TVector3D shear_;
216 XYZ kx_;
217 XYZ ky_;
218 XYZ kz_;
219};
220
221
222}
223}
224}
225
226#endif
227
228// EOF
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
implementation details of lass::prim
set of geometrical primitives
Definition aabb_2d.h:81
Result
meta information on the result you have from an operation like an intersection ...
Definition result.h:74
@ rNone
operation has no answer, output arguments are meaningless
Definition result.h:76
@ rOne
there's exactly one answer, 1 output argument contains the answer
Definition result.h:77
Library for Assembled Shared Sources.
Definition config.h:53