/
VXLHelpers.cpp
823 lines (656 loc) · 19.5 KB
/
VXLHelpers.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
#include "VXLHelpers.h"
#include <vnl/vnl_cross.h>
#include <vnl/vnl_rotation_matrix.h>
#include <vnl/vnl_double_3.h>
#include <vgl/vgl_point_3d.h>
#include <vgl/vgl_vector_3d.h>
#include <vgl/vgl_sphere_3d.h>
#include <vgl/vgl_distance.h>
#include <vnl/algo/vnl_matrix_inverse.h>
#include <vnl/algo/vnl_real_eigensystem.h>
#include <vnl/algo/vnl_symmetric_eigensystem.h>
#include <vnl/vnl_real.h>
#include <vbl/vbl_array_2d.h>
//VIL
#include <vil/vil_rgb.h>
#include <vil/vil_load.h>
#include <vil/vil_save.h>
#include <vil/vil_image_view.h>
#include <vil/vil_convert.h>
// STL
#include <limits>
namespace VXLHelpers
{
/////////////// Conversions /////////////////
vgl_vector_3d<double> SubtractVector(const vgl_vector_3d<double> &V1, const vgl_vector_3d<double> &V2)
{
//returns V1 - V2
vgl_vector_3d<double> V(V1.x() - V2.x(), V1.y() - V2.y(), V1.z() - V2.z());
return V;
}
vgl_vector_3d<double> AddVector(const vgl_vector_3d<double> &V1, const vgl_vector_3d<double> &V2)
{
//returns V1 + V2
vgl_vector_3d<double> V(V1.x() + V2.x(), V1.y() + V2.y(), V1.z() + V2.z());
return V;
}
std::vector<double> vnl_vector_to_vector(const vnl_vector<double> &v)
{
std::vector<double> vec;
for(unsigned int i = 0; i < v.size(); i++)
vec.push_back(v(i));
return vec;
}
void OutputVNLVector(const vnl_vector<double> &V)
{
for(unsigned int i = 0; i < V.size(); i++)
std::cout << V[i] << " ";
std::cout << std::endl;
}
////////////// vgl conversions ////////////////
double Length(const vgl_point_3d<double> &p)
{
return vgl_point_to_vgl_vector(p).length();
}
vgl_vector_3d<double> vgl_point_to_vgl_vector(const vgl_point_3d<double> &p)
{
return vgl_vector_3d<double> (p.x(), p.y(), p.z());
}
vgl_point_3d<double> vgl_vector_to_vgl_point(const vgl_vector_3d<double> &v)
{
return vgl_point_3d<double> (v.x(), v.y(), v.z());
}
////////////// Convert between vgl and vnl ////////////////
//vnl_vector<double> vgl_vector_to_vnl_vector(const vgl_vector_3d<double> &vgl_vec)
vnl_double_3 vgl_vector_to_vnl_vector(const vgl_vector_3d<double> &vgl_vec)
{
vnl_vector<double> vnl_vec(3);
vnl_vec(0) = vgl_vec.x();
vnl_vec(1) = vgl_vec.y();
vnl_vec(2) = vgl_vec.z();
return vnl_vec;
}
vgl_vector_3d<double> vnl_vector_to_vgl_vector(const vnl_vector<double> &vnl_vec)
{
vgl_vector_3d<double> vgl_vec(vnl_vec(0), vnl_vec(1), vnl_vec(2));
return vgl_vec;
}
//vnl_vector<double> vgl_point_to_vnl_vector(const vgl_point_3d<double> &vgl_point)
vnl_double_3 vgl_point_to_vnl_vector(const vgl_point_3d<double> &vgl_point)
{
vnl_vector<double> vnl_vec(3);
vnl_vec(0) = vgl_point.x();
vnl_vec(1) = vgl_point.y();
vnl_vec(2) = vgl_point.z();
return vnl_vec;
}
vgl_point_3d<double> vnl_vector_to_vgl_point(const vnl_vector<double> &vnl_vec)
{
vgl_point_3d<double> vgl_vec(vnl_vec(0), vnl_vec(1), vnl_vec(2));
return vgl_vec;
}
//////////// Functions //////////////
vgl_vector_3d<double> cross(const vgl_vector_3d<double> &v1, const vgl_vector_3d<double> &v2)
{
vnl_vector<double> vnl_vec1 = vgl_vector_to_vnl_vector(v1);
vnl_vector<double> vnl_vec2 = vgl_vector_to_vnl_vector(v2);
vnl_vector<double> Crossed = vnl_cross_3d(vnl_vec1, vnl_vec2);
return vnl_vector_to_vgl_vector(Crossed);
}
double dot(const vgl_vector_3d<double> &v1, const vgl_vector_3d<double> &v2)
{
vnl_double_3 a = vgl_vector_to_vnl_vector(v1);
vnl_double_3 b = vgl_vector_to_vnl_vector(v2);
double dp = dot_product(a,b);
return dp;
}
vgl_vector_3d<double> normalize(const vgl_vector_3d<double> &v)
{
vnl_vector<double> vnl_vec = vgl_vector_to_vnl_vector(v);
vnl_vec.normalize();
return vnl_vector_to_vgl_vector(vnl_vec);
}
vnl_double_3x3 MakeRotation(const char WhichAxis, const double Angle)
{
//expects Angle in radians
assert((WhichAxis == 'x') || (WhichAxis == 'y') || (WhichAxis == 'z'));
vnl_matrix<double> R(3,3);
if (WhichAxis == 'x')
{
R(0,0) = 1;
R(0,1) = 0;
R(0,2) = 0;
R(1,0) = 0;
R(1,1) = cos(Angle);
R(1,2) = sin(Angle);
R(2,0) = 0;
R(2,1) = -sin(Angle);
R(2,2) = cos(Angle);
}
else if(WhichAxis == 'y')
{
R(0,0) = cos(Angle);
R(0,1) = 0;
R(0,2) = -sin(Angle);
R(1,0) = 0;
R(1,1) = 1;
R(1,2) = 0;
R(2,0) = sin(Angle);
R(2,1) = 0;
R(2,2) = cos(Angle);
}
else if(WhichAxis == 'z')
{
R(0,0) = cos(Angle);
R(0,1) = sin(Angle);
R(0,2) = 0;
R(1,0) = -sin(Angle);
R(1,1) = cos(Angle);
R(1,2) = 0;
R(2,0) = 0;
R(2,1) = 0;
R(2,2) = 1;
}
return R;
}
vgl_point_3d<double> PointAlong(const vgl_point_3d<double> &P, const vgl_vector_3d<double> &V, const double D)
{
return P + V*D;
}
/////////////////// Box /////////////////////////
// MaxPosition
// |<--width-->|
// O-----------O ---
// / /| ^
// / T / | |
// O-----------O | height
// | o | |R |
// L | centroid | | v
// | F | O ---
// Y | | / /_____depth
// | Z | |/ /
// | / O-----------O ---
// | / MinPosition
// O-----X
vgl_plane_3d<double> GetFrontPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.min_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (1,0,0);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (0,1,0);
return vgl_plane_3d<double> (P0, P1, P2);
}
vgl_plane_3d<double> GetBackPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.max_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (-1,0,0);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (0,-1,0);
return vgl_plane_3d<double> (P0, P1, P2);
}
vgl_plane_3d<double> GetTopPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.max_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (0,0,-1);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (-1,0,0);
return vgl_plane_3d<double> (P0, P1, P2);
}
vgl_plane_3d<double> GetBottomPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.min_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (1,0,0);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (0,0,1);
return vgl_plane_3d<double> (P0, P1, P2);
}
vgl_plane_3d<double> GetLeftPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.min_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (0,1,0);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (0,0,1);
return vgl_plane_3d<double> (P0, P1, P2);
}
vgl_plane_3d<double> GetRightPlane(const vgl_box_3d<double> &B)
{
vgl_point_3d<double> P0 = B.max_point();
vgl_point_3d<double> P1 = P0 + vgl_vector_3d<double> (0,0,-1);
vgl_point_3d<double> P2 = P0 + vgl_vector_3d<double> (0,-1,0);
return vgl_plane_3d<double> (P0, P1, P2);
}
std::vector<vgl_plane_3d<double> > GetAllPlanes(const vgl_box_3d<double> &B)
{
std::vector<vgl_plane_3d<double> > Planes(6);
Planes[0] = GetFrontPlane(B);
Planes[1] = GetBackPlane(B);
Planes[2] = GetLeftPlane(B);
Planes[3] = GetRightPlane(B);
Planes[4] = GetTopPlane(B);
Planes[5] = GetBottomPlane(B);
return Planes;
}
bool SameSign(double a, double b)
{
return (a < 0) == (b < 0);
}
vnl_double_3x3 RotationMatrix(vnl_double_3 axis, const double angle)
{
//make the norm of axis = angle
axis = axis.normalize();
axis = axis*angle;
vnl_double_3x3 R = vnl_rotation_matrix (axis);
return R;
}
vnl_double_3x3 MakeScaleMatrix(const double x, const double y, const double z)
{
vnl_double_3x3 s;
s.set_identity();
s(0,0) = x;
s(1,1) = y;
s(2,2) = z;
return s;
}
vnl_matrix<double> MakeTranslationMatrix(const vgl_vector_3d<double> &T)
{
vnl_matrix<double> Trans(4,4);
Trans.set_identity();
Trans(0,3) = T.x();
Trans(1,3) = T.y();
Trans(2,3) = T.z();
return Trans;
}
vnl_matrix<double> Get4x4(const vnl_double_3x3 &M)
{
vnl_matrix<double> M4(4,4);
M4.set_identity();
//M4 = vnl_matrix<double> (M,1,1);
M4(0,0) = M(0,0);
M4(0,1) = M(0,1);
M4(0,2) = M(0,1);
M4(1,0) = M(1,0);
M4(1,1) = M(1,1);
M4(1,2) = M(1,2);
M4(2,0) = M(2,0);
M4(2,1) = M(2,1);
M4(2,2) = M(2,2);
return M4;
}
vnl_double_3 Rotate(const vnl_double_3 &V, vnl_double_3 axis, const double angle)
{
vnl_double_3x3 R = RotationMatrix(axis, angle);
vnl_double_3 Rotated = R*V;
return Rotated;
}
vgl_vector_3d<double> Rotate(const vgl_vector_3d<double> &V, vgl_vector_3d<double> ax, const double angle)
{
vnl_double_3 v = vgl_vector_to_vnl_vector(V);
vnl_double_3 axis = vgl_vector_to_vnl_vector(ax);
return vnl_vector_to_vgl_vector(Rotate(v,axis,angle));
}
vgl_vector_3d<double> ProjectAonB(const vgl_vector_3d<double> &A, const vgl_vector_3d<double> &B)
{
vgl_vector_3d<double> a = A;
vgl_vector_3d<double> b = normalize(B);
return (dot_product(a,b) * b );
}
/*
vgl_vector_3d<double> ProjectAonPlane(const vgl_vector_3d<double> &A, const vgl_plane_3d<double> &P)
{
//!!! NOT COMPLETE
//vgl_line_3d_2_points<double> Line(p1, p2);
//vgl_point_3d<double> intersection = vgl_intersection(Line, P);
//vgl_point_3d<T> vgl_closest_point(vgl_plane_3d<T> const& pl, vgl_point_3d<T> const& p);
return vgl_vector_3d<double> (0,0,0);
}
*/
vnl_matrix<double> VectorToMatrix(const vnl_double_3 &A)
{
vnl_matrix<double> M(3,1);
M(0,0) = A(0);
M(1,0) = A(1);
M(2,0) = A(2);
//cout << M << endl;
return M;
}
std::vector<double> EigenValues(const vnl_double_3x3 &M)
{
/*
vnl_real_eigensystem Eigs(M);
vnl_matrix<vcl_complex<double> > Vals = Eigs.D;
vector<double> EVals;
EVals.push_back(vnl_real(Vals)(0,0));
EVals.push_back(vnl_real(Vals)(1,1));
EVals.push_back(vnl_real(Vals)(2,2));
*/
vnl_symmetric_eigensystem<double> Eigs(M);
std::vector<double> EVals;
for(unsigned int i = 0; i < M.columns(); i++)
EVals.push_back(Eigs.get_eigenvalue(i));
return EVals;
}
std::vector<double> EigenValues(const vnl_matrix<double> &M)
{
vnl_symmetric_eigensystem<double> Eigs(M);
std::vector<double> EVals;
for(unsigned int i = 0; i < M.columns(); i++)
EVals.push_back(Eigs.get_eigenvalue(i));
return EVals;
}
std::vector<vnl_double_3> EigenVectors(const vnl_double_3x3 &M)
{
std::vector<vnl_double_3> EVecs;
//assuming input matrix is symmetric
vnl_symmetric_eigensystem<double> Eigs(M);
EVecs.push_back(Eigs.get_eigenvector(0));
EVecs.push_back(Eigs.get_eigenvector(1));
EVecs.push_back(Eigs.get_eigenvector(2));
//assuming input matrix is NOT symmetric
/*
vnl_real_eigensystem Eigs(M);
vnl_double_3x3 Vecs = Eigs.Vreal;
EVecs.push_back(vnl_double_3 (Vecs(0,0), Vecs(1,0), Vecs(2,0)));
EVecs.push_back(vnl_double_3 (Vecs(0,1), Vecs(1,1), Vecs(2,1)));
EVecs.push_back(vnl_double_3 (Vecs(0,2), Vecs(1,2), Vecs(2,2)));
*/
return EVecs;
}
std::vector<vnl_vector<double> > EigenVectors(const vnl_matrix<double> &M)
{
vnl_symmetric_eigensystem<double> Eigs(M);
std::vector<vnl_vector<double> > EVecs;
for(unsigned int i = 0; i < M.columns(); i++)
EVecs.push_back(Eigs.get_eigenvector(i));
return EVecs;
}
vnl_double_3x3 OuterProduct(const vgl_vector_3d<double> &A, const vgl_vector_3d<double> &B)
{
vnl_double_3 a = vgl_vector_to_vnl_vector(A);
vnl_double_3 b = vgl_vector_to_vnl_vector(B);
vnl_double_3x3 OP = OuterProduct(a, b);
return OP;
}
vnl_double_3x3 OuterProduct(const vgl_point_3d<double> &A, const vgl_point_3d<double> &B)
{
vnl_double_3 a = vgl_point_to_vnl_vector(A);
vnl_double_3 b = vgl_point_to_vnl_vector(B);
vnl_double_3x3 OP = OuterProduct(a, b);
return OP;
}
vnl_double_3x3 OuterProduct(const vnl_double_3 &A, const vnl_double_3 &B)
{
//vnl_double_3x3 A_mat = VectorToMatrix(A);
//vnl_double_3x3 B_mat = VectorToMatrix(B);
vnl_matrix<double> A_mat = VectorToMatrix(A);
vnl_matrix<double> B_mat = VectorToMatrix(B);
vnl_double_3x3 OP = A_mat*B_mat.transpose();
return OP;
}
void WriteMatrixImageScaled(const vnl_matrix<double> &M, const std::string &Filename)
{
vil_image_view<vxl_byte> Image(M.rows(), M.columns(), 1, 1); //(ni, nj, n_planes, n_interleaved_planes)
//double Max = Tools::VectorMax(Vectorize(M));
double Max = M.max_value();
for (unsigned j = 0; j < Image.nj(); j++)
{
for (unsigned i = 0; i < Image.ni(); i++)
{
Image(i,j) = static_cast<vxl_byte>(255 * M(i,j)/Max);
//cout << "M: " << M(i,j) << endl;
//cout << "Image: " << Image(i,j) << endl;
}
}
vil_save(Image, Filename.c_str());
}
void ImageFromVector(const std::vector<double> &V, const std::string &Filename)
{
double MaxValue = 0;
for(unsigned int i = 0; i < V.size(); i++)
{
if(V[i] < 0.0)
{
std::cout << "Error: all entries must be positive!" << std::endl;
exit(-1);
}
if(V[i] > MaxValue)
{
MaxValue = V[i];
}
}
if(MaxValue == 0.0)
{
std::cout << "Error: no values!" << std::endl;
exit(-1);
}
/*
vil_image_view<vxl_byte> Image(M.rows(), M.columns(), 1, 1); //(ni, nj, n_planes, n_interleaved_planes)
for (unsigned j = 0; j < Image.nj(); j++)
{
for (unsigned i = 0; i < Image.ni(); i++)
{
Image(i,j) = static_cast<vxl_byte>(255 * M(i,j));
}
}
vil_save(Image, Filename.c_str());
*/
}
void WriteMatrixImage(const vnl_matrix<double> &M, const std::string &Filename)
{
vil_image_view<vxl_byte> Image(M.rows(), M.columns(), 1, 1); //(ni, nj, n_planes, n_interleaved_planes)
for (unsigned j = 0; j < Image.nj(); j++)
{
for (unsigned i = 0; i < Image.ni(); i++)
{
Image(i,j) = static_cast<vxl_byte>(255 * M(i,j));
}
}
vil_save(Image, Filename.c_str());
}
vnl_matrix<double> Array2DtoMatrix(const vbl_array_2d<double> &Array)
{
vnl_matrix<double> M(Array.rows(), Array.cols());
for (unsigned r = 0; r < Array.rows(); r++)
{
for (unsigned c = 0; c < Array.cols(); c++)
{
M(r,c) = Array(r,c);
}
}
return M;
}
void Write2DArrayImage(const vbl_array_2d<double> &Array, const std::string &Filename)
{
vnl_matrix<double> M = Array2DtoMatrix(Array);
WriteMatrixImage(M.transpose(), Filename);
}
vnl_matrix<double> ReadImageMatrix(const std::string &Filename)
{
vil_image_view<vxl_byte> Image = vil_load(Filename.c_str());
vnl_matrix<double> M(Image.ni(), Image.nj(), 0);
for (unsigned j = 0; j < Image.nj(); j++)
{
for (unsigned i = 0; i < Image.ni(); i++)
{
M(i,j) = static_cast<double> (Image(i,j));
}
}
return M/M.frobenius_norm();
}
//std::vector<double> Vectorize(const vnl_matrix<double> &M)
vnl_vector<double> Vectorize(const vnl_matrix<double> &M)
{
//std::vector<double> V;
vnl_vector<double> V(M.rows() * M.columns());
for (unsigned j = 0; j < M.rows(); j++)
{
for (unsigned i = 0; i < M.columns(); i++)
{
//V.push_back(M(i,j));
V[M.columns() * j + i] = M(i,j);
}
}
return V;
}
double DistToSphere(const vgl_point_3d<double> &P, const vgl_sphere_3d<double> &S)
{
double d = vgl_distance(P, S.centre());
return d - S.radius(); //will be negative if point is inside sphere
}
std::ostream& operator<<(std::ostream& output, const vgl_sphere_3d<double> &P)
{
output << "Center: " << P.centre() << " radius: " << P.radius() << std::endl;
return output;
}
vnl_double_3 Multiply4x4(const vnl_double_3 &V, const vnl_matrix<double> &M)
{
vnl_vector<double> V4(4);
V4(0) = V(0);
V4(1) = V(1);
V4(2) = V(2);
V4(3) = 1.0;
vnl_vector<double> Result(4);
Result = M * V4;
Result(0) /= Result(3);
Result(1) /= Result(3);
Result(2) /= Result(3);
vnl_double_3 ReturnVec;
ReturnVec(0) = Result(0);
ReturnVec(1) = Result(1);
ReturnVec(2) = Result(2);
return ReturnVec;
}
/*
pair<unsigned int, unsigned int> MinLocation(const vnl_matrix<double> &M)
{
double Smallest = numeric_limits<double>::infinity();
pair<unsigned int, unsigned int> SmallestIndex;
for(unsigned int r = 0; r < M.rows(); r++)
{
for(unsigned int c = 0; c < M.cols(); c++)
{
if(M(r,c) < Smallest)
{
Smallest = M(r,c);
SmallestIndex = pair<unsigned int, unsigned int> (r, c);
}
}
}
return SmallestIndex;
}
*/
vnl_double_3x3 Get3x3SubMatrix(const vnl_matrix<double> &M)
{
vnl_double_3x3 R;
for(unsigned r = 0; r < 3; r++)
{
for(unsigned c = 0; c < 3; c++)
{
R.put(r,c, M.get(r,c));
}
}
return R;
}
vbl_array_2d<double> ArraySum(const vbl_array_2d<double> &A1, const vbl_array_2d<double> &A2)
{
assert(A1.rows() == A2.rows());
assert(A1.cols() == A2.cols());
vbl_array_2d<double> SumArray(A1.rows(), A1.cols());
for(unsigned int r = 0; r < A1.rows(); r++)
{
for(unsigned int c = 0; c < A1.cols(); c++)
{
SumArray(r,c) = A1(r,c) + A2(r,c);
}
}
return SumArray;
}
vbl_array_2d<double> ArrayDifference(const vbl_array_2d<double> &A1, const vbl_array_2d<double> &A2)
{
//return A1 - A2
assert(A1.rows() == A2.rows());
assert(A1.cols() == A2.cols());
vbl_array_2d<double> DifferenceArray(A1.rows(), A1.cols());
for(unsigned int r = 0; r < A1.rows(); r++)
{
for(unsigned int c = 0; c < A1.cols(); c++)
{
DifferenceArray(r,c) = A1(r,c) - A2(r,c);
}
}
return DifferenceArray;
}
bool CloseEnough(const vnl_matrix<double> &M1, const vnl_matrix<double> &M2, const double eps)
{
unsigned int NumRows = M1.rows();
unsigned int NumCols = M1.columns();
if((M2.rows() != NumRows) || (M2.columns() != NumCols))
{
std::cout << "Dimensions do not match!" << std::endl;
return false;
}
for(unsigned int r = 0; r < NumRows; r++)
{
for(unsigned int c = 0; c < NumCols; c++)
{
if(fabs(M1(r,c) - M2(r,c)) > eps)
{
std::cout << "Failed comparison: " << "M1: " << M1(r,c) << " M2: " << M2(r,c) << " diff: " << fabs(M1(r,c) - M2(r,c)) << std::endl;
return false;
}
}
}
return true;
}
bool CloseEnough(const vnl_vector<double> &v1, const vnl_vector<double> &v2, const double eps)
{
for(unsigned int i = 0; i < v1.size(); i++)
{
if(fabs(v1[i] - v2[i]) > eps)
return false;
}
return true;
}
bool CloseEnough(const vnl_double_3 &v1, const vnl_double_3 &v2, const double eps)
{
for(unsigned int i = 0; i < 3; i++)
{
if(fabs(v1[i] - v2[i]) > eps)
return false;
}
return true;
}
bool CloseEnough(const double a, const double b, const double eps)
{
if(fabs(a-b) > eps)
return false;
return true;
}
vnl_matrix<double> Reshape(const vnl_vector<double> &V, const unsigned int rows, const unsigned int cols)
{
//This function reshapes a vector into a matrix using row major construction.
//check input sizes
if(V.size() != rows*cols)
{
std::cout << "Data sizes do not match!" << std::endl;
}
vnl_matrix<double> M(rows, cols);
unsigned int counter = 0;
for(unsigned int r = 0; r < rows; r++)
{
for(unsigned int c = 0; c < cols; c++)
{
M(r,c) = V[counter];
counter++;
}
}
return M;
}
vnl_matrix<double> MatrixPower(const vnl_matrix<double> &M, const double MatPow)
{
vnl_symmetric_eigensystem<double> Eigs(M);
vnl_matrix<double> V = Eigs.V;
vnl_diag_matrix<double> D = Eigs.D;
vnl_matrix<double> DRaised(D.rows(), D.columns(), 0.0);
for(unsigned int i = 0; i < D.rows(); i++)
{
DRaised(i,i) = pow(D(i,i), MatPow);
}
vnl_matrix<double> Raised = V * DRaised * vnl_matrix_inverse<double>(V);
return Raised;
}
} //end namespace