DGtal  1.5.beta
ImplicitDigitalSurface.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 ImplicitDigitalSurface.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 ImplicitDigitalSurface.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 TPointPredicate>
42 inline
43 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
44 ::~Tracker()
45 {}
46 //-----------------------------------------------------------------------------
47 template <typename TKSpace, typename TPointPredicate>
48 inline
49 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::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 TPointPredicate>
60 inline
61 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
62 ::Tracker( const Tracker & other )
63  : mySurface( other.mySurface ), myNeighborhood( other.myNeighborhood )
64 {
65 }
66 //-----------------------------------------------------------------------------
67 template <typename TKSpace, typename TPointPredicate>
68 inline
69 const typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
70 ::DigitalSurfaceContainer &
71 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
72 ::surface() const
73 {
74  return mySurface;
75 }
76 //-----------------------------------------------------------------------------
77 template <typename TKSpace, typename TPointPredicate>
78 inline
79 const typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
80 ::Surfel &
81 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
82 ::current() const
83 {
84  return myNeighborhood.surfel();
85 }
86 //-----------------------------------------------------------------------------
87 template <typename TKSpace, typename TPointPredicate>
88 inline
89 DGtal::Dimension
90 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
91 ::orthDir() const
92 {
93  return myNeighborhood.orthDir();
94 }
95 //-----------------------------------------------------------------------------
96 template <typename TKSpace, typename TPointPredicate>
97 inline
98 void
99 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
100 ::move( const Surfel & s )
101 {
102  ASSERT( surface().isInside( s ) );
103  myNeighborhood.setSurfel( s );
104 }
105 //-----------------------------------------------------------------------------
106 template <typename TKSpace, typename TPointPredicate>
107 inline
108 DGtal::uint8_t
109 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Tracker
110 ::adjacent( Surfel & s, Dimension d, bool pos ) const
111 {
112  return static_cast<uint8_t>
113  ( myNeighborhood.getAdjacentOnPointPredicate( s, surface().pointPredicate(), d, pos ) );
114 }
115 
116 ///////////////////////////////////////////////////////////////////////////////
117 // ----------------------- Standard services ------------------------------
118 
119 //-----------------------------------------------------------------------------
120 template <typename TKSpace, typename TPointPredicate>
121 inline
122 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::~ImplicitDigitalSurface()
123 {
124 }
125 //-----------------------------------------------------------------------------
126 template <typename TKSpace, typename TPointPredicate>
127 inline
128 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::ImplicitDigitalSurface
129 ( const ImplicitDigitalSurface & other )
130  : myKSpace( other.myKSpace ),
131  myPointPredicate( other.myPointPredicate ),
132  mySurfelAdjacency( other.mySurfelAdjacency ),
133  mySurfels( other. mySurfels )
134 {
135 }
136 //-----------------------------------------------------------------------------
137 template <typename TKSpace, typename TPointPredicate>
138 inline
139 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::ImplicitDigitalSurface
140 ( ConstAlias<KSpace> aKSpace,
141  ConstAlias<PointPredicate> aPP,
142  const Adjacency & adj,
143  const Surfel & s,
144  bool closed )
145  : myKSpace( aKSpace ), myPointPredicate( aPP ), mySurfelAdjacency( adj )
146 {
147  computeSurfels( s, closed );
148 }
149 //-----------------------------------------------------------------------------
150 template <typename TKSpace, typename TPointPredicate>
151 inline
152 const
153 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Adjacency &
154 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::surfelAdjacency() const
155 {
156  return mySurfelAdjacency;
157 }
158 //-----------------------------------------------------------------------------
159 template <typename TKSpace, typename TPointPredicate>
160 inline
161 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Adjacency &
162 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::surfelAdjacency()
163 {
164  return mySurfelAdjacency;
165 }
166 //-----------------------------------------------------------------------------
167 template <typename TKSpace, typename TPointPredicate>
168 inline
169 const
170 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::PointPredicate &
171 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::pointPredicate() const
172 {
173  return myPointPredicate;
174 }
175 
176 //-----------------------------------------------------------------------------
177 // --------- CDigitalSurfaceContainer realization -------------------------
178 //-----------------------------------------------------------------------------
179 template <typename TKSpace, typename TPointPredicate>
180 inline
181 const typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::KSpace &
182 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::space() const
183 {
184  return myKSpace;
185 }
186 //-----------------------------------------------------------------------------
187 template <typename TKSpace, typename TPointPredicate>
188 inline
189 bool
190 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::isInside
191 ( const Surfel & s ) const
192 {
193  Dimension k = myKSpace.sOrthDir( s );
194  // checks if the surfel is on the space boundary.
195  if ( myKSpace.sIsMax( s, k ) || myKSpace.sIsMin( s, k ) )
196  return false;
197  // p1 must be in the set and p2 must not be in the set.
198  SCell spel1 = myKSpace.sDirectIncident( s, k );
199  Point p1 = myKSpace.sCoords( spel1 );
200  if ( myPointPredicate( p1 ) )
201  {
202  SCell spel2 = myKSpace.sIndirectIncident( s, k );
203  Point p2 = myKSpace.sCoords( spel2 );
204  return ! myPointPredicate( p2 );
205  }
206  return false;
207 }
208 //-----------------------------------------------------------------------------
209 template <typename TKSpace, typename TPointPredicate>
210 inline
211 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::SurfelConstIterator
212 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::begin() const
213 {
214  return mySurfels.begin();
215 }
216 //-----------------------------------------------------------------------------
217 template <typename TKSpace, typename TPointPredicate>
218 inline
219 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::SurfelConstIterator
220 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::end() const
221 {
222  return mySurfels.end();
223 }
224 //-----------------------------------------------------------------------------
225 template <typename TKSpace, typename TPointPredicate>
226 inline
227 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::Size
228 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::nbSurfels() const
229 {
230  return static_cast<Size>(mySurfels.size());
231 }
232 //-----------------------------------------------------------------------------
233 template <typename TKSpace, typename TPointPredicate>
234 inline
235 bool
236 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::empty() const
237 {
238  return mySurfels.empty();
239 }
240 //-----------------------------------------------------------------------------
241 template <typename TKSpace, typename TPointPredicate>
242 inline
243 typename DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::DigitalSurfaceTracker*
244 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::newTracker
245 ( const Surfel & s ) const
246 {
247  return new Tracker( *this, s );
248 }
249 //-----------------------------------------------------------------------------
250 template <typename TKSpace, typename TPointPredicate>
251 inline
252 DGtal::Connectedness
253 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::connectedness() const
254 {
255  return CONNECTED;
256 }
257 
258 // ------------------------- Hidden services ------------------------------
259 //-----------------------------------------------------------------------------
260 template <typename TKSpace, typename TPointPredicate>
261 inline
262 void
263 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::computeSurfels
264 ( const Surfel & p, bool closed )
265 {
266  mySurfels.clear();
267  typename KSpace::SCellSet surface;
268  if ( closed )
269  Surfaces<KSpace>::trackClosedBoundary( surface,
270  myKSpace,
271  mySurfelAdjacency,
272  myPointPredicate,
273  p );
274  else
275  Surfaces<KSpace>::trackBoundary( surface,
276  myKSpace,
277  mySurfelAdjacency,
278  myPointPredicate,
279  p );
280  for ( typename KSpace::SCellSet::const_iterator it = surface.begin(),
281  it_end = surface.end(); it != it_end; ++it )
282  mySurfels.push_back( *it );
283 }
284 
285 ///////////////////////////////////////////////////////////////////////////////
286 // Interface - public :
287 
288 /**
289  * Writes/Displays the object on an output stream.
290  * @param out the output stream where the object is written.
291  */
292 template <typename TKSpace, typename TPointPredicate>
293 inline
294 void
295 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::selfDisplay ( std::ostream & out ) const
296 {
297  out << "[ImplicitDigitalSurface]";
298 }
299 
300 /**
301  * Checks the validity/consistency of the object.
302  * @return 'true' if the object is valid, 'false' otherwise.
303  */
304 template <typename TKSpace, typename TPointPredicate>
305 inline
306 bool
307 DGtal::ImplicitDigitalSurface<TKSpace,TPointPredicate>::isValid() const
308 {
309  return true;
310 }
311 
312 
313 
314 ///////////////////////////////////////////////////////////////////////////////
315 // Implementation of inline functions //
316 
317 template <typename TKSpace, typename TPointPredicate>
318 inline
319 std::ostream&
320 DGtal::operator<< ( std::ostream & out,
321  const ImplicitDigitalSurface<TKSpace,TPointPredicate> & object )
322 {
323  object.selfDisplay( out );
324  return out;
325 }
326 
327 // //
328 ///////////////////////////////////////////////////////////////////////////////
329 
330