DGtal  0.9.4beta
EstimatorCache.h
1 
17 #pragma once
18 
31 #if defined(EstimatorCache_RECURSES)
32 #error Recursive header files inclusion detected in EstimatorCache.h
33 #else // defined(EstimatorCache_RECURSES)
34 
35 #define EstimatorCache_RECURSES
36 
37 #if !defined EstimatorCache_h
38 
39 #define EstimatorCache_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <map>
45 #include "DGtal/base/Common.h"
46 #include "DGtal/base/Alias.h"
47 #include "DGtal/geometry/surfaces/estimation/CSurfelLocalEstimator.h"
49 
50 namespace DGtal
51 {
52 
54  // template class EstimatorCache
74  template <typename TEstimator,
75  typename TContainer = std::map<typename TEstimator::Surfel,
76  typename TEstimator::Quantity> >
78  {
79  // ----------------------- Standard services ------------------------------
80  public:
81 
82 
84  typedef TEstimator Estimator;
86 
88  typedef TContainer Container;
90 
91  //Concept of CSurfelLocalEstimator
92 
94  typedef typename Estimator::Surfel Surfel;
95 
97  typedef typename Estimator::Quantity Quantity;
98 
101 
106  {}
107 
112  EstimatorCache( Alias<Estimator> anEstimator): myEstimator(&anEstimator),
113  myInit(false)
114  {}
115 
120  {}
121 
125  EstimatorCache(const Self &other): myContainer(other.myContainer),
126  myEstimator(other.myEstimator),
127  myInit(other.myInit)
128  {}
129 
136  Self & operator= ( const Self & other )
137  {
138  myContainer = other.myContainer;
139  myEstimator = other.myEstimator;
140  myInit = other.myInit;
141 
142  return *this;
143  }
144 
145  // ----------------------- CSurfelLocalEstimator Interface --------------------------------------
146 
157  template <typename SurfelConstIterator>
158  void init(const double aH, SurfelConstIterator itb, SurfelConstIterator ite)
159  {
160  ASSERT(myEstimator);
161  myEstimator->init(aH,itb,ite);
162  myContainer.clear();
163 
164  //We estimate and store the quantities
165  //(since SurfelConstIterator models are usually SinglePass, we
166  //cannot use the optimized "range" eval on the estimator)
167  for(SurfelConstIterator it = itb; it != ite; ++it)
168  myContainer.insert( std::pair<Surfel, Quantity>(*it, myEstimator->eval(it) ) );
169 
170  myInit = true;
171  }
172 
182  template <typename SurfelConstIterator>
183  Quantity eval(const SurfelConstIterator it) const
184  {
185  ASSERT_MSG(myInit, " init() method must have been called first.");
186  return myContainer.find( *it )->second;
187  }
188 
197  Quantity eval(const Surfel s) const
198  {
199  ASSERT_MSG(myInit, " init() method must have been called first.");
200  return myContainer.find( s )->second;
201  }
202 
203 
217  template <typename SurfelConstIterator,typename OutputIterator>
218  OutputIterator eval(SurfelConstIterator itb,
219  SurfelConstIterator ite,
220  OutputIterator result ) const
221  {
222  ASSERT_MSG(myInit, " init() method must have been called first.");
223  for(SurfelConstIterator it = itb; it != ite; ++it)
224  *result++ = this->eval(it);
225 
226  return result;
227  }
228 
236  double h() const
237  {
238  return myEstimator->h();
239  }
240 
241  // ----------------------- Interface --------------------------------------
242  public:
243 
248  typename Container::size_type size() const
249  {
250  ASSERT_MSG(myInit, " init() method must have been called first.");
251  return myContainer.size();
252  }
253 
258  void selfDisplay ( std::ostream & out ) const
259  {
260  out<< "[EstimatorCache] number of surfels="<<myContainer.size();
261  }
262 
267  bool isValid() const
268  {
269  return myEstimator && myEstimator->isValid();
270  }
271 
272  // ------------------------- Protected Datas ------------------------------
273  private:
274  // ------------------------- Private Datas --------------------------------
275  private:
276 
277 
279  Container myContainer;
280 
282  Estimator *myEstimator;
283 
285  bool myInit;
286 
287  // ------------------------- Internals ------------------------------------
288  private:
289 
290  }; // end of class EstimatorCache
291 
292 
299  template <typename T, typename TC>
300  std::ostream&
301  operator<< ( std::ostream & out, const EstimatorCache<T,TC> & object )
302  {
303  object.selfDisplay( out );
304  return out;
305  }
306 
307 } // namespace DGtal
308 // //
310 
311 #endif // !defined EstimatorCache_h
312 
313 #undef EstimatorCache_RECURSES
314 #endif // else defined(EstimatorCache_RECURSES)
bool myInit
Init flag.
Go to http://www.sgi.com/tech/stl/PairAssociativeContainer.html.
Definition: Boost.dox:149
EstimatorCache< Estimator, Container > Self
Self.
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: Alias.h:182
OutputIterator eval(SurfelConstIterator itb, SurfelConstIterator ite, OutputIterator result) const
Container myContainer
Instance of estimator.
TContainer Container
Container type.
Quantity eval(const Surfel s) const
TEstimator Estimator
Estimator type.
EstimatorCache(const Self &other)
Container::size_type size() const
void init(const double aH, SurfelConstIterator itb, SurfelConstIterator ite)
DGtal is the top-level namespace which contains all DGtal functions and types.
Estimator * myEstimator
Alias of the estimator.
Aim: This concept describes an object that can process a range of surfels (that are supposed to belon...
Estimator::Surfel Surfel
Surfel type.
BOOST_CONCEPT_ASSERT((concepts::CSurfelLocalEstimator< TEstimator >))
Aim: this class adapts any local surface estimator to cache the estimated values in a associative con...
Self & operator=(const Self &other)
EstimatorCache(Alias< Estimator > anEstimator)
Estimator::Quantity Quantity
Quantity type.
void selfDisplay(std::ostream &out) const
Quantity eval(const SurfelConstIterator it) const