FileManagerT.cc 9.29 KB
Newer Older
Mike Kremer's avatar
Mike Kremer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                           www.openmesh.org                                *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                          *
 *   $Date$                   *
 *   $LastChangedBy$                                                *
 *                                                                           *
\*===========================================================================*/

Mike Kremer's avatar
Mike Kremer committed
43
44
#define FILEMANAGERT_CC

Mike Kremer's avatar
Mike Kremer committed
45
46
#include <OpenVolumeMesh/Geometry/VectorT.hh>

Mike Kremer's avatar
Mike Kremer committed
47
48
#include "FileManager.hh"

Mike Kremer's avatar
Mike Kremer committed
49
50
51
52
#include <fstream>
#include <vector>
#include <iostream>

Mike Kremer's avatar
Mike Kremer committed
53
54
55
56
namespace OpenVolumeMesh {

namespace IO {

Mike Kremer's avatar
Mike Kremer committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//==================================================

FileManager::FileManager() {

}

//==================================================

FileManager::~FileManager() {

}

//==================================================

template <class MeshT>
Mike Kremer's avatar
Mike Kremer committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
bool FileManager::readFile(const std::string& _filename, MeshT& _mesh,
    bool _topologyCheck, bool _computeBottomUpAdjacencies, bool _computeFaceNormals) const {

  // found edges in file
  bool edges_in_file = false;

  typedef typename MeshT::Vertex Vertex;
  _mesh.clear();

  std::ifstream iff(_filename.c_str(), std::ios::in);

  std::string s;

  // read header
  iff >> s;
  if (s != "Vertices") {
    std::cerr << "Error reading OpenVolumeMesh file: Vertex section failed!" << std::endl;
    iff.close();
    return false;
  }

  // read vertices
  int nv = 0;
  iff >> nv;
  for (int i = 0; i < nv; ++i) {
    double x, y, z;
    iff >> x;
    iff >> y;
    iff >> z;

    Vec3d v(x, y, z);
    _mesh.add_vertex(v);
  }

  iff >> s;
  if (s != "Edges") {
    std::cerr << "No edges found!" << std::endl;
  } else {
    edges_in_file = true;
    int ne = 0;
    iff >> ne;
    for (int e = 0; e < ne; ++e) {
      int v1 = 0;
      int v2 = 0;
      iff >> v1;
      iff >> v2;
      _mesh.add_edge(typename MeshT::VertexHandle(v1), typename MeshT::VertexHandle(v2));
    }
  }

  if (edges_in_file) {
    iff >> s;
  }

  if (s != "Faces") {
    std::cerr << "Error reading OpenVolumeMesh file: Face section failed!" << std::endl;
    iff.close();
    return false;
  }

  // Read halfface indices
  int nf = 0;
  iff >> nf;
  for (int f = 0; f < nf; ++f) {
    int nfhe;
    iff >> nfhe;
    std::vector<typename MeshT::HalfEdgeHandle> hes;
    for (int he = 0; he < nfhe; ++he) {
      int i;
      iff >> i;
      hes.push_back(i);
    }

    _mesh.add_face(hes, _topologyCheck);
  }

  // Read faces and find the respective halffaces
  iff >> s;
  if (s != "Polyhedra") {
    std::cerr << "Error reading OpenVolumeMesh file: Polyhedra section failed!" << std::endl;
    iff.close();
    return false;
  }

  // Just read the specified halffaces
  int nc = 0;
  iff >> nc;
  for (int c = 0; c < nc; ++c) {

    int nhf;
    iff >> nhf;
    std::vector<typename MeshT::HalfFaceHandle> hfs;
    for (int hf = 0; hf < nhf; ++hf) {
      int i;
      iff >> i;
      hfs.push_back(i);
    }

    _mesh.add_cell(hfs, _topologyCheck);
  }

  iff.close();

  // Compute top-down-adjacencies
  if(_computeBottomUpAdjacencies) {
    _mesh.update_adjacencies();
  }

  // Compute face normals
  if(_computeFaceNormals) {
    _mesh.request_face_normals();
  }

  std::cerr << "######## openvolumemesh info #########" << std::endl;
  std::cerr << "#vertices: " << _mesh.n_vertices() << std::endl;
  std::cerr << "#edges:    " << _mesh.n_edges() << std::endl;
  std::cerr << "#faces:    " << _mesh.n_faces() << std::endl;
  std::cerr << "#cells:    " << _mesh.n_cells() << std::endl;
  std::cerr << "######################################" << std::endl;
Mike Kremer's avatar
Mike Kremer committed
191

Mike Kremer's avatar
Mike Kremer committed
192
  return true;
Mike Kremer's avatar
Mike Kremer committed
193
194
195
196
197
}

//==================================================

template <class MeshT>
Mike Kremer's avatar
Mike Kremer committed
198
bool FileManager::writeFile(const std::string& _filename, const MeshT& _mesh) const {
Mike Kremer's avatar
Mike Kremer committed
199

Mike Kremer's avatar
Mike Kremer committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  typedef typename MeshT::Face Face;
  std::ofstream off(_filename.c_str(), std::ios::out);

  if(!off.good()) {
    std::cerr << "Error: Could not open file " << _filename << " for writing!" << std::endl;
    off.close();
    return false;
  }

  int n_vertices(_mesh.n_vertices());
  off << "Vertices" << std::endl;
  off << n_vertices << std::endl;

  // write vertices
  for (typename MeshT::VertexIter v_it = _mesh.v_iter(); v_it; ++v_it) {

    Vec3d v = _mesh.vertex(*v_it).position();
    off << v[0] << " " << v[1] << " " << v[2] << std::endl;
  }

  int n_edges(_mesh.n_edges());
  off << "Edges" << std::endl;
  off << n_edges << std::endl;

  // write edges
  for (typename MeshT::EdgeIter e_it = _mesh.e_iter(); e_it; ++e_it) {

    typename MeshT::VertexHandle from_vertex = _mesh.edge(*e_it).from_vertex();
    typename MeshT::VertexHandle to_vertex = _mesh.edge(*e_it).to_vertex();
    off << from_vertex << " " << to_vertex << std::endl;
  }

  int n_faces(_mesh.n_faces());
  off << "Faces" << std::endl;
  off << n_faces << std::endl;

  // write faces
  for (typename MeshT::FaceIter f_it = _mesh.f_iter(); f_it; ++f_it) {

    off << _mesh.face(*f_it).halfedges().size() << " ";

    std::vector<typename MeshT::HalfEdgeHandle> halfedges = _mesh.face(*f_it).halfedges();

    for (typename std::vector<typename MeshT::HalfEdgeHandle>::const_iterator it = halfedges.begin(); it
        != halfedges.end(); ++it) {

      off << *it;

      if ((it + 1) != halfedges.end())
        off << " ";
    }

    off << std::endl;
  }

  int n_cells(_mesh.n_cells());
  off << "Polyhedra" << std::endl;
  off << n_cells << std::endl;

  for (typename MeshT::CellIter c_it = _mesh.c_iter(); c_it; ++c_it) {

    off << _mesh.cell(*c_it).halffaces().size() << " ";

    std::vector<typename MeshT::HalfFaceHandle> halffaces = _mesh.cell(*c_it).halffaces();

    for (typename std::vector<typename MeshT::HalfFaceHandle>::const_iterator it = halffaces.begin(); it
        != halffaces.end(); ++it) {

      off << *it;

      if ((it + 1) != halffaces.end())
        off << " ";
    }

    off << std::endl;
  }

  off.close();

  return true;
}

//==================================================

bool FileManager::isHexahedralMesh(const std::string& _filename) const {

  std::ifstream iff(_filename.c_str(), std::ios::in);

  std::string s;
  unsigned int n = 0u;

  // Skip until we find polyhedra section
  while (true || !iff.eof()) {
    iff >> s;
    if (s == "Polyhedra") {
      break;
    }
  }

  if (iff.eof()) {
    // Polyhedra section not found in file. Defaulting to polyhedral type.
    iff.close();
    return false;
  }

  // Read in number of cells
  iff >> n;
  unsigned int v = 0;
  char tmp[256];
  for (unsigned int i = 0; i < n; ++i) {
    iff >> v;
    iff.getline(tmp, 256);
    if (v != 6u) {
      iff.close();
      return false;
    }
  }
  iff.close();
Mike Kremer's avatar
Mike Kremer committed
318
  return true;
Mike Kremer's avatar
Mike Kremer committed
319
}
Mike Kremer's avatar
Mike Kremer committed
320

Mike Kremer's avatar
Mike Kremer committed
321
322
//==================================================

Mike Kremer's avatar
Mike Kremer committed
323
324
325
} // Namespace IO

} // Namespace FileManager