Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/triangle-closestPoint.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 Closest point in a 3D triangle to the given query 3D point
31 Jernej Barbic
32 Carnegie Mellon University, 2003-2007
33 */
34 #include "triangle.h"
35 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
36 double TriangleWithCollisionInfo::distanceToPoint2(const Vec3d & point, int * closestFeature, double * alpha, double * beta, double * gamma) const
37 #else
38 double TriangleWithCollisionInfo::distanceToPoint2(const Vec3d & point, int * closestFeature) const
39 #endif
40
41 {
42 // squared 3d distance to a point
43 // also returns the closest feature to the query point:
44 // 0: vertex0
45 // 1: vertex1
46 // 2: vertex2
47 // 3: edge among 01
48 // 4: edge among 12
49 // 5: edge among 20
50 // 6: the face itself
51
52 // and also returns the barycentric coordinates of the closest point
53
54 /*
55
56 y \G E /
57 ^ N21\ /N12
58 | 2/
59 | I /\
60 | / \ F
61 N22| / \ /
62 \ | /S2 S1\ /N11
63 \ | / J \ /
64 H \|/ \/ D
65 ---------------------->
66 0| |1 x
67 | |
68 A | C | B
69 | |
70
71 */
72
73 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
74 double * barycentricCoordsRaw[3] = { alpha, beta, gamma};
75 double * barycentricCoords[3] = {
76 barycentricCoordsRaw[permutation[0]], barycentricCoordsRaw[permutation[1]], barycentricCoordsRaw[permutation[2]]};
77 #endif
78
79 // first transform the point into the plane
80 Vec3d P = Q * point + x0;
81
82 //printf("Transformed point: %.15f %.15f %.15f\n", P[0], P[1], P[2]);
83 //printf("a: %.15f\n", sidea);
84 double d = P[2] * P[2]; // contribution due to the z-offset
85
86 // determine distance of P[0],P[1] to the planar triangle
87
88 P[2] = 1; // transformation to homogeneous coordinates
89 double x = P[0];
90 double y = P[1];
91
92 double u,v;
93 bool caseECandidate = false;
94
95 if (y <= 0)
96 {
97 if (x < 0)
98 {
99 // case A
100 //printf("case A\n");
101 *closestFeature = permutation[0]; // vertex 0
102 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
103 // *alpha = 1; *beta = 0; *gamma = 0;
104 *(barycentricCoords[0]) = 1; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 0;
105 #endif
106 return (d + x*x + y*y);
107 }
108 else if (x > sidea)
109 {
110 // case B
111 //printf("case B\n");
112 *closestFeature = permutation[1]; // vertex 1
113 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
114 // *alpha = 0; *beta = 1; *gamma = 0;
115 *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = 1; *(barycentricCoords[2]) = 0;
116 #endif
117 return (d + (x-sidea)*(x-sidea) + y*y);
118 }
119 else
120 {
121 // case C
122 //printf("case C\n");
123 *closestFeature = permutation[3]; // edge 01
124 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
125 // *alpha = (sidea-x)/sidea; *beta = 1 - *alpha; *gamma = 0;
126 *(barycentricCoords[0]) = (sidea-x)/sidea; *(barycentricCoords[1]) = 1 - *(barycentricCoords[0]); *(barycentricCoords[2]) = 0;
127 #endif
128 return (d + y*y);
129 }
130 }
131 else
132 {
133 // we have y > 0
134
135 u = dot(S1,P);
136 if (u >= 0) // above s1
137 {
138 v = dot(N11,P);
139 if (v <= 0) // below N11
140 {
141 // case D
142 //printf("case D\n");
143 *closestFeature = permutation[1]; // vertex 1
144 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
145 // *alpha = 0; *beta = 1; *gamma = 0;
146 *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = 1; *(barycentricCoords[2]) = 0;
147 #endif
148 return (d + v*v + u*u);
149 }
150 else
151 {
152 v = dot(N12,P);
153 if (v >= 0) // above N12
154 {
155 // case E
156 caseECandidate = true;
157
158 //printf("case E (candidate)\n");
159 // *closestFeature = permutation[2]; // vertex 2
160 // *alpha = 0; *beta = 0; *gamma = 1;
161 // *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 1;
162 //return (d + v*v + u*u);
163 }
164 else // between N11 and N12
165 {
166 // case F
167 //printf("case F\n");
168 *closestFeature = permutation[4]; // edge 12
169 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
170 // *alpha = 0; *beta = -v / sideb; *gamma = 1 - *beta;
171 *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = -v / sideb; *(barycentricCoords[2]) = 1 - *(barycentricCoords[1]);
172 #endif
173 return (d + u*u);
174 }
175 }
176 }
177
178 u = dot(S2,P);
179 if ((u >= 0) || (caseECandidate)) // above s2
180 {
181 v = dot(N21,P);
182 //begin
183 if (v > 0) // above N21
184 {
185 // here, we always have u >= 0 (check?)
186
187 v = dot(N22,P);
188 if (v >= 0) // above N22
189 {
190 // case H
191 //printf("case H\n");
192 *closestFeature = permutation[0]; // vertex 0
193 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
194 // *alpha = 1; *beta = 0; *gamma = 0;
195 *(barycentricCoords[0]) = 1; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 0;
196 #endif
197 return (d + v*v + u*u);
198 }
199 else // between N21 and N22
200 {
201 // case I
202 //printf("case I\n");
203 *closestFeature = permutation[5]; // edge 20
204 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
205 // *beta = 0;
206 // *gamma = -v / sidec;
207 // *alpha = 1-*gamma;
208 *(barycentricCoords[1]) = 0;
209 *(barycentricCoords[2]) = -v / sidec;
210 *(barycentricCoords[0]) = 1- *(barycentricCoords[2]);
211 #endif
212 return (d + u*u);
213 }
214 }
215 else
216 {
217 // below N21
218
219 // case G
220 //printf("case G\n");
221 *closestFeature = permutation[2]; // vertex 2
222 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
223 //*alpha = 0; *beta = 0; *gamma = 1;
224 *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 1;
225 #endif
226 return (d + v*v + u*u);
227 }
228 //end
229
230
231 /*
232 // old begin
233 if ((v <= 0) || (caseECandidate) )// below N21
234 {
235 // case G
236 //printf("case G\n");
237 *closestFeature = permutation[2]; // vertex 2
238 // *alpha = 0; *beta = 0; *gamma = 1;
239 *(barycentricCoords[0]) = 0; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 1;
240 return (d + v*v + u*u);
241 }
242 else
243 {
244 // not caseECandidate or else would have exited already
245
246 v = dot(N22,P);
247 if (v >= 0) // above N22
248 {
249 // case H
250 printf("case H\n");
251 *closestFeature = permutation[0]; // vertex 0
252 // *alpha = 1; *beta = 0; *gamma = 0;
253 *(barycentricCoords[0]) = 1; *(barycentricCoords[1]) = 0; *(barycentricCoords[2]) = 0;
254 return (d + v*v + u*u);
255 }
256 else // between N21 and N22
257 {
258 // case I
259 printf("case I\n");
260 // *beta = 0;
261 // *gamma = -v / sidec;
262 // *alpha = 1-*gamma;
263 *(barycentricCoords[1]) = 0;
264 *(barycentricCoords[2]) = -v / sidec;
265 *(barycentricCoords[0]) = 1- *(barycentricCoords[2]);
266 *closestFeature = permutation[5]; // edge 20
267 return (d + u*u);
268 }
269 }
270 // old end
271 */
272 }
273
274 // case J
275 //printf("case J\n");
276 *closestFeature = 6; // the face itself
277 #ifdef __VERSION_WITH_BARYCENTRIC_COORDS_JNB_CMU__
278 // *gamma = 0.5 * y * sidea / area;
279 // *beta = - 0.5 * u * sidec / area;
280 // *alpha = 1 - *beta - *gamma;
281 *(barycentricCoords[2]) = 0.5 * y * sidea / area;
282 *(barycentricCoords[1]) = - 0.5 * u * sidec / area;
283 *(barycentricCoords[0]) = 1 - *(barycentricCoords[1]) - *(barycentricCoords[2]);
284 #endif
285 return d; // inside the triangle
286 }
287
288 }