aboutsummaryrefslogtreecommitdiff
path: root/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.cpp
blob: 075bce9b8ad08e8d10a4944a9db2c415c0615c4f (plain) (blame)
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#include "NvBlastExtAuthoringAccelerator.h"
#include "NvBlastExtAuthoringMesh.h"
#include "NvBlastExtAuthoringInternalCommon.h"


using namespace physx;


namespace Nv
{
namespace Blast
{

DummyAccelerator::DummyAccelerator(int32_t count) :count(count)
{
	current = 0;
}
void DummyAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc)
{
	current = 0;
	(void)pos;
	(void)ed;
	(void)fc;
}
void DummyAccelerator::setState(const physx::PxVec3& point) {
	current = 0;
	(void)point;
}
int32_t DummyAccelerator::getNextFacet()
{
	if (current < count)
	{
		++current;
		return current - 1;
	}
	else
		return -1;
}



BBoxBasedAccelerator::BBoxBasedAccelerator(Mesh* mesh, int32_t resolution) : mResolution(resolution), alreadyGotValue(1)
{
	mBounds = mesh->getBoundingBox();
	mSpatialMap.resize(resolution * resolution * resolution);
	mCells.resize(resolution * resolution * resolution);
	int32_t currentCell = 0;
	PxVec3 incr = (mBounds.maximum - mBounds.minimum) * (1.0f / mResolution);
	for (int32_t z = 0; z < resolution; ++z)
	{
		for (int32_t y = 0; y < resolution; ++y)
		{
			for (int32_t x = 0; x < resolution; ++x)
			{
				mCells[currentCell].minimum.x = mBounds.minimum.x + x * incr.x;
				mCells[currentCell].minimum.y = mBounds.minimum.y + y * incr.y;
				mCells[currentCell].minimum.z = mBounds.minimum.z + z * incr.z;

				mCells[currentCell].maximum.x = mBounds.minimum.x + (x + 1) * incr.x;
				mCells[currentCell].maximum.y = mBounds.minimum.y + (y + 1) * incr.y;
				mCells[currentCell].maximum.z = mBounds.minimum.z + (z + 1) * incr.z;

				++currentCell;
			}
		}
	}

	buildAccelStructure(mesh->getVertices(), mesh->getEdges(), mesh->getFacetsBuffer(), mesh->getFacetCount());
}


BBoxBasedAccelerator::~BBoxBasedAccelerator()
{
	mResolution = 0;
	mBounds.setEmpty();
	mSpatialMap.clear();
	mCells.clear();
}

int32_t BBoxBasedAccelerator::getNextFacet()
{
	int32_t facetId = -1;

	while (mIteratorCell != -1)
	{
		if (mIteratorFacet >= (int32_t)mSpatialMap[mIteratorCell].size())
		{
			if (!cellList.empty())
			{
				mIteratorCell = cellList.back();
				cellList.pop_back();
				mIteratorFacet = 0;
			}
			else
			{
				mIteratorCell = -1;
				break;
			}
		}
		if (alreadyGotFlag[mSpatialMap[mIteratorCell][mIteratorFacet]] != alreadyGotValue)
		{
			facetId = mSpatialMap[mIteratorCell][mIteratorFacet];
			mIteratorFacet++;
			break;
		}
		else
		{
			mIteratorFacet++;
		}
	}
	if (facetId != -1)
	{
		alreadyGotFlag[facetId] = alreadyGotValue;
	}
	return facetId;
}
void BBoxBasedAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc)
{
	alreadyGotValue++;
	mIteratorCell = -1;
	mIteratorFacet = -1;
	cellList.clear();
	facetBox.setEmpty();
	Edge* edge = ed + fc.firstEdgeNumber;
	uint32_t count = fc.edgesCount;
	for (uint32_t ec = 0; ec < count; ++ec)
	{
		facetBox.include(pos[edge->s].p);
		facetBox.include(pos[edge->e].p);
		edge++;
	}
	for (uint32_t i = 0; i < mCells.size(); ++i)
	{
		if (testCellPolygonIntersection(i, facetBox))
		{
			if (!mSpatialMap[i].empty())
				cellList.push_back(i);
		}
	}
	if (!cellList.empty())
	{
		mIteratorFacet = 0;
		mIteratorCell = cellList.back();
		cellList.pop_back();
	}
}


void BBoxBasedAccelerator::setState(const PxVec3& p)
{
	alreadyGotValue++;
	mIteratorCell = -1;
	mIteratorFacet = -1;
	cellList.clear();
	int32_t perSlice = mResolution * mResolution;
	for (uint32_t i = 0; i < mCells.size(); ++i)
	{
		if (mCells[i].contains(p))
		{
			int32_t xyCellId = i % perSlice;
			for (int32_t zCell = 0; zCell < mResolution; ++zCell)
			{
				int32_t cell = zCell * perSlice + xyCellId;
				if (!mSpatialMap[cell].empty())
					cellList.push_back(cell);
			}
		}
	}
	if (!cellList.empty())
	{
		mIteratorFacet = 0;
		mIteratorCell = cellList.back();
		cellList.pop_back();
	}
}


