forked from CedricGuillemet/ImGuizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImGuizmo.cpp
executable file
·3144 lines (2702 loc) · 116 KB
/
ImGuizmo.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
// https://github.com/CedricGuillemet/ImGuizmo
// v1.91.3 WIP
//
// The MIT License(MIT)
//
// Copyright(c) 2021 Cedric Guillemet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "imgui.h"
#include "imgui_internal.h"
#include "ImGuizmo.h"
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <malloc.h>
#endif
#if !defined(_MSC_VER) && !defined(__MINGW64_VERSION_MAJOR)
#define _malloca(x) alloca(x)
#define _freea(x)
#endif
// includes patches for multiview from
// https://github.com/CedricGuillemet/ImGuizmo/issues/15
namespace IMGUIZMO_NAMESPACE
{
static const float ZPI = 3.14159265358979323846f;
static const float RAD2DEG = (180.f / ZPI);
static const float DEG2RAD = (ZPI / 180.f);
const float screenRotateSize = 0.06f;
// scale a bit so translate axis do not touch when in universal
const float rotationDisplayFactor = 1.2f;
static OPERATION operator&(OPERATION lhs, OPERATION rhs)
{
return static_cast<OPERATION>(static_cast<int>(lhs) & static_cast<int>(rhs));
}
static bool operator!=(OPERATION lhs, int rhs)
{
return static_cast<int>(lhs) != rhs;
}
static bool Intersects(OPERATION lhs, OPERATION rhs)
{
return (lhs & rhs) != 0;
}
// True if lhs contains rhs
static bool Contains(OPERATION lhs, OPERATION rhs)
{
return (lhs & rhs) == rhs;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// utility and math
void FPU_MatrixF_x_MatrixF(const float* a, const float* b, float* r)
{
r[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12];
r[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13];
r[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14];
r[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15];
r[4] = a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12];
r[5] = a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13];
r[6] = a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14];
r[7] = a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15];
r[8] = a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12];
r[9] = a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13];
r[10] = a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14];
r[11] = a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15];
r[12] = a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12];
r[13] = a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13];
r[14] = a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14];
r[15] = a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15];
}
void Frustum(float left, float right, float bottom, float top, float znear, float zfar, float* m16)
{
float temp, temp2, temp3, temp4;
temp = 2.0f * znear;
temp2 = right - left;
temp3 = top - bottom;
temp4 = zfar - znear;
m16[0] = temp / temp2;
m16[1] = 0.0;
m16[2] = 0.0;
m16[3] = 0.0;
m16[4] = 0.0;
m16[5] = temp / temp3;
m16[6] = 0.0;
m16[7] = 0.0;
m16[8] = (right + left) / temp2;
m16[9] = (top + bottom) / temp3;
m16[10] = (-zfar - znear) / temp4;
m16[11] = -1.0f;
m16[12] = 0.0;
m16[13] = 0.0;
m16[14] = (-temp * zfar) / temp4;
m16[15] = 0.0;
}
void Perspective(float fovyInDegrees, float aspectRatio, float znear, float zfar, float* m16)
{
float ymax, xmax;
ymax = znear * tanf(fovyInDegrees * DEG2RAD);
xmax = ymax * aspectRatio;
Frustum(-xmax, xmax, -ymax, ymax, znear, zfar, m16);
}
void Cross(const float* a, const float* b, float* r)
{
r[0] = a[1] * b[2] - a[2] * b[1];
r[1] = a[2] * b[0] - a[0] * b[2];
r[2] = a[0] * b[1] - a[1] * b[0];
}
float Dot(const float* a, const float* b)
{
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
void Normalize(const float* a, float* r)
{
float il = 1.f / (sqrtf(Dot(a, a)) + FLT_EPSILON);
r[0] = a[0] * il;
r[1] = a[1] * il;
r[2] = a[2] * il;
}
void LookAt(const float* eye, const float* at, const float* up, float* m16)
{
float X[3], Y[3], Z[3], tmp[3];
tmp[0] = eye[0] - at[0];
tmp[1] = eye[1] - at[1];
tmp[2] = eye[2] - at[2];
Normalize(tmp, Z);
Normalize(up, Y);
Cross(Y, Z, tmp);
Normalize(tmp, X);
Cross(Z, X, tmp);
Normalize(tmp, Y);
m16[0] = X[0];
m16[1] = Y[0];
m16[2] = Z[0];
m16[3] = 0.0f;
m16[4] = X[1];
m16[5] = Y[1];
m16[6] = Z[1];
m16[7] = 0.0f;
m16[8] = X[2];
m16[9] = Y[2];
m16[10] = Z[2];
m16[11] = 0.0f;
m16[12] = -Dot(X, eye);
m16[13] = -Dot(Y, eye);
m16[14] = -Dot(Z, eye);
m16[15] = 1.0f;
}
template <typename T> T Clamp(T x, T y, T z) { return ((x < y) ? y : ((x > z) ? z : x)); }
template <typename T> T max(T x, T y) { return (x > y) ? x : y; }
template <typename T> T min(T x, T y) { return (x < y) ? x : y; }
template <typename T> bool IsWithin(T x, T y, T z) { return (x >= y) && (x <= z); }
struct matrix_t;
struct vec_t
{
public:
float x, y, z, w;
void Lerp(const vec_t& v, float t)
{
x += (v.x - x) * t;
y += (v.y - y) * t;
z += (v.z - z) * t;
w += (v.w - w) * t;
}
void Set(float v) { x = y = z = w = v; }
void Set(float _x, float _y, float _z = 0.f, float _w = 0.f) { x = _x; y = _y; z = _z; w = _w; }
vec_t& operator -= (const vec_t& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
vec_t& operator += (const vec_t& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
vec_t& operator *= (const vec_t& v) { x *= v.x; y *= v.y; z *= v.z; w *= v.w; return *this; }
vec_t& operator *= (float v) { x *= v; y *= v; z *= v; w *= v; return *this; }
vec_t operator * (float f) const;
vec_t operator - () const;
vec_t operator - (const vec_t& v) const;
vec_t operator + (const vec_t& v) const;
vec_t operator * (const vec_t& v) const;
const vec_t& operator + () const { return (*this); }
float Length() const { return sqrtf(x * x + y * y + z * z); };
float LengthSq() const { return (x * x + y * y + z * z); };
vec_t Normalize() { (*this) *= (1.f / ( Length() > FLT_EPSILON ? Length() : FLT_EPSILON ) ); return (*this); }
vec_t Normalize(const vec_t& v) { this->Set(v.x, v.y, v.z, v.w); this->Normalize(); return (*this); }
vec_t Abs() const;
void Cross(const vec_t& v)
{
vec_t res;
res.x = y * v.z - z * v.y;
res.y = z * v.x - x * v.z;
res.z = x * v.y - y * v.x;
x = res.x;
y = res.y;
z = res.z;
w = 0.f;
}
void Cross(const vec_t& v1, const vec_t& v2)
{
x = v1.y * v2.z - v1.z * v2.y;
y = v1.z * v2.x - v1.x * v2.z;
z = v1.x * v2.y - v1.y * v2.x;
w = 0.f;
}
float Dot(const vec_t& v) const
{
return (x * v.x) + (y * v.y) + (z * v.z) + (w * v.w);
}
float Dot3(const vec_t& v) const
{
return (x * v.x) + (y * v.y) + (z * v.z);
}
void Transform(const matrix_t& matrix);
void Transform(const vec_t& s, const matrix_t& matrix);
void TransformVector(const matrix_t& matrix);
void TransformPoint(const matrix_t& matrix);
void TransformVector(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformVector(matrix); }
void TransformPoint(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformPoint(matrix); }
float& operator [] (size_t index) { return ((float*)&x)[index]; }
const float& operator [] (size_t index) const { return ((float*)&x)[index]; }
bool operator!=(const vec_t& other) const { return memcmp(this, &other, sizeof(vec_t)) != 0; }
};
vec_t makeVect(float _x, float _y, float _z = 0.f, float _w = 0.f) { vec_t res; res.x = _x; res.y = _y; res.z = _z; res.w = _w; return res; }
vec_t makeVect(ImVec2 v) { vec_t res; res.x = v.x; res.y = v.y; res.z = 0.f; res.w = 0.f; return res; }
vec_t vec_t::operator * (float f) const { return makeVect(x * f, y * f, z * f, w * f); }
vec_t vec_t::operator - () const { return makeVect(-x, -y, -z, -w); }
vec_t vec_t::operator - (const vec_t& v) const { return makeVect(x - v.x, y - v.y, z - v.z, w - v.w); }
vec_t vec_t::operator + (const vec_t& v) const { return makeVect(x + v.x, y + v.y, z + v.z, w + v.w); }
vec_t vec_t::operator * (const vec_t& v) const { return makeVect(x * v.x, y * v.y, z * v.z, w * v.w); }
vec_t vec_t::Abs() const { return makeVect(fabsf(x), fabsf(y), fabsf(z)); }
vec_t Normalized(const vec_t& v) { vec_t res; res = v; res.Normalize(); return res; }
vec_t Cross(const vec_t& v1, const vec_t& v2)
{
vec_t res;
res.x = v1.y * v2.z - v1.z * v2.y;
res.y = v1.z * v2.x - v1.x * v2.z;
res.z = v1.x * v2.y - v1.y * v2.x;
res.w = 0.f;
return res;
}
float Dot(const vec_t& v1, const vec_t& v2)
{
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}
vec_t BuildPlan(const vec_t& p_point1, const vec_t& p_normal)
{
vec_t normal, res;
normal.Normalize(p_normal);
res.w = normal.Dot(p_point1);
res.x = normal.x;
res.y = normal.y;
res.z = normal.z;
return res;
}
struct matrix_t
{
public:
union
{
float m[4][4];
float m16[16];
struct
{
vec_t right, up, dir, position;
} v;
vec_t component[4];
};
operator float* () { return m16; }
operator const float* () const { return m16; }
void Translation(float _x, float _y, float _z) { this->Translation(makeVect(_x, _y, _z)); }
void Translation(const vec_t& vt)
{
v.right.Set(1.f, 0.f, 0.f, 0.f);
v.up.Set(0.f, 1.f, 0.f, 0.f);
v.dir.Set(0.f, 0.f, 1.f, 0.f);
v.position.Set(vt.x, vt.y, vt.z, 1.f);
}
void Scale(float _x, float _y, float _z)
{
v.right.Set(_x, 0.f, 0.f, 0.f);
v.up.Set(0.f, _y, 0.f, 0.f);
v.dir.Set(0.f, 0.f, _z, 0.f);
v.position.Set(0.f, 0.f, 0.f, 1.f);
}
void Scale(const vec_t& s) { Scale(s.x, s.y, s.z); }
matrix_t& operator *= (const matrix_t& mat)
{
matrix_t tmpMat;
tmpMat = *this;
tmpMat.Multiply(mat);
*this = tmpMat;
return *this;
}
matrix_t operator * (const matrix_t& mat) const
{
matrix_t matT;
matT.Multiply(*this, mat);
return matT;
}
void Multiply(const matrix_t& matrix)
{
matrix_t tmp;
tmp = *this;
FPU_MatrixF_x_MatrixF((float*)&tmp, (float*)&matrix, (float*)this);
}
void Multiply(const matrix_t& m1, const matrix_t& m2)
{
FPU_MatrixF_x_MatrixF((float*)&m1, (float*)&m2, (float*)this);
}
float GetDeterminant() const
{
return m[0][0] * m[1][1] * m[2][2] + m[0][1] * m[1][2] * m[2][0] + m[0][2] * m[1][0] * m[2][1] -
m[0][2] * m[1][1] * m[2][0] - m[0][1] * m[1][0] * m[2][2] - m[0][0] * m[1][2] * m[2][1];
}
float Inverse(const matrix_t& srcMatrix, bool affine = false);
void SetToIdentity()
{
v.right.Set(1.f, 0.f, 0.f, 0.f);
v.up.Set(0.f, 1.f, 0.f, 0.f);
v.dir.Set(0.f, 0.f, 1.f, 0.f);
v.position.Set(0.f, 0.f, 0.f, 1.f);
}
void Transpose()
{
matrix_t tmpm;
for (int l = 0; l < 4; l++)
{
for (int c = 0; c < 4; c++)
{
tmpm.m[l][c] = m[c][l];
}
}
(*this) = tmpm;
}
void RotationAxis(const vec_t& axis, float angle);
void OrthoNormalize()
{
v.right.Normalize();
v.up.Normalize();
v.dir.Normalize();
}
};
void vec_t::Transform(const matrix_t& matrix)
{
vec_t out;
out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + w * matrix.m[3][0];
out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + w * matrix.m[3][1];
out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + w * matrix.m[3][2];
out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + w * matrix.m[3][3];
x = out.x;
y = out.y;
z = out.z;
w = out.w;
}
void vec_t::Transform(const vec_t& s, const matrix_t& matrix)
{
*this = s;
Transform(matrix);
}
void vec_t::TransformPoint(const matrix_t& matrix)
{
vec_t out;
out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + matrix.m[3][0];
out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + matrix.m[3][1];
out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + matrix.m[3][2];
out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + matrix.m[3][3];
x = out.x;
y = out.y;
z = out.z;
w = out.w;
}
void vec_t::TransformVector(const matrix_t& matrix)
{
vec_t out;
out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0];
out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1];
out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2];
out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3];
x = out.x;
y = out.y;
z = out.z;
w = out.w;
}
float matrix_t::Inverse(const matrix_t& srcMatrix, bool affine)
{
float det = 0;
if (affine)
{
det = GetDeterminant();
float s = 1 / det;
m[0][0] = (srcMatrix.m[1][1] * srcMatrix.m[2][2] - srcMatrix.m[1][2] * srcMatrix.m[2][1]) * s;
m[0][1] = (srcMatrix.m[2][1] * srcMatrix.m[0][2] - srcMatrix.m[2][2] * srcMatrix.m[0][1]) * s;
m[0][2] = (srcMatrix.m[0][1] * srcMatrix.m[1][2] - srcMatrix.m[0][2] * srcMatrix.m[1][1]) * s;
m[1][0] = (srcMatrix.m[1][2] * srcMatrix.m[2][0] - srcMatrix.m[1][0] * srcMatrix.m[2][2]) * s;
m[1][1] = (srcMatrix.m[2][2] * srcMatrix.m[0][0] - srcMatrix.m[2][0] * srcMatrix.m[0][2]) * s;
m[1][2] = (srcMatrix.m[0][2] * srcMatrix.m[1][0] - srcMatrix.m[0][0] * srcMatrix.m[1][2]) * s;
m[2][0] = (srcMatrix.m[1][0] * srcMatrix.m[2][1] - srcMatrix.m[1][1] * srcMatrix.m[2][0]) * s;
m[2][1] = (srcMatrix.m[2][0] * srcMatrix.m[0][1] - srcMatrix.m[2][1] * srcMatrix.m[0][0]) * s;
m[2][2] = (srcMatrix.m[0][0] * srcMatrix.m[1][1] - srcMatrix.m[0][1] * srcMatrix.m[1][0]) * s;
m[3][0] = -(m[0][0] * srcMatrix.m[3][0] + m[1][0] * srcMatrix.m[3][1] + m[2][0] * srcMatrix.m[3][2]);
m[3][1] = -(m[0][1] * srcMatrix.m[3][0] + m[1][1] * srcMatrix.m[3][1] + m[2][1] * srcMatrix.m[3][2]);
m[3][2] = -(m[0][2] * srcMatrix.m[3][0] + m[1][2] * srcMatrix.m[3][1] + m[2][2] * srcMatrix.m[3][2]);
}
else
{
// transpose matrix
float src[16];
for (int i = 0; i < 4; ++i)
{
src[i] = srcMatrix.m16[i * 4];
src[i + 4] = srcMatrix.m16[i * 4 + 1];
src[i + 8] = srcMatrix.m16[i * 4 + 2];
src[i + 12] = srcMatrix.m16[i * 4 + 3];
}
// calculate pairs for first 8 elements (cofactors)
float tmp[12]; // temp array for pairs
tmp[0] = src[10] * src[15];
tmp[1] = src[11] * src[14];
tmp[2] = src[9] * src[15];
tmp[3] = src[11] * src[13];
tmp[4] = src[9] * src[14];
tmp[5] = src[10] * src[13];
tmp[6] = src[8] * src[15];
tmp[7] = src[11] * src[12];
tmp[8] = src[8] * src[14];
tmp[9] = src[10] * src[12];
tmp[10] = src[8] * src[13];
tmp[11] = src[9] * src[12];
// calculate first 8 elements (cofactors)
m16[0] = (tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7]) - (tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7]);
m16[1] = (tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7]) - (tmp[0] * src[4] + tmp[7] * src[6] + tmp[8] * src[7]);
m16[2] = (tmp[2] * src[4] + tmp[7] * src[5] + tmp[10] * src[7]) - (tmp[3] * src[4] + tmp[6] * src[5] + tmp[11] * src[7]);
m16[3] = (tmp[5] * src[4] + tmp[8] * src[5] + tmp[11] * src[6]) - (tmp[4] * src[4] + tmp[9] * src[5] + tmp[10] * src[6]);
m16[4] = (tmp[1] * src[1] + tmp[2] * src[2] + tmp[5] * src[3]) - (tmp[0] * src[1] + tmp[3] * src[2] + tmp[4] * src[3]);
m16[5] = (tmp[0] * src[0] + tmp[7] * src[2] + tmp[8] * src[3]) - (tmp[1] * src[0] + tmp[6] * src[2] + tmp[9] * src[3]);
m16[6] = (tmp[3] * src[0] + tmp[6] * src[1] + tmp[11] * src[3]) - (tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3]);
m16[7] = (tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2]) - (tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2]);
// calculate pairs for second 8 elements (cofactors)
tmp[0] = src[2] * src[7];
tmp[1] = src[3] * src[6];
tmp[2] = src[1] * src[7];
tmp[3] = src[3] * src[5];
tmp[4] = src[1] * src[6];
tmp[5] = src[2] * src[5];
tmp[6] = src[0] * src[7];
tmp[7] = src[3] * src[4];
tmp[8] = src[0] * src[6];
tmp[9] = src[2] * src[4];
tmp[10] = src[0] * src[5];
tmp[11] = src[1] * src[4];
// calculate second 8 elements (cofactors)
m16[8] = (tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15]) - (tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15]);
m16[9] = (tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15]) - (tmp[0] * src[12] + tmp[7] * src[14] + tmp[8] * src[15]);
m16[10] = (tmp[2] * src[12] + tmp[7] * src[13] + tmp[10] * src[15]) - (tmp[3] * src[12] + tmp[6] * src[13] + tmp[11] * src[15]);
m16[11] = (tmp[5] * src[12] + tmp[8] * src[13] + tmp[11] * src[14]) - (tmp[4] * src[12] + tmp[9] * src[13] + tmp[10] * src[14]);
m16[12] = (tmp[2] * src[10] + tmp[5] * src[11] + tmp[1] * src[9]) - (tmp[4] * src[11] + tmp[0] * src[9] + tmp[3] * src[10]);
m16[13] = (tmp[8] * src[11] + tmp[0] * src[8] + tmp[7] * src[10]) - (tmp[6] * src[10] + tmp[9] * src[11] + tmp[1] * src[8]);
m16[14] = (tmp[6] * src[9] + tmp[11] * src[11] + tmp[3] * src[8]) - (tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9]);
m16[15] = (tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9]) - (tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8]);
// calculate determinant
det = src[0] * m16[0] + src[1] * m16[1] + src[2] * m16[2] + src[3] * m16[3];
// calculate matrix inverse
float invdet = 1 / det;
for (int j = 0; j < 16; ++j)
{
m16[j] *= invdet;
}
}
return det;
}
void matrix_t::RotationAxis(const vec_t& axis, float angle)
{
float length2 = axis.LengthSq();
if (length2 < FLT_EPSILON)
{
SetToIdentity();
return;
}
vec_t n = axis * (1.f / sqrtf(length2));
float s = sinf(angle);
float c = cosf(angle);
float k = 1.f - c;
float xx = n.x * n.x * k + c;
float yy = n.y * n.y * k + c;
float zz = n.z * n.z * k + c;
float xy = n.x * n.y * k;
float yz = n.y * n.z * k;
float zx = n.z * n.x * k;
float xs = n.x * s;
float ys = n.y * s;
float zs = n.z * s;
m[0][0] = xx;
m[0][1] = xy + zs;
m[0][2] = zx - ys;
m[0][3] = 0.f;
m[1][0] = xy - zs;
m[1][1] = yy;
m[1][2] = yz + xs;
m[1][3] = 0.f;
m[2][0] = zx + ys;
m[2][1] = yz - xs;
m[2][2] = zz;
m[2][3] = 0.f;
m[3][0] = 0.f;
m[3][1] = 0.f;
m[3][2] = 0.f;
m[3][3] = 1.f;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
enum MOVETYPE
{
MT_NONE,
MT_MOVE_X,
MT_MOVE_Y,
MT_MOVE_Z,
MT_MOVE_YZ,
MT_MOVE_ZX,
MT_MOVE_XY,
MT_MOVE_SCREEN,
MT_ROTATE_X,
MT_ROTATE_Y,
MT_ROTATE_Z,
MT_ROTATE_SCREEN,
MT_SCALE_X,
MT_SCALE_Y,
MT_SCALE_Z,
MT_SCALE_XYZ
};
static bool IsTranslateType(int type)
{
return type >= MT_MOVE_X && type <= MT_MOVE_SCREEN;
}
static bool IsRotateType(int type)
{
return type >= MT_ROTATE_X && type <= MT_ROTATE_SCREEN;
}
static bool IsScaleType(int type)
{
return type >= MT_SCALE_X && type <= MT_SCALE_XYZ;
}
// Matches MT_MOVE_AB order
static const OPERATION TRANSLATE_PLANS[3] = { TRANSLATE_Y | TRANSLATE_Z, TRANSLATE_X | TRANSLATE_Z, TRANSLATE_X | TRANSLATE_Y };
Style::Style()
{
// default values
TranslationLineThickness = 3.0f;
TranslationLineArrowSize = 6.0f;
RotationLineThickness = 2.0f;
RotationOuterLineThickness = 3.0f;
ScaleLineThickness = 3.0f;
ScaleLineCircleSize = 6.0f;
HatchedAxisLineThickness = 6.0f;
CenterCircleSize = 6.0f;
// initialize default colors
Colors[DIRECTION_X] = ImVec4(0.666f, 0.000f, 0.000f, 1.000f);
Colors[DIRECTION_Y] = ImVec4(0.000f, 0.666f, 0.000f, 1.000f);
Colors[DIRECTION_Z] = ImVec4(0.000f, 0.000f, 0.666f, 1.000f);
Colors[PLANE_X] = ImVec4(0.666f, 0.000f, 0.000f, 0.380f);
Colors[PLANE_Y] = ImVec4(0.000f, 0.666f, 0.000f, 0.380f);
Colors[PLANE_Z] = ImVec4(0.000f, 0.000f, 0.666f, 0.380f);
Colors[SELECTION] = ImVec4(1.000f, 0.500f, 0.062f, 0.541f);
Colors[INACTIVE] = ImVec4(0.600f, 0.600f, 0.600f, 0.600f);
Colors[TRANSLATION_LINE] = ImVec4(0.666f, 0.666f, 0.666f, 0.666f);
Colors[SCALE_LINE] = ImVec4(0.250f, 0.250f, 0.250f, 1.000f);
Colors[ROTATION_USING_BORDER] = ImVec4(1.000f, 0.500f, 0.062f, 1.000f);
Colors[ROTATION_USING_FILL] = ImVec4(1.000f, 0.500f, 0.062f, 0.500f);
Colors[HATCHED_AXIS_LINES] = ImVec4(0.000f, 0.000f, 0.000f, 0.500f);
Colors[TEXT] = ImVec4(1.000f, 1.000f, 1.000f, 1.000f);
Colors[TEXT_SHADOW] = ImVec4(0.000f, 0.000f, 0.000f, 1.000f);
}
struct Context
{
Context() : mbUsing(false), mbUsingViewManipulate(false), mbEnable(true), mIsViewManipulatorHovered(false), mbUsingBounds(false)
{
}
ImDrawList* mDrawList;
Style mStyle;
MODE mMode;
matrix_t mViewMat;
matrix_t mProjectionMat;
matrix_t mModel;
matrix_t mModelLocal; // orthonormalized model
matrix_t mModelInverse;
matrix_t mModelSource;
matrix_t mModelSourceInverse;
matrix_t mMVP;
matrix_t mMVPLocal; // MVP with full model matrix whereas mMVP's model matrix might only be translation in case of World space edition
matrix_t mViewProjection;
vec_t mModelScaleOrigin;
vec_t mCameraEye;
vec_t mCameraRight;
vec_t mCameraDir;
vec_t mCameraUp;
vec_t mRayOrigin;
vec_t mRayVector;
float mRadiusSquareCenter;
ImVec2 mScreenSquareCenter;
ImVec2 mScreenSquareMin;
ImVec2 mScreenSquareMax;
float mScreenFactor;
vec_t mRelativeOrigin;
bool mbUsing;
bool mbUsingViewManipulate;
bool mbEnable;
bool mbMouseOver;
bool mReversed; // reversed projection matrix
bool mIsViewManipulatorHovered;
// translation
vec_t mTranslationPlan;
vec_t mTranslationPlanOrigin;
vec_t mMatrixOrigin;
vec_t mTranslationLastDelta;
// rotation
vec_t mRotationVectorSource;
float mRotationAngle;
float mRotationAngleOrigin;
//vec_t mWorldToLocalAxis;
// scale
vec_t mScale;
vec_t mScaleValueOrigin;
vec_t mScaleLast;
float mSaveMousePosx;
// save axis factor when using gizmo
bool mBelowAxisLimit[3];
int mAxisMask = 0;
bool mBelowPlaneLimit[3];
float mAxisFactor[3];
float mAxisLimit=0.0025f;
float mPlaneLimit=0.02f;
// bounds stretching
vec_t mBoundsPivot;
vec_t mBoundsAnchor;
vec_t mBoundsPlan;
vec_t mBoundsLocalPivot;
int mBoundsBestAxis;
int mBoundsAxis[2];
bool mbUsingBounds;
matrix_t mBoundsMatrix;
//
int mCurrentOperation;
float mX = 0.f;
float mY = 0.f;
float mWidth = 0.f;
float mHeight = 0.f;
float mXMax = 0.f;
float mYMax = 0.f;
float mDisplayRatio = 1.f;
bool mIsOrthographic = false;
// check to not have multiple gizmo highlighted at the same time
bool mbOverGizmoHotspot = false;
ImGuiWindow* mAlternativeWindow = nullptr;
ImVector<ImGuiID> mIDStack;
ImGuiID mEditingID = -1;
OPERATION mOperation = OPERATION(-1);
bool mAllowAxisFlip = true;
float mGizmoSizeClipSpace = 0.1f;
inline ImGuiID GetCurrentID()
{
if (mIDStack.empty())
{
mIDStack.push_back(-1);
}
return mIDStack.back();
}
};
static Context gContext;
static const vec_t directionUnary[3] = { makeVect(1.f, 0.f, 0.f), makeVect(0.f, 1.f, 0.f), makeVect(0.f, 0.f, 1.f) };
static const char* translationInfoMask[] = { "X : %5.3f", "Y : %5.3f", "Z : %5.3f",
"Y : %5.3f Z : %5.3f", "X : %5.3f Z : %5.3f", "X : %5.3f Y : %5.3f",
"X : %5.3f Y : %5.3f Z : %5.3f" };
static const char* scaleInfoMask[] = { "X : %5.2f", "Y : %5.2f", "Z : %5.2f", "XYZ : %5.2f" };
static const char* rotationInfoMask[] = { "X : %5.2f deg %5.2f rad", "Y : %5.2f deg %5.2f rad", "Z : %5.2f deg %5.2f rad", "Screen : %5.2f deg %5.2f rad" };
static const int translationInfoIndex[] = { 0,0,0, 1,0,0, 2,0,0, 1,2,0, 0,2,0, 0,1,0, 0,1,2 };
static const float quadMin = 0.5f;
static const float quadMax = 0.8f;
static const float quadUV[8] = { quadMin, quadMin, quadMin, quadMax, quadMax, quadMax, quadMax, quadMin };
static const int halfCircleSegmentCount = 64;
static const float snapTension = 0.5f;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
static int GetMoveType(OPERATION op, vec_t* gizmoHitProportion);
static int GetRotateType(OPERATION op);
static int GetScaleType(OPERATION op);
Style& GetStyle()
{
return gContext.mStyle;
}
static ImU32 GetColorU32(int idx)
{
IM_ASSERT(idx < COLOR::COUNT);
return ImGui::ColorConvertFloat4ToU32(gContext.mStyle.Colors[idx]);
}
static ImVec2 worldToPos(const vec_t& worldPos, const matrix_t& mat, ImVec2 position = ImVec2(gContext.mX, gContext.mY), ImVec2 size = ImVec2(gContext.mWidth, gContext.mHeight))
{
vec_t trans;
trans.TransformPoint(worldPos, mat);
trans *= 0.5f / trans.w;
trans += makeVect(0.5f, 0.5f);
trans.y = 1.f - trans.y;
trans.x *= size.x;
trans.y *= size.y;
trans.x += position.x;
trans.y += position.y;
return ImVec2(trans.x, trans.y);
}
static void ComputeCameraRay(vec_t& rayOrigin, vec_t& rayDir, ImVec2 position = ImVec2(gContext.mX, gContext.mY), ImVec2 size = ImVec2(gContext.mWidth, gContext.mHeight))
{
ImGuiIO& io = ImGui::GetIO();
matrix_t mViewProjInverse;
mViewProjInverse.Inverse(gContext.mViewMat * gContext.mProjectionMat);
const float mox = ((io.MousePos.x - position.x) / size.x) * 2.f - 1.f;
const float moy = (1.f - ((io.MousePos.y - position.y) / size.y)) * 2.f - 1.f;
const float zNear = gContext.mReversed ? (1.f - FLT_EPSILON) : 0.f;
const float zFar = gContext.mReversed ? 0.f : (1.f - FLT_EPSILON);
rayOrigin.Transform(makeVect(mox, moy, zNear, 1.f), mViewProjInverse);
rayOrigin *= 1.f / rayOrigin.w;
vec_t rayEnd;
rayEnd.Transform(makeVect(mox, moy, zFar, 1.f), mViewProjInverse);
rayEnd *= 1.f / rayEnd.w;
rayDir = Normalized(rayEnd - rayOrigin);
}
static float GetSegmentLengthClipSpace(const vec_t& start, const vec_t& end, const bool localCoordinates = false)
{
vec_t startOfSegment = start;
const matrix_t& mvp = localCoordinates ? gContext.mMVPLocal : gContext.mMVP;
startOfSegment.TransformPoint(mvp);
if (fabsf(startOfSegment.w) > FLT_EPSILON) // check for axis aligned with camera direction
{
startOfSegment *= 1.f / startOfSegment.w;
}
vec_t endOfSegment = end;
endOfSegment.TransformPoint(mvp);
if (fabsf(endOfSegment.w) > FLT_EPSILON) // check for axis aligned with camera direction
{
endOfSegment *= 1.f / endOfSegment.w;
}
vec_t clipSpaceAxis = endOfSegment - startOfSegment;
if (gContext.mDisplayRatio < 1.0)
clipSpaceAxis.x *= gContext.mDisplayRatio;
else
clipSpaceAxis.y /= gContext.mDisplayRatio;
float segmentLengthInClipSpace = sqrtf(clipSpaceAxis.x * clipSpaceAxis.x + clipSpaceAxis.y * clipSpaceAxis.y);
return segmentLengthInClipSpace;
}
static float GetParallelogram(const vec_t& ptO, const vec_t& ptA, const vec_t& ptB)
{
vec_t pts[] = { ptO, ptA, ptB };
for (unsigned int i = 0; i < 3; i++)
{
pts[i].TransformPoint(gContext.mMVP);
if (fabsf(pts[i].w) > FLT_EPSILON) // check for axis aligned with camera direction
{
pts[i] *= 1.f / pts[i].w;
}
}
vec_t segA = pts[1] - pts[0];
vec_t segB = pts[2] - pts[0];
segA.y /= gContext.mDisplayRatio;
segB.y /= gContext.mDisplayRatio;
vec_t segAOrtho = makeVect(-segA.y, segA.x);
segAOrtho.Normalize();
float dt = segAOrtho.Dot3(segB);
float surface = sqrtf(segA.x * segA.x + segA.y * segA.y) * fabsf(dt);
return surface;
}
inline vec_t PointOnSegment(const vec_t& point, const vec_t& vertPos1, const vec_t& vertPos2)
{
vec_t c = point - vertPos1;
vec_t V;
V.Normalize(vertPos2 - vertPos1);
float d = (vertPos2 - vertPos1).Length();
float t = V.Dot3(c);
if (t < 0.f)
{
return vertPos1;
}
if (t > d)
{
return vertPos2;
}
return vertPos1 + V * t;
}
static float IntersectRayPlane(const vec_t& rOrigin, const vec_t& rVector, const vec_t& plan)
{
const float numer = plan.Dot3(rOrigin) - plan.w;
const float denom = plan.Dot3(rVector);
if (fabsf(denom) < FLT_EPSILON) // normal is orthogonal to vector, cant intersect
{
return -1.0f;
}
return -(numer / denom);
}
static float DistanceToPlane(const vec_t& point, const vec_t& plan)
{
return plan.Dot3(point) + plan.w;
}
static bool IsInContextRect(ImVec2 p)
{
return IsWithin(p.x, gContext.mX, gContext.mXMax) && IsWithin(p.y, gContext.mY, gContext.mYMax);
}
static bool IsHoveringWindow()
{
ImGuiContext& g = *ImGui::GetCurrentContext();
ImGuiWindow* window = ImGui::FindWindowByName(gContext.mDrawList->_OwnerName);
if (g.HoveredWindow == window) // Mouse hovering drawlist window
return true;
if (gContext.mAlternativeWindow != nullptr && g.HoveredWindow == gContext.mAlternativeWindow)
return true;
if (g.HoveredWindow != NULL) // Any other window is hovered
return false;
if (ImGui::IsMouseHoveringRect(window->InnerRect.Min, window->InnerRect.Max, false)) // Hovering drawlist window rect, while no other window is hovered (for _NoInputs windows)
return true;
return false;
}
void SetRect(float x, float y, float width, float height)
{
gContext.mX = x;
gContext.mY = y;
gContext.mWidth = width;
gContext.mHeight = height;
gContext.mXMax = gContext.mX + gContext.mWidth;
gContext.mYMax = gContext.mY + gContext.mXMax;
gContext.mDisplayRatio = width / height;
}
void SetOrthographic(bool isOrthographic)
{
gContext.mIsOrthographic = isOrthographic;
}
void SetDrawlist(ImDrawList* drawlist)
{
gContext.mDrawList = drawlist ? drawlist : ImGui::GetWindowDrawList();
}
void SetImGuiContext(ImGuiContext* ctx)
{
ImGui::SetCurrentContext(ctx);
}
void BeginFrame()
{
const ImU32 flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus;
#ifdef IMGUI_HAS_VIEWPORT
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size);
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->Pos);
#else
ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::SetNextWindowPos(ImVec2(0, 0));
#endif
ImGui::PushStyleColor(ImGuiCol_WindowBg, 0);
ImGui::PushStyleColor(ImGuiCol_Border, 0);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::Begin("gizmo", NULL, flags);
gContext.mDrawList = ImGui::GetWindowDrawList();