ModProgMeshT_impl.hh 6.96 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6
7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
8
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
9
10
11
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13
14
15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17
18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20
21
22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24
25
26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28
29
30
31
32
33
34
35
36
37
38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41

42

Jan Möbius's avatar
Jan Möbius committed
43

44
/** \file ModProgMeshT_impl.hh
Jan Möbius's avatar
Jan Möbius committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 */


//=============================================================================
//
//  CLASS ModProgMeshT - IMPLEMENTATION
//
//=============================================================================

#define OPENMESH_DECIMATER_MODPROGMESH_CC


//== INCLUDES =================================================================

#include <vector>
#include <fstream>
// --------------------
#include <OpenMesh/Core/Utils/vector_cast.hh>
#include <OpenMesh/Core/IO/BinaryHelper.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
// --------------------
#include <OpenMesh/Tools/Decimater/ModProgMeshT.hh>


//== NAMESPACE =============================================================== 

namespace OpenMesh  {
namespace Decimater {

   

//== IMPLEMENTATION ========================================================== 


79
template <class MeshT>
Jan Möbius's avatar
Jan Möbius committed
80
bool 
81
ModProgMeshT<MeshT>::
Jan Möbius's avatar
Jan Möbius committed
82
83
84
85
86
87
88
89
90
91
92
93
94
write( const std::string& _ofname )
{
  // sort vertices
  size_t i=0, N=Base::mesh().n_vertices(), n_base_vertices(0), n_base_faces(0);
  std::vector<typename Mesh::VertexHandle>  vhandles(N);


  // base vertices
  typename Mesh::VertexIter 
    v_it=Base::mesh().vertices_begin(), 
    v_end=Base::mesh().vertices_end();

  for (; v_it != v_end; ++v_it)  
Jan Möbius's avatar
Jan Möbius committed
95
    if (!Base::mesh().status(*v_it).deleted())
Jan Möbius's avatar
Jan Möbius committed
96
    {
Jan Möbius's avatar
Jan Möbius committed
97
      vhandles[i] = *v_it;
Jan Möbius's avatar
Jan Möbius committed
98
      Base::mesh().property( idx_, *v_it ) = i;
Jan Möbius's avatar
Jan Möbius committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      ++i;
    }
  n_base_vertices = i;


  // deleted vertices
  typename InfoList::reverse_iterator
    r_it=pmi_.rbegin(), r_end=pmi_.rend();

  for (; r_it!=r_end; ++r_it)  
  { 
    vhandles[i] = r_it->v0;  
    Base::mesh().property( idx_, r_it->v0) = i;  
    ++i; 
  }


  // base faces
  typename Mesh::ConstFaceIter f_it  = Base::mesh().faces_begin(), 
                               f_end = Base::mesh().faces_end();
  for (; f_it != f_end; ++f_it) 
Jan Möbius's avatar
Jan Möbius committed
120
    if (!Base::mesh().status(*f_it).deleted())
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
      ++n_base_faces;

  // ---------------------------------------- write progressive mesh

  std::ofstream out( _ofname.c_str(), std::ios::binary );
    
  if (!out)
    return false;   

  // always use little endian byte ordering
  bool swap = Endian::local() != Endian::LSB;

  // write header
  out << "ProgMesh";
135
136
137
  IO::store( out, static_cast<unsigned int>(n_base_vertices), swap );//store in 32-bit
  IO::store( out, static_cast<unsigned int>(n_base_faces)   , swap );
  IO::store( out, static_cast<unsigned int>(pmi_.size())    , swap );
Jan Möbius's avatar
Jan Möbius committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

  Vec3f p;

  // write base vertices
  for (i=0; i<n_base_vertices; ++i)
  {
    assert (!Base::mesh().status(vhandles[i]).deleted());
    p  = vector_cast< Vec3f >( Base::mesh().point(vhandles[i]) );
    
    IO::store( out, p, swap );
  }
    

  // write base faces
  for (f_it=Base::mesh().faces_begin(); f_it != f_end; ++f_it)  
  {
Jan Möbius's avatar
Jan Möbius committed
154
    if (!Base::mesh().status(*f_it).deleted())
Jan Möbius's avatar
Jan Möbius committed
155
    {
Jan Möbius's avatar
Jan Möbius committed
156
      typename Mesh::ConstFaceVertexIter fv_it(Base::mesh(), *f_it);
Jan Möbius's avatar
Jan Möbius committed
157
      
158
159
160
      IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_,   *fv_it )) );
      IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_, *(++fv_it ))) );
      IO::store( out, static_cast<unsigned int>(Base::mesh().property( idx_, *(++fv_it ))) );
Jan Möbius's avatar
Jan Möbius committed
161
162
163
164
165
166
167
168
169
    }
  }
  
  
  // write detail info
  for (r_it=pmi_.rbegin(); r_it!=r_end; ++r_it)  
  { 
    // store v0.pos, v1.idx, vl.idx, vr.idx
    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)));
Jan Möbius's avatar
Jan Möbius committed
171
    IO::store( out, 
172
        r_it->vl.is_valid() ? static_cast<unsigned int>(Base::mesh().property(idx_, r_it->vl)) : -1);
Jan Möbius's avatar
Jan Möbius committed
173
    IO::store( out, 
174
        r_it->vr.is_valid() ? static_cast<unsigned int>(Base::mesh().property(idx_, r_it->vr)) : -1);
Jan Möbius's avatar
Jan Möbius committed
175
176
177
178
179
180
181
182
183
184
185
186
  }

  return true;
}



//=============================================================================
} // END_NS_DECIMATER
} // END_NS_OPENMESH
//=============================================================================