Point Cloud Library (PCL)  1.10.0-dev
triangle_mesh.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 <utility>
44 
45 #include <pcl/pcl_macros.h>
46 #include <pcl/geometry/mesh_base.h>
47 
48 namespace pcl
49 {
50  namespace geometry
51  {
52  /** \brief Tag describing the type of the mesh. */
53  struct TriangleMeshTag {};
54 
55  /** \brief Half-edge mesh that can only store triangles.
56  * \tparam MeshTraitsT Please have a look at pcl::geometry::DefaultMeshTraits.
57  * \author Martin Saelzle
58  * \ingroup geometry
59  */
60  template <class MeshTraitsT>
61  class TriangleMesh : public pcl::geometry::MeshBase <TriangleMesh <MeshTraitsT>, MeshTraitsT, TriangleMeshTag>
62  {
63  public:
64 
66 
70 
71  using VertexData = typename Base::VertexData;
72  using HalfEdgeData = typename Base::HalfEdgeData;
73  using EdgeData = typename Base::EdgeData;
74  using FaceData = typename Base::FaceData;
75  using IsManifold = typename Base::IsManifold;
76  using MeshTag = typename Base::MeshTag;
77 
80  using HasEdgeData = typename Base::HasEdgeData;
81  using HasFaceData = typename Base::HasFaceData;
82 
87 
88  // Indices
89  using VertexIndex = typename Base::VertexIndex;
91  using EdgeIndex = typename Base::EdgeIndex;
92  using FaceIndex = typename Base::FaceIndex;
93  using FaceIndexPair = std::pair <FaceIndex, FaceIndex>;
94 
97  using EdgeIndices = typename Base::EdgeIndices;
98  using FaceIndices = typename Base::FaceIndices;
99 
100  // Circulators
109 
110  /** \brief Constructor. */
112  : Base (),
113  add_triangle_ (3),
114  inner_he_atp_ (4),
115  is_new_atp_ (4)
116  {
117  }
118 
119  /** \brief The base method of addFace is hidden because of the overloads in this class. */
120  using Base::addFace;
121 
122  /** \brief Add a triangle to the mesh. Data is only added if it is associated with the elements. The last vertex is connected with the first one.
123  * \param[in] idx_v_0 Index to the first vertex.
124  * \param[in] idx_v_1 Index to the second vertex.
125  * \param[in] idx_v_2 Index to the third vertex.
126  * \param[in] face_data Data that is set for the face.
127  * \param[in] half_edge_data Data that is set for all added half-edges.
128  * \param[in] edge_data Data that is set for all added edges.
129  * \return Index to the new face. Failure is signaled by returning an invalid face index.
130  * \warning The vertices must be valid and unique (each vertex may be contained only once). Not complying with this requirement results in undefined behavior!
131  */
132  inline FaceIndex
133  addFace (const VertexIndex& idx_v_0,
134  const VertexIndex& idx_v_1,
135  const VertexIndex& idx_v_2,
136  const FaceData& face_data = FaceData (),
137  const EdgeData& edge_data = EdgeData (),
138  const HalfEdgeData& half_edge_data = HalfEdgeData ())
139  {
140  add_triangle_ [0] = idx_v_0;
141  add_triangle_ [1] = idx_v_1;
142  add_triangle_ [2] = idx_v_2;
143 
144  return (this->addFaceImplBase (add_triangle_, face_data, edge_data, half_edge_data));
145  }
146 
147  /** \brief Add two triangles for the four given input vertices. When using a manifold triangle mesh it is not possible to connect two bounded regions without going through a non-manifold intermediate step. This method first tries to add the triangles individually and if this fails connects the whole configuration at once (if possible).
148  * \param[in] vertices Indices to the vertices of the new face. (The size must be equal to four).
149  * \param[in] face_data Data that is set for the face.
150  * \param[in] half_edge_data Data that is set for all added half-edges.
151  * \param[in] edge_data Data that is set for all added edges.
152  * \return Pair of face indices. The first index is valid if one triangle was added. Both indices are valid if two triangles were added.
153  * \warning The vertices must be valid and unique (each vertex may be contained only once). Not complying with this requirement results in undefined behavior!
154  */
156  addTrianglePair (const VertexIndices& vertices,
157  const FaceData& face_data = FaceData (),
158  const EdgeData& edge_data = EdgeData (),
159  const HalfEdgeData& half_edge_data = HalfEdgeData ())
160  {
161  if (vertices.size () != 4)
162  {
163  return (std::make_pair (FaceIndex (), FaceIndex ()));
164  }
165  return (this->addTrianglePair (vertices [0], vertices [1], vertices [2], vertices [3], face_data, edge_data, half_edge_data));
166  }
167 
168  /** \brief Add two triangles for the four given input vertices. When using a manifold triangle mesh it is not possible to connect two bounded regions without going through a non-manifold intermediate step. This method first tries to add the triangles individually and if this fails connects the whole configuration at once (if possible).
169  * \param[in] idx_v_0 Index to the first vertex.
170  * \param[in] idx_v_1 Index to the second vertex.
171  * \param[in] idx_v_2 Index to the third vertex.
172  * \param[in] idx_v_3 Index to the fourth vertex.
173  * \param[in] face_data Data that is set for the face.
174  * \param[in] half_edge_data Data that is set for all added half-edges.
175  * \param[in] edge_data Data that is set for all added edges.
176  * \return Pair of face indices. The first index is valid if one triangle was added. Both indices are valid if two triangles were added.
177  * \warning The vertices must be valid and unique (each vertex may be contained only once). Not complying with this requirement results in undefined behavior!
178  */
179  inline FaceIndexPair
180  addTrianglePair (const VertexIndex& idx_v_0,
181  const VertexIndex& idx_v_1,
182  const VertexIndex& idx_v_2,
183  const VertexIndex& idx_v_3,
184  const FaceData& face_data = FaceData (),
185  const EdgeData& edge_data = EdgeData (),
186  const HalfEdgeData& half_edge_data = HalfEdgeData ())
187  {
188  // Try to add two faces
189  // 3 - 2
190  // | / |
191  // 0 - 1
192  FaceIndex idx_face_0 = this->addFace (idx_v_0, idx_v_1, idx_v_2, face_data);
193  FaceIndex idx_face_1 = this->addFace (idx_v_0, idx_v_2, idx_v_3, face_data);
194 
195  if (idx_face_0.isValid ())
196  {
197  return (std::make_pair (idx_face_0, idx_face_1));
198  }
199  if (idx_face_1.isValid ())
200  {
201  idx_face_0 = this->addFace (idx_v_0, idx_v_1, idx_v_2, face_data); // might be possible to add now
202  return (std::make_pair (idx_face_1, idx_face_0));
203  }
204 
205  // Try to add two faces
206  // 3 - 2
207  // | \ |
208  // 0 - 1
209  idx_face_0 = this->addFace (idx_v_1, idx_v_2, idx_v_3, face_data);
210  idx_face_1 = this->addFace (idx_v_0, idx_v_1, idx_v_3, face_data);
211 
212  if (idx_face_0.isValid ())
213  {
214  return (std::make_pair (idx_face_0, idx_face_1));
215  }
216  if (idx_face_1.isValid ())
217  {
218  idx_face_0 = this->addFace (idx_v_1, idx_v_2, idx_v_3, face_data); // might be possible to add now
219  return (std::make_pair (idx_face_1, idx_face_0));
220  }
221 
222  if (!IsManifold::value)
223  {
224  return (std::make_pair (FaceIndex (), FaceIndex ()));
225  }
226 
227  // Check manifoldness
228  if (!Base::checkTopology1 (idx_v_0,idx_v_1, inner_he_atp_ [0], is_new_atp_ [0], IsManifold ()) ||
229  !Base::checkTopology1 (idx_v_1,idx_v_2, inner_he_atp_ [1], is_new_atp_ [1], IsManifold ()) ||
230  !Base::checkTopology1 (idx_v_2,idx_v_3, inner_he_atp_ [2], is_new_atp_ [2], IsManifold ()) ||
231  !Base::checkTopology1 (idx_v_3,idx_v_0, inner_he_atp_ [3], is_new_atp_ [3], IsManifold ()))
232  {
233  return (std::make_pair (FaceIndex (), FaceIndex ()));
234  }
235 
236  // Connect the triangle pair
237  if (!is_new_atp_ [0] && is_new_atp_ [1] && !is_new_atp_ [2] && is_new_atp_ [3])
238  {
239  return (this->connectTrianglePair (inner_he_atp_ [0], inner_he_atp_ [2], idx_v_0, idx_v_1, idx_v_2, idx_v_3, face_data, edge_data, half_edge_data));
240  }
241  if (is_new_atp_ [0] && !is_new_atp_ [1] && is_new_atp_ [2] && !is_new_atp_ [3])
242  {
243  return (this->connectTrianglePair (inner_he_atp_ [1], inner_he_atp_ [3], idx_v_1, idx_v_2, idx_v_3, idx_v_0, face_data, edge_data, half_edge_data));
244  }
245  return (std::make_pair (FaceIndex (), FaceIndex ()));
246  }
247 
248  private:
249 
250  // NOTE: Can't use the typedef of Base as a friend.
251  friend class pcl::geometry::MeshBase <TriangleMesh <MeshTraitsT>, MeshTraitsT, pcl::geometry::TriangleMeshTag>;
252 
253  /** \brief addFace for the triangular mesh. */
254  inline FaceIndex
255  addFaceImpl (const VertexIndices& vertices,
256  const FaceData& face_data,
257  const EdgeData& edge_data,
258  const HalfEdgeData& half_edge_data)
259  {
260  if (vertices.size () == 3)
261  return (this->addFaceImplBase (vertices, face_data, edge_data, half_edge_data));
262  return (FaceIndex ());
263  }
264 
265  /** \brief Connect the triangles a-b-c and a-c-d. The edges a-b and c-d must be old and the edges b-c and d-a must be new. */
266  // d - c
267  // | / |
268  // a - b
270  connectTrianglePair (const HalfEdgeIndex& idx_he_ab,
271  const HalfEdgeIndex& idx_he_cd,
272  const VertexIndex& idx_v_a,
273  const VertexIndex& idx_v_b,
274  const VertexIndex& idx_v_c,
275  const VertexIndex& idx_v_d,
276  const FaceData& face_data,
277  const EdgeData& edge_data,
278  const HalfEdgeData& he_data)
279  {
280  // Add new half-edges
281  const HalfEdgeIndex idx_he_bc = Base::addEdge (idx_v_b, idx_v_c, he_data, edge_data);
282  const HalfEdgeIndex idx_he_da = Base::addEdge (idx_v_d, idx_v_a, he_data, edge_data);
283  const HalfEdgeIndex idx_he_ca = Base::addEdge (idx_v_c, idx_v_a, he_data, edge_data);
284 
285  const HalfEdgeIndex idx_he_cb = Base::getOppositeHalfEdgeIndex (idx_he_bc);
286  const HalfEdgeIndex idx_he_ad = Base::getOppositeHalfEdgeIndex (idx_he_da);
287  const HalfEdgeIndex idx_he_ac = Base::getOppositeHalfEdgeIndex (idx_he_ca);
288 
289  // Get the existing half-edges
290  const HalfEdgeIndex idx_he_ab_prev = Base::getPrevHalfEdgeIndex (idx_he_ab); // No reference!
291  const HalfEdgeIndex idx_he_ab_next = Base::getNextHalfEdgeIndex (idx_he_ab); // No reference!
292 
293  const HalfEdgeIndex idx_he_cd_prev = Base::getPrevHalfEdgeIndex (idx_he_cd); // No reference!
294  const HalfEdgeIndex idx_he_cd_next = Base::getNextHalfEdgeIndex (idx_he_cd); // No reference!
295 
296  // Connect the outer half-edges
297  Base::connectPrevNext (idx_he_ab_prev, idx_he_ad );
298  Base::connectPrevNext (idx_he_ad , idx_he_cd_next);
299  Base::connectPrevNext (idx_he_cd_prev, idx_he_cb );
300  Base::connectPrevNext (idx_he_cb , idx_he_ab_next);
301 
302  // Connect the inner half-edges
303  Base::connectPrevNext (idx_he_ab, idx_he_bc);
304  Base::connectPrevNext (idx_he_bc, idx_he_ca);
305  Base::connectPrevNext (idx_he_ca, idx_he_ab);
306 
307  Base::connectPrevNext (idx_he_ac, idx_he_cd);
308  Base::connectPrevNext (idx_he_cd, idx_he_da);
309  Base::connectPrevNext (idx_he_da, idx_he_ac);
310 
311  // Connect the vertices to the boundary half-edges
312  Base::setOutgoingHalfEdgeIndex (idx_v_a, idx_he_ad );
313  Base::setOutgoingHalfEdgeIndex (idx_v_b, idx_he_ab_next);
314  Base::setOutgoingHalfEdgeIndex (idx_v_c, idx_he_cb );
315  Base::setOutgoingHalfEdgeIndex (idx_v_d, idx_he_cd_next);
316 
317  // Add and connect the faces
318  HalfEdgeIndices inner_he_abc; inner_he_abc.reserve (3);
319  inner_he_abc.push_back (idx_he_ab);
320  inner_he_abc.push_back (idx_he_bc);
321  inner_he_abc.push_back (idx_he_ca);
322 
323  HalfEdgeIndices inner_he_acd; inner_he_acd.reserve (3);
324  inner_he_acd.push_back (idx_he_ac);
325  inner_he_acd.push_back (idx_he_cd);
326  inner_he_acd.push_back (idx_he_da);
327 
328  const FaceIndex idx_f_abc = Base::connectFace (inner_he_abc, face_data);
329  const FaceIndex idx_f_acd = Base::connectFace (inner_he_acd, face_data);
330 
331  return (std::make_pair (idx_f_abc, idx_f_acd));
332  }
333 
334  ////////////////////////////////////////////////////////////////////////
335  // Members
336  ////////////////////////////////////////////////////////////////////////
337 
338  /** \brief Storage for adding a triangle. */
339  VertexIndices add_triangle_;
340 
341  /** \brief Storage for addTrianglePair. */
342  HalfEdgeIndices inner_he_atp_;
343 
344  /** \brief Storage for addTrianglePair. */
345  std::vector <bool> is_new_atp_;
346 
347  public:
349  };
350  } // End namespace geom
351 } // End namespace pcl
FaceIndexPair addTrianglePair(const VertexIndex &idx_v_0, const VertexIndex &idx_v_1, const VertexIndex &idx_v_2, const VertexIndex &idx_v_3, const FaceData &face_data=FaceData(), const EdgeData &edge_data=EdgeData(), const HalfEdgeData &half_edge_data=HalfEdgeData())
Add two triangles for the four given input vertices.
typename Base::IncomingHalfEdgeAroundVertexCirculator IncomingHalfEdgeAroundVertexCirculator
typename Base::HalfEdgeIndex HalfEdgeIndex
Definition: triangle_mesh.h:90
typename Base::InnerHalfEdgeAroundFaceCirculator InnerHalfEdgeAroundFaceCirculator
shared_ptr< const Self > ConstPtr
Definition: triangle_mesh.h:69
typename Base::EdgeDataCloud EdgeDataCloud
Definition: triangle_mesh.h:85
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
typename Base::VertexIndices VertexIndices
Definition: triangle_mesh.h:95
Circulates clockwise around a face and returns an index to the face of the outer half-edge (the targe...
Circulates counter-clockwise around a vertex and returns an index to the incoming half-edge (the targ...
typename Base::EdgeData EdgeData
Definition: triangle_mesh.h:73
typename Base::FaceDataCloud FaceDataCloud
Definition: triangle_mesh.h:86
FaceIndexPair addTrianglePair(const VertexIndices &vertices, const FaceData &face_data=FaceData(), const EdgeData &edge_data=EdgeData(), const HalfEdgeData &half_edge_data=HalfEdgeData())
Add two triangles for the four given input vertices.
typename Base::IsManifold IsManifold
Definition: triangle_mesh.h:75
FaceIndex addFace(const VertexIndex &idx_v_0, const VertexIndex &idx_v_1, const VertexIndex &idx_v_2, const FaceData &face_data=FaceData(), const EdgeData &edge_data=EdgeData(), const HalfEdgeData &half_edge_data=HalfEdgeData())
Add a triangle to the mesh.
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:371
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:339
shared_ptr< Self > Ptr
Definition: triangle_mesh.h:68
typename Base::EdgeIndices EdgeIndices
Definition: triangle_mesh.h:97
typename Base::HasFaceData HasFaceData
Definition: triangle_mesh.h:81
typename Base::HalfEdgeIndices HalfEdgeIndices
Definition: triangle_mesh.h:96
Circulates counter-clockwise around a vertex and returns an index to the terminating vertex of the ou...
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:61
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:478
typename Base::FaceData FaceData
Definition: triangle_mesh.h:74
Circulates clockwise around a face and returns an index to the terminating vertex of the inner half-e...
typename Base::VertexIndex VertexIndex
Definition: triangle_mesh.h:89
std::vector< HalfEdgeIndex > HalfEdgeIndices
Definition: mesh_base.h:139
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:200
typename Base::OuterHalfEdgeAroundFaceCirculator OuterHalfEdgeAroundFaceCirculator
typename Base::VertexAroundFaceCirculator VertexAroundFaceCirculator
typename Base::VertexAroundVertexCirculator VertexAroundVertexCirculator
Circulates counter-clockwise around a vertex and returns an index to the outgoing half-edge (the targ...
typename Base::FaceIndices FaceIndices
Definition: triangle_mesh.h:98
typename Base::MeshTag MeshTag
Definition: triangle_mesh.h:76
std::integral_constant< bool, !std::is_same< FaceData, pcl::geometry::NoData >::value > HasFaceData
Definition: mesh_base.h:125
typename Base::HasEdgeData HasEdgeData
Definition: triangle_mesh.h:80
typename Base::VertexData VertexData
Definition: triangle_mesh.h:71
std::integral_constant< bool, !std::is_same< VertexData, pcl::geometry::NoData >::value > HasVertexData
Definition: mesh_base.h:122
typename Base::HalfEdgeDataCloud HalfEdgeDataCloud
Definition: triangle_mesh.h:84
std::integral_constant< bool, !std::is_same< HalfEdgeData, pcl::geometry::NoData >::value > HasHalfEdgeData
Definition: mesh_base.h:123
std::integral_constant< bool, !std::is_same< EdgeData, pcl::geometry::NoData >::value > HasEdgeData
Definition: mesh_base.h:124
typename Base::FaceIndex FaceIndex
Definition: triangle_mesh.h:92
Circulates clockwise around a face and returns an index to the inner half-edge (the target)...
typename Base::HalfEdgeData HalfEdgeData
Definition: triangle_mesh.h:72
std::pair< FaceIndex, FaceIndex > FaceIndexPair
Definition: triangle_mesh.h:93
typename Base::EdgeIndex EdgeIndex
Definition: triangle_mesh.h:91
Circulates clockwise around a face and returns an index to the outer half-edge (the target)...
Circulates counter-clockwise around a vertex and returns an index to the face of the outgoing half-ed...
Base class for the half-edge mesh.
Definition: mesh_base.h:99
Half-edge mesh that can only store triangles.
Definition: triangle_mesh.h:61
typename Base::FaceAroundFaceCirculator FaceAroundFaceCirculator
typename Base::VertexDataCloud VertexDataCloud
Definition: triangle_mesh.h:83
typename Base::FaceAroundVertexCirculator FaceAroundVertexCirculator
typename Base::HasVertexData HasVertexData
Definition: triangle_mesh.h:78
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
std::vector< VertexIndex > VertexIndices
Definition: mesh_base.h:138
Defines all the PCL and non-PCL macros used.
typename Base::OutgoingHalfEdgeAroundVertexCirculator OutgoingHalfEdgeAroundVertexCirculator
typename Base::HasHalfEdgeData HasHalfEdgeData
Definition: triangle_mesh.h:79
Tag describing the type of the mesh.
Definition: triangle_mesh.h:53