DGtal 1.3.0
Loading...
Searching...
No Matches
COBAGenericNaivePlaneComputer.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 COBAGenericNaivePlaneComputer.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/09/20
23 *
24 * Implementation of inline methods defined in COBAGenericNaivePlaneComputer.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//-----------------------------------------------------------------------------
42template <typename TSpace, typename TInternalInteger>
43inline
44DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
45~COBAGenericNaivePlaneComputer()
46{ // Nothing to do.
47}
48//-----------------------------------------------------------------------------
49template <typename TSpace, typename TInternalInteger>
50inline
51DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
52COBAGenericNaivePlaneComputer()
53{ // Object is invalid
54 _axesToErase.reserve( 3 );
55}
56//-----------------------------------------------------------------------------
57template <typename TSpace, typename TInternalInteger>
58inline
59DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
60COBAGenericNaivePlaneComputer( const COBAGenericNaivePlaneComputer & other )
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//-----------------------------------------------------------------------------
69template <typename TSpace, typename TInternalInteger>
70inline
71DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger> &
72DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
73operator=( const COBAGenericNaivePlaneComputer & other )
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//-----------------------------------------------------------------------------
85template <typename TSpace, typename TInternalInteger>
86inline
87DGtal::Dimension
88DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
89active() const
90{
91 ASSERT( myAxes.size() > 0 );
92 return myAxes[ 0 ];
93}
94//-----------------------------------------------------------------------------
95template <typename TSpace, typename TInternalInteger>
96inline
97typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::MyIntegerComputer &
98DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
99ic() const
100{
101 return myComputers[ active() ].ic();
102}
103//-----------------------------------------------------------------------------
104template <typename TSpace, typename TInternalInteger>
105inline
106void
107DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
108clear()
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//-----------------------------------------------------------------------------
118template <typename TSpace, typename TInternalInteger>
119inline
120void
121DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
122init( InternalInteger diameter,
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//-----------------------------------------------------------------------------
131template <typename TSpace, typename TInternalInteger>
132inline
133typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::ConstIterator
134DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
135begin() const
136{
137 return myComputers[ active() ].begin();
138}
139//-----------------------------------------------------------------------------
140template <typename TSpace, typename TInternalInteger>
141inline
142typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::ConstIterator
143DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
144end() const
145{
146 return myComputers[ active() ].end();
147}
148//-----------------------------------------------------------------------------
149template <typename TSpace, typename TInternalInteger>
150inline
151typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Size
152DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
153size() const
154{
155 return myComputers[ active() ].size();
156}
157//-----------------------------------------------------------------------------
158template <typename TSpace, typename TInternalInteger>
159inline
160bool
161DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
162empty() const
163{
164 return myComputers[ active() ].empty();
165}
166//-----------------------------------------------------------------------------
167template <typename TSpace, typename TInternalInteger>
168inline
169typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Size
170DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
171max_size() const
172{
173 return myComputers[ active() ].max_size();
174}
175//-----------------------------------------------------------------------------
176template <typename TSpace, typename TInternalInteger>
177inline
178typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Size
179DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
180maxSize() const
181{
182 return max_size();
183}
184//-----------------------------------------------------------------------------
185template <typename TSpace, typename TInternalInteger>
186inline
187typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Size
188DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
189complexity() const
190{
191 return myComputers[ active() ].complexity();
192}
193//-----------------------------------------------------------------------------
194template <typename TSpace, typename TInternalInteger>
195inline
196bool
197DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
198operator()( const Point & p ) const
199{
200 return myComputers[ active() ].operator()( p );
201}
202
203//-----------------------------------------------------------------------------
204template <typename TSpace, typename TInternalInteger>
205inline
206bool
207DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
208extendAsIs( const Point & p )
209{
210 ASSERT( isValid() );
211 unsigned int nbok = 0;
212 for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
213 it != itE; ++it )
214 {
215 nbok += myComputers[ *it ].operator()( p ) ? 1 : 0;
216 }
217 if ( nbok != 0 ) // at least one is ok.
218 {
219 for ( AxisIterator it = myAxes.begin(); it != myAxes.end(); )
220 // cannot put end() in variable, since end() moves when
221 // modifiying a vector.
222 {
223 bool ok = myComputers[ *it ].extendAsIs( p );
224 if ( ! ok )
225 it = myAxes.erase( it );
226 else
227 ++it;
228 }
229 ASSERT( ! myAxes.empty() );
230 return true;
231 }
232 return false;
233}
234
235//-----------------------------------------------------------------------------
236template <typename TSpace, typename TInternalInteger>
237bool
238DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
239extend( const Point & p )
240{
241 ASSERT( isValid() );
242 unsigned int nbok = 0;
243 _axesToErase.clear();
244 for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
245 axIt != axItE; ++axIt )
246 {
247 bool ok = myComputers[ *axIt ].extend( p );
248 if ( ! ok ) _axesToErase.push_back( *axIt );
249 else ++nbok;
250 }
251 if ( nbok != 0 ) // at least one is ok.
252 { // if one is ok, we must remove ko ones from the list of active
253 // axes.
254 AxisIterator axIt = myAxes.begin();
255 for ( unsigned int i = 0; i < _axesToErase.size(); ++i )
256 {
257 while ( *axIt != _axesToErase[ i ] ) ++axIt;
258 axIt = myAxes.erase( axIt );
259 }
260 return true;
261 }
262 return false;
263}
264//-----------------------------------------------------------------------------
265template <typename TSpace, typename TInternalInteger>
266bool
267DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
268isExtendable( const Point & p ) const
269{
270 ASSERT( isValid() );
271 unsigned int nbok = 0;
272 for ( AxisConstIterator it = myAxes.begin(), itE = myAxes.end();
273 it != itE; ++it )
274 {
275 nbok += myComputers[ *it ].isExtendable( p ) ? 1 : 0;
276 }
277 return nbok != 0;
278}
279//-----------------------------------------------------------------------------
280template <typename TSpace, typename TInternalInteger>
281template <typename TInputIterator>
282bool
283DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
284extend( TInputIterator it, TInputIterator itE )
285{
286 BOOST_CONCEPT_ASSERT(( boost::InputIterator<TInputIterator> ));
287
288 ASSERT( isValid() );
289 unsigned int nbok = 0;
290 _axesToErase.clear();
291 for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
292 axIt != axItE; ++axIt )
293 {
294 bool ok = myComputers[ *axIt ].extend( it, itE );
295 if ( ! ok ) _axesToErase.push_back( *axIt );
296 else ++nbok;
297 }
298 if ( nbok != 0 ) // at least one is ok.
299 { // if one is ok, we must remove ko ones from the list of active
300 // axes.
301 AxisIterator axIt = myAxes.begin();
302 for ( unsigned int i = 0; i < _axesToErase.size(); ++i )
303 {
304 while ( *axIt != _axesToErase[ i ] ) ++axIt;
305 axIt = myAxes.erase( axIt );
306 }
307 return true;
308 }
309 return false;
310}
311//-----------------------------------------------------------------------------
312template <typename TSpace, typename TInternalInteger>
313template <typename TInputIterator>
314bool
315DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
316isExtendable( TInputIterator it, TInputIterator itE ) const
317{
318 BOOST_CONCEPT_ASSERT(( boost::InputIterator<TInputIterator> ));
319
320 ASSERT( isValid() );
321 unsigned int nbok = 0;
322 for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
323 axIt != axItE; ++axIt )
324 {
325 nbok += myComputers[ *axIt ].isExtendable( it, itE ) ? 1 : 0;
326 }
327 return nbok != 0;
328}
329//-----------------------------------------------------------------------------
330template <typename TSpace, typename TInternalInteger>
331inline
332typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Primitive
333DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
334primitive() const
335{
336 return myComputers[ active() ].primitive();
337}
338//-----------------------------------------------------------------------------
339template <typename TSpace, typename TInternalInteger>
340template <typename Vector3D>
341inline
342void
343DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
344getNormal( Vector3D & normal ) const
345{
346 myComputers[ active() ].getNormal( normal );
347}
348//-----------------------------------------------------------------------------
349//-----------------------------------------------------------------------------
350template <typename TSpace, typename TInternalInteger>
351template <typename Vector3D>
352inline
353void
354DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
355getUnitNormal( Vector3D & normal ) const
356{
357 myComputers[ active() ].getUnitNormal( normal );
358}
359//-----------------------------------------------------------------------------
360template <typename TSpace, typename TInternalInteger>
361inline
362void
363DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
364getBounds( double & min, double & max ) const
365{
366 myComputers[ active() ].getBounds( min, max );
367}
368//-----------------------------------------------------------------------------
369template <typename TSpace, typename TInternalInteger>
370inline
371const typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Point &
372DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
373minimalPoint() const
374{
375 return myComputers[ active() ].minimalPoint();
376}
377//-----------------------------------------------------------------------------
378template <typename TSpace, typename TInternalInteger>
379inline
380const typename DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::Point &
381DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::
382maximalPoint() const
383{
384 return myComputers[ active() ].maximalPoint();
385}
386
387
388
389///////////////////////////////////////////////////////////////////////////////
390// Interface - public :
391
392/**
393 * Writes/Displays the object on an output stream.
394 * @param out the output stream where the object is written.
395 */
396template <typename TSpace, typename TInternalInteger>
397inline
398void
399DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::selfDisplay ( std::ostream & out ) const
400{
401 out << "[COBAGenericNaivePlane";
402 for ( AxisConstIterator axIt = myAxes.begin(), axItE = myAxes.end();
403 axIt != axItE; ++axIt )
404 out << " " << myComputers[ *axIt ];
405 out << " ]";
406}
407
408/**
409 * Checks the validity/consistency of the object.
410 * @return 'true' if the object is valid, 'false' otherwise.
411 */
412template <typename TSpace, typename TInternalInteger>
413inline
414bool
415DGtal::COBAGenericNaivePlaneComputer<TSpace, TInternalInteger>::isValid() const
416{
417 return myComputers[ active() ].isValid();
418}
419
420
421///////////////////////////////////////////////////////////////////////////////
422// Internals
423///////////////////////////////////////////////////////////////////////////////
424
425///////////////////////////////////////////////////////////////////////////////
426// Implementation of inline functions //
427
428template <typename TSpace, typename TInternalInteger>
429inline
430std::ostream&
431DGtal::operator<< ( std::ostream & out,
432 const COBAGenericNaivePlaneComputer<TSpace, TInternalInteger> & object )
433{
434 object.selfDisplay( out );
435 return out;
436}
437
438// //
439///////////////////////////////////////////////////////////////////////////////