DGtal 1.4.0
Loading...
Searching...
No Matches
DigitalSurface2DSlice.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 DigitalSurface2DSlice.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 2012/02/29
23 *
24 * Implementation of inline methods defined in DigitalSurface2DSlice.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
37
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
40
41/**
42 * Destructor.
43 */
44template <typename TDigitalSurfaceTracker>
45inline
46DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
47~DigitalSurface2DSlice()
48{
49}
50//-----------------------------------------------------------------------------
51template <typename TDigitalSurfaceTracker>
52inline
53DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
54DigitalSurface2DSlice( DigitalSurfaceTracker* tracker,
55 Dimension i )
56{
57 init( tracker, i );
58}
59//-----------------------------------------------------------------------------
60template <typename TDigitalSurfaceTracker>
61bool
62DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
63init( DigitalSurfaceTracker* tracker, Dimension i )
64{
65 const KSpace & K = tracker->surface().space();
66 Surfel start_s = tracker->current(); // the start surfel
67 Surfel current_s = start_s; // the current surfel during the extraction
68 Surfel next_s; // the surfel after the current surfel during the extraction
69 uint8_t code = 0;// the code turn when going to the next surfel.
70 Dimension j = i; // the current tracking direction.
71 if ( K.sOrthDir( start_s ) == i ) return false;
72 do
73 {
74 mySurfels.push_back( current_s );
75 bool direct = K.sDirect( current_s, j );
76 code = tracker->adjacent( next_s, j, direct );
77 if ( code == 0 ) break; // slice is open
78 if ( code != 2 ) // 1 or 3, swap tracking dir and orthogonal dir.
79 j = K.sOrthDir( current_s );
80 tracker->move( next_s );
81 current_s = next_s;
82 }
83 while ( next_s != start_s );
84 // Test if we have looped or if the slice is open.
85 myIsClosed = code != 0;
86 unsigned int shift = 0;
87 if ( ! myIsClosed )
88 { // open slice
89 tracker->move( start_s );
90 j = i;
91 current_s = start_s;
92 bool indirect = ! K.sDirect( current_s, j );
93 while ( ( code = tracker->adjacent( next_s, j, indirect ) ) )
94 { // valid movement;
95 mySurfels.push_front( next_s );
96 ++shift;
97 if ( code != 2 ) // 1 or 3, swap tracking dir and orthogonal dir.
98 j = K.sOrthDir( current_s );
99 tracker->move( next_s );
100 current_s = next_s;
101 indirect = ! K.sDirect( current_s, j );
102 }
103 }
104 myStart = begin() + shift;
105 return true;
106}
107//-----------------------------------------------------------------------------
108template <typename TDigitalSurfaceTracker>
109inline
110typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::Size
111DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
112size() const
113{
114 return static_cast<Size>(mySurfels.size());
115}
116//-----------------------------------------------------------------------------
117template <typename TDigitalSurfaceTracker>
118inline
119bool
120DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
121isClosed() const
122{
123 return myIsClosed;
124}
125//-----------------------------------------------------------------------------
126template <typename TDigitalSurfaceTracker>
127inline
128typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
129DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
130start() const
131{
132 return myStart;
133}
134//-----------------------------------------------------------------------------
135template <typename TDigitalSurfaceTracker>
136inline
137typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
138DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
139rstart() const
140{
141 return ConstReverseIterator( myStart + 1 );
142}
143//-----------------------------------------------------------------------------
144template <typename TDigitalSurfaceTracker>
145inline
146typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstCirculator
147DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
148cstart() const
149{
150 return ConstCirculator( myStart, begin(), end() );
151}
152//-----------------------------------------------------------------------------
153template <typename TDigitalSurfaceTracker>
154inline
155typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseCirculator
156DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
157rcstart() const
158{
159 return ConstReverseCirculator( rstart(), rbegin(), rend() );
160}
161
162//-----------------------------------------------------------------------------
163template <typename TDigitalSurfaceTracker>
164inline
165typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
166DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
167begin() const
168{
169 return mySurfels.begin();
170}
171//-----------------------------------------------------------------------------
172template <typename TDigitalSurfaceTracker>
173inline
174typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
175DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
176end() const
177{
178 return mySurfels.end();
179}
180//-----------------------------------------------------------------------------
181template <typename TDigitalSurfaceTracker>
182inline
183typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
184DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
185rbegin() const
186{
187 return mySurfels.rbegin();
188}
189//-----------------------------------------------------------------------------
190template <typename TDigitalSurfaceTracker>
191inline
192typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
193DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
194rend() const
195{
196 return mySurfels.rend();
197}
198//-----------------------------------------------------------------------------
199template <typename TDigitalSurfaceTracker>
200inline
201typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstCirculator
202DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
203c() const
204{
205 return ConstCirculator( this->begin(), this->begin(), this->end() );
206}
207//-----------------------------------------------------------------------------
208template <typename TDigitalSurfaceTracker>
209inline
210typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseCirculator
211DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
212rc() const
213{
214 return ConstReverseCirculator( ConstReverseIterator( begin() + 1 ),
215 rbegin(),
216 rend() );
217}
218
219///////////////////////////////////////////////////////////////////////////////
220// Interface - public :
221
222/**
223 * Writes/Displays the object on an output stream.
224 * @param out the output stream where the object is written.
225 */
226template <typename TDigitalSurfaceTracker>
227inline
228void
229DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::selfDisplay ( std::ostream & out ) const
230{
231 out << "[DigitalSurface2DSlice]";
232}
233
234/**
235 * Checks the validity/consistency of the object.
236 * @return 'true' if the object is valid, 'false' otherwise.
237 */
238template <typename TDigitalSurfaceTracker>
239inline
240bool
241DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::isValid() const
242{
243 return true;
244}
245
246
247
248///////////////////////////////////////////////////////////////////////////////
249// Implementation of inline functions //
250
251template <typename TDigitalSurfaceTracker>
252inline
253std::ostream&
254DGtal::operator<< ( std::ostream & out,
255 const DigitalSurface2DSlice<TDigitalSurfaceTracker> & object )
256{
257 object.selfDisplay( out );
258 return out;
259}
260
261// //
262///////////////////////////////////////////////////////////////////////////////
263
264