DGtal  1.5.beta
testPointFunctorHolder.cpp
1 
27 #include <iostream>
28 #include <cmath>
29 
30 #include "DGtal/kernel/CPointFunctor.h"
31 #include "DGtal/kernel/PointVector.h"
32 #include "DGtal/kernel/PointFunctorHolder.h"
33 
34 #include "DGtalCatch.h"
35 
36 template <typename T, typename Point>
37 T kernel(Point const& pt, Point const& center, T radius)
38 {
39  return (pt - center).norm() - radius;
40 }
41 
42 template <typename T, typename Point>
43 struct Functor
44 {
45  Point center;
46  T radius;
47 
48  Functor(Point const& c, T r)
49  : center(c), radius(r)
50  {}
51 
52  T operator() (Point const& pt) const
53  {
54  return kernel<T>(pt, center, radius);
55  }
56 };
57 
58 TEST_CASE( "2D PointFunctorHolder from functor by rvalue", "[2D][functor][rvalue]" )
59 {
60  using namespace DGtal;
61  using Point = PointVector<2, int>;
62  using Value = double;
63 
64  auto fn = functors::holdPointFunctor<Point, Value>( Functor<Value, Point>( Point(1, 0), 1 ) );
65 
66  // Checks CPointFunctor concept.
67  BOOST_CONCEPT_ASSERT( (DGtal::concepts::CPointFunctor<decltype(fn)>) );
68 
69  // Checking standard services
70  std::cout << fn << std::endl;
71  REQUIRE( fn.isValid() );
72 
73  // Checking operator()
74  REQUIRE( fn(Point(2, 1)) == Approx( std::sqrt(2) - 1. ) );
75 }
76 
77 TEST_CASE( "2D PointFunctorHolder from functor by lvalue", "[2D][functor][lvalue]" )
78 {
79  using namespace DGtal;
80  using Point = PointVector<2, int>;
81  using Value = double;
82 
83  const auto functor = Functor<Value, Point>( Point(1, 0), 1 );
84  auto fn = functors::holdPointFunctor<Point>( functor ); // auto deduction of the return type
85 
86  // Checks CPointFunctor concept.
87  BOOST_CONCEPT_ASSERT( (DGtal::concepts::CPointFunctor<decltype(fn)>) );
88 
89  // Checking standard services
90  std::cout << fn << std::endl;
91  REQUIRE( fn.isValid() );
92 
93  // Checking operator()
94  REQUIRE( fn(Point(2, 1)) == Approx( std::sqrt(2) - 1. ) );
95 }
96 
97 TEST_CASE( "2D PointFunctorHolder from lambda by rvalue", "[2D][lambda][rvalue]" )
98 {
99  using namespace DGtal;
100  using Point = PointVector<2, int>;
101  using Value = double;
102 
103  const Point center(1, 0);
104  const Value radius = 1;
105 
106  auto fn = functors::holdPointFunctor<Point, Value>(
107  [&center, &radius] (Point const& pt) { return kernel<Value>(pt, center, radius); }
108  );
109 
110  // Checks CPointFunctor concept.
111  BOOST_CONCEPT_ASSERT( (DGtal::concepts::CPointFunctor<decltype(fn)>) );
112 
113  // Checking standard services
114  std::cout << fn << std::endl;
115  REQUIRE( fn.isValid() );
116 
117  // Checking operator()
118  REQUIRE( fn(Point(2, 1)) == Approx( std::sqrt(2) - 1. ) );
119 }
120 
121 TEST_CASE( "2D PointFunctorHolder from lambda by lvalue", "[2D][lambda][lvalue]" )
122 {
123  using namespace DGtal;
124  using Point = PointVector<2, int>;
125  using Value = double;
126 
127  const Point center(1, 0);
128  const Value radius = 1;
129 
130  const auto lambda = [&center, &radius] (Point const& pt) { return kernel<Value>(pt, center, radius); };
131  auto fn = functors::holdPointFunctor<Point, Value>( lambda );
132 
133  // Checks CPointFunctor concept.
134  BOOST_CONCEPT_ASSERT( (DGtal::concepts::CPointFunctor<decltype(fn)>) );
135 
136  // Checking standard services
137  std::cout << fn << std::endl;
138  REQUIRE( fn.isValid() );
139 
140  // Checking operator()
141  REQUIRE( fn(Point(2, 1)) == Approx( std::sqrt(2) - 1. ) );
142 }
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Point center(const std::vector< Point > &points)
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Defines a functor on points.
Definition: CPointFunctor.h:89
MyPointD Point
Definition: testClone2.cpp:383
TEST_CASE("int container traits", "[int][traits]")
InHalfPlaneBySimple3x3Matrix< Point, double > Functor
REQUIRE(domain.isInside(aPoint))