DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
COBAGenericNaivePlane.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 //-----------------------------------------------------------------------------
42 template <typename TSpace, typename TInternalInteger>
43 inline
46 { // Nothing to do.
47 }
48 //-----------------------------------------------------------------------------
49 template <typename TSpace, typename TInternalInteger>
50 inline
53 { // Object is invalid
54  _axesToErase.reserve( 3 );
55 }
56 //-----------------------------------------------------------------------------
57 template <typename TSpace, typename TInternalInteger>
58 inline
61  : myAxes( other.myAxes )
62 {
63  for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
64  it != itE; ++it )
65  myComputers[ *it ] = other.myComputers[ *it ];
66  _axesToErase.reserve( 3 );
67 }
68 //-----------------------------------------------------------------------------
69 template <typename TSpace, typename TInternalInteger>
70 inline
74 {
75  if ( this != &other )
76  {
77  myAxes = other.myAxes;
78  for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
79  it != itE; ++it )
80  myComputers[ *it ] = other.myComputers[ *it ];
81  }
82  return *this;
83 }
84 //-----------------------------------------------------------------------------
85 template <typename TSpace, typename TInternalInteger>
86 inline
89 active() const
90 {
91  ASSERT( myAxes.size() > 0 );
92  return myAxes[ 0 ];
93 }
94 //-----------------------------------------------------------------------------
95 template <typename TSpace, typename TInternalInteger>
96 inline
99 ic() const
100 {
101  return myComputers[ active() ].ic();
102 }
103 //-----------------------------------------------------------------------------
104 template <typename TSpace, typename TInternalInteger>
105 inline
106 void
109 {
110  myAxes.clear();
111  for ( unsigned int i = 0; i < 3; ++i )
112  {
113  myAxes.push_back( i );
114  myComputers[ i ].clear();
115  }
116 }
117 //-----------------------------------------------------------------------------
118 template <typename TSpace, typename TInternalInteger>
119 inline
120 void
123  InternalInteger widthNumerator,
124  InternalInteger widthDenominator )
125 {
126  clear();
127  for ( unsigned int i = 0; i < 3; ++i )
128  myComputers[ i ].init( i, diameter, widthNumerator, widthDenominator );
129 }
130 //-----------------------------------------------------------------------------
131 template <typename TSpace, typename TInternalInteger>
132 inline
135 size() const
136 {
137  return myComputers[ active() ].size();
138 }
139 //-----------------------------------------------------------------------------
140 template <typename TSpace, typename TInternalInteger>
141 inline
142 bool
144 empty() const
145 {
146  return myComputers[ active() ].empty();
147 }
148 //-----------------------------------------------------------------------------
149 template <typename TSpace, typename TInternalInteger>
150 inline
153 max_size() const
154 {
155  return myComputers[ active() ].max_size();
156 }
157 //-----------------------------------------------------------------------------
158 template <typename TSpace, typename TInternalInteger>
159 inline
162 maxSize() const
163 {
164  return max_size();
165 }
166 //-----------------------------------------------------------------------------
167 template <typename TSpace, typename TInternalInteger>
168 inline
171 complexity() const
172 {
173  return myComputers[ active() ].complexity();
174 }
175 //-----------------------------------------------------------------------------
176 template <typename TSpace, typename TInternalInteger>
177 inline
178 bool
180 operator()( const Point & p ) const
181 {
182  return myComputers[ active() ].operator()( p );
183 }
184 
185 //-----------------------------------------------------------------------------
186 template <typename TSpace, typename TInternalInteger>
187 inline
188 bool
190 extendAsIs( const Point & p )
191 {
192  ASSERT( isValid() );
193  unsigned int nbok = 0;
194  for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
195  it != itE; ++it )
196  {
197  nbok += myComputers[ *it ].operator()( p ) ? 1 : 0;
198  }
199  if ( nbok != 0 ) // at least one is ok.
200  {
201  for ( AxisIterator it = myAxes.begin(); it != myAxes.end(); )
202  // cannot put end() in variable, since end() moves when
203  // modifiying a vector.
204  {
205  bool ok = myComputers[ *it ].extendAsIs( p );
206  if ( ! ok )
207  it = myAxes.erase( *it );
208  else
209  ++it;
210  }
211  ASSERT( ! myAxes.empty() );
212  return true;
213  }
214  return false;
215 }
216 
217 //-----------------------------------------------------------------------------
218 template <typename TSpace, typename TInternalInteger>
219 bool
221 extend( const Point & p )
222 {
223  ASSERT( isValid() );
224  unsigned int nbok = 0;
225  _axesToErase.clear();
226  for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
227  axIt != axItE; ++axIt )
228  {
229  bool ok = myComputers[ *axIt ].extend( p );
230  if ( ! ok ) _axesToErase.push_back( *axIt );
231  else ++nbok;
232  }
233  if ( nbok != 0 ) // at least one is ok.
234  { // if one is ok, we must remove ko ones from the list of active
235  // axes.
236  AxisIterator axIt = myAxes.begin();
237  for ( unsigned int i = 0; i < _axesToErase.size(); ++i )
238  {
239  while ( *axIt != _axesToErase[ i ] ) ++axIt;
240  axIt = myAxes.erase( axIt );
241  }
242  return true;
243  }
244  return false;
245 }
246 //-----------------------------------------------------------------------------
247 template <typename TSpace, typename TInternalInteger>
248 bool
250 isExtendable( const Point & p ) const
251 {
252  ASSERT( isValid() );
253  unsigned int nbok = 0;
254  for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
255  it != itE; ++it )
256  {
257  nbok += myComputers[ *it ].isExtendable( p ) ? 1 : 0;
258  }
259  return nbok != 0;
260 }
261 //-----------------------------------------------------------------------------
262 template <typename TSpace, typename TInternalInteger>
263 template <typename TInputIterator>
264 bool
266 extend( TInputIterator it, TInputIterator itE )
267 {
268  BOOST_CONCEPT_ASSERT(( boost::InputIterator<TInputIterator> ));
269 
270  ASSERT( isValid() );
271  unsigned int nbok = 0;
272  _axesToErase.clear();
273  for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
274  axIt != axItE; ++axIt )
275  {
276  bool ok = myComputers[ *axIt ].extend( it, itE );
277  if ( ! ok ) _axesToErase.push_back( *axIt );
278  else ++nbok;
279  }
280  if ( nbok != 0 ) // at least one is ok.
281  { // if one is ok, we must remove ko ones from the list of active
282  // axes.
283  AxisIterator axIt = myAxes.begin();
284  for ( unsigned int i = 0; i < _axesToErase.size(); ++i )
285  {
286  while ( *axIt != _axesToErase[ i ] ) ++axIt;
287  axIt = myAxes.erase( axIt );
288  }
289  return true;
290  }
291  return false;
292 }
293 //-----------------------------------------------------------------------------
294 template <typename TSpace, typename TInternalInteger>
295 template <typename TInputIterator>
296 bool
298 isExtendable( TInputIterator it, TInputIterator itE ) const
299 {
300  BOOST_CONCEPT_ASSERT(( boost::InputIterator<TInputIterator> ));
301 
302  ASSERT( isValid() );
303  unsigned int nbok = 0;
304  for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
305  axIt != axItE; ++axIt )
306  {
307  nbok += myComputers[ *axIt ].isExtendable( it, itE ) ? 1 : 0;
308  }
309  return nbok != 0;
310 }
311 
312 
313 //-----------------------------------------------------------------------------
314 template <typename TSpace, typename TInternalInteger>
315 template <typename Vector3D>
316 inline
317 void
319 getNormal( Vector3D & normal ) const
320 {
321  myComputers[ active() ].getNormal( normal );
322 }
323 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
325 template <typename TSpace, typename TInternalInteger>
326 template <typename Vector3D>
327 inline
328 void
330 getUnitNormal( Vector3D & normal ) const
331 {
332  myComputers[ active() ].getUnitNormal( normal );
333 }
334 //-----------------------------------------------------------------------------
335 template <typename TSpace, typename TInternalInteger>
336 inline
337 void
339 getBounds( double & min, double & max ) const
340 {
341  myComputers[ active() ].getBounds( min, max );
342 }
343 //-----------------------------------------------------------------------------
344 template <typename TSpace, typename TInternalInteger>
345 inline
349 {
350  return myComputers[ active() ].minimalPoint();
351 }
352 //-----------------------------------------------------------------------------
353 template <typename TSpace, typename TInternalInteger>
354 inline
358 {
359  return myComputers[ active() ].maximalPoint();
360 }
361 
362 
363 
365 // Interface - public :
366 
371 template <typename TSpace, typename TInternalInteger>
372 inline
373 void
375 {
376  double min, max;
377  double N[ 3 ];
378  out << "[COBAGenericNaivePlane";
379  for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
380  axIt != axItE; ++axIt )
381  out << " " << myComputers[ *axIt ];
382  out << " ]";
383 }
384 
389 template <typename TSpace, typename TInternalInteger>
390 inline
391 bool
393 {
394  return myComputers[ active() ].isValid();
395 }
396 
397 
399 // Internals
401 
403 // Implementation of inline functions //
404 
405 template <typename TSpace, typename TInternalInteger>
406 inline
407 std::ostream&
408 DGtal::operator<< ( std::ostream & out,
410 {
411  object.selfDisplay( out );
412  return out;
413 }
414 
415 // //
417 
418