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: 1258 $ *
45  * $Date: 2015-04-28 15:07:46 +0200 (Di, 28 Apr 2015) $ *
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  VertexAttributes(OpenMesh::Attributes::Status);
62  FaceAttributes(OpenMesh::Attributes::Status);
63  EdgeAttributes(OpenMesh::Attributes::Status);
64 };
65 
66 
68 
69 
70 // ----------------------------------------------------------------------------
71 // Build a simple cube and delete it except one face
72 
73 int main()
74 {
75  MyMesh mesh;
76 
77  // generate vertices
78 
79  MyMesh::VertexHandle vhandle[8];
80  MyMesh::FaceHandle fhandle[6];
81 
82  vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
83  vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1));
84  vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1));
85  vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
86  vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
87  vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
88  vhandle[6] = mesh.add_vertex(MyMesh::Point( 1, 1, -1));
89  vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));
90 
91 
92  // generate (quadrilateral) faces
93 
94  std::vector<MyMesh::VertexHandle> tmp_face_vhandles;
95 
96  tmp_face_vhandles.clear();
97  tmp_face_vhandles.push_back(vhandle[0]);
98  tmp_face_vhandles.push_back(vhandle[1]);
99  tmp_face_vhandles.push_back(vhandle[2]);
100  tmp_face_vhandles.push_back(vhandle[3]);
101  fhandle[0] = mesh.add_face(tmp_face_vhandles);
102 
103  tmp_face_vhandles.clear();
104  tmp_face_vhandles.push_back(vhandle[7]);
105  tmp_face_vhandles.push_back(vhandle[6]);
106  tmp_face_vhandles.push_back(vhandle[5]);
107  tmp_face_vhandles.push_back(vhandle[4]);
108  fhandle[1] = mesh.add_face(tmp_face_vhandles);
109 
110  tmp_face_vhandles.clear();
111  tmp_face_vhandles.push_back(vhandle[1]);
112  tmp_face_vhandles.push_back(vhandle[0]);
113  tmp_face_vhandles.push_back(vhandle[4]);
114  tmp_face_vhandles.push_back(vhandle[5]);
115  fhandle[2] = mesh.add_face(tmp_face_vhandles);
116 
117 
118  tmp_face_vhandles.clear();
119  tmp_face_vhandles.push_back(vhandle[2]);
120  tmp_face_vhandles.push_back(vhandle[1]);
121  tmp_face_vhandles.push_back(vhandle[5]);
122  tmp_face_vhandles.push_back(vhandle[6]);
123  fhandle[3] = mesh.add_face(tmp_face_vhandles);
124 
125 
126  tmp_face_vhandles.clear();
127  tmp_face_vhandles.push_back(vhandle[3]);
128  tmp_face_vhandles.push_back(vhandle[2]);
129  tmp_face_vhandles.push_back(vhandle[6]);
130  tmp_face_vhandles.push_back(vhandle[7]);
131  fhandle[4] = mesh.add_face(tmp_face_vhandles);
132 
133 
134  tmp_face_vhandles.clear();
135  tmp_face_vhandles.push_back(vhandle[0]);
136  tmp_face_vhandles.push_back(vhandle[3]);
137  tmp_face_vhandles.push_back(vhandle[7]);
138  tmp_face_vhandles.push_back(vhandle[4]);
139  fhandle[5] = mesh.add_face(tmp_face_vhandles);
140 
141  // And now delete all faces and vertices
142  // except face (vh[7], vh[6], vh[5], vh[4])
143  // whose handle resides in fhandle[1]
144 
145 
146  // Delete face 0
147  mesh.delete_face(fhandle[0], false);
148  // ... face 2
149  mesh.delete_face(fhandle[2], false);
150  // ... face 3
151  mesh.delete_face(fhandle[3], false);
152  // ... face 4
153  mesh.delete_face(fhandle[4], false);
154  // ... face 5
155  mesh.delete_face(fhandle[5], false);
156 
157 
158  // If isolated vertices result in a face deletion
159  // they have to be deleted manually. If you want this
160  // to happen automatically, change the second parameter
161  // to true.
162 
163  // Now delete the isolated vertices 0, 1, 2 and 3
164  mesh.delete_vertex(vhandle[0], false);
165  mesh.delete_vertex(vhandle[1], false);
166  mesh.delete_vertex(vhandle[2], false);
167  mesh.delete_vertex(vhandle[3], false);
168 
169  // Delete all elements that are marked as deleted
170  // from memory.
171  mesh.garbage_collection();
172 
173  // write mesh to output.obj
174  try {
175  if ( !OpenMesh::IO::write_mesh(mesh, "output.off") ) {
176  std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
177  return 1;
178  }
179  }
180  catch( std::exception& x )
181  {
182  std::cerr << x.what() << std::endl;
183  return 1;
184  }
185 
186  return 0;
187 }
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
Add status to mesh item (all items)
Definition: Attributes.hh:90