Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BOP_MathUtils.cpp « intern « boolop « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c7d10f1d231bc37e723464410990d44d469bd22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
 *
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Marc Freixas, Ken Hughes
 *
 * ***** END GPL LICENSE BLOCK *****
 */
 
#include "BOP_MathUtils.h"
#include <iostream>
using namespace std;

/**
 * Compares two scalars with EPSILON accuracy.
 * @param A scalar
 * @param B scalar
 * @return 1 if A > B, -1 if A < B, 0 otherwise
 */
int BOP_comp(const MT_Scalar A, const MT_Scalar B)
{
#ifndef VAR_EPSILON
	if (A >= B + BOP_EPSILON) return 1;
	else if (B >= A + BOP_EPSILON) return -1;
	else return 0; 
#else
	int expA, expB;
	float mant;
	frexp(A, &expA);	/* get exponents of each number */
	frexp(B, &expB);

	if(expA < expB)		/* find the larger exponent */
		expA = expB;
	mant = frexp((A-B), &expB);	/* get exponent of the difference */
	/* mantissa will only be zero is (A-B) is really zero; otherwise, also
	 * also allow a "reasonably" small exponent or "reasonably large"
	 * difference in exponents to be considers "close to zero" */
	if( mant == 0 || expB < -30 || expA - expB > 31) return 0;
	else if( mant > 0) return 1;
	else return -1;
#endif
}

/**
 * Compares a scalar with EPSILON accuracy.
 * @param A scalar
 * @return 1 if A > 0, -1 if A < 0, 0 otherwise
 */
int BOP_comp0(const MT_Scalar A)
{
	if (A >= BOP_EPSILON) return 1;
	else if (0 >= A + BOP_EPSILON) return -1;
	else return 0; 
}

/**
 * Compares two scalar triplets with EPSILON accuracy.
 * @param A scalar triplet
 * @param B scalar triplet
 * @return 1 if A > B, -1 if A < B, 0 otherwise
 */
int BOP_comp(const MT_Tuple3& A, const MT_Tuple3& B)
{
#ifndef VAR_EPSILON
	if (A.x() >= (B.x() + BOP_EPSILON)) return 1;
	else if (B.x() >= (A.x() + BOP_EPSILON)) return -1;
	else if (A.y() >= (B.y() + BOP_EPSILON)) return 1;
	else if (B.y() >= (A.y() + BOP_EPSILON)) return -1;
	else if (A.z() >= (B.z() + BOP_EPSILON)) return 1;
	else if (B.z() >= (A.z() + BOP_EPSILON)) return -1;
	else return 0;
#else
	int result = BOP_comp(A.x(), B.x());
	if (result != 0) return result;
	result = BOP_comp(A.y(), B.y());
	if (result != 0) return result;
	return BOP_comp(A.z(), B.z());
#endif
}

/**
 * Compares two scalars strictly.
 * @param A scalar
 * @param B scalar
 * @return 1 if A > B, -1 if A < B, 0 otherwise
 */
int BOP_exactComp(const MT_Scalar A, const MT_Scalar B)
{
	if (A > B) return 1;
	else if (B > A) return -1;
	else return 0; 
}
/**
 * Compares two scalar strictly.
 * @param A scalar triplet
 * @param B scalar triplet
 * @return 1 if A > B, -1 if A < B, 0 otherwise
 */
int BOP_exactComp(const MT_Tuple3& A, const MT_Tuple3& B)
{
	if (A.x() > B.x()) return 1;
	else if (B.x() > A.x()) return -1;
	else if (A.y() > B.y()) return 1;
	else if (B.y() > A.y()) return -1;
	else if (A.z() > B.z()) return 1;
	else if (B.z() > A.z()) return -1;
	else return 0;
}

/**
 * Returns if p1 is between p2 and p3 and lay on the same line (are collinears).
 * @param p1 point
 * @param p2 point
 * @param p3 point
 * @return true if p1 is between p2 and p3 and lay on the same line, false otherwise
 */
bool BOP_between(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3)
{
	MT_Scalar distance = p2.distance(p3);
	return (p1.distance(p2) < distance && p1.distance(p3) < distance) && BOP_collinear(p1,p2,p3);
}

