File: dgMeshEffectSolidTree.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (556 lines) | stat: -rw-r--r-- 16,418 bytes parent folder | download | duplicates (3)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source distribution.
 */

#include "dgMeshEffectSolidTree.h"
#include "dgMeshEffect.h"
#include "hpl1/engine/libraries/newton/core/dg.h"


#define DG_CLIPPER_TOL dgFloat64(1.0e-12)

dgMeshTreeCSGFace::dgMeshTreeCSGFace(const dgMeshEffect &mesh,
                                     dgEdge *const face) : dgList<dgHugeVector>(mesh.GetAllocator()), m_side(dgMeshEffectSolidTree::m_divider) {
	const dgEdge *ptr = face;
	const dgMeshEffect::dgVertexAtribute *const attib = mesh.m_attib;
	do {
		Append(attib[ptr->m_userData].m_vertex);
		ptr = ptr->m_next;
	} while (ptr != face);
}

dgMeshTreeCSGFace::dgMeshTreeCSGFace(dgMemoryAllocator *const allocator,
                                     dgInt32 count, const dgHugeVector *const points) : dgList<dgHugeVector>(allocator), m_side(dgMeshEffectSolidTree::m_divider) {
	for (dgInt32 i = 0; i < count; i++) {
		Append(points[i]);
	}
}

bool dgMeshTreeCSGFace::IsPointOnEdge(const dgHugeVector &p0,
                                      const dgHugeVector &p1, const dgHugeVector &q0) const {
	dgHugeVector p1p0(p1 - p0);
	dgGoogol den(p1p0 % p1p0);

	dgHugeVector q0p0(q0 - p0);
	dgGoogol num(q0p0 % p1p0);

	dgFloat64 numf = num.GetAproximateValue();
	if (numf > (DG_CLIPPER_TOL * dgFloat64(1.0e3))) {
		if (numf < den.GetAproximateValue() * dgFloat64(1.0 - (DG_CLIPPER_TOL * dgFloat64(1.0e3)))) {
			dgGoogol t = num / den;
			NEWTON_ASSERT(t.GetAproximateValue() > dgFloat64(0.0f));
			NEWTON_ASSERT(t.GetAproximateValue() < dgFloat64(1.0f));
			dgHugeVector q1(p0 + p1p0.Scale(t));
			dgHugeVector dist(q1 - q0);
			dgGoogol dist2 = dist % dist;
			if (dist2.GetAproximateValue() < DG_CLIPPER_TOL) {
				return true;
			}
		}
	}
	return false;
}

bool dgMeshTreeCSGFace::CheckConvex(const dgHugeVector &normal) const {
	dgHugeVector p1(GetLast()->GetInfo());
	dgHugeVector p0(GetLast()->GetPrev()->GetInfo());
	dgHugeVector e0(p0 - p1);
	for (dgListNode *node = GetFirst(); node; node = node->GetNext()) {
		dgHugeVector p2(node->GetInfo());
		dgHugeVector e1(p2 - p1);

		dgHugeVector n(e1 * e0);
		dgGoogol convex = n % normal;
		if (convex.GetAproximateValue() < dgFloat64(-1.0e10f)) {
			return false;
		}
		p1 = p2;
		e0 = e1.Scale(dgGoogol(-1.0f));
	}
	return true;
}

void dgMeshTreeCSGFace::MergeMissingVertex(const dgMeshTreeCSGFace *const face) {
	for (dgMeshTreeCSGFace::dgListNode *outNode = GetFirst();
	        outNode != GetLast(); outNode = outNode->GetNext()) {
		dgHugeVector p0(outNode->GetInfo());
		for (dgMeshTreeCSGFace::dgListNode *node = face->GetFirst(); node; node =
		            node->GetNext()) {
			if (IsPointOnEdge(p0, outNode->GetNext()->GetInfo(), node->GetInfo())) {
				dgMeshTreeCSGFace::dgListNode *const insertNode = Append(
				            node->GetInfo());
				InsertAfter(outNode, insertNode);
			}
		}
	}

	RotateToBegin(GetLast());
	dgListNode *const last = GetFirst()->GetNext();
	for (dgMeshTreeCSGFace::dgListNode *outNode = GetFirst(); outNode != last;
	        outNode = outNode->GetNext()) {
		dgHugeVector p0(outNode->GetInfo());
		dgHugeVector p1(outNode->GetNext()->GetInfo());

		for (dgMeshTreeCSGFace::dgListNode *node = face->GetFirst(); node; node =
		            node->GetNext()) {
			if (IsPointOnEdge(p0, outNode->GetNext()->GetInfo(), node->GetInfo())) {
				dgMeshTreeCSGFace::dgListNode *const insertNode = Append(
				            node->GetInfo());
				InsertAfter(outNode, insertNode);
			}
		}
	}
}

