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