mesh_conversion.h
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2009-2012, Willow Garage, Inc.
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#pragma once
#include <pcl/PolygonMesh.h>
#include <pcl/conversions.h>
namespace pcl
{
namespace geometry
{
/** \brief Convert a half-edge mesh to a face-vertex mesh.
* \param[in] half_edge_mesh The input mesh.
* \param[out] face_vertex_mesh The output mesh.
* \author Martin Saelzle
* \ingroup geometry
*/
template <class HalfEdgeMeshT> void
toFaceVertexMesh (const HalfEdgeMeshT& half_edge_mesh, pcl::PolygonMesh& face_vertex_mesh)
{
using HalfEdgeMesh = HalfEdgeMeshT;
using VAFC = typename HalfEdgeMesh::VertexAroundFaceCirculator;
using FaceIndex = typename HalfEdgeMesh::FaceIndex;
pcl::Vertices polygon;
pcl::toPCLPointCloud2 (half_edge_mesh.getVertexDataCloud (), face_vertex_mesh.cloud);
face_vertex_mesh.polygons.reserve (half_edge_mesh.sizeFaces ());
for (std::size_t i=0; i<half_edge_mesh.sizeFaces (); ++i)
{
VAFC circ = half_edge_mesh.getVertexAroundFaceCirculator (FaceIndex (i));
const VAFC circ_end = circ;
polygon.vertices.clear ();
do
{
polygon.vertices.push_back (circ.getTargetIndex ().get ());
} while (++circ != circ_end);
face_vertex_mesh.polygons.push_back (polygon);
}
}
/** \brief Convert a face-vertex mesh to a half-edge mesh.
* \param[in] face_vertex_mesh The input mesh.
* \param[out] half_edge_mesh The output mesh. It must have data associated with the vertices.
* \return The number of faces that could NOT be added to the half-edge mesh.
* \author Martin Saelzle
* \ingroup geometry
*/
template <class HalfEdgeMeshT> int
toHalfEdgeMesh (const pcl::PolygonMesh& face_vertex_mesh, HalfEdgeMeshT& half_edge_mesh)
{
using HalfEdgeMesh = HalfEdgeMeshT;
using VertexDataCloud = typename HalfEdgeMesh::VertexDataCloud;
using VertexIndices = typename HalfEdgeMesh::VertexIndices;
static_assert (HalfEdgeMesh::HasVertexData::value, "Output mesh must have data associated with the vertices!");
VertexDataCloud vertices;
pcl::fromPCLPointCloud2 (face_vertex_mesh.cloud, vertices);
half_edge_mesh.reserveVertices (vertices.size ());
half_edge_mesh.reserveEdges (3 * face_vertex_mesh.polygons.size ());
half_edge_mesh.reserveFaces ( face_vertex_mesh.polygons.size ());
for (const auto &vertex : vertices)
{
half_edge_mesh.addVertex (vertex);
}
assert (half_edge_mesh.sizeVertices () == vertices.size ());
int count_not_added = 0;
VertexIndices vi;
vi.reserve (3); // Minimum number (triangle)
for (const auto &polygon : face_vertex_mesh.polygons)
{
vi.clear ();
for (const unsigned int &vertex : polygon.vertices)
{
vi.emplace_back (vertex);
}
if (!half_edge_mesh.addFace (vi).isValid ())
{
++count_not_added;
}
}
return (count_not_added);
}
} // End namespace geometry
} // End namespace pcl
pcl::PolygonMesh::cloud
::pcl::PCLPointCloud2 cloud
Definition: PolygonMesh.h:22
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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
std::vector< std::uint32_t > vertices
Definition: Vertices.h:19
pcl::PolygonMesh
Definition: PolygonMesh.h:15
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:24
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:241
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:168