-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathbtSequentialImpulseConstraintSolverMt.cpp
More file actions
1562 lines (1421 loc) · 58.7 KB
/
Copy pathbtSequentialImpulseConstraintSolverMt.cpp
File metadata and controls
1562 lines (1421 loc) · 58.7 KB
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
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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 "btSequentialImpulseConstraintSolverMt.h"
#include "LinearMath/btQuickprof.h"
#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
#include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
#include "BulletDynamics/Dynamics/btRigidBody.h"
bool btSequentialImpulseConstraintSolverMt::s_allowNestedParallelForLoops = false; // some task schedulers don't like nested loops
int btSequentialImpulseConstraintSolverMt::s_minimumContactManifoldsForBatching = 250;
int btSequentialImpulseConstraintSolverMt::s_minBatchSize = 50;
int btSequentialImpulseConstraintSolverMt::s_maxBatchSize = 100;
btBatchedConstraints::BatchingMethod btSequentialImpulseConstraintSolverMt::s_contactBatchingMethod = btBatchedConstraints::BATCHING_METHOD_SPATIAL_GRID_2D;
btBatchedConstraints::BatchingMethod btSequentialImpulseConstraintSolverMt::s_jointBatchingMethod = btBatchedConstraints::BATCHING_METHOD_SPATIAL_GRID_2D;
btSequentialImpulseConstraintSolverMt::btSequentialImpulseConstraintSolverMt()
{
m_numFrictionDirections = 1;
m_useBatching = false;
m_useObsoleteJointConstraints = false;
}
btSequentialImpulseConstraintSolverMt::~btSequentialImpulseConstraintSolverMt()
{
}
void btSequentialImpulseConstraintSolverMt::setupBatchedContactConstraints()
{
BT_PROFILE("setupBatchedContactConstraints");
m_batchedContactConstraints.setup(&m_tmpSolverContactConstraintPool,
m_tmpSolverBodyPool,
s_contactBatchingMethod,
s_minBatchSize,
s_maxBatchSize,
&m_scratchMemory);
}
void btSequentialImpulseConstraintSolverMt::setupBatchedJointConstraints()
{
BT_PROFILE("setupBatchedJointConstraints");
m_batchedJointConstraints.setup(&m_tmpSolverNonContactConstraintPool,
m_tmpSolverBodyPool,
s_jointBatchingMethod,
s_minBatchSize,
s_maxBatchSize,
&m_scratchMemory);
}
void btSequentialImpulseConstraintSolverMt::internalSetupContactConstraints(int iContactConstraint, const btContactSolverInfo& infoGlobal)
{
btSolverConstraint& contactConstraint = m_tmpSolverContactConstraintPool[iContactConstraint];
btVector3 rel_pos1;
btVector3 rel_pos2;
btScalar relaxation;
int solverBodyIdA = contactConstraint.m_solverBodyIdA;
int solverBodyIdB = contactConstraint.m_solverBodyIdB;
btSolverBody* solverBodyA = &m_tmpSolverBodyPool[solverBodyIdA];
btSolverBody* solverBodyB = &m_tmpSolverBodyPool[solverBodyIdB];
btRigidBody* colObj0 = solverBodyA->m_originalBody;
btRigidBody* colObj1 = solverBodyB->m_originalBody;
btManifoldPoint& cp = *static_cast<btManifoldPoint*>(contactConstraint.m_originalContactPoint);
const btVector3& pos1 = cp.getPositionWorldOnA();
const btVector3& pos2 = cp.getPositionWorldOnB();
rel_pos1 = pos1 - solverBodyA->getWorldTransform().getOrigin();
rel_pos2 = pos2 - solverBodyB->getWorldTransform().getOrigin();
btVector3 vel1;
btVector3 vel2;
solverBodyA->getVelocityInLocalPointNoDelta(rel_pos1, vel1);
solverBodyB->getVelocityInLocalPointNoDelta(rel_pos2, vel2);
btVector3 vel = vel1 - vel2;
btScalar rel_vel = cp.m_normalWorldOnB.dot(vel);
setupContactConstraint(contactConstraint, solverBodyIdA, solverBodyIdB, cp, infoGlobal, relaxation, rel_pos1, rel_pos2);
// setup rolling friction constraints
int rollingFrictionIndex = m_rollingFrictionIndexTable[iContactConstraint];
if (rollingFrictionIndex >= 0)
{
btSolverConstraint& spinningFrictionConstraint = m_tmpSolverContactRollingFrictionConstraintPool[rollingFrictionIndex];
btAssert(spinningFrictionConstraint.m_frictionIndex == iContactConstraint);
setupTorsionalFrictionConstraint(spinningFrictionConstraint,
cp.m_normalWorldOnB,
solverBodyIdA,
solverBodyIdB,
cp,
cp.m_combinedSpinningFriction,
rel_pos1,
rel_pos2,
colObj0,
colObj1,
relaxation,
0.0f,
0.0f);
btVector3 axis[2];
btPlaneSpace1(cp.m_normalWorldOnB, axis[0], axis[1]);
axis[0].normalize();
axis[1].normalize();
applyAnisotropicFriction(colObj0, axis[0], btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
applyAnisotropicFriction(colObj1, axis[0], btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
applyAnisotropicFriction(colObj0, axis[1], btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
applyAnisotropicFriction(colObj1, axis[1], btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
// put the largest axis first
if (axis[1].length2() > axis[0].length2())
{
btSwap(axis[0], axis[1]);
}
const btScalar kRollingFrictionThreshold = 0.001f;
for (int i = 0; i < 2; ++i)
{
int iRollingFric = rollingFrictionIndex + 1 + i;
btSolverConstraint& rollingFrictionConstraint = m_tmpSolverContactRollingFrictionConstraintPool[iRollingFric];
btAssert(rollingFrictionConstraint.m_frictionIndex == iContactConstraint);
btVector3 dir = axis[i];
if (dir.length() > kRollingFrictionThreshold)
{
setupTorsionalFrictionConstraint(rollingFrictionConstraint,
dir,
solverBodyIdA,
solverBodyIdB,
cp,
cp.m_combinedRollingFriction,
rel_pos1,
rel_pos2,
colObj0,
colObj1,
relaxation,
0.0f,
0.0f);
}
else
{
rollingFrictionConstraint.m_frictionIndex = -1; // disable constraint
}
}
}
// setup friction constraints
// setupFrictionConstraint(solverConstraint, normalAxis, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal, desiredVelocity, cfmSlip);
{
///Bullet has several options to set the friction directions
///By default, each contact has only a single friction direction that is recomputed automatically very frame
///based on the relative linear velocity.
///If the relative velocity it zero, it will automatically compute a friction direction.
///You can also enable two friction directions, using the SOLVER_USE_2_FRICTION_DIRECTIONS.
///In that case, the second friction direction will be orthogonal to both contact normal and first friction direction.
///
///If you choose SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION, then the friction will be independent from the relative projected velocity.
///
///The user can manually override the friction directions for certain contacts using a contact callback,
///and set the cp.m_lateralFrictionInitialized to true
///In that case, you can set the target relative motion in each friction direction (cp.m_contactMotion1 and cp.m_contactMotion2)
///this will give a conveyor belt effect
///
btSolverConstraint* frictionConstraint1 = &m_tmpSolverContactFrictionConstraintPool[contactConstraint.m_frictionIndex];
btAssert(frictionConstraint1->m_frictionIndex == iContactConstraint);
btSolverConstraint* frictionConstraint2 = NULL;
if (infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS)
{
frictionConstraint2 = &m_tmpSolverContactFrictionConstraintPool[contactConstraint.m_frictionIndex + 1];
btAssert(frictionConstraint2->m_frictionIndex == iContactConstraint);
}
if (!(infoGlobal.m_solverMode & SOLVER_ENABLE_FRICTION_DIRECTION_CACHING) || !(cp.m_contactPointFlags & BT_CONTACT_FLAG_LATERAL_FRICTION_INITIALIZED))
{
cp.m_lateralFrictionDir1 = vel - cp.m_normalWorldOnB * rel_vel;
btScalar lat_rel_vel = cp.m_lateralFrictionDir1.length2();
if (!(infoGlobal.m_solverMode & SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION) && lat_rel_vel > SIMD_EPSILON)
{
cp.m_lateralFrictionDir1 *= 1.f / btSqrt(lat_rel_vel);
applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
setupFrictionConstraint(*frictionConstraint1, cp.m_lateralFrictionDir1, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal);
if (frictionConstraint2)
{
cp.m_lateralFrictionDir2 = cp.m_lateralFrictionDir1.cross(cp.m_normalWorldOnB);
cp.m_lateralFrictionDir2.normalize(); //??
applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
setupFrictionConstraint(*frictionConstraint2, cp.m_lateralFrictionDir2, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal);
}
}
else
{
btPlaneSpace1(cp.m_normalWorldOnB, cp.m_lateralFrictionDir1, cp.m_lateralFrictionDir2);
applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
setupFrictionConstraint(*frictionConstraint1, cp.m_lateralFrictionDir1, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal);
if (frictionConstraint2)
{
applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
setupFrictionConstraint(*frictionConstraint2, cp.m_lateralFrictionDir2, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal);
}
if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS) && (infoGlobal.m_solverMode & SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION))
{
cp.m_contactPointFlags |= BT_CONTACT_FLAG_LATERAL_FRICTION_INITIALIZED;
}
}
}
else
{
setupFrictionConstraint(*frictionConstraint1, cp.m_lateralFrictionDir1, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal, cp.m_contactMotion1, cp.m_frictionCFM);
if (frictionConstraint2)
{
setupFrictionConstraint(*frictionConstraint2, cp.m_lateralFrictionDir2, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2, colObj0, colObj1, relaxation, infoGlobal, cp.m_contactMotion2, cp.m_frictionCFM);
}
}
}
setFrictionConstraintImpulse(contactConstraint, solverBodyIdA, solverBodyIdB, cp, infoGlobal);
}
struct SetupContactConstraintsLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
const btBatchedConstraints* m_bc;
const btContactSolverInfo* m_infoGlobal;
SetupContactConstraintsLoop(btSequentialImpulseConstraintSolverMt* solver, const btBatchedConstraints* bc, const btContactSolverInfo& infoGlobal)
{
m_solver = solver;
m_bc = bc;
m_infoGlobal = &infoGlobal;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
BT_PROFILE("SetupContactConstraintsLoop");
for (int iBatch = iBegin; iBatch < iEnd; ++iBatch)
{
const btBatchedConstraints::Range& batch = m_bc->m_batches[iBatch];
for (int i = batch.begin; i < batch.end; ++i)
{
int iContact = m_bc->m_constraintIndices[i];
m_solver->internalSetupContactConstraints(iContact, *m_infoGlobal);
}
}
}
};
void btSequentialImpulseConstraintSolverMt::setupAllContactConstraints(const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("setupAllContactConstraints");
if (m_useBatching)
{
const btBatchedConstraints& batchedCons = m_batchedContactConstraints;
SetupContactConstraintsLoop loop(this, &batchedCons, infoGlobal);
for (int iiPhase = 0; iiPhase < batchedCons.m_phases.size(); ++iiPhase)
{
int iPhase = batchedCons.m_phaseOrder[iiPhase];
const btBatchedConstraints::Range& phase = batchedCons.m_phases[iPhase];
int grainSize = 1;
btParallelFor(phase.begin, phase.end, grainSize, loop);
}
}
else
{
for (int i = 0; i < m_tmpSolverContactConstraintPool.size(); ++i)
{
internalSetupContactConstraints(i, infoGlobal);
}
}
}
int btSequentialImpulseConstraintSolverMt::getOrInitSolverBodyThreadsafe(btCollisionObject& body, btScalar timeStep)
{
//
// getOrInitSolverBody is threadsafe only for a single thread per solver (with potentially multiple solvers)
//
// getOrInitSolverBodyThreadsafe -- attempts to be fully threadsafe (however may affect determinism)
//
int solverBodyId = -1;
bool isRigidBodyType = btRigidBody::upcast(&body) != NULL;
if (isRigidBodyType && !body.isStaticOrKinematicObject())
{
// dynamic body
// Dynamic bodies can only be in one island, so it's safe to write to the companionId
solverBodyId = body.getCompanionId();
if (solverBodyId < 0)
{
m_bodySolverArrayMutex.lock();
// now that we have the lock, check again
solverBodyId = body.getCompanionId();
if (solverBodyId < 0)
{
solverBodyId = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody, &body, timeStep);
body.setCompanionId(solverBodyId);
}
m_bodySolverArrayMutex.unlock();
}
}
else if (isRigidBodyType && body.isKinematicObject())
{
//
// NOTE: must test for kinematic before static because some kinematic objects also
// identify as "static"
//
// Kinematic bodies can be in multiple islands at once, so it is a
// race condition to write to them, so we use an alternate method
// to record the solverBodyId
int uniqueId = body.getWorldArrayIndex();
const int INVALID_SOLVER_BODY_ID = -1;
m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();
if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
{
// now that we have the lock, check again
if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
{
m_kinematicBodyUniqueIdToSolverBodyTable.resize(uniqueId + 1, INVALID_SOLVER_BODY_ID);
}
}
// when another thread resizes the table, it will perform the following two steps:
// ANOTHER-1. allocate the new meomry block
// ANOTHER-2. copy the existing data from the old memory block to the new meomry block
//
// there can be such timeline:
// ANOTHER-1. another thread has allocated the new meomry block
// CURRENT. current thread is reading the **uninitialized** data from the new memory block
// ANOTHER-2. another thread will copy the existing data from the old memory block to the new meomry block, but this will not affect the uninitialized data read by the current thread
solverBodyId = m_kinematicBodyUniqueIdToSolverBodyTable[uniqueId];
m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();
// if no table entry yet,
if (INVALID_SOLVER_BODY_ID == solverBodyId)
{
// need to acquire both locks
m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();
m_bodySolverArrayMutex.lock();
// now that we have the lock, check again
solverBodyId = m_kinematicBodyUniqueIdToSolverBodyTable[uniqueId];
if (INVALID_SOLVER_BODY_ID == solverBodyId)
{
// create a table entry for this body
solverBodyId = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody, &body, timeStep);
m_kinematicBodyUniqueIdToSolverBodyTable[uniqueId] = solverBodyId;
}
m_bodySolverArrayMutex.unlock();
m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();
}
}
else
{
// all fixed bodies (inf mass) get mapped to a single solver id
if (m_fixedBodyId < 0)
{
m_bodySolverArrayMutex.lock();
// now that we have the lock, check again
if (m_fixedBodyId < 0)
{
m_fixedBodyId = m_tmpSolverBodyPool.size();
btSolverBody& fixedBody = m_tmpSolverBodyPool.expand();
initSolverBody(&fixedBody, 0, timeStep);
}
m_bodySolverArrayMutex.unlock();
}
solverBodyId = m_fixedBodyId;
}
btAssert(solverBodyId >= 0 && solverBodyId < m_tmpSolverBodyPool.size());
return solverBodyId;
}
void btSequentialImpulseConstraintSolverMt::internalCollectContactManifoldCachedInfo(btContactManifoldCachedInfo* cachedInfoArray, btPersistentManifold** manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("internalCollectContactManifoldCachedInfo");
for (int i = 0; i < numManifolds; ++i)
{
btContactManifoldCachedInfo* cachedInfo = &cachedInfoArray[i];
btPersistentManifold* manifold = manifoldPtr[i];
btCollisionObject* colObj0 = (btCollisionObject*)manifold->getBody0();
btCollisionObject* colObj1 = (btCollisionObject*)manifold->getBody1();
int solverBodyIdA = getOrInitSolverBodyThreadsafe(*colObj0, infoGlobal.m_timeStep);
int solverBodyIdB = getOrInitSolverBodyThreadsafe(*colObj1, infoGlobal.m_timeStep);
cachedInfo->solverBodyIds[0] = solverBodyIdA;
cachedInfo->solverBodyIds[1] = solverBodyIdB;
cachedInfo->numTouchingContacts = 0;
btSolverBody* solverBodyA = &m_tmpSolverBodyPool[solverBodyIdA];
btSolverBody* solverBodyB = &m_tmpSolverBodyPool[solverBodyIdB];
// A contact manifold between 2 static object should not exist!
// check the collision flags of your objects if this assert fires.
// Incorrectly set collision object flags can degrade performance in various ways.
btAssert(!m_tmpSolverBodyPool[solverBodyIdA].m_invMass.isZero() || !m_tmpSolverBodyPool[solverBodyIdB].m_invMass.isZero());
int iContact = 0;
for (int j = 0; j < manifold->getNumContacts(); j++)
{
btManifoldPoint& cp = manifold->getContactPoint(j);
if (cp.getDistance() <= manifold->getContactProcessingThreshold())
{
cachedInfo->contactPoints[iContact] = &cp;
cachedInfo->contactHasRollingFriction[iContact] = (cp.m_combinedRollingFriction > 0.f);
iContact++;
}
}
cachedInfo->numTouchingContacts = iContact;
}
}
struct CollectContactManifoldCachedInfoLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
btSequentialImpulseConstraintSolverMt::btContactManifoldCachedInfo* m_cachedInfoArray;
btPersistentManifold** m_manifoldPtr;
const btContactSolverInfo* m_infoGlobal;
CollectContactManifoldCachedInfoLoop(btSequentialImpulseConstraintSolverMt* solver, btSequentialImpulseConstraintSolverMt::btContactManifoldCachedInfo* cachedInfoArray, btPersistentManifold** manifoldPtr, const btContactSolverInfo& infoGlobal)
{
m_solver = solver;
m_cachedInfoArray = cachedInfoArray;
m_manifoldPtr = manifoldPtr;
m_infoGlobal = &infoGlobal;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
m_solver->internalCollectContactManifoldCachedInfo(m_cachedInfoArray + iBegin, m_manifoldPtr + iBegin, iEnd - iBegin, *m_infoGlobal);
}
};
void btSequentialImpulseConstraintSolverMt::internalAllocContactConstraints(const btContactManifoldCachedInfo* cachedInfoArray, int numManifolds)
{
BT_PROFILE("internalAllocContactConstraints");
// possibly parallel part
for (int iManifold = 0; iManifold < numManifolds; ++iManifold)
{
const btContactManifoldCachedInfo& cachedInfo = cachedInfoArray[iManifold];
int contactIndex = cachedInfo.contactIndex;
int frictionIndex = contactIndex * m_numFrictionDirections;
int rollingFrictionIndex = cachedInfo.rollingFrictionIndex;
for (int i = 0; i < cachedInfo.numTouchingContacts; i++)
{
btSolverConstraint& contactConstraint = m_tmpSolverContactConstraintPool[contactIndex];
contactConstraint.m_solverBodyIdA = cachedInfo.solverBodyIds[0];
contactConstraint.m_solverBodyIdB = cachedInfo.solverBodyIds[1];
contactConstraint.m_originalContactPoint = cachedInfo.contactPoints[i];
// allocate the friction constraints
contactConstraint.m_frictionIndex = frictionIndex;
for (int iDir = 0; iDir < m_numFrictionDirections; ++iDir)
{
btSolverConstraint& frictionConstraint = m_tmpSolverContactFrictionConstraintPool[frictionIndex];
frictionConstraint.m_frictionIndex = contactIndex;
frictionIndex++;
}
// allocate rolling friction constraints
if (cachedInfo.contactHasRollingFriction[i])
{
m_rollingFrictionIndexTable[contactIndex] = rollingFrictionIndex;
// allocate 3 (although we may use only 2 sometimes)
for (int i = 0; i < 3; i++)
{
m_tmpSolverContactRollingFrictionConstraintPool[rollingFrictionIndex].m_frictionIndex = contactIndex;
rollingFrictionIndex++;
}
}
else
{
// indicate there is no rolling friction for this contact point
m_rollingFrictionIndexTable[contactIndex] = -1;
}
contactIndex++;
}
}
}
struct AllocContactConstraintsLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
const btSequentialImpulseConstraintSolverMt::btContactManifoldCachedInfo* m_cachedInfoArray;
AllocContactConstraintsLoop(btSequentialImpulseConstraintSolverMt* solver, btSequentialImpulseConstraintSolverMt::btContactManifoldCachedInfo* cachedInfoArray)
{
m_solver = solver;
m_cachedInfoArray = cachedInfoArray;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
m_solver->internalAllocContactConstraints(m_cachedInfoArray + iBegin, iEnd - iBegin);
}
};
void btSequentialImpulseConstraintSolverMt::allocAllContactConstraints(btPersistentManifold** manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("allocAllContactConstraints");
btAlignedObjectArray<btContactManifoldCachedInfo> cachedInfoArray; // = m_manifoldCachedInfoArray;
cachedInfoArray.resizeNoInitialize(numManifolds);
if (/* DISABLES CODE */ (false))
{
// sequential
internalCollectContactManifoldCachedInfo(&cachedInfoArray[0], manifoldPtr, numManifolds, infoGlobal);
}
else
{
// may alter ordering of bodies which affects determinism
CollectContactManifoldCachedInfoLoop loop(this, &cachedInfoArray[0], manifoldPtr, infoGlobal);
int grainSize = 200;
btParallelFor(0, numManifolds, grainSize, loop);
}
{
// serial part
int numContacts = 0;
int numRollingFrictionConstraints = 0;
for (int iManifold = 0; iManifold < numManifolds; ++iManifold)
{
btContactManifoldCachedInfo& cachedInfo = cachedInfoArray[iManifold];
cachedInfo.contactIndex = numContacts;
cachedInfo.rollingFrictionIndex = numRollingFrictionConstraints;
numContacts += cachedInfo.numTouchingContacts;
for (int i = 0; i < cachedInfo.numTouchingContacts; ++i)
{
if (cachedInfo.contactHasRollingFriction[i])
{
numRollingFrictionConstraints += 3;
}
}
}
{
BT_PROFILE("allocPools");
if (m_tmpSolverContactConstraintPool.capacity() < numContacts)
{
// if we need to reallocate, reserve some extra so we don't have to reallocate again next frame
int extraReserve = numContacts / 16;
m_tmpSolverContactConstraintPool.reserve(numContacts + extraReserve);
m_rollingFrictionIndexTable.reserve(numContacts + extraReserve);
m_tmpSolverContactFrictionConstraintPool.reserve((numContacts + extraReserve) * m_numFrictionDirections);
m_tmpSolverContactRollingFrictionConstraintPool.reserve(numRollingFrictionConstraints + extraReserve);
}
m_tmpSolverContactConstraintPool.resizeNoInitialize(numContacts);
m_rollingFrictionIndexTable.resizeNoInitialize(numContacts);
m_tmpSolverContactFrictionConstraintPool.resizeNoInitialize(numContacts * m_numFrictionDirections);
m_tmpSolverContactRollingFrictionConstraintPool.resizeNoInitialize(numRollingFrictionConstraints);
}
}
{
AllocContactConstraintsLoop loop(this, &cachedInfoArray[0]);
int grainSize = 200;
btParallelFor(0, numManifolds, grainSize, loop);
}
}
void btSequentialImpulseConstraintSolverMt::convertContacts(btPersistentManifold** manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal)
{
if (!m_useBatching)
{
btSequentialImpulseConstraintSolver::convertContacts(manifoldPtr, numManifolds, infoGlobal);
return;
}
BT_PROFILE("convertContacts");
if (numManifolds > 0)
{
if (m_fixedBodyId < 0)
{
m_fixedBodyId = m_tmpSolverBodyPool.size();
btSolverBody& fixedBody = m_tmpSolverBodyPool.expand();
initSolverBody(&fixedBody, 0, infoGlobal.m_timeStep);
}
allocAllContactConstraints(manifoldPtr, numManifolds, infoGlobal);
if (m_useBatching)
{
setupBatchedContactConstraints();
}
setupAllContactConstraints(infoGlobal);
}
}
void btSequentialImpulseConstraintSolverMt::internalInitMultipleJoints(btTypedConstraint** constraints, int iBegin, int iEnd)
{
BT_PROFILE("internalInitMultipleJoints");
for (int i = iBegin; i < iEnd; i++)
{
btTypedConstraint* constraint = constraints[i];
btTypedConstraint::btConstraintInfo1& info1 = m_tmpConstraintSizesPool[i];
if (constraint->isEnabled())
{
constraint->buildJacobian();
constraint->internalSetAppliedImpulse(0.0f);
btJointFeedback* fb = constraint->getJointFeedback();
if (fb)
{
fb->m_appliedForceBodyA.setZero();
fb->m_appliedTorqueBodyA.setZero();
fb->m_appliedForceBodyB.setZero();
fb->m_appliedTorqueBodyB.setZero();
}
constraint->getInfo1(&info1);
}
else
{
info1.m_numConstraintRows = 0;
info1.nub = 0;
}
}
}
struct InitJointsLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
btTypedConstraint** m_constraints;
InitJointsLoop(btSequentialImpulseConstraintSolverMt* solver, btTypedConstraint** constraints)
{
m_solver = solver;
m_constraints = constraints;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
m_solver->internalInitMultipleJoints(m_constraints, iBegin, iEnd);
}
};
void btSequentialImpulseConstraintSolverMt::internalConvertMultipleJoints(const btAlignedObjectArray<JointParams>& jointParamsArray, btTypedConstraint** constraints, int iBegin, int iEnd, const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("internalConvertMultipleJoints");
for (int i = iBegin; i < iEnd; ++i)
{
const JointParams& jointParams = jointParamsArray[i];
int currentRow = jointParams.m_solverConstraint;
if (currentRow != -1)
{
const btTypedConstraint::btConstraintInfo1& info1 = m_tmpConstraintSizesPool[i];
btAssert(currentRow < m_tmpSolverNonContactConstraintPool.size());
btAssert(info1.m_numConstraintRows > 0);
btSolverConstraint* currentConstraintRow = &m_tmpSolverNonContactConstraintPool[currentRow];
btTypedConstraint* constraint = constraints[i];
convertJoint(currentConstraintRow, constraint, info1, jointParams.m_solverBodyA, jointParams.m_solverBodyB, infoGlobal);
}
}
}
struct ConvertJointsLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
const btAlignedObjectArray<btSequentialImpulseConstraintSolverMt::JointParams>& m_jointParamsArray;
btTypedConstraint** m_srcConstraints;
const btContactSolverInfo& m_infoGlobal;
ConvertJointsLoop(btSequentialImpulseConstraintSolverMt* solver,
const btAlignedObjectArray<btSequentialImpulseConstraintSolverMt::JointParams>& jointParamsArray,
btTypedConstraint** srcConstraints,
const btContactSolverInfo& infoGlobal) : m_jointParamsArray(jointParamsArray),
m_infoGlobal(infoGlobal)
{
m_solver = solver;
m_srcConstraints = srcConstraints;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
m_solver->internalConvertMultipleJoints(m_jointParamsArray, m_srcConstraints, iBegin, iEnd, m_infoGlobal);
}
};
void btSequentialImpulseConstraintSolverMt::convertJoints(btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal)
{
if (!m_useBatching)
{
btSequentialImpulseConstraintSolver::convertJoints(constraints, numConstraints, infoGlobal);
return;
}
BT_PROFILE("convertJoints");
bool parallelJointSetup = true;
m_tmpConstraintSizesPool.resizeNoInitialize(numConstraints);
if (parallelJointSetup)
{
InitJointsLoop loop(this, constraints);
int grainSize = 40;
btParallelFor(0, numConstraints, grainSize, loop);
}
else
{
internalInitMultipleJoints(constraints, 0, numConstraints);
}
int totalNumRows = 0;
btAlignedObjectArray<JointParams> jointParamsArray;
jointParamsArray.resizeNoInitialize(numConstraints);
//calculate the total number of contraint rows
for (int i = 0; i < numConstraints; i++)
{
btTypedConstraint* constraint = constraints[i];
JointParams& params = jointParamsArray[i];
const btTypedConstraint::btConstraintInfo1& info1 = m_tmpConstraintSizesPool[i];
if (info1.m_numConstraintRows)
{
params.m_solverConstraint = totalNumRows;
params.m_solverBodyA = getOrInitSolverBody(constraint->getRigidBodyA(), infoGlobal.m_timeStep);
params.m_solverBodyB = getOrInitSolverBody(constraint->getRigidBodyB(), infoGlobal.m_timeStep);
}
else
{
params.m_solverConstraint = -1;
}
totalNumRows += info1.m_numConstraintRows;
}
m_tmpSolverNonContactConstraintPool.resizeNoInitialize(totalNumRows);
///setup the btSolverConstraints
if (parallelJointSetup)
{
ConvertJointsLoop loop(this, jointParamsArray, constraints, infoGlobal);
int grainSize = 20;
btParallelFor(0, numConstraints, grainSize, loop);
}
else
{
internalConvertMultipleJoints(jointParamsArray, constraints, 0, numConstraints, infoGlobal);
}
setupBatchedJointConstraints();
}
void btSequentialImpulseConstraintSolverMt::internalConvertBodies(btCollisionObject** bodies, int iBegin, int iEnd, const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("internalConvertBodies");
for (int i = iBegin; i < iEnd; i++)
{
btCollisionObject* obj = bodies[i];
obj->setCompanionId(i);
btSolverBody& solverBody = m_tmpSolverBodyPool[i];
initSolverBody(&solverBody, obj, infoGlobal.m_timeStep);
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getInvMass())
{
btVector3 gyroForce(0, 0, 0);
if (body->getFlags() & BT_ENABLE_GYROSCOPIC_FORCE_EXPLICIT)
{
gyroForce = body->computeGyroscopicForceExplicit(infoGlobal.m_maxGyroscopicForce);
solverBody.m_externalTorqueImpulse -= gyroForce * body->getInvInertiaTensorWorld() * infoGlobal.m_timeStep;
}
if (body->getFlags() & BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_WORLD)
{
gyroForce = body->computeGyroscopicImpulseImplicit_World(infoGlobal.m_timeStep);
solverBody.m_externalTorqueImpulse += gyroForce;
}
if (body->getFlags() & BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY)
{
gyroForce = body->computeGyroscopicImpulseImplicit_Body(infoGlobal.m_timeStep);
solverBody.m_externalTorqueImpulse += gyroForce;
}
}
}
}
struct ConvertBodiesLoop : public btIParallelForBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
btCollisionObject** m_bodies;
int m_numBodies;
const btContactSolverInfo& m_infoGlobal;
ConvertBodiesLoop(btSequentialImpulseConstraintSolverMt* solver,
btCollisionObject** bodies,
int numBodies,
const btContactSolverInfo& infoGlobal) : m_infoGlobal(infoGlobal)
{
m_solver = solver;
m_bodies = bodies;
m_numBodies = numBodies;
}
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
m_solver->internalConvertBodies(m_bodies, iBegin, iEnd, m_infoGlobal);
}
};
void btSequentialImpulseConstraintSolverMt::convertBodies(btCollisionObject** bodies, int numBodies, const btContactSolverInfo& infoGlobal)
{
BT_PROFILE("convertBodies");
m_kinematicBodyUniqueIdToSolverBodyTable.resize(0);
m_tmpSolverBodyPool.resizeNoInitialize(numBodies + 1);
m_fixedBodyId = numBodies;
{
btSolverBody& fixedBody = m_tmpSolverBodyPool[m_fixedBodyId];
initSolverBody(&fixedBody, NULL, infoGlobal.m_timeStep);
}
bool parallelBodySetup = true;
if (parallelBodySetup)
{
ConvertBodiesLoop loop(this, bodies, numBodies, infoGlobal);
int grainSize = 40;
btParallelFor(0, numBodies, grainSize, loop);
}
else
{
internalConvertBodies(bodies, 0, numBodies, infoGlobal);
}
}
btScalar btSequentialImpulseConstraintSolverMt::solveGroupCacheFriendlySetup(
btCollisionObject** bodies,
int numBodies,
btPersistentManifold** manifoldPtr,
int numManifolds,
btTypedConstraint** constraints,
int numConstraints,
const btContactSolverInfo& infoGlobal,
btIDebugDraw* debugDrawer)
{
m_numFrictionDirections = (infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS) ? 2 : 1;
m_useBatching = false;
if (numManifolds >= s_minimumContactManifoldsForBatching &&
(s_allowNestedParallelForLoops || !btThreadsAreRunning()))
{
m_useBatching = true;
m_batchedContactConstraints.m_debugDrawer = debugDrawer;
m_batchedJointConstraints.m_debugDrawer = debugDrawer;
}
btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(bodies,
numBodies,
manifoldPtr,
numManifolds,
constraints,
numConstraints,
infoGlobal,
debugDrawer);
return 0.0f;
}
btScalar btSequentialImpulseConstraintSolverMt::resolveMultipleContactSplitPenetrationImpulseConstraints(const btAlignedObjectArray<int>& consIndices, int batchBegin, int batchEnd)
{
btScalar leastSquaresResidual = 0.f;
for (int iiCons = batchBegin; iiCons < batchEnd; ++iiCons)
{
int iCons = consIndices[iiCons];
const btSolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[iCons];
btSolverBody& bodyA = m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA];
btSolverBody& bodyB = m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB];
btScalar residual = resolveSplitPenetrationImpulse(bodyA, bodyB, solveManifold);
leastSquaresResidual += residual * residual;
}
return leastSquaresResidual;
}
struct ContactSplitPenetrationImpulseSolverLoop : public btIParallelSumBody
{
btSequentialImpulseConstraintSolverMt* m_solver;
const btBatchedConstraints* m_bc;
ContactSplitPenetrationImpulseSolverLoop(btSequentialImpulseConstraintSolverMt* solver, const btBatchedConstraints* bc)
{
m_solver = solver;
m_bc = bc;
}
btScalar sumLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
BT_PROFILE("ContactSplitPenetrationImpulseSolverLoop");
btScalar sum = 0;
for (int iBatch = iBegin; iBatch < iEnd; ++iBatch)
{
const btBatchedConstraints::Range& batch = m_bc->m_batches[iBatch];
sum += m_solver->resolveMultipleContactSplitPenetrationImpulseConstraints(m_bc->m_constraintIndices, batch.begin, batch.end);
}
return sum;
}
};
void btSequentialImpulseConstraintSolverMt::solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
{
BT_PROFILE("solveGroupCacheFriendlySplitImpulseIterations");
if (infoGlobal.m_splitImpulse)
{
for (int iteration = 0; iteration < infoGlobal.m_numIterations; iteration++)
{
btScalar leastSquaresResidual = 0.f;
if (m_useBatching)
{
const btBatchedConstraints& batchedCons = m_batchedContactConstraints;
ContactSplitPenetrationImpulseSolverLoop loop(this, &batchedCons);
btScalar leastSquaresResidual = 0.f;
for (int iiPhase = 0; iiPhase < batchedCons.m_phases.size(); ++iiPhase)
{
int iPhase = batchedCons.m_phaseOrder[iiPhase];
const btBatchedConstraints::Range& phase = batchedCons.m_phases[iPhase];
int grainSize = batchedCons.m_phaseGrainSize[iPhase];
leastSquaresResidual += btParallelSum(phase.begin, phase.end, grainSize, loop);
}
}
else
{
// non-batched
leastSquaresResidual = resolveMultipleContactSplitPenetrationImpulseConstraints(m_orderTmpConstraintPool, 0, m_tmpSolverContactConstraintPool.size());
}
if (leastSquaresResidual <= infoGlobal.m_leastSquaresResidualThreshold || iteration >= (infoGlobal.m_numIterations - 1))
{
#ifdef VERBOSE_RESIDUAL_PRINTF
printf("residual = %f at iteration #%d\n", leastSquaresResidual, iteration);
#endif
break;
}
}
}
}
btScalar btSequentialImpulseConstraintSolverMt::solveSingleIteration(int iteration, btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
{
if (!m_useBatching)
{
return btSequentialImpulseConstraintSolver::solveSingleIteration(iteration, bodies, numBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
}
BT_PROFILE("solveSingleIterationMt");
btScalar leastSquaresResidual = 0.f;
if (infoGlobal.m_solverMode & SOLVER_RANDMIZE_ORDER)
{
if (1) // uncomment this for a bit less random ((iteration & 7) == 0)
{
randomizeConstraintOrdering(iteration, infoGlobal.m_numIterations);
}
}
{
///solve all joint constraints
leastSquaresResidual += resolveAllJointConstraints(iteration);
if (iteration < infoGlobal.m_numIterations)
{
// this loop is only used for cone-twist constraints,
// it would be nice to skip this loop if none of the constraints need it
if (m_useObsoleteJointConstraints)
{
for (int j = 0; j < numConstraints; j++)
{
if (constraints[j]->isEnabled())
{
int bodyAid = getOrInitSolverBody(constraints[j]->getRigidBodyA(), infoGlobal.m_timeStep);
int bodyBid = getOrInitSolverBody(constraints[j]->getRigidBodyB(), infoGlobal.m_timeStep);
btSolverBody& bodyA = m_tmpSolverBodyPool[bodyAid];
btSolverBody& bodyB = m_tmpSolverBodyPool[bodyBid];
constraints[j]->solveConstraintObsolete(bodyA, bodyB, infoGlobal.m_timeStep);
}
}
}
if (infoGlobal.m_solverMode & SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS)
{
// solve all contact, contact-friction, and rolling friction constraints interleaved
leastSquaresResidual += resolveAllContactConstraintsInterleaved();
}
else //SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS
{
// don't interleave them
// solve all contact constraints
leastSquaresResidual += resolveAllContactConstraints();
// solve all contact friction constraints
leastSquaresResidual += resolveAllContactFrictionConstraints();
// solve all rolling friction constraints
leastSquaresResidual += resolveAllRollingFrictionConstraints();
}
}
}
return leastSquaresResidual;