dgInt32 dgMeshTreeCSGFace::RemoveDuplicates(dgInt32 count,
        dgHugeVector *const points) const {
	dgInt32 index[256];
	for (dgInt32 i = 0; i < count; i++) {
		index[i] = i + 1;
	}
	index[count - 1] = 0;

	dgInt32 originalCount = count;
	dgInt32 start = index[0];
	for (dgInt32 count1 = 0; (count1 != count) && (count >= 3);) {
		count1 = 0;
		for (dgInt32 i = 0; i < count; i++) {
			dgInt32 next = index[start];
			dgHugeVector err(points[start] - points[next]);
			dgGoogol dist2 = err % err;
			dgFloat64 val = dist2.GetAproximateValue();
			if (val < dgFloat64(1.0e-12f)) {
				index[start] = index[index[start]];
				count1 = 0;
				count--;
				break;
			}

			count1++;
			start = index[start];
		}
	}

	if ((count != originalCount) && (count >= 3)) {
		dgHugeVector tmp[256];
		for (dgInt32 i = 0; i < count; i++) {
			tmp[i] = points[start];
			start = index[start];
		}

		for (dgInt32 i = 0; i < count; i++) {
			points[i] = tmp[i];
		}
	}

	return count;
}

#ifdef _DEBUG
dgMatrix dgMeshTreeCSGFace::DebugMatrix() const {
	dgMatrix matrix(dgGetIdentityMatrix());
	dgHugeVector e0(GetFirst()->GetNext()->GetInfo() - GetFirst()->GetInfo());
	dgHugeVector e1(
	    GetFirst()->GetNext()->GetNext()->GetInfo() - GetFirst()->GetInfo());
	dgHugeVector aaa(e0 * e1);
	dgVector aaaa(dgFloat32(aaa.m_x.GetAproximateValue()),
	              dgFloat32(aaa.m_y.GetAproximateValue()),
	              dgFloat32(aaa.m_z.GetAproximateValue()), 0.0f);
	aaaa = aaaa.Scale(dgFloat32(1.0) / dgSqrt(aaaa % aaaa));

	dgVector bbbb(dgFloat32(e0.m_x.GetAproximateValue()),
	              dgFloat32(e0.m_y.GetAproximateValue()),
	              dgFloat32(e0.m_z.GetAproximateValue()), 0.0f);
	bbbb = bbbb.Scale(dgFloat32(1.0) / dgSqrt(bbbb % bbbb));

	matrix.m_up = bbbb;
	matrix.m_right = aaaa;
	matrix.m_front = bbbb * aaaa;

	return matrix;
}

void dgMeshTreeCSGFace::Trace(const dgMatrix &matrix) const {
	for (dgMeshTreeCSGFace::dgListNode *node = GetFirst(); node;
	        node = node->GetNext()) {
		const dgHugeVector &bbb = node->GetInfo();
		dgVector bbbb(dgFloat32(bbb.m_x.GetAproximateValue()),
		              dgFloat32(bbb.m_y.GetAproximateValue()),
		              dgFloat32(bbb.m_z.GetAproximateValue()), 0.0f);
		bbbb = matrix.UnrotateVector(bbbb);
		bbbb.Trace();
	}
	dgTrace(("\n"));
}

#endif

dgHugeVector dgMeshTreeCSGFace::FaceNormal() const {
	dgHugeVector area(0.0f, 0.0f, 0.0f, 0.0f);
	dgHugeVector e0(GetFirst()->GetNext()->GetInfo() - GetFirst()->GetInfo());
	for (dgListNode *node = GetFirst()->GetNext()->GetNext(); node;
	        node = node->GetNext()) {
		dgHugeVector e1(node->GetInfo() - GetFirst()->GetInfo());
		area += e0 * e1;
		e0 = e1;
	}
	return area;
}

