example_half_edge_mesh.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Point Cloud Library (PCL) - www.pointclouds.org
00005  * Copyright (c) 2009-2012, Willow Garage, Inc.
00006  * Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  *
00014  *  * Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  *  * Redistributions in binary form must reproduce the above
00017  *    copyright notice, this list of conditions and the following
00018  *    disclaimer in the documentation and/or other materials provided
00019  *    with the distribution.
00020  *  * Neither the name of the copyright holder(s) nor the names of its
00021  *    contributors may be used to endorse or promote products derived
00022  *    from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  * POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  * $Id$
00038  *
00039  */
00040 
00041 #include <iostream>
00042 
00043 #include <pcl/geometry/polygon_mesh.h>
00044 
00046 
00047 // User data for the vertices. Here I just store an Id. In a 3D application this would be, for example
00048 //  - x, y, z
00049 //  - nx, ny, nz
00050 //  - r, g, b
00051 // ...
00052 class MyVertexData
00053 {
00054   public:
00055 
00056     MyVertexData (const int id = -1) : id_ (id) {}
00057 
00058     int  id () const {return (id_);}
00059     int& id ()       {return (id_);}
00060 
00061   private:
00062 
00063     int id_;
00064 };
00065 
00066 std::ostream&
00067 operator << (std::ostream& os, const MyVertexData& vd)
00068 {
00069   return (os << vd.id ());
00070 }
00071 
00073 
00074 // Declare the mesh.
00075 typedef pcl::geometry::PolygonMesh <pcl::geometry::DefaultMeshTraits <MyVertexData> > Mesh;
00076 
00077 typedef Mesh::VertexIndex   VertexIndex;
00078 typedef Mesh::HalfEdgeIndex HalfEdgeIndex;
00079 typedef Mesh::FaceIndex     FaceIndex;
00080 
00081 typedef Mesh::VertexIndices   VertexIndices;
00082 typedef Mesh::HalfEdgeIndices HalfEdgeIndices;
00083 typedef Mesh::FaceIndices     FaceIndices;
00084 
00085 typedef Mesh::VertexAroundVertexCirculator           VAVC;
00086 typedef Mesh::OutgoingHalfEdgeAroundVertexCirculator OHEAVC;
00087 typedef Mesh::IncomingHalfEdgeAroundVertexCirculator IHEAVC;
00088 typedef Mesh::FaceAroundVertexCirculator             FAVC;
00089 typedef Mesh::VertexAroundFaceCirculator             VAFC;
00090 typedef Mesh::InnerHalfEdgeAroundFaceCirculator      IHEAFC;
00091 typedef Mesh::OuterHalfEdgeAroundFaceCirculator      OHEAFC;
00092 
00094 
00095 // Some output functions
00096 void
00097 printVertices (const Mesh& mesh)
00098 {
00099   std::cout << "Vertices:\n   ";
00100   for (unsigned int i=0; i<mesh.sizeVertices (); ++i)
00101   {
00102     std::cout << mesh.getVertexDataCloud () [i] << " ";
00103   }
00104   std::cout << std::endl;
00105 }
00106 
00107 void
00108 printEdge (const Mesh& mesh, const HalfEdgeIndex& idx_he)
00109 {
00110   std::cout << "  "
00111             << mesh.getVertexDataCloud () [mesh.getOriginatingVertexIndex (idx_he).get ()]
00112             << " "
00113             << mesh.getVertexDataCloud () [mesh.getTerminatingVertexIndex (idx_he).get ()]
00114             << std::endl;
00115 }
00116 
00117 void
00118 printFace (const Mesh& mesh, const FaceIndex& idx_face)
00119 {
00120   // Circulate around all vertices in the face
00121   VAFC       circ     = mesh.getVertexAroundFaceCirculator (idx_face);
00122   const VAFC circ_end = circ;
00123   std::cout << "  ";
00124   do
00125   {
00126     std::cout << mesh.getVertexDataCloud () [circ.getTargetIndex ().get ()] << " ";
00127   } while (++circ != circ_end);
00128   std::cout << std::endl;
00129 }
00130 
00131 void
00132 printFaces (const Mesh& mesh)
00133 {
00134   std::cout << "Faces:\n";
00135   for (unsigned int i=0; i<mesh.sizeFaces (); ++i)
00136   {
00137     printFace (mesh, FaceIndex (i));
00138   }
00139 }
00140 
00142 
00143 int main ()
00144 {
00145   Mesh          mesh;
00146   VertexIndices vi;
00147 
00148   // Create a closed circle around vertex 0 //
00149   //   2 - 1                                //
00150   //  / \ / \     7 <- Isolated vertex      //
00151   // 3 - 0   6                              //
00152   //  \ / \ /                               //
00153   //   4 - 5                                //
00154   for (unsigned int i=0; i<8; ++i)
00155   {
00156     vi.push_back (mesh.addVertex (MyVertexData (i)));
00157   }
00158 
00159   // General method to add faces.
00160   VertexIndices tmp;
00161   tmp.push_back (vi [0]);
00162   tmp.push_back (vi [1]);
00163   tmp.push_back (vi [2]);
00164   mesh.addFace (tmp);
00165   tmp.clear ();
00166 
00167   // Convenience method: Works only for triangles
00168   mesh.addFace (vi [0], vi [2], vi [3]);
00169   mesh.addFace (vi [0], vi [3], vi [4]);
00170   mesh.addFace (vi [0], vi [4], vi [5]);
00171 
00172   // Convenience method: Works only for quads
00173   mesh.addFace (vi [0], vi [5], vi [6], vi [1]);
00174 
00175   printVertices (mesh);
00176   printFaces (mesh);
00177 
00179 
00180   std::cout << "Outgoing half-edges of vertex 0:" << std::endl;
00181   OHEAVC       circ_oheav     = mesh.getOutgoingHalfEdgeAroundVertexCirculator (vi[0]);
00182   const OHEAVC circ_oheav_end = circ_oheav;
00183   do
00184   {
00185     printEdge (mesh,circ_oheav.getTargetIndex ());
00186   } while (++circ_oheav != circ_oheav_end);
00187 
00189 
00190   std::cout << "Circulate around the boundary half-edges:" << std::endl;
00191   const HalfEdgeIndex& idx_he_boundary = mesh.getOutgoingHalfEdgeIndex (vi [6]);
00192   IHEAFC       circ_iheaf     = mesh.getInnerHalfEdgeAroundFaceCirculator (idx_he_boundary);
00193   const IHEAFC circ_iheaf_end = circ_iheaf;
00194   do
00195   {
00196     printEdge (mesh, circ_iheaf.getTargetIndex ());
00197   } while (++circ_iheaf != circ_iheaf_end);
00198 
00200 
00201   std::cout << std::endl << "Deleting face 1 (0 2 3) and 3 (0 4 5) ...\n";
00202   std::cout << "(If the mesh is set to manifold further faces are removed automatically)\n\n";
00203   mesh.deleteFace (FaceIndex (1));
00204   mesh.deleteFace (FaceIndex (3));
00205 
00206   mesh.cleanUp (); // Removes the isolated vertex (7) as well!
00207   vi.clear ();     // The vertex indices are no longer synchronized with the mesh!
00208 
00209   printVertices (mesh);
00210   printFaces    (mesh);
00211 
00213 
00214   std::cout << "Circulate around all faces of vertex 0:\n";
00215 
00216   FAVC       circ_fav     = mesh.getFaceAroundVertexCirculator (vi [0]);
00217   const FAVC circ_fav_end = circ_fav;
00218   do
00219   {
00220     // Very important: Some half_edges are on the boundary
00221     //  -> have an invalid face index
00222     if (!mesh.isBoundary (circ_fav.getCurrentHalfEdgeIndex ()))
00223     {
00224       printFace (mesh, circ_fav.getTargetIndex ());
00225     }
00226     else
00227     {
00228       std::cout << "  invalid face -> boundary half-edge\n";
00229     }
00230   } while (++circ_fav!=circ_fav_end);
00231 
00232   return (0);
00233 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:36