DGtal  1.3.beta
ConstImageFunctorHolder.h
1 
17 #pragma once
18 
29 #if defined(ConstImageFunctorHolder_RECURSES)
30 #error Recursive header files inclusion detected in ConstImageFunctorHolder.h
31 #else // defined(ConstImageFunctorHolder_RECURSES)
32 
33 #define ConstImageFunctorHolder_RECURSES
34 
35 #if !defined ConstImageFunctorHolder_h
36 
37 #define ConstImageFunctorHolder_h
38 
40 // Inclusions
41 #include <iostream>
42 #include <type_traits>
43 #include <utility>
44 #include <iterator>
45 
46 #include <boost/iterator/transform_iterator.hpp>
47 #include <boost/concept/assert.hpp>
48 
49 #include "DGtal/kernel/domains/CDomain.h"
50 #include "DGtal/base/FunctorHolder.h"
52 
53 namespace DGtal
54 {
55 namespace functors
56 {
57 
98 template <
99  typename TDomain,
100  typename TValue,
101  typename TFunctor
102 >
104 {
106 
107  // ----------------------- Interface --------------------------------------
108 public:
109 
110  // DGtal types
112  using Domain = TDomain;
113  using Point = typename Domain::Point;
114  using Vector = typename Domain::Vector;
115  using Integer = typename Domain::Integer;
116  using Size = typename Domain::Size;
117  using Dimension = typename Domain::Dimension;
118  using Vertex = Point;
119  using Value = TValue;
120  using Functor = TFunctor;
121 
122  using ConstIterator = boost::transform_iterator< std::reference_wrapper<const Self>, typename Domain::ConstIterator >;
123  using ConstReverseIterator = std::reverse_iterator< ConstIterator >;
124  class ConstRange;
125 
126  BOOST_STATIC_CONSTANT( Dimension, dimension = Domain::Space::dimension );
127 
128  // ------------------------- Private Datas --------------------------------
129  // Private members moved to the beginning of the class because of
130  // the SFINAE trick in the operator() methods.
131 private:
134 
135  // ----------------------- Standard services ------------------------------
136 public:
137 
142  template < class TGivenFunctor >
143  explicit ConstImageFunctorHolder( Domain const& aDomain, TGivenFunctor && aFunctor )
144  : myDomain( aDomain )
145  , myFunctor( std::forward<TGivenFunctor>(aFunctor) )
146  {
147  }
148 
149  // ----------------------- Interface --------------------------------------
150 public:
151 
153  inline
154  Domain const& domain() const
155  {
156  return myDomain;
157  }
158 
160 
168  template <typename TPoint> // Needed template parameter to enable SFINAE trick
169  inline
170  auto operator() ( TPoint const& aPoint ) const
171  -> decltype( myFunctor( aPoint ) ) // Using SFINAE to enable this overload for unary functor
172  {
173  ASSERT_MSG(
174  myDomain.isInside(aPoint),
175  "The point is outside the domain."
176  );
177  return myFunctor( aPoint );
178  }
179 
180  template <typename TPoint> // Needed template parameter to enable SFINAE trick
181  inline
182  auto operator() ( TPoint const& aPoint ) const
183  -> decltype( myFunctor( aPoint, myDomain ) ) // Using SFINAE to enable this overload for binary functor
184  {
185  ASSERT_MSG(
186  myDomain.isInside(aPoint),
187  "The point is outside the domain."
188  );
189  return myFunctor( aPoint, myDomain );
190  }
191 
193 
196  inline
198  {
199  return ConstRange( *this );
200  }
201 
205  inline
206  void selfDisplay ( std::ostream & out ) const
207  {
208  out << "[ConstImageFunctorHolder] holding a " << myFunctor << " on domain " << myDomain;
209  }
210 
214  inline constexpr
215  bool isValid() const
216  {
217  return true;
218  }
219 
220 public:
223  {
224  public:
225  ConstRange( Self const& aConstImageFunctorHolder )
226  : myConstImageFunctorHolder( aConstImageFunctorHolder )
227  {}
228 
231  using Point = typename Self::Point;
232 
236 
237  inline ConstReverseIterator rbegin() const { return ConstReverseIterator( end() ); }
238  inline ConstReverseIterator rbegin( Point const& aPoint ) const { return ConstReverseIterator( ++begin(aPoint) ); }
239  inline ConstReverseIterator rend() const { return ConstReverseIterator( begin() ); }
240 
241  private:
243  }; // End of class ConstRange
244 }; // End of class ConstImageFunctorHolder
245 
252 template <typename TDomain, typename TValue, typename TFunctor>
253 std::ostream&
255 {
256  object.selfDisplay(out);
257  return out;
258 }
259 
271 template <
272  typename TValue,
273  typename TDomain,
274  typename TFunctor
275 >
276 inline auto
277 holdConstImageFunctor( TDomain const& aDomain, TFunctor && aFunctor )
279  {
280  return ConstImageFunctorHolder<TDomain, TValue, decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))>{ aDomain, holdFunctor(std::forward<TFunctor>(aFunctor)) };
281  }
282 
284 
299 // Auto-deduction of the return type in case of an unary functor.
300 template <
301  typename TDomain,
302  typename TFunctor
303 >
304 inline auto
305 holdConstImageFunctor( TDomain const& aDomain, TFunctor && aFunctor )
307  TDomain,
308  typename std::decay<decltype(aFunctor(std::declval<typename TDomain::Point>()))>::type,
309  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
310  >
311  {
313  TDomain,
314  typename std::decay<decltype(aFunctor(std::declval<typename TDomain::Point>()))>::type,
315  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
316  >{ aDomain, holdFunctor(std::forward<TFunctor>(aFunctor)) };
317  }
318 
319 // Auto-deduction of the return type in case of a binary functor.
320 template <
321  typename TDomain,
322  typename TFunctor
323 >
324 inline auto
325 holdConstImageFunctor( TDomain const& aDomain, TFunctor && aFunctor )
326  -> ConstImageFunctorHolder<
327  TDomain,
328  typename std::decay<decltype(aFunctor(std::declval<typename TDomain::Point>(), aDomain))>::type,
329  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
330  >
331  {
332  return ConstImageFunctorHolder<
333  TDomain,
334  typename std::decay<decltype(aFunctor(std::declval<typename TDomain::Point>(), aDomain))>::type,
335  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
336  >{ aDomain, holdFunctor(std::forward<TFunctor>(aFunctor)) };
337  }
338 
340 
341 } // namespace functors
342 } // namespace DGtal
343 
344 #endif // !defined ConstImageFunctorHolder_h
345 
346 #undef ConstImageFunctorHolder_RECURSES
347 #endif // else defined(ConstImageFunctorHolder_RECURSES)
DGtal::functors::ConstImageFunctorHolder::ConstRange::rbegin
ConstReverseIterator rbegin(Point const &aPoint) const
Definition: ConstImageFunctorHolder.h:238
DGtal::HyperRectDomain< Space >::Dimension
Space::Dimension Dimension
Definition: HyperRectDomain.h:116
DGtal::functors::ConstImageFunctorHolder::ConstRange::Point
typename Self::Point Point
Definition: ConstImageFunctorHolder.h:231
DGtal::functors::ConstImageFunctorHolder
Transform a point-dependent (and possibly domain-dependent) functor into a constant image.
Definition: ConstImageFunctorHolder.h:103
DGtal::functors::ConstImageFunctorHolder::Size
typename Domain::Size Size
Definition: ConstImageFunctorHolder.h:116
DGtal::functors::ConstImageFunctorHolder::ConstRange::rbegin
ConstReverseIterator rbegin() const
Definition: ConstImageFunctorHolder.h:237
DGtal::functors::ConstImageFunctorHolder::Functor
TFunctor Functor
Definition: ConstImageFunctorHolder.h:120
DGtal::functors::ConstImageFunctorHolder::Integer
typename Domain::Integer Integer
Definition: ConstImageFunctorHolder.h:115
DGtal::functors::ConstImageFunctorHolder::Vector
typename Domain::Vector Vector
Definition: ConstImageFunctorHolder.h:114
DGtal::functors::ConstImageFunctorHolder::BOOST_STATIC_CONSTANT
BOOST_STATIC_CONSTANT(Dimension, dimension=Domain::Space::dimension)
aPoint
const Point aPoint(3, 4)
DGtal::functors::ConstImageFunctorHolder::myFunctor
Functor myFunctor
The functor that generates the image.
Definition: ConstImageFunctorHolder.h:133
DGtal::functors::ConstImageFunctorHolder::myDomain
Domain myDomain
The image domain.
Definition: ConstImageFunctorHolder.h:132
DGtal::functors::ConstImageFunctorHolder::domain
Domain const & domain() const
Returns the associated domain.
Definition: ConstImageFunctorHolder.h:154
DGtal::functors::ConstImageFunctorHolder::ConstRange::end
ConstIterator end() const
Definition: ConstImageFunctorHolder.h:235
Size
HalfEdgeDataStructure::Size Size
Definition: testHalfEdgeDataStructure.cpp:50
DGtal::functors::ConstImageFunctorHolder::Value
TValue Value
Definition: ConstImageFunctorHolder.h:119
DGtal::functors::ConstImageFunctorHolder::isValid
constexpr bool isValid() const
Checks the validity/consistency of the object.
Definition: ConstImageFunctorHolder.h:215
DGtal::functors::ConstImageFunctorHolder::ConstImageFunctorHolder
ConstImageFunctorHolder(Domain const &aDomain, TGivenFunctor &&aFunctor)
Constructor.
Definition: ConstImageFunctorHolder.h:143
DGtal::functors::ConstImageFunctorHolder::operator()
auto operator()(TPoint const &aPoint) const -> decltype(myFunctor(aPoint))
Evaluates the functor at the given point.
Definition: ConstImageFunctorHolder.h:170
DGtal::concepts::CDomain
Aim: This concept represents a digital domain, i.e. a non mutable subset of points of the given digit...
Definition: CDomain.h:129
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::functors::holdConstImageFunctor
auto holdConstImageFunctor(TDomain const &aDomain, TFunctor &&aFunctor) -> ConstImageFunctorHolder< TDomain, TValue, decltype(holdFunctor(std::forward< TFunctor >(aFunctor)))>
ConstImageFunctorHolder construction helper with specification of the return type.
Definition: ConstImageFunctorHolder.h:277
ConstRange
Image::ConstRange ConstRange
Definition: testSimpleRandomAccessRangeFromPoint.cpp:47
DGtal::functors::ConstImageFunctorHolder::ConstRange::begin
ConstIterator begin() const
Definition: ConstImageFunctorHolder.h:233
DGtal::functors::ConstImageFunctorHolder::ConstRange
Constant range on a ConstImageFunctorHolder.
Definition: ConstImageFunctorHolder.h:222
DGtal::functors::ConstImageFunctorHolder::BOOST_CONCEPT_ASSERT
BOOST_CONCEPT_ASSERT((DGtal::concepts::CDomain< TDomain >))
DGtal::functors::operator<<
std::ostream & operator<<(std::ostream &out, const FunctorHolder< FunctorStorage, NeedDereference > &object)
Overloads 'operator<<' for displaying objects of class FunctorHolder.
Definition: FunctorHolder.h:239
DGtal::functors::ConstImageFunctorHolder::selfDisplay
void selfDisplay(std::ostream &out) const
Writes/Displays the object on an output stream.
Definition: ConstImageFunctorHolder.h:206
DGtal::functors::ConstImageFunctorHolder::ConstReverseIterator
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: ConstImageFunctorHolder.h:123
DGtal::functors::ConstImageFunctorHolder::Domain
TDomain Domain
Definition: ConstImageFunctorHolder.h:112
Vector
FreemanChain< int >::Vector Vector
Definition: testCombinDSS.cpp:60
Integer
Point::Coordinate Integer
Definition: examplePlaneProbingParallelepipedEstimator.cpp:44
DGtal::HyperRectDomain_Iterator
Iterator for HyperRectDomain.
Definition: HyperRectDomain_Iterator.h:142
DGtal::functors::ConstImageFunctorHolder::Vertex
Point Vertex
Definition: ConstImageFunctorHolder.h:118
DGtal::functors::ConstImageFunctorHolder::ConstRange::ConstReverseIterator
typename Self::ConstReverseIterator ConstReverseIterator
Definition: ConstImageFunctorHolder.h:230
DGtal::functors::ConstImageFunctorHolder::ConstIterator
boost::transform_iterator< std::reference_wrapper< const Self >, typename Domain::ConstIterator > ConstIterator
Definition: ConstImageFunctorHolder.h:122
DGtal::functors::ConstImageFunctorHolder::ConstRange::myConstImageFunctorHolder
Self const & myConstImageFunctorHolder
Definition: ConstImageFunctorHolder.h:242
DGtal::functors::holdFunctor
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.
Definition: FunctorHolder.h:279
DGtal::functors::ConstImageFunctorHolder::Dimension
typename Domain::Dimension Dimension
Definition: ConstImageFunctorHolder.h:117
DGtal::functors::ConstImageFunctorHolder::ConstRange::begin
ConstIterator begin(Point const &aPoint) const
Definition: ConstImageFunctorHolder.h:234
DGtal::functors::ConstImageFunctorHolder::ConstRange::ConstRange
ConstRange(Self const &aConstImageFunctorHolder)
Definition: ConstImageFunctorHolder.h:225
DGtal::functors::ConstImageFunctorHolder::ConstRange::rend
ConstReverseIterator rend() const
Definition: ConstImageFunctorHolder.h:239
DGtal::functors::ConstImageFunctorHolder::constRange
ConstRange constRange() const
Returns a constant range over this image.
Definition: ConstImageFunctorHolder.h:197
DGtal::functors::ConstImageFunctorHolder::Point
typename Domain::Point Point
Definition: ConstImageFunctorHolder.h:113
Point
MyPointD Point
Definition: testClone2.cpp:383
DGtal::functors::ConstImageFunctorHolder::ConstRange::ConstIterator
typename Self::ConstIterator ConstIterator
Definition: ConstImageFunctorHolder.h:229