DGtal  0.9.2
LocalEstimatorFromSurfelFunctorAdapter.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file LocalEstimatorFromSurfelFunctorAdapter.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2013/05/28
23  *
24  * Implementation of inline methods defined in LocalEstimatorFromSurfelFunctorAdapter.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 //////////////////////////////////////////////////////////////////////////////
30 #include <cstdlib>
31 //////////////////////////////////////////////////////////////////////////////
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // IMPLEMENTATION of inline methods.
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 template <typename TDigitalSurfaceContainer, typename TMetric,
38  typename TFunctorOnSurfel, typename TConvolutionFunctor>
39 inline
40 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
41  TFunctorOnSurfel, TConvolutionFunctor>::
42 LocalEstimatorFromSurfelFunctorAdapter()
43 {
44  myInit = false;
45 }
46 
47 template <typename TDigitalSurfaceContainer, typename TMetric,
48  typename TFunctorOnSurfel, typename TConvolutionFunctor>
49 inline
50 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
51  TFunctorOnSurfel, TConvolutionFunctor>::
52 LocalEstimatorFromSurfelFunctorAdapter
53 ( ConstAlias< Surface > aSurf,
54  ConstAlias< Metric> aMetric,
55  Alias< FunctorOnSurfel > aFunctor,
56  ConstAlias< ConvolutionFunctor > aConvolutionFunctor)
57  : mySurface(aSurf), myFunctor(&aFunctor), myMetric(aMetric),
58  myEmbedder(Embedder( mySurface->container().space())), myConvFunctor(aConvolutionFunctor)
59 {
60  myInit = false;
61 }
62 ///////////////////////////////////////////////////////////////////////////////
63 template <typename TDigitalSurfaceContainer, typename TMetric,
64  typename TFunctorOnSurfel, typename TConvolutionFunctor>
65 inline
66 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
67  TFunctorOnSurfel, TConvolutionFunctor>::
68 ~LocalEstimatorFromSurfelFunctorAdapter()
69 {
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////////
73 template <typename TDigitalSurfaceContainer, typename TMetric,
74  typename TFunctorOnSurfel, typename TConvolutionFunctor>
75 inline
76 void
77 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
78  TFunctorOnSurfel, TConvolutionFunctor>::
79 selfDisplay ( std::ostream & out ) const
80 {
81  out << "[LocalEstimatorFromSurfelFunctorAdapter]";
82 }
83 ///////////////////////////////////////////////////////////////////////////////
84 template <typename TDigitalSurfaceContainer, typename TMetric,
85  typename TFunctorOnSurfel, typename TConvolutionFunctor>
86 inline
87 bool
88 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
89  TFunctorOnSurfel, TConvolutionFunctor>::
90 isValid() const
91 {
92  return myInit;
93 }
94 
95 
96 //-----------------------------------------------------------------------------
97 template <typename TDigitalSurfaceContainer, typename TMetric,
98  typename TFunctorOnSurfel, typename TConvolutionFunctor>
99 inline
100 void
101 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
102  TFunctorOnSurfel, TConvolutionFunctor>::
103 attach( ConstAlias<Surface> aSurface )
104 {
105  mySurface = aSurface;
106  myEmbedder = Embedder( mySurface->container().space());
107 }
108 
109 //-----------------------------------------------------------------------------
110 template <typename TDigitalSurfaceContainer, typename TMetric,
111  typename TFunctorOnSurfel, typename TConvolutionFunctor>
112 inline
113 void
114 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
115  TFunctorOnSurfel, TConvolutionFunctor>::
116 setParams( ConstAlias<TMetric> aMetric,
117  Alias<FunctorOnSurfel> aFunctor,
118  ConstAlias<ConvolutionFunctor> aConvolutionFunctor,
119  const Value radius )
120 {
121  ASSERT(radius>0);
122  myFunctor = &aFunctor;
123  myMetric = aMetric;
124  myConvFunctor = aConvolutionFunctor;
125  myRadius = radius;
126 }
127 
128 //-----------------------------------------------------------------------------
129 template <typename TDigitalSurfaceContainer, typename TMetric,
130  typename TFunctorOnSurfel, typename TConvolutionFunctor>
131 inline
132 typename DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric, TFunctorOnSurfel, TConvolutionFunctor>::Scalar
133 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
134  TFunctorOnSurfel, TConvolutionFunctor>::
135 h() const
136 {
137  return myH;
138 }
139 
140 ///////////////////////////////////////////////////////////////////////////////
141 template <typename TDigitalSurfaceContainer, typename TMetric,
142  typename TFunctorOnSurfel, typename TConvolutionFunctor>
143 
144 template<typename SurfelIt>
145 inline
146 void
147 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
148  TFunctorOnSurfel, TConvolutionFunctor>::
149 init(const double _h,
150  SurfelIt /*itb*/,
151  SurfelIt /*ite*/)
152 {
153  ASSERT(_h>0);
154  myH = _h;
155  myInit = true;
156 }
157 ///////////////////////////////////////////////////////////////////////////////
158 template <typename TDigitalSurfaceContainer, typename TMetric,
159  typename TFunctorOnSurfel, typename TConvolutionFunctor>
160 template <typename SurfelConstIterator>
161 inline
162 typename DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
163  TFunctorOnSurfel, TConvolutionFunctor>::Quantity
164 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
165  TFunctorOnSurfel, TConvolutionFunctor>::
166 eval( const SurfelConstIterator& it ) const
167 {
168  ASSERT_MSG( isValid(), "Missing init() before evaluation" );
169 
170  const MetricToPoint metricToPoint = std::bind( *myMetric, myEmbedder( *it ), std::placeholders::_1 );
171  const VertexFunctor vfunctor( myEmbedder, metricToPoint);
172  Visitor visitor( *mySurface, vfunctor, *it);
173  ASSERT( ! visitor.finished() );
174  double currentDistance = 0.0;
175  while ( (! visitor.finished() ) && (currentDistance < myRadius) )
176  {
177  typename Visitor::Node node = visitor.current();
178  currentDistance = node.second;
179  if ( currentDistance < myRadius )
180  myFunctor->pushSurfel( node.first , myConvFunctor->operator()((myRadius - currentDistance)/myRadius));
181  else break;
182  visitor.expand();
183  }
184  Quantity val = myFunctor->eval();
185  myFunctor->reset();
186  return val;
187 }
188 ///////////////////////////////////////////////////////////////////////////////
189 template <typename TDigitalSurfaceContainer, typename TMetric,
190  typename TFunctorOnSurfel, typename TConvolutionFunctor>
191 template <typename SurfelConstIterator, typename OutputIterator>
192 inline
193 OutputIterator
194 DGtal::LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
195  TFunctorOnSurfel, TConvolutionFunctor>::
196 eval ( const SurfelConstIterator& itb,
197  const SurfelConstIterator& ite,
198  OutputIterator result ) const
199 {
200  for ( SurfelConstIterator it = itb; it != ite; ++it )
201  {
202  Quantity q = eval( it );
203  *result++ = q;
204  }
205  return result;
206 }
207 ///////////////////////////////////////////////////////////////////////////////
208 template <typename TDigitalSurfaceContainer, typename TMetric,
209  typename TFunctorOnSurfel, typename TConvolutionFunctor>
210 inline
211 std::ostream&
212 DGtal::operator<< ( std::ostream & out,
213  const LocalEstimatorFromSurfelFunctorAdapter<TDigitalSurfaceContainer, TMetric,
214  TFunctorOnSurfel, TConvolutionFunctor> & object )
215 {
216  object.selfDisplay( out );
217  return out;
218 }
219 ///////////////////////////////////////////////////////////////////////////////