forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfemodelbuilder.cpp
3277 lines (2913 loc) · 102 KB
/
femodelbuilder.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 (c) Valve Corporation. All Rights Reserved. ======================
#include "mathlib/femodelbuilder.h"
#include "tier1/utlhashtable.h"
#include "tier1/utlsortvector.h"
#include "tier1/heapsort.h"
#include "bitvec.h"
#include "tier1/utlpair.h"
#include "modellib/clothhelpers.h"
#include "tier1/utlstringtoken.h"
#include "mathlib/feagglomerator.h"
#include <algorithm>
#include "femodel.inl"
// this function is exclusively for use with invM0/(invM0+invM1) type of formulae
// if one of the particles is almost infinite mass, I don't want to see any creep in its position (mostly for debugging)
inline float SnapWeight01( float w )
{
Assert( w >= 0.0f && w <= 1.0f );
return w < 1e-6f ? 0 : w > 0.99999f ? 1.0f : w;
}
inline float SnapWeight0( float w )
{
Assert( w >= 0.0f && w <= 1.0f );
return w < 1e-12f ? 0 : w ;
}
inline bool Is0To1( float w )
{
return w >= 0 && w <= 1.0f;
}
inline void KeepIn01( float &ref )
{
if ( ref > 1.0f )
{
ref = 1.0f;
}
else if ( !( ref >= 0.0f ) ) // the "!" should catch inf or nan
{
ref = 0.0f;
}
}
void CFeModelBuilder::BuildKelagerBends( )
{
CUtlVector< CUtlVector< uint16 > * > neighbors;
neighbors.SetCount( m_Nodes.Count( ) );
for ( int i = 0; i < m_Nodes.Count( ); ++i )
{
( neighbors[ i ] = new CUtlVector< uint16 > )->EnsureCapacity( 8 );
}
CVarBitVec staticNodes( m_Nodes.Count( ) );
int nMaxValency = 0;
for ( int i = 0; i < m_Elems.Count( ); ++i )
{
BuildElem_t elem = m_Elems[ i ];
uint nElemEdges = elem.nNode[ 3 ] == elem.nNode[ 2 ] ? 3 : 4;
for ( uint j = 0; j < nElemEdges; ++j )
{
uint j1 = ( j + 1 ) % nElemEdges;
uint v0 = elem.nNode[ j ], v1 = elem.nNode[ j1 ];
if ( j < elem.nStaticNodes )
{
staticNodes.Set( v0 );
}
if ( neighbors[ v0 ]->Find( v1 ) < 0 )
{
int v0elem = neighbors[ v0 ]->AddToTail( v1 );
int v1elem = neighbors[ v1 ]->AddToTail( v0 );
nMaxValency = Max( nMaxValency, Max( v0elem, v1elem ) + 1 );
}
}
}
// now we know which vertex is connected to which, and which is static
CVarBitVec paired( nMaxValency );
for ( int nNode = 0; nNode < m_Nodes.Count( ); ++nNode )
{
const CUtlVector< uint16 > &ring = *( neighbors[ nNode ] );
paired.ClearAll( );
Vector v = m_Nodes[ nNode ].transform.m_vPosition;
for ( int b0idx = 0; b0idx < ring.Count( ); ++b0idx )
{
if ( paired[ b0idx ] )
continue;
float flBestCos = -0.87f, flBestH0 = 0;
int nBestB1idx = -1;
int b0 = ring[ b0idx ];
int isB0static = staticNodes[ b0 ];
Vector vB0 = m_Nodes[ b0 ].transform.m_vPosition - v;
for ( int b1idx = 0; b1idx < ring.Count( ); ++b1idx )
{
if ( b1idx == b0idx )
continue;
int b1 = ring[ b1idx ];
if ( isB0static && staticNodes[ b1 ] )
continue; // no need to check static-static
Vector vB1 = m_Nodes[ b1 ].transform.m_vPosition - v;
float flCos = DotProduct( vB1.Normalized( ), vB0.Normalized( ) );
if ( flCos < flBestCos )
{
flBestCos = flCos;
nBestB1idx = b1idx;
flBestH0 = ( vB1 + vB0 ).Length( ) / 3.0f;
}
}
if ( nBestB1idx >= 0 )
{
FeKelagerBend_t kbend;
kbend.flHeight0 = flBestH0;
kbend.m_nNode[ 0 ] = nNode;
kbend.m_nNode[ 1 ] = b0;
kbend.m_nNode[ 2 ] = ring[ nBestB1idx ];
kbend.m_nFlags = 7;
for ( int j = 0; j < 3; ++j )
{
if ( staticNodes[ kbend.m_nNode[ j ] ] )
kbend.m_nFlags ^= 1 << j;
}
Assert( kbend.m_nFlags );
m_KelagerBends.AddToTail( kbend );
paired.Set( nBestB1idx );
}
}
}
neighbors.PurgeAndDeleteElements( );
}
void CFeModelBuilder::BuildAndSortRods( float flCurvatureAngle, bool bTriangulate ) // flCurvatureAngle is additional angle we can bend each side
{
CUtlHashtable< uint32, uint32 > edgeToRod;
// TODO: filter out rods that connect invalid here?
if ( m_bAddStiffnessRods )
{
if ( bTriangulate )
{
for ( int nElem = 0; nElem < m_Elems.Count(); ++nElem )
{
const BuildElem_t &elem = m_Elems[ nElem ];
if ( elem.nNode[ 2 ] != elem.nNode[ 3 ] )
{
uint v0 = elem.nNode[ 1 ], v1 = elem.nNode[ 3 ];
BuildRod( flCurvatureAngle, v0, v1, nElem, nElem, elem.nNode[ 0 ], elem.nNode[ 2 ], edgeToRod );
}
}
}
for ( int nEdge = 0; nEdge < m_FeEdgeDesc.Count(); ++nEdge )
{
FeEdgeDesc_t edge = m_FeEdgeDesc[ nEdge ];
for ( uint nLong = 0; nLong < 2; ++nLong )
{
uint v0 = edge.nSide[ 0 ][ nLong ], v1 = edge.nSide[ 1 ][ nLong ];
BuildRod( flCurvatureAngle, v0, v1, edge.nElem[ 0 ], edge.nElem[ 1 ], edge.nEdge[ 0 ], edge.nEdge[ 1 ], edgeToRod );
}
}
}
for ( int i = 0; i < m_Rods.Count( ); ++i )
{
FeRodConstraint_t &rod = m_Rods[ i ];
if ( m_Nodes[ rod.nNode[ 0 ] ].nRank > m_Nodes[ rod.nNode[ 1 ] ].nRank )
{
rod.InvariantReverse( );
}
}
HeapSort( m_Rods, [&] ( const FeRodConstraint_t &left, const FeRodConstraint_t &right ) {
int nRankLeft = Min( m_Nodes[ left.nNode[ 0 ] ].nRank, m_Nodes[ left.nNode[ 1 ] ].nRank );
int nRankRight = Min( m_Nodes[ right.nNode[ 0 ] ].nRank, m_Nodes[ right.nNode[ 1 ] ].nRank );
if ( nRankLeft == nRankRight )
{
float flLeft = Min( left.flWeight0, 1 - left.flWeight0 );
float flRight = Min( right.flWeight0, 1 - right.flWeight0 );
return flLeft < flRight;
}
else
{
return nRankLeft < nRankRight;
}
} );
}
void CFeModelBuilder::BuildRod( float flCurvatureAngle, uint v0, uint v1, uint nElem0, uint nElem1, uint nEdgeV0, uint nEdgeV1, CUtlHashtable< uint32, uint32 > &edgeToRod )
{
if ( !m_Nodes[ v0 ].bSimulated && m_Nodes[ v1 ].bSimulated )
return; // rods between static nodes don't work
uint nRodV0 = v0 | ( v1 << 16 ), nRodV1 = v1 | ( v0 << 16 );
float invM0 = m_Nodes[ v0 ].invMass, invM1 = m_Nodes[ v1 ].invMass;
if ( invM1 + invM0 < 1e-12f )
return; // doesn't matter, this rod is between two infinitely massive nodes
Vector vEdge = ( m_Nodes[ nEdgeV1 ].transform.m_vPosition - m_Nodes[ nEdgeV0 ].transform.m_vPosition ).NormalizedSafe( Vector( 0, 0, 1 ) );
float flRelaxedRodLength = ( m_Nodes[ v1 ].transform.m_vPosition - m_Nodes[ v0 ].transform.m_vPosition ).Length();
float flSumSlack = m_Elems[ nElem0 ].flSlack + m_Elems[ nElem1 ].flSlack;
// max distance happens when the two polygons are rotated around edge.nEdge[] so that v0, v1 and edge.nEdge[] are in one plane
// then, the angle v0-edge.nEdge[1-nLong]-v1 is at its maximum, and is the sum of angles in each quad's plane
float flMaxDist, flMinDist;
if ( m_bRigidEdgeHinges )
{
flMaxDist = flMinDist = flRelaxedRodLength;
}
else
{
Vector vCenter = m_Nodes[ nEdgeV0 ].transform.m_vPosition;
Vector vSide[ 2 ] = { m_Nodes[ v0 ].transform.m_vPosition - vCenter, m_Nodes[ v1 ].transform.m_vPosition - vCenter };
float flSideProj[ 2 ] = { DotProduct( vSide[ 0 ], vEdge ), DotProduct( vSide[ 1 ], vEdge ) }; // projection of side vectors onto the hinge edge
float flSideProjDelta = flSideProj[ 0 ] - flSideProj[ 1 ];
Vector vSideHeight[ 2 ] = { vSide[ 0 ] - vEdge * flSideProj[ 0 ], vSide[ 1 ] - vEdge * flSideProj[ 1 ] };// orthogonal to the hinge components of the sides
float h[ 2 ] = { vSideHeight[ 0 ].Length( ), vSideHeight[ 1 ].Length( ) };
flMaxDist = sqrtf( Sqr( flSideProjDelta ) + Sqr( h[ 0 ] + h[ 1 ] ) ) + flSumSlack;
// min distance happens when triangles are closed like a book at angle flCurvatureAngle. THe points on triangles are running in circles, so it's a distance between points on 2 circles in 3D
Vector deltaMin( h[ 1 ] * cosf( flCurvatureAngle ) - h[ 0 ], h[1] * sinf( flCurvatureAngle ), flSideProjDelta );
flMinDist = Min( deltaMin.Length() - flSumSlack, flRelaxedRodLength ); // the mindist cannot be higher than relaxed pose mindist
}
Assert( flMinDist <= flMaxDist * 1.0001f + FLT_EPSILON );
UtlHashHandle_t hFind = edgeToRod.Find( nRodV0 );
uint nRodVidx = 0;
if ( hFind == edgeToRod.InvalidHandle( ) )
{
hFind = edgeToRod.Find( nRodV1 );
nRodVidx = 1;
}
if ( hFind == edgeToRod.InvalidHandle( ) )
{
nRodVidx = 0;
FeRodConstraint_t rod;
rod.nNode[ 0 ] = v0;
rod.nNode[ 1 ] = v1;
rod.flRelaxationFactor = 1.0f;
rod.flWeight0 = SnapWeight01( invM0 / ( invM1 + invM0 ) );
rod.flMinDist = flMinDist;
rod.flMaxDist = flMaxDist;
int nRod = m_Rods.AddToTail( rod );
edgeToRod.Insert( nRodV0, ( uint ) nRod );
}
else
{
FeRodConstraint_t &rod = m_Rods[ edgeToRod[ hFind ] ];
Assert( nRodVidx == 0 ? ( rod.nNode[ 0 ] == v0 && rod.nNode[ 1 ] == v1 ) : ( rod.nNode[ 0 ] == v1 && rod.nNode[ 1 ] == v0 ) );
rod.flMinDist = Min( rod.flMinDist, flMinDist );
rod.flMaxDist = Max( rod.flMaxDist, flMaxDist );
}
}
void CFeModelBuilder::BuildSprings( CUtlVector< FeSpringIntegrator_t > &springs )
{
CUtlHashtable< uint32 > created;
springs.EnsureCapacity( m_Springs.Count() );
for ( int i = 0; i < m_Springs.Count( ); ++i )
{
FeSpringIntegrator_t spring;
spring.nNode[ 0 ] = m_Springs[ i ].nNode[ 0 ];
spring.nNode[ 1 ] = m_Springs[ i ].nNode[ 1 ];
if ( created.HasElement( spring.nNode[ 0 ] + ( uint( spring.nNode[ 1 ] ) << 16 ) ) )
{
continue;
}
const BuildNode_t &node0 = m_Nodes[ spring.nNode[ 0 ] ], &node1 = m_Nodes[ spring.nNode[ 1 ] ];
if ( node1.invMass + node0.invMass < 1e-9f )
{
continue; // skip springs between pinned points
}
//float flAveDamping = ( node0.integrator.flPointDamping + node1.integrator.flPointDamping ) * 0.5f;
float expd0 = expf( -node0.integrator.flPointDamping ), expd1 = expf( -node1.integrator.flPointDamping );
// if particles are severely damped, or are massive, the spring should feel proportionally weaker:
// the original Dota spring constants had dimentionality to compute force (not acceleration), so they
// effectively (implicitly) were divided by mass to compute acceleration later
spring.flSpringConstant = m_Springs[ i ].flSpringConstant * ( node1.invMass * expd1 + node0.invMass * expd0 );
spring.flSpringDamping = m_Springs[ i ].flSpringDamping * ( node1.invMass * expd1 + node0.invMass * expd0 );
if ( spring.flSpringConstant == 0 && spring.flSpringDamping == 0 )
{
continue; // this spring will never generate any force...
}
spring.flSpringRestLength = ( m_Nodes[ spring.nNode[ 0 ] ].transform.m_vPosition - m_Nodes[ spring.nNode[ 1 ] ].transform.m_vPosition ).Length();
// make well-damped node to have effectively higher mass (lower invMass)
// so, node0 damping greater => node0 invMass lower
// because damping is an exponential function, this should figure out fine
// also, node0 fixed => weight0 == 0, and vice versa
spring.flNodeWeight0 = SnapWeight01( node0.invMass * expd0 / ( node1.invMass * expd1 + node0.invMass * expd0 ) );
created.Insert( spring.nNode[ 0 ] + ( uint( spring.nNode[ 1 ] ) << 16 ) );
created.Insert( spring.nNode[ 1 ] + ( uint( spring.nNode[ 0 ] ) << 16 ) );
springs.AddToTail( spring );
}
}
void CFeModelBuilder::BuildQuads( CUtlVector< FeQuad_t > &quads, bool bSkipTris )
{
quads.EnsureCapacity( m_Elems.Count( ) );
uint nQuadCount[ 3 ] = { 0, 0, 0 };
uint nLastStaticNodes = 2;
for ( int nElem = 0; nElem < m_Elems.Count( ); ++nElem )
{
const BuildElem_t &buildElem = m_Elems[ nElem ];
bool bIsQuad = buildElem.nNode[ 2 ] != buildElem.nNode[ 3 ];
if ( bSkipTris && !bIsQuad )
{
continue; // skip this quad: it's really a triangle and we skip triangles
}
uint nStaticNodes = buildElem.nStaticNodes;
Assert( nStaticNodes <= nLastStaticNodes ); // quads should already be sorted
nLastStaticNodes = nStaticNodes;
FeQuad_t quad;
quad.flSlack = buildElem.flSlack;
BuildNode_t node[ 4 ];
for ( uint j = 0; j < 4; ++j )
{
node[ j ] = m_Nodes[ buildElem.nNode[ j ] ];
}
float flSumMass = 0;
float flMass[ 4 ];
for ( uint j = nStaticNodes; j < 4; ++j )
{
flSumMass += ( flMass[ j ] = 1.0f / Max( 1e-6f, node[ j ].invMass ) );
}
Vector vCoM = vec3_origin;
float flWeight[ 4 ] = { 0,0,0,0}, flSumWeights = 0;
for ( uint j = nStaticNodes; j < 4; ++j )
{
flWeight[ j ] = flMass[ j ] / flSumMass;
vCoM += flWeight[ j ] * node[ j ].transform.m_vPosition;
flSumWeights += flWeight[ j ];
}
const VectorAligned &p0 = node[ 0 ].transform.m_vPosition;
const VectorAligned &p1 = node[ 1 ].transform.m_vPosition;
const VectorAligned &p2 = node[ 2 ].transform.m_vPosition;
const VectorAligned &p3 = node[ 3 ].transform.m_vPosition;
// special case: the center of mass is assumed to be at or between the infinite-mass nodes
switch ( nStaticNodes )
{
case 1:
vCoM = p0;
break;
case 2:
vCoM = ( p0 + p1 ) * 0.5f;
break;
}
CFeBasis basis;
if ( nStaticNodes == 2 )
{
basis.Init( p1 - p0, p2 + p3 - 2 * p0 );
}
else
{
basis.Init( p2 - p0, p3 - p1 );
}
Vector vShapeWorld[ 4 ];
for ( uint j = 0; j < 4; ++j )
{
vShapeWorld[ j ] = node[ j ].transform.m_vPosition - vCoM;
quad.nNode[ j ] = buildElem.nNode[ j ];
quad.vShape[ j ].Init( basis.WorldToLocal( vShapeWorld[ j ] ), j < nStaticNodes ? 0.0f : SnapWeight0( flWeight[ j ] / flSumWeights ) );
}
quads.AddToTail( quad );
nQuadCount[ nStaticNodes ] = quads.Count( );
}
m_nQuadCount[ 0 ] = quads.Count( );
m_nQuadCount[ 1 ] = Max( nQuadCount[ 1 ], nQuadCount[ 2 ] );
m_nQuadCount[ 2 ] = nQuadCount[ 2 ];
}
FeTri_t CFeModelBuilder::BuildTri( const BuildElem_t &buildElem, int nTriStaticNodes, int nSubTri )
{
FeTri_t tri;
BuildNode_t node[ 3 ];
for ( uint j = 0; j < 3; ++j )
{
tri.nNode[ j ] = buildElem.nNode[ j > 0 ? ( j + nSubTri ) : 0 ];
node[ j ] = m_Nodes[ tri.nNode[ j ] ];
}
float flSumMass = 0;
float flMass[ 3 ] = { 0, 0, 0 };
for ( uint j = nTriStaticNodes; j < 3; ++j )
{
flSumMass += ( flMass[ j ] = 1.0f / Max( 1e-6f, node[ j ].invMass ) );
}
Vector vEdge1 = node[ 1 ].transform.m_vPosition - node[ 0 ].transform.m_vPosition, vEdge2 = node[ 2 ].transform.m_vPosition - node[ 0 ].transform.m_vPosition;
CFeTriBasis basis( vEdge1, vEdge2 );
AssertDbg( fabsf( DotProduct( basis.vAxisY, vEdge1 ) ) < 1e-4f * ( 1 + vEdge1.Length() ) );
AssertDbg( CrossProduct( basis.vAxisX, vEdge1 ).Length( ) < 1e-4f * ( 1 + vEdge1.Length() ) );
tri.v1x = vEdge1.Length( );
tri.v2.x = DotProduct( vEdge2, basis.vAxisX );
tri.v2.y = DotProduct( vEdge2, basis.vAxisY );
tri.w1 = flMass[ 1 ] / flSumMass;
tri.w2 = flMass[ 2 ] / flSumMass;
return tri;
}
void CFeModelBuilder::BuildTris( CUtlVector< FeTri_t > &trisOut, bool bTriangulate )
{
CUtlVector< FeTri_t > tris[ 3 ];
tris[ 0 ].EnsureCapacity( m_Elems.Count( ) * 2 );
for ( int nElem = 0; nElem < m_Elems.Count( ); ++nElem )
{
const BuildElem_t &buildElem = m_Elems[ nElem ];
bool bIsQuad = buildElem.nNode[ 2 ] != buildElem.nNode[ 3 ];
if ( !bTriangulate && bIsQuad )
{
// this is a quad, skip
continue;
}
tris[ buildElem.nStaticNodes ].AddToTail( BuildTri( buildElem, buildElem.nStaticNodes, 0 ) );
if ( bIsQuad )
{
// add the second triangle of the quad ; it's 1 static node if the quad had 2 static nodes
uint nTri2static = Min< uint >( 1u, buildElem.nStaticNodes );
tris[ nTri2static ].AddToTail( BuildTri( buildElem, nTri2static, 1 ) );
}
}
m_nTriCount[ 0 ] = tris[ 0 ].Count( ) + tris[ 1 ].Count( ) + tris[ 2 ].Count( );
m_nTriCount[ 1 ] = tris[ 1 ].Count( ) + tris[ 2 ].Count( );
m_nTriCount[ 2 ] = tris[ 2 ].Count( );
trisOut.EnsureCapacity( m_nTriCount[ 0 ] );
trisOut.AddVectorToTail( tris[ 2 ] );
trisOut.AddVectorToTail( tris[ 1 ] );
trisOut.AddVectorToTail( tris[ 0 ] );
}
void CFeModelBuilder::BuildFeEdgeDesc( )
{
struct Side_t
{
uint16 nNode[ 2 ];
uint16 nElem;
};
CUtlHashtable< uint32, Side_t > edgeToVert;
uint nNodeCount = ( uint )m_Nodes.Count(); NOTE_UNUSED( nNodeCount );
for ( int nElement = 0; nElement < m_Elems.Count( ); ++nElement )
{
BuildElem_t elem = m_Elems[ nElement ];
const uint nElemEdgeCount = 4;
for ( uint nElemEdge = 0; nElemEdge < nElemEdgeCount; ++nElemEdge )
{
uint j1 = ( nElemEdge + 1 ) % nElemEdgeCount;
if ( nElemEdge == j1 )
continue; // skip degenerate edges
uint32 n0 = elem.nNode[ nElemEdge ], n1 = elem.nNode[ j1 ];
uint nEdgeV0 = n0 | ( n1 << 16 ), nEdgeV1 = n1 | ( n0 << 16 );
int j2 = ( nElemEdge + 2 ) % nElemEdgeCount, j3 = ( nElemEdge + 3 ) % nElemEdgeCount;
uint n2 = elem.nNode[ j2 ], n3 = elem.nNode[ j3 ];
Assert( n3 < nNodeCount && n2 < nNodeCount && n1 < nNodeCount && n0 < nNodeCount );
UtlHashHandle_t hFind = edgeToVert.Find( nEdgeV0 );
uint nEdgeVidx = 0;
if ( hFind == edgeToVert.InvalidHandle( ) )
{
hFind = edgeToVert.Find( nEdgeV1 );
nEdgeVidx = 1;
}
if ( hFind == edgeToVert.InvalidHandle( ) )
{
Side_t side = { { (uint16)n2, (uint16)n3 }, (uint16)nElement };
edgeToVert.Insert( nEdgeV0, side );
}
else
{
// found a matching edge!
FeEdgeDesc_t edge;
edge.nEdge[ 0 ] = n0;
edge.nEdge[ 1 ] = n1;
edge.nSide[ 0 ][ 0 ] = n2;
edge.nSide[ 0 ][ 1 ] = n3;
edge.nSide[ 1 ][ 0 ] = edgeToVert[ hFind ].nNode[ nEdgeVidx ];
edge.nSide[ 1 ][ 1 ] = edgeToVert[ hFind ].nNode[ 1 - nEdgeVidx ];
edge.nElem[ 0 ] = nElement;
edge.nElem[ 1 ] = edgeToVert[ hFind ].nElem;
m_FeEdgeDesc.AddToTail( edge );
edgeToVert.RemoveByHandle( hFind );
}
}
}
}
void CFeModelBuilder::BuildNodeSlack( float flSlackMultiplier )
{
for ( int nNode = 0; nNode < m_Nodes.Count( ); ++nNode )
{
m_Nodes[ nNode ].flSlack = FLT_MAX;
}
for ( int nElem = 0; nElem < m_Elems.Count( ); ++nElem )
{
BuildElem_t elem = m_Elems[ nElem ];
for ( int i = 0; i < 4; ++i )
{
int i1 = ( i + 1 ) % 4;
uint n0 = elem.nNode[ i ], n1 = elem.nNode[ i1 ];
if ( n0 != n1 )
{
float flSlack = flSlackMultiplier * ( m_Nodes[ n0 ].transform.m_vPosition - m_Nodes[ n1 ].transform.m_vPosition ).Length( );
m_Nodes[ n0 ].flSlack = Min( m_Nodes[ n0 ].flSlack, flSlack );
m_Nodes[ n1 ].flSlack = Min( m_Nodes[ n0 ].flSlack, flSlack );
}
}
}
for ( int nElem = 0; nElem < m_Elems.Count( ); ++nElem )
{
BuildElem_t &elem = m_Elems[ nElem ];
elem.flSlack = FLT_MAX;
for ( int i = 0; i < 4; ++i )
{
elem.flSlack = Min( elem.flSlack, m_Nodes[ elem.nNode[ i ] ].flSlack );
}
}
}
void CFeModelBuilder::BuildOldFeEdges( )
{
for ( int ei = 0; ei < m_FeEdgeDesc.Count( ); ++ei )
{
// found a matching edge!
FeEdgeDesc_t edgeIn = m_FeEdgeDesc[ ei ];
OldFeEdge_t edge;
edge.m_nNode[ 0 ] = edgeIn.nEdge[ 0 ];
edge.m_nNode[ 1 ] = edgeIn.nEdge[ 1 ];
edge.m_nNode[ 2 ] = edgeIn.nSide[ 0 ][ 0 ];
edge.m_nNode[ 3 ] = edgeIn.nSide[ 1 ][ 0 ];
Vector x[ 4 ];
for ( int k = 0; k < 4; ++k )
x[ k ] = m_Nodes[ edge.m_nNode[ k ] ].transform.m_vPosition;
Vector e[ 5 ] = { x[ 1 ] - x[ 0 ], x[ 2 ] - x[ 0 ], x[ 3 ] - x[ 0 ], x[ 2 ] - x[ 1 ], x[ 3 ] - x[ 1 ] };
float cot0[ 5 ];
// this is the factor f to make Q = fK^t * fK, see "Discrete IBM: Implementation", page 2 of "A Quadratic Bending Model for Inextensible Surfaces"
float flInvAreasSqrt = sqrtf( 6.0f / ( CrossProduct( e[ 0 ], e[ 1 ] ).Length( ) + CrossProduct( e[ 0 ], e[ 2 ] ).Length( ) ) );
for ( int k = 0; k < 5; ++k )
cot0[ k ] = DotProduct( e[ 0 ], e[ k ] ) / CrossProduct( e[ 0 ], e[ k ] ).Length( ); // ( cosine / sine ), coincidentally a stable way to find a non-zero (non-degenerate) angle..
edge.m_flK[ 0 ] = flInvAreasSqrt * ( cot0[ 3 ] + cot0[ 4 ] );
edge.m_flK[ 1 ] = flInvAreasSqrt * ( cot0[ 1 ] + cot0[ 2 ] );
edge.m_flK[ 2 ] = flInvAreasSqrt * ( -cot0[ 1 ] - cot0[ 3 ] );
Vector n1 = CrossProduct( e[ 0 ], e[ 1 ] ), n2 = CrossProduct( e[ 0 ], e[ 2 ] );
float e_sqr = e[ 0 ].LengthSqr( ), n1_len = n1.Length( ), n2_len = n2.Length( );
float flTheta0 = atan2f( CrossProduct( n1, n2 ).Length( ), DotProduct( n1, n2 ) );
edge.flThetaRelaxed = sinf( 0.5f * flTheta0 );
edge.flThetaFactor = e_sqr / ( n1_len + n2_len );
edge.t = 0.5f * DotProduct( e[ 2 ] + e[ 1 ], e[ 0 ] ) / e[ 0 ].LengthSqr( );
edge.invA = flInvAreasSqrt;
edge.c01 = cot0[ 1 ];
edge.c02 = cot0[ 2 ];
edge.c03 = cot0[ 3 ];
edge.c04 = cot0[ 4 ];
// virtual edge and axial normal
m_OldFeEdges.AddToTail( edge );
}
}
// return 1/(x0^2/invM0+x1^2/invM1)
static inline float Compute1DInvInertiaTensor( float invM0, float x0, float invM1, float x1 )
{
if ( invM0 <= 1e-8f )
{
return invM1 <= 1e-8f || fabsf( x0 ) > 1e-6f ? 0.0f : invM1 / Sqr( x1 );
}
else
{
if ( invM1 <= 1e-8f )
{
return fabsf( x1 ) > 1e-6f ? 0.0f : invM0 / Sqr( x0 );
}
else
{
return 1.0f / ( Sqr( x0 ) / invM0 + Sqr( x1 ) / invM1 );
}
}
}
static inline float ComputeEdgeCenterOfMass( float a, float b )
{
return fabsf( b ) >= 1e-8f ? a / b : 0.5f;
}
static inline float CombineInvMasses( float invM0, float invM1 )
{
if ( invM0 <= 0 || invM1 <= 0 )
return 0;
return ( invM0 * invM1 ) / ( invM0 + invM1 ); // ( m0 + m1 ) ^ -1
}
void CFeModelBuilder::BuildAxialEdges( )
{
for ( int ei = 0; ei < m_FeEdgeDesc.Count( ); ++ei )
{
// found a matching edge!
FeEdgeDesc_t edgeIn = m_FeEdgeDesc[ ei ];
FeAxialEdgeBend_t edge;
edge.nNode[ 0 ] = edgeIn.nEdge[ 0 ];
edge.nNode[ 1 ] = edgeIn.nEdge[ 1 ];
edge.nNode[ 2 ] = edgeIn.nSide[ 0 ][ 0 ];
edge.nNode[ 3 ] = edgeIn.nSide[ 0 ][ 1 ];
edge.nNode[ 4 ] = edgeIn.nSide[ 1 ][ 0 ];
edge.nNode[ 5 ] = edgeIn.nSide[ 1 ][ 1 ];
Vector x[ 4 ] = {
m_Nodes[ edge.nNode[ 0 ] ].transform.m_vPosition,
m_Nodes[ edge.nNode[ 1 ] ].transform.m_vPosition,
( m_Nodes[ edge.nNode[ 2 ] ].transform.m_vPosition + m_Nodes[ edge.nNode[ 3 ] ].transform.m_vPosition )* 0.5f,
( m_Nodes[ edge.nNode[ 4 ] ].transform.m_vPosition + m_Nodes[ edge.nNode[ 5 ] ].transform.m_vPosition )* 0.5f
};
// do we need to flip it? The direction of the bend depends on the ordering of nodes
{
Vector vApproximateAxis = ( x[ 1 ] + x[ 0 ] ) - ( x[ 2 ] + x[ 3 ] );
Vector vEdge = x[ 1 ] - x[ 0 ], vVirtualEdge = x[ 2 ] - x[ 3 ];
Vector vCrossEdges = CrossProduct( vEdge, vVirtualEdge );
if ( DotProduct( vApproximateAxis, vCrossEdges ) < 0 )
{
// flip one of the edges around
Swap( x[ 0 ], x[ 1 ] );
Swap( edge.nNode[ 0 ], edge.nNode[ 1 ] );
}
}
float invM[ 4 ] = {
m_Nodes[ edge.nNode[ 0 ] ].invMass,
m_Nodes[ edge.nNode[ 1 ] ].invMass,
edge.nNode[ 2 ] == edge.nNode[ 3 ] ? m_Nodes[ edge.nNode[ 2 ] ].invMass : CombineInvMasses( m_Nodes[ edge.nNode[ 2 ] ].invMass, m_Nodes[ edge.nNode[ 3 ] ].invMass ),
edge.nNode[ 4 ] == edge.nNode[ 5 ] ? m_Nodes[ edge.nNode[ 4 ] ].invMass : CombineInvMasses( m_Nodes[ edge.nNode[ 4 ] ].invMass, m_Nodes[ edge.nNode[ 5 ] ].invMass )
};
if ( invM[ 0 ] + invM[ 1 ] + invM[ 2 ] + invM[ 3 ] == 0 )
{
// nothing to keep bent, the static nodes at the fringes will take care of it
continue;
}
Vector e[ 5 ] = { x[ 1 ] - x[ 0 ], x[ 2 ] - x[ 0 ], x[ 3 ] - x[ 0 ], x[ 2 ] - x[ 1 ], x[ 3 ] - x[ 1 ] };
float cot0[ 5 ] = { 0 };
// this is the factor f to make Q = fK^t * fK, see "Discrete IBM: Implementation", page 2 of "A Quadratic Bending Model for Inextensible Surfaces"
//float flInvAreasSqrt = sqrtf( 6.0f / ( CrossProduct( e[ 0 ], e[ 1 ] ).Length() + CrossProduct( e[ 0 ], e[ 2 ] ).Length() ) );
for ( int k = 1; k < 5; ++k )
{
cot0[ k ] = DotProduct( e[ 0 ], e[ k ] ) / CrossProduct( e[ 0 ], e[ k ] ).Length(); // ( cosine / sine ), coincidentally a stable way to find a non-zero (non-degenerate) angle..
}
Vector n1 = CrossProduct( e[ 0 ], e[ 1 ] ), n2 = CrossProduct( e[ 0 ], e[ 2 ] );
// virtual edge and axial normal
Vector eV = x[ 3 ] - x[ 2 ], an = CrossProduct( e[ 0 ], eV );
float flAxialNormalLength = an.Length( );
if ( flAxialNormalLength < 0.001f )
continue;
an /= flAxialNormalLength;
float e0len = e[ 0 ].Length( ), eVlen = eV.Length( );
Vector e0dir = e[ 0 ] / e0len;
Vector side = CrossProduct( an, e0dir );
float t2 = DotProduct( side, e[ 1 ] ), t3 = DotProduct( side, e[ 2 ] );
// the indices are a misnomer; t correspond to x, e to e in "Discrete Quadratic Curvature Energies" by Wardetzky et al, page 28
edge.tv = -t2 / ( t3 - t2 );
Vector v23 = e[ 1 ] + eV * edge.tv;
float f0len = DotProduct( v23, e0dir );
edge.te = f0len / e0len;
Vector v01 = e0dir * f0len;
Assert( fabs( DotProduct( v01 - v23, e[ 0 ] ) ) < 0.001f && fabs( DotProduct( v01 - v23, eV ) ) < 0.001f );
edge.flDist = ( v01 - v23 ).Length( );
float c0len = ComputeEdgeCenterOfMass( e0len * invM[ 0 ], invM[ 0 ] + invM[ 1 ] ); // pretend x0 = 0, x1 = e0len, find the center of mass along that line
float cVlen = ComputeEdgeCenterOfMass( eVlen * invM[ 2 ], invM[ 2 ] + invM[ 3 ] ); // pretend x2 = 0, x3 = eVlen, find the center of mass of the virtual edge
// Inv Inertia Tensor is ( m0 * x0^2 + m1 * x1^2 )^-1 == m0^-1 * m1^-1 / ( m1^-1 * x0^2 + m0^-1 * x1^2 ) , we just compute it here
float flI0inv = Compute1DInvInertiaTensor( invM[ 0 ], ( c0len ), invM[ 1 ], ( e0len - c0len ) );
float flIVinv = Compute1DInvInertiaTensor( invM[ 2 ], ( cVlen ), invM[ 3 ], ( eVlen - cVlen ) );
float /*f0len = edge.t01 * e0len,*/ fVlen = edge.tv * eVlen;
float f0 = f0len - c0len, fV = fVlen - cVlen;
if ( 1 )
{
// when applying impulse 1.0 to edge 0-1, and -1.0 to virtual edge 2-3, this is how much the x[.] vertices move
float flReact[ 4 ] = {
invM[ 0 ] - flI0inv * f0 * c0len, invM[ 1 ] + flI0inv * f0 * ( e0len - c0len ),
-( invM[ 2 ] - flIVinv * fV * cVlen ), -( invM[ 3 ] + flIVinv * fV * ( eVlen - cVlen ) )
};
// reactions at the fulcrum points
float flReact01 = edge.te * flReact[ 0 ] + ( 1 - edge.te ) * flReact[ 1 ], flReact23 = edge.tv * flReact[ 2 ] + ( 1 - edge.tv ) * flReact[ 3 ];
float flReactSumAtFulcrum = flReact01 - flReact23; // applying impulse 1.0 at the connection between fulcrums, this is how much the gap closes
if ( flReactSumAtFulcrum > 1e-8f )
{
edge.flWeight[ 0 ] = ( flReact[ 0 ] / flReactSumAtFulcrum );
edge.flWeight[ 1 ] = ( flReact[ 1 ] / flReactSumAtFulcrum );
edge.flWeight[ 2 ] = ( flReact[ 2 ] / flReactSumAtFulcrum );
//if ( edge.nNode[ 2 ] == edge.nNode[ 3 ] )
// edge.flWeight[ 2 ] *= 0.5f; // the same weight will be applied to the same node twice.. Careful! SIMD implementation doesn't need *=0.5! and scalar can easily avoid this, too
edge.flWeight[ 3 ] = ( flReact[ 3 ] / flReactSumAtFulcrum );
//if ( edge.nNode[ 4 ] == edge.nNode[ 5 ] )
// edge.flWeight[ 3 ] *= 0.5f; // the same weight will be applied to the same node twice.. Careful! SIMD implementation doesn't need *=0.5! and scalar can easily avoid this, too
Assert( invM[ 0 ] == 0 || invM[ 1 ] == 0 || invM[ 2 ] == 0 || invM[ 3 ] == 0 || fabsf( edge.flWeight[ 0 ] / invM[ 0 ] + edge.flWeight[ 1 ] / invM[ 1 ] + edge.flWeight[ 2 ] / invM[ 2 ] + edge.flWeight[ 3 ] / invM[ 3 ] ) < 1e-5f / ( invM[ 0 ] + invM[ 1 ] + invM[ 2 ] + invM[ 3 ] ) );
m_AxialEdges.AddToTail( edge );
}
}
else
{
// just apply impulse along the line between the edge and virtual edge.
float flEdgeInvMass = CombineInvMasses( invM[ 0 ], invM[ 1 ] );
// virtual edge will move against the direction, main edge will move along
float flVirtEdgeReact = invM[ 2 ] * ( 1 - edge.tv ) + invM[ 3 ] * edge.tv;
float flTotalReaction = flEdgeInvMass + flVirtEdgeReact; // this is how much the edge and virt edge close when applying impulse 1.0
if ( flTotalReaction < FLT_EPSILON )
continue; // we have nothing to move except one of the vertices on the real edge, and that'll be taken care of by quad contraints
edge.flWeight[ 0 ] = edge.flWeight[ 1 ] = flEdgeInvMass / flTotalReaction;
edge.flWeight[ 2 ] = - invM[ 2 ] / flTotalReaction;
edge.flWeight[ 3 ] = -invM[ 3 ] / flTotalReaction;
}
}
CFeModelBuilder *pThis = this;
HeapSort( m_AxialEdges, [ pThis ]( const FeAxialEdgeBend_t &left, const FeAxialEdgeBend_t &right )
{
return pThis->GetRank( left ) < pThis->GetRank( right );
}
);
}
int CFeModelBuilder::ReconcileElemStaticNodes()
{
int nExtraStaticNodesFound = 0;
// find out if any elements have 3+ static nodes and remove them; sort nodes in elements that have more static nodes than declared
// we may FastRemove elements during this loop, so counting downwards is easiest
for ( int nElem = m_Elems.Count(); nElem-- > 0; )
{
BuildElem_t &elem = m_Elems[ nElem ];
int nExtraStatics = 0;
{
uint numNodes = elem.NumNodes();
for ( uint j = elem.nStaticNodes; j < numNodes; ++j )
{
uint nNode = elem.nNode[ j ];
if ( !m_Nodes[ nNode ].bSimulated )
{
++nExtraStatics;
}
}
}
nExtraStaticNodesFound += nExtraStatics;
elem.nStaticNodes += nExtraStatics;
if ( nExtraStatics )
{
CUtlVectorAligned< BuildNode_t > &nodes = m_Nodes;
// element is still dynamic; sort it and go on
uint numNodes = elem.NumNodes();
BubbleSort( elem.nNode, numNodes, [ &nodes ]( uint nLeftNode, uint nRightNode ) {
return int( nodes[ nLeftNode ].bSimulated ) < int( nodes[ nRightNode ].bSimulated ); // sort: non-simulated, then simulated
} );
elem.nNode[ 3 ] = elem.nNode[ numNodes - 1 ];
for ( elem.nStaticNodes = 0; elem.nStaticNodes < numNodes && !nodes[ elem.nNode[ elem.nStaticNodes ] ].bSimulated ; ++elem.nStaticNodes )
continue;
}
if ( elem.nStaticNodes >= 3 )
{
// the whole element is static; remove it, mark all as static
for ( uint j = elem.nStaticNodes; j < BuildElem_t::MAX_NODES; ++j )
{
BuildNode_t &refNode = m_Nodes[ elem.nNode[ j ] ];
if ( refNode.bSimulated && !refNode.bForceSimulated )
{
refNode.bSimulated = false;
nExtraStaticNodesFound++;
elem.nStaticNodes++;
}
}
// Note: we still need fully static elements to construct stiffness rods, perhaps some face angle constraints, and the analogs of 3-static-node quads
//m_Elems.FastRemove( nElem );
}
}
return nExtraStaticNodesFound;
}
int CFeModelBuilder::RemoveFullyStaticElems()
{
int nRemoved = 0;
for ( int nElem = m_Elems.Count(); nElem-- > 0; )
{
BuildElem_t &elem = m_Elems[ nElem ];
if ( elem.nStaticNodes >= 3 )
{
++nRemoved;
m_Elems.Remove( nElem ); // keep the order
}
}
return nRemoved;
}
void CFeModelBuilder::BuildInvMassesAndSortNodes( )
{
// we don't remap anything, because we assume everything's downstream of this call
Assert( m_AxialEdges.IsEmpty( ) && m_OldFeEdges.IsEmpty( ) && m_KelagerBends.IsEmpty( ) && m_FeEdgeDesc.IsEmpty( ) );
CleanupElements();
const int nInvalidRank = INT_MAX;
CUtlVector< float > nodeMass;
if ( m_bEnableExplicitNodeMasses )
{
nodeMass.SetCount( m_Nodes.Count( ) );
for ( int i = 0; i < m_Nodes.Count( ); ++i )
{
if ( m_Nodes[ i ].invMass > FLT_EPSILON )
{
nodeMass[ i ] = 1.0f / m_Nodes[ i ].invMass;
}
else
{
nodeMass[ i ] = 0;
m_Nodes[ i ].bSimulated = false;
}
}
}
else
{
RecomputeMasses( nodeMass );
BalanceGlobalMassMultipliers( nodeMass );
}
CUtlVector< CUtlSortVector< int >* >nodeConn; // nodes this node is connected to
nodeConn.SetCount( m_Nodes.Count( ) );
for ( int nNode = 0; nNode < m_Nodes.Count( ); ++nNode )
{
m_Nodes[ nNode ].nRank = nInvalidRank;
nodeConn[ nNode ] = new CUtlSortVector< int >( ); // this shouldn't normally be more than 8 elements, so even sorting this vector isn't a win, but the utility functions on Sortvector are convenient
}
// propagate static nodes
do
{
// mark all nodes declared as static in Finite ELements, as static
for ( int nElem = 0; nElem < m_Elems.Count( ); ++nElem )
{
const BuildElem_t &elem = m_Elems[ nElem ];
for ( uint j = 0; j < elem.nStaticNodes; ++j )
{
uint nNode = elem.nNode[ j ];
m_Nodes[ nNode ].bSimulated = false;
}
}
}
while ( ReconcileElemStaticNodes() );
// slam nodes that are not simulated
for ( int i = 0; i < m_Nodes.Count( ); ++i )
{
if ( !m_Nodes[ i ].bSimulated )
{
nodeMass[ i ] = 0.0f;
m_Nodes[ i ].invMass = 0.0f;
}
}
// add node connectivity from elements
for ( int nElem = 0; nElem < m_Elems.Count(); ++nElem)
{
const BuildElem_t &elem = m_Elems[ nElem ];
if ( CountSimulatedNodesIn( elem ) >= 1 )
{
AssertDbg( elem.nNode[ 1 ] != elem.nNode[ 0 ] && elem.nNode[ 2 ] != elem.nNode[ 1 ] && elem.nNode[ 0 ] != elem.nNode[ 2 ] && elem.nNode[ 0 ] != elem.nNode[ 3 ] );
for ( int j = 0; j < 3; ++j )
{
for ( int k = j + 1; k < 4; ++k )
{
nodeConn[ elem.nNode[ j ] ]->InsertIfNotFound( elem.nNode[ k ] );
nodeConn[ elem.nNode[ k ] ]->InsertIfNotFound( elem.nNode[ j ] );
}
}
}
}
// add node connectivity from rods
for ( int nRod = 0; nRod < m_Rods.Count( ); )
{
const FeRodConstraint_t &rod = m_Rods[ nRod ];
if ( CountSimulatedNodesIn( rod ) >= 1 )
{
nodeConn[ rod.nNode[ 0 ] ]->InsertIfNotFound( rod.nNode[ 1 ] );
nodeConn[ rod.nNode[ 1 ] ]->InsertIfNotFound( rod.nNode[ 0 ] );
++nRod;
}
else
{
m_Rods.FastRemove( nRod );
}
}
// invert accumulated masses
if ( !m_bEnableExplicitNodeMasses )
{
bool bShouldReconcileElems = false;
for ( int nNode = 0; nNode < m_Nodes.Count( ); ++nNode )
{
BuildNode_t &refNode = m_Nodes[ nNode ];
float flMassMultiplier = refNode.flMassMultiplier;
float flMass = nodeMass[ nNode ] * ( flMassMultiplier > FLT_EPSILON ? flMassMultiplier : 1.0f ) + refNode.flMassBias;
if ( flMass > 1e-8f )
{
refNode.invMass = 1.0f / flMass;
}
else if ( refNode.bForceSimulated && refNode.bSimulated )
{
refNode.invMass = 1.0f;
}
else
{
if ( refNode.bSimulated )
{
bShouldReconcileElems = true;
}
refNode.invMass = 0;
refNode.bSimulated = false;
}
}
if ( bShouldReconcileElems )
{
ReconcileElemStaticNodes();
}
}
// set static nodes to invM = 0 (infinite mass)
m_CtrlToNode.SetCount( m_Nodes.Count( ) );