DGtal  0.9.4.1
PolygonalSurface.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 PolygonalSurface.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 2017/02/05
23  *
24  * Implementation of inline methods defined in PolygonalSurface.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 template <typename TPoint>
43 inline
44 bool
45 DGtal::PolygonalSurface<TPoint>::build()
46 {
47  if ( isHEDSValid ) {
48  trace.warning() << "[DGtal::PolygonalSurface<TPoint>::build()]"
49  << " attempting to rebuild a polygonal surface." << std::endl;
50  return false;
51  }
52  isHEDSValid = myHEDS.build( myPolygonalFaces );
53  if ( myHEDS.nbVertices() != myPositions.size() ) {
54  trace.warning() << "[DGtal::PolygonalSurface<TPoint>::build()]"
55  << " the size of vertex data array (s1) and the number of vertices (s2) in the polygonal surface does not match:"
56  << " s1=" << myPositions.size()
57  << " s2=" << myHEDS.nbVertices() << std::endl;
58  isHEDSValid = false;
59  }
60  return isHEDSValid;
61 }
62 
63 //-----------------------------------------------------------------------------
64 template <typename TPoint>
65 inline
66 void
67 DGtal::PolygonalSurface<TPoint>::clear()
68 {
69  isHEDSValid = false;
70  myHEDS.clear();
71  myPositions.clear();
72  myPolygonalFaces.clear();
73 
74 }
75 //-----------------------------------------------------------------------------
76 template <typename TPoint>
77 inline
78 typename DGtal::PolygonalSurface<TPoint>::VertexIndex
79 DGtal::PolygonalSurface<TPoint>::addVertex( const Point& vdata )
80 {
81  VertexIndex vi = myPositions.size();
82  myPositions.push_back( vdata );
83  return vi;
84 }
85 
86 //-----------------------------------------------------------------------------
87 template <typename TPoint>
88 inline
89 typename DGtal::PolygonalSurface<TPoint>::FaceIndex
90 DGtal::PolygonalSurface<TPoint>::addTriangle
91 ( VertexIndex v0, VertexIndex v1, VertexIndex v2 )
92 {
93  FaceIndex fi = myPolygonalFaces.size();
94  myPolygonalFaces.push_back( PolygonalFace( { v0, v1, v2 } ) );
95  return fi;
96 }
97 //-----------------------------------------------------------------------------
98 template <typename TPoint>
99 inline
100 typename DGtal::PolygonalSurface<TPoint>::FaceIndex
101 DGtal::PolygonalSurface<TPoint>::addQuadrangle
102 ( VertexIndex v0, VertexIndex v1, VertexIndex v2, VertexIndex v3 )
103 {
104  FaceIndex fi = myPolygonalFaces.size();
105  myPolygonalFaces.push_back( PolygonalFace( { v0, v1, v2, v3 } ) );
106  return fi;
107 }
108 //-----------------------------------------------------------------------------
109 template <typename TPoint>
110 inline
111 typename DGtal::PolygonalSurface<TPoint>::FaceIndex
112 DGtal::PolygonalSurface<TPoint>::addPolygonalFace
113 ( const PolygonalFace& f )
114 {
115  FaceIndex fi = myPolygonalFaces.size();
116  myPolygonalFaces.push_back( f );
117  return fi;
118 }
119 
120 //-----------------------------------------------------------------------------
121 template <typename TPoint>
122 inline
123 typename DGtal::PolygonalSurface<TPoint>::Point&
124 DGtal::PolygonalSurface<TPoint>::position( Vertex v )
125 {
126  ASSERT( 0 <= v && v < myPositions.size() );
127  return myPositions[ v ];
128 }
129 //-----------------------------------------------------------------------------
130 template <typename TPoint>
131 inline
132 const typename DGtal::PolygonalSurface<TPoint>::Point&
133 DGtal::PolygonalSurface<TPoint>::position( Vertex v ) const
134 {
135  ASSERT( 0 <= v && v < myPositions.size() );
136  return myPositions[ v ];
137 }
138 //-----------------------------------------------------------------------------
139 template <typename TPoint>
140 inline
141 typename DGtal::PolygonalSurface<TPoint>::Size
142 DGtal::PolygonalSurface<TPoint>::size() const
143 {
144  return myPositions.size();
145 }
146 //-----------------------------------------------------------------------------
147 template <typename TPoint>
148 inline
149 typename DGtal::PolygonalSurface<TPoint>::Size
150 DGtal::PolygonalSurface<TPoint>::bestCapacity() const
151 {
152  return 8;
153 }
154 //-----------------------------------------------------------------------------
155 template <typename TPoint>
156 inline
157 typename DGtal::PolygonalSurface<TPoint>::Size
158 DGtal::PolygonalSurface<TPoint>::degree( const Vertex & v ) const
159 {
160  ASSERT( isValid() );
161  return myHEDS.nbNeighboringVertices( v );
162 }
163 
164 //-----------------------------------------------------------------------------
165 template <typename TPoint>
166 template <typename OutputIterator>
167 inline
168 void
169 DGtal::PolygonalSurface<TPoint>::writeNeighbors
170 ( OutputIterator &it, const Vertex & v ) const
171 {
172  ASSERT( isValid() );
173  typedef HalfEdgeDataStructure::VertexIndexRange VertexIndexRange;
174  VertexIndexRange neighbors;
175  myHEDS.getNeighboringVertices( v, neighbors );
176  for ( Vertex nv : neighbors ) *it++ = nv;
177 }
178 
179 //-----------------------------------------------------------------------------
180 template <typename TPoint>
181 template <typename OutputIterator, typename VertexPredicate>
182 inline
183 void
184 DGtal::PolygonalSurface<TPoint>::writeNeighbors
185 ( OutputIterator &it, const Vertex & v, const VertexPredicate & pred) const
186 {
187  ASSERT( isValid() );
188  typedef HalfEdgeDataStructure::VertexIndexRange VertexIndexRange;
189  VertexIndexRange neighbors;
190  myHEDS.getNeighboringVertices( v, neighbors );
191  for ( Vertex nv : neighbors ) if ( pred( nv ) ) *it++ = nv;
192 }
193 
194 //-----------------------------------------------------------------------------
195 template <typename TPoint>
196 inline
197 typename DGtal::PolygonalSurface<TPoint>::ArcRange
198 DGtal::PolygonalSurface<TPoint>::outArcs( const Vertex & v ) const
199 {
200  ArcRange result;
201  const Index start_hei = myHEDS.halfEdgeIndexFromVertexIndex( v );
202  Index hei = start_hei;
203  do
204  {
205  const HalfEdge& he = myHEDS.halfEdge( hei );
206  if( INVALID_FACE != he.face ) result.push_back( hei );
207  hei = myHEDS.halfEdge( he.opposite ).next;
208  }
209  while ( hei != start_hei );
210  return result;
211 }
212 
213 //-----------------------------------------------------------------------------
214 template <typename TPoint>
215 inline
216 typename DGtal::PolygonalSurface<TPoint>::ArcRange
217 DGtal::PolygonalSurface<TPoint>::inArcs( const Vertex & v ) const
218 {
219  ArcRange result;
220  const Index start_hei = myHEDS.halfEdgeIndexFromVertexIndex( v );
221  Index hei = start_hei;
222  do
223  {
224  const HalfEdge& he = myHEDS.halfEdge( hei );
225  if( INVALID_FACE != he.face ) result.push_back( he.opposite );
226  hei = myHEDS.halfEdge( he.opposite ).next;
227  }
228  while ( hei != start_hei );
229  return result;
230 }
231 
232 //-----------------------------------------------------------------------------
233 template <typename TPoint>
234 inline
235 typename DGtal::PolygonalSurface<TPoint>::FaceRange
236 DGtal::PolygonalSurface<TPoint>::facesAroundVertex( const Vertex & v ) const
237 {
238  FaceRange result;
239  const Index start_hei = myHEDS.halfEdgeIndexFromVertexIndex( v );
240  Index hei = start_hei;
241  do
242  {
243  const HalfEdge& he = myHEDS.halfEdge( hei );
244  if( INVALID_FACE != he.face ) result.push_back( he.face );
245  hei = myHEDS.halfEdge( he.opposite ).next;
246  }
247  while ( hei != start_hei );
248  return result;
249 }
250 
251 //-----------------------------------------------------------------------------
252 template <typename TPoint>
253 inline
254 typename DGtal::PolygonalSurface<TPoint>::Vertex
255 DGtal::PolygonalSurface<TPoint>::head( const Arc & a ) const
256 {
257  return myHEDS.halfEdge( a ).toVertex;
258 }
259 
260 //-----------------------------------------------------------------------------
261 template <typename TPoint>
262 inline
263 typename DGtal::PolygonalSurface<TPoint>::Vertex
264 DGtal::PolygonalSurface<TPoint>::tail( const Arc & a ) const
265 {
266  return head( opposite( a ) );
267 }
268 
269 //-----------------------------------------------------------------------------
270 template <typename TPoint>
271 inline
272 typename DGtal::PolygonalSurface<TPoint>::Arc
273 DGtal::PolygonalSurface<TPoint>::opposite( const Arc & a ) const
274 {
275  return myHEDS.halfEdge( a ).opposite;
276 }
277 
278 //-----------------------------------------------------------------------------
279 template <typename TPoint>
280 inline
281 typename DGtal::PolygonalSurface<TPoint>::Arc
282 DGtal::PolygonalSurface<TPoint>::next( const Arc & a ) const
283 {
284  return myHEDS.halfEdge( a ).next;
285 }
286 //-----------------------------------------------------------------------------
287 template <typename TPoint>
288 inline
289 typename DGtal::PolygonalSurface<TPoint>::Arc
290 DGtal::PolygonalSurface<TPoint>::arc
291 ( const Vertex & t, const Vertex & h ) const
292 {
293  return myHEDS.halfEdgeIndexFromArc( t, h );
294 }
295 
296 //-----------------------------------------------------------------------------
297 template <typename TPoint>
298 inline
299 typename DGtal::PolygonalSurface<TPoint>::Face
300 DGtal::PolygonalSurface<TPoint>::faceAroundArc( const Arc & a ) const
301 {
302  return myHEDS.halfEdge( a ).face;
303 }
304 //-----------------------------------------------------------------------------
305 template <typename TPoint>
306 inline
307 typename DGtal::PolygonalSurface<TPoint>::FaceRange
308 DGtal::PolygonalSurface<TPoint>::facesAroundArc( const Arc & a ) const
309 {
310  FaceRange result;
311  Face f = faceAroundArc( a );
312  if ( f != INVALID_FACE ) result.push_back( f );
313  return result;
314 }
315 
316 //-----------------------------------------------------------------------------
317 template <typename TPoint>
318 inline
319 typename DGtal::PolygonalSurface<TPoint>::VertexRange
320 DGtal::PolygonalSurface<TPoint>::verticesAroundFace( const Face & f ) const
321 {
322  VertexRange result;
323  const Index start_hei = myHEDS.halfEdgeIndexFromFaceIndex( f );
324  Index hei = start_hei;
325  do {
326  const HalfEdge& he = myHEDS.halfEdge( hei );
327  ASSERT( ( he.face == f )
328  && "[PolygonalSurface::verticesAroundFace] invalid face." );
329  result.push_back( he.toVertex );
330  hei = he.next;
331  } while ( hei != start_hei );
332  ASSERT( result.size() >= 3 );
333  return result;
334 }
335 
336 //-----------------------------------------------------------------------------
337 template <typename TPoint>
338 inline
339 bool
340 DGtal::PolygonalSurface<TPoint>::isVertexBoundary( const Vertex& v ) const
341 {
342  return myHEDS.isVertexBoundary( v );
343 }
344 
345 //-----------------------------------------------------------------------------
346 template <typename TPoint>
347 inline
348 bool
349 DGtal::PolygonalSurface<TPoint>::isArcBoundary( const Arc& v ) const
350 {
351  return INVALID_FACE == myHEDS.halfEdge( v ).face;
352 }
353 
354 //-----------------------------------------------------------------------------
355 template <typename TPoint>
356 inline
357 typename DGtal::PolygonalSurface<TPoint>::FaceRange
358 DGtal::PolygonalSurface<TPoint>::allFaces() const
359 {
360  FaceRange result( nbFaces() );
361  for ( Face fi = 0; fi < result.size(); ++fi )
362  result[ fi ] = fi;
363  return result;
364 }
365 //-----------------------------------------------------------------------------
366 template <typename TPoint>
367 inline
368 typename DGtal::PolygonalSurface<TPoint>::ArcRange
369 DGtal::PolygonalSurface<TPoint>::allArcs() const
370 {
371  ArcRange result( nbArcs() );
372  for ( Arc fi = 0; fi < result.size(); ++fi )
373  result[ fi ] = fi;
374  return result;
375 }
376 //-----------------------------------------------------------------------------
377 template <typename TPoint>
378 inline
379 typename DGtal::PolygonalSurface<TPoint>::VertexRange
380 DGtal::PolygonalSurface<TPoint>::allVertices() const
381 {
382  VertexRange result( nbVertices() );
383  for ( Vertex fi = 0; fi < result.size(); ++fi )
384  result[ fi ] = fi;
385  return result;
386 }
387 
388 //-----------------------------------------------------------------------------
389 template <typename TPoint>
390 inline
391 typename DGtal::PolygonalSurface<TPoint>::ArcRange
392 DGtal::PolygonalSurface<TPoint>::allBoundaryArcs() const
393 {
394  return myHEDS.boundaryHalfEdgeIndices();
395 }
396 
397 //-----------------------------------------------------------------------------
398 template <typename TPoint>
399 inline
400 typename DGtal::PolygonalSurface<TPoint>::VertexRange
401 DGtal::PolygonalSurface<TPoint>::allBoundaryVertices() const
402 {
403  return myHEDS.boundaryVertices();
404 }
405 
406 
407 ///////////////////////////////////////////////////////////////////////////////
408 // Interface - public :
409 
410 /**
411  * Writes/Displays the object on an output stream.
412  * @param out the output stream where the object is written.
413  */
414 template <typename TPoint>
415 inline
416 void
417 DGtal::PolygonalSurface<TPoint>::selfDisplay ( std::ostream & out ) const
418 {
419  out << "[PolygonalSurface #V=" << myHEDS.nbVertices()
420  << " #E=" << myHEDS.nbEdges() << " #F=" << myHEDS.nbFaces()
421  << " Chi=" << Euler() << "]";
422 }
423 
424 /**
425  * Checks the validity/consistency of the object.
426  * @return 'true' if the object is valid, 'false' otherwise.
427  */
428 template <typename TPoint>
429 inline
430 bool
431 DGtal::PolygonalSurface<TPoint>::isValid() const
432 {
433  return isHEDSValid;
434 }
435 
436 
437 
438 ///////////////////////////////////////////////////////////////////////////////
439 // Implementation of inline functions //
440 
441 template <typename TPoint>
442 inline
443 std::ostream&
444 DGtal::operator<< ( std::ostream & out,
445  const PolygonalSurface<TPoint> & object )
446 {
447  object.selfDisplay( out );
448  return out;
449 }
450 
451 // //
452 ///////////////////////////////////////////////////////////////////////////////
453 
454