38 #include "DGtal/base/FunctorHolder.h"
39 #include "DGtal/kernel/PointVector.h"
41 #include "DGtal/base/Common.h"
42 #include "DGtal/helpers/StdDefs.h"
43 #include "ConfigExamples.h"
44 #include "DGtal/io/readers/PGMReader.h"
45 #include "DGtal/io/readers/GenericReader.h"
46 #include "DGtal/images/ImageContainerBySTLVector.h"
47 #include "DGtal/kernel/BasicPointPredicates.h"
54 return pt.
norm() - 1.;
59 template <
typename Po
int>
61 typename Point::Component
64 return pt.norm() -
typename Point::Component(1);
69 template <
typename Po
int>
70 struct SignedDistToCircle
72 using Real =
typename Point::Component;
77 SignedDistToCircle(
Point const& pt, Real r)
82 Real operator() (
Point const& pt)
const
84 return (pt - center).norm() - radius;
94 explicit Binarizer(T v) : threshold(v) {}
95 Binarizer& operator= (Binarizer
const&) =
delete;
96 bool operator() (T v)
const {
return v <= threshold; }
109 template <
typename Iterator>
114 using value_type =
typename std::iterator_traits<Iterator>::value_type;
115 auto const mean = std::accumulate(first, last, value_type(0));
116 auto const size = std::distance(first, last);
122 template <
typename Image>
125 std::declval<Image>().begin(), std::declval<Image>().end() ))
132 #if __cplusplus >= 201402L
134 template <
typename Iterator>
135 auto get_mean_binarizer_from_range_cpp14(Iterator first, Iterator last)
137 using value_type =
typename std::iterator_traits<Iterator>::value_type;
138 auto const mean = std::accumulate(first, last, value_type(0));
139 auto const size = std::distance(first, last);
146 template <
typename T>
150 return Binarizer<T>(v);
157 typename PointFunctor,
172 typename PointFunctor,
184 typename PointFunctor,
188 typename std::decay<PointFunctor>::type,
189 typename std::decay<Predicate>::type
194 typename std::decay<PointFunctor>::type,
195 typename std::decay<Predicate>::type
197 std::forward<PointFunctor>(aFun),
198 std::forward<Predicate>(aPred)
211 std::cout <<
"Holding a function" << std::endl;
215 std::cout << fn(
Point(1, 1) ) << std::endl;
220 std::cout <<
"Holding a templated function" << std::endl;
224 std::cout << fn(
Point(1, 1) ) << std::endl;
229 std::cout <<
"Holding a function through a lambda" << std::endl;
233 std::cout << fn(
Point(1, 1) ) << std::endl;
238 std::cout <<
"Holding a templated function through a lambda" << std::endl;
242 std::cout << fn(
Point(1, 1) ) << std::endl;
246 #if __cplusplus >= 201402L
248 std::cout <<
"Holding a templated function through a C++14 lambda" << std::endl;
252 std::cout << fn(
Point(1, 1) ) << std::endl;
261 std::cout <<
"Holding a functor by (lvalue) reference" << std::endl;
264 SignedDistToCircle<Point> dist(
Point(0, 1), 2);
266 std::cout << fn(
Point(1, 1) ) << std::endl;
271 std::cout <<
"Holding a functor by rvalue reference" << std::endl;
275 std::cout << fn(
Point(1, 1) ) << std::endl;
280 std::cout <<
"Holding a functor by moving it" << std::endl;
283 SignedDistToCircle<Point> dist(
Point(0, 1), 2);
285 std::cout << fn(
Point(1, 1) ) << std::endl;
293 std::cout <<
"Holding a lambda" << std::endl;
300 [¢er, &radius] (
Point const& pt) {
301 return (pt - center).norm() - radius;
304 std::cout << fn(
Point(1, 1) ) << std::endl;
309 std::cout <<
"Holding a non-unary lambda" << std::endl;
313 auto dist = [] (
Point const& pt,
Point const& center,
double radius)
315 return (pt - center).
norm() - radius;
320 std::cout << fn(
Point(1, 1),
Point(0, 1), 2 ) << std::endl;
328 std::cout <<
"Copying a functor held by lvalue reference" << std::endl;
331 SignedDistToCircle<Point> dist(
Point(0, 1), 2);
335 std::cout << fn(
Point(1, 1) ) << std::endl;
336 std::cout << fn2(
Point(1, 1) ) << std::endl;
340 std::cout << fn(
Point(1, 1) ) << std::endl;
341 std::cout << fn2(
Point(1, 1) ) << std::endl;
346 std::cout <<
"Copying a lambda held by rvalue reference" << std::endl;
350 std::cout << fn() << std::endl;
352 std::cout << fn2() << std::endl;
353 std::cout << fn() << std::endl;
361 std::cout <<
"Storing a FunctorHolder" << std::endl;
370 std::cout <<
"Passing a FunctorHolder" << std::endl;
375 std::string filename = examplesPath +
"samples/contourS.pgm";
383 Point const pt(50, 25);
384 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
388 std::cout <<
"Returning a functor" << std::endl;
390 auto binarizer = get_trivial_binarizer<int>();
392 std::cout <<
"binarizer(120) = " << binarizer(120) << std::endl;
396 std::cout <<
"Returning a functor using trailing return syntax" << std::endl;
401 std::string filename = examplesPath +
"samples/contourS.pgm";
409 Point const pt(50, 25);
410 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
414 std::cout <<
"Returning a functor using trailing return syntax and declval" << std::endl;
418 std::string filename = examplesPath +
"samples/contourS.pgm";
420 auto binarizer = get_mean_binarizer_from_an_image<Image>(filename);
422 std::cout <<
"binarizer(120) = " << binarizer(120) << std::endl;
425 #if __cplusplus >= 201402L
427 std::cout <<
"Returning a functor using auto in C++14" << std::endl;
432 std::string filename = examplesPath +
"samples/contourS.pgm";
435 auto binarizer = get_mean_binarizer_from_range_cpp14(image.begin(), image.end());
438 Point const pt(50, 25);
439 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
444 std::cout <<
"Usage of Binarizer factory" << std::endl;
449 std::string filename = examplesPath +
"samples/contourS.pgm";
457 Point const pt(50, 25);
458 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
462 #if __cplusplus >= 201703L
464 std::cout <<
"Binarizer factory using deduction guides of C++17" << std::endl;
469 std::string filename = examplesPath +
"samples/contourS.pgm";
477 Point const pt(50, 25);
478 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
483 std::cout <<
"Usage of PointFunctorPredicate factory" << std::endl;
488 std::string filename = examplesPath +
"samples/contourS.pgm";
496 Point const pt(50, 25);
497 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
501 std::cout <<
"Usage of PointFunctorPredicate factory with perfect forwarding" << std::endl;
506 std::string filename = examplesPath +
"samples/contourS.pgm";
512 Point const pt(50, 25);
513 std::cout <<
"binarizer(" <<
image( pt ) <<
") = " << predicate( pt ) << std::endl;
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
double norm(const NormType type=L_2) const
auto get_mean_binarizer_from_an_image(std::string const &file_name) -> decltype(get_mean_binarizer_from_range(std::declval< Image >().begin(), std::declval< Image >().end()))
[Returning a FunctorHolder using trailing return]
decltype(DGtal::functors::holdFunctor(Binarizer< T >(128))) get_trivial_binarizer()
double signed_dist_to_unit_circle(DGtal::PointVector< 2, double > const &pt)
[signed_dist_to_unit_circle]
DGtal::functors::PointFunctorPredicate< typename std::decay< PointFunctor >::type, typename std::decay< Predicate >::type > makePointFunctorPredicate_Example2(PointFunctor &&aFun, Predicate &&aPred)
[Factory of PointFunctorPredicate]
Point::Component templated_signed_dist_to_unit_circle(Point const &pt)
[signed_dist_to_unit_circle]
auto get_mean_binarizer_from_range(Iterator first, Iterator last) -> decltype(DGtal::functors::holdFunctor(Binarizer< decltype(*first/std::distance(first, last))>(0)))
[Returning a FunctorHolder]
Binarizer< T > makeBinarizer(T const &v)
[Returning a FunctorHolder using trailing return and declval]
DGtal::functors::PointFunctorPredicate< PointFunctor, Predicate > makePointFunctorPredicate_Example(PointFunctor const &aFun, Predicate const &aPred)
[Factory of Binarizer]
int main()
[Factory of PointFunctorPredicate using perfect forwarding]
Point center(const std::vector< Point > &points)
auto holdFunctor(Function &&fn) -> decltype(holdFunctorImpl(std::forward< Function >(fn), typename std::is_lvalue_reference< Function >{}))
Hold any callable object (function, functor, lambda, ...) as a C(Unary)Functor model.
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
Aim: The predicate returns true when the predicate returns true for the value assigned to a given poi...
ImageContainerBySTLVector< Domain, Value > Image