bool dgMeshTreeCSGFace::CheckFaceArea(dgInt32 count,
                                      const dgHugeVector *const points) const {
	dgHugeVector area(dgFloat64(0.0), dgFloat64(0.0), dgFloat64(0.0),
	                  dgFloat64(0.0));
	dgHugeVector e0(points[1] - points[0]);
	for (dgInt32 i = 2; i < count; i++) {
		dgHugeVector e1(points[i] - points[0]);
		area += e0 * e1;
		e0 = e1;
	}

	dgFloat64 val = (area % area).GetAproximateValue();
	return (val > dgFloat64(1.0e-12f)) ? true : false;
}

/*
 //dgGoogol xxx1(dgGoogol v)
 dgGoogol xxx1(dgInt32 x)
 {
 dgGoogol old (0);
 dgGoogol ans (1);
 while (x > 1) {
 dgGoogol n (ans + old);
 old = ans;
 ans = n;
 x = x - 1;
 }
 return ans;
 }

 dgGoogol xxx2(dgInt32 x)
 {
 dgGoogol one (1);
 dgGoogol n (x);
 dgGoogol a (1);
 for (int i = x; i >= 1; i --) {
 a = a * n;
 n -= one;
 }
 return a;
 }

 void xxxx()
 {
 char text[256];
 // dgGoogol a (xxx1(100));
 // dgGoogol a (1.05);
 dgGoogol a (xxx2 (60));
 a.ToString(text);
 // dgFloat64 xxxxx = a.GetAproximateValue();
 }
 */

void dgMeshTreeCSGFace::Clip(const dgHugeVector &plane,
                             dgMeshTreeCSGFace **leftOut, dgMeshTreeCSGFace **rightOut) {
	// xxxx ();

	dgInt8 pointSide[256];

	dgInt32 count = 0;
	dgInt32 rightPCount = 0;
	dgInt32 leftPCount = 0;
	for (dgMeshTreeCSGFace::dgListNode *ptr = GetFirst(); ptr; ptr =
	            ptr->GetNext()) {
		const dgHugeVector &p = ptr->GetInfo();

		dgGoogol test = plane.EvaluePlane(p);
		dgFloat64 val = test.GetAproximateValue();
		if (fabs(val) < DG_CLIPPER_TOL) {
			val = dgFloat64(0.0f);
		}

		if (val > dgFloat64(0.0f)) {
			pointSide[count] = 1;
			rightPCount++;
		} else if (val < dgFloat64(0.0f)) {
			pointSide[count] = -1;
			leftPCount++;
		} else {
			pointSide[count] = 0;
		}
		count++;
	}

	*leftOut = NULL;
	*rightOut = NULL;
	if ((leftPCount && !rightPCount) || (!leftPCount && rightPCount)) {
		if (leftPCount) {
			NEWTON_ASSERT(!rightPCount);
			AddRef();
			*leftOut = this;
		} else {
			NEWTON_ASSERT(!leftPCount);
			*rightOut = this;
			AddRef();
		}
	} else if (!(leftPCount || rightPCount)) {
		NEWTON_ASSERT(!leftPCount);
		NEWTON_ASSERT(!rightPCount);
		AddRef();
		// AddRef();
	} else {
		dgInt32 leftCount = 0;
		dgInt32 rightCount = 0;
		dgHugeVector leftFace[256];
		dgHugeVector rightFace[256];

		dgInt32 i1 = 0;
		dgInt32 i0 = count - 1;
		dgHugeVector p0(GetLast()->GetInfo());
		for (dgMeshTreeCSGFace::dgListNode *ptr = GetFirst(); ptr;
		        ptr = ptr->GetNext()) {
			const dgHugeVector &p1(ptr->GetInfo());

			dgHugeVector inter;
			if (((pointSide[i0] == -1) && (pointSide[i1] == 1)) || ((pointSide[i0] == 1) && (pointSide[i1] == -1))) {
				// inter = Interpolate (plane, p0, p1);
				dgHugeVector dp(p1 - p0);
				dgGoogol den(plane % dp);
				dgGoogol num = plane.EvaluePlane(p0);
				NEWTON_ASSERT(fabs(num.GetAproximateValue()) > dgFloat64(0.0f));
				dgGoogol ti(num / den);
				inter = p0 - dp.Scale(num / den);
			}

			if (pointSide[i1] == -1) {
				if (pointSide[i0] == 1) {
					rightFace[rightCount] = inter;
					rightCount++;
				}
			} else {
				if ((pointSide[i1] == 1) && (pointSide[i0] == -1)) {
					rightFace[rightCount] = inter;
					rightCount++;
				}
				rightFace[rightCount] = p1;
				rightCount++;
			}

			if (pointSide[i1] == 1) {
				if (pointSide[i0] == -1) {
					leftFace[leftCount] = inter;
					leftCount++;
				}
			} else {
				if ((pointSide[i1] == -1) && (pointSide[i0] == 1)) {
					leftFace[leftCount] = inter;
					leftCount++;
				}
				leftFace[leftCount] = p1;
				leftCount++;
			}
			NEWTON_ASSERT(leftCount < dgInt32((sizeof(leftFace) / sizeof(leftFace[0])) - 1));
			NEWTON_ASSERT(rightCount < dgInt32((sizeof(rightFace) / sizeof(rightFace[0])) - 1));

			i0 = i1;
			i1++;
			p0 = p1;
		}

		leftCount = RemoveDuplicates(leftCount, leftFace);
		rightCount = RemoveDuplicates(rightCount, rightFace);
		if ((leftCount >= 3) && CheckFaceArea(leftCount, leftFace)) {
			*leftOut = new (GetAllocator()) dgMeshTreeCSGFace(GetAllocator(),
			        leftCount, leftFace);
		}

		if ((rightCount >= 3) && CheckFaceArea(rightCount, rightFace)) {
			*rightOut = new (GetAllocator()) dgMeshTreeCSGFace(GetAllocator(),
			        rightCount, rightFace);
		}
		NEWTON_ASSERT(*leftOut || *rightOut);
	}
}