/**
 * Returns if three points lay on the same line (are collinears).
 * @param p1 point
 * @param p2 point
 * @param p3 point
 * @return true if the three points lay on the same line, false otherwise
 */
bool BOP_collinear(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3)
{
	if( BOP_comp(p1,p2) == 0 || BOP_comp(p2,p3) == 0 ) return true;

	MT_Vector3 v1 = p2 - p1;
	MT_Vector3 v2 = p3 - p2;

	/* normalize vectors before taking their cross product, so its length 
     * has some actual meaning */
	// if(MT_fuzzyZero(v1.length()) || MT_fuzzyZero(v2.length())) return true;
	v1.normalize();	
	v2.normalize();

	MT_Vector3 w = v1.cross(v2);
	
	return (BOP_fuzzyZero(w.x()) && BOP_fuzzyZero(w.y()) && BOP_fuzzyZero(w.z()));
}

/**
 * Returns if a quad (coplanar) is convex.
 * @return true if the quad is convex, false otherwise
 */
bool BOP_convex(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3, const MT_Point3& p4)
{
	MT_Vector3 v1 = p3 - p1;
	MT_Vector3 v2 = p4 - p2;
	MT_Vector3 quadPlane = v1.cross(v2);
	// plane1 is the perpendicular plane that contains the quad diagonal (p2,p4)
	MT_Plane3 plane1(quadPlane.cross(v2),p2);
	// if p1 and p3 are classified in the same region, the quad is not convex 
	if (BOP_classify(p1,plane1) == BOP_classify(p3,plane1)) return false;
	else {
		// Test the other quad diagonal (p1,p3) and perpendicular plane
		MT_Plane3 plane2(quadPlane.cross(v1),p1);
		// if p2 and p4 are classified in the same region, the quad is not convex
		return (BOP_classify(p2,plane2) != BOP_classify(p4,plane2));
	}
}

/**
 * Returns if a quad (coplanar) is concave and where is the split edge.
 * @return 0 if is convex, 1 if is concave and split edge is p1-p3 and -1 if is
 * cancave and split edge is p2-p4.
 */
int BOP_concave(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3, const MT_Point3& p4)
{
	MT_Vector3 v1 = p3 - p1;
	MT_Vector3 v2 = p4 - p2;
	MT_Vector3 quadPlane = v1.cross(v2);
	// plane1 is the perpendicular plane that contains the quad diagonal (p2,p4)
	MT_Plane3 plane1(quadPlane.cross(v2),p2);
	// if p1 and p3 are classified in the same region, the quad is not convex 
	if (BOP_classify(p1,plane1) == BOP_classify(p3,plane1)) return 1;
	else {
		// Test the other quad diagonal (p1,p3) and perpendicular plane
		MT_Plane3 plane2(quadPlane.cross(v1),p1);
		// if p2 and p4 are classified in the same region, the quad is not convex
		if (BOP_classify(p2,plane2) == BOP_classify(p4,plane2)) return -1;
		else return 0;
	}
}

/**
 * Computes the intersection between two lines (on the same plane).
 * @param vL1 first line vector
 * @param pL1 first line point
 * @param vL2 second line vector
 * @param pL2 second line point
 * @param intersection intersection point (if exists)
 * @return false if lines are parallels, true otherwise
 */
bool BOP_intersect(const MT_Vector3& vL1, const MT_Point3& pL1, const MT_Vector3& vL2, 
				   const MT_Point3& pL2, MT_Point3 &intersection)
{
	// NOTE: 
    // If the lines aren't on the same plane, the intersection point will not be valid. 
	// So be careful !!

	MT_Scalar t = -1;
	MT_Scalar den = (vL1.y()*vL2.x() - vL1.x() * vL2.y());
	
	if (!BOP_fuzzyZero(den)) {
		t =  (pL2.y()*vL1.x() - vL1.y()*pL2.x() + pL1.x()*vL1.y() - pL1.y()*vL1.x()) / den ;
	}
	else {
		den = (vL1.y()*vL2.z() - vL1.z() * vL2.y());
		if (!BOP_fuzzyZero(den)) {
			t =  (pL2.y()*vL1.z() - vL1.y()*pL2.z() + pL1.z()*vL1.y() - pL1.y()*vL1.z()) / den ;
		}
		else {
			den = (vL1.x()*vL2.z() - vL1.z() * vL2.x());
			if (!BOP_fuzzyZero(den)) {
				t =  (pL2.x()*vL1.z() - vL1.x()*pL2.z() + pL1.z()*vL1.x() - pL1.x()*vL1.z()) / den ;
			}
			else {
				return false;
			}
		}
	}
	
	intersection.setValue(vL2.x()*t + pL2.x(), vL2.y()*t + pL2.y(), vL2.z()*t + pL2.z());
	return true;
}

