forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyhedron.cpp
2293 lines (1810 loc) · 89.2 KB
/
polyhedron.cpp
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "mathlib/polyhedron.h"
#include "mathlib/vmatrix.h"
#include <stdlib.h>
#include <stdio.h>
#include "tier1/utlvector.h"
struct GeneratePolyhedronFromPlanes_Point;
struct GeneratePolyhedronFromPlanes_PointLL;
struct GeneratePolyhedronFromPlanes_Line;
struct GeneratePolyhedronFromPlanes_LineLL;
struct GeneratePolyhedronFromPlanes_Polygon;
struct GeneratePolyhedronFromPlanes_PolygonLL;
struct GeneratePolyhedronFromPlanes_UnorderedPointLL;
struct GeneratePolyhedronFromPlanes_UnorderedLineLL;
struct GeneratePolyhedronFromPlanes_UnorderedPolygonLL;
Vector FindPointInPlanes( const float *pPlanes, int planeCount );
bool FindConvexShapeLooseAABB( const float *pInwardFacingPlanes, int iPlaneCount, Vector *pAABBMins, Vector *pAABBMaxs );
CPolyhedron *ClipLinkedGeometry( GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pPolygons, GeneratePolyhedronFromPlanes_UnorderedLineLL *pLines, GeneratePolyhedronFromPlanes_UnorderedPointLL *pPoints, const float *pOutwardFacingPlanes, int iPlaneCount, float fOnPlaneEpsilon, bool bUseTemporaryMemory );
CPolyhedron *ConvertLinkedGeometryToPolyhedron( GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pPolygons, GeneratePolyhedronFromPlanes_UnorderedLineLL *pLines, GeneratePolyhedronFromPlanes_UnorderedPointLL *pPoints, bool bUseTemporaryMemory );
//#define ENABLE_DEBUG_POLYHEDRON_DUMPS //Dumps debug information to disk for use with glview. Requires that tier2 also be in all projects using debug mathlib
//#define DEBUG_DUMP_POLYHEDRONS_TO_NUMBERED_GLVIEWS //dumps successfully generated polyhedrons
#ifdef _DEBUG
void DumpPolyhedronToGLView( const CPolyhedron *pPolyhedron, const char *pFilename, const VMatrix *pTransform );
void DumpPlaneToGlView( const float *pPlane, float fGrayScale, const char *pszFileName, const VMatrix *pTransform );
void DumpLineToGLView( const Vector &vPoint1, const Vector &vColor1, const Vector &vPoint2, const Vector &vColor2, float fThickness, FILE *pFile );
void DumpAABBToGLView( const Vector &vCenter, const Vector &vExtents, const Vector &vColor, FILE *pFile );
#if defined( ENABLE_DEBUG_POLYHEDRON_DUMPS ) && defined( WIN32 )
#include "winlite.h"
#endif
static VMatrix s_matIdentity( 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f );
#endif
#if defined( DEBUG_DUMP_POLYHEDRONS_TO_NUMBERED_GLVIEWS )
static int g_iPolyhedronDumpCounter = 0;
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#if defined( _DEBUG ) && defined( ENABLE_DEBUG_POLYHEDRON_DUMPS )
void CreateDumpDirectory( const char *szDirectoryName )
{
#if defined( WIN32 )
CreateDirectory( szDirectoryName, NULL );
#else
Assert( false ); //TODO: create directories in linux
#endif
}
#endif
void CPolyhedron_AllocByNew::Release( void )
{
delete this;
}
CPolyhedron_AllocByNew *CPolyhedron_AllocByNew::Allocate( unsigned short iVertices, unsigned short iLines, unsigned short iIndices, unsigned short iPolygons ) //creates the polyhedron along with enough memory to hold all it's data in a single allocation
{
void *pMemory = new unsigned char [ sizeof( CPolyhedron_AllocByNew ) +
(iVertices * sizeof(Vector)) +
(iLines * sizeof(Polyhedron_IndexedLine_t)) +
(iIndices * sizeof( Polyhedron_IndexedLineReference_t )) +
(iPolygons * sizeof( Polyhedron_IndexedPolygon_t ))];
#include "tier0/memdbgoff.h" //the following placement new doesn't compile with memory debugging
CPolyhedron_AllocByNew *pAllocated = new ( pMemory ) CPolyhedron_AllocByNew;
#include "tier0/memdbgon.h"
pAllocated->iVertexCount = iVertices;
pAllocated->iLineCount = iLines;
pAllocated->iIndexCount = iIndices;
pAllocated->iPolygonCount = iPolygons;
pAllocated->pVertices = (Vector *)(pAllocated + 1); //start vertex memory at the end of the class
pAllocated->pLines = (Polyhedron_IndexedLine_t *)(pAllocated->pVertices + iVertices);
pAllocated->pIndices = (Polyhedron_IndexedLineReference_t *)(pAllocated->pLines + iLines);
pAllocated->pPolygons = (Polyhedron_IndexedPolygon_t *)(pAllocated->pIndices + iIndices);
return pAllocated;
}
class CPolyhedron_TempMemory : public CPolyhedron
{
public:
#ifdef DBGFLAG_ASSERT
int iReferenceCount;
#endif
virtual void Release( void )
{
#ifdef DBGFLAG_ASSERT
--iReferenceCount;
#endif
}
CPolyhedron_TempMemory( void )
#ifdef DBGFLAG_ASSERT
: iReferenceCount( 0 )
#endif
{ };
};
static CUtlVector<unsigned char> s_TempMemoryPolyhedron_Buffer;
static CPolyhedron_TempMemory s_TempMemoryPolyhedron;
CPolyhedron *GetTempPolyhedron( unsigned short iVertices, unsigned short iLines, unsigned short iIndices, unsigned short iPolygons ) //grab the temporary polyhedron. Avoids new/delete for quick work. Can only be in use by one chunk of code at a time
{
AssertMsg( s_TempMemoryPolyhedron.iReferenceCount == 0, "Temporary polyhedron memory being rewritten before released" );
#ifdef DBGFLAG_ASSERT
++s_TempMemoryPolyhedron.iReferenceCount;
#endif
s_TempMemoryPolyhedron_Buffer.SetCount( (sizeof( Vector ) * iVertices) +
(sizeof( Polyhedron_IndexedLine_t ) * iLines) +
(sizeof( Polyhedron_IndexedLineReference_t ) * iIndices) +
(sizeof( Polyhedron_IndexedPolygon_t ) * iPolygons) );
s_TempMemoryPolyhedron.iVertexCount = iVertices;
s_TempMemoryPolyhedron.iLineCount = iLines;
s_TempMemoryPolyhedron.iIndexCount = iIndices;
s_TempMemoryPolyhedron.iPolygonCount = iPolygons;
s_TempMemoryPolyhedron.pVertices = (Vector *)s_TempMemoryPolyhedron_Buffer.Base();
s_TempMemoryPolyhedron.pLines = (Polyhedron_IndexedLine_t *)(&s_TempMemoryPolyhedron.pVertices[s_TempMemoryPolyhedron.iVertexCount]);
s_TempMemoryPolyhedron.pIndices = (Polyhedron_IndexedLineReference_t *)(&s_TempMemoryPolyhedron.pLines[s_TempMemoryPolyhedron.iLineCount]);
s_TempMemoryPolyhedron.pPolygons = (Polyhedron_IndexedPolygon_t *)(&s_TempMemoryPolyhedron.pIndices[s_TempMemoryPolyhedron.iIndexCount]);
return &s_TempMemoryPolyhedron;
}
Vector CPolyhedron::Center( void )
{
if( iVertexCount == 0 )
return vec3_origin;
Vector vAABBMin, vAABBMax;
vAABBMin = vAABBMax = pVertices[0];
for( int i = 1; i != iVertexCount; ++i )
{
Vector &vPoint = pVertices[i];
if( vPoint.x < vAABBMin.x )
vAABBMin.x = vPoint.x;
if( vPoint.y < vAABBMin.y )
vAABBMin.y = vPoint.y;
if( vPoint.z < vAABBMin.z )
vAABBMin.z = vPoint.z;
if( vPoint.x > vAABBMax.x )
vAABBMax.x = vPoint.x;
if( vPoint.y > vAABBMax.y )
vAABBMax.y = vPoint.y;
if( vPoint.z > vAABBMax.z )
vAABBMax.z = vPoint.z;
}
return ((vAABBMin + vAABBMax) * 0.5f);
}
enum PolyhedronPointPlanarity
{
POINT_DEAD,
POINT_ONPLANE,
POINT_ALIVE
};
struct GeneratePolyhedronFromPlanes_Point
{
Vector ptPosition;
GeneratePolyhedronFromPlanes_LineLL *pConnectedLines; //keep these in a clockwise order, circular linking
float fPlaneDist; //used in plane cutting
PolyhedronPointPlanarity planarity;
int iSaveIndices;
};
struct GeneratePolyhedronFromPlanes_Line
{
GeneratePolyhedronFromPlanes_Point *pPoints[2]; //the 2 connecting points in no particular order
GeneratePolyhedronFromPlanes_Polygon *pPolygons[2]; //viewing from the outside with the point connections going up, 0 is the left polygon, 1 is the right
int iSaveIndices;
bool bAlive; //connected to at least one living point
bool bCut; //connected to at least one dead point
GeneratePolyhedronFromPlanes_LineLL *pPointLineLinks[2]; //rather than going into a point and searching for its link to this line, lets just cache it to eliminate searching
GeneratePolyhedronFromPlanes_LineLL *pPolygonLineLinks[2]; //rather than going into a polygon and searching for its link to this line, lets just cache it to eliminate searching
#ifdef POLYHEDRON_EXTENSIVE_DEBUGGING
int iDebugFlags;
#endif
};
struct GeneratePolyhedronFromPlanes_LineLL
{
GeneratePolyhedronFromPlanes_Line *pLine;
int iReferenceIndex; //whatever is referencing the line should know which side of the line it's on (points and polygons), for polygons, it's which point to follow to continue going clockwise, which makes polygon 0 the one on the left side of an upward facing line vector, for points, it's the OTHER point's index
GeneratePolyhedronFromPlanes_LineLL *pPrev;
GeneratePolyhedronFromPlanes_LineLL *pNext;
};
struct GeneratePolyhedronFromPlanes_Polygon
{
Vector vSurfaceNormal;
GeneratePolyhedronFromPlanes_LineLL *pLines; //keep these in a clockwise order, circular linking
bool bMissingASide;
};
struct GeneratePolyhedronFromPlanes_UnorderedPolygonLL //an unordered collection of polygons
{
GeneratePolyhedronFromPlanes_Polygon *pPolygon;
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pNext;
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pPrev;
};
struct GeneratePolyhedronFromPlanes_UnorderedLineLL //an unordered collection of lines
{
GeneratePolyhedronFromPlanes_Line *pLine;
GeneratePolyhedronFromPlanes_UnorderedLineLL *pNext;
GeneratePolyhedronFromPlanes_UnorderedLineLL *pPrev;
};
struct GeneratePolyhedronFromPlanes_UnorderedPointLL //an unordered collection of points
{
GeneratePolyhedronFromPlanes_Point *pPoint;
GeneratePolyhedronFromPlanes_UnorderedPointLL *pNext;
GeneratePolyhedronFromPlanes_UnorderedPointLL *pPrev;
};
CPolyhedron *ClipPolyhedron( const CPolyhedron *pExistingPolyhedron, const float *pOutwardFacingPlanes, int iPlaneCount, float fOnPlaneEpsilon, bool bUseTemporaryMemory )
{
if( pExistingPolyhedron == NULL )
return NULL;
AssertMsg( (pExistingPolyhedron->iVertexCount >= 3) && (pExistingPolyhedron->iPolygonCount >= 2), "Polyhedron doesn't meet absolute minimum spec" );
float *pUsefulPlanes = (float *)stackalloc( sizeof( float ) * 4 * iPlaneCount );
int iUsefulPlaneCount = 0;
Vector *pExistingVertices = pExistingPolyhedron->pVertices;
//A large part of clipping will either eliminate the polyhedron entirely, or clip nothing at all, so lets just check for those first and throw away useless planes
{
int iLiveCount = 0;
int iDeadCount = 0;
const float fNegativeOnPlaneEpsilon = -fOnPlaneEpsilon;
for( int i = 0; i != iPlaneCount; ++i )
{
Vector vNormal = *((Vector *)&pOutwardFacingPlanes[(i * 4) + 0]);
float fPlaneDist = pOutwardFacingPlanes[(i * 4) + 3];
for( int j = 0; j != pExistingPolyhedron->iVertexCount; ++j )
{
float fPointDist = vNormal.Dot( pExistingVertices[j] ) - fPlaneDist;
if( fPointDist <= fNegativeOnPlaneEpsilon )
++iLiveCount;
else if( fPointDist > fOnPlaneEpsilon )
++iDeadCount;
}
if( iLiveCount == 0 )
{
//all points are dead or on the plane, so the polyhedron is dead
return NULL;
}
if( iDeadCount != 0 )
{
//at least one point died, this plane yields useful results
pUsefulPlanes[(iUsefulPlaneCount * 4) + 0] = vNormal.x;
pUsefulPlanes[(iUsefulPlaneCount * 4) + 1] = vNormal.y;
pUsefulPlanes[(iUsefulPlaneCount * 4) + 2] = vNormal.z;
pUsefulPlanes[(iUsefulPlaneCount * 4) + 3] = fPlaneDist;
++iUsefulPlaneCount;
}
}
}
if( iUsefulPlaneCount == 0 )
{
//testing shows that the polyhedron won't even be cut, clone the existing polyhedron and return that
CPolyhedron *pReturn;
if( bUseTemporaryMemory )
{
pReturn = GetTempPolyhedron( pExistingPolyhedron->iVertexCount,
pExistingPolyhedron->iLineCount,
pExistingPolyhedron->iIndexCount,
pExistingPolyhedron->iPolygonCount );
}
else
{
pReturn = CPolyhedron_AllocByNew::Allocate( pExistingPolyhedron->iVertexCount,
pExistingPolyhedron->iLineCount,
pExistingPolyhedron->iIndexCount,
pExistingPolyhedron->iPolygonCount );
}
memcpy( pReturn->pVertices, pExistingPolyhedron->pVertices, sizeof( Vector ) * pReturn->iVertexCount );
memcpy( pReturn->pLines, pExistingPolyhedron->pLines, sizeof( Polyhedron_IndexedLine_t ) * pReturn->iLineCount );
memcpy( pReturn->pIndices, pExistingPolyhedron->pIndices, sizeof( Polyhedron_IndexedLineReference_t ) * pReturn->iIndexCount );
memcpy( pReturn->pPolygons, pExistingPolyhedron->pPolygons, sizeof( Polyhedron_IndexedPolygon_t ) * pReturn->iPolygonCount );
return pReturn;
}
//convert the polyhedron to linked geometry
GeneratePolyhedronFromPlanes_Point *pStartPoints = (GeneratePolyhedronFromPlanes_Point *)stackalloc( pExistingPolyhedron->iVertexCount * sizeof( GeneratePolyhedronFromPlanes_Point ) );
GeneratePolyhedronFromPlanes_Line *pStartLines = (GeneratePolyhedronFromPlanes_Line *)stackalloc( pExistingPolyhedron->iLineCount * sizeof( GeneratePolyhedronFromPlanes_Line ) );
GeneratePolyhedronFromPlanes_Polygon *pStartPolygons = (GeneratePolyhedronFromPlanes_Polygon *)stackalloc( pExistingPolyhedron->iPolygonCount * sizeof( GeneratePolyhedronFromPlanes_Polygon ) );
GeneratePolyhedronFromPlanes_LineLL *pStartLineLinks = (GeneratePolyhedronFromPlanes_LineLL *)stackalloc( pExistingPolyhedron->iLineCount * 4 * sizeof( GeneratePolyhedronFromPlanes_LineLL ) );
int iCurrentLineLinkIndex = 0;
//setup points
for( int i = 0; i != pExistingPolyhedron->iVertexCount; ++i )
{
pStartPoints[i].ptPosition = pExistingPolyhedron->pVertices[i];
pStartPoints[i].pConnectedLines = NULL; //we won't be circular linking until later
}
//setup lines and interlink to points (line links are not yet circularly linked, and are unordered)
for( int i = 0; i != pExistingPolyhedron->iLineCount; ++i )
{
for( int j = 0; j != 2; ++j )
{
pStartLines[i].pPoints[j] = &pStartPoints[pExistingPolyhedron->pLines[i].iPointIndices[j]];
GeneratePolyhedronFromPlanes_LineLL *pLineLink = &pStartLineLinks[iCurrentLineLinkIndex++];
pStartLines[i].pPointLineLinks[j] = pLineLink;
pLineLink->pLine = &pStartLines[i];
pLineLink->iReferenceIndex = 1 - j;
//pLineLink->pPrev = NULL;
pLineLink->pNext = pStartLines[i].pPoints[j]->pConnectedLines;
pStartLines[i].pPoints[j]->pConnectedLines = pLineLink;
}
}
//setup polygons
for( int i = 0; i != pExistingPolyhedron->iPolygonCount; ++i )
{
pStartPolygons[i].vSurfaceNormal = pExistingPolyhedron->pPolygons[i].polyNormal;
Polyhedron_IndexedLineReference_t *pOffsetPolyhedronLines = &pExistingPolyhedron->pIndices[pExistingPolyhedron->pPolygons[i].iFirstIndex];
GeneratePolyhedronFromPlanes_LineLL *pFirstLink = &pStartLineLinks[iCurrentLineLinkIndex];
pStartPolygons[i].pLines = pFirstLink; //technically going to link to itself on first pass, then get linked properly immediately afterward
for( int j = 0; j != pExistingPolyhedron->pPolygons[i].iIndexCount; ++j )
{
GeneratePolyhedronFromPlanes_LineLL *pLineLink = &pStartLineLinks[iCurrentLineLinkIndex++];
pLineLink->pLine = &pStartLines[pOffsetPolyhedronLines[j].iLineIndex];
pLineLink->iReferenceIndex = pOffsetPolyhedronLines[j].iEndPointIndex;
pLineLink->pLine->pPolygons[pLineLink->iReferenceIndex] = &pStartPolygons[i];
pLineLink->pLine->pPolygonLineLinks[pLineLink->iReferenceIndex] = pLineLink;
pLineLink->pPrev = pStartPolygons[i].pLines;
pStartPolygons[i].pLines->pNext = pLineLink;
pStartPolygons[i].pLines = pLineLink;
}
pFirstLink->pPrev = pStartPolygons[i].pLines;
pStartPolygons[i].pLines->pNext = pFirstLink;
}
Assert( iCurrentLineLinkIndex == (pExistingPolyhedron->iLineCount * 4) );
//go back to point line links so we can circularly link them as well as order them now that every point has all its line links
for( int i = 0; i != pExistingPolyhedron->iVertexCount; ++i )
{
//interlink the points
{
GeneratePolyhedronFromPlanes_LineLL *pLastVisitedLink = pStartPoints[i].pConnectedLines;
GeneratePolyhedronFromPlanes_LineLL *pCurrentLink = pLastVisitedLink;
do
{
pCurrentLink->pPrev = pLastVisitedLink;
pLastVisitedLink = pCurrentLink;
pCurrentLink = pCurrentLink->pNext;
} while( pCurrentLink );
//circular link
pLastVisitedLink->pNext = pStartPoints[i].pConnectedLines;
pStartPoints[i].pConnectedLines->pPrev = pLastVisitedLink;
}
//fix ordering
GeneratePolyhedronFromPlanes_LineLL *pFirstLink = pStartPoints[i].pConnectedLines;
GeneratePolyhedronFromPlanes_LineLL *pWorkLink = pFirstLink;
GeneratePolyhedronFromPlanes_LineLL *pSearchLink;
GeneratePolyhedronFromPlanes_Polygon *pLookingForPolygon;
Assert( pFirstLink->pNext != pFirstLink );
do
{
pLookingForPolygon = pWorkLink->pLine->pPolygons[1 - pWorkLink->iReferenceIndex]; //grab pointer to left polygon
pSearchLink = pWorkLink->pPrev;
while( pSearchLink->pLine->pPolygons[pSearchLink->iReferenceIndex] != pLookingForPolygon )
pSearchLink = pSearchLink->pPrev;
Assert( pSearchLink->pLine->pPolygons[pSearchLink->iReferenceIndex] == pWorkLink->pLine->pPolygons[1 - pWorkLink->iReferenceIndex] );
//pluck the search link from wherever it is
pSearchLink->pPrev->pNext = pSearchLink->pNext;
pSearchLink->pNext->pPrev = pSearchLink->pPrev;
//insert the search link just before the work link
pSearchLink->pPrev = pWorkLink->pPrev;
pSearchLink->pNext = pWorkLink;
pSearchLink->pPrev->pNext = pSearchLink;
pWorkLink->pPrev = pSearchLink;
pWorkLink = pSearchLink;
} while( pWorkLink != pFirstLink );
}
GeneratePolyhedronFromPlanes_UnorderedPointLL *pPoints = (GeneratePolyhedronFromPlanes_UnorderedPointLL *)stackalloc( pExistingPolyhedron->iVertexCount * sizeof( GeneratePolyhedronFromPlanes_UnorderedPointLL ) );
GeneratePolyhedronFromPlanes_UnorderedLineLL *pLines = (GeneratePolyhedronFromPlanes_UnorderedLineLL *)stackalloc( pExistingPolyhedron->iLineCount * sizeof( GeneratePolyhedronFromPlanes_UnorderedLineLL ) );
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pPolygons = (GeneratePolyhedronFromPlanes_UnorderedPolygonLL *)stackalloc( pExistingPolyhedron->iPolygonCount * sizeof( GeneratePolyhedronFromPlanes_UnorderedPolygonLL ) );
//setup point collection
{
pPoints[0].pPrev = NULL;
pPoints[0].pPoint = &pStartPoints[0];
pPoints[0].pNext = &pPoints[1];
int iLastPoint = pExistingPolyhedron->iVertexCount - 1;
for( int i = 1; i != iLastPoint; ++i )
{
pPoints[i].pPrev = &pPoints[i - 1];
pPoints[i].pPoint = &pStartPoints[i];
pPoints[i].pNext = &pPoints[i + 1];
}
pPoints[iLastPoint].pPrev = &pPoints[iLastPoint - 1];
pPoints[iLastPoint].pPoint = &pStartPoints[iLastPoint];
pPoints[iLastPoint].pNext = NULL;
}
//setup line collection
{
pLines[0].pPrev = NULL;
pLines[0].pLine = &pStartLines[0];
pLines[0].pNext = &pLines[1];
int iLastLine = pExistingPolyhedron->iLineCount - 1;
for( int i = 1; i != iLastLine; ++i )
{
pLines[i].pPrev = &pLines[i - 1];
pLines[i].pLine = &pStartLines[i];
pLines[i].pNext = &pLines[i + 1];
}
pLines[iLastLine].pPrev = &pLines[iLastLine - 1];
pLines[iLastLine].pLine = &pStartLines[iLastLine];
pLines[iLastLine].pNext = NULL;
}
//setup polygon collection
{
pPolygons[0].pPrev = NULL;
pPolygons[0].pPolygon = &pStartPolygons[0];
pPolygons[0].pNext = &pPolygons[1];
int iLastPolygon = pExistingPolyhedron->iPolygonCount - 1;
for( int i = 1; i != iLastPolygon; ++i )
{
pPolygons[i].pPrev = &pPolygons[i - 1];
pPolygons[i].pPolygon = &pStartPolygons[i];
pPolygons[i].pNext = &pPolygons[i + 1];
}
pPolygons[iLastPolygon].pPrev = &pPolygons[iLastPolygon - 1];
pPolygons[iLastPolygon].pPolygon = &pStartPolygons[iLastPolygon];
pPolygons[iLastPolygon].pNext = NULL;
}
return ClipLinkedGeometry( pPolygons, pLines, pPoints, pUsefulPlanes, iUsefulPlaneCount, fOnPlaneEpsilon, bUseTemporaryMemory );
}
Vector FindPointInPlanes( const float *pPlanes, int planeCount )
{
Vector point = vec3_origin;
for ( int i = 0; i < planeCount; i++ )
{
float fD = DotProduct( *(Vector *)&pPlanes[i*4], point ) - pPlanes[i*4 + 3];
if ( fD < 0 )
{
point -= fD * (*(Vector *)&pPlanes[i*4]);
}
}
return point;
}
bool FindConvexShapeLooseAABB( const float *pInwardFacingPlanes, int iPlaneCount, Vector *pAABBMins, Vector *pAABBMaxs ) //bounding box of the convex shape (subject to floating point error)
{
//returns false if the AABB hasn't been set
if( pAABBMins == NULL && pAABBMaxs == NULL ) //no use in actually finding out what it is
return false;
struct FindConvexShapeAABB_Polygon_t
{
float *verts;
int iVertCount;
};
float *pMovedPlanes = (float *)stackalloc( iPlaneCount * 4 * sizeof( float ) );
//Vector vPointInPlanes = FindPointInPlanes( pInwardFacingPlanes, iPlaneCount );
for( int i = 0; i != iPlaneCount; ++i )
{
pMovedPlanes[(i * 4) + 0] = pInwardFacingPlanes[(i * 4) + 0];
pMovedPlanes[(i * 4) + 1] = pInwardFacingPlanes[(i * 4) + 1];
pMovedPlanes[(i * 4) + 2] = pInwardFacingPlanes[(i * 4) + 2];
pMovedPlanes[(i * 4) + 3] = pInwardFacingPlanes[(i * 4) + 3] - 100.0f; //move planes out a lot to kill some imprecision problems
}
//vAABBMins = vAABBMaxs = FindPointInPlanes( pPlanes, iPlaneCount );
float *vertsIn = NULL; //we'll be allocating a new buffer for this with each new polygon, and moving it off to the polygon array
float *vertsOut = (float *)stackalloc( (iPlaneCount + 4) * (sizeof( float ) * 3) ); //each plane will initially have 4 points in its polygon representation, and each plane clip has the possibility to add 1 point to the polygon
float *vertsSwap;
FindConvexShapeAABB_Polygon_t *pPolygons = (FindConvexShapeAABB_Polygon_t *)stackalloc( iPlaneCount * sizeof( FindConvexShapeAABB_Polygon_t ) );
int iPolyCount = 0;
for ( int i = 0; i < iPlaneCount; i++ )
{
Vector *pPlaneNormal = (Vector *)&pInwardFacingPlanes[i*4];
float fPlaneDist = pInwardFacingPlanes[(i*4) + 3];
if( vertsIn == NULL )
vertsIn = (float *)stackalloc( (iPlaneCount + 4) * (sizeof( float ) * 3) );
// Build a big-ass poly in this plane
int vertCount = PolyFromPlane( (Vector *)vertsIn, *pPlaneNormal, fPlaneDist, 100000.0f );
//chop it by every other plane
for( int j = 0; j < iPlaneCount; j++ )
{
// don't clip planes with themselves
if ( i == j )
continue;
// Chop the polygon against this plane
vertCount = ClipPolyToPlane( (Vector *)vertsIn, vertCount, (Vector *)vertsOut, *(Vector *)&pMovedPlanes[j*4], pMovedPlanes[(j*4) + 3], 0.0f );
//swap the input and output arrays
vertsSwap = vertsIn; vertsIn = vertsOut; vertsOut = vertsSwap;
// Less than a poly left, something's wrong, don't bother with this polygon
if ( vertCount < 3 )
break;
}
if ( vertCount < 3 )
continue; //not enough to work with
pPolygons[iPolyCount].iVertCount = vertCount;
pPolygons[iPolyCount].verts = vertsIn;
vertsIn = NULL;
++iPolyCount;
}
if( iPolyCount == 0 )
return false;
//initialize the AABB to the first point available
Vector vAABBMins, vAABBMaxs;
vAABBMins = vAABBMaxs = ((Vector *)pPolygons[0].verts)[0];
if( pAABBMins && pAABBMaxs ) //they want the full box
{
for( int i = 0; i != iPolyCount; ++i )
{
Vector *PolyVerts = (Vector *)pPolygons[i].verts;
for( int j = 0; j != pPolygons[i].iVertCount; ++j )
{
if( PolyVerts[j].x < vAABBMins.x )
vAABBMins.x = PolyVerts[j].x;
if( PolyVerts[j].y < vAABBMins.y )
vAABBMins.y = PolyVerts[j].y;
if( PolyVerts[j].z < vAABBMins.z )
vAABBMins.z = PolyVerts[j].z;
if( PolyVerts[j].x > vAABBMaxs.x )
vAABBMaxs.x = PolyVerts[j].x;
if( PolyVerts[j].y > vAABBMaxs.y )
vAABBMaxs.y = PolyVerts[j].y;
if( PolyVerts[j].z > vAABBMaxs.z )
vAABBMaxs.z = PolyVerts[j].z;
}
}
*pAABBMins = vAABBMins;
*pAABBMaxs = vAABBMaxs;
}
else if( pAABBMins ) //they only want the min
{
for( int i = 0; i != iPolyCount; ++i )
{
Vector *PolyVerts = (Vector *)pPolygons[i].verts;
for( int j = 0; j != pPolygons[i].iVertCount; ++j )
{
if( PolyVerts[j].x < vAABBMins.x )
vAABBMins.x = PolyVerts[j].x;
if( PolyVerts[j].y < vAABBMins.y )
vAABBMins.y = PolyVerts[j].y;
if( PolyVerts[j].z < vAABBMins.z )
vAABBMins.z = PolyVerts[j].z;
}
}
*pAABBMins = vAABBMins;
}
else //they only want the max
{
for( int i = 0; i != iPolyCount; ++i )
{
Vector *PolyVerts = (Vector *)pPolygons[i].verts;
for( int j = 0; j != pPolygons[i].iVertCount; ++j )
{
if( PolyVerts[j].x > vAABBMaxs.x )
vAABBMaxs.x = PolyVerts[j].x;
if( PolyVerts[j].y > vAABBMaxs.y )
vAABBMaxs.y = PolyVerts[j].y;
if( PolyVerts[j].z > vAABBMaxs.z )
vAABBMaxs.z = PolyVerts[j].z;
}
}
*pAABBMaxs = vAABBMaxs;
}
return true;
}
CPolyhedron *ConvertLinkedGeometryToPolyhedron( GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pPolygons, GeneratePolyhedronFromPlanes_UnorderedLineLL *pLines, GeneratePolyhedronFromPlanes_UnorderedPointLL *pPoints, bool bUseTemporaryMemory )
{
Assert( (pPolygons != NULL) && (pLines != NULL) && (pPoints != NULL) );
unsigned int iPolyCount = 0, iLineCount = 0, iPointCount = 0, iIndexCount = 0;
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pActivePolygonWalk = pPolygons;
do
{
++iPolyCount;
GeneratePolyhedronFromPlanes_LineLL *pLineWalk = pActivePolygonWalk->pPolygon->pLines;
GeneratePolyhedronFromPlanes_LineLL *pFirstLine = pLineWalk;
Assert( pLineWalk != NULL );
do
{
++iIndexCount;
pLineWalk = pLineWalk->pNext;
} while( pLineWalk != pFirstLine );
pActivePolygonWalk = pActivePolygonWalk->pNext;
} while( pActivePolygonWalk );
GeneratePolyhedronFromPlanes_UnorderedLineLL *pActiveLineWalk = pLines;
do
{
++iLineCount;
pActiveLineWalk = pActiveLineWalk->pNext;
} while( pActiveLineWalk );
GeneratePolyhedronFromPlanes_UnorderedPointLL *pActivePointWalk = pPoints;
do
{
++iPointCount;
pActivePointWalk = pActivePointWalk->pNext;
} while( pActivePointWalk );
CPolyhedron *pReturn;
if( bUseTemporaryMemory )
{
pReturn = GetTempPolyhedron( iPointCount, iLineCount, iIndexCount, iPolyCount );
}
else
{
pReturn = CPolyhedron_AllocByNew::Allocate( iPointCount, iLineCount, iIndexCount, iPolyCount );
}
Vector *pVertexArray = pReturn->pVertices;
Polyhedron_IndexedLine_t *pLineArray = pReturn->pLines;
Polyhedron_IndexedLineReference_t *pIndexArray = pReturn->pIndices;
Polyhedron_IndexedPolygon_t *pPolyArray = pReturn->pPolygons;
//copy points
pActivePointWalk = pPoints;
for( unsigned int i = 0; i != iPointCount; ++i )
{
pVertexArray[i] = pActivePointWalk->pPoint->ptPosition;
pActivePointWalk->pPoint->iSaveIndices = i; //storing array indices
pActivePointWalk = pActivePointWalk->pNext;
}
//copy lines
pActiveLineWalk = pLines;
for( unsigned int i = 0; i != iLineCount; ++i )
{
pLineArray[i].iPointIndices[0] = (unsigned short)pActiveLineWalk->pLine->pPoints[0]->iSaveIndices;
pLineArray[i].iPointIndices[1] = (unsigned short)pActiveLineWalk->pLine->pPoints[1]->iSaveIndices;
pActiveLineWalk->pLine->iSaveIndices = i; //storing array indices
pActiveLineWalk = pActiveLineWalk->pNext;
}
//copy polygons and indices at the same time
pActivePolygonWalk = pPolygons;
iIndexCount = 0;
for( unsigned int i = 0; i != iPolyCount; ++i )
{
pPolyArray[i].polyNormal = pActivePolygonWalk->pPolygon->vSurfaceNormal;
pPolyArray[i].iFirstIndex = iIndexCount;
GeneratePolyhedronFromPlanes_LineLL *pLineWalk = pActivePolygonWalk->pPolygon->pLines;
GeneratePolyhedronFromPlanes_LineLL *pFirstLine = pLineWalk;
do
{
//pIndexArray[iIndexCount] = pLineWalk->pLine->pPoints[pLineWalk->iReferenceIndex]->iWorkData; //startpoint of each line, iWorkData is the index of the vertex
pIndexArray[iIndexCount].iLineIndex = pLineWalk->pLine->iSaveIndices;
pIndexArray[iIndexCount].iEndPointIndex = pLineWalk->iReferenceIndex;
++iIndexCount;
pLineWalk = pLineWalk->pNext;
} while( pLineWalk != pFirstLine );
pPolyArray[i].iIndexCount = iIndexCount - pPolyArray[i].iFirstIndex;
pActivePolygonWalk = pActivePolygonWalk->pNext;
}
#if defined( _DEBUG ) && defined( ENABLE_DEBUG_POLYHEDRON_DUMPS ) && defined( DEBUG_DUMP_POLYHEDRONS_TO_NUMBERED_GLVIEWS )
char szCollisionFile[128];
CreateDumpDirectory( "PolyhedronDumps" );
Q_snprintf( szCollisionFile, 128, "PolyhedronDumps/NewStyle_PolyhedronDump%i.txt", g_iPolyhedronDumpCounter );
++g_iPolyhedronDumpCounter;
remove( szCollisionFile );
DumpPolyhedronToGLView( pReturn, szCollisionFile, &s_matIdentity );
DumpPolyhedronToGLView( pReturn, "PolyhedronDumps/NewStyle_PolyhedronDump_All-Appended.txt", &s_matIdentity );
#endif
return pReturn;
}
#ifdef _DEBUG
void DumpPointListToGLView( GeneratePolyhedronFromPlanes_UnorderedPointLL *pHead, PolyhedronPointPlanarity planarity, const Vector &vColor, const char *szDumpFile, const VMatrix *pTransform )
{
#ifdef ENABLE_DEBUG_POLYHEDRON_DUMPS
if( pTransform == NULL )
pTransform = &s_matIdentity;
FILE *pFile = fopen( szDumpFile, "ab" );
while( pHead )
{
if( pHead->pPoint->planarity == planarity )
{
const Vector vPointExtents( 0.5f, 0.5f, 0.01f );
DumpAABBToGLView( (*pTransform) * pHead->pPoint->ptPosition, vPointExtents, vColor, pFile );
}
pHead = pHead->pNext;
}
fclose( pFile );
#endif
}
const char * DumpPolyhedronCutHistory( const CUtlVector<CPolyhedron *> &DumpedHistory, const CUtlVector<const float *> &CutHistory, const VMatrix *pTransform )
{
#ifdef ENABLE_DEBUG_POLYHEDRON_DUMPS
if( pTransform == NULL )
pTransform = &s_matIdentity;
static char szDumpFile[100] = "FailedPolyhedronCut_Error.txt"; //most recent filename returned for further dumping
for( int i = 0; i != DumpedHistory.Count(); ++i )
{
if( DumpedHistory[i] != NULL )
{
Q_snprintf( szDumpFile, 100, "FailedPolyhedronCut_%d.txt", i );
DumpPolyhedronToGLView( DumpedHistory[i], szDumpFile, pTransform );
DumpPlaneToGlView( CutHistory[i], 1.0f, szDumpFile, pTransform );
}
}
return szDumpFile;
#else
return NULL;
#endif
}
#ifdef ENABLE_DEBUG_POLYHEDRON_DUMPS
#define AssertMsg_DumpPolyhedron(condition, message)\
if( (condition) == false )\
{\
VMatrix matTransform;\
matTransform.Identity();\
matTransform[0][0] = matTransform[1][1] = matTransform[2][2] = 25.0f;\
matTransform.SetTranslation( -DebugCutHistory.Tail()->Center() * 25.0f );\
const char *szLastDumpFile = DumpPolyhedronCutHistory( DebugCutHistory, PlaneCutHistory, &matTransform );\
DumpPointListToGLView( pAllPoints, POINT_ALIVE, Vector( 0.9f, 0.9f, 0.9f ), szLastDumpFile, &matTransform );\
DumpPointListToGLView( pAllPoints, POINT_ONPLANE, Vector( 0.5f, 0.5f, 0.5f ), szLastDumpFile, &matTransform );\
DumpPointListToGLView( pDeadPointCollection, POINT_DEAD, Vector( 0.1f, 0.1f, 0.1f ), szLastDumpFile, &matTransform );\
if( pStartPoint )\
{\
FILE *pFileDumpRepairProgress = fopen( szLastDumpFile, "ab" );\
DumpAABBToGLView( matTransform * pStartPoint->ptPosition, Vector( 2.0f, 0.05f, 0.05f ), Vector( 0.0f, 1.0f, 0.0f ), pFileDumpRepairProgress );\
DumpAABBToGLView( matTransform * pWorkPoint->ptPosition, Vector( 2.0f, 0.05f, 0.05f ), Vector( 1.0f, 0.0f, 0.0f ), pFileDumpRepairProgress );\
fclose( pFileDumpRepairProgress );\
}\
AssertMsg( condition, message );\
}
#else
#define AssertMsg_DumpPolyhedron(condition, message) AssertMsg( condition, message )
#endif
#define Assert_DumpPolyhedron(condition) AssertMsg_DumpPolyhedron( condition, #condition )
#else
#define AssertMsg_DumpPolyhedron(condition, message) NULL;
#define Assert_DumpPolyhedron(condition) NULL;
#endif
CPolyhedron *ClipLinkedGeometry( GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pAllPolygons, GeneratePolyhedronFromPlanes_UnorderedLineLL *pAllLines, GeneratePolyhedronFromPlanes_UnorderedPointLL *pAllPoints, const float *pOutwardFacingPlanes, int iPlaneCount, float fOnPlaneEpsilon, bool bUseTemporaryMemory )
{
const float fNegativeOnPlaneEpsilon = -fOnPlaneEpsilon;
#ifdef _DEBUG
CUtlVector<CPolyhedron *> DebugCutHistory;
CUtlVector<const float *> PlaneCutHistory;
GeneratePolyhedronFromPlanes_Point *pStartPoint = NULL;
GeneratePolyhedronFromPlanes_Point *pWorkPoint = NULL;
static int iPolyhedronClipCount = 0;
++iPolyhedronClipCount;
DebugCutHistory.AddToTail( ConvertLinkedGeometryToPolyhedron( pAllPolygons, pAllLines, pAllPoints, false ) );
#endif
//clear out polygon work variables
{
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pActivePolygonWalk = pAllPolygons;
do
{
pActivePolygonWalk->pPolygon->bMissingASide = false;
pActivePolygonWalk = pActivePolygonWalk->pNext;
} while( pActivePolygonWalk );
}
//Collections of dead pointers for reallocation, shouldn't be touched until the current loop iteration is done.
GeneratePolyhedronFromPlanes_UnorderedPointLL *pDeadPointCollection = NULL;
GeneratePolyhedronFromPlanes_UnorderedLineLL *pDeadLineCollection = NULL;
GeneratePolyhedronFromPlanes_UnorderedPolygonLL *pDeadPolygonCollection = NULL;
GeneratePolyhedronFromPlanes_LineLL *pDeadLineLinkCollection = NULL;
for( int iCurrentPlane = 0; iCurrentPlane != iPlaneCount; ++iCurrentPlane )
{
//clear out line work variables
{
GeneratePolyhedronFromPlanes_UnorderedLineLL *pActiveLineWalk = pAllLines;
do
{
pActiveLineWalk->pLine->bAlive = false;
pActiveLineWalk->pLine->bCut = false;
pActiveLineWalk = pActiveLineWalk->pNext;
} while( pActiveLineWalk );
}
//TODO: Move these pointers into a reallocation pool
pDeadPointCollection = NULL;
pDeadLineCollection = NULL;
pDeadLineLinkCollection = NULL;
pDeadPolygonCollection = NULL;
Vector vNormal = *((Vector *)&pOutwardFacingPlanes[(iCurrentPlane * 4) + 0]);
/*double vNormalAsDouble[3];
vNormalAsDouble[0] = vNormal.x;
vNormalAsDouble[1] = vNormal.y;
vNormalAsDouble[2] = vNormal.z;*/
float fPlaneDist = pOutwardFacingPlanes[(iCurrentPlane * 4) + 3];
//===================================================================================================
// Step 1: Categorize each point as being either cut, split, or alive
//===================================================================================================
{
bool bAllPointsDead = true;
bool bAllPointsAlive = true;
//find point distances from the plane
GeneratePolyhedronFromPlanes_UnorderedPointLL *pActivePointWalk = pAllPoints;
do
{
GeneratePolyhedronFromPlanes_Point *pPoint = pActivePointWalk->pPoint;
float fPointDist = vNormal.Dot( pPoint->ptPosition ) - fPlaneDist;
if( fPointDist > fOnPlaneEpsilon )
{
pPoint->planarity = POINT_DEAD; //point is dead, bang bang
//mark connected lines as cut
GeneratePolyhedronFromPlanes_LineLL *pLineWalk = pPoint->pConnectedLines;
GeneratePolyhedronFromPlanes_LineLL *pFirstLine = pLineWalk;
do
{
pLineWalk->pLine->bCut = true;
pLineWalk = pLineWalk->pNext;
} while( pLineWalk != pFirstLine );
bAllPointsAlive = false;
}
else if( fPointDist <= fNegativeOnPlaneEpsilon )
{
pPoint->planarity = POINT_ALIVE; //point is in behind plane, not voted off the island....yet
bAllPointsDead = false;
//mark connected lines as alive
GeneratePolyhedronFromPlanes_LineLL *pLineWalk = pPoint->pConnectedLines;
GeneratePolyhedronFromPlanes_LineLL *pFirstLine = pLineWalk;
do
{
pLineWalk->pLine->bAlive = true; //mark the line as alive
pLineWalk = pLineWalk->pNext;
} while( pLineWalk != pFirstLine );
}
else
{
pPoint->planarity = POINT_ONPLANE; //point is on the plane, he's everyone's buddy
//Project on-plane points leaning towards death closer to the plane. This battles floating point precision decay.
// Consider the case of a large on-plane epsilon leaving protrusions over time
/*if( fPointDist < 0.0f )
{
double distAsDouble = fPointDist;
double vPositionAsDouble[3];
vPositionAsDouble[0] = pPoint->ptPosition.x;
vPositionAsDouble[1] = pPoint->ptPosition.y;
vPositionAsDouble[2] = pPoint->ptPosition.z;
pPoint->ptPosition.x = vPositionAsDouble[0] - (distAsDouble * vNormalAsDouble[0]);
pPoint->ptPosition.y = vPositionAsDouble[1] - (distAsDouble * vNormalAsDouble[1]);
pPoint->ptPosition.z = vPositionAsDouble[2] - (distAsDouble * vNormalAsDouble[2]);
#if ( 0 && defined( _DEBUG ) )
float fDebugDist = vNormal.Dot( pPoint->ptPosition ) - fPlaneDist; //just for looking at in watch windows
AssertMsg( fabs( fDebugDist ) < fabs(fPointDist), "Projected point is further from plane than unprojected." );
#endif
fPointDist = vNormal.Dot( pPoint->ptPosition ) - fPlaneDist; //recompute dist (not guaranteed to be 0.0 like we want)
}*/
}
pPoint->fPlaneDist = fPointDist;
pActivePointWalk = pActivePointWalk->pNext;
} while( pActivePointWalk );
if( bAllPointsDead ) //all the points either died or are on the plane, no polyhedron left at all
{
#ifdef _DEBUG