-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtcSoftIkSolverNew.cpp
1080 lines (899 loc) · 31.6 KB
/
tcSoftIkSolverNew.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
#include "tcSoftIkSolverNew.h"
#include <maya/MFnIkHandle.h>
#include <maya/MFnTransform.h>
#include <maya/MMatrix.h>
#include <maya/MQuaternion.h>
#include <maya/MDoubleArray.h>
#include <maya/MIkHandleGroup.h>
#include <maya/MFnIkHandle.h>
#include <maya/MFnIkEffector.h>
#include <maya/MFnIkJoint.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MRampAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MTransformationMatrix.h>
#include <maya/MPlugArray.h>
#include <maya/MGlobal.h>
#include <maya/MEulerRotation.h>
#include <math.h>
#define kFloatEpsilon 1.0e-5F
#define kDoubleEpsilon 1.0e-10
#define kDoubleEpsilonSqr 1.0e-20
#define kExtendedEpsilon kDoubleEpsilon
MTypeId TcSoftIkSolverNodeNew::id(0x00122C00);
inline bool equivalent(double x, double y, double fudge = kDoubleEpsilon)
{
return ((x > y) ? (x - y <= fudge) : (y - x <= fudge));
}
bool isParallel(const MVector &thisVector, const MVector &otherVector, double tolerance = kDoubleEpsilon)
{
MVector v1, v2;
v1 = thisVector.normal();
v2 = otherVector.normal();
double dotPrd = v1*v2;
return (::equivalent(fabs(dotPrd), (double) 1.0, tolerance));
}
MVector getBoneScales(MFnIkJoint& jointFn)
{
MPlug sxPlug = jointFn.findPlug("sx");
MPlug syPlug = jointFn.findPlug("sy");
MPlug szPlug = jointFn.findPlug("sz");
double sxValue = sxPlug.asDouble();
double syValue = syPlug.asDouble();
double szValue = szPlug.asDouble();
return MVector(sxValue, syValue, szValue);
}
TcSoftIkSolverNodeNew::TcSoftIkSolverNodeNew()
: MPxIkSolverNode()
{
setSingleChainOnly(true);
setUniqueSolution(true);
setPositionOnly(true);
setMaxIterations(true);
}
TcSoftIkSolverNodeNew::~TcSoftIkSolverNodeNew() {}
void* TcSoftIkSolverNodeNew::creator()
{
return new TcSoftIkSolverNodeNew;
}
MStatus TcSoftIkSolverNodeNew::initialize()
{
return MS::kSuccess;
}
MString TcSoftIkSolverNodeNew::solverTypeName() const
{
return MString(kSolverType);
}
MStatus setPointValue(MObject attr, MObject node, MVector& value)
{
MPlug parentPlug(node, attr);
if (parentPlug.isNull() && (parentPlug.numChildren()!=3))
return MS::kFailure;
MPlug xPlug = parentPlug.child(0);
MPlug yPlug = parentPlug.child(1);
MPlug zPlug = parentPlug.child(2);
xPlug.setValue(value.x);
yPlug.setValue(value.y);
zPlug.setValue(value.z);
return MS::kSuccess;
}
MVector getPointValue(MPlug parentPlug)
{
MVector outVec;
if (parentPlug.isNull() && (parentPlug.numChildren() != 3))
return outVec;
MPlug xPlug = parentPlug.child(0);
MPlug yPlug = parentPlug.child(1);
MPlug zPlug = parentPlug.child(2);
xPlug.getValue(outVec.x);
yPlug.getValue(outVec.y);
zPlug.getValue(outVec.z);
return outVec;
}
MTransformationMatrix::RotationOrder getRotationOrder(short value)
{
switch (value)
{
case 0:
return MTransformationMatrix::kInvalid;
case 1:
return MTransformationMatrix::kXYZ; //!< \nop
case 2:
return MTransformationMatrix::kYZX; //!< \nop
case 3:
return MTransformationMatrix::kZXY; //!< \nop
case 4:
return MTransformationMatrix::kXZY; //!< \nop
case 5:
return MTransformationMatrix::kYXZ; //!< \nop
case 6:
return MTransformationMatrix::kZYX; //!< \nop
case 7:
return MTransformationMatrix::kLast; //!< \nop
default:
return MTransformationMatrix::kInvalid;
}
}
MEulerRotation::RotationOrder getEulerRotationOrder(short value)
{
switch (value)
{
case 0:
return MEulerRotation::kXYZ;
case 1:
return MEulerRotation::kXYZ; //!< \nop
case 2:
return MEulerRotation::kYZX; //!< \nop
case 3:
return MEulerRotation::kZXY; //!< \nop
case 4:
return MEulerRotation::kXZY; //!< \nop
case 5:
return MEulerRotation::kYXZ; //!< \nop
case 6:
return MEulerRotation::kZYX; //!< \nop
default:
return MEulerRotation::kXYZ;
}
}
short getShortRotationOrder(MTransformationMatrix::RotationOrder value)
{
if (MTransformationMatrix::kInvalid == value)
return 0;
if (MTransformationMatrix::kXYZ == value)
return 1;
if (MTransformationMatrix::kYZX == value)
return 2;
if (MTransformationMatrix::kZXY == value)
return 3;
if (MTransformationMatrix::kXZY == value)
return 4;
if (MTransformationMatrix::kYXZ == value)
return 5;
if (MTransformationMatrix::kZYX == value)
return 6;
if (MTransformationMatrix::kLast == value)
return 7;
return MTransformationMatrix::kInvalid;
}
MMatrix getMidRestLocalMatrix(MFnIkHandle& handleFn, MVector parentScale = MVector(1.0, 1.0, 1.0))
{
MMatrix outMtx;
MPlug plug = handleFn.findPlug("mjs");
MVector scale = getPointValue(plug);
plug = handleFn.findPlug("mjr");
MVector rotation = getPointValue(plug);
short value = 1;
plug = handleFn.findPlug("mjro");
plug.getValue(value);
MEulerRotation::RotationOrder rotOrder = getEulerRotationOrder(value);
plug = handleFn.findPlug("mjt");
MVector translation = getPointValue(plug);
MMatrix parentInverScale;
parentInverScale[0][0] = parentScale.x;
parentInverScale[1][1] = parentScale.y;
parentInverScale[2][2] = parentScale.z;
parentInverScale = parentInverScale.inverse();
plug = handleFn.findPlug("mjo");
MVector jointOrient = getPointValue(plug);
plug = handleFn.findPlug("mjra");
MVector rotateAxis = getPointValue(plug);
MTransformationMatrix mr1;
mr1.rotateBy(MEulerRotation(rotation, rotOrder), MSpace::kTransform);
MTransformationMatrix ms1;
double scaleVec[3] = { scale.x, scale.y, scale.z };
ms1.setScale(scaleVec, MSpace::kTransform);
MTransformationMatrix mt1;
mt1.setTranslation(translation, MSpace::kTransform);
MTransformationMatrix mjo1;
mjo1.rotateBy(MEulerRotation(jointOrient, rotOrder), MSpace::kTransform);
MTransformationMatrix mra1;
mra1.rotateBy(MEulerRotation(rotateAxis, rotOrder), MSpace::kTransform);
outMtx = ms1.asMatrix() * mra1.asMatrix() * mr1.asMatrix() * mjo1.asMatrix() * parentInverScale * mt1.asMatrix();
return outMtx;
}
MMatrix getEndRestLocalMatrix(MFnIkHandle& handleFn)
{
MMatrix outMtx;
MPlug plug = handleFn.findPlug("ejs");
MVector scale = getPointValue(plug);
plug = handleFn.findPlug("ejr");
MVector rotation = getPointValue(plug);
short value = 1;
plug = handleFn.findPlug("ejro");
plug.getValue(value);
MEulerRotation::RotationOrder rotOrder = getEulerRotationOrder(value);
plug = handleFn.findPlug("ejt");
MVector translation = getPointValue(plug);
plug = handleFn.findPlug("ejis");
MVector parentScale = getPointValue(plug);
MMatrix parentInverScale;
parentInverScale[0][0] = parentScale.x;
parentInverScale[1][1] = parentScale.y;
parentInverScale[2][2] = parentScale.z;
parentInverScale = parentInverScale.inverse();
plug = handleFn.findPlug("ejo");
MVector jointOrient = getPointValue(plug);
plug = handleFn.findPlug("ejra");
MVector rotateAxis = getPointValue(plug);
MTransformationMatrix mr1;
mr1.rotateBy(MEulerRotation(rotation, rotOrder), MSpace::kTransform);
MTransformationMatrix ms1;
double scaleVec[3] = { scale.x, scale.y, scale.z };
ms1.setScale(scaleVec, MSpace::kTransform);
MTransformationMatrix mt1;
mt1.setTranslation(translation, MSpace::kTransform);
MTransformationMatrix mjo1;
mjo1.rotateBy(MEulerRotation(jointOrient, rotOrder), MSpace::kTransform);
MTransformationMatrix mra1;
mra1.rotateBy(MEulerRotation(rotateAxis, rotOrder), MSpace::kTransform);
outMtx = ms1.asMatrix() * mra1.asMatrix() * mr1.asMatrix() * mjo1.asMatrix() * parentInverScale * mt1.asMatrix();
return outMtx;
}
MStatus TcSoftIkSolverNodeNew::preSolve()
{
MStatus status;
setRotatePlane(true);
setSingleChainOnly(true);
setPositionOnly(true);
MIkHandleGroup *handleGrp = handleGroup();
MObject handle = handleGrp->handle(0);
MDagPath handlePath = MDagPath::getAPathTo(handle);
MFnIkHandle handleFn(handlePath, &status);
MDagPath effectorPath;
handleFn.getEffector(effectorPath);
MFnIkEffector effectorFn(effectorPath);
effectorPath.pop();
MFnIkJoint midJointFn(effectorPath);
MDagPath startJointPath;
handleFn.getStartJoint(startJointPath);
MFnIkJoint startJointFn(startJointPath);
MDagPath lastJointPath = getEndJoint(handlePath);
MFnIkJoint endJointFn(lastJointPath);
if (!handleFn.hasAttribute("asft", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("activateSoft", "asft", MFnNumericData::kBoolean, true);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("sftd", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("softDistance", "sftd", MFnNumericData::kDouble, 1.0);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("astc", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("activateStretch", "astc", MFnNumericData::kDouble, 1.0);
fnAttr.setMin(0.0);
fnAttr.setMax(1.0);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("mjsld", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("midJointSlide", "mjsld", MFnNumericData::kDouble, 0.0);
fnAttr.setMax(1.0);
fnAttr.setMin(-1.0);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
bool initRestPose = false;
if (!handleFn.hasAttribute("mjlockw", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("midJointLockWeight", "mjlockw", MFnNumericData::kDouble, 0.0);
fnAttr.setMin(0.0);
fnAttr.setMax(1.0);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("mjlockp", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointLockPosition", "mjlockp");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("plLock", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("usePoleVectorAsLockPosition", "plLock", MFnNumericData::kBoolean, false);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
}
if (!handleFn.hasAttribute("mjs", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointScale", "mjs");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double scale[3] = { 0.0, 0.0, 0.0 };
midJointFn.getScale(scale);
setPointValue(attr, handleFn.object(), MVector(scale[0], scale[1], scale[2]));
}
MTransformationMatrix::RotationOrder rotateOrder = MTransformationMatrix::kXYZ;
if (!handleFn.hasAttribute("mjro", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("midJointRotateOrder", "mjro", MFnNumericData::kShort, 1);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MPlug plug(handleFn.object(), attr);
rotateOrder = midJointFn.rotationOrder();
plug.setValue(getShortRotationOrder(rotateOrder));
}
if (!handleFn.hasAttribute("mjr", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointRotate", "mjr");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double rotate[3] = { 0.0, 0.0, 0.0 };
midJointFn.getRotation(rotate, rotateOrder);
setPointValue(attr, handleFn.object(), MVector(rotate[0], rotate[1], rotate[2]));
}
if (!handleFn.hasAttribute("mjt", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointTranslate", "mjt");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
setPointValue(attr, handleFn.object(), midJointFn.getTranslation(MSpace::kTransform));
}
if (!handleFn.hasAttribute("mjo", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointOrient", "mjo");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MQuaternion orientation;
midJointFn.getOrientation(orientation);
MEulerRotation rot = orientation.asEulerRotation();
setPointValue(attr, handleFn.object(), MVector(rot.x, rot.y, rot.z));
}
if (!handleFn.hasAttribute("mjra", &status))
{
MFnNumericAttribute fnAttr;
MFnCompoundAttribute cAttr;
MObject attr = fnAttr.createPoint("midJointRotateAxis", "mjra");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MQuaternion orientation;
midJointFn.getScaleOrientation(orientation);
MEulerRotation rot = orientation.asEulerRotation();
setPointValue(attr, handleFn.object(), MVector(rot.x, rot.y, rot.z));
}
if (!handleFn.hasAttribute("mjis", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("midJointParentInverseScale", "mjis");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double scale[3] = { 0.0, 0.0, 0.0 };
startJointFn.getScale(scale);
setPointValue(attr, handleFn.object(), MVector(scale[0], scale[1], scale[2]));
}
// end joint
if (!handleFn.hasAttribute("ejs", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("endJointScale", "ejs");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double scale[3] = { 0.0, 0.0, 0.0 };
endJointFn.getScale(scale);
setPointValue(attr, handleFn.object(), MVector(scale[0], scale[1], scale[2]));
}
if (!handleFn.hasAttribute("ejro", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.create("endJointRotateOrder", "ejro", MFnNumericData::kShort, 1);
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MPlug plug(handleFn.object(), attr);
rotateOrder = endJointFn.rotationOrder();
plug.setValue(getShortRotationOrder(rotateOrder));
}
if (!handleFn.hasAttribute("ejr", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("endJointRotate", "ejr");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double rotate[3] = { 0.0, 0.0, 0.0 };
endJointFn.getRotation(rotate, rotateOrder);
setPointValue(attr, handleFn.object(), MVector(rotate[0], rotate[1], rotate[2]));
}
if (!handleFn.hasAttribute("ejt", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("endJointTranslate", "ejt");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
setPointValue(attr, handleFn.object(), endJointFn.getTranslation(MSpace::kTransform));
}
if (!handleFn.hasAttribute("ejo", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("endJointOrient", "ejo");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MQuaternion orientation;
endJointFn.getOrientation(orientation);
MEulerRotation rot = orientation.asEulerRotation();
setPointValue(attr, handleFn.object(), MVector(rot.x, rot.y, rot.z));
}
if (!handleFn.hasAttribute("ejra", &status))
{
MFnNumericAttribute fnAttr;
MFnCompoundAttribute cAttr;
MObject attr = fnAttr.createPoint("endJointRotateAxis", "ejra");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
MQuaternion orientation;
endJointFn.getScaleOrientation(orientation);
MEulerRotation rot = orientation.asEulerRotation();
setPointValue(attr, handleFn.object(), MVector(rot.x, rot.y, rot.z));
}
if (!handleFn.hasAttribute("ejis", &status))
{
MFnNumericAttribute fnAttr;
MObject attr = fnAttr.createPoint("endJointParentInverseScale", "ejis");
fnAttr.setKeyable(1);
fnAttr.setWritable(1);
fnAttr.setStorable(1);
fnAttr.setReadable(1);
handleFn.addAttribute(attr, MFnDependencyNode::kLocalDynamicAttr);
double scale[3] = { 0.0, 0.0, 0.0 };
midJointFn.getScale(scale);
setPointValue(attr, handleFn.object(), MVector(scale[0], scale[1], scale[2]));
}
return MS::kSuccess;
}
MStatus TcSoftIkSolverNodeNew::doSolve()
{
MStatus stat;
// Handle Group
//
MIkHandleGroup * handle_group = handleGroup();
if (NULL == handle_group) {
return MS::kFailure;
}
// Handle
//
// For single chain types of solvers, get the 0th handle.
// Single chain solvers are solvers which act on one handle only,
// i.e. the handle group for a single chain solver
// has only one handle
//
MObject handle = handle_group->handle(0);
MDagPath handlePath = MDagPath::getAPathTo(handle);
MFnIkHandle handleFn(handlePath, &stat);
// Effector
//
MDagPath effectorPath;
handleFn.getEffector(effectorPath);
MFnIkEffector effectorFn(effectorPath);
// Mid Joint
//
effectorPath.pop();
MFnIkJoint midJointFn(effectorPath);
// Start Joint
//
MDagPath startJointPath;
handleFn.getStartJoint(startJointPath);
MFnIkJoint startJointFn(startJointPath);
MDagPath lastJointPath = getEndJoint(handlePath);
MFnIkJoint endJointFn(lastJointPath);
MPlug sftdPlug = handleFn.findPlug("sftd");
double m_softDistance = sftdPlug.asDouble();
MPlug doSoftPlug = handleFn.findPlug("asft");
double m_doSoft = doSoftPlug.asBool();
MPlug doStretchPlug = handleFn.findPlug("astc");
double m_doStretch = doStretchPlug.asDouble();
MPlug elbowSlidePlug = handleFn.findPlug("mjsld");
double m_elbowSlide = elbowSlidePlug.asDouble();
MPlug elbowLockWeigthPlug = handleFn.findPlug("mjlockw");
double m_elbowLockWeigth = elbowLockWeigthPlug.asDouble();
double scale[3] = { 1.0, 1.0, 1.0 };
startJointFn.getScale(scale);
MMatrix midJointStartMatrix = getMidRestLocalMatrix(handleFn, MVector(scale[0], scale[1], scale[2]));
MMatrix endJointStartMatrix = getEndRestLocalMatrix(handleFn);
MMatrix midWorldMatrix = midJointStartMatrix * startJointPath.inclusiveMatrix();
MMatrix endWorldMatrix = endJointStartMatrix * midWorldMatrix;
MPoint handlePos = handleFn.rotatePivot(MSpace::kWorld);
//MPoint effectorPos = effectorFn.rotatePivot(MSpace::kWorld);
//MPoint midJointPos = midJointFn.rotatePivot(MSpace::kWorld);
MPoint startJointPos = startJointFn.rotatePivot(MSpace::kWorld);
MPoint effectorPos(endWorldMatrix[3][0], endWorldMatrix[3][1], endWorldMatrix[3][2]);
MPoint midJointPos(midWorldMatrix[3][0], midWorldMatrix[3][1], midWorldMatrix[3][2]);
// slide joint
MPoint slideMidPosition;
MVector globalVec = effectorPos - startJointPos;
MVector midVec = midJointPos - startJointPos;
MVector endVec = effectorPos - midJointPos;
MVector dispVec;
if (m_elbowSlide < 0.0)
{
dispVec = (globalVec.normal() * midVec.normal()) * m_elbowSlide * midVec.length() * globalVec.normal();
}
else
{
dispVec = (globalVec.normal() * endVec.normal()) * m_elbowSlide * endVec.length() * globalVec.normal();
}
MVector vector1 = midVec + dispVec;
double newMidLength = vector1.length();
MVector vector2 = globalVec - vector1;
double newEndLength = vector2.length();
double newStartAngle = globalVec.normal().angle(vector1.normal());
double newMidAngle = vector1.normal().angle(vector2.normal());
double oldStartAngle = globalVec.normal().angle((midJointPos - startJointPos).normal());
double oldMidAngle = (midJointPos - startJointPos).normal().angle((effectorPos - midJointPos).normal());
MVector startCross = globalVec.normal() ^ (midJointPos - startJointPos).normal();
startCross.normalize();
MQuaternion qStartElbow(newStartAngle - oldStartAngle, startCross);
MVector midCross = (midJointPos - startJointPos).normal() ^ (effectorPos - midJointPos).normal();
midCross.normalize();
MQuaternion qMidElbow(newMidAngle - oldMidAngle, midCross);
//midJointFn.rotateBy(qMidElbow, MSpace::kWorld);
//startJointFn.rotateBy(qStartElbow, MSpace::kWorld);
MPlug plug2 = handleFn.findPlug("ejt");
MVector effectorTranslation2 = getPointValue(plug2);
plug2 = handleFn.findPlug("mjt");
MVector midTranslation2 = getPointValue(plug2);
MVector midTranslationOffset = midTranslation2;
MVector effectorTranslationOffset = effectorTranslation2;
midTranslationOffset = midTranslation2 - midTranslation2.normal() * newMidLength;
effectorTranslationOffset = effectorTranslation2 - effectorTranslation2.normal() * newEndLength;
//midJointFn.setTranslation(midTranslation, MSpace::kTransform);
//endJointFn.setTranslation(effectorTranslation, MSpace::kTransform);
// end slide joint
MVector poleVector = poleVectorFromHandle(handlePath);
poleVector *= handlePath.exclusiveMatrix();
double twistValue = twistFromHandle(handlePath);
MQuaternion qStart, qMid;
// vector from startJoint to midJoint
//MVector vector1 = midJointPos - startJointPos;
// vector from midJoint to effector
//MVector vector2 = effectorPos - midJointPos;
// vector from startJoint to handle
MVector vectorH = handlePos - startJointPos;
// vector from startJoint to effector
MVector vectorE = effectorPos - startJointPos;
//double startScale2[3] = {m_initStartScale[0] * m_stretchRatio, m_initStartScale[1], m_initStartScale[2]};
//double midScale2[3] = {m_initMidScale[0] * m_stretchRatio, m_initMidScale[1], m_initMidScale[2]};
bool doingSoft = false;
if ((m_doSoft) && (m_softDistance > 0.0) && (vectorH.length() > ((vector1.length() + vector2.length()) - m_softDistance)))
{
double dChain = (vector1.length() + vector2.length());
double dSoft = m_softDistance;
double dA = dChain - m_softDistance;
double x = vectorH.length();
vectorH = vectorH.normal() * (dA + dSoft * (1.0 - exp(-(x - dA) / dSoft)));
doingSoft = true;
}
// lengths of those vectors
double length1 = vector1.length();
double length2 = vector2.length();
double lengthH = vectorH.length();
// component of the vector1 orthogonal to the vectorE
MVector vectorO =
vector1 - vectorE*((vector1*vectorE) / (vectorE*vectorE));
//////////////////////////////////////////////////////////////////
// calculate q12 which solves for the midJoint rotation
//////////////////////////////////////////////////////////////////
// angle between vector1 and vector2
double vectorAngle12 = vector1.angle(vector2);
// vector orthogonal to vector1 and 2
MVector vectorCross12 = vector1^vector2;
double lengthHsquared = lengthH*lengthH;
// angle for arm extension
double cos_theta =
(lengthHsquared - length1*length1 - length2*length2)
/ (2 * length1*length2);
if (cos_theta > 1)
cos_theta = 1;
else if (cos_theta < -1)
cos_theta = -1;
double theta = acos(cos_theta);
// quaternion for arm extension
MQuaternion q12(theta - vectorAngle12, vectorCross12);
//////////////////////////////////////////////////////////////////
// calculate qEH which solves for effector rotating onto the handle
//////////////////////////////////////////////////////////////////
// vector2 with quaternion q12 applied
vector2 = vector2.rotateBy(q12);
// vectorE with quaternion q12 applied
vectorE = vector1 + vector2;
// quaternion for rotating the effector onto the handle
MQuaternion qEH(vectorE, vectorH);
//////////////////////////////////////////////////////////////////
// calculate qNP which solves for the rotate plane
//////////////////////////////////////////////////////////////////
// vector1 with quaternion qEH applied
vector1 = vector1.rotateBy(qEH);
if (vector1.isParallel(vectorH))
// singular case, use orthogonal component instead
vector1 = vectorO.rotateBy(qEH);
// quaternion for rotate plane
MQuaternion qNP;
if (!isParallel(poleVector, vectorH) && (lengthHsquared != 0)) {
// component of vector1 orthogonal to vectorH
MVector vectorN =
vector1 - vectorH*((vector1*vectorH) / lengthHsquared);
// component of pole vector orthogonal to vectorH
MVector vectorP =
poleVector - vectorH*((poleVector*vectorH) / lengthHsquared);
double dotNP = (vectorN*vectorP) / (vectorN.length()*vectorP.length());
if (absoluteValue(dotNP + 1.0) < kEpsilon) {
// singular case, rotate halfway around vectorH
MQuaternion qNP1(kPi, vectorH);
qNP = qNP1;
}
else {
MQuaternion qNP2(vectorN, vectorP);
qNP = qNP2;
}
}
//////////////////////////////////////////////////////////////////
// calculate qTwist which adds the twist
//////////////////////////////////////////////////////////////////
MQuaternion qTwist(twistValue, vectorH);
// quaternion for the mid joint
qMid = q12;
// concatenate the quaternions for the start joint
qStart = qEH*qNP*qTwist;
MVector midRotAxis;
double midRot = 0.0;
qMid.getAxisAngle(midRotAxis, midRot);
midRotAxis = midRotAxis * midWorldMatrix.inverse();
MPlug plug = handleFn.findPlug("mjr");
MVector rotation = getPointValue(plug);
short value = 1;
plug = handleFn.findPlug("mjro");
plug.getValue(value);
MEulerRotation::RotationOrder rotOrder = getEulerRotationOrder(value);
MEulerRotation mRot(rotation, rotOrder);
//qMidElbow
MVector midElbowRotAxis;
double midElbowRot = 0.0;
qMidElbow.getAxisAngle(midElbowRotAxis, midElbowRot);
midElbowRotAxis = midElbowRotAxis * midWorldMatrix.inverse();
midJointFn.setRotation(mRot.asQuaternion() * MQuaternion(midElbowRot, midElbowRotAxis) * MQuaternion(midRot, midRotAxis), MSpace::kTransform);
startJointFn.rotateBy(qStart * qStartElbow, MSpace::kWorld);
//midJointFn.setRotation(mRot.asQuaternion() * MQuaternion(midRot, midRotAxis), MSpace::kTransform);
//startJointFn.rotateBy(qStart, MSpace::kWorld);
//////////////////////////
// Stretch
/////////////////////////
plug = handleFn.findPlug("ejt");
MVector effectorTranslation = getPointValue(plug) -effectorTranslationOffset;
plug = handleFn.findPlug("mjt");
MVector midTranslation = getPointValue(plug) -midTranslationOffset;
double m_stretchRatio = 1.0;
double ratio = vectorH.length() / vectorE.length();
MVector vectorH2 = handlePos - startJointPos;
if (doingSoft)
{
ratio = vectorH2.length() / vectorH.length();
}
if ((m_doStretch > 0.0001) && (ratio > 1.0))
{
m_stretchRatio = 1.0 + (ratio - 1.0) * m_doStretch;
}
else
{
m_stretchRatio = 1.0;
}
effectorTranslation = effectorTranslation * m_stretchRatio;
midTranslation = midTranslation * m_stretchRatio;
endJointFn.setTranslation(effectorTranslation, MSpace::kTransform);
midJointFn.setTranslation(midTranslation, MSpace::kTransform);
/////////////////////////
// Elbow lock
/////////////////////////
if (m_elbowLockWeigth > 0.0)
{
MVector m_elbowPos;
MPlug elbowPosmjlockplug = handleFn.findPlug("plLock");
if (elbowPosmjlockplug.asBool())
{
MVector poleVector = poleVectorFromHandle(handlePath);
poleVector *= handlePath.exclusiveMatrix();
m_elbowPos = startJointPos + poleVector;
}
else
{
MPlug elbowLockPosPlug = handleFn.findPlug("mjlockp");
MPlug pXPlug = elbowLockPosPlug.child(0);
MPlug pYPlug = elbowLockPosPlug.child(1);
MPlug pZPlug = elbowLockPosPlug.child(2);
m_elbowPos.x = pXPlug.asDouble();
m_elbowPos.y = pYPlug.asDouble();
m_elbowPos.z = pZPlug.asDouble();
}
MPlug endJointRestPosePlug = handleFn.findPlug("ejrp");
MPlug endXPlug = endJointRestPosePlug.child(0);
MPlug endYPlug = endJointRestPosePlug.child(1);
MPlug endZPlug = endJointRestPosePlug.child(2);
double endLength = MVector(endXPlug.asDouble(), endYPlug.asDouble(), endZPlug.asDouble()).length();
MPoint handlePos3 = handleFn.rotatePivot(MSpace::kWorld);
MPoint effectorPos3 = effectorFn.rotatePivot(MSpace::kWorld);
MPoint midJointPos3 = midJointFn.rotatePivot(MSpace::kWorld);
MPoint startJointPos3 = startJointFn.rotatePivot(MSpace::kWorld);
MVector newFirstVector = m_elbowPos - startJointPos3 + (1.0 - m_elbowLockWeigth) * (midJointPos3 - m_elbowPos);
MVector currVec1 = midJointPos3 - startJointPos3;
MQuaternion newQ1(currVec1.normal(), newFirstVector.normal());
startJointFn.rotateBy(newQ1, MSpace::kWorld);
midJointFn.setTranslation(m_elbowPos + (1.0 - m_elbowLockWeigth) * (midJointPos3 - m_elbowPos), MSpace::kWorld);
handlePos3 = handleFn.rotatePivot(MSpace::kWorld);
effectorPos3 = endJointFn.rotatePivot(MSpace::kWorld);
midJointPos3 = midJointFn.rotatePivot(MSpace::kWorld);
startJointPos3 = startJointFn.rotatePivot(MSpace::kWorld);
MVector newSecondVector = handlePos3 - midJointPos3;
MVector currVec2 = effectorPos3 - midJointPos3;
MQuaternion newQ2(currVec2.normal(), newSecondVector.normal());
midJointFn.rotateBy(newQ2, MSpace::kWorld);
endJointFn.setTranslation(endJointFn.translation(MSpace::kTransform).normal() * (endLength + (m_doStretch * (newSecondVector.length() - endLength))), MSpace::kTransform);
}
return MS::kSuccess;
}
MStatus TcSoftIkSolverNodeNew::postSolve(MStatus status)
{
return MS::kSuccess;
}
MVector TcSoftIkSolverNodeNew::poleVectorFromHandle(const MDagPath &handlePath)
//
// This method returns the pole vector of the IK handle.
//
{
MStatus stat;
MFnIkHandle handleFn(handlePath, &stat);
MPlug pvxPlug = handleFn.findPlug("pvx");
MPlug pvyPlug = handleFn.findPlug("pvy");
MPlug pvzPlug = handleFn.findPlug("pvz");
double pvxValue, pvyValue, pvzValue;
pvxPlug.getValue(pvxValue);
pvyPlug.getValue(pvyValue);
pvzPlug.getValue(pvzValue);