Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/triangle.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 /*
30 A triangle
31 Jernej Barbic, CMU
32 */
33
34 #if defined(_WIN32) || defined(WIN32)
35 #pragma warning(disable : 4996)
36 #pragma warning(disable : 4267)
37 #pragma warning(disable : 4244)
38 #endif
39
40 #include "openGL-headers.h"
41 #include <set>
42 #include "triangle.h"
43 #include "tribox3.h"
44 using namespace std;
45
46 bool TriangleBasic::doesIntersectBox(const BoundingBox & bbox) const
47 {
48 // prepare the data and call the "triBoxOverlap" worker routine
49 const Vec3d & center = bbox.center();
50 const Vec3d & halfSides = bbox.halfSides();
51 return triBoxOverlap(&center[0], &halfSides[0], &first_[0], &second_[0], &third_[0]);
52 }
53
54 int TriangleBasic::lineSegmentIntersection(const Vec3d & segmentStart, const Vec3d & segmentEnd, Vec3d * intersectionPoint) const
55 {
56 // Jernej Barbic, CMU, 2005
57 // code modified from the following code:
58
59 // ------------------------------------
60 // Copyright 2001, softSurfer (www.softsurfer.com)
61 // This code may be freely used and modified for any purpose
62 // providing that this copyright notice is included with it.
63 // SoftSurfer makes no warranty for this code, and cannot be held
64 // liable for any real or imagined damage resulting from its use.
65 // Users of this code must verify correctness for their application.
66 // ------------------------------------
67
68 // Output: intersection point (when it exists)
69 // Return: -1 = triangle is degenerate (a segment or point)
70 // 0 = disjoint (no intersect)
71 // 1 = intersect in unique point I1
72 // 2 = are in the same plane
73
74 Vec3d u, v, n; // triangle vectors
75 Vec3d dir, w0, w; // ray vectors
76 double r, a, b; // params to calc ray-plane intersect
77
78 // get triangle edge vectors and plane normal
79 u = second_ - first_;
80 v = third_- first_;
81 n = cross(u, v); // cross product
82 if ((n[0] == 0) && // triangle is degenerate
83 (n[1] == 0) &&
84 (n[2] == 0) )
85 return -1; // do not deal with this case
86
87 dir = segmentEnd - segmentStart; // ray direction vector
88 w0 = segmentStart - first_;
89 a = -dot(n,w0);
90 b = dot(n,dir);
91 if (b == 0)
92 {
93 // ray is parallel to triangle plane
94 if (a == 0) // ray lies in triangle plane
95 return 2;
96 else
97 return 0; // ray disjoint from plane
98 }
99
100 // get intersection point of ray with triangle plane
101 r = a / b;
102 if (r < 0.0) // ray goes away from triangle
103 return 0; // => no intersect
104 // for a segment, also test if (r > 1.0) => no intersect
105 if (r > 1.0) // ray goes away from triangle
106 return 0; // => no intersect
107
108 *intersectionPoint = segmentStart + r * dir; // intersect point of ray and plane
109
110 // is intersectionPoint inside T?
111 double uu, uv, vv, wu, wv, D;
112 uu = dot(u,u);
113 uv = dot(u,v);
114 vv = dot(v,v);
115 w = *intersectionPoint - first_;
116 wu = dot(w,u);
117 wv = dot(w,v);
118 D = uv * uv - uu * vv;
119
120 // get and test parametric coords
121 double s, t;
122 s = (uv * wv - vv * wu) / D;
123 if (s < 0.0 || s > 1.0) // intersectionPoint is outside T
124 return 0;
125 t = (uv * wu - uu * wv) / D;
126 if (t < 0.0 || (s + t) > 1.0) // intersectionPoint is outside T
127 return 0;
128
129 return 1; // intersectionPoint is in T
130 }
131
132 void TriangleBasic::render() const
133 {
134 Vec3d a = first_;
135 Vec3d b = second_;
136 Vec3d c = third_;
137
138 glBegin(GL_TRIANGLES);
139 glVertex3f(a[0],a[1],a[2]);
140 glVertex3f(b[0],b[1],b[2]);
141 glVertex3f(c[0],c[1],c[2]);
142 glEnd();
143 }
144
145 void TriangleBasic::renderEdges() const
146 {
147 Vec3d a = first_;
148 Vec3d b = second_;
149 Vec3d c = third_;
150
151 glBegin(GL_LINES);
152 glVertex3f(a[0],a[1],a[2]);
153 glVertex3f(b[0],b[1],b[2]);
154 glVertex3f(b[0],b[1],b[2]);
155 glVertex3f(c[0],c[1],c[2]);
156 glVertex3f(c[0],c[1],c[2]);
157 glVertex3f(a[0],a[1],a[2]);
158 glEnd();
159 }
160
161 template<class TriangleClass>
162 void makeUniqueList(const vector<TriangleClass*> & triangleList, vector<TriangleClass*> & uniqueList)
163 {
164 uniqueList.clear();
165 set<int> indices;
166 unsigned int ssize = triangleList.size();
167 for(unsigned int i=0; i<ssize; i++)
168 {
169 int index = triangleList[i]->index();
170 if (indices.find(index) == indices.end())
171 {
172 indices.insert(index);
173 uniqueList.push_back(triangleList[i]);
174 }
175 }
176 }
177
178 template<class TriangleClass>
179 void makeUniqueList(vector<TriangleClass*> & triangleList)
180 {
181 vector<TriangleClass*> uniqueList;
182 makeUniqueList(triangleList,uniqueList);
183 triangleList = uniqueList;
184 }
185
186 void TriangleWithCollisionInfo::ComputeCollisionInfo()
187 {
188 // first, determine the affine transformation that maps the triangle into xy plane,
189 // with first vertex to the origin,
190 // second vertex on the positive x-axis, and
191 // with a positive second coordinate for the third vertex
192
193 Vec3d p0Temp = first_;
194 Vec3d p1Temp = second_;
195 Vec3d p2Temp = third_;
196
197 double sideLengths[3] = { len(p1Temp-p0Temp), len(p2Temp-p1Temp), len(p0Temp-p2Temp) };
198
199 int longestIndex = -1;
200 double longestSide = -1.0;
201
202 for(int i=0; i<3; i++)
203 {
204 if (sideLengths[i] >= longestSide)
205 {
206 longestSide = sideLengths[i];
207 longestIndex = i;
208 }
209 }
210
211 if ((longestIndex < 0) || (longestIndex > 2))
212 {
213 // this should never happen
214 printf("Warning: an internal index is negative.\n");
215 longestIndex = 0; // we could also exit here
216 }
217
218 Vec3d p0,p1,p2;
219
220 if (longestIndex == 0)
221 {
222 p0 = p0Temp;
223 p1 = p1Temp;
224 p2 = p2Temp;
225
226 permutation[0] = 0;
227 permutation[1] = 1;
228 permutation[2] = 2;
229 permutation[3] = 3;
230 permutation[4] = 4;
231 permutation[5] = 5;
232 }
233 else
234 if (longestIndex == 1)
235 {
236 p0 = p1Temp;
237 p1 = p2Temp;
238 p2 = p0Temp;
239
240 permutation[0] = 1;
241 permutation[1] = 2;
242 permutation[2] = 0;
243 permutation[3] = 4;
244 permutation[4] = 5;
245 permutation[5] = 3;
246 }
247 else
248 //if (longestIndex == 2)
249 {
250 p0 = p2Temp;
251 p1 = p0Temp;
252 p2 = p1Temp;
253
254 permutation[0] = 2;
255 permutation[1] = 0;
256 permutation[2] = 1;
257 permutation[3] = 5;
258 permutation[4] = 3;
259 permutation[5] = 4;
260 }
261
262 // 0: vertex0
263 // 1: vertex1
264 // 2: vertex2
265 // 3: edge among 01
266 // 4: edge among 12
267 // 5: edge among 20
268 // 6: the face itself
269
270 // now, p0,p1,p2 have been renumbered and permutation has been set
271
272 sidea = len(p1-p0);
273 sideb = len(p2-p1);
274 sidec = len(p0-p2);
275
276 Vec3d q1T,q2T,q3T;
277
278 q1T = (p1-p0)/sidea;
279
280 q3T = norm(cross(p1-p0,p2-p0));
281
282 q2T = cross(q3T,q1T);
283
284 // assemble matrix Q
285 Mat3d QT = Mat3d(q1T[0],q2T[0],q3T[0],
286 q1T[1],q2T[1],q3T[1],
287 q1T[2],q2T[2],q3T[2]);
288 Q = trans(QT);
289
290 x0 = (-1)*Q*p0;
291
292 // sanity checks
293 Vec3d y;
294 y = Q * p0 + x0;
295 if (len(y) > 1E-5)
296 {
297 cout << "Warning: transformation does not map p0 vector to the origin." << endl;
298 }
299 if (sidea < 0)
300 {
301 cout << "Warning: distance parameter a=" << sidea << " is negative." << endl;
302 }
303
304 if (sidea == 0)
305 {
306 cout << "Warning: distance parameter a=" << sidea << " is zero. Degenerate triangle?" << endl;
307 }
308
309 y = Q * p1 + x0;
310 if (len(y-Vec3d(sidea,0,0)) > 1E-5)
311 {
312 cout << "Warning: transformation does not map p1 vector to (a,0,0)." << endl;
313 }
314 y = Q * p2 + x0;
315 if (fabs(y[2]) > 1E-5)
316 {
317 cout << "Warning: transformation does not map p2 vector into the xy plane (z=" << y[2] << ")." << endl;
318 }
319 if (y[1] < 0)
320 {
321 cout << "Warning: transformation does not map p2 vector into the upper half of the xy plane. (y=" << y[1] << ")." << endl;
322 }
323 if (y[0] < 0)
324 {
325 cout << "Warning: transformation does not map p2 vector into the positive right half of the xy plane. (x=" << y[0] << ")." << endl;
326 }
327 if (y[0] > sidea)
328 {
329 cout << "Warning: transformation does not map p2 vector into a vector with smaller x-coordinate than sidea=" << sidea <<
330 " (x=" << y[0] << ")." << endl;
331 }
332
333 area = 0.5 * sidea * y[1];
334
335 // cache normals for line distance tests
336 // compute image of p2 in the xy plane
337 Vec3d P2_3d = Q * p2 + x0;
338 Vec2d P2(P2_3d[0],P2_3d[1]);
339 Vec2d P1(sidea,0);
340 Vec2d P0(0,0);
341
342 Vec2d n1 = norm(P2-P1);
343 // s1 equals n1 rotate 90 deg clockwise
344 Vec2d s1 = Vec2d(n1[1],-n1[0]);
345 S1 = Vec3d(s1[0],s1[1],-dot(P1,s1));
346 N11 = Vec3d(n1[0],n1[1],-dot(P1,n1));
347 N12 = Vec3d(n1[0],n1[1],-dot(P2,n1));
348
349 Vec2d n2 = norm(P0-P2);
350 // s2 equals n2 rotate 90 deg clockwise
351 Vec2d s2 = Vec2d(n2[1],-n2[0]);
352 S2 = Vec3d(s2[0],s2[1],-dot(P2,s2));
353 N21 = Vec3d(n2[0],n2[1],-dot(P2,n2));
354 N22 = Vec3d(n2[0],n2[1],-dot(P0,n2));
355 }
356
357 #undef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
358 #include "triangle-closestPoint.cpp"
359 #define __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
360 #include "triangle-closestPoint.cpp"
361
362 TriangleWithCollisionInfoAndPseudoNormals::TriangleWithCollisionInfoAndPseudoNormals
363 (const Vec3d & first_g, const Vec3d & second_g, const Vec3d & third_g, const Vec3d pseudoNormals[7]): TriangleWithCollisionInfo(first_g,second_g,third_g)
364 {
365 ComputeCollisionInfo();
366 pseudoNormal_[0] = pseudoNormals[0];
367 pseudoNormal_[1] = pseudoNormals[1];
368 pseudoNormal_[2] = pseudoNormals[2];
369 pseudoNormal_[3] = pseudoNormals[3];
370 pseudoNormal_[4] = pseudoNormals[4];
371 pseudoNormal_[5] = pseudoNormals[5];
372 pseudoNormal_[6] = pseudoNormals[6];
373
374 pseudoClosestPosition_[0] = first_;
375 pseudoClosestPosition_[1] = second_;
376 pseudoClosestPosition_[2] = third_;
377 pseudoClosestPosition_[3] = 0.5 * (first_ + second_);
378 pseudoClosestPosition_[4] = 0.5 * (second_ + third_);
379 pseudoClosestPosition_[5] = 0.5 * (third_ + first_);
380 pseudoClosestPosition_[6] = 1.0 / 3 * (first_ + second_ + third_);
381 }
382
383 template void makeUniqueList<TriangleBasic>(const vector<TriangleBasic*> & triangleList, vector<TriangleBasic*> & uniqueList);
384 template void makeUniqueList<TriangleBasic>(vector<TriangleBasic*> & triangleList);
385
386 template void makeUniqueList<TriangleWithCollisionInfo>(const vector<TriangleWithCollisionInfo*> & triangleList, vector<TriangleWithCollisionInfo*> & uniqueList);
387 template void makeUniqueList<TriangleWithCollisionInfo>(vector<TriangleWithCollisionInfo*> & triangleList);
388
389 template void makeUniqueList<TriangleWithCollisionInfoAndPseudoNormals>(const vector<TriangleWithCollisionInfoAndPseudoNormals*> & triangleList, vector<TriangleWithCollisionInfoAndPseudoNormals*> & uniqueList);
390 template void makeUniqueList<TriangleWithCollisionInfoAndPseudoNormals>(vector<TriangleWithCollisionInfoAndPseudoNormals*> & triangleList);
391