bool BBoxBasedAccelerator::testCellPolygonIntersection(int32_t cellId, PxBounds3& facetBB)
{
	if (weakBoundingBoxIntersection(mCells[cellId], facetBB))
	{
		return true;
	}
	else
		return false;
}

void BBoxBasedAccelerator::buildAccelStructure(Vertex* pos, Edge* edges, Facet* fc, int32_t facetCount)
{
	for (int32_t facet = 0; facet < facetCount; ++facet)
	{
		PxBounds3 bBox;
		bBox.setEmpty();
		Edge* edge = &edges[0] + fc->firstEdgeNumber;
		int32_t count = fc->edgesCount;
		for (int32_t ec = 0; ec < count; ++ec)
		{
			bBox.include(pos[edge->s].p);
			bBox.include(pos[edge->e].p);
			edge++;
		}

		for (uint32_t i = 0; i < mCells.size(); ++i)
		{
			if (testCellPolygonIntersection(i, bBox))
			{
				mSpatialMap[i].push_back(facet);
			}
		}
		fc++;
	}
	alreadyGotFlag.resize(facetCount, 0);
	cellList.resize(mCells.size());
}

int32_t testEdgeAgainstCube(PxVec3& p1, PxVec3& p2)
{
	PxVec3 vec = p2 - p1;
	PxVec3 vecSigns;
	for (int32_t i = 0; i < 3; ++i)
	{
		vecSigns[i] = (vec[i] < 0) ? -1 : 1;
	}
	for (int32_t i = 0; i < 3; ++i)
	{
		if (p1[i] * vecSigns[i] > 0.5f) return 0;
		if (p2[i] * vecSigns[i] < -0.5f) return 0;
	}

	for (int32_t i = 0; i < 3; ++i)
	{
		int32_t ip1 = (i + 1) % 3;
		int32_t ip2 = (i + 2) % 3;

		float vl1 = vec[ip2] * p1[ip1] - vec[ip1] * p1[ip2];
		float vl2 = 0.5f * (vec[ip2] * vecSigns[ip1] + vec[ip1] * vecSigns[ip2]);
		if (vl1 * vl1 > vl2 * vl2)
		{
			return 0;
		}
	}
	return 1;
}

NV_INLINE int32_t isInSegm(float a, float b, float c)
{
	return (b >= c) - (a >= c);
}

NV_INLINE int32_t edgeIsAbovePoint(PxVec2& p1, PxVec2& p2, PxVec2& p)
{
	int32_t direction = isInSegm(p1.x, p2.x, p.x);
	if (direction != 0)
	{
		if (isInSegm(p1.y, p2.y, p.y))
		{
			if (direction * (p.x - p1.x) * (p2.y - p1.y) >= direction * (p.y - p1.y) * (p2.x - p1.x))
			{
				return direction;
			}
		}
		else
		{
			if (p1.y > p.y)
				return direction;
		}		
	}
	return 0;
}

int32_t pointInPolygon(PxVec3* vertices, PxVec3& diagPoint, int32_t edgeCount, PxVec3& normal)
{
	std::vector<PxVec2> projectedVertices(edgeCount * 2);
	ProjectionDirections pDir = getProjectionDirection(normal);
	PxVec2 projectedDiagPoint = getProjectedPoint(diagPoint, pDir);
	PxVec2* saveVert = projectedVertices.data();
	PxVec3* p = vertices;
	for (int32_t i = 0; i < edgeCount * 2; ++i)
	{
		*saveVert = getProjectedPoint(*p, pDir);
		++saveVert;
		++p;
	}	
	int32_t counter = 0;
	PxVec2* v = projectedVertices.data();
	for (int32_t i = 0; i < edgeCount; ++i)
	{
		PxVec2& p1 = *v;
		PxVec2& p2 = *(v + 1);
		counter += edgeIsAbovePoint(p1, p2, projectedDiagPoint);
		v += 2;
	}
	return counter != 0;
}



