Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/wildMagic/geometryQuery/verticesQueryRational.cpp

raw symbols SCIP
1 /*
2 This code is based on code from the Geometric Tools library,
3 which is licensed under a boost license.
4 Such usage is permitted by the boost license; for details,
5 please see the boost license below.
6 */
7
8 // Geometric Tools, LLC
9 // Copyright (c) 1998-2014
10 // Distributed under the Boost Software License, Version 1.0.
11 // http://www.boost.org/LICENSE_1_0.txt
12 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
13
14 /*************************************************************************
15 * *
16 * We release our improvements to the wildMagic code under our standard *
17 * Vega FEM license, as follows: *
18 * *
19 * Vega FEM Simulation Library Version 3.1 *
20 * *
21 * "improvements to the wildMagic library" , Copyright (C) 2016 USC *
22 * All rights reserved. *
23 * *
24 * Code author: Yijing Li *
25 * http://www.jernejbarbic.com/code *
26 * *
27 * Funding: National Science Foundation *
28 * *
29 * This library is free software; you can redistribute it and/or *
30 * modify it under the terms of the BSD-style license that is *
31 * included with this library in the file LICENSE.txt *
32 * *
33 * This library is distributed in the hope that it will be useful, *
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
36 * LICENSE.TXT for more details. *
37 * *
38 *************************************************************************/
39
40 #include "verticesQueryRational.h"
41 #include <cstring>
42 using namespace std;
43
44 const double ep = 0;
45 VerticesQueryRational::VerticesQueryRational (int numVertices,
46 const Vec3d * v) : VerticesQuery(numVertices, v)
47 {
48 rVertices.resize(vertices.size());
49 evaluated.resize(vertices.size(), 0);
50 // memset(&mEvaluated[0], 0, mNumVertices * sizeof(bool));
51 }
52 //----------------------------------------------------------------------------
53
54 VerticesQueryRational::~VerticesQueryRational ()
55 {
56 }
57 //----------------------------------------------------------------------------
58
59 void VerticesQueryRational::addVertex(const Vec3d & v)
60 {
61 VerticesQuery::addVertex(v);
62 rVertices.push_back(QRVector());
63 evaluated.push_back(0);
64 }
65
66 int VerticesQueryRational::toPlane (int i, int v0, int v1, int v2) const
67 {
68 int indices[4] = { i, v0, v1, v2 };
69 convert(4, indices);
70 return toPlane(rVertices[i], v0, v1, v2);
71 }
72 //----------------------------------------------------------------------------
73
74 int VerticesQueryRational::toPlane (const Vec3d& test, int v0, int v1,
75 int v2) const
76 {
77 QRVector ratTest;
78 ratTest[0] = QRational(test[0]);
79 ratTest[1] = QRational(test[1]);
80 ratTest[2] = QRational(test[2]);
81 int indices[3] = { v0, v1, v2 };
82 convert(3, indices);
83 return toPlane(ratTest, v0, v1, v2);
84 }
85 //----------------------------------------------------------------------------
86
87 int VerticesQueryRational::toTetrahedron (int i, int v0, int v1, int v2,
88 int v3) const
89 {
90 int indices[5] = { i, v0, v1, v2, v3 };
91 convert(5, indices);
92 return toTetrahedron(rVertices[i], v0, v1, v2, v3);
93 }
94 //----------------------------------------------------------------------------
95
96 int VerticesQueryRational::toTetrahedron (const Vec3d& test, int v0,
97 int v1, int v2, int v3) const
98 {
99 QRVector ratTest;
100 ratTest[0] = QRational(test[0]);
101 ratTest[1] = QRational(test[1]);
102 ratTest[2] = QRational(test[2]);
103 int indices[4] = { v0, v1, v2, v3 };
104 convert(4, indices);
105 return toTetrahedron(ratTest, v0, v1, v2, v3);
106 }
107 //----------------------------------------------------------------------------
108
109 int VerticesQueryRational::toCircumsphere (int i, int v0, int v1, int v2,
110 int v3) const
111 {
112 int indices[5] = { i, v0, v1, v2, v3 };
113 convert(5, indices);
114 return toCircumsphere(rVertices[i], v0, v1, v2, v3);
115 }
116 //----------------------------------------------------------------------------
117
118 int VerticesQueryRational::toCircumsphere (const Vec3d& test, int v0,
119 int v1, int v2, int v3) const
120 {
121 QRVector ratTest;
122 ratTest[0] = QRational(test[0]);
123 ratTest[1] = QRational(test[1]);
124 ratTest[2] = QRational(test[2]);
125 int indices[4] = { v0, v1, v2, v3 };
126 convert(4, indices);
127 return toCircumsphere(ratTest, v0, v1, v2, v3);
128 }
129 //----------------------------------------------------------------------------
130
131 void VerticesQueryRational::convert (int numIndices, int* indices) const
132 {
133 for(int i = 0; i < numIndices; ++i)
134 {
135 int j = indices[i];
136 if (!evaluated[j])
137 {
138 evaluated[j] = true;
139 rVertices[j][0] = QRational(vertices[j][0]);
140 rVertices[j][1] = QRational(vertices[j][1]);
141 rVertices[j][2] = QRational(vertices[j][2]);
142 }
143 }
144 }
145 //----------------------------------------------------------------------------
146
147 int VerticesQueryRational::toPlane (const QRVector& ratTest, int v0, int v1,
148 int v2) const
149 {
150
151 // QRVector ab = (rVertices[v1] - rVertices[v0]);
152 // QRVector ac = (rVertices[v2] - rVertices[v0]);
153 // QRVector ax = ratTest - rVertices[v0];
154 //
155 // QRational dd = ax.tripleScalar(ab, ac);
156 // return (dd > 0 ? +1 : (dd < 0 ? -1 : 0));
157 //
158 // QRational x0 = ratTest[0] - rVertices[v0][0];
159 // QRational y0 = ratTest[1] - rVertices[v0][1];
160 // QRational z0 = ratTest[2] - rVertices[v0][2];
161 // QRational x1 = rVertices[v1][0] - rVertices[v0][0];
162 // QRational y1 = rVertices[v1][1] - rVertices[v0][1];
163 // QRational z1 = rVertices[v1][2] - rVertices[v0][2];
164 // QRational x2 = rVertices[v2][0] - rVertices[v0][0];
165 // QRational y2 = rVertices[v2][1] - rVertices[v0][1];
166 // QRational z2 = rVertices[v2][2] - rVertices[v0][2];
167
168 // QRational det = det3(x0, y0, z0, x1, y1, z1, x2, y2, z2);
169 // return (det > 0 ? +1 : (det < 0 ? -1 : 0));
170
171 OTriKey key(v0,v1,v2);
172
173 PlaneDataMapIter it = planeDataMap.find(key);
174 if (it == planeDataMap.end())
175 {
176 PlaneData data;
177 QRVector ab = (rVertices[v1] - rVertices[v0]);
178 QRVector ac = (rVertices[v2] - rVertices[v0]);
179 data.normal = ab.cross(ac);
180 data.d = rVertices[v0].dot(data.normal);
181 data.d = -data.d;
182 planeDataMap[key] = data;
183 QRational ret = ratTest.dot(data.normal) + data.d;
184 double r = ret.toDouble();
185 return (r > ep ? +1 : (r < -ep ? -1 : 0));
186 //
187 // QRVector ab = (rVertices[v1] - rVertices[v0]);
188 // QRVector ac = (rVertices[v2] - rVertices[v0]);
189 // QRVector ax = ratTest - rVertices[v0];
190 //
191 // QRational dd = ax.tripleScalar(ab, ac);
192 // return (dd > 0 ? +1 : (dd < 0 ? -1 : 0));
193
194
195 }
196 else
197 {
198 PlaneData & data = it->second;
199 planeDataMap[key] = data;
200 QRational ret = ratTest.dot(data.normal) + data.d;
201 double r = ret.toDouble();
202 return (r > ep ? +1 : (r < -ep ? -1 : 0));
203 }
204
205 }
206 //----------------------------------------------------------------------------
207
208 int VerticesQueryRational::toTetrahedron (const QRVector& ratTest, int v0,
209 int v1, int v2, int v3) const
210 {
211 int sign0 = toPlane(ratTest, v1, v2, v3);
212 if (sign0 > 0)
213 return +1;
214
215 int sign1 = toPlane(ratTest, v0, v2, v3);
216 if (sign1 < 0)
217 return +1;
218
219 int sign2 = toPlane(ratTest, v0, v1, v3);
220 if (sign2 > 0)
221 return +1;
222
223 int sign3 = toPlane(ratTest, v0, v1, v2);
224 if (sign3 < 0)
225 return +1;
226
227 return ((sign0 && sign1 && sign2 && sign3) ? -1 : 0);
228 }
229 //----------------------------------------------------------------------------
230
231
232 int VerticesQueryRational::toCircumsphere (const QRVector& ratTest, int v0, int v1, int v2, int v3) const
233 {
234 QRVector& ratVec0 = rVertices[v0];
235 QRVector& ratVec1 = rVertices[v1];
236 QRVector& ratVec2 = rVertices[v2];
237 QRVector& ratVec3 = rVertices[v3];
238
239 QRational s0x = ratVec0[0] + ratTest[0];
240 QRational s0y = ratVec0[1] + ratTest[1];
241 QRational s0z = ratVec0[2] + ratTest[2];
242
243 QRational d0x = ratVec0[0] - ratTest[0];
244 QRational d0y = ratVec0[1] - ratTest[1];
245 QRational d0z = ratVec0[2] - ratTest[2];
246
247 QRational s1x = ratVec1[0] + ratTest[0];
248 QRational s1y = ratVec1[1] + ratTest[1];
249 QRational s1z = ratVec1[2] + ratTest[2];
250
251 QRational d1x = ratVec1[0] - ratTest[0];
252 QRational d1y = ratVec1[1] - ratTest[1];
253 QRational d1z = ratVec1[2] - ratTest[2];
254
255 QRational s2x = ratVec2[0] + ratTest[0];
256 QRational s2y = ratVec2[1] + ratTest[1];
257 QRational s2z = ratVec2[2] + ratTest[2];
258
259 QRational d2x = ratVec2[0] - ratTest[0];
260 QRational d2y = ratVec2[1] - ratTest[1];
261 QRational d2z = ratVec2[2] - ratTest[2];
262
263 QRational s3x = ratVec3[0] + ratTest[0];
264 QRational s3y = ratVec3[1] + ratTest[1];
265 QRational s3z = ratVec3[2] + ratTest[2];
266
267 QRational d3x = ratVec3[0] - ratTest[0];
268 QRational d3y = ratVec3[1] - ratTest[1];
269 QRational d3z = ratVec3[2] - ratTest[2];
270
271 // wi = dot(si,di)
272 QRational w0 = s0x*d0x + s0y*d0y + s0z*d0z;
273 QRational w1 = s1x*d1x + s1y*d1y + s1z*d1z;
274 QRational w2 = s2x*d2x + s2y*d2y + s2z*d2z;
275 QRational w3 = s3x*d3x + s3y*d3y + s3z*d3z;
276 QRational det = det4(d0x, d0y, d0z, w0, d1x, d1y, d1z, w1, d2x, d2y, d2z,
277 w2, d3x, d3y, d3z, w3);
278 return (det > ep ? 1 : (det < -ep ? -1 : 0));
279 }
280
281 int VerticesQueryRational::toCircumsphere (int i, int v0, int v1, int v2) const
282 {
283 return toCircumsphere(vertices[i], v0, v1, v2);
284 }
285
286 static VerticesQueryRational::QRVector cross(const VerticesQueryRational::QRVector &r1, const VerticesQueryRational::QRVector &r2)
287 {
288 return r1.cross(r2);
289 }
290
291 static VerticesQueryRational::QRational len2(const VerticesQueryRational::QRVector &r)
292 {
293 return r.dot(r);
294 }
295
296 int VerticesQueryRational::toCircumsphere (const Vec3d& test, int v0, int v1, int v2) const
297 {
298 QRVector ratTest;
299 ratTest[0] = QRational(test[0]);
300 ratTest[1] = QRational(test[1]);
301 ratTest[2] = QRational(test[2]);
302 int indices[3] = { v0, v1, v2};
303 convert(3, indices);
304 return toCircumsphere(ratTest, v0, v1, v2);
305 }
306
307 int VerticesQueryRational::toCircumsphere (const QRVector& test, int v0, int v1, int v2) const
308 {
309 const QRVector & p0 = rVertices[v0];
310 const QRVector & p1 = rVertices[v1];
311 const QRVector & p2 = rVertices[v2];
312 sort(v0,v1,v2);
313 QRVector d0 = p1 - p0, d1 = p2 - p1, d2 = p0 - p2;
314 QRVector S = cross(d0, d1);
315 QRational S2 = len2(S);
316 QRational d02 = len2(d0), d12 = len2(d1), d22 = len2(d2);
317 QRVector center( (d12 * d0.dot(d2)) * p0 + (d22 * d0.dot(d1)) * p1 + (d02 * d2.dot(d1)) * p2 );
318 center /= -(S2 * 2);
319 QRational r2 = d02 * d12 * d22 / (S2 * 4);
320 //assert(abs(len2(center - p0) - r2) < 1e-6);
321 //assert(abs(len2(center - p1) - r2) < 1e-6);
322 //assert(abs(len2(center - p2) - r2) < 1e-6);
323
324 QRational dist2 = len2(test - center);
325 return dist2 > r2 + ep ? +1 : (dist2 < r2 - ep ? -1 : 0);
326 }
327
328 //----------------------------------------------------------------------------
329
330 VerticesQueryRational::QRational VerticesQueryRational::dot (
331 QRational& x0, QRational& y0, QRational& z0, QRational& x1,
332 QRational& y1, QRational& z1)
333 {
334 return x0*x1 + y0*y1 + z0*z1;
335 }
336 //----------------------------------------------------------------------------
337
338 VerticesQueryRational::QRational VerticesQueryRational::det3 (
339 QRational& x0, QRational& y0, QRational& z0, QRational& x1,
340 QRational& y1, QRational& z1, QRational& x2, QRational& y2,
341 QRational& z2)
342 {
343 QRational c00 = y1*z2 - y2*z1;
344 QRational c01 = y2*z0 - y0*z2;
345 QRational c02 = y0*z1 - y1*z0;
346 return x0*c00 + x1*c01 + x2*c02;
347 }
348 //----------------------------------------------------------------------------
349
350 VerticesQueryRational::QRational VerticesQueryRational::det4 (
351 QRational& x0, QRational& y0, QRational& z0, QRational& w0,
352 QRational& x1, QRational& y1, QRational& z1, QRational& w1,
353 QRational& x2, QRational& y2, QRational& z2, QRational& w2,
354 QRational& x3, QRational& y3, QRational& z3, QRational& w3)
355 {
356 QRational a0 = x0*y1 - x1*y0;
357 QRational a1 = x0*y2 - x2*y0;
358 QRational a2 = x0*y3 - x3*y0;
359 QRational a3 = x1*y2 - x2*y1;
360 QRational a4 = x1*y3 - x3*y1;
361 QRational a5 = x2*y3 - x3*y2;
362 QRational b0 = z0*w1 - z1*w0;
363 QRational b1 = z0*w2 - z2*w0;
364 QRational b2 = z0*w3 - z3*w0;
365 QRational b3 = z1*w2 - z2*w1;
366 QRational b4 = z1*w3 - z3*w1;
367 QRational b5 = z2*w3 - z3*w2;
368 return a0*b5 - a1*b4 + a2*b3 + a3*b2 - a4*b1 + a5*b0;
369 }