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