/**
 * Returns the center of the circle defined by three points.
 * @param p1 point
 * @param p2 point
 * @param p3 point
 * @param center circle center
 * @return false if points are collinears, true otherwise
 */
bool BOP_getCircleCenter(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3, 
						 MT_Point3& center)
{
	// Compute quad plane
	MT_Vector3 p1p2 = p2-p1;
	MT_Vector3 p1p3 = p3-p1;
	MT_Plane3 plane1(p1,p2,p3);
	MT_Vector3 plane = plane1.Normal();
	
	// Compute first line vector, perpendicular to plane vector and edge (p1,p2)
	MT_Vector3 vL1 = p1p2.cross(plane);
	if( MT_fuzzyZero(vL1.length() ) )
			return false;
	vL1.normalize();
	
	// Compute first line point, middle point of edge (p1,p2)
	MT_Point3 pL1 = p1.lerp(p2, 0.5);

	// Compute second line vector, perpendicular to plane vector and edge (p1,p3)
	MT_Vector3 vL2 = p1p3.cross(plane);
	if( MT_fuzzyZero(vL2.length() ) )
			return false;
	vL2.normalize();
	
	// Compute second line point, middle point of edge (p1,p3)
	MT_Point3 pL2 = p1.lerp(p3, 0.5);

	// Compute intersection (the lines lay on the same plane, so the intersection exists
    // only if they are not parallel!!)
	return BOP_intersect(vL1,pL1,vL2,pL2,center);
}

/**
 * Returns if points q is inside the circle defined by p1, p2 and p3.
 * @param p1 point
 * @param p2 point
 * @param p3 point
 * @param q point
 * @return true if p4 or p5 are inside the circle, false otherwise. If 
 * the circle does not exist (p1, p2 and p3 are collinears) returns true
 */
bool BOP_isInsideCircle(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3, 
						const MT_Point3& q)
{
	MT_Point3 center;

	// Compute circle center
	bool ok = BOP_getCircleCenter(p1,p2,p3,center);
	
	if (!ok) return true; // p1,p2 and p3 are collinears

	// Check if q is inside the circle
	MT_Scalar r = p1.distance(center);
	MT_Scalar d = q.distance(center);    
	return (BOP_comp(d,r) <= 0);
}

/**
 * Returns if points p4 or p5 is inside the circle defined by p1, p2 and p3.
 * @param p1 point
 * @param p2 point
 * @param p3 point
 * @param p4 point
 * @param p5 point
 * @return true if p4 or p5 is inside the circle, false otherwise. If 
 * the circle does not exist (p1, p2 and p3 are collinears) returns true
 */
bool BOP_isInsideCircle(const MT_Point3& p1, const MT_Point3& p2, const MT_Point3& p3, 
						const MT_Point3& p4, const MT_Point3& p5)
{
	MT_Point3 center;
	bool ok = BOP_getCircleCenter(p1,p2,p3,center);

	if (!ok) return true; // Collinear points!

	// Check if p4 or p5 is inside the circle
	MT_Scalar r = p1.distance(center);
	MT_Scalar d1 = p4.distance(center);
	MT_Scalar d2 = p5.distance(center);
	return (BOP_comp(d1,r) <= 0 || BOP_comp(d2,r) <= 0);
}

/**
 * Returns if two planes share the same orientation.
 * @return >0 if planes share the same orientation
 */
