DGtal  1.5.beta
LightExplicitDigitalSurface.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 LightExplicitDigitalSurface.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/09/01
23  *
24  * Implementation of inline methods defined in LightExplicitDigitalSurface.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <iterator>
33 #include "DGtal/graph/CVertexPredicate.h"
34 #include "DGtal/topology/helpers/Surfaces.h"
35 //////////////////////////////////////////////////////////////////////////////
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // IMPLEMENTATION of inline methods.
39 ///////////////////////////////////////////////////////////////////////////////
40 
41 //-----------------------------------------------------------------------------
42 template <typename TKSpace, typename TSurfelPredicate>
43 inline
44 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
45 ::~Tracker()
46 {}
47 //-----------------------------------------------------------------------------
48 template <typename TKSpace, typename TSurfelPredicate>
49 inline
50 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
51 ::Tracker( ConstAlias<DigitalSurfaceContainer> aSurface,
52  const Surfel & s )
53  : mySurface( aSurface ), myNeighborhood()
54 {
55  myNeighborhood.init( & surface().space(),
56  & surface().surfelAdjacency(),
57  s );
58 }
59 //-----------------------------------------------------------------------------
60 template <typename TKSpace, typename TSurfelPredicate>
61 inline
62 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
63 ::Tracker( const Tracker & other )
64  : mySurface( other.mySurface ), myNeighborhood( other.myNeighborhood )
65 {
66 }
67 //-----------------------------------------------------------------------------
68 template <typename TKSpace, typename TSurfelPredicate>
69 inline
70 const typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
71 ::DigitalSurfaceContainer &
72 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
73 ::surface() const
74 {
75  return mySurface;
76 }
77 //-----------------------------------------------------------------------------
78 template <typename TKSpace, typename TSurfelPredicate>
79 inline
80 const typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
81 ::Surfel &
82 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
83 ::current() const
84 {
85  return myNeighborhood.surfel();
86 }
87 //-----------------------------------------------------------------------------
88 template <typename TKSpace, typename TSurfelPredicate>
89 inline
90 DGtal::Dimension
91 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
92 ::orthDir() const
93 {
94  return myNeighborhood.orthDir();
95 }
96 //-----------------------------------------------------------------------------
97 template <typename TKSpace, typename TSurfelPredicate>
98 inline
99 void
100 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
101 ::move( const Surfel & s )
102 {
103  ASSERT( surface().isInside( s ) );
104  myNeighborhood.setSurfel( s );
105 }
106 //-----------------------------------------------------------------------------
107 template <typename TKSpace, typename TSurfelPredicate>
108 inline
109 DGtal::uint8_t
110 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Tracker
111 ::adjacent( Surfel & s, Dimension d, bool pos ) const
112 {
113  return static_cast<uint8_t>
114  ( myNeighborhood.getAdjacentOnSurfelPredicate
115  ( s, surface().surfelPredicate(), d, pos ) );
116 }
117 
118 ///////////////////////////////////////////////////////////////////////////////
119 // ----------------------- Standard services ------------------------------
120 
121 //-----------------------------------------------------------------------------
122 template <typename TKSpace, typename TSurfelPredicate>
123 inline
124 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::~LightExplicitDigitalSurface()
125 {
126 }
127 //-----------------------------------------------------------------------------
128 template <typename TKSpace, typename TSurfelPredicate>
129 inline
130 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::LightExplicitDigitalSurface
131 ( const LightExplicitDigitalSurface & other )
132  : myKSpace( other.myKSpace ),
133  mySurfelPredicate( other.mySurfelPredicate ),
134  mySurfelAdjacency( other.mySurfelAdjacency ),
135  mySurfel( other.mySurfel ),
136  myTracker( *this, other.mySurfel )
137 {
138 }
139 //-----------------------------------------------------------------------------
140 template <typename TKSpace, typename TSurfelPredicate>
141 inline
142 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::LightExplicitDigitalSurface
143 ( ConstAlias<KSpace> aKSpace,
144  ConstAlias<SurfelPredicate> aSP,
145  const Adjacency & adj,
146  const Surfel & s )
147  : myKSpace( aKSpace ), mySurfelPredicate( aSP ), mySurfelAdjacency( adj ),
148  mySurfel( s ), myTracker( *this, s )
149 {
150 }
151 //-----------------------------------------------------------------------------
152 template <typename TKSpace, typename TSurfelPredicate>
153 inline
154 const
155 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Adjacency &
156 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelAdjacency() const
157 {
158  return mySurfelAdjacency;
159 }
160 //-----------------------------------------------------------------------------
161 template <typename TKSpace, typename TSurfelPredicate>
162 inline
163 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Adjacency &
164 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelAdjacency()
165 {
166  return mySurfelAdjacency;
167 }
168 //-----------------------------------------------------------------------------
169 template <typename TKSpace, typename TSurfelPredicate>
170 inline
171 const
172 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelPredicate &
173 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfelPredicate() const
174 {
175  return mySurfelPredicate;
176 }
177 //-----------------------------------------------------------------------------
178 template <typename TKSpace, typename TSurfelPredicate>
179 inline
180 const
181 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Surfel &
182 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::surfel() const
183 {
184  return mySurfel;
185 }
186 //-----------------------------------------------------------------------------
187 template <typename TKSpace, typename TSurfelPredicate>
188 inline
189 void
190 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::setSurfel(const Surfel& aSurfel)
191 {
192  mySurfel = aSurfel;
193  ASSERT( myKSpace.sIsSurfel( mySurfel ) );
194  ASSERT( mySurfelPredicate( mySurfel ) );
195 
196  myTracker = Tracker( *this, mySurfel );
197 }
198 
199 //-----------------------------------------------------------------------------
200 // --------- CDigitalSurfaceContainer realization -------------------------
201 //-----------------------------------------------------------------------------
202 template <typename TKSpace, typename TSurfelPredicate>
203 inline
204 const typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::KSpace &
205 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::space() const
206 {
207  return myKSpace;
208 }
209 //-----------------------------------------------------------------------------
210 template <typename TKSpace, typename TSurfelPredicate>
211 inline
212 bool
213 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::isInside
214 ( const Surfel & s ) const
215 {
216  return surfelPredicate()( s );
217 }
218 //-----------------------------------------------------------------------------
219 template <typename TKSpace, typename TSurfelPredicate>
220 inline
221 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelConstIterator
222 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::begin() const
223 {
224  SelfVisitorRange range( new SelfVisitor( *this, mySurfel ) );
225  return range.begin();
226  // SelfVisitor* ptrVisitor = new SelfVisitor( *this, mySurfel );
227  // ASSERT( ptrVisitor != 0 );
228  // return SurfelConstIterator( ptrVisitor );
229 }
230 //-----------------------------------------------------------------------------
231 template <typename TKSpace, typename TSurfelPredicate>
232 inline
233 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::SurfelConstIterator
234 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::end() const
235 {
236  return SurfelConstIterator();
237 }
238 //-----------------------------------------------------------------------------
239 template <typename TKSpace, typename TSurfelPredicate>
240 inline
241 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Size
242 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::nbSurfels() const
243 {
244  Size nb = 0;
245  for ( SurfelConstIterator it = begin(), it_end = end(); it != it_end; ++it )
246  ++nb;
247  return nb;
248 }
249 //-----------------------------------------------------------------------------
250 template <typename TKSpace, typename TSurfelPredicate>
251 inline
252 bool
253 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::empty() const
254 {
255  return false;
256 }
257 //-----------------------------------------------------------------------------
258 template <typename TKSpace, typename TSurfelPredicate>
259 inline
260 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::DigitalSurfaceTracker*
261 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::newTracker
262 ( const Surfel & s ) const
263 {
264  return new Tracker( *this, s );
265 }
266 //-----------------------------------------------------------------------------
267 template <typename TKSpace, typename TSurfelPredicate>
268 inline
269 DGtal::Connectedness
270 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::connectedness() const
271 {
272  return CONNECTED;
273 }
274 //-----------------------------------------------------------------------------
275 // ----------------- UndirectedSimplePreGraph realization --------------------
276 //-----------------------------------------------------------------------------
277 template <typename TKSpace, typename TSurfelPredicate>
278 inline
279 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Size
280 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>
281 ::degree( const Vertex & v ) const
282 {
283  Size d = 0;
284  Vertex s;
285  myTracker.move( v );
286  for ( typename KSpace::DirIterator q = space().sDirs( v );
287  q != 0; ++q )
288  {
289  if ( myTracker.adjacent( s, *q, true ) )
290  ++d;
291  if ( myTracker.adjacent( s, *q, false ) )
292  ++d;
293  }
294  return d;
295 }
296 //-----------------------------------------------------------------------------
297 template <typename TKSpace, typename TSurfelPredicate>
298 template <typename OutputIterator>
299 inline
300 void
301 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>
302 ::writeNeighbors( OutputIterator & it,
303  const Vertex & v ) const
304 {
305  Vertex s;
306  myTracker.move( v );
307  for ( typename KSpace::DirIterator q = space().sDirs( v );
308  q != 0; ++q )
309  {
310  if ( myTracker.adjacent( s, *q, true ) )
311  *it++ = s;
312  if ( myTracker.adjacent( s, *q, false ) )
313  *it++ = s;
314  }
315 }
316 //-----------------------------------------------------------------------------
317 template <typename TKSpace, typename TSurfelPredicate>
318 template <typename OutputIterator, typename VertexPredicate>
319 inline
320 void
321 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>
322 ::writeNeighbors( OutputIterator & it,
323  const Vertex & v,
324  const VertexPredicate & pred ) const
325 {
326  BOOST_CONCEPT_ASSERT(( concepts::CVertexPredicate< VertexPredicate > ));
327  Vertex s;
328  myTracker.move( v );
329  for ( typename KSpace::DirIterator q = space().sDirs( v );
330  q != 0; ++q )
331  {
332  if ( myTracker.adjacent( s, *q, true ) )
333  {
334  if ( pred( s ) ) *it++ = s;
335  }
336  if ( myTracker.adjacent( s, *q, false ) )
337  {
338  if ( pred( s ) ) *it++ = s;
339  }
340  }
341 }
342 //-----------------------------------------------------------------------------
343 template <typename TKSpace, typename TSurfelPredicate>
344 inline
345 typename DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::Size
346 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>
347 ::bestCapacity() const
348 {
349  return KSpace::dimension * 2 - 2;
350 }
351 
352 
353 // ------------------------- Hidden services ------------------------------
354 
355 ///////////////////////////////////////////////////////////////////////////////
356 // Interface - public :
357 
358 /**
359  * Writes/Displays the object on an output stream.
360  * @param out the output stream where the object is written.
361  */
362 template <typename TKSpace, typename TSurfelPredicate>
363 inline
364 void
365 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::selfDisplay ( std::ostream & out ) const
366 {
367  out << "[LightExplicitDigitalSurface]";
368 }
369 
370 /**
371  * Checks the validity/consistency of the object.
372  * @return 'true' if the object is valid, 'false' otherwise.
373  */
374 template <typename TKSpace, typename TSurfelPredicate>
375 inline
376 bool
377 DGtal::LightExplicitDigitalSurface<TKSpace,TSurfelPredicate>::isValid() const
378 {
379  return true;
380 }
381 
382 
383 
384 ///////////////////////////////////////////////////////////////////////////////
385 // Implementation of inline functions //
386 
387 template <typename TKSpace, typename TSurfelPredicate>
388 inline
389 std::ostream&
390 DGtal::operator<< ( std::ostream & out,
391  const LightExplicitDigitalSurface< TKSpace, TSurfelPredicate > & object )
392 {
393  object.selfDisplay( out );
394  return out;
395 }
396 
397 // //
398 ///////////////////////////////////////////////////////////////////////////////
399 
400