dgMeshEffectSolidTree::dgMeshEffectSolidTree(dgPlaneType type) : m_planeType(type), m_back(NULL), m_front(NULL), m_plane(0.0, 0.0, 0.0, 0.0) {
}

dgMeshEffectSolidTree::dgMeshEffectSolidTree(const dgMeshEffect &mesh,
        dgEdge *const face) : m_planeType(m_divider), m_back(new (mesh.GetAllocator()) dgMeshEffectSolidTree(m_solid)), m_front(new (mesh.GetAllocator()) dgMeshEffectSolidTree(m_empty)), m_plane(BuildPlane(mesh, face)) {
}

dgMeshEffectSolidTree::dgMeshEffectSolidTree(const dgHugeVector &plane,
        dgMemoryAllocator *const allocator) : m_planeType(m_divider), m_back(new (allocator) dgMeshEffectSolidTree(m_solid)), m_front(new (allocator) dgMeshEffectSolidTree(m_empty)), m_plane(plane) {
}

dgMeshEffectSolidTree::~dgMeshEffectSolidTree() {
	if (m_front) {
		delete m_front;
	}

	if (m_back) {
		delete m_back;
	}
}

dgHugeVector dgMeshEffectSolidTree::BuildPlane(const dgMeshEffect &mesh,
        dgEdge *const face) const {
	dgEdge *edge = face;
	dgHugeVector plane(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f),
	                   dgFloat32(0.0f));

	dgHugeVector p0(mesh.m_points[edge->m_incidentVertex]);

	edge = edge->m_next;
	dgHugeVector p1(mesh.m_points[edge->m_incidentVertex]);

	dgHugeVector e1(p1 - p0);
	for (edge = edge->m_next; edge != face; edge = edge->m_next) {
		dgHugeVector p2(mesh.m_points[edge->m_incidentVertex]);
		dgHugeVector e2(p2 - p0);
		plane += e1 * e2;
		e1 = e2;
	}

	plane.m_w = (p0 % plane) * dgGoogol(-1.0);
	return plane;
}

