Point Cloud Library (PCL)  1.7.0
mesh_conversion.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_GEOMETRY_MESH_CONVERSION_H
42 #define PCL_GEOMETRY_MESH_CONVERSION_H
43 
44 #include <pcl/PolygonMesh.h>
45 #include <pcl/conversions.h>
46 
47 namespace pcl
48 {
49  namespace geometry
50  {
51  /** \brief Convert a half-edge mesh to a face-vertex mesh.
52  * \param[in] half_edge_mesh The input mesh.
53  * \param[out] face_vertex_mesh The output mesh.
54  * \author Martin Saelzle
55  * \ingroup geometry
56  */
57  template <class HalfEdgeMeshT> void
58  toFaceVertexMesh (const HalfEdgeMeshT& half_edge_mesh, pcl::PolygonMesh& face_vertex_mesh)
59  {
60  typedef HalfEdgeMeshT HalfEdgeMesh;
61  typedef typename HalfEdgeMesh::VertexAroundFaceCirculator VAFC;
62  typedef typename HalfEdgeMesh::FaceIndex FaceIndex;
63 
64  pcl::Vertices polygon;
65  pcl::toPCLPointCloud2 (half_edge_mesh.getVertexDataCloud (), face_vertex_mesh.cloud);
66 
67  face_vertex_mesh.polygons.reserve (half_edge_mesh.sizeFaces ());
68  for (size_t i=0; i<half_edge_mesh.sizeFaces (); ++i)
69  {
70  VAFC circ = half_edge_mesh.getVertexAroundFaceCirculator (FaceIndex (i));
71  const VAFC circ_end = circ;
72  polygon.vertices.clear ();
73  do
74  {
75  polygon.vertices.push_back (circ.getTargetIndex ().get ());
76  } while (++circ != circ_end);
77  face_vertex_mesh.polygons.push_back (polygon);
78  }
79  }
80 
81  /** \brief Convert a face-vertex mesh to a half-edge mesh.
82  * \param[in] face_vertex_mesh The input mesh.
83  * \param[out] half_edge_mesh The output mesh. It must have data associated with the vertices.
84  * \return The number of faces that could NOT be added to the half-edge mesh.
85  * \author Martin Saelzle
86  * \ingroup geometry
87  */
88  template <class HalfEdgeMeshT> int
89  toHalfEdgeMesh (const pcl::PolygonMesh& face_vertex_mesh, HalfEdgeMeshT& half_edge_mesh)
90  {
91  typedef HalfEdgeMeshT HalfEdgeMesh;
92  typedef typename HalfEdgeMesh::VertexDataCloud VertexDataCloud;
93  typedef typename HalfEdgeMesh::VertexIndex VertexIndex;
94  typedef typename HalfEdgeMesh::VertexIndices VertexIndices;
95 
96  BOOST_STATIC_ASSERT (HalfEdgeMesh::HasVertexData::value); // Output mesh must have data associated with the vertices!
97 
98  VertexDataCloud vertices;
99  pcl::fromPCLPointCloud2 (face_vertex_mesh.cloud, vertices);
100 
101  half_edge_mesh.reserveVertices (vertices.size ());
102  half_edge_mesh.reserveEdges (3 * face_vertex_mesh.polygons.size ());
103  half_edge_mesh.reserveFaces ( face_vertex_mesh.polygons.size ());
104 
105  for (typename VertexDataCloud::const_iterator it=vertices.begin (); it!=vertices.end (); ++it)
106  {
107  half_edge_mesh.addVertex (*it);
108  }
109 
110  assert (half_edge_mesh.sizeVertices () == vertices.size ());
111 
112  int count_not_added = 0;
113  VertexIndices vi;
114  vi.reserve (3); // Minimum number (triangle)
115  for (size_t i=0; i<face_vertex_mesh.polygons.size (); ++i)
116  {
117  vi.clear ();
118  for (size_t j=0; j<face_vertex_mesh.polygons [i].vertices.size (); ++j)
119  {
120  vi.push_back (VertexIndex (face_vertex_mesh.polygons [i].vertices [j]));
121  }
122 
123  if (!half_edge_mesh.addFace (vi).isValid ())
124  {
125  ++count_not_added;
126  }
127  }
128 
129  return (count_not_added);
130  }
131  } // End namespace geometry
132 } // End namespace pcl
133 
134 #endif // PCL_GEOMETRY_MESH_CONVERSION_H