Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
delete_geometry.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43  * *
44  * $Revision: 736 $ *
45  * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08. Okt 2012) $ *
46  * *
47 \*===========================================================================*/
48 
49 
50 #include <iostream>
51 // -------------------- OpenMesh
52 #include <OpenMesh/Core/IO/MeshIO.hh>
53 #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
55 #include <OpenMesh/Core/Mesh/Status.hh>
56 
57 // ----------------------------------------------------------------------------
58 
59 struct MyTraits : public OpenMesh::DefaultTraits
60 {
61 };
62 
63 
65 
66 
67 // ----------------------------------------------------------------------------
68 // Build a simple cube and delete it except one face
69 
70 int main()
71 {
72  MyMesh mesh;
73 
74  // the request has to be called before a vertex/face/edge can be deleted. it grants access to the status attribute
75  mesh.request_face_status();
76  mesh.request_edge_status();
77  mesh.request_vertex_status();
78 
79  // generate vertices
80 
81  MyMesh::VertexHandle vhandle[8];
82  MyMesh::FaceHandle fhandle[6];
83 
84  vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
85  vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1));
86  vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1));
87  vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
88  vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
89  vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
90  vhandle[6] = mesh.add_vertex(MyMesh::Point( 1, 1, -1));
91  vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));
92 
93 
94  // generate (quadrilateral) faces
95 
96  std::vector<MyMesh::VertexHandle> tmp_face_vhandles;
97 
98  tmp_face_vhandles.clear();
99  tmp_face_vhandles.push_back(vhandle[0]);
100  tmp_face_vhandles.push_back(vhandle[1]);
101  tmp_face_vhandles.push_back(vhandle[2]);
102  tmp_face_vhandles.push_back(vhandle[3]);
103  fhandle[0] = mesh.add_face(tmp_face_vhandles);
104 
105  tmp_face_vhandles.clear();
106  tmp_face_vhandles.push_back(vhandle[7]);
107  tmp_face_vhandles.push_back(vhandle[6]);
108  tmp_face_vhandles.push_back(vhandle[5]);
109  tmp_face_vhandles.push_back(vhandle[4]);
110  fhandle[1] = mesh.add_face(tmp_face_vhandles);
111 
112  tmp_face_vhandles.clear();
113  tmp_face_vhandles.push_back(vhandle[1]);
114  tmp_face_vhandles.push_back(vhandle[0]);
115  tmp_face_vhandles.push_back(vhandle[4]);
116  tmp_face_vhandles.push_back(vhandle[5]);
117  fhandle[2] = mesh.add_face(tmp_face_vhandles);
118 
119 
120  tmp_face_vhandles.clear();
121  tmp_face_vhandles.push_back(vhandle[2]);
122  tmp_face_vhandles.push_back(vhandle[1]);
123  tmp_face_vhandles.push_back(vhandle[5]);
124  tmp_face_vhandles.push_back(vhandle[6]);
125  fhandle[3] = mesh.add_face(tmp_face_vhandles);
126 
127 
128  tmp_face_vhandles.clear();
129  tmp_face_vhandles.push_back(vhandle[3]);
130  tmp_face_vhandles.push_back(vhandle[2]);
131  tmp_face_vhandles.push_back(vhandle[6]);
132  tmp_face_vhandles.push_back(vhandle[7]);
133  fhandle[4] = mesh.add_face(tmp_face_vhandles);
134 
135 
136  tmp_face_vhandles.clear();
137  tmp_face_vhandles.push_back(vhandle[0]);
138  tmp_face_vhandles.push_back(vhandle[3]);
139  tmp_face_vhandles.push_back(vhandle[7]);
140  tmp_face_vhandles.push_back(vhandle[4]);
141  fhandle[5] = mesh.add_face(tmp_face_vhandles);
142 
143  // And now delete all faces and vertices
144  // except face (vh[7], vh[6], vh[5], vh[4])
145  // whose handle resides in fhandle[1]
146 
147 
148  // Delete face 0
149  mesh.delete_face(fhandle[0], false);
150  // ... face 2
151  mesh.delete_face(fhandle[2], false);
152  // ... face 3
153  mesh.delete_face(fhandle[3], false);
154  // ... face 4
155  mesh.delete_face(fhandle[4], false);
156  // ... face 5
157  mesh.delete_face(fhandle[5], false);
158 
159 
160  // If isolated vertices result in a face deletion
161  // they have to be deleted manually. If you want this
162  // to happen automatically, change the second parameter
163  // to true.
164 
165  // Now delete the isolated vertices 0, 1, 2 and 3
166  mesh.delete_vertex(vhandle[0], false);
167  mesh.delete_vertex(vhandle[1], false);
168  mesh.delete_vertex(vhandle[2], false);
169  mesh.delete_vertex(vhandle[3], false);
170 
171  // Delete all elements that are marked as deleted
172  // from memory.
173  mesh.garbage_collection();
174 
175  // write mesh to output.obj
176  try {
177  if ( !OpenMesh::IO::write_mesh(mesh, "output.off") ) {
178  std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
179  return 1;
180  }
181  }
182  catch( std::exception& x )
183  {
184  std::cerr << x.what() << std::endl;
185  return 1;
186  }
187 
188  return 0;
189 }
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
VertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:236
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition: MeshIO.hh:199