int32_t testFacetUnitCubeIntersectionInternal(PxVec3* vertices,PxVec3& facetNormal, int32_t edgeCount)
{
	PxVec3* pnt_p = vertices;
	for (int32_t i = 0; i < edgeCount; ++i)
	{
		if (testEdgeAgainstCube(*pnt_p, *(pnt_p + 1)) == 1)
		{
			return 1;
		}
		pnt_p += 2;
	}

	PxVec3 cubeDiag(0, 0, 0);
	for (int32_t i = 0; i < 3; ++i)
		cubeDiag[i] = (facetNormal[i] < 0) ? -1 : 1;
	float t = vertices->dot(facetNormal) / (cubeDiag.dot(facetNormal));
	if (t > 0.5 || t < -0.5)
		return 0;
	
	PxVec3 intersPoint = cubeDiag * t;
	int trs = pointInPolygon(vertices, intersPoint, edgeCount, facetNormal);
	return trs;
}

enum TrivialFlags
{
	HAS_POINT_BELOW_HIGH_X = ~(1 << 0),
	HAS_POINT_ABOVE_LOW_X = ~(1 << 1),

	HAS_POINT_BELOW_HIGH_Y = ~(1 << 2),
	HAS_POINT_ABOVE_LOW_Y = ~(1 << 3),

	HAS_POINT_BELOW_HIGH_Z = ~(1 << 4),
	HAS_POINT_ABOVE_LOW_Z = ~(1 << 5),



	ALL_ONE = (1 << 6) - 1
};





