DGtal  1.5.beta
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  */
44 template <typename TDigitalSurfaceTracker>
45 inline
46 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
47 ~DigitalSurface2DSlice()
48 {
49 }
50 //-----------------------------------------------------------------------------
51 template <typename TDigitalSurfaceTracker>
52 inline
53 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
54 DigitalSurface2DSlice( DigitalSurfaceTracker* tracker,
55  Dimension i )
56 {
57  init( tracker, i );
58 }
59 //-----------------------------------------------------------------------------
60 template <typename TDigitalSurfaceTracker>
61 bool
62 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
63 init( 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 //-----------------------------------------------------------------------------
108 template <typename TDigitalSurfaceTracker>
109 inline
110 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::Size
111 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
112 size() const
113 {
114  return static_cast<Size>(mySurfels.size());
115 }
116 //-----------------------------------------------------------------------------
117 template <typename TDigitalSurfaceTracker>
118 inline
119 bool
120 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
121 isClosed() const
122 {
123  return myIsClosed;
124 }
125 //-----------------------------------------------------------------------------
126 template <typename TDigitalSurfaceTracker>
127 inline
128 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
129 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
130 start() const
131 {
132  return myStart;
133 }
134 //-----------------------------------------------------------------------------
135 template <typename TDigitalSurfaceTracker>
136 inline
137 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
138 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
139 rstart() const
140 {
141  return ConstReverseIterator( myStart + 1 );
142 }
143 //-----------------------------------------------------------------------------
144 template <typename TDigitalSurfaceTracker>
145 inline
146 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstCirculator
147 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
148 cstart() const
149 {
150  return ConstCirculator( myStart, begin(), end() );
151 }
152 //-----------------------------------------------------------------------------
153 template <typename TDigitalSurfaceTracker>
154 inline
155 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseCirculator
156 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
157 rcstart() const
158 {
159  return ConstReverseCirculator( rstart(), rbegin(), rend() );
160 }
161 
162 //-----------------------------------------------------------------------------
163 template <typename TDigitalSurfaceTracker>
164 inline
165 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
166 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
167 begin() const
168 {
169  return mySurfels.begin();
170 }
171 //-----------------------------------------------------------------------------
172 template <typename TDigitalSurfaceTracker>
173 inline
174 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstIterator
175 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
176 end() const
177 {
178  return mySurfels.end();
179 }
180 //-----------------------------------------------------------------------------
181 template <typename TDigitalSurfaceTracker>
182 inline
183 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
184 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
185 rbegin() const
186 {
187  return mySurfels.rbegin();
188 }
189 //-----------------------------------------------------------------------------
190 template <typename TDigitalSurfaceTracker>
191 inline
192 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseIterator
193 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
194 rend() const
195 {
196  return mySurfels.rend();
197 }
198 //-----------------------------------------------------------------------------
199 template <typename TDigitalSurfaceTracker>
200 inline
201 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstCirculator
202 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
203 c() const
204 {
205  return ConstCirculator( this->begin(), this->begin(), this->end() );
206 }
207 //-----------------------------------------------------------------------------
208 template <typename TDigitalSurfaceTracker>
209 inline
210 typename DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::ConstReverseCirculator
211 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::
212 rc() 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  */
226 template <typename TDigitalSurfaceTracker>
227 inline
228 void
229 DGtal::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  */
238 template <typename TDigitalSurfaceTracker>
239 inline
240 bool
241 DGtal::DigitalSurface2DSlice<TDigitalSurfaceTracker>::isValid() const
242 {
243  return true;
244 }
245 
246 
247 
248 ///////////////////////////////////////////////////////////////////////////////
249 // Implementation of inline functions //
250 
251 template <typename TDigitalSurfaceTracker>
252 inline
253 std::ostream&
254 DGtal::operator<< ( std::ostream & out,
255  const DigitalSurface2DSlice<TDigitalSurfaceTracker> & object )
256 {
257  object.selfDisplay( out );
258  return out;
259 }
260 
261 // //
262 ///////////////////////////////////////////////////////////////////////////////
263 
264