Developer Documentation

build_cube.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>
47
48
49 // ----------------------------------------------------------------------------
50
51 typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
52
53
54 // ----------------------------------------------------------------------------
55 // Build a simple cube and write it to std::cout
56
57 int main()
58 {
59 MyMesh mesh;
60
61 // generate vertices
62
63 MyMesh::VertexHandle vhandle[8];
64
65 vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
66 vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1));
67 vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1));
68 vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
69 vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
70 vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
71 vhandle[6] = mesh.add_vertex(MyMesh::Point( 1, 1, -1));
72 vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));
73
74
75 // generate (quadrilateral) faces
76
77 std::vector<MyMesh::VertexHandle> face_vhandles;
78
79 face_vhandles.clear();
80 face_vhandles.push_back(vhandle[0]);
81 face_vhandles.push_back(vhandle[1]);
82 face_vhandles.push_back(vhandle[2]);
83 face_vhandles.push_back(vhandle[3]);
84 mesh.add_face(face_vhandles);
85
86 face_vhandles.clear();
87 face_vhandles.push_back(vhandle[7]);
88 face_vhandles.push_back(vhandle[6]);
89 face_vhandles.push_back(vhandle[5]);
90 face_vhandles.push_back(vhandle[4]);
91 mesh.add_face(face_vhandles);
92
93 face_vhandles.clear();
94 face_vhandles.push_back(vhandle[1]);
95 face_vhandles.push_back(vhandle[0]);
96 face_vhandles.push_back(vhandle[4]);
97 face_vhandles.push_back(vhandle[5]);
98 mesh.add_face(face_vhandles);
99
100 face_vhandles.clear();
101 face_vhandles.push_back(vhandle[2]);
102 face_vhandles.push_back(vhandle[1]);
103 face_vhandles.push_back(vhandle[5]);
104 face_vhandles.push_back(vhandle[6]);
105 mesh.add_face(face_vhandles);
106
107 face_vhandles.clear();
108 face_vhandles.push_back(vhandle[3]);
109 face_vhandles.push_back(vhandle[2]);
110 face_vhandles.push_back(vhandle[6]);
111 face_vhandles.push_back(vhandle[7]);
112 mesh.add_face(face_vhandles);
113
114 face_vhandles.clear();
115 face_vhandles.push_back(vhandle[0]);
116 face_vhandles.push_back(vhandle[3]);
117 face_vhandles.push_back(vhandle[7]);
118 face_vhandles.push_back(vhandle[4]);
119 mesh.add_face(face_vhandles);
120
121
122 // write mesh to output.obj
123 try
124 {
125 if ( !OpenMesh::IO::write_mesh(mesh, "output.off") )
126 {
127 std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
128 return 1;
129 }
130 }
131 catch( std::exception& x )
132 {
133 std::cerr << x.what() << std::endl;
134 return 1;
135 }
136
137 return 0;
138 }
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::PolyMeshT::Point
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
OpenMesh::TriMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:73
OpenMesh::PolyMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:94
OpenMesh::PolyMeshT::add_vertex
SmartVertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
OpenMesh::PolyMeshT::VertexHandle
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136