MT_Scalar BOP_orientation(const MT_Plane3& p1, const MT_Plane3& p2)
{
	// Dot product between plane normals
	return (p1.x()*p2.x() + p1.y()*p2.y() + p1.z()*p2.z());
}

/**
 * Classifies a point according to the specified plane with EPSILON accuracy.
 * @param p point
 * @param plane plane
 * @return >0 if the point is above (OUT), 
 *         =0 if the point is on (ON), 
 *         <0 if the point is below (IN)
 */
int BOP_classify(const MT_Point3& p, const MT_Plane3& plane)
{
	// Compare plane - point distance with zero
	return BOP_comp0(plane.signedDistance(p));
}

/**
 * Intersects a plane with the line that contains the specified points.
 * @param plane split plane
 * @param p1 first line point
 * @param p2 second line point
 * @return intersection between plane and line that contains p1 and p2
 */
MT_Point3 BOP_intersectPlane(const MT_Plane3& plane, const MT_Point3& p1, const MT_Point3& p2)
{
	// Compute intersection between plane and line ...
    //
	//       L: (p2-p1)lambda + p1
	//
	// supposes resolve equation ...
	//
	//       coefA*((p2.x - p1.y)*lambda + p1.x) + ... + coefD = 0
	
    MT_Point3 intersection = MT_Point3(0,0,0); //never ever return anything undefined! 
    MT_Scalar den = plane.x()*(p2.x()-p1.x()) + 
					plane.y()*(p2.y()-p1.y()) + 
					plane.z()*(p2.z()-p1.z());
	if (den != 0) {
		MT_Scalar lambda = (-plane.x()*p1.x()-plane.y()*p1.y()-plane.z()*p1.z()-plane.w()) / den;
		intersection.setValue(p1.x() + (p2.x()-p1.x())*lambda, 
						  p1.y() + (p2.y()-p1.y())*lambda, 
						  p1.z() + (p2.z()-p1.z())*lambda);
		return intersection;
	}
	return intersection;
}

/**
 * Returns if a plane contains a point with EPSILON accuracy.
 * @param plane plane
 * @param point point
 * @return true if the point is on the plane, false otherwise
 */
bool BOP_containsPoint(const MT_Plane3& plane, const MT_Point3& point)
{
	return BOP_fuzzyZero(plane.signedDistance(point));
}

/**
 * Pre: p0, p1 and p2 is a triangle and q is an interior point.
 * @param p0 point
 * @param p1 point
 * @param p2 point
 * @param q point
 * @return intersection point I
 *                v 
 *  (p0)-----(I)----->(p1)
 *    \       ^        /
 *     \      |w      /
 *      \     |      /
 *       \   (q)    /
 *        \   |    /
 *         \  |   /
 *          \ |  /
 *           (p2)
 *
 * v = P1-P2
 * w = P3-Q
 * r0(t) = v*t+P1
 * r1(t) = w*t+P3
 * I = r0^r1
 */
MT_Point3 BOP_4PointIntersect(const MT_Point3& p0, const MT_Point3& p1, const MT_Point3& p2, 
							  const MT_Point3& q)
{
	MT_Vector3 v(p0.x()-p1.x(), p0.y()-p1.y(), p0.z()-p1.z());
	MT_Vector3 w(p2.x()-q.x(), p2.y()-q.y(), p2.z()-q.z());
	MT_Point3 I;
	
	BOP_intersect(v,p0,w,p2,I);
	return I;
}

/**
 * Pre: p0, p1 and q are collinears.
 * @param p0 point
 * @param p1 point
 * @param q point
 * @return 0 if q == p0, 1 if q == p1, or a value between 0 and 1 otherwise
 *
 * (p0)-----(q)------------(p1)
 *   |<-d1-->|               |
 *   |<---------d0---------->|
 * 
 */
MT_Scalar BOP_EpsilonDistance(const MT_Point3& p0, const MT_Point3& p1, const MT_Point3& q)
{
	MT_Scalar d0 = p0.distance(p1);
	MT_Scalar d1 = p0.distance(q);
	MT_Scalar d;
	
	if (BOP_fuzzyZero(d0)) d = 1.0;
	else if (BOP_fuzzyZero(d1)) d = 0.0;
	else d = d1 / d0;
	return d;
}