void dgMeshEffectSolidTree::AddFace(const dgMeshEffect &mesh,
                                    dgEdge *const face) {
	dgBigVector normal(
	    mesh.FaceNormal(face, &mesh.m_points[0][0], sizeof(dgBigVector)));
	dgFloat64 mag2 = normal % normal;

	if (mag2 > dgFloat32(1.0e-14f)) {
		dgMeshTreeCSGFace *faces[DG_MESH_EFFECT_BOLLEAN_STACK];
		dgMeshEffectSolidTree *pool[DG_MESH_EFFECT_BOLLEAN_STACK];

		dgHugeVector plane(BuildPlane(mesh, face));

		dgInt32 stack = 1;
		pool[0] = this;
		faces[0] = new (mesh.GetAllocator()) dgMeshTreeCSGFace(mesh, face);

		while (stack) {

			stack--;
			dgMeshEffectSolidTree *const root = pool[stack];
			NEWTON_ASSERT(root->m_planeType == m_divider);

			dgMeshTreeCSGFace *const curve = faces[stack];
			NEWTON_ASSERT(curve->CheckConvex(plane));

			dgMeshTreeCSGFace *backOut;
			dgMeshTreeCSGFace *frontOut;
			curve->Clip(root->m_plane, &backOut, &frontOut);

			if ((backOut == NULL) && (frontOut == NULL)) {
				curve->Release();
			} else {
				if (backOut && frontOut) {
					dgHugeVector backArea(backOut->FaceNormal());
					dgHugeVector frontArea(frontOut->FaceNormal());

					dgFloat64 backMag = (backArea % backArea).GetAproximateValue();
					dgFloat64 frontMag = (frontArea % frontArea).GetAproximateValue();
					if (backMag > frontMag) {
						if (backMag > (frontMag * dgFloat64(1.0e6))) {
							frontOut->Release();
							frontOut = NULL;
						}
					} else {
						if (frontMag > (backMag * dgFloat64(1.0e6))) {
							backOut->Release();
							backOut = NULL;
						}
					}
				}

				if (backOut) {
					if (root->m_back->m_planeType != m_divider) {
						backOut->Release();
						delete root->m_back;
						root->m_back = new (mesh.GetAllocator()) dgMeshEffectSolidTree(
						    plane, mesh.GetAllocator());
					} else {
						faces[stack] = backOut;
						pool[stack] = root->m_back;
						stack++;
						NEWTON_ASSERT(stack < dgInt32(sizeof(pool) / sizeof(pool[0])));
					}
				}

				if (frontOut) {
					if (root->m_front->m_planeType != m_divider) {
						frontOut->Release();
						delete root->m_front;
						root->m_front = new (mesh.GetAllocator()) dgMeshEffectSolidTree(
						    plane, mesh.GetAllocator());
					} else {
						faces[stack] = frontOut;
						pool[stack] = root->m_front;
						stack++;
						NEWTON_ASSERT(stack < dgInt32(sizeof(pool) / sizeof(pool[0])));
					}
				}
			}

			curve->Release();
		}
	}
}

dgMeshEffectSolidTree::dgPlaneType dgMeshEffectSolidTree::GetPointSide(
    const dgHugeVector &point) const {
	const dgMeshEffectSolidTree *root = this;

	NEWTON_ASSERT(root);
	while (root->m_planeType == dgMeshEffectSolidTree::m_divider) {
		dgGoogol test = root->m_plane.EvaluePlane(point);
		dgFloat64 dist = test.GetAproximateValue();

		if (fabs(dist) < dgFloat64(1.0e-16f)) {
			dgPlaneType isBackSide = root->m_back->GetPointSide(point);
			dgPlaneType isFrontSide = root->m_front->GetPointSide(point);
			return (isBackSide == isFrontSide) ? isFrontSide : m_divider;

		} else if (dist > dgFloat64(0.0f)) {
			root = root->m_front;
		} else {
			NEWTON_ASSERT(dist < dgFloat64(0.0f));
			root = root->m_back;
		}
	}
	return root->m_planeType;
}

dgMeshEffectSolidTree::dgPlaneType dgMeshEffectSolidTree::GetFaceSide(
    const dgMeshTreeCSGFace *const face) const {
	dgHugeVector center(0.0, 0.0, 0.0, 0.0);
	for (dgMeshTreeCSGFace::dgListNode *node = face->GetFirst(); node;
	        node = node->GetNext()) {
		const dgHugeVector &point = node->GetInfo();
		center += point;
	}
	center = center.Scale(dgGoogol(1.0) / dgGoogol(face->GetCount()));
	dgPlaneType faceSide(GetPointSide(center));
	return faceSide;
}