Developer Documentation

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