Point Cloud Library (PCL)  1.12.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 namespace geometry {
48 /** \brief Convert a half-edge mesh to a face-vertex mesh.
49  * \param[in] half_edge_mesh The input mesh.
50  * \param[out] face_vertex_mesh The output mesh.
51  * \author Martin Saelzle
52  * \ingroup geometry
53  */
54 template <class HalfEdgeMeshT>
55 void
56 toFaceVertexMesh(const HalfEdgeMeshT& half_edge_mesh,
57  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  VAFC circ = half_edge_mesh.getVertexAroundFaceCirculator(FaceIndex(i));
69  const VAFC circ_end = circ;
70  polygon.vertices.clear();
71  do {
72  polygon.vertices.push_back(circ.getTargetIndex().get());
73  } while (++circ != circ_end);
74  face_vertex_mesh.polygons.push_back(polygon);
75  }
76 }
77 
78 /** \brief Convert a face-vertex mesh to a half-edge mesh.
79  * \param[in] face_vertex_mesh The input mesh.
80  * \param[out] half_edge_mesh The output mesh. It must have data associated with the
81  * vertices.
82  * \return The number of faces that could NOT be added to the half-edge mesh.
83  * \author Martin Saelzle
84  * \ingroup geometry
85  */
86 template <class HalfEdgeMeshT>
87 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,
95  "Output mesh must have data associated with the vertices!");
96 
97  VertexDataCloud vertices;
98  pcl::fromPCLPointCloud2(face_vertex_mesh.cloud, vertices);
99 
100  half_edge_mesh.reserveVertices(vertices.size());
101  half_edge_mesh.reserveEdges(3 * face_vertex_mesh.polygons.size());
102  half_edge_mesh.reserveFaces(face_vertex_mesh.polygons.size());
103 
104  for (const auto& vertex : vertices) {
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  vi.clear();
115  for (const auto& vertex : polygon.vertices) {
116  vi.emplace_back(vertex);
117  }
118 
119  if (!half_edge_mesh.addFace(vi).isValid()) {
120  ++count_not_added;
121  }
122  }
123 
124  return (count_not_added);
125 }
126 } // End namespace geometry
127 } // End namespace pcl
void toFaceVertexMesh(const HalfEdgeMeshT &half_edge_mesh, pcl::PolygonMesh &face_vertex_mesh)
Convert a half-edge mesh to a face-vertex mesh.
int toHalfEdgeMesh(const pcl::PolygonMesh &face_vertex_mesh, HalfEdgeMeshT &half_edge_mesh)
Convert a face-vertex mesh to a half-edge mesh.
pcl::detail::MeshIndex< struct FaceIndexTag > FaceIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:211
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
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
std::vector< ::pcl::Vertices > polygons
Definition: PolygonMesh.h:22
::pcl::PCLPointCloud2 cloud
Definition: PolygonMesh.h:20
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
Definition: Vertices.h:15
Indices vertices
Definition: Vertices.h:18