int32_t testFacetUnitCubeIntersection(Vertex* vertices, Edge* edges, Facet& fc, PxBounds3 cube, float fattening)
{
	Edge* ed = edges + fc.firstEdgeNumber;
	int32_t trivialFlags = ALL_ONE;
	cube.fattenFast(fattening);
	for (uint32_t i = 0; i < fc.edgesCount; ++i)
	{
		{
			PxVec3& p = vertices[ed->s].p;
			if (cube.contains(p))
				return 1;
			if (p.x < cube.getCenter().x + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_X;
			if (p.x > cube.getCenter().x - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_X;

			if (p.y < cube.getCenter().y + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_Y;
			if (p.y > cube.getCenter().y - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_Y;

			if (p.z < cube.getCenter().z + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_Z;
			if (p.z > cube.getCenter().z - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_Z;
		}
		{
			PxVec3& p = vertices[ed->e].p;
			if (cube.contains(p))
				return 1;
			if (p.x < cube.getCenter().x + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_X;
			if (p.x > cube.getCenter().x - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_X;

			if (p.y < cube.getCenter().y + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_Y;
			if (p.y > cube.getCenter().y - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_Y;

			if (p.z < cube.getCenter().z + 0.5)
				trivialFlags &= HAS_POINT_BELOW_HIGH_Z;
			if (p.z > cube.getCenter().z - 0.5)
				trivialFlags &= HAS_POINT_ABOVE_LOW_Z;
		}

		++ed;
	}
	if (trivialFlags != 0)
	{
		return 0;
	}
	std::vector<PxVec3> verticesRescaled(fc.edgesCount * 2);
	
	int32_t vrt = 0;
	ed = edges + fc.firstEdgeNumber;
	PxVec3 offset = cube.getCenter();
	PxVec3 normal(1, 1, 1);
	
	/**
		Compute normal
	*/
	PxVec3& v1 = vertices[ed->s].p;
	PxVec3* v2 = nullptr;
	PxVec3* v3 = nullptr;
	
	for (uint32_t i = 0; i < fc.edgesCount; ++i)
	{
		if (v1 != vertices[ed->s].p)
		{
			v2 = &vertices[ed->s].p;
			break;
		}
		if (v1 != vertices[ed->e].p)
		{
			v2 = &vertices[ed->e].p;
			break;
		}
		ed++;
	}
	ed = edges + fc.firstEdgeNumber;
	for (uint32_t i = 0; i < fc.edgesCount; ++i)
	{
		if (v1 != vertices[ed->s].p && *v2 != vertices[ed->s].p)
		{
			v3 = &vertices[ed->s].p;
			break;
		}
		if (v1 != vertices[ed->e].p && *v2 != vertices[ed->e].p)
		{
			v3 = &vertices[ed->e].p;
			break;
		}
		ed++;
	}
	ed = edges + fc.firstEdgeNumber;
	if (v2 != nullptr && v3 != nullptr)
	{
		normal = (*v2 - v1).cross(*v3 - v1); 
	}
	else
	{
		return true; // If cant find normal, assume it intersects box.
	}

	
	normal.normalize();

	PxVec3 rescale(.5f / (cube.getExtents().x), .5f / (cube.getExtents().y), 0.5f / (cube.getExtents().z));
	for (uint32_t i = 0; i < fc.edgesCount; ++i)
	{
		verticesRescaled[vrt] = vertices[ed->s].p - offset;
		verticesRescaled[vrt].x *= rescale.x;
		verticesRescaled[vrt].y *= rescale.y;
		verticesRescaled[vrt].z *= rescale.z;
		++vrt;
		verticesRescaled[vrt] = vertices[ed->e].p - offset;
		verticesRescaled[vrt].x *= rescale.x;
		verticesRescaled[vrt].y *= rescale.y;
		verticesRescaled[vrt].z *= rescale.z;
		++ed;
		++vrt;
	}
	return testFacetUnitCubeIntersectionInternal(verticesRescaled.data(), normal, fc.edgesCount);
}


IntersectionTestingAccelerator::IntersectionTestingAccelerator(Mesh* in, int32_t resolution)
{


	alreadyGotFlag.resize(in->getFacetCount(), 0);
	alreadyGotValue = 0;
	mResolution = resolution;

	float cubeSize = 1.0f / resolution;
	PxVec3 cubeMinimal(-0.5, -0.5, -0.5);
	PxVec3 extents(cubeSize, cubeSize, cubeSize);
	mCubes.resize(mResolution * mResolution * mResolution);
	mSpatialMap.resize(mCubes.size());
	int32_t cubeId = 0;

	// Build unit cube partition
	for (int32_t i = 0; i < mResolution; ++i)
	{				
		cubeMinimal.y = -0.5;
		cubeMinimal.z = -0.5;
		for (int32_t j = 0; j < mResolution; ++j)
		{
			cubeMinimal.z = -0.5;			
			for (int32_t k = 0; k < mResolution; ++k)
			{				
				mCubes[cubeId].minimum = cubeMinimal;
				mCubes[cubeId].maximum = cubeMinimal + extents;
				cubeMinimal.z += cubeSize;
				++cubeId;
			}
			cubeMinimal.y += cubeSize;
		}
		cubeMinimal.x += cubeSize;
	}
	

	for (uint32_t i = 0; i < in->getFacetCount(); ++i)
	{
		for (uint32_t c = 0; c < mCubes.size(); ++c)
		{
			if (testFacetUnitCubeIntersection(in->getVertices(), in->getEdges(), *in->getFacet(i), mCubes[c], 0.001))
			{
				mSpatialMap[c].push_back(i);
			}
		}
	}
}


int32_t IntersectionTestingAccelerator::getNextFacet()
{
	int32_t facetId = -1;

	while (mIteratorCell != -1)
	{
		if (mIteratorFacet >= (int32_t)mSpatialMap[mIteratorCell].size())
		{
			if (!cellList.empty())
			{
				mIteratorCell = cellList.back();
				cellList.pop_back();
				mIteratorFacet = 0;
			}
			else
			{
				mIteratorCell = -1;
				break;
			}
		}
		if (alreadyGotFlag[mSpatialMap[mIteratorCell][mIteratorFacet]] != alreadyGotValue)
		{
			facetId = mSpatialMap[mIteratorCell][mIteratorFacet];
			mIteratorFacet++;
			break;
		}
		else
		{
			mIteratorFacet++;
		}
	}
	if (facetId != -1)
	{
		alreadyGotFlag[facetId] = alreadyGotValue;
	}
	return facetId;
}

void IntersectionTestingAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc)
{
	alreadyGotValue++;
	mIteratorCell = -1;
	mIteratorFacet = -1;
	cellList.clear();
	PxBounds3 bigBox(PxVec3(-0.5, -0.5, -0.5), PxVec3(0.5, 0.5, 0.5));
	if (!testFacetUnitCubeIntersection(pos, ed, fc, bigBox, 0.001f))
	{
		return;
	}
	for (uint32_t i = 0; i < mCubes.size(); ++i)
	{
		if (testFacetUnitCubeIntersection(pos, ed, fc, mCubes[i], 0.001f))
		{
			if (!mSpatialMap[i].empty())
				cellList.push_back(i);
		}
	}
	if (!cellList.empty())
	{
		mIteratorFacet = 0;
		mIteratorCell = cellList.back();
		cellList.pop_back();
	}
}

void IntersectionTestingAccelerator::setState(const PxVec3& p)
{
	alreadyGotValue++;
	mIteratorCell = -1;
	mIteratorFacet = -1;
	cellList.clear();
	
	
	for (uint32_t i = 0; i < mCubes.size(); ++i)
	{		
		PxBounds3 tmp = mCubes[i];
		tmp.fattenFast(0.001);
		if (tmp.contains(p))
		{
			int32_t xyCellId = (((int)((float)i / mResolution)) * mResolution);
			for (int32_t zCell = 0; zCell < mResolution; ++zCell)
			{
				int32_t cell = zCell + xyCellId;
				if (!mSpatialMap[cell].empty())
				{
					cellList.push_back(cell);
				}
					
			}
		}
	}
	if (!cellList.empty())
	{
		mIteratorFacet = 0;
		mIteratorCell = cellList.back();
		cellList.pop_back();
	}
}


} // namespace Blast
} // namespace Nv