-
Notifications
You must be signed in to change notification settings - Fork 14
/
ofxsMatrix2D.h
1046 lines (887 loc) · 32.4 KB
/
ofxsMatrix2D.h
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of openfx-supportext <https://github.com/NatronGitHub/openfx-supportext>,
* (C) 2018-2021 The Natron Developers
* (C) 2013-2018 INRIA
*
* openfx-supportext is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* openfx-supportext is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openfx-supportext. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
/*
* OFX Matrix utilities.
*/
#ifndef openfx_supportext_ofxsMatrix2D_h
#define openfx_supportext_ofxsMatrix2D_h
#include <cmath>
#include <cfloat>
namespace OFX {
// NEVER define a variable/constant in a header (said it 100 times already)
// an inline function is OK
inline double
ofxsPi()
{
return 3.14159265358979323846264338327950288419717;
}
/**
* @brief A simple 3 * 3 matrix class layed out as such:
* a b c
* d e f
* g h i
**/
inline double
ofxsToDegrees(double rad)
{
rad = rad * 180.0 / ofxsPi();
return rad;
}
inline double
ofxsToRadians(double deg)
{
deg = deg * ofxsPi() / 180.0;
return deg;
}
struct Point3D
{
double x, y, z;
Point3D()
: x(0), y(0), z(0)
{
}
Point3D(double x,
double y,
double z)
: x(x), y(y), z(z)
{
}
Point3D(const Point3D & p)
: x(p.x), y(p.y), z(p.z)
{
}
Point3D& operator=(const Point3D & p)
{
x = p.x;
y = p.y;
z = p.z;
return *this;
}
bool operator==(const Point3D & other)
{
return x == other.x && y == other.y && z == other.z;
}
};
/**
* \brief Compute the cross-product of two vectors
*
*/
inline OFX::Point3D
crossprod(const OFX::Point3D & a,
const OFX::Point3D & b)
{
OFX::Point3D c;
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
struct Point4D
{
double x, y, z, w;
Point4D()
: x(0), y(0), z(0), w(0)
{
}
Point4D(double x,
double y,
double z,
double w)
: x(x), y(y), z(z), w(w)
{
}
Point4D(const Point4D & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
{
}
double & operator() (int i)
{
switch (i) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
assert(false);
return x;
}
}
double operator() (int i) const
{
switch (i) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
assert(false);
return x;
}
}
bool operator==(const Point4D & o)
{
return x == o.x && y == o.y && z == o.z && w == o.w;
}
};
struct Matrix3x3
{
double m[3*3];
Matrix3x3()
{
std::fill(m, m + 3*3, 0.);
}
Matrix3x3(double a,
double b,
double c,
double d,
double e,
double f,
double g,
double h,
double i)
{
m[0] = a; m[1] = b; m[2] = c;
m[3] = d; m[4] = e; m[5] = f;
m[6] = g; m[7] = h; m[8] = i;
}
Matrix3x3(const Matrix3x3 & other)
{
std::copy(other.m, other.m + 3 * 3, m);
}
Matrix3x3& operator=(const Matrix3x3& other)
{
if (this != &other) { // not a self-assignment
std::copy(other.m, other.m + 3 * 3, m);
}
return *this;
}
/// Contruct from columns
Matrix3x3(const OFX::Point3D &m0,
const OFX::Point3D &m1,
const OFX::Point3D &m2)
{
m[0] = m0.x; m[1] = m1.x; m[2] = m2.x;
m[3] = m0.y; m[4] = m1.y; m[5] = m2.y;
m[6] = m0.z; m[7] = m1.z; m[8] = m2.z;
}
bool isIdentity() const
{
return m[0] == 1 && m[1] == 0 && m[2] == 0 &&
m[3] == 0 && m[4] == 1 && m[5] == 0 &&
m[6] == 0 && m[7] == 0 && m[8] == 1;
}
double & operator()(int row,
int col)
{
assert(row >= 0 && row < 3 && col >= 0 && col < 3);
return m[row * 3 + col];
}
double operator()(int row,
int col) const
{
assert(row >= 0 && row < 3 && col >= 0 && col < 3);
return m[row * 3 + col];
}
Matrix3x3 operator*(const Matrix3x3 & m2) const
{
return Matrix3x3(m[0] * m2.m[0] + m[1] * m2.m[3] + m[2] * m2.m[6],
m[0] * m2.m[1] + m[1] * m2.m[4] + m[2] * m2.m[7],
m[0] * m2.m[2] + m[1] * m2.m[5] + m[2] * m2.m[8],
m[3] * m2.m[0] + m[4] * m2.m[3] + m[5] * m2.m[6],
m[3] * m2.m[1] + m[4] * m2.m[4] + m[5] * m2.m[7],
m[3] * m2.m[2] + m[4] * m2.m[5] + m[5] * m2.m[8],
m[6] * m2.m[0] + m[7] * m2.m[3] + m[8] * m2.m[6],
m[6] * m2.m[1] + m[7] * m2.m[4] + m[8] * m2.m[7],
m[6] * m2.m[2] + m[7] * m2.m[5] + m[8] * m2.m[8]);
}
Point3D operator*(const Point3D & p) const
{
Point3D ret;
ret.x = m[0] * p.x + m[1] * p.y + m[2] * p.z;
ret.y = m[3] * p.x + m[4] * p.y + m[5] * p.z;
ret.z = m[6] * p.x + m[7] * p.y + m[8] * p.z;
return ret;
}
Matrix3x3 operator *(double s)
{
return Matrix3x3(m[0] * s,
m[1] * s,
m[2] * s,
m[3] * s,
m[4] * s,
m[5] * s,
m[6] * s,
m[7] * s,
m[8] * s);
}
Matrix3x3& operator *=(double s)
{
for (int i=0; i < 9; ++i) {
m[i] *= s;
}
return *this;
}
double determinant() const
{
return m[0] * (m[4] * m[8] - m[7] * m[5])
- m[1] * (m[3] * m[8] - m[6] * m[5])
+ m[2] * (m[3] * m[7] - m[6] * m[4]);
}
Matrix3x3 scaledAdjoint(double s) const
{
Matrix3x3 ret;
ret.m[0] = (s) * (m[4] * m[8] - m[7] * m[5]);
ret.m[3] = (s) * (m[5] * m[6] - m[3] * m[8]);
ret.m[6] = (s) * (m[3] * m[7] - m[4] * m[6]);
ret.m[1] = (s) * (m[2] * m[7] - m[1] * m[8]);
ret.m[4] = (s) * (m[0] * m[8] - m[2] * m[6]);
ret.m[7] = (s) * (m[1] * m[6] - m[0] * m[7]);
ret.m[2] = (s) * (m[1] * m[5] - m[2] * m[4]);
ret.m[5] = (s) * (m[2] * m[3] - m[0] * m[5]);
ret.m[8] = (s) * (m[0] * m[4] - m[1] * m[3]);
return ret;
}
bool inverse(Matrix3x3* invOut) const
{
double inv[9], det;
int i;
inv[0] = (m[4] * m[8] - m[7] * m[5]);
inv[3] = (m[5] * m[6] - m[3] * m[8]);
inv[6] = (m[3] * m[7] - m[4] * m[6]);
det = m[0] * inv[0] + m[1] * inv[3] + m[2] * inv[6];
if (det == 0) {
return false;
}
inv[1] = (m[2] * m[7] - m[1] * m[8]);
inv[4] = (m[0] * m[8] - m[2] * m[6]);
inv[7] = (m[1] * m[6] - m[0] * m[7]);
inv[2] = (m[1] * m[5] - m[2] * m[4]);
inv[5] = (m[2] * m[3] - m[0] * m[5]);
inv[8] = (m[0] * m[4] - m[1] * m[3]);
det = 1.0 / det;
for (i = 0; i < 9; ++i) {
invOut->m[i] = inv[i] * det;
}
return true;
/* old version
double det = determinant();
if (det == 0) {
return false;
}
*invOut = scaledAdjoint( 1. / det );
return true;
*/
}
void setIdentity()
{
m[0] = 1; m[1] = 0; m[2] = 0;
m[3] = 0; m[4] = 1; m[5] = 0;
m[6] = 0; m[7] = 0; m[8] = 1;
}
/**
* \brief Compute a homography from 4 points correspondences
* \param p1 source point
* \param p2 source point
* \param p3 source point
* \param p4 source point
* \param q1 target point
* \param q2 target point
* \param q3 target point
* \param q4 target point
* \return the homography matrix that maps pi's to qi's
*
Using four point-correspondences pi ↔ pi^, we can set up an equation system to solve for the homography matrix H.
An algorithm to obtain these parameters requiring only the inversion of a 3 × 3 equation system is as follows.
From the four point-correspondences pi ↔ pi^ with (i ∈ {1, 2, 3, 4}),
compute h1 = (p1 × p2 ) × (p3 × p4 ), h2 = (p1 × p3 ) × (p2 × p4 ), h3 = (p1 × p4 ) × (p2 × p3 ).
Also compute h1^ , h2^ , h3^ using the same principle from the points pi^.
Now, the homography matrix H can be obtained easily from
H · [h1 h2 h3] = [h1^ h2^ h3^],
which only requires the inversion of the matrix [h1 h2 h3].
Algo from:
http://www.dirk-farin.net/publications/phd/text/AB_EfficientComputationOfHomographiesFromFourCorrespondences.pdf
*/
bool setHomographyFromFourPoints(const OFX::Point3D &p1,
const OFX::Point3D &p2,
const OFX::Point3D &p3,
const OFX::Point3D &p4,
const OFX::Point3D &q1,
const OFX::Point3D &q2,
const OFX::Point3D &q3,
const OFX::Point3D &q4)
{
OFX::Matrix3x3 invHp;
OFX::Matrix3x3 Hp( crossprod( crossprod(p1, p2), crossprod(p3, p4) ),
crossprod( crossprod(p1, p3), crossprod(p2, p4) ),
crossprod( crossprod(p1, p4), crossprod(p2, p3) ) );
if ( !Hp.inverse(&invHp) ) {
return false;
}
OFX::Matrix3x3 Hq( crossprod( crossprod(q1, q2), crossprod(q3, q4) ),
crossprod( crossprod(q1, q3), crossprod(q2, q4) ),
crossprod( crossprod(q1, q4), crossprod(q2, q3) ) );
double detHq = Hq.determinant();
if (detHq == 0.) {
return false;
}
*this = Hq * invHp;
return true;
}
bool setAffineFromThreePoints(const OFX::Point3D &p1,
const OFX::Point3D &p2,
const OFX::Point3D &p3,
const OFX::Point3D &q1,
const OFX::Point3D &q2,
const OFX::Point3D &q3)
{
OFX::Matrix3x3 invHp;
OFX::Matrix3x3 Hp(p1, p2, p3);
if ( !Hp.inverse(&invHp) ) {
return false;
}
OFX::Matrix3x3 Hq(q1, q2, q3);
double detHq = Hq.determinant();
if (detHq == 0.) {
return false;
}
*this = Hq * invHp;
return true;
}
bool setSimilarityFromTwoPoints(const OFX::Point3D &p1,
const OFX::Point3D &p2,
const OFX::Point3D &q1,
const OFX::Point3D &q2)
{
// Generate a third point so that p1p3 is orthogonal to p1p2, and compute the affine transform
OFX::Point3D p3, q3;
p3.x = p1.x - (p2.y - p1.y);
p3.y = p1.y + (p2.x - p1.x);
p3.z = 1.;
q3.x = q1.x - (q2.y - q1.y);
q3.y = q1.y + (q2.x - q1.x);
q3.z = 1.;
return setAffineFromThreePoints(p1, p2, p3, q1, q2, q3);
/*
there is probably a better solution.
we have to solve for H in
[x1 x2]
[ h1 -h2 h3] [y1 y2] [x1' x2']
[ h2 h1 h4] [ 1 1] = [y1' y2']
which is equivalent to
[x1 -y1 1 0] [h1] [x1']
[x2 -y2 1 0] [h2] [x2']
[y1 x1 0 1] [h3] = [y1']
[y2 x2 0 1] [h4] [y2']
The 4x4 matrix should be easily invertible
with(linalg);
M := Matrix([[x1, -y1, 1, 0], [x2, -y2, 1, 0], [y1, x1, 0, 1], [y2, x2, 0, 1]]);
inverse(M);
*/
/*
double det = p1.x*p1.x - 2*p2.x*p1.x + p2.x*p2.x +p1.y*p1.y -2*p1.y*p2.y +p2.y*p2.y;
if (det == 0.) {
return false;
}
double h1 = (p1.x-p2.x)*(q1.x-q2.x) + (p1.y-p2.y)*(q1.y-q2.y);
double h2 = (p1.x-p2.x)*(q1.y-q2.y) - (p1.y-p2.y)*(q1.x-q2.x);
double h3 =
todo...
*/
}
bool setTranslationFromOnePoint(const OFX::Point3D &p1,
const OFX::Point3D &q1)
{
m[0] = 1.;
m[1] = 0.;
m[2] = q1.x - p1.x;
m[3] = 0.;
m[4] = 1.;
m[5] = q1.y - p1.y;
m[6] = 0.;
m[7] = 0.;
m[8] = 1.;
return true;
}
};
inline Matrix3x3 ofxsMatRotation(double rads);
inline Matrix3x3 ofxsMatRotationAroundPoint(double rads, double pointX, double pointY);
inline Matrix3x3 ofxsMatTranslation(double translateX, double translateY);
inline Matrix3x3 ofxsMatScale(double scaleX, double scaleY);
inline Matrix3x3 ofxsMatScale(double scale);
inline Matrix3x3 ofxsMatScaleAroundPoint(double scaleX, double scaleY, double pointX, double pointY);
inline Matrix3x3 ofxsMatSkewXY(double skewX, double skewY, bool skewOrderYX);
// matrix transform from destination to source, in canonical coordinates
inline Matrix3x3 ofxsMatInverseTransformCanonical(double translateX, double translateY, double scaleX, double scaleY, double skewX, double skewY, bool skewOrderYX, double rads, double centerX, double centerY);
// matrix transform from source to destination in canonical coordinates
inline Matrix3x3 ofxsMatTransformCanonical(double translateX, double translateY, double scaleX, double scaleY, double skewX, double skewY, bool skewOrderYX, double rads, double centerX, double centerY);
/// transform from pixel coordinates to canonical coordinates
inline Matrix3x3 ofxsMatPixelToCanonical(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded); //!< true if the image property kOfxImagePropField is kOfxImageFieldLower or kOfxImageFieldUpper (apply 0.5 field scale in Y
/// transform from canonical coordinates to pixel coordinates
inline Matrix3x3 ofxsMatCanonicalToPixel(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded); //!< true if the image property kOfxImagePropField is kOfxImageFieldLower or kOfxImageFieldUpper (apply 0.5 field scale in Y
// matrix transform from destination to source, in pixel coordinates
inline Matrix3x3 ofxsMatInverseTransformPixel(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded,
double translateX, double translateY,
double scaleX, double scaleY,
double skewX,
double skewY,
bool skewOrderYX,
double rads,
double centerX, double centerY);
// matrix transform from source to destination in pixel coordinates
inline Matrix3x3 ofxsMatTransformPixel(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded,
double translateX, double translateY,
double scaleX, double scaleY,
double skewX,
double skewY,
bool skewOrderYX,
double rads,
double centerX, double centerY);
struct Matrix4x4
{
double m[16];
Matrix4x4()
{
std::fill(m, m + 16, 0.);
}
Matrix4x4(const double d[16])
{
std::copy(d, d + 16, m);
}
Matrix4x4(const Matrix4x4 & other)
{
std::copy(other.m, other.m + 16, m);
}
Matrix4x4& operator=(const Matrix4x4& other)
{
if (this != &other) { // not a self-assignment
std::copy(other.m, other.m + 16, m);
}
return *this;
}
double & operator()(int row,
int col)
{
assert(row >= 0 && row < 4 && col >= 0 && col < 4);
return m[row * 4 + col];
}
double operator()(int row,
int col) const
{
assert(row >= 0 && row < 4 && col >= 0 && col < 4);
return m[row * 4 + col];
}
double determinant() const
{
double inv0 = (m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10]);
double inv4 = (-m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10]);
double inv8 = (m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9]);
double inv12 = (-m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9]);
return m[0] * inv0 + m[1] * inv4 + m[2] * inv8 + m[3] * inv12;
}
bool inverse(Matrix4x4* invOut) const
{
double inv[16], det;
int i;
inv[0] = (m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10]);
inv[4] = (-m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10]);
inv[8] = (m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9]);
inv[12] = (-m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9]);
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0) {
return false;
}
inv[1] = (-m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10]);
inv[5] = (m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10]);
inv[9] = (-m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9]);
inv[13] = (m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9]);
inv[2] = (m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6]);
inv[6] = (-m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6]);
inv[10] = (m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5]);
inv[14] = (-m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5]);
inv[3] = (-m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6]);
inv[7] = (m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6]);
inv[11] = (-m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5]);
inv[15] = (m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5]);
det = 1.0 / det;
for (i = 0; i < 16; i++) {
invOut->m[i] = inv[i] * det;
}
return true;
}
};
inline Matrix4x4
operator*(const Matrix4x4 & m1,
const Matrix4x4 & m2)
{
Matrix4x4 ret;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int x = 0; x < 4; ++x) {
ret(i, j) += m1(i, x) * m2(x, j);
}
}
}
return ret;
}
inline Point4D
operator*(const Matrix4x4 & m,
const Point4D & p)
{
Point4D ret;
for (int i = 0; i < 4; ++i) {
ret(i) = 0.;
for (int j = 0; j < 4; ++j) {
ret(i) += m(i, j) * p(j);
}
}
return ret;
}
inline Matrix4x4
matrix4x4FromMatrix3x3(const Matrix3x3 & m)
{
Matrix4x4 ret;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
ret(i,j) = m(i,j);
}
}
ret(0, 3) = 0.;
ret(1, 3) = 0.;
ret(2, 3) = 0.;
ret(3, 0) = 0.; ret(3, 1) = 0.; ret(3, 2) = 0.; ret(3, 3) = 1.;
return ret;
}
////////////////////
// IMPLEMENTATION //
////////////////////
inline Matrix3x3
ofxsMatRotation(double rads)
{
double c = std::cos(rads);
double s = std::sin(rads);
return Matrix3x3(c, s, 0, -s, c, 0, 0, 0, 1);
}
inline Matrix3x3
ofxsMatRotationAroundPoint(double rads,
double px,
double py)
{
return ofxsMatTranslation(px, py) * ( ofxsMatRotation(rads) * ofxsMatTranslation(-px, -py) );
}
inline Matrix3x3
ofxsMatTranslation(double x,
double y)
{
return Matrix3x3(1., 0., x,
0., 1., y,
0., 0., 1.);
}
inline Matrix3x3
ofxsMatScale(double x,
double y)
{
return Matrix3x3(x, 0., 0.,
0., y, 0.,
0., 0., 1.);
}
inline Matrix3x3
ofxsMatScale(double s)
{
return ofxsMatScale(s, s);
}
inline Matrix3x3
ofxsMatScaleAroundPoint(double scaleX,
double scaleY,
double px,
double py)
{
return ofxsMatTranslation(px, py) * ( ofxsMatScale(scaleX, scaleY) * ofxsMatTranslation(-px, -py) );
}
inline Matrix3x3
ofxsMatSkewXY(double skewX,
double skewY,
bool skewOrderYX)
{
return Matrix3x3(skewOrderYX ? 1. : (1. + skewX * skewY), skewX, 0.,
skewY, skewOrderYX ? (1. + skewX * skewY) : 1, 0.,
0., 0., 1.);
}
// matrix transform from destination to source
inline Matrix3x3
ofxsMatInverseTransformCanonical(double translateX,
double translateY,
double scaleX,
double scaleY,
double skewX,
double skewY,
bool skewOrderYX,
double rads,
double centerX,
double centerY)
{
///1) We translate to the center of the transform.
///2) We scale
///3) We apply skewX and skewY in the right order
///4) We rotate
///5) We apply the global translation
///5) We translate back to the origin
// avoid division by zero (use FLT_MIN instead of DBL_MIN in case there are further casts to float)
if (std::fabs(scaleX) < FLT_MIN ) {
scaleX = scaleX > 0 ? FLT_MIN : -FLT_MIN;
}
if (std::fabs(scaleY) < FLT_MIN ) {
scaleY = scaleY > 0 ? FLT_MIN : -FLT_MIN;
}
// since this is the inverse, oerations are in reverse order
return ofxsMatTranslation(centerX, centerY) *
ofxsMatScale(1. / scaleX, 1. / scaleY) *
ofxsMatSkewXY(-skewX, -skewY, !skewOrderYX) *
ofxsMatRotation(rads) *
ofxsMatTranslation(-translateX, -translateY) *
ofxsMatTranslation(-centerX, -centerY);
}
// matrix transform from source to destination
inline Matrix3x3
ofxsMatTransformCanonical(double translateX,
double translateY,
double scaleX,
double scaleY,
double skewX,
double skewY,
bool skewOrderYX,
double rads,
double centerX,
double centerY)
{
///1) We translate to the center of the transform.
///2) We scale
///3) We apply skewX and skewY in the right order
///4) We rotate
///5) We apply the global translation
///5) We translate back to the origin
return ofxsMatTranslation(centerX, centerY) *
ofxsMatTranslation(translateX, translateY) *
ofxsMatRotation(-rads) *
ofxsMatSkewXY(skewX, skewY, skewOrderYX) *
ofxsMatScale(scaleX, scaleY) *
ofxsMatTranslation(-centerX, -centerY);
}
// The transforms between pixel and canonical coordinated
// http://openfx.sourceforge.net/Documentation/1.3/ofxProgrammingReference.html#MappingCoordinates
/// transform from pixel coordinates to canonical coordinates
inline Matrix3x3
ofxsMatPixelToCanonical(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded) //!< true if the image property kOfxImagePropField is kOfxImageFieldLower or kOfxImageFieldUpper (apply 0.5 field scale in Y
{
/*
To map an X and Y coordinates from Pixel coordinates to Canonical coordinates, we perform the following multiplications...
X' = (X * PAR)/SX
Y' = Y/(SY * FS)
*/
// FIXME: when it's the Upper field, showuldn't the first pixel start at canonical coordinate (0,0.5) ?
return ofxsMatScale( pixelaspectratio / renderscaleX, 1. / ( renderscaleY * (fielded ? 0.5 : 1.0) ) );
}
/// transform from canonical coordinates to pixel coordinates
inline Matrix3x3
ofxsMatCanonicalToPixel(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded) //!< true if the image property kOfxImagePropField is kOfxImageFieldLower or kOfxImageFieldUpper (apply 0.5 field scale in Y
{
/*
To map an X and Y coordinates from Canonical coordinates to Pixel coordinates, we perform the following multiplications...
X' = (X * SX)/PAR
Y' = Y * SY * FS
*/
// FIXME: when it's the Upper field, showuldn't the first pixel start at canonical coordinate (0,0.5) ?
return ofxsMatScale( renderscaleX / pixelaspectratio, renderscaleY * (fielded ? 0.5 : 1.0) );
}
// matrix transform from destination to source
inline Matrix3x3
ofxsMatInverseTransformPixel(double pixelaspectratio, //!< 1.067 for PAL, where 720x576 pixels occupy 768x576 in canonical coords
double renderscaleX, //!< 0.5 for a half-resolution image
double renderscaleY,
bool fielded,
double translateX,
double translateY,