DGtal  0.9.2
ExplicitDigitalSurface.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 ExplicitDigitalSurface.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21  *
22  * @date 2011/11/27
23  *
24  * Implementation of inline methods defined in ExplicitDigitalSurface.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <iterator>
33 #include "DGtal/topology/helpers/Surfaces.h"
34 //////////////////////////////////////////////////////////////////////////////
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 //-----------------------------------------------------------------------------
41 template <typename TKSpace, typename TSurfelPredicate>
42 inline
43 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
44 ::~Tracker()
45 {}
46 //-----------------------------------------------------------------------------
47 template <typename TKSpace, typename TSurfelPredicate>
48 inline
49 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
50 ::Tracker( ConstAlias<DigitalSurfaceContainer> aSurface,
51  const Surfel & s )
52  : mySurface( aSurface ), myNeighborhood()
53 {
54  myNeighborhood.init( & surface().space(),
55  & surface().surfelAdjacency(),
56  s );
57 }
58 //-----------------------------------------------------------------------------
59 template <typename TKSpace, typename TSurfelPredicate>
60 inline
61 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
62 ::Tracker( const Tracker & other )
63  : mySurface( other.mySurface ), myNeighborhood( other.myNeighborhood )
64 {
65 }
66 //-----------------------------------------------------------------------------
67 template <typename TKSpace, typename TSurfelPredicate>
68 inline
69 const typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
70 ::DigitalSurfaceContainer &
71 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
72 ::surface() const
73 {
74  return mySurface;
75 }
76 //-----------------------------------------------------------------------------
77 template <typename TKSpace, typename TSurfelPredicate>
78 inline
79 const typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
80 ::Surfel &
81 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
82 ::current() const
83 {
84  return myNeighborhood.surfel();
85 }
86 //-----------------------------------------------------------------------------
87 template <typename TKSpace, typename TSurfelPredicate>
88 inline
89 DGtal::Dimension
90 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
91 ::orthDir() const
92 {
93  return myNeighborhood.orthDir();
94 }
95 //-----------------------------------------------------------------------------
96 template <typename TKSpace, typename TSurfelPredicate>
97 inline
98 void
99 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
100 ::move( const Surfel & s )
101 {
102  ASSERT( surface().isInside( s ) );
103  myNeighborhood.setSurfel( s );
104 }
105 //-----------------------------------------------------------------------------
106 template <typename TKSpace, typename TSurfelPredicate>
107 inline
108 DGtal::uint8_t
109 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
110 ::adjacent( Surfel & s, Dimension d, bool pos ) const
111 {
112  return static_cast<uint8_t>
113  ( myNeighborhood.getAdjacentOnSurfelPredicate
114  ( s, surface().surfelPredicate(), d, pos ) );
115 }
116 
117 ///////////////////////////////////////////////////////////////////////////////
118 // ----------------------- Standard services ------------------------------
119 
120 //-----------------------------------------------------------------------------
121 template <typename TKSpace, typename TSurfelPredicate>
122 inline
123 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::~ExplicitDigitalSurface()
124 {
125 }
126 //-----------------------------------------------------------------------------
127 template <typename TKSpace, typename TSurfelPredicate>
128 inline
129 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::ExplicitDigitalSurface
130 ( const ExplicitDigitalSurface & other )
131  : myKSpace( other.myKSpace ),
132  mySurfelPredicate( other.mySurfelPredicate ),
133  mySurfelAdjacency( other.mySurfelAdjacency ),
134  mySurfels( other.mySurfels )
135 {
136 }
137 //-----------------------------------------------------------------------------
138 template <typename TKSpace, typename TSurfelPredicate>
139 inline
140 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::ExplicitDigitalSurface
141 ( ConstAlias<KSpace> aKSpace,
142  const SurfelPredicate & aPP,
143  const Adjacency & adj,
144  const Surfel & s,
145  bool closed )
146  : myKSpace( aKSpace ), mySurfelPredicate( aPP ), mySurfelAdjacency( adj )
147 {
148  computeSurfels( s, closed );
149 }
150 //-----------------------------------------------------------------------------
151 template <typename TKSpace, typename TSurfelPredicate>
152 inline
153 const
154 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Adjacency &
155 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelAdjacency() const
156 {
157  return mySurfelAdjacency;
158 }
159 //-----------------------------------------------------------------------------
160 template <typename TKSpace, typename TSurfelPredicate>
161 inline
162 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Adjacency &
163 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelAdjacency()
164 {
165  return mySurfelAdjacency;
166 }
167 //-----------------------------------------------------------------------------
168 template <typename TKSpace, typename TSurfelPredicate>
169 inline
170 const
171 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelPredicate &
172 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelPredicate() const
173 {
174  return mySurfelPredicate;
175 }
176 
177 //-----------------------------------------------------------------------------
178 // --------- CDigitalSurfaceContainer realization -------------------------
179 //-----------------------------------------------------------------------------
180 template <typename TKSpace, typename TSurfelPredicate>
181 inline
182 const typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::KSpace &
183 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::space() const
184 {
185  return myKSpace;
186 }
187 //-----------------------------------------------------------------------------
188 template <typename TKSpace, typename TSurfelPredicate>
189 inline
190 bool
191 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::isInside
192 ( const Surfel & s ) const
193 {
194  return surfelPredicate()( s );
195 }
196 //-----------------------------------------------------------------------------
197 template <typename TKSpace, typename TSurfelPredicate>
198 inline
199 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelConstIterator
200 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::begin() const
201 {
202  return mySurfels.begin();
203 }
204 //-----------------------------------------------------------------------------
205 template <typename TKSpace, typename TSurfelPredicate>
206 inline
207 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelConstIterator
208 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::end() const
209 {
210  return mySurfels.end();
211 }
212 //-----------------------------------------------------------------------------
213 template <typename TKSpace, typename TSurfelPredicate>
214 inline
215 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Size
216 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::nbSurfels() const
217 {
218  return mySurfels.size();
219 }
220 //-----------------------------------------------------------------------------
221 template <typename TKSpace, typename TSurfelPredicate>
222 inline
223 bool
224 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::empty() const
225 {
226  return mySurfels.empty();
227 }
228 //-----------------------------------------------------------------------------
229 template <typename TKSpace, typename TSurfelPredicate>
230 inline
231 typename DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::DigitalSurfaceTracker*
232 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::newTracker
233 ( const Surfel & s ) const
234 {
235  return new Tracker( *this, s );
236 }
237 //-----------------------------------------------------------------------------
238 template <typename TKSpace, typename TSurfelPredicate>
239 inline
240 DGtal::Connectedness
241 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::connectedness() const
242 {
243  return CONNECTED;
244 }
245 
246 // ------------------------- Hidden services ------------------------------
247 //-----------------------------------------------------------------------------
248 template <typename TKSpace, typename TSurfelPredicate>
249 inline
250 void
251 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::computeSurfels
252 ( const Surfel & p, bool closed )
253 {
254  mySurfels.clear();
255  if ( KSpace::dimension > 2 )
256  {
257  typename KSpace::SCellSet surface;
258  if ( closed )
259  Surfaces<KSpace>::trackClosedSurface( surface,
260  myKSpace,
261  mySurfelAdjacency,
262  mySurfelPredicate,
263  p
264  );
265  else
266  Surfaces<KSpace>::trackSurface( surface,
267  myKSpace,
268  mySurfelAdjacency,
269  mySurfelPredicate,
270  p
271  );
272 
273  for ( typename KSpace::SCellSet::const_iterator it = surface.begin(),
274  it_end = surface.end(); it != it_end; ++it )
275  mySurfels.push_back( *it );
276  }
277  else
278  {
279  ASSERT( KSpace::dimension == 2 );
280  Surfaces<KSpace>::track2DSurface( mySurfels,
281  myKSpace,
282  mySurfelAdjacency,
283  mySurfelPredicate,
284  p
285  );
286  }
287 }
288 
289 ///////////////////////////////////////////////////////////////////////////////
290 // Interface - public :
291 
292 /**
293  * Writes/Displays the object on an output stream.
294  * @param out the output stream where the object is written.
295  */
296 template <typename TKSpace, typename TSurfelPredicate>
297 inline
298 void
299 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::selfDisplay ( std::ostream & out ) const
300 {
301  out << "[ExplicitDigitalSurface]";
302 }
303 
304 /**
305  * Checks the validity/consistency of the object.
306  * @return 'true' if the object is valid, 'false' otherwise.
307  */
308 template <typename TKSpace, typename TSurfelPredicate>
309 inline
310 bool
311 DGtal::ExplicitDigitalSurface<TKSpace,TSurfelPredicate>::isValid() const
312 {
313  return true;
314 }
315 
316 
317 
318 ///////////////////////////////////////////////////////////////////////////////
319 // Implementation of inline functions //
320 
321 template <typename TKSpace, typename TSurfelPredicate>
322 inline
323 std::ostream&
324 DGtal::operator<< ( std::ostream & out,
325  const ExplicitDigitalSurface<TKSpace,TSurfelPredicate> & object )
326 {
327  object.selfDisplay( out );
328  return out;
329 }
330 
331 // //
332 ///////////////////////////////////////////////////////////////////////////////
333 
334