DGtal  1.0.0
testPointVector.cpp
Go to the documentation of this file.
1
32 #include <cstdio>
33 #include <cmath>
34 #include <iostream>
35 #include <fstream>
36 #include <vector>
37 #include <type_traits>
38 #include <functional>
39
40 #include "DGtal/base/Common.h"
41 #include "DGtal/kernel/PointVector.h"
42
43 #include "DGtalCatch.h"
44
45 using namespace DGtal;
46 using namespace std;
47
48 // Compare the value of the two parameters and also their component type.
49 #define COMPARE_VALUE_AND_TYPE(expr, check) \
50  REQUIRE( (expr) == (check) ); \
51  REQUIRE( ( std::is_same<decltype(expr)::Component, decltype(check)::Component>::value ) );
52
53 TEST_CASE( "2D Point Vector Unit tests" )
54 {
55  using Real = double;
56  using Integer = DGtal::int32_t;
57
58  typedef PointVector<2, Integer> Point2D;
59  typedef PointVector<2, Real> RealPoint2D;
60  typedef PointVector<3, Integer> Point3D;
62
63  Integer t1[] = {1,2};
64  Integer t2[] = {5,4};
65  Real t3[] = {1.5,2.5};
66  Real t4[] = {5.5,4.5};
67
68  Point2D p1( t1 );
69  Point2D p2( t2 );
70  RealPoint2D p3(t3);
71  RealPoint2D p4(t4);
72
73  Point3D p1_3d( p1[0], p1[1] );
74  Point3D p2_3d( p2[0], p2[1] );
75  RealPoint3D p3_3d( p3[0], p3[1] );
76  RealPoint3D p4_3d( p4[0], p4[1] );
77
78  SECTION("Cross products with integers")
79  {
80  COMPARE_VALUE_AND_TYPE( p1.crossProduct(p2), p1_3d.crossProduct(p2_3d) );
81  COMPARE_VALUE_AND_TYPE( crossProduct(p1, p2), crossProduct(p1_3d, p2_3d) );
82  COMPARE_VALUE_AND_TYPE( p2.crossProduct(p1), p2_3d.crossProduct(p1_3d) );
83  COMPARE_VALUE_AND_TYPE( crossProduct(p2, p1), crossProduct(p2_3d, p1_3d) );
84  }
85
86  SECTION("Cross products with reals")
87  {
88  COMPARE_VALUE_AND_TYPE( p3.crossProduct(p4), p3_3d.crossProduct(p4_3d) );
89  COMPARE_VALUE_AND_TYPE( crossProduct(p3, p4), crossProduct(p3_3d, p4_3d) );
90  COMPARE_VALUE_AND_TYPE( p4.crossProduct(p3), p4_3d.crossProduct(p3_3d) );
91  COMPARE_VALUE_AND_TYPE( crossProduct(p4, p3), crossProduct(p4_3d, p3_3d) );
92  }
93
94  SECTION("Cross products with mixed integers/reals")
95  {
96  COMPARE_VALUE_AND_TYPE( p1.crossProduct(p3), p1_3d.crossProduct(p3_3d) );
97  COMPARE_VALUE_AND_TYPE( crossProduct(p1, p3), crossProduct(p1_3d, p3_3d) );
98  COMPARE_VALUE_AND_TYPE( p3.crossProduct(p1), p3_3d.crossProduct(p1_3d) );
99  COMPARE_VALUE_AND_TYPE( crossProduct(p3, p1), crossProduct(p3_3d, p1_3d) );
100  }
101 }
102
103 TEST_CASE( "3D Point Vector Unit tests" )
104 {
105  using Real = double;
106  using Integer = DGtal::int32_t;
107
110
111  Integer t1[] = {1,2,3};
112  Integer t2[] = {5,4,3};
113  Real t3[] = {1.5,2.5,3.5};
114  Real t4[] = {5.5,4.5,3.5};
115
116  Point p1( t1 );
117  Point p2( t2 );
118  RealPoint p3(t3);
119  RealPoint p4(t4);
120
121  SECTION("Cross products with integers")
122  {
123  COMPARE_VALUE_AND_TYPE( p1.crossProduct(p2), Point(-6, 12, -6) );
124  COMPARE_VALUE_AND_TYPE( crossProduct(p1, p2), Point(-6, 12, -6) );
125  COMPARE_VALUE_AND_TYPE( p2.crossProduct(p1), Point(6, -12, 6) );
126  COMPARE_VALUE_AND_TYPE( crossProduct(p2, p1), Point(6, -12, 6) );
127  }
128
129  SECTION("Cross products with reals")
130  {
131  COMPARE_VALUE_AND_TYPE( p3.crossProduct(p4), RealPoint(-7., 14., -7.) );
132  COMPARE_VALUE_AND_TYPE( crossProduct(p3, p4), RealPoint(-7., 14., -7.) );
133  COMPARE_VALUE_AND_TYPE( p4.crossProduct(p3), RealPoint(7., -14., 7.) );
134  COMPARE_VALUE_AND_TYPE( crossProduct(p4, p3), RealPoint(7., -14., 7.) );
135  }
136
137  SECTION("Cross products with mixed integers/reals")
138  {
139  COMPARE_VALUE_AND_TYPE( p1.crossProduct(p3), RealPoint(-0.5, 1., -0.5) );
140  COMPARE_VALUE_AND_TYPE( crossProduct(p1, p3), RealPoint(-0.5, 1., -0.5) );
141  COMPARE_VALUE_AND_TYPE( p3.crossProduct(p1), RealPoint(0.5, -1., 0.5) );
142  COMPARE_VALUE_AND_TYPE( crossProduct(p3, p1), RealPoint(0.5, -1., 0.5) );
143  }
144 }
145
146 TEST_CASE( "4D Point Vector Unit tests" )
147 {
148  using Real = double;
149  using Integer = DGtal::int32_t;
150
153
154  const Real pi = std::acos(Real(-1));
155
156  Integer t1[] = {1,2,3,4};
157  Integer t2[] = {5,4,3,2};
158  Real t3[] = {1.0,-1.0,2.0,-2.0};
159  Real t4[] = {5.5,-4.5,3.5,2.5};
160
161  Point p1( t1 );
162  Point p1bis( t1 );
163  Point p2( t2 );
164  RealPoint p3(t3);
165  RealPoint p4(t4);
166  RealPoint p1r(p1);
167  RealPoint p2r(p2);
168
169  SECTION("Construction")
170  {
171  REQUIRE( p1 == Point( {1, 2, 3, 4} ) );
172  REQUIRE( p1r == RealPoint( {1., 2., 3., 4.} ) );
173
174  REQUIRE( p1 == Point( p1r ) );
175  REQUIRE( p1 == Point( RealPoint( {0.5, 2.1, 3.1, 3.9} ), DGtal::functors::Round<>() ) );
176  REQUIRE( p1 == Point( Point(0, 0, 1, 3), Point(1, 2, 2, 1), std::plus<Integer>() ) );
177  }
178
179  SECTION("Assignments")
180  {
181  Point dummy1;
182  dummy1 = p1;
183  REQUIRE( p1 == dummy1 );
184
185  Point dummy2(1, 3, 3, 5);
186  dummy2.partialCopy( Point(0, 2, 0, 4), {1, 3} );
187  REQUIRE( p1 == dummy2 );
188
189  Point dummy3(2, 2, 1, 4);
190  dummy3.partialCopyInv( Point(1, 0, 3, 0), {1, 3} );
191  REQUIRE( p1 == dummy3 );
192
193  RealPoint dummy1r;
194  dummy1r = p1;
195  REQUIRE( p1r == dummy1r );
196
197  RealPoint dummy2r(1, 3, 3, 5);
198  dummy2r.partialCopy( Point(0, 2, 0, 4), {1, 3} );
199  REQUIRE( p1r == dummy2r );
200
201  RealPoint dummy3r(2, 2, 1, 4);
202  dummy3r.partialCopyInv( Point(1, 0, 3, 0), {1, 3} );
203  REQUIRE( p1r == dummy3r );
204
205  Point dummy4(1, 3, 3, 5);
206  dummy4.partialCopy( RealPoint(0, 1.5, 0, 4.1), {1, 3}, DGtal::functors::Round<>() );
207  REQUIRE( p1 == dummy4 );
208
209  Point dummy5(2, 2, 1, 4);
210  dummy5.partialCopyInv( RealPoint(1.1, 0, 2.5, 0), {1, 3}, DGtal::functors::Round<>() );
211  REQUIRE( p1 == dummy5 );
212  }
213
214  SECTION("Comparisons")
215  {
216  // Partial equality
217  REQUIRE( p1.partialEqual( RealPoint(0, 2, 0, 4), {1, 3} ) );
218  REQUIRE( ! p1.partialEqual( RealPoint(0, 1, 0, 4), {1, 3} ) );
219  REQUIRE( p1.partialEqualInv( RealPoint(1, 0, 3, 0), {1, 3} ) );
220  REQUIRE( ! p1.partialEqualInv( RealPoint(1, 0, 2, 0), {1, 3} ) );
221
222  // Two equal points of same type
223  REQUIRE( p1 == p1bis );
224  REQUIRE( p1bis == p1 );
225  REQUIRE( ! (p1 != p1bis) );
226  REQUIRE( ! (p1bis != p1) );
227  REQUIRE( p1 <= p1bis );
228  REQUIRE( p1 >= p1bis );
229  REQUIRE( p1bis >= p1 );
230  REQUIRE( p1bis <= p1 );
231  REQUIRE( ! (p1 < p1bis) );
232  REQUIRE( ! (p1 > p1bis) );
233  REQUIRE( ! (p1bis > p1) );
234  REQUIRE( ! (p1bis < p1) );
235
236  // Two equal points of different types
237  REQUIRE( p1 == p1r );
238  REQUIRE( p1r == p1 );
239  REQUIRE( ! (p1 != p1r) );
240  REQUIRE( ! (p1r != p1) );
241  REQUIRE( p1 <= p1r );
242  REQUIRE( p1 >= p1r );
243  REQUIRE( p1r >= p1 );
244  REQUIRE( p1r <= p1 );
245  REQUIRE( ! (p1 < p1r) );
246  REQUIRE( ! (p1 > p1r) );
247  REQUIRE( ! (p1r > p1) );
248  REQUIRE( ! (p1r < p1) );
249
250  // Two ordered points of same type
251  REQUIRE( ! (p1 == p2) );
252  REQUIRE( ! (p2 == p1) );
253  REQUIRE( p1 != p2 );
254  REQUIRE( p2 != p1 );
255  REQUIRE( p1 <= p2 );
256  REQUIRE( ! (p1 >= p2) );
257  REQUIRE( p2 >= p1 );
258  REQUIRE( ! (p2 <= p1) );
259  REQUIRE( p1 < p2 );
260  REQUIRE( ! (p1 > p2) );
261  REQUIRE( p2 > p1 );
262  REQUIRE( ! (p2 < p1) );
263
264  // Two ordered points of different types
265  REQUIRE( ! (p1 == p2r) );
266  REQUIRE( ! (p2r == p1) );
267  REQUIRE( p1 != p2r );
268  REQUIRE( p2r != p1 );
269  REQUIRE( p1 <= p2r );
270  REQUIRE( ! (p1 >= p2r) );
271  REQUIRE( p2r >= p1 );
272  REQUIRE( ! (p2r <= p1) );
273  REQUIRE( p1 < p2r );
274  REQUIRE( ! (p1 > p2r) );
275  REQUIRE( p2r > p1 );
276  REQUIRE( ! (p2r < p1) );
277  }
278
279  SECTION("Min/Max of vector components")
280  {
281  REQUIRE( p3.max() == 2.0 );
282  REQUIRE( p3.min() == -2.0 );
283  REQUIRE( *p3.maxElement() == 2.0 );
284  REQUIRE( *p3.minElement() == -2.0 );
285  }
286
287  Point aPoint;
288  aPoint[ 3 ] = 0;
289  aPoint[ 2 ] = 2;
290  aPoint[ 1 ] = -1;
291  aPoint[ 0 ] = 3;
292
293  SECTION("Testing norms")
294  {
295  RealPoint normalized = aPoint.getNormalized();
296  CAPTURE( normalized );
297  REQUIRE( aPoint.norm ( Point::L_1 ) == 6 );
298  REQUIRE( aPoint.norm ( Point::L_infty ) == 3 );
299  REQUIRE( aPoint.squaredNorm() == Approx(aPoint.norm()*aPoint.norm()) );
300  REQUIRE( normalized[0] == Approx( 0.801784) );
301  REQUIRE( normalized[1] == Approx( -0.267261) );
302  REQUIRE( normalized[2] == Approx( 0.534522) );
303  REQUIRE( normalized[3] == Approx( 0.0) );
304  }
305
306  SECTION("PointVector Iterator")
307  {
308  PointVector<25,int> aPoint25;
309  for (unsigned int i=0;i<25;++i)
310  aPoint25[i] = i;
311
312  int sum = 0;
313  for (PointVector<25,int>::ConstIterator it = aPoint25.begin() ; it != aPoint25.end(); ++it)
314  sum += (*it);
315
316  CAPTURE(aPoint25);
317  CAPTURE(sum);
318  REQUIRE( sum == 300 );
319  }
320
321  SECTION("Arithmetical operators with integers")
322  {
323  COMPARE_VALUE_AND_TYPE( p1 + p2, Point(6,6,6,6) );
324  COMPARE_VALUE_AND_TYPE( p1 - p2, Point(-4,-2,0,2) );
325  COMPARE_VALUE_AND_TYPE( p1 * p2, Point(5,8,9,8) );
326  COMPARE_VALUE_AND_TYPE( p2 / p1, Point(5,2,1,0) );
327
328  COMPARE_VALUE_AND_TYPE( p1 + 2, Point(3,4,5,6) );
329  COMPARE_VALUE_AND_TYPE( 2 + p1, Point(3,4,5,6) );
330  COMPARE_VALUE_AND_TYPE( p1 - 2, Point(-1,0,1,2) );
331  COMPARE_VALUE_AND_TYPE( 2 - p1, Point(1,0,-1,-2) );
332  COMPARE_VALUE_AND_TYPE( p1 * 2, Point(2,4,6,8) );
333  COMPARE_VALUE_AND_TYPE( 2 * p1, Point(2,4,6,8) );
334  COMPARE_VALUE_AND_TYPE( p1 / 2, Point(0,1,1,2) );
335  COMPARE_VALUE_AND_TYPE( 2 / p1, Point(2,1,0,0) );
336
337  COMPARE_VALUE_AND_TYPE( -p1, Point(-1,-2,-3,-4) );
338
339  p1 *= 2; COMPARE_VALUE_AND_TYPE( p1, Point(2,4,6,8) );
340  p1 += 2; COMPARE_VALUE_AND_TYPE( p1, Point(4,6,8,10) );
341  p1 -= 2; COMPARE_VALUE_AND_TYPE( p1, Point(2,4,6,8) );
342  p1 /= 2; COMPARE_VALUE_AND_TYPE( p1, Point(1,2,3,4) );
343
344  p1 *= p2; COMPARE_VALUE_AND_TYPE( p1, Point(5,8,9,8) );
345  p1 += p2; COMPARE_VALUE_AND_TYPE( p1, Point(10,12,12,10) );
346  p1 -= p2; COMPARE_VALUE_AND_TYPE( p1, Point(5,8,9,8) );
347  p1 /= p2; COMPARE_VALUE_AND_TYPE( p1, Point(1,2,3,4) );
348  }
349
350  SECTION("Other operators with integers")
351  {
352  COMPARE_VALUE_AND_TYPE( p1.inf(p2), Point(1,2,3,2) );
353  COMPARE_VALUE_AND_TYPE( p1.sup(p2), Point(5,4,3,4) );
354  COMPARE_VALUE_AND_TYPE( inf(p1, p2), Point(1,2,3,2) );
355  COMPARE_VALUE_AND_TYPE( sup(p1, p2), Point(5,4,3,4) );
356
357  REQUIRE( p1.dot(p2) == 30 );
358  REQUIRE( dotProduct(p1, p2) == 30 );
359
360  REQUIRE( p1.cosineSimilarity(p1) == Approx(0.) );
361  REQUIRE( p1.cosineSimilarity(-p1) == Approx(pi) );
362  REQUIRE( p1.cosineSimilarity( Point(-2,1,-4,3) ) == Approx(pi/2) );
363  REQUIRE( cosineSimilarity(p1, p1) == Approx(0.) );
364  REQUIRE( cosineSimilarity(p1, -p1) == Approx(pi) );
365  REQUIRE( cosineSimilarity(p1, Point(-2,1,-4,3)) == Approx(pi/2) );
366
367  REQUIRE( p1.isLower(p2) == false );
368  REQUIRE( isLower(p1, p2) == false );
369  REQUIRE( p2.isUpper(p1) == false );
370  REQUIRE( isUpper(p2, p1) == false );
371  p1[3] = 2;
372  REQUIRE( p1.isLower(p2) == true );
373  REQUIRE( isLower(p1, p2) == true );
374  REQUIRE( p2.isUpper(p1) == true );
375  REQUIRE( isUpper(p2, p1) == true );
376  }
377
378  SECTION("Arithmetical Operators with reals")
379  {
380  COMPARE_VALUE_AND_TYPE( p3 + p4, RealPoint(6.5,-5.5,5.5,0.5) );
381  COMPARE_VALUE_AND_TYPE( p3 - p4, RealPoint(-4.5,3.5,-1.5,-4.5) );
382  COMPARE_VALUE_AND_TYPE( p3 * p4, RealPoint(5.5,4.5,7.0,-5.0) );
383  COMPARE_VALUE_AND_TYPE( p4 / p3, RealPoint(5.5,4.5,1.75,-1.25) );
384
385  COMPARE_VALUE_AND_TYPE( p3 + 2., RealPoint(3.,1.,4.,0.) );
386  COMPARE_VALUE_AND_TYPE( 2. + p3, RealPoint(3.,1.,4.,0.) );
387  COMPARE_VALUE_AND_TYPE( p3 - 2., RealPoint(-1.,-3.,0.,-4.) );
388  COMPARE_VALUE_AND_TYPE( 2. - p3, RealPoint(1.,3.,0.,4.) );
389  COMPARE_VALUE_AND_TYPE( p3 * 2., RealPoint(2.,-2.,4.,-4.) );
390  COMPARE_VALUE_AND_TYPE( 2. * p3, RealPoint(2.,-2.,4.,-4.) );
391  COMPARE_VALUE_AND_TYPE( p3 / 2., RealPoint(0.5,-0.5,1.,-1.) );
392  COMPARE_VALUE_AND_TYPE( 2. / p3, RealPoint(2.,-2.,1.,-1.) );
393
394  COMPARE_VALUE_AND_TYPE( -p3, RealPoint(-1.,1.,-2.,2.) );
395
396  p3 *= 2.5; COMPARE_VALUE_AND_TYPE( p3, RealPoint(2.5, -2.5, 5., -5.) );
397  p3 += 2.5; COMPARE_VALUE_AND_TYPE( p3, RealPoint(5., 0., 7.5, -2.5) );
398  p3 -= 2.5; COMPARE_VALUE_AND_TYPE( p3, RealPoint(2.5, -2.5, 5., -5.) );
399  p3 /= 2.5; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -1., 2., -2.) );
400
401  p3 *= p4; COMPARE_VALUE_AND_TYPE( p3, RealPoint(5.5, 4.5, 7., -5.) );
402  p3 += p4; COMPARE_VALUE_AND_TYPE( p3, RealPoint(11, 0., 10.5, -2.5) );
403  p3 -= p4; COMPARE_VALUE_AND_TYPE( p3, RealPoint(5.5, 4.5, 7., -5.) );
404  p3 /= p4; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -1., 2., -2.) );
405  }
406
407  SECTION("Other operators with reals")
408  {
409  COMPARE_VALUE_AND_TYPE( p3.inf(p4), RealPoint(1.,-4.5,2.,-2.) );
410  COMPARE_VALUE_AND_TYPE( p3.sup(p4), RealPoint(5.5,-1.,3.5,2.5) );
411  COMPARE_VALUE_AND_TYPE( inf(p3, p4), RealPoint(1.,-4.5,2.,-2.) );
412  COMPARE_VALUE_AND_TYPE( sup(p3, p4), RealPoint(5.5,-1.,3.5,2.5) );
413
414  REQUIRE( p3.dot(p4) == 12. );
415  REQUIRE( dotProduct(p3, p4) == 12. );
416
417  REQUIRE( p3.cosineSimilarity(p3) == Approx(0.) );
418  REQUIRE( p3.cosineSimilarity(-p3) == Approx(pi) );
419  REQUIRE( p3.cosineSimilarity( RealPoint(1.0,1.0,2.0,2.0) ) == Approx(pi/2) );
420  REQUIRE( cosineSimilarity(p3, p3) == Approx(0.) );
421  REQUIRE( cosineSimilarity(p3, -p3) == Approx(pi) );
422  REQUIRE( cosineSimilarity(p3, RealPoint(1.0,1.0,2.0,2.0)) == Approx(pi/2) );
423
424  REQUIRE( p3.isLower(p4) == false );
425  REQUIRE( isLower(p3, p4) == false );
426  REQUIRE( p4.isUpper(p3) == false );
427  REQUIRE( isUpper(p4, p3) == false );
428  p4[1] = -p4[1];
429  REQUIRE( p3.isLower(p4) == true );
430  REQUIRE( isLower(p3, p4) == true );
431  REQUIRE( p4.isUpper(p3) == true );
432  REQUIRE( isUpper(p4, p3) == true );
433  }
434
435  SECTION("Arithmetical Operators with mixed integers/reals")
436  {
437  COMPARE_VALUE_AND_TYPE( p1 + p4, RealPoint(6.5,-2.5,6.5,6.5) );
438  COMPARE_VALUE_AND_TYPE( p4 + p1, RealPoint(6.5,-2.5,6.5,6.5) );
439  COMPARE_VALUE_AND_TYPE( p1 - p4, RealPoint(-4.5,6.5,-0.5,1.5) );
440  COMPARE_VALUE_AND_TYPE( p4 - p1, RealPoint(4.5,-6.5,0.5,-1.5) );
441  COMPARE_VALUE_AND_TYPE( p1 * p4, RealPoint(5.5,-9.0,10.5,10.0) );
442  COMPARE_VALUE_AND_TYPE( p4 * p1, RealPoint(5.5,-9.0,10.5,10.0) );
443  COMPARE_VALUE_AND_TYPE( p1 / p3, RealPoint(1.,-2.,1.5,-2.0) );
444  COMPARE_VALUE_AND_TYPE( p3 / p1, RealPoint(1.,-0.5,2./3.,-0.5) );
445
446  COMPARE_VALUE_AND_TYPE( p3 + 2, RealPoint(3.,1.,4.,0.) );
447  COMPARE_VALUE_AND_TYPE( 2 + p3, RealPoint(3.,1.,4.,0.) );
448  COMPARE_VALUE_AND_TYPE( p3 - 2, RealPoint(-1.,-3.,0.,-4.) );
449  COMPARE_VALUE_AND_TYPE( 2 - p3, RealPoint(1.,3.,0.,4.) );
450  COMPARE_VALUE_AND_TYPE( p3 * 2, RealPoint(2.,-2.,4.,-4.) );
451  COMPARE_VALUE_AND_TYPE( 2 * p3, RealPoint(2.,-2.,4.,-4.) );
452  COMPARE_VALUE_AND_TYPE( p3 / 2, RealPoint(0.5,-0.5,1.,-1.) );
453  COMPARE_VALUE_AND_TYPE( 2 / p3, RealPoint(2.,-2.,1.,-1.) );
454
455  COMPARE_VALUE_AND_TYPE( p1 + 2.5, RealPoint(3.5,4.5,5.5,6.5) );
456  COMPARE_VALUE_AND_TYPE( 2.5 + p1, RealPoint(3.5,4.5,5.5,6.5) );
457  COMPARE_VALUE_AND_TYPE( p1 - 2.5, RealPoint(-1.5,-0.5,0.5,1.5) );
458  COMPARE_VALUE_AND_TYPE( 2.5 - p1, RealPoint(1.5,0.5,-0.5,-1.5) );
459  COMPARE_VALUE_AND_TYPE( p1 * 2.5, RealPoint(2.5,5.,7.5,10.) );
460  COMPARE_VALUE_AND_TYPE( 2.5 * p1, RealPoint(2.5,5.,7.5,10.) );
461  COMPARE_VALUE_AND_TYPE( p1 / 0.5, RealPoint(2.,4.,6.,8.) );
462  COMPARE_VALUE_AND_TYPE( 2. / p1, RealPoint(2.,1.,2./3.,0.5) );
463
464  p3 *= 2; COMPARE_VALUE_AND_TYPE( p3, RealPoint(2, -2, 4., -4.) );
465  p3 += 2; COMPARE_VALUE_AND_TYPE( p3, RealPoint(4., 0., 6., -2.) );
466  p3 -= 2; COMPARE_VALUE_AND_TYPE( p3, RealPoint(2, -2, 4., -4.) );
467  p3 /= 2; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -1., 2., -2.) );
468
469  p3 *= p1; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -2., 6., -8.) );
470  p3 += p1; COMPARE_VALUE_AND_TYPE( p3, RealPoint(2., 0., 9., -4.) );
471  p3 -= p1; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -2., 6., -8.) );
472  p3 /= p1; COMPARE_VALUE_AND_TYPE( p3, RealPoint(1., -1., 2., -2.) );
473  }
474
475  SECTION("Other operators with mixed integers/reals")
476  {
477  COMPARE_VALUE_AND_TYPE( p1.inf(p3), RealPoint(1.,-1.,2.,-2.) );
478  COMPARE_VALUE_AND_TYPE( p3.inf(p1), RealPoint(1.,-1.,2.,-2.) );
479  COMPARE_VALUE_AND_TYPE( p1.sup(p3), RealPoint(1.,2.,3.,4.) );
480  COMPARE_VALUE_AND_TYPE( p3.sup(p1), RealPoint(1.,2.,3.,4.) );
481  COMPARE_VALUE_AND_TYPE( inf(p1, p3), RealPoint(1.,-1.,2.,-2.) );
482  COMPARE_VALUE_AND_TYPE( inf(p3, p1), RealPoint(1.,-1.,2.,-2.) );
483  COMPARE_VALUE_AND_TYPE( sup(p1, p3), RealPoint(1.,2.,3.,4.) );
484  COMPARE_VALUE_AND_TYPE( sup(p3, p1), RealPoint(1.,2.,3.,4.) );
485
486  REQUIRE( p4.dot(p1) == 17.0 );
487  REQUIRE( dotProduct(p4, p1) == 17.0 );
488  REQUIRE( dotProduct(p1, p4) == 17.0 );
489
490  REQUIRE( p1.cosineSimilarity(RealPoint(p1)) == Approx(0.) );
491  REQUIRE( p1.cosineSimilarity(-RealPoint(p1)) == Approx(pi) );
492  REQUIRE( p1.cosineSimilarity( RealPoint(-2,1,-4,3) ) == Approx(pi/2) );
493  REQUIRE( cosineSimilarity(p1, RealPoint(p1)) == Approx(0.) );
494  REQUIRE( cosineSimilarity(p1, -RealPoint(p1)) == Approx(pi) );
495  REQUIRE( cosineSimilarity(p1, RealPoint(-2,1,-4,3)) == Approx(pi/2) );
496
497  REQUIRE( p3.cosineSimilarity(Point(1,-1,2,-2)) == Approx(0.) );
498  REQUIRE( p3.cosineSimilarity(-Point(1,-1,2,-2)) == Approx(pi) );
499  REQUIRE( p3.cosineSimilarity( Point(1,1,2,2) ) == Approx(pi/2) );
500  REQUIRE( cosineSimilarity(p3, Point(1,-1,2,-2)) == Approx(0.) );
501  REQUIRE( cosineSimilarity(p3, -Point(1,-1,2,-2)) == Approx(pi) );
502  REQUIRE( cosineSimilarity(p3, Point(1,1,2,2)) == Approx(pi/2) );
503
504  REQUIRE( p2.isLower(p4) == false );
505  REQUIRE( isLower(p2, p4) == false );
506  REQUIRE( p4.isUpper(p2) == false );
507  REQUIRE( isUpper(p4, p2) == false );
508  p4[1] = -p4[1];
509  REQUIRE( p2.isLower(p4) == true );
510  REQUIRE( isLower(p2, p4) == true );
511  REQUIRE( p4.isUpper(p2) == true );
512  REQUIRE( isUpper(p4, p2) == true );
513  }
514
515 }
516
517
const Point aPoint(3, 4)
CAPTURE(thicknessHV)
Component max() const
Component min() const
auto inf(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs) -> decltype(DGtal::constructFromArithmeticConversion(lhs, rhs))
Implements the infimum (or greatest lower bound).
bool isUpper(const PointVector< dim, OtherComponent, OtherStorage > &p) const
Return true if this point is upper a given point.
DGtal::ArithmeticConversionType< LeftEuclideanRing, RightEuclideanRing > dotProduct(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Dot product between two points/vectors.
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
bool isLower(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Return true if the first point is below the second point.
PointVector< 3, double > RealPoint
auto crossProduct(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::crossProduct(*this, v))
Cross product with a PointVector.
REQUIRE(domain.isInside(aPoint))
double cosineSimilarity(const PointVector< dim, OtherComponent, OtherStorage > &v) const
Positive angle between two vectors, deduced from their scalar product.
#define COMPARE_VALUE_AND_TYPE(expr, check)
bool isLower(const PointVector< dim, OtherComponent, OtherStorage > &p) const
Return true if this point is below a given point.
auto crossProduct(PointVector< 3, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< 3, RightEuclideanRing, RightContainer > const &rhs) -> decltype(DGtal::constructFromArithmeticConversion(lhs, rhs))
Cross product of two 3D Points/Vectors.
Functor that rounds to the nearest integer.
auto inf(const PointVector< dim, OtherComponent, OtherStorage > &aPoint) const -> decltype(DGtal::inf(*this, aPoint))
Implements the infimum (or greatest lower bound).
Container::const_iterator ConstIterator
Constant iterator type.
Definition: PointVector.h:641
double cosineSimilarity(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Positive angle between two vectors, deduced from their scalar product.
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
Iterator maxElement()
TEST_CASE("2D Point Vector Unit tests")
Self & partialCopyInv(const PointVector< dim, OtherComponent, OtherContainer > &pv, const std::vector< Dimension > &dimensions)
Partial copy of a given PointVector.
Iterator minElement()
Z3i::RealPoint RealPoint3D
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
auto sup(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs) -> decltype(DGtal::constructFromArithmeticConversion(lhs, rhs))
Implements the supremum (or least upper bound).
SECTION("Testing constant forward iterators")
auto sup(const PointVector< dim, OtherComponent, OtherStorage > &aPoint) const -> decltype(DGtal::sup(*this, aPoint))
Implements the supremum (or least upper bound).
bool isUpper(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Return true if the first point is upper the second point.
Self & partialCopy(const PointVector< dim, OtherComponent, OtherContainer > &pv, const std::vector< Dimension > &dimensions)
Partial copy of a given PointVector.