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