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

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .