-
Notifications
You must be signed in to change notification settings - Fork 3
/
Math.cls
1537 lines (1445 loc) · 54.5 KB
/
Math.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Math"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
' Original code made by Elroy Sullivan, PhD (User in VbForums)
' Was a module and now is a Class, by George Karras
' NOT FINISHED YET
' We can't pass UDT from M2000 script but we can pass a pointer to memory
' which we make using Buffer statement, and using Structure we can create offsets too.
Private mcheck As Boolean
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal lpvDest As Long, ByVal lpvSource As Long, ByVal cbCopy As Long)
Private Const Pi = 3.14159265358979
Private Const pihalf = 1.5707963267949
Private Const pidivby180 = 1.74532925199433E-02
Private Const xyz As Double = 1 ' X+ to X- Z- to Z+
Private Const xzy As Double = -1 ' X+ to X- Y+ to Y-
Private Const yzx As Double = 2 ' Y+ to y- X+ to X-
Private Const yxz As Double = -2 ' Y+ to Y- Z- to Z+
Private Const zxy As Double = 3 ' Z+ to Z- Y- to Y+
Private Const zyx As Double = -3 ' Z+ to Z- X+ to X-
Public Type VecType
' This is used for both 3D spatial vectors, and Euler angles as well.
X As Double
y As Double
z As Double
End Type
Public Type SegType
Origin As VecType
AxisF As VecType ' Forward, West, AxisX
AxisL As VecType ' Left, North, AxisY
AxisU As VecType ' Up, Up, AxisZ
End Type
'
Public Type LineType
point1 As VecType
point2 As VecType
End Type
'
Public Type QuatType ' To get axis and theta back out (JPL convention):
W As Double ' cos(theta/2) theta = ACos(q.w) * 2
X As Double ' v.x * sin(theta/2) v.x = q.x / sin(theta/2) solve for theta first, and then plug in here.
y As Double ' v.y * sin(theta/2) v.y = q.y / sin(theta/2)
z As Double ' v.z * sin(theta/2) v.z = q.z / sin(theta/2)
End Type
Public Sub Vector(a As Long, X As Double, y As Double, z As Double)
Dim V1 As VecType
V1.X = X
V1.y = y
V1.z = z
CopyMemory a, VarPtr(V1.X), 24
End Sub
Private Function GetVector(a As Long) As VecType
CopyMemory VarPtr(GetVector), a, 24
End Function
Private Sub PutVector(v As VecType, a As Long)
CopyMemory a, VarPtr(v), 24
End Sub
Private Function GetRealType(a As Long) As Double
CopyMemory VarPtr(GetRealType), a, 8
End Function
Private Function GetQuatType(a As Long) As QuatType
CopyMemory VarPtr(GetQuatType), a, 32
End Function
Private Sub PutQuatType(q As QuatType, a As Long)
CopyMemory a, VarPtr(q), 32
End Sub
Private Function GetLine(a As Long) As LineType
CopyMemory VarPtr(GetLine), a, 48
End Function
Private Sub PutLine(q As LineType, a As Long)
CopyMemory a, VarPtr(q), 48
End Sub
Public Function DotProduct(a As Long, b As Long) As Double
Dim V1 As VecType, V2 As VecType
CopyMemory VarPtr(V1), a, 24
CopyMemory VarPtr(V2), b, 24
DotProduct = V1.X * V2.X + V1.y * V2.y + V1.z * V2.z
End Function
Public Sub XProduct(a As Long, b As Long, c As Long)
Dim V1 As VecType, V2 As VecType, V3 As VecType
CopyMemory VarPtr(V1), a, 24
CopyMemory VarPtr(V2), b, 24
V3.X = V1.y * V2.z - V1.z * V2.y
V3.y = V1.z * V2.X - V1.X * V2.z
V3.z = V1.X * V2.y - V1.y * V2.X
PutVector V3, c
End Sub
Public Sub RotVect(a As Long, b As Long, c As Long, Angle As Double, Optional bDegrees As Boolean = True) '(vec As VecType, axis As VecType, angle As Double, Optional bDegrees As Boolean = True) As VecType
Dim vec As VecType, axis As VecType, RotVect As VecType
CopyMemory VarPtr(vec), a, 24
CopyMemory VarPtr(axis), b, 24
RotVect = RotVecByQuat(vec, AxisAngle2Quat(axis, Angle, bDegrees))
CopyMemory c, VarPtr(RotVect), 24
End Sub
Public Sub RotVectMult(n As Long, a As Long, b As Long, c As Long, Angle As Double, Optional bDegrees As Boolean = True) '(vec As VecType, axis As VecType, angle As Double, Optional bDegrees As Boolean = True) As VecType
Dim vec As VecType, axis As VecType, RotVect As VecType, m As Long
CopyMemory VarPtr(axis), b, 24
For m = 1 To n
CopyMemory VarPtr(vec), a, 24
RotVect = RotVecByQuat(vec, AxisAngle2Quat(axis, Angle, bDegrees))
CopyMemory c, VarPtr(RotVect), 24
a = a + 24
c = c + 24
Next m
End Sub
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
'
' Make Segment (ordered basis) functions.
'
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
Private Function SegFrom3Pts(Origin As VecType, point1 As VecType, point2 As VecType, Optional Order As Double = xyz) As SegType
' BodyBuilderSegment = [Origin, Point1 - Origin, Point2 - Origin, Order]
SegFrom3Pts = SegFrom5Pts(Origin, point1, Origin, point2, Origin, Order)
End Function
Private Function SegFrom5Pts(Origin As VecType, Point1Line1 As VecType, Point2Line1 As VecType, Point1Line2 As VecType, Point2Line2 As VecType, Optional Order As Double = xyz) As SegType
' BodyBuilderSegment = [Origin, Point2Line1 - Point1Line2, Point2Line2 - Point2Line2, Order]
'
SegFrom5Pts = SegFromLines(Origin, MakeLine(Point1Line1, Point2Line1), MakeLine(Point1Line2, Point2Line2), Order)
End Function
Private Function SegFromLines(Origin As VecType, Line1 As LineType, Line2 As LineType, Optional Order As Double = xyz) As SegType
' This returns an ordered basis (following the right-hand-rule) with all vectors being a unit-in-length (orthonormal).
'
' This function is designed to replicate the line in BodyBuilder that allows you to create a segment from an origin and two lines (but just for one frame):
' Segment = [Origin, Line1.Point1 - Line1.Point2, Line2.Point1 - Line2.Point2, Order]
'
' Note that the output axes are set at origin <0,0,0>. Only the origin reflects an offset translation.
' The return axes will always be orthonormal.
'
' (Up)
' Z
' The figure at right attempts to illustrate one of these segments: | Y (Left, North)
' This is the output of this function. | /
' | /
' | /
' |/
' (Origin) *------- X (Forward, East)
'
' The input is an origin and two lines. The origin really stands alone and often isn't terribly important,
' especially if we're just concerned with rotations.
'
' The actual axes of the segment are constructed from the two lines, and things are constructed differently depending on the specified order.
' The first line (Line1) defines the first letter of the order of the segment, with the positive direction defined by Line1.Point1
' (and the line's origin defined by Line1.Point2). The following may help:
'
' For Xyz(1) & Xzy(-1) AxisX is defined by Line1 with positive heading to Line1.Point1.
' For Yzx(2) & Yxz(-2) AxisY is defined by Line1 with positive heading to Line1.Point1.
' For Zxy(3) & Zyx(-3) AxisZ is defined by Line1 with positive heading to Line1.Point1.
'
' The second line (Line2) approximates the last (third) letter of the order, with the alphabetic order specifying whether Line2.Point1 or Line2.Point2 is positive.
' With respect to Line2, the following may provide as a cheat-sheet:
'
' For xyZ(1) AxisZ is approximated by Line2 with positive heading to Line2.Point1.
' For xzY(-1) AxisY is approximated by Line2 with positive heading to Line2.Point2.
' For yzX(2) AxisX is approximated by Line2 with positive heading to Line2.Point1.
' For yxZ(-2) AxisZ is approximated by Line2 with positive heading to Line2.Point2.
' For zxY(3) AxisY is approximated by Line2 with positive heading to Line2.Point1.
' For zyX(-3) AxisX is approximated by Line2 with positive heading to Line2.Point2.
'
' Now, we've still got another axis of our segment to address. For instance, for XYZ, we've yet to talk about Y.
' This "middle" axis is defined by the cross product of the other two axes.
' Also when discussing the last axis, I used the word "approximate". I did this because, once the middle axis is determined,
' this "last" axis is recalculated as the cross product of the first two. This is the way an orthogonal segment is achieved.
' The following are the actual calculations (with × denoting a cross product):
'
' LineA = (Line1.Point1 - Line1.Point2) ; Unitized.
' LineB = (Line2.Point1 - Line2.Point2) ; Unitized.
'
' If constant is XYZ (1): AxisX = LineA
' AxisY = LineB × LineA
' AxisZ = LineA × AxisY
'
' If constant is XZY (-1): AxisX = LineA
' AxisZ = LineB × LineA
' AxisY = AxisZ × LineA
'
' If constant is YZX (2): AxisY = LineA
' AxisZ = LineB × LineA
' AxisX = LineA × AxisZ
'
' If constant is YXZ (-2): AxisY = LineA
' AxisX = LineB × LineA
' AxisZ = AxisX × LineA
'
' If constant is ZXY (3): AxisZ = LineA
' AxisX = LineB × LineA
' AxisY = LineA × AxisX
'
' If constant is ZYX (-3): AxisZ = LineA
' AxisY = LineB × LineA
' AxisX = AxisY × LineA
'
' And finally, here's another cheat-sheet that may help:
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), xyz] (1)
' Origin + AxisX - + AxisZ -
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), xzy] (-1)
' Origin + AxisX - - AxisY +
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), yzx] (2)
' Origin + AxisY - + AxisX -
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), yxz] (-2)
' Origin + AxisY - - AxisZ +
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), zxy] (3)
' Origin + AxisZ - + AxisY -
'
' Segment = [Point0, (L1.p1 - L1.p2), (L2.p1 - L2.p2), zyx] (-3)
' Origin + AxisZ - - AxisX +
'
Dim V1 As VecType
Dim V2 As VecType
'
SegFromLines.Origin = Origin
'
V1 = VecDif(Line1.point1, Line1.point2)
V2 = VecDif(Line2.point1, Line2.point2)
V1 = UnitVec(V1)
V2 = UnitVec(V2)
'
Select Case Order
Case xyz ' 1
SegFromLines.AxisF = V1 ' X
SegFromLines.AxisL = UnitVec(XProd(V2, SegFromLines.AxisF)) ' Y
SegFromLines.AxisU = UnitVec(XProd(SegFromLines.AxisF, SegFromLines.AxisL)) ' Z
Case xzy ' -1
SegFromLines.AxisF = V1 ' X
SegFromLines.AxisU = UnitVec(XProd(V2, SegFromLines.AxisF)) ' Z
SegFromLines.AxisL = UnitVec(XProd(SegFromLines.AxisU, SegFromLines.AxisF)) ' Y
Case yzx ' 2
SegFromLines.AxisL = V1 ' Y
SegFromLines.AxisU = UnitVec(XProd(V2, SegFromLines.AxisL)) ' Z
SegFromLines.AxisF = UnitVec(XProd(SegFromLines.AxisL, SegFromLines.AxisU)) ' X
Case yxz ' -2
SegFromLines.AxisL = V1 ' Y
SegFromLines.AxisF = UnitVec(XProd(V2, SegFromLines.AxisL)) ' X
SegFromLines.AxisU = UnitVec(XProd(SegFromLines.AxisF, SegFromLines.AxisL)) ' Z
Case zxy ' 3
SegFromLines.AxisU = V1 ' Z
SegFromLines.AxisF = UnitVec(XProd(V2, SegFromLines.AxisU)) ' X
SegFromLines.AxisL = UnitVec(XProd(SegFromLines.AxisU, SegFromLines.AxisF)) ' Y
Case zyx ' -3
SegFromLines.AxisU = V1 ' Z
SegFromLines.AxisL = UnitVec(XProd(V2, SegFromLines.AxisU)) ' Y
SegFromLines.AxisF = UnitVec(XProd(SegFromLines.AxisL, SegFromLines.AxisU)) ' X
End Select
End Function
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
'
' Conversion functions.
'
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
Private Function Euler2Quat(euler As VecType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As QuatType
' To get the same return from Quat2Euler, the following Euler angle constraints should be observed:
' pi > euler.x > -pi ' In theory, 180 (or -180) should be possible, but not here.
' pi/2 > euler.y > pi/2 ' Avoid pi/2 to avoid gimbal lock.
' pi > euler.z > -pi ' In theory, 180 (or -180) should be possible, but not here.
'
Dim sX As Double, sY As Double, SZ As Double
Dim cx As Double, cy As Double, cZ As Double
Dim EulerTemp As VecType
'
EulerTemp = euler
If bDegrees Then EulerTemp = VecDeg2Rad(EulerTemp)
'
' Speed-up variables, so that Sin and Cos aren't repeatedly called.
sX = Sin(EulerTemp.X / 2#): sY = Sin(EulerTemp.y / 2#): SZ = Sin(EulerTemp.z / 2#)
cx = Cos(EulerTemp.X / 2#): cy = Cos(EulerTemp.y / 2#): cZ = Cos(EulerTemp.z / 2#)
'
Select Case Order
Case xyz
Euler2Quat.W = cx * cy * cZ - sX * sY * SZ
Euler2Quat.X = sX * cy * cZ + cx * sY * SZ
Euler2Quat.y = cx * sY * cZ - sX * cy * SZ
Euler2Quat.z = cx * cy * SZ + sX * sY * cZ
Case xzy
Euler2Quat.W = cx * cy * cZ + sX * sY * SZ
Euler2Quat.X = sX * cy * cZ - cx * sY * SZ
Euler2Quat.y = cx * cy * SZ - sX * sY * cZ
Euler2Quat.z = cx * sY * cZ + sX * cy * SZ
Case yzx
Euler2Quat.W = cx * cy * cZ - sX * sY * SZ
Euler2Quat.X = cx * cy * SZ + sX * sY * cZ
Euler2Quat.y = sX * cy * cZ + cx * sY * SZ
Euler2Quat.z = cx * sY * cZ - sX * cy * SZ
Case yxz
Euler2Quat.W = cx * cy * cZ + sX * sY * SZ
Euler2Quat.X = cx * sY * cZ + sX * cy * SZ
Euler2Quat.y = sX * cy * cZ - cx * sY * SZ
Euler2Quat.z = cx * cy * SZ - sX * sY * cZ
Case zxy
Euler2Quat.W = cx * cy * cZ - sX * sY * SZ
Euler2Quat.X = cx * sY * cZ - sX * cy * SZ
Euler2Quat.y = cx * cy * SZ + sX * sY * cZ
Euler2Quat.z = sX * cy * cZ + cx * sY * SZ
Case zyx
Euler2Quat.W = cx * cy * cZ + sX * sY * SZ
Euler2Quat.X = cx * cy * SZ - sX * sY * cZ
Euler2Quat.y = cx * sY * cZ + sX * cy * SZ
Euler2Quat.z = sX * cy * cZ - cx * sY * SZ
End Select
End Function
Private Function Quat2Euler(q As QuatType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As VecType
' The order (xyzEnum) may be reversed to some mathematicians.
' However, it's done this way so that the same xyzEnum constant can be used for Euler2Quat and Quat2Euler, and return to the same Euler angles.
' Also, this is the way BodyBuilder does it, and these procedures are meant to reproduce that functionality.
'
Select Case Order
Case xyz: Quat2Euler = Quat2EulerHelper(-2# * (q.y * q.z - q.W * q.X), q.W * q.W - q.X * q.X - q.y * q.y + q.z * q.z, 2# * (q.X * q.z + q.W * q.y), -2# * (q.X * q.y - q.W * q.z), q.W * q.W + q.X * q.X - q.y * q.y - q.z * q.z)
Case xzy: Quat2Euler = Quat2EulerHelper(2# * (q.y * q.z + q.W * q.X), q.W * q.W - q.X * q.X + q.y * q.y - q.z * q.z, -2# * (q.X * q.y - q.W * q.z), 2# * (q.X * q.z + q.W * q.y), q.W * q.W + q.X * q.X - q.y * q.y - q.z * q.z)
Case yzx: Quat2Euler = Quat2EulerHelper(-2# * (q.X * q.z - q.W * q.y), q.W * q.W + q.X * q.X - q.y * q.y - q.z * q.z, 2# * (q.X * q.y + q.W * q.z), -2# * (q.y * q.z - q.W * q.X), q.W * q.W - q.X * q.X + q.y * q.y - q.z * q.z)
Case yxz: Quat2Euler = Quat2EulerHelper(2# * (q.X * q.z + q.W * q.y), q.W * q.W - q.X * q.X - q.y * q.y + q.z * q.z, -2# * (q.y * q.z - q.W * q.X), 2# * (q.X * q.y + q.W * q.z), q.W * q.W - q.X * q.X + q.y * q.y - q.z * q.z)
Case zxy: Quat2Euler = Quat2EulerHelper(-2# * (q.X * q.y - q.W * q.z), q.W * q.W - q.X * q.X + q.y * q.y - q.z * q.z, 2# * (q.y * q.z + q.W * q.X), -2# * (q.X * q.z - q.W * q.y), q.W * q.W - q.X * q.X - q.y * q.y + q.z * q.z)
Case zyx: Quat2Euler = Quat2EulerHelper(2# * (q.X * q.y + q.W * q.z), q.W * q.W + q.X * q.X - q.y * q.y - q.z * q.z, -2# * (q.X * q.z - q.W * q.y), 2# * (q.y * q.z + q.W * q.X), q.W * q.W - q.X * q.X - q.y * q.y + q.z * q.z)
End Select
'
If bDegrees Then Quat2Euler = VecRad2Deg(Quat2Euler)
End Function
Public Function Quat2EulerHelper(r11 As Double, r12 As Double, r21 As Double, r31 As Double, r32 As Double) As VecType
Quat2EulerHelper.X = ATan2(r11, r12)
Quat2EulerHelper.y = ASin(r21)
Quat2EulerHelper.z = ATan2(r31, r32)
End Function
Private Function Seg2Quat(seg As SegType) As QuatType
' Ignores origin.
'
Seg2Quat = Axes2Quat(seg.AxisF, seg.AxisL, seg.AxisU)
End Function
Private Function Quat2Seg(q As QuatType) As SegType
' Leaves the segment origin as zero.
'
Quat2Seg.AxisF = Quat2Fwd(q)
Quat2Seg.AxisL = Quat2Left(q)
Quat2Seg.AxisU = Quat2Up(q)
End Function
Private Function Euler2Seg(euler As VecType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As SegType
' Leaves the segment origin as zero.
Euler2Seg = Quat2Seg(Euler2Quat(euler, Order, bDegrees))
End Function
Private Function Seg2Euler(seg As SegType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As VecType
' Ignores origin.
Seg2Euler = Quat2Euler(Seg2Quat(seg), Order, bDegrees)
End Function
Private Function Axes2Quat(AxisF As VecType, AxisL As VecType, AxisU As VecType) As QuatType
' Must be three orthogonal unit vectors.
' Basically, the vectors that define a BodyBuilder segment.
'
Dim s As Double
Dim tr As Double
Dim Max As Double
Dim X As Double, y As Double, z As Double
'
tr = (AxisF.X + AxisL.y + AxisU.z + 1#)
'
If (tr >= 1#) Then
s = (0.5 / Sqr(tr))
Axes2Quat.W = 0.25 / s
Axes2Quat.X = (AxisL.z - AxisU.y) * s
Axes2Quat.y = (AxisU.X - AxisF.z) * s
Axes2Quat.z = (AxisF.y - AxisL.X) * s
Else
If (AxisL.y > AxisU.z) Then Max = AxisL.y Else Max = AxisU.z
'
If (Max < AxisF.X) Then
s = (Sqr(AxisF.X - (AxisL.y + AxisU.z) + 1#))
X = (s * 0.5)
s = (0.5 / s)
Axes2Quat.W = (AxisL.z - AxisU.y) * s
Axes2Quat.X = X
Axes2Quat.y = (AxisF.y + AxisL.X) * s
Axes2Quat.z = (AxisU.X + AxisF.z) * s
ElseIf (Max = AxisL.y) Then
s = (Sqr(AxisL.y - (AxisU.z + AxisF.X) + 1#))
y = (s * 0.5)
s = (0.5 / s)
Axes2Quat.W = (AxisU.X - AxisF.z) * s
Axes2Quat.X = (AxisF.y + AxisL.X) * s
Axes2Quat.y = y
Axes2Quat.z = (AxisL.z + AxisU.y) * s
Else
s = (Sqr(AxisU.z - (AxisF.X + AxisL.y) + 1#))
z = (s * 0.5)
s = (0.5 / s)
Axes2Quat.W = (AxisF.y - AxisL.X) * s
Axes2Quat.X = (AxisU.X + AxisF.z) * s
Axes2Quat.y = (AxisL.z + AxisU.y) * s
Axes2Quat.z = z
End If
End If
End Function
Private Function Quat2Fwd(q As QuatType) As VecType
' Make sure quat is normed.
Quat2Fwd = RotVecByQuat(MakeVec(1, 0, 0), q)
End Function
Private Function Quat2Left(q As QuatType) As VecType
' Make sure quat is normed.
Quat2Left = RotVecByQuat(MakeVec(0, 1, 0), q)
End Function
Private Function Quat2Up(q As QuatType) As VecType
' Make sure quat is normed.
Quat2Up = RotVecByQuat(MakeVec(0, 0, 1), q)
End Function
Private Function AxisAngle2Quat(axis As VecType, ByVal Angle As Double, Optional bDegrees As Boolean = True) As QuatType
' You should be sure the vector is a UNIT vector before calling this, else the quaternion will not be a unit quaternion.
' Note that the Axis(vector) is the rotation vector, and has nothing to do with basis vectors.
'
Dim SinThetaDivTwo As Double
'
If bDegrees Then Angle = Deg2Rad(Angle)
SinThetaDivTwo = Sin(Angle / 2)
'
AxisAngle2Quat.W = Cos(Angle / 2)
AxisAngle2Quat.X = axis.X * SinThetaDivTwo
AxisAngle2Quat.y = axis.y * SinThetaDivTwo
AxisAngle2Quat.z = axis.z * SinThetaDivTwo
End Function
Private Function QuatAxis(quat As QuatType) As VecType
' This is the rotation axis of the quaternion.
' It has nothing to do with the quaternion's basis axes.
Dim SinThetaByTwo As Double
'
SinThetaByTwo = Sin(QuatAngle(quat, False) / 2)
'
On Error Resume Next
QuatAxis.X = quat.X / SinThetaByTwo
QuatAxis.y = quat.y / SinThetaByTwo
QuatAxis.z = quat.z / SinThetaByTwo
On Error GoTo 0
End Function
Private Function QuatAngle(quat As QuatType, Optional bDegrees As Boolean = True) As Double
' Just the angle from the quaternion.
QuatAngle = ACos(quat.W) * 2
If bDegrees Then QuatAngle = Rad2Deg(QuatAngle)
End Function
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
'
' Rotation functions.
'
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
Private Function RotSeg(seg As SegType, axis As VecType, Angle As Double, Optional bDegrees As Boolean = True) As SegType
' This is exactly like a rotation: (Seg2 = Rot(Seg1, Axis, Angle)).
'
RotSeg = RotSegByQuat(seg, AxisAngle2Quat(axis, Angle, bDegrees))
End Function
Private Function RotSegByQuat(seg As SegType, quat As QuatType) As SegType
RotSegByQuat.AxisF = RotVecByQuat(seg.AxisF, quat)
RotSegByQuat.AxisL = RotVecByQuat(seg.AxisL, quat)
RotSegByQuat.AxisU = RotVecByQuat(seg.AxisU, quat)
RotSegByQuat.Origin = seg.Origin
End Function
Private Function RotQuat(q1 As QuatType, q2 As QuatType) As QuatType
' q1 is being rotated by q2 amount.
'
RotQuat.W = q1.W * q2.W - q1.X * q2.X - q1.y * q2.y - q1.z * q2.z
RotQuat.X = q1.W * q2.X + q1.X * q2.W - q1.y * q2.z + q1.z * q2.y
RotQuat.y = q1.W * q2.y + q1.X * q2.z + q1.y * q2.W - q1.z * q2.X
RotQuat.z = q1.W * q2.z - q1.X * q2.y + q1.y * q2.X + q1.z * q2.W
End Function
Private Function MulQuat(q1 As QuatType, q2 As QuatType) As QuatType
' q1 is being rotated by q2 amount.
'
MulQuat.W = q1.W * q2.W - q1.X * q2.X - q1.y * q2.y - q1.z * q2.z
MulQuat.X = q1.W * q2.X + q1.X * q2.W + q1.y * q2.z - q1.z * q2.y
MulQuat.y = q1.W * q2.y - q1.X * q2.z + q1.y * q2.W + q1.z * q2.X
MulQuat.z = q1.W * q2.z + q1.X * q2.y - q1.y * q2.X + q1.z * q2.W
End Function
Private Function UnRotQuat(q1 As QuatType, q2 As QuatType) As QuatType
' q1 is being UNrotated by q2 amount.
'
UnRotQuat = RotQuat(q1, NegQuat(q2))
End Function
Private Function RotVec(vec As VecType, axis As VecType, Angle As Double, Optional bDegrees As Boolean = True) As VecType
RotVec = RotVecByQuat(vec, AxisAngle2Quat(axis, Angle, bDegrees))
End Function
Private Function RotVecByQuat(vec As VecType, quat As QuatType) As VecType
' This is essentially a vector rotation.
' To emulate BodyBuilder: NewVec = RotVecByQuat(Vec, AxisAng2Quat(Axis, Deg2Rad(Angle)))
'
Dim num1 As Double, num2 As Double, num3 As Double
Dim num4 As Double, num5 As Double, num6 As Double
Dim num7 As Double, num8 As Double, num9 As Double
Dim numa As Double, numb As Double, numc As Double
'
num1 = quat.X * 2#: num2 = quat.y * 2#: num3 = quat.z * 2#
num4 = quat.X * num1: num5 = quat.y * num2: num6 = quat.z * num3
num7 = quat.X * num2: num8 = quat.X * num3: num9 = quat.y * num3
numa = quat.W * num1: numb = quat.W * num2: numc = quat.W * num3
'
' RotVecByQuat.x = (1# - (num5 + num6)) * vec.x + (num7 - numc) * vec.y + (num8 + numb) * vec.z
' RotVecByQuat.y = (num7 + numc) * vec.x + (1# - (num4 + num6)) * vec.y + (num9 - numa) * vec.z
' RotVecByQuat.z = (num8 - numb) * vec.x + (num9 + numa) * vec.y + (1# - (num4 + num5)) * vec.z
'
RotVecByQuat.X = (1# - (num5 + num6)) * vec.X + (num7 - numc) * vec.y + (num8 + numb) * vec.z
RotVecByQuat.y = (num7 + numc) * vec.X + (1# - (num4 + num6)) * vec.y + (num9 - numa) * vec.z
RotVecByQuat.z = (num8 - numb) * vec.X + (num9 + numa) * vec.y + (1# - (num4 + num5)) * vec.z
End Function
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
'
' Angles Between functions.
'
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
Private Function FixedBetweenSegs(seg1 As SegType, seg2 As SegType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As VecType
' In BodyBuilder terms, this is: FixedBetweenSegs = <seg1, seg2, order>
' This takes the "Fixed" angle approach rather than the "Euler" angle approach.
' Conceptually, seg1 & seg2 are reversed with respect to what's rotated against what for this "Fixed" approach,
' but it's left this way to correspond to BodyBuilder code.
'
FixedBetweenSegs = NegVec(EulerBetweenSegs(seg1, seg2, Order, bDegrees))
End Function
Private Function EulerBetweenSegs(seg1 As SegType, seg2 As SegType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As VecType
' In BodyBuilder terms, this is: EulerBetweenSegs = -<seg1, seg2, order>
' We do NOT need to negate (as BodyBuilder does) when using this function.
'
EulerBetweenSegs = EulerBetweenQuats(Seg2Quat(seg1), Seg2Quat(seg2), Order, bDegrees)
End Function
Private Function EulerBetweenQuats(q1 As QuatType, q2 As QuatType, Optional Order As Double = xyz, Optional bDegrees As Boolean = True) As VecType
' Put q2's rotation basis at <0,0,0>, and then convert to Euler angles.
' In other words, what Euler angles will rotate q1 to q2.
'
EulerBetweenQuats = Quat2Euler(UnRotQuat(q2, q1), Order, bDegrees)
End Function
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
'
' Simple Linear Algebra functions.
'
'****************************************************************************************************
'****************************************************************************************************
'****************************************************************************************************
Private Function VecAvg(V1 As VecType, V2 As VecType) As VecType
VecAvg = VecDivNum(VecSum(V1, V2), 2)
End Function
Public Sub VecAver(a As Long, b As Long, c As Long)
Dim V1 As VecType
V1 = VecDivNum(VecSumm1(a, b), 2)
CopyMemory c, VarPtr(V1), 24
End Sub
Public Sub VecAverMult(n As Long, a As Long, b As Long, c As Long)
Dim V1 As VecType, m As Long
For m = 1 To n
V1 = VecDivNum(VecSumm1(a, b), 2)
CopyMemory c, VarPtr(V1), 24
a = a + 24
b = b + 24
c = c + 24
Next m
End Sub
Private Function VecSumm1(a As Long, b As Long) As VecType
Dim V1 As VecType, V2 As VecType, V3 As VecType
CopyMemory VarPtr(V1), a, 24
CopyMemory VarPtr(V2), b, 24
VecSumm1.X = V1.X + V2.X
VecSumm1.y = V1.y + V2.y
VecSumm1.z = V1.z + V2.z
End Function
Private Function VecSum(V1 As VecType, V2 As VecType) As VecType
VecSum.X = V1.X + V2.X
VecSum.y = V1.y + V2.y
VecSum.z = V1.z + V2.z
End Function
Public Sub VecSumm(a As Long, b As Long, c As Long)
Dim V1 As VecType, V2 As VecType, V3 As VecType
CopyMemory VarPtr(V1), a, 24
CopyMemory VarPtr(V2), b, 24
V3.X = V1.X + V2.X
V3.y = V1.y + V2.y
V3.z = V1.z + V2.z
CopyMemory c, VarPtr(V3), 24
End Sub
Public Sub VecSummMult(n As Long, a As Long, b As Long, c As Long)
Dim V1 As VecType, V2 As VecType, V3 As VecType, m As Long
CopyMemory VarPtr(V2), b, 24
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
V3.X = V1.X + V2.X
V3.y = V1.y + V2.y
V3.z = V1.z + V2.z
CopyMemory c, VarPtr(V3), 24
a = a + 24
c = c + 24
Next m
End Sub
Private Function VecDif(V1 As VecType, V2 As VecType) As VecType
VecDif.X = V1.X - V2.X
VecDif.y = V1.y - V2.y
VecDif.z = V1.z - V2.z
End Function
Public Sub VecDiff(a As Long, b As Long, c As Long)
Dim V1 As VecType, V2 As VecType, V3 As VecType
CopyMemory VarPtr(V1), a, 24
CopyMemory VarPtr(V2), b, 24
V3.X = V1.X - V2.X
V3.y = V1.y - V2.y
V3.z = V1.z - V2.z
CopyMemory c, VarPtr(V3), 24
End Sub
Public Sub VecDiffMult(n As Long, a As Long, b As Long, c As Long)
Dim V1 As VecType, V2 As VecType, V3 As VecType, m As Long
CopyMemory VarPtr(V2), b, 24
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
V3.X = V1.X - V2.X
V3.y = V1.y - V2.y
V3.z = V1.z - V2.z
CopyMemory c, VarPtr(V3), 24
a = a + 24
c = c + 24
Next m
End Sub
Sub VecDiffNum(a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType
CopyMemory VarPtr(V1), a, 24
v.X = V1.X - D
v.y = V1.y - D
v.z = V1.z - D
CopyMemory b, VarPtr(v), 24
End Sub
Sub VecDiffNumMult(n As Long, a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType, m As Long
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
v.X = V1.X - D
v.y = V1.y - D
v.z = V1.z - D
CopyMemory b, VarPtr(v), 24
a = a + 24
b = b + 24
Next m
End Sub
Private Function VecAddNum(v As VecType, D As Double) As VecType
VecAddNum.X = v.X + D
VecAddNum.y = v.y + D
VecAddNum.z = v.z + D
End Function
Sub VecAddiNum(a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType
CopyMemory VarPtr(V1), a, 24
v.X = V1.X + D
v.y = V1.y + D
v.z = V1.z + D
CopyMemory b, VarPtr(v), 24
End Sub
Sub VecAddiNumMult(n As Long, a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType, m As Long
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
v.X = V1.X + D
v.y = V1.y + D
v.z = V1.z + D
CopyMemory b, VarPtr(v), 24
a = a + 24
b = b + 24
Next m
End Sub
Sub VecDiviNum(a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType
CopyMemory VarPtr(V1), a, 24
v.X = V1.X / D
v.y = V1.y / D
v.z = V1.z / D
CopyMemory b, VarPtr(v), 24
End Sub
Sub VecDiviNumMult(n As Long, a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType, m As Long
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
v.X = V1.X / D
v.y = V1.y / D
v.z = V1.z / D
CopyMemory b, VarPtr(v), 24
a = a + 24
b = b + 24
Next m
End Sub
Private Function VecDivNum(V1 As VecType, D As Double) As VecType
VecDivNum.X = V1.X / D
VecDivNum.y = V1.y / D
VecDivNum.z = V1.z / D
End Function
Sub VecMulNum(a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType
CopyMemory VarPtr(V1), a, 24
v.X = V1.X * D
v.y = V1.y * D
v.z = V1.z * D
CopyMemory b, VarPtr(v), 24
End Sub
Sub VecMulNumMult(n As Long, a As Long, D As Double, b As Long)
Dim v As VecType, V1 As VecType, m As Long
For m = 1 To n
CopyMemory VarPtr(V1), a, 24
v.X = V1.X * D
v.y = V1.y * D
v.z = V1.z * D
CopyMemory b, VarPtr(v), 24
a = a + 24
b = b + 24
Next m
End Sub
Private Function VecMultNum(V1 As VecType, D As Double) As VecType
VecMultNum.X = V1.X * D
VecMultNum.y = V1.y * D
VecMultNum.z = V1.z * D
End Function
Public Function VecMagnitude(a As Long) As Double
Dim v As VecType
CopyMemory VarPtr(v), a, 24
VecMagnitude = Sqr(v.X * v.X + v.y * v.y + v.z * v.z)
End Function
Private Function VecMag(v As VecType) As Double
VecMag = Sqr(v.X * v.X + v.y * v.y + v.z * v.z)
End Function
Private Function NegVec(v As VecType) As VecType
NegVec.X = -v.X
NegVec.y = -v.y
NegVec.z = -v.z
End Function
Public Sub UnitVect(a As Long)
' Returns ZERO vector if vector has no magnitude.
Dim v As VecType, d1 As VecType
Dim D As Double
CopyMemory VarPtr(v), a, 24
'
D = VecMag(v)
If D > 0 Then
d1.X = v.X / D
d1.y = v.y / D
d1.z = v.z / D
End If
CopyMemory a, VarPtr(d1), 24
End Sub
Public Sub UnitVectMult(n As Long, a As Long)
' Returns ZERO vector if vector has no magnitude.
Dim v As VecType, d1 As VecType, Zero As VecType, m As Long
Dim D As Double
For m = 1 To n
CopyMemory VarPtr(v), a, 24
'
D = VecMag(v)
If D > 0 Then
d1.X = v.X / D
d1.y = v.y / D
d1.z = v.z / D
Else
d1 = Zero
End If
CopyMemory a, VarPtr(d1), 24
a = a + 24
Next m
End Sub
Private Function UnitVec(v As VecType) As VecType
' Returns ZERO vector if vector has no magnitude.
Dim D As Double
'
D = VecMag(v)
If D > 0 Then
UnitVec.X = v.X / D
UnitVec.y = v.y / D
UnitVec.z = v.z / D
End If
End Function
' XProduct
Private Function XProd(V1 As VecType, V2 As VecType) As VecType
XProd.X = V1.y * V2.z - V1.z * V2.y
XProd.y = V1.z * V2.X - V1.X * V2.z
XProd.z = V1.X * V2.y - V1.y * V2.X
End Function
' XDotProduct
Private Function DotProd(V1 As VecType, V2 As VecType) As Double
DotProd = V1.X * V2.X + V1.y * V2.y + V1.z * V2.z
End Function
Sub NegateQuat(a As Long, b As Long)
Dim quat As QuatType
CopyMemory VarPtr(quat), a, 48
' quat.w = quat.w
quat.X = -quat.X
quat.y = -quat.y
quat.z = -quat.z
CopyMemory b, VarPtr(quat), 48
End Sub
Sub NegateQuatMult(n As Long, a As Long, b As Long)
Dim quat As QuatType, m As Long
For m = 1 To n
CopyMemory VarPtr(quat), a, 48
' quat.w = quat.w
quat.X = -quat.X
quat.y = -quat.y
quat.z = -quat.z
CopyMemory b, VarPtr(quat), 48
a = a + 48
b = b + 48
Next m
End Sub
Private Function NegQuat(quat As QuatType) As QuatType
' There are actually two ways to do this, negate the angle or negate the axis.
' It turns out that negating the axis works a bit better with the RotQuat function
NegQuat.W = quat.W
NegQuat.X = -quat.X
NegQuat.y = -quat.y
NegQuat.z = -quat.z
End Function
Private Function NegQuatAll(quat As QuatType) As QuatType
NegQuatAll.W = -quat.W
NegQuatAll.X = -quat.X
NegQuatAll.y = -quat.y
NegQuatAll.z = -quat.z
End Function
Public Sub MakeLineFrom2Vec(a As Long, b As Long, c As Long) ' Pointers VecType, VecType, LineType
CopyMemory c, a, 24
CopyMemory c + 24, a, 24
End Sub
Private Function MakeLine(point1 As VecType, point2 As VecType) As LineType
MakeLine.point1 = point1
MakeLine.point2 = point2
End Function
Private Function MakeVec(X As Double, y As Double, z As Double) As VecType
MakeVec.X = X
MakeVec.y = y
MakeVec.z = z
End Function
Public Sub Quaternion(a As Long, W As Double, X As Double, y As Double, z As Double)
CopyMemory a, VarPtr(W), 8
CopyMemory a + 8, VarPtr(X), 8
CopyMemory a + 16, VarPtr(y), 8
CopyMemory a + 24, VarPtr(z), 8
End Sub
Private Function MakeQuat(W As Double, X As Double, y As Double, z As Double) As QuatType
MakeQuat.W = W
MakeQuat.X = X
MakeQuat.y = y
MakeQuat.z = z
End Function
' quad functions
Public Sub QuatNeg(a As Long, Optional b As Long = 0)
Dim q As QuatType
q = NegQuatAll(GetQuatType(a))
If b = 0 Then CopyMemory a, VarPtr(q), 32: Exit Sub
CopyMemory b, VarPtr(q), 32
End Sub
Public Sub QuatNegMul(n As Long, a As Long, Optional b As Long = 0)
Dim q As QuatType, m As Long
For m = 1 To n
q = NegQuatAll(GetQuatType(a))
If b = 0 Then CopyMemory a, VarPtr(q), 32 Else CopyMemory b, VarPtr(q), 32: b = b + 32
a = a + 32
Next m
End Sub
Public Sub QuatMultQuat(a As Long, b As Long, Optional c As Long = 0)
Dim q As QuatType, q2 As QuatType
q = GetQuatType(a)
q2 = GetQuatType(b)
q = MulQuat(q, q2)
If c = 0 Then CopyMemory a, VarPtr(q), 32: Exit Sub
CopyMemory c, VarPtr(q), 32
End Sub
Public Sub QuatMultQuatMul(n As Long, a As Long, b As Long, Optional c As Long = 0)
Dim q As QuatType, q2 As QuatType, m As Long
If c = 0 Then
For m = 1 To n
q = GetQuatType(a)
q2 = GetQuatType(b)
q = MulQuat(q, q2)
CopyMemory a, VarPtr(q), 32
a = a + 32
b = b + 32
Next m
Else
For m = 1 To n
q = GetQuatType(a)
q2 = GetQuatType(b)
q = MulQuat(q, q2)
CopyMemory c, VarPtr(q), 32
c = c + 32
a = a + 32
b = b + 32
Next m
End If
End Sub
Public Sub QuatMultByReal(a As Long, R As Double, Optional c As Long = 0)
Dim q As QuatType
q = GetQuatType(a)
q.W = q.W * R
q.X = q.X * R
q.y = q.y * R
q.z = q.z * R
If c = 0 Then CopyMemory a, VarPtr(q), 32: Exit Sub
CopyMemory c, VarPtr(q), 32
End Sub
Public Sub QuatMultReal(a As Long, b As Long, Optional c As Long = 0)
Dim q As QuatType, R As Double
q = GetQuatType(a)
R = GetRealType(b)
q.W = q.W * R
q.X = q.X * R
q.y = q.y * R
q.z = q.z * R
If c = 0 Then CopyMemory a, VarPtr(q), 32: Exit Sub
CopyMemory c, VarPtr(q), 32
End Sub
Public Sub QuatMultRealMul(n As Long, a As Long, b As Long, Optional c As Long = 0)
Dim q As QuatType, R As Double, m As Long
For m = 1 To n
q = GetQuatType(a)
R = GetRealType(b)
q.W = q.W * R
q.X = q.X * R
q.y = q.y * R
q.z = q.z * R
If c = 0 Then CopyMemory a, VarPtr(q), 32 Else CopyMemory c, VarPtr(q), 32: c = c + 32
a = a + 32
b = b + 8
Next m
End Sub
Public Sub QuatMultRealMulOne(n As Long, a As Long, b As Long, Optional c As Long = 0)
Dim q As QuatType, R As Double, m As Long
R = GetRealType(b)
For m = 1 To n
q = GetQuatType(a)
q.W = q.W * R
q.X = q.X * R
q.y = q.y * R
q.z = q.z * R
If c = 0 Then CopyMemory a, VarPtr(q), 32 Else CopyMemory c, VarPtr(q), 32: c = c + 32
a = a + 32
Next m
End Sub
Public Sub QuatAddByReal(a As Long, R As Double, Optional c As Long = 0)
Dim q As QuatType
If c = 0 Then
q = GetQuatType(a)
q.W = q.W + R
CopyMemory a, VarPtr(q), 8