-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcore.cpp
More file actions
2127 lines (1754 loc) · 49.9 KB
/
core.cpp
File metadata and controls
2127 lines (1754 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BY USING OR DOWNLOADING THE SOFTWARE, YOU ARE AGREEING TO THE TERMS OF THIS LICENSE AGREEMENT. IF YOU DO NOT AGREE WITH THESE TERMS, YOU MAY NOT USE OR DOWNLOAD THE SOFTWARE.
//
// This is a license agreement ("Agreement") between you (called "Licensee" or "You" in this Agreement) and EVS Broadcast Equipment SA. (called "Licensor" in this Agreement). All rights not specifically granted to you in this Agreement are reserved for Licensor.
//
// RESERVATION OF OWNERSHIP AND GRANT OF LICENSE:
// Licensor retains exclusive ownership of any copy of the Software (as defined below) licensed under this Agreement and hereby grants to Licensee a personal, non-exclusive, non-transferable license to use the Software for noncommercial research purposes, without the right to sublicense, pursuant to the terms and conditions of this Agreement. As used in this Agreement, the term "Software" means (i) the actual copy of all or any portion of code for program routines made accessible to Licensee by Licensor pursuant to this Agreement, inclusive of backups, updates, and/or merged copies permitted hereunder or subsequently supplied by Licensor, including all or any file structures, programming instructions, user interfaces and screen formats and sequences as well as any and all documentation and instructions related to it, and (ii) all or any derivatives and/or modifications created or made by You to any of the items specified in (i).
// CONFIDENTIALITY: Licensee acknowledges that the Software is proprietary to Licensor, and as such, Licensee agrees to receive all such materials in confidence and use the Software only in accordance with the terms of this Agreement. Licensee agrees to use reasonable effort to protect the Software from unauthorized use, reproduction, distribution, or publication.
// COPYRIGHT: The Software is owned by Licensor and is protected by copyright laws and applicable international treaties and/or conventions.
// PERMITTED USES: The Software may be used for your own noncommercial internal research purposes. You understand and agree that Licensor is not obligated to implement any suggestions and/or feedback you might provide regarding the Software, but to the extent Licensor does so, you are not entitled to any compensation related thereto.
// DERIVATIVES: You may create derivatives of or make modifications to the Software, however, You agree that all and any such derivatives and modifications will be owned by Licensor and become a part of the Software licensed to You under this Agreement. You may only use such derivatives and modifications for your own noncommercial internal research purposes, and you may not otherwise use, distribute or copy such derivatives and modifications in violation of this Agreement.
// BACKUPS: If Licensee is an organization, it may make that number of copies of the Software necessary for internal noncommercial use at a single site within its organization provided that all information appearing in or on the original labels, including the copyright and trademark notices are copied onto the labels of the copies.
// USES NOT PERMITTED: You may not distribute, copy or use the Software except as explicitly permitted herein. Licensee has not been granted any trademark license as part of this Agreement and may not use the name or mark "EVS" or any renditions thereof without the prior written permission of Licensor.
// You may not sell, rent, lease, sublicense, lend, time-share or transfer, in whole or in part, or provide third parties access to prior or present versions (or any parts thereof) of the Software.
// ASSIGNMENT: You may not assign this Agreement or your rights hereunder without the prior written consent of Licensor. Any attempted assignment without such consent shall be null and void.
// TERM: The term of the license granted by this Agreement is from Licensee's acceptance of this Agreement by downloading the Software or by using the Software until terminated as provided below.
// The Agreement automatically terminates without notice if you fail to comply with any provision of this Agreement. Licensee may terminate this Agreement by ceasing using the Software. Upon any termination of this Agreement, Licensee will delete any and all copies of the Software. You agree that all provisions which operate to protect the proprietary rights of Licensor shall remain in force should breach occur and that the obligation of confidentiality described in this Agreement is binding in perpetuity and, as such, survives the term of the Agreement.
// FEE: Provided Licensee abides completely by the terms and conditions of this Agreement, there is no fee due to Licensor for Licensee's use of the Software in accordance with this Agreement.
// DISCLAIMER OF WARRANTIES: THE SOFTWARE IS PROVIDED "AS-IS" WITHOUT WARRANTY OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE OR OF NON-INFRINGEMENT. LICENSEE BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND RELATED MATERIALS.
// SUPPORT AND MAINTENANCE: No Software support or training by the Licensor is provided as part of this Agreement.
// EXCLUSIVE REMEDY AND LIMITATION OF LIABILITY: To the maximum extent permitted under applicable law, Licensor shall not be liable for direct, indirect, special, incidental, or consequential damages or lost profits related to Licensee's use of and/or inability to use the Software, even if Licensor is advised of the possibility of such damage.
// EXPORT REGULATION: Licensee agrees to comply with any and all applicable export control laws, regulations, and/or other laws related to embargoes and sanction programs.
// SEVERABILITY: If any provision(s) of this Agreement shall be held to be invalid, illegal, or unenforceable by a court or other tribunal of competent jurisdiction, the validity, legality and enforceability of the remaining provisions shall not in any way be affected or impaired thereby.
// NO IMPLIED WAIVERS: No failure or delay by Licensor in enforcing any right or remedy under this Agreement shall be construed as a waiver of any future or other exercise of such right or remedy by Licensor.
// GOVERNING LAW: This Agreement shall be construed and enforced in accordance with the laws of Belgium without reference to conflict of laws principles. You consent to the exclusive jurisdiction of the courts of Liège.
// ENTIRE AGREEMENT AND AMENDMENTS: This Agreement constitutes the sole and entire agreement between Licensee and Licensor as to the matter set forth herein and supersedes any previous agreements, understandings, and arrangements between the parties relating hereto.
#include "core.h"
#include <numeric>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Matrix3x3 Matrix3x3::identity()
{
return Matrix3x3(1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0);
}
Matrix3x3::Matrix3x3()
{
for (int i = 0; i < 9; i++)
_data[i] = 0.0;
}
Matrix3x3::Matrix3x3(const double data[9])
{
for (int i = 0; i < 9; i++)
_data[i] = data[i];
}
Matrix3x3::Matrix3x3(const Matrix3x3 &other)
{
for (int i = 0; i < 9; i++)
_data[i] = other._data[i];
}
Matrix3x3::Matrix3x3(double m11, double m12, double m13,
double m21, double m22, double m23,
double m31, double m32, double m33)
{
set(m11, m12, m13,
m21, m22, m23,
m31, m32, m33);
}
Matrix3x3::Matrix3x3(const Vector3x1 &c1, const Vector3x1 &c2, const Vector3x1 &c3)
{
set(c1[0], c2[0], c3[0],
c1[1], c2[1], c3[1],
c1[2], c2[2], c3[2]);
}
double *Matrix3x3::data()
{
return _data;
}
const double *Matrix3x3::data() const
{
return _data;
}
int Matrix3x3::nbElems() const
{
return 9;
}
double &Matrix3x3::operator[](int index)
{
return _data[index];
}
const double &Matrix3x3::operator[](int index) const
{
return _data[index];
}
Matrix3x3 Matrix3x3::operator*(const Matrix3x3 &mat) const
{
return multiply(mat);
}
Vector3x1 Matrix3x3::operator*(const Vector3x1 &vec) const
{
return multiply(vec);
}
Point3D Matrix3x3::operator*(const Point3D &point3D) const
{
return multiply(point3D);
}
Line3D Matrix3x3::operator*(const Line3D &line3D) const
{
return multiply(line3D);
}
Matrix3x3 Matrix3x3::operator*(double scalar) const
{
return multiply(scalar);
}
Matrix3x3 Matrix3x3::operator-() const
{
return Matrix3x3((-Matx33d(this->data())).val);
}
double &Matrix3x3::operator()(int row, int col)
{
return _data[row * 3 + col];
}
void Matrix3x3::set(const double data[9])
{
for (int i = 0; i < 9; i++)
_data[i] = data[i];
}
void Matrix3x3::set(double m11, double m12, double m13,
double m21, double m22, double m23,
double m31, double m32, double m33)
{
_data[0] = m11;
_data[1] = m12;
_data[2] = m13;
_data[3] = m21;
_data[4] = m22;
_data[5] = m23;
_data[6] = m31;
_data[7] = m32;
_data[8] = m33;
}
double Matrix3x3::norm() const
{
return sqrt(_data[0] * _data[0] + _data[1] * _data[1] + _data[2] * _data[2] +
_data[3] * _data[3] + _data[4] * _data[4] + _data[5] * _data[5] +
_data[6] * _data[6] + _data[7] * _data[7] + _data[8] * _data[8]);
}
Matrix3x3 Matrix3x3::inverse() const
{
double *m = (double *)_data;
Matx33d Minv = Matx33d(m).inv();
return Matrix3x3(Minv.val);
}
Matrix3x3 Matrix3x3::transpose() const
{
double *m = (double *)_data;
Matx33d Mt = Matx33d(m).t();
return Matrix3x3(Mt.val);
}
Matrix3x3 Matrix3x3::multiply(const Matrix3x3 &mat) const
{
double *m1 = (double *)_data;
double *m2 = (double *)mat._data;
Matx33d M1M2 = Matx33d(m1) * Matx33d(m2);
return Matrix3x3(M1M2.val);
}
Vector3x1 Matrix3x3::multiply(const Vector3x1 &vec) const
{
double *m = (double *)_data;
double *v = (double *)vec.data();
Matx31d MV = Matx33d(m) * Matx31d(v);
return Vector3x1(MV.val);
}
Point3D Matrix3x3::multiply(const Point3D &point3D) const
{
Vector3x1 vec = multiply(Vector3x1(point3D.hx(), point3D.hy(), point3D.hz()));
return Point3D(vec[0], vec[1], vec[2], point3D.w());
}
Line3D Matrix3x3::multiply(const Line3D &line3D) const
{
Point3D p1 = multiply(line3D.parametricEquationPoint(0.0));
Point3D p2 = multiply(line3D.parametricEquationPoint(1.0));
return Line3D(p1, p2);
}
Matrix3x3 Matrix3x3::multiply(double scalar) const
{
double *m = (double *)_data;
Matx33d ms = scalar * Matx33d(m);
return Matrix3x3(ms.val);
}
Vector3x1 Matrix3x3::column(int index) const
{
return Vector3x1(_data[index], _data[3 + index], _data[6 + index]);
}
ostream &operator<<(ostream &out, const Matrix3x3 &mat)
{
return out << "["
<< mat._data[0] << ", " << mat._data[1] << ", " << mat._data[2] << ";" << std::endl
<< mat._data[3] << ", " << mat._data[4] << ", " << mat._data[5] << ";" << std::endl
<< mat._data[6] << ", " << mat._data[7] << ", " << mat._data[8] << "]";
}
Vector4x1::Vector4x1()
{
for (int i = 0; i < 4; i++)
_data[i] = 0.;
}
Vector4x1::Vector4x1(const double data[4])
{
for (int i = 0; i < 4; i++)
_data[i] = data[i];
}
Vector4x1::Vector4x1(const Vector4x1 &other)
{
for (int i = 0; i < 4; i++)
_data[i] = other._data[i];
}
Vector4x1::Vector4x1(double v1, double v2, double v3, double v4)
{
_data[0] = v1;
_data[1] = v2;
_data[2] = v3;
_data[3] = v4;
}
double *Vector4x1::data()
{
return _data;
}
const double *Vector4x1::data() const
{
return _data;
}
int Vector4x1::nbElems() const
{
return 4;
}
double &Vector4x1::operator[](int index)
{
return _data[index];
}
const double &Vector4x1::operator[](int index) const
{
return _data[index];
}
Vector4x1 Vector4x1::operator+(const Vector4x1 &other) const
{
return Vector4x1(_data[0] + other._data[0], _data[1] + other._data[1],
_data[2] + other._data[2], _data[3] + other._data[3]);
}
Vector4x1 Vector4x1::operator-(const Vector4x1 &other) const
{
return Vector4x1(_data[0] - other._data[0], _data[1] - other._data[1],
_data[2] - other._data[2], _data[3] - other._data[3]);
}
Vector4x1 Vector4x1::operator-() const
{
return Vector4x1((-Matx41d(this->data())).val);
}
double Vector4x1::dotProduct(const Vector4x1 &vec) const
{
return _data[0] * vec._data[0] + _data[1] * vec._data[1] + _data[2] * vec._data[2] + _data[3] * vec._data[3];
}
double Vector4x1::norm() const
{
return sqrt(
_data[0] * _data[0] + _data[1] * _data[1] + _data[2] * _data[2] + _data[3] * _data[3]);
}
void Vector4x1::scale(double s)
{
_data[0] *= s;
_data[1] *= s;
_data[2] *= s;
_data[3] *= s;
}
ostream &operator<<(ostream &out, const Vector4x1 &vec)
{
return out << "[" << vec._data[0]
<< "; " << vec._data[1]
<< "; " << vec._data[2]
<< "; " << vec._data[3] << "]";
}
Vector3x1 Vector3x1::zero()
{
return Vector3x1(0.0, 0.0, 0.0);
}
Vector3x1::Vector3x1()
{
for (int i = 0; i < 3; i++)
_data[i] = 0.;
}
Vector3x1::Vector3x1(const double data[3])
{
for (int i = 0; i < 3; i++)
_data[i] = data[i];
}
Vector3x1::Vector3x1(const Vector3x1 &other)
{
for (int i = 0; i < 3; i++)
_data[i] = other._data[i];
}
Vector3x1::Vector3x1(double v1, double v2, double v3)
{
_data[0] = v1;
_data[1] = v2;
_data[2] = v3;
}
double *Vector3x1::data()
{
return _data;
}
const double *Vector3x1::data() const
{
return _data;
}
int Vector3x1::nbElems() const
{
return 3;
}
double &Vector3x1::operator[](int index)
{
return _data[index];
}
const double &Vector3x1::operator[](int index) const
{
return _data[index];
}
Vector3x1 Vector3x1::operator+(const Vector3x1 &other) const
{
return Vector3x1(_data[0] + other._data[0], _data[1] + other._data[1], _data[2] + other._data[2]);
}
Vector3x1 Vector3x1::operator-(const Vector3x1 &other) const
{
return Vector3x1(_data[0] - other._data[0], _data[1] - other._data[1], _data[2] - other._data[2]);
}
Vector3x1 Vector3x1::operator-() const
{
return Vector3x1((-Matx31d(this->data())).val);
}
Vector3x1 Vector3x1::crossProduct(const Vector3x1 &vec) const
{
Vector3x1 cp;
cp[0] = _data[1] * vec._data[2] - _data[2] * vec._data[1];
cp[1] = _data[2] * vec._data[0] - _data[0] * vec._data[2];
cp[2] = _data[0] * vec._data[1] - _data[1] * vec._data[0];
return cp;
}
double Vector3x1::dotProduct(const Vector3x1 &vec) const
{
return _data[0] * vec._data[0] + _data[1] * vec._data[1] + _data[2] * vec._data[2];
}
double Vector3x1::norm() const
{
return sqrt(_data[0] * _data[0] + _data[1] * _data[1] + _data[2] * _data[2]);
}
void Vector3x1::scale(double s)
{
_data[0] *= s;
_data[1] *= s;
_data[2] *= s;
}
Vector3x1 Vector3x1::operator*(double factor) const
{
Vector3x1 v = *this;
v.scale(factor);
return v;
}
Vector3x1 Vector3x1::operator/(double factor) const
{
Vector3x1 v = *this;
v.scale(1.0 / factor);
return v;
}
Vector3x1 Vector3x1::unitLength() const
{
return *this / this->norm();
}
ostream &operator<<(ostream &out, const Vector3x1 &vec)
{
return out << "[" << vec._data[0]
<< "; " << vec._data[1]
<< "; " << vec._data[2] << "]";
}
Line3D::Line3D()
{
first.set(0.0, 0.0, 0.0);
second = {0.0, 0.0, 1.0};
}
Line3D::Line3D(const Point3D &firstPoint, const Point3D &secondPoint)
{
if (secondPoint.atInfinity())
{
first = secondPoint.normalized();
}
else
{
first = firstPoint.normalized();
}
Point3D A = firstPoint.normalized();
Point3D B = secondPoint.normalized();
if (A.atInfinity())
{
A.set(A.hx(), A.hy(), A.hz(), 1.0);
}
if (B.atInfinity())
{
B.set(B.hx(), B.hy(), B.hz(), 1.0);
}
Vector3x1 slope(B.x() - A.x(), B.y() - A.y(), B.z() - A.z());
double slopeNorm = slope.norm();
if (slopeNorm < numeric_limits<double>::epsilon())
{
second = {0.0, 0.0, 1.0};
}
else
{
second = slope;
second.scale(1.0 / slopeNorm);
}
}
Line3D::Line3D(const Point3D ¶metricEquationPoint, const Vector3x1 ¶metricEquationSlope)
{
first = parametricEquationPoint;
double slopeNorm = parametricEquationSlope.norm();
if (slopeNorm < numeric_limits<double>::epsilon())
{
second = {0.0, 0.0, 1.0};
}
else
{
second = parametricEquationSlope;
second.scale(1.0 / slopeNorm);
}
}
Line3D::Line3D(const Line3D &other)
{
first = other.first;
second = other.second;
}
Point3D Line3D::parametricEquationPoint(double t) const
{
if (first.atInfinity())
{
return Point3D(first.hx() + t * second[0], first.hy() + t * second[1], first.hz() + t * second[2], 0.0);
}
return Point3D(first.x() + t * second[0], first.y() + t * second[1], first.z() + t * second[2], 1.0);
}
Vector3x1 Line3D::parametricEquationSlope() const
{
return second;
}
Point3D Line3D::intersection(const Plane3D &plane) const
{
return plane.intersection(*this);
}
bool Line3D::atInfinity() const
{
return first.atInfinity();
}
Point2D::Point2D()
{
_data[0] = 0.0;
_data[1] = 0.0;
_data[2] = 1.0;
}
Point2D::Point2D(const double *data) : Vector3x1(data)
{
}
Point2D::Point2D(const Point2D &other) : Vector3x1(other)
{
}
Point2D::Point2D(const Vector3x1 &vec)
{
_data[0] = vec[0];
_data[1] = vec[1];
_data[2] = vec[2];
}
Point2D::Point2D(double x, double y)
{
_data[0] = x;
_data[1] = y;
_data[2] = 1.0;
}
Point2D::Point2D(double hx, double hy, double w)
{
_data[0] = hx;
_data[1] = hy;
_data[2] = w;
}
Point2D::Point2D(const Line2D &l1, const Line2D &l2)
{
Vector3x1 cp = l1.normalized().crossProduct(l2.normalized());
_data[0] = cp[0];
_data[1] = cp[1];
_data[2] = cp[2];
*this = this->normalized();
}
double Point2D::x() const
{
return _data[0] / _data[2];
}
double Point2D::y() const
{
return _data[1] / _data[2];
}
double Point2D::hx() const
{
return _data[0];
}
double Point2D::hy() const
{
return _data[1];
}
double Point2D::w() const
{
return _data[2];
}
void Point2D::setX(double value)
{
_data[0] = value * _data[2];
}
void Point2D::setY(double value)
{
_data[1] = value * _data[2];
}
void Point2D::set(double hx, double hy, double w)
{
_data[0] = hx;
_data[1] = hy;
_data[2] = w;
}
bool Point2D::operator==(const Point2D &other) const
{
if ((_data[2] == 0.0 && other._data[2] != 0.0) ||
(_data[2] != 0.0 && other._data[2] == 0.0))
return false;
double w1 = (_data[2] == 0.0) ? 1.0 : _data[2];
double w2 = (other._data[2] == 0.0) ? 1.0 : other._data[2];
return (_data[0] * w2 == other._data[0] * w1) &&
(_data[1] * w2 == other._data[1] * w1);
}
bool Point2D::operator!=(const Point2D &other) const
{
return !operator==(other);
}
// TODO: infinity case(s)
Point2D Point2D::operator+(const Point2D &other) const
{
return Point2D(this->x() + other.x(), this->y() + other.y());
}
// TODO: infinity case(s)
Point2D Point2D::operator-(const Point2D &other) const
{
return Point2D(this->x() - other.x(), this->y() - other.y());
}
Point2D Point2D::operator-() const
{
return Point2D(-_data[0], -_data[1], _data[2]);
}
Point2D Point2D::operator*(double scale) const
{
return Point2D(scale * _data[0], scale * _data[1], _data[2]);
}
// TODO: infinity case(s)
Point2D Point2D::operator/(double scale) const
{
return Point2D(1.0 / scale * _data[0], 1.0 / scale * _data[1], _data[2]);
}
double Point2D::squaredNorm() const
{
return x() * x() + y() * y();
};
double Point2D::norm() const
{
return sqrt(squaredNorm());
}
void Point2D::scale(double s)
{
_data[0] *= s;
_data[1] *= s;
}
double Point2D::squaredDistance(const Point2D &point) const
{
double dx = x() - point.x();
double dy = y() - point.y();
return dx * dx + dy * dy;
}
double Point2D::distance(const Point2D &point) const
{
return sqrt(squaredDistance(point));
}
double Point2D::signedDistance(const Line2D &line) const
{
if (line.atInfinity())
{
return this->atInfinity() ? 0.0 : numeric_limits<double>::infinity();
}
Line2D l = line.normalized();
Point2D p = this->normalized();
return l.a() * p.hx() + l.b() * p.hy() + l.c() * p.w();
}
double Point2D::distance(const Line2D &line) const
{
return fabs(signedDistance(line));
}
double Point2D::distance(const LineSegment2D &lineSegment)
{
Line2D line(lineSegment.start(), lineSegment.end());
Point2D closestPointOnLine = this->projected(line);
double ax = lineSegment.start().x();
double ay = lineSegment.start().y();
double bx = lineSegment.end().x();
double by = lineSegment.end().y();
double cx = closestPointOnLine.x();
double cy = closestPointOnLine.y();
Point2D closestPointOnLineSegment;
double t = ((cx - ax) * (bx - ax) + (cy - ay) * (by - ay)) /
((bx - ax) * (bx - ax) + (by - ay) * (by - ay));
if (t < 0.0)
{
closestPointOnLineSegment = lineSegment.start();
}
else if (t > 1.0)
{
closestPointOnLineSegment = lineSegment.end();
}
else
{
closestPointOnLineSegment = closestPointOnLine;
}
return this->distance(closestPointOnLineSegment);
}
double Point2D::distance(const std::vector<Point2D> pointList)
{
double minDist = numeric_limits<double>::infinity();
for (const auto &point : pointList)
{
double dist = this->distance(point);
if (dist < minDist)
{
minDist = dist;
}
}
return minDist;
}
double Point2D::distance(const Polyline2D &polyline2D) const
{
double minDist = numeric_limits<double>::infinity();
for (int i = 0; i < polyline2D.size() - 1; i++)
{
Point2D closestPointOnLineSegment;
Matx21d A(polyline2D[i].x(), polyline2D[i].y());
Matx21d B(polyline2D[i + 1].x(), polyline2D[i + 1].y());
Matx21d AB = B - A;
double ABdotAB = AB.dot(AB);
if (ABdotAB < numeric_limits<double>::epsilon())
{
closestPointOnLineSegment.set(A(0), A(1));
}
else
{
Matx21d P(this->x(), this->y());
Matx21d AP = P - A;
Matx21d C = A + AP.dot(AB) / ABdotAB * AB;
Matx21d AC = C - A;
double t = AC.dot(AB) / ABdotAB;
if (t < 0.0)
{
closestPointOnLineSegment.set(A(0), A(1));
}
else if (t > 1.0)
{
closestPointOnLineSegment.set(B(0), B(1));
}
else
{
closestPointOnLineSegment.set(C(0), C(1));
}
}
double dist = this->distance(closestPointOnLineSegment);
if (dist < minDist)
{
minDist = dist;
}
}
return minDist;
}
Point2D Point2D::normalized() const
{
Point2D p(*this);
for (int i = 0; i < 3; i++)
if (fabs(p[i]) < numeric_limits<double>::epsilon())
p[i] = 0.0;
if (p.atInfinity())
{
double n = sqrt(p[0] * p[0] + p[1] * p[1]);
if (n == 0)
return p;
if (p[0] < 0)
n = -n;
p[0] /= n;
p[1] /= n;
return p;
}
p[0] /= p[2];
p[1] /= p[2];
p[2] = 1;
return p;
}
Point2D Point2D::projected(const Line2D &l) const
{
if (!l.atInfinity())
{
double xp = (l.b() * (l.b() * x() - l.a() * y()) - l.a() * l.c()) / (l.a() * l.a() + l.b() * l.b());
double yp = (l.a() * (-l.b() * x() + l.a() * y()) - l.b() * l.c()) / (l.a() * l.a() + l.b() * l.b());
return Point2D(xp, yp);
}
else
{
return Point2D(x(), y(), 0.0);
}
}
Point2D Point2D::projected(const Matrix3x3 &mat) const
{
Matx31d xp = Matx33d(mat.data()) * Matx31d(_data);
return Point2D(xp.val);
}
bool Point2D::isWithin(const cv::Size &size) const
{
return x() >= 0.0 && x() < size.width && y() >= 0.0 && y() < size.height;
}
bool Point2D::atInfinity() const
{
return _data[2] == 0;
}
Point3D::Point3D()
{
_data[0] = 0.0;
_data[1] = 0.0;
_data[2] = 0.0;
_data[3] = 1.0;
}
Point3D::Point3D(const double data[4]) : Vector4x1(data)
{
}
Point3D::Point3D(const Point3D &other) : Vector4x1(other)
{
}
Point3D::Point3D(const Vector4x1 &vec)
{
_data[0] = vec[0];
_data[1] = vec[1];
_data[2] = vec[2];
_data[3] = vec[3];
}
Point3D::Point3D(const Vector3x1 &vec)
{
_data[0] = vec[0];
_data[1] = vec[1];
_data[2] = vec[2];
_data[3] = 1.0;
}
Point3D::Point3D(double x, double y, double z)
{
_data[0] = x;
_data[1] = y;
_data[2] = z;
_data[3] = 1;
}
Point3D Point3D::operator+(const Point3D &other) const
{
// TODO: Infinity cases
return Point3D(this->x() + other.x(), this->y() + other.y(), this->z() + other.z());
}
Point3D Point3D::operator-(const Point3D &other) const
{
// TODO: Infinity cases
return Point3D(this->x() - other.x(), this->y() - other.y(), this->z() - other.z());
}
Point3D Point3D::operator-() const
{
// TODO: Infinity cases
return Point3D(-this->x(), -this->y(), -this->z());
}
Point3D Point3D::operator*(double scale) const
{
return Point3D(scale * _data[0], scale * _data[1], scale * _data[2], _data[3]);
}
// TODO: infinity case(s)
Point3D Point3D::operator/(double scale) const
{
return Point3D(1.0 / scale * _data[0], 1.0 / scale * _data[1], 1.0 / scale * _data[2], _data[3]);
}
Point3D::Point3D(double hx, double hy, double hz, double w)
{
_data[0] = hx;
_data[1] = hy;
_data[2] = hz;
_data[3] = w;
}
Point3D::Point3D(const Point2D &xy, double z)
{
_data[0] = xy.x();
_data[1] = xy.y();
_data[2] = z;
_data[3] = 1.0;
}
Point3D::Point3D(const std::vector<Line3D> &lines)
{
Mat_<double> A(3 * lines.size(), 3);
Mat_<double> b(3 * lines.size(), 1);
Mat_<double> x(3, 1);
for (int i = 0; i < lines.size(); i++)
{
auto a = lines[i].parametricEquationPoint();
auto d = lines[i].parametricEquationSlope();
auto ax = a.x();
auto ay = a.y();
auto az = a.z();
auto dx = d[0];
auto dy = d[1];
auto dz = d[2];
auto dx2 = dx * dx;
auto dy2 = dy * dy;
auto dz2 = dz * dz;
auto n2 = dx2 + dy2 + dz2;
int i1 = i * 2;
int i2 = i * 2 + 1;
int i3 = i * 2 + 2;
A(i1, 0) = n2 - dx2;
A(i1, 1) = -dx * dy;
A(i1, 2) = -dx * dz;
b(i1) = n2 * ax - ax * dx2 - ay * dx * dy - az * dx * dz;
A(i2, 0) = -dx * dy;
A(i2, 1) = n2 - dy2;
A(i2, 2) = -dy * dz;
b(i2) = n2 * ay - ax * dx * dy - ay * dy2 - az * dy * dz;
A(i3, 0) = -dx * dz;