Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ModProgMeshT.cc
Go to the documentation of this file.
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 
53 //=============================================================================
54 //
55 // CLASS ModProgMeshT - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define OPENMESH_DECIMATER_MODPROGMESH_CC
60 
61 
62 //== INCLUDES =================================================================
63 
64 #include <vector>
65 #include <fstream>
66 // --------------------
67 #include <OpenMesh/Core/Utils/vector_cast.hh>
68 #include <OpenMesh/Core/IO/BinaryHelper.hh>
69 #include <OpenMesh/Core/Utils/Endian.hh>
70 // --------------------
72 
73 
74 //== NAMESPACE ===============================================================
75 
76 namespace OpenMesh {
77 namespace Decimater {
78 
79 
80 
81 //== IMPLEMENTATION ==========================================================
82 
83 
84 template <class MeshT>
85 bool
87 write( const std::string& _ofname )
88 {
89  // sort vertices
90  size_t i=0, N=Base::mesh().n_vertices(), n_base_vertices(0), n_base_faces(0);
91  std::vector<typename Mesh::VertexHandle> vhandles(N);
92 
93 
94  // base vertices
95  typename Mesh::VertexIter
96  v_it=Base::mesh().vertices_begin(),
97  v_end=Base::mesh().vertices_end();
98 
99  for (; v_it != v_end; ++v_it)
100  if (!Base::mesh().status(*v_it).deleted())
101  {
102  vhandles[i] = *v_it;
103  Base::mesh().property( idx_, *v_it ) = i;
104  ++i;
105  }
106  n_base_vertices = i;
107 
108 
109  // deleted vertices
110  typename InfoList::reverse_iterator
111  r_it=pmi_.rbegin(), r_end=pmi_.rend();
112 
113  for (; r_it!=r_end; ++r_it)
114  {
115  vhandles[i] = r_it->v0;
116  Base::mesh().property( idx_, r_it->v0) = i;
117  ++i;
118  }
119 
120 
121  // base faces
122  typename Mesh::ConstFaceIter f_it = Base::mesh().faces_begin(),
123  f_end = Base::mesh().faces_end();
124  for (; f_it != f_end; ++f_it)
125  if (!Base::mesh().status(*f_it).deleted())
126  ++n_base_faces;
127 
128  // ---------------------------------------- write progressive mesh
129 
130  std::ofstream out( _ofname.c_str(), std::ios::binary );
131 
132  if (!out)
133  return false;
134 
135  // always use little endian byte ordering
136  bool swap = Endian::local() != Endian::LSB;
137 
138  // write header
139  out << "ProgMesh";
140  IO::store( out, static_cast<unsigned int>(n_base_vertices), swap );//store in 32-bit
141  IO::store( out, static_cast<unsigned int>(n_base_faces) , swap );
142  IO::store( out, static_cast<unsigned int>(pmi_.size()) , swap );
143 
144  Vec3f p;
145 
146  // write base vertices
147  for (i=0; i<n_base_vertices; ++i)
148  {
149  assert (!Base::mesh().status(vhandles[i]).deleted());
150  p = vector_cast< Vec3f >( Base::mesh().point(vhandles[i]) );
151 
152  IO::store( out, p, swap );
153  }
154 
155 
156  // write base faces
157  for (f_it=Base::mesh().faces_begin(); f_it != f_end; ++f_it)
158  {
159  if (!Base::mesh().status(*f_it).deleted())
160  {
161  typename Mesh::ConstFaceVertexIter fv_it(Base::mesh(), *f_it);
162 
163  IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_, *fv_it )) );
164  IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_, *(++fv_it ))) );
165  IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_, *(++fv_it ))) );
166  }
167  }
168 
169 
170  // write detail info
171  for (r_it=pmi_.rbegin(); r_it!=r_end; ++r_it)
172  {
173  // store v0.pos, v1.idx, vl.idx, vr.idx
174  IO::store( out, vector_cast<Vec3f>(Base::mesh().point(r_it->v0)));
175  IO::store(out, static_cast<unsigned int>(Base::mesh().property(idx_, r_it->v1)));
176  IO::store( out,
177  r_it->vl.is_valid() ? static_cast<unsigned int>(Base::mesh().property(idx_, r_it->vl)) : -1);
178  IO::store( out,
179  r_it->vr.is_valid() ? static_cast<unsigned int>(Base::mesh().property(idx_, r_it->vr)) : -1);
180  }
181 
182  return true;
183 }
184 
185 
186 
187 //=============================================================================
188 } // END_NS_DECIMATER
189 } // END_NS_OPENMESH
190 //=============================================================================
191 
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:180
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:86
bool write(const std::string &_ofname)
Definition: ModProgMeshT.cc:87
static Type local()
Return endian type of host system.
Definition: Endian.hh:88
Little endian (Intel family and clones)
Definition: Endian.hh:83