Point Cloud Library (PCL)  1.11.1-dev
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 #pragma once
42 
43 #include <pcl/PolygonMesh.h>
44 #include <pcl/conversions.h>
45 
46 namespace pcl
47 {
48  namespace geometry
49  {
50  /** \brief Convert a half-edge mesh to a face-vertex mesh.
51  * \param[in] half_edge_mesh The input mesh.
52  * \param[out] face_vertex_mesh The output mesh.
53  * \author Martin Saelzle
54  * \ingroup geometry
55  */
56  template <class HalfEdgeMeshT> void
57  toFaceVertexMesh (const HalfEdgeMeshT& half_edge_mesh, pcl::PolygonMesh& face_vertex_mesh)
58  {
59  using HalfEdgeMesh = HalfEdgeMeshT;
60  using VAFC = typename HalfEdgeMesh::VertexAroundFaceCirculator;
61  using FaceIndex = typename HalfEdgeMesh::FaceIndex;
62 
63  pcl::Vertices polygon;
64  pcl::toPCLPointCloud2 (half_edge_mesh.getVertexDataCloud (), face_vertex_mesh.cloud);
65 
66  face_vertex_mesh.polygons.reserve (half_edge_mesh.sizeFaces ());
67  for (std::size_t i=0; i<half_edge_mesh.sizeFaces (); ++i)
68  {
69  VAFC circ = half_edge_mesh.getVertexAroundFaceCirculator (FaceIndex (i));
70  const VAFC circ_end = circ;
71  polygon.vertices.clear ();
72  do
73  {
74  polygon.vertices.push_back (circ.getTargetIndex ().get ());
75  } while (++circ != circ_end);
76  face_vertex_mesh.polygons.push_back (polygon);
77  }
78  }
79 
80  /** \brief Convert a face-vertex mesh to a half-edge mesh.
81  * \param[in] face_vertex_mesh The input mesh.
82  * \param[out] half_edge_mesh The output mesh. It must have data associated with the vertices.
83  * \return The number of faces that could NOT be added to the half-edge mesh.
84  * \author Martin Saelzle
85  * \ingroup geometry
86  */
87  template <class HalfEdgeMeshT> int
88  toHalfEdgeMesh (const pcl::PolygonMesh& face_vertex_mesh, HalfEdgeMeshT& half_edge_mesh)
89  {
90  using HalfEdgeMesh = HalfEdgeMeshT;
91  using VertexDataCloud = typename HalfEdgeMesh::VertexDataCloud;
92  using VertexIndices = typename HalfEdgeMesh::VertexIndices;
93 
94  static_assert (HalfEdgeMesh::HasVertexData::value, "Output mesh must have data associated with the vertices!");
95 
96  VertexDataCloud vertices;
97  pcl::fromPCLPointCloud2 (face_vertex_mesh.cloud, vertices);
98 
99  half_edge_mesh.reserveVertices (vertices.size ());
100  half_edge_mesh.reserveEdges (3 * face_vertex_mesh.polygons.size ());
101  half_edge_mesh.reserveFaces ( face_vertex_mesh.polygons.size ());
102 
103  for (const auto &vertex : vertices)
104  {
105  half_edge_mesh.addVertex (vertex);
106  }
107 
108  assert (half_edge_mesh.sizeVertices () == vertices.size ());
109 
110  int count_not_added = 0;
111  VertexIndices vi;
112  vi.reserve (3); // Minimum number (triangle)
113  for (const auto &polygon : face_vertex_mesh.polygons)
114  {
115  vi.clear ();
116  for (const auto &vertex : polygon.vertices)
117  {
118  vi.emplace_back (vertex);
119  }
120 
121  if (!half_edge_mesh.addFace (vi).isValid ())
122  {
123  ++count_not_added;
124  }
125  }
126 
127  return (count_not_added);
128  }
129  } // End namespace geometry
130 } // End namespace pcl
pcl::PolygonMesh::cloud
::pcl::PCLPointCloud2 cloud
Definition: PolygonMesh.h:21
pcl
Definition: convolution.h:46
pcl::geometry::toFaceVertexMesh
void toFaceVertexMesh(const HalfEdgeMeshT &half_edge_mesh, pcl::PolygonMesh &face_vertex_mesh)
Convert a half-edge mesh to a face-vertex mesh.
Definition: mesh_conversion.h:57
pcl::Vertices::vertices
Indices vertices
Definition: Vertices.h:19
pcl::PolygonMesh
Definition: PolygonMesh.h:14
pcl::geometry::FaceIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:478
pcl::Vertices
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
Definition: Vertices.h:14
pcl::PolygonMesh::polygons
std::vector< ::pcl::Vertices > polygons
Definition: PolygonMesh.h:23
pcl::toPCLPointCloud2
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:240
pcl::geometry::toHalfEdgeMesh
int toHalfEdgeMesh(const pcl::PolygonMesh &face_vertex_mesh, HalfEdgeMeshT &half_edge_mesh)
Convert a face-vertex mesh to a half-edge mesh.
Definition: mesh_conversion.h:88
pcl::fromPCLPointCloud2
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
Definition: conversions.h:167