Developer Documentation
FileOpenVolumeMesh.cc
1 /*===========================================================================*\
2 * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
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: 13576 $ *
45  * $LastChangedBy: kremer $ *
46  * $Date: 2012-01-30 11:22:27 +0100 (Mo, 30 Jan 2012) $ *
47  * *
48  \*===========================================================================*/
49 
50 #include <iostream>
51 
52 #include <ACG/GL/GLState.hh>
53 
56 
57 #include "FileOpenVolumeMesh.hh"
58 
59 #if QT_VERSION >= 0x050000
60 #else
61  #include <QtGui>
62 #endif
63 
64 
65 FileOpenVolumeMeshPlugin::FileOpenVolumeMeshPlugin() :
66 loadOptions_(0),
67 saveOptions_(0),
68 typeCheck_(0),
69 loadCompMode_(0),
70 loadTopCheck_(0),
71 saveCompMode_(0) {
72 
73 }
74 
75 //----------------------------------------------------------------------------
76 
77 void FileOpenVolumeMeshPlugin::initializePlugin() {
78 
79  loadOptions_ = new QWidget();
80 
81  QVBoxLayout* llayout = new QVBoxLayout();
82  llayout->setAlignment(Qt::AlignTop);
83 
84  typeCheck_ = new QComboBox();
85  typeCheck_->addItem("Autodetect");
86  typeCheck_->addItem("Polyhedral Mesh");
87  typeCheck_->addItem("Hexahedral Mesh");
88 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
89  typeCheck_->addItem("Tetrahedral Mesh");
90 #endif
91  typeCheck_->setCurrentIndex(0);
92  loadCompMode_ = new QCheckBox("Load PolyVolMesh format");
93  loadTopCheck_ = new QCheckBox("Perform topology checks");
94  llayout->addWidget(typeCheck_);
95  llayout->addWidget(loadCompMode_);
96  llayout->addWidget(loadTopCheck_);
97 
98  loadOptions_->setLayout(llayout);
99 
100  saveOptions_ = new QWidget();
101 
102  QVBoxLayout* slayout = new QVBoxLayout();
103  slayout->setAlignment(Qt::AlignTop);
104 
105  saveCompMode_ = new QCheckBox("Save in PolyVolMesh format");
106  slayout->addWidget(saveCompMode_);
107 
108  saveOptions_->setLayout(slayout);
109 }
110 
111 //----------------------------------------------------------------------------
112 
113 
115  return QString(tr("Polyhedral Volume Mesh files ( *.ovm *.polyvolmesh *.tetmesh )"));
116 }
117 ;
118 
119 //----------------------------------------------------------------------------
120 
121 
123  return QString(tr("Polyhedral Volume Mesh files ( *.ovm )"));
124 }
125 ;
126 
127 //----------------------------------------------------------------------------
128 
129 
131 
133 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
134  type |= DATA_TETRAHEDRAL_MESH;
135 #endif
136  return type;
137 }
138 
139 //----------------------------------------------------------------------------
140 
141 
142 int FileOpenVolumeMeshPlugin::loadObject(QString _filename) {
143 
144  bool compatibility_mode = false;
145  if(!OpenFlipper::Options::nogui()) {
146  compatibility_mode = loadCompMode_->isChecked();
147  }
148 
149  bool topology_checks = true;
150  if(!OpenFlipper::Options::nogui()) {
151  topology_checks = loadTopCheck_->isChecked();
152  }
153 
154  int id = -1;
155  bool hexMesh = false;
156  bool tetMesh = false;
157 
158  if(!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 0) {
159  hexMesh = fileManager_.isHexahedralMesh(_filename.toStdString());
160  } else if (!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 2) {
161  hexMesh = true;
162  }
163 
164 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
165  if(!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 0) {
166  tetMesh = fileManager_.isTetrahedralMesh(_filename.toStdString());
167  } else if (!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 3) {
168  tetMesh = true;
169  }
170 #endif // ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
171 
172  BaseObjectData* baseObj = 0;
173 
174  if(hexMesh) {
175 
176  emit addEmptyObject(DATA_HEXAHEDRAL_MESH, id);
177  HexahedralMeshObject* obj(0);
178 
179  if (PluginFunctions::getObject(id, obj)) {
180  baseObj = obj;
181 
182  if(compatibility_mode) {
183 
184  loadMesh((const char*) _filename.toLatin1(), *(obj->mesh()), compatibility_mode,
185  topology_checks);
186 
187  } else {
188  if(!fileManager_.readFile(_filename.toStdString(), *(obj->mesh()),
189  topology_checks,true)) {
190  emit log(LOGERR, QString("Could not open file %1!").arg(_filename));
191  }
192  }
193 
194  // Scale hexahedra a bit
195  obj->meshNode()->set_scaling(0.8);
196 
197  }
198 
199  }
200 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
201  else if(tetMesh) {
202 
203  emit addEmptyObject(DATA_TETRAHEDRAL_MESH, id);
204  TetrahedralMeshObject* obj(0);
205 
206  if (PluginFunctions::getObject(id, obj)) {
207  baseObj = obj;
208 
209  if(compatibility_mode) {
210 
211  loadMesh((const char*) _filename.toLatin1(), *(obj->mesh()), compatibility_mode,
212  topology_checks);
213 
214  } else {
215  if(!fileManager_.readFile(_filename.toStdString(), *(obj->mesh()),
216  topology_checks,true)) {
217  emit log(LOGERR, QString("Could not open file %1!").arg(_filename));
218  }
219  }
220 
221  // Scale tetrahedra a bit
222  obj->meshNode()->set_scaling(0.8);
223  }
224 
225 
226  }
227 #endif // ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
228  else {
229 
230  emit addEmptyObject(DATA_POLYHEDRAL_MESH, id);
231  PolyhedralMeshObject* obj(0);
232 
233  if (PluginFunctions::getObject(id, obj)) {
234  baseObj = obj;
235 
236  if(compatibility_mode) {
237 
238  loadMesh((const char*) _filename.toLatin1(), *(obj->mesh()), compatibility_mode,
239  topology_checks);
240 
241  } else {
242  if(!fileManager_.readFile(_filename.toStdString(), *(obj->mesh()),
243  topology_checks,true)) {
244  emit log(LOGERR, QString("Could not open file %1!").arg(_filename));
245  }
246  }
247 
248  // Scale polyhedra a bit
249  obj->meshNode()->set_scaling(0.8);
250 
251  }
252  }
253 
254  if (baseObj)
255  {
256  baseObj->setFromFileName(_filename);
257  baseObj->setName(baseObj->filename());
258 
259  // Go into solid flat shaded mode
260  baseObj->setObjectDrawMode(ACG::SceneGraph::DrawModes::getDrawMode("Cells (flat shaded)"));
261 
262  // Compute face normals
263  emit updatedObject(baseObj->id(), UPDATE_ALL);
264 
265  emit openedFile(baseObj->id());
266  }
267 
268  return id;
269 }
270 
271 //----------------------------------------------------------------------------
272 
273 
274 bool FileOpenVolumeMeshPlugin::saveObject(int _id, QString _filename) {
275 
276  BaseObjectData* obj(0);
277  if (PluginFunctions::getObject(_id, obj)) {
278 
281 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
282  TetrahedralMeshObject* tet_mesh_obj = PluginFunctions::tetrahedralMeshObject(obj);
283 #endif
284  if (mesh_obj) {
285 
286  obj->setFromFileName(_filename);
287  obj->setName(obj->filename());
288  if(!fileManager_.writeFile(_filename.toStdString(), *(mesh_obj->mesh()))) {
289  emit log(LOGERR, tr("Unable to save ") + _filename);
290  return false;
291  }
292  }
293  else if (hex_mesh_obj) {
294 
295  obj->setFromFileName(_filename);
296  obj->setName(obj->filename());
297  if (!fileManager_.writeFile(_filename.toStdString(), *(hex_mesh_obj->mesh()))) {
298  emit log(LOGERR, tr("Unable to save ") + _filename);
299  return false;
300  }
301  }
302 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
303  else if (tet_mesh_obj) {
304 
305  obj->setFromFileName(_filename);
306  obj->setName(obj->filename());
307  if (!fileManager_.writeFile(_filename.toStdString(), *(tet_mesh_obj->mesh()))) {
308  emit log(LOGERR, tr("Unable to save ") + _filename);
309  return false;
310  }
311  }
312 #endif // ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
313 
314  return true;
315 
316  } else {
317  emit log(LOGERR, tr("saveObject : cannot get object id %1 for save name %2").arg(_id).arg(_filename) );
318  return false;
319  }
320 
321 
322 }
323 
324 //----------------------------------------------------------------------------
325 
326 
327 void FileOpenVolumeMeshPlugin::loadIniFileLast(INIFile& _ini, int _id) {
328 
329  BaseObjectData* baseObject;
330  if (!PluginFunctions::getObject(_id, baseObject)) {
331  emit log(LOGERR, tr("Cannot find object for id %1 in saveFile!").arg(_id));
332  return;
333  }
334 
336 
337  if (object) {
338  ACG::Vec4f col(0.0, 0.0, 0.0, 0.0);
339 
340  if (_ini.get_entryVecf(col, object->name(), "BaseColor"))
341  object->materialNode()->set_base_color(col);
342  }
343 
344 }
345 
346 //----------------------------------------------------------------------------
347 
348 void FileOpenVolumeMeshPlugin::saveIniFile(INIFile& _ini, int _id) {
349 
350  BaseObjectData* baseObject;
351  if (!PluginFunctions::getObject(_id, baseObject)) {
352  emit log(LOGERR, tr("Cannot find object for id %1 in saveFile!").arg(_id));
353  return;
354  }
355 
357 
358  if (object) {
359  _ini.add_entryVec(object->name(), "BaseColor", object->materialNode()->base_color());
360  }
361 }
362 
363 //----------------------------------------------------------------------------
364 
365 QWidget* FileOpenVolumeMeshPlugin::saveOptionsWidget(QString _currentFilter) {
366 
367  return saveOptions_;
368 }
369 
370 //----------------------------------------------------------------------------
371 
372 QWidget* FileOpenVolumeMeshPlugin::loadOptionsWidget(QString _currentFilter) {
373 
374  return loadOptions_;
375 }
376 #if QT_VERSION < 0x050000
377  Q_EXPORT_PLUGIN2(fileopenvolumemeshplugin, FileOpenVolumeMeshPlugin)
378 #endif
379 
Predefined datatypes.
Definition: DataTypes.hh:96
const DrawMode & getDrawMode(const std::string &_name)
Get a custom DrawMode.
Definition: DrawModes.cc:813
ACG::SceneGraph::VolumeMeshNodeT< MeshT > * meshNode()
Get the Scenegraph Mesh Node.
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
void setObjectDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, const bool &_force=false)
Set the draw mode for the object.
QString filename() const
return the filename of the object
Definition: BaseObject.cc:717
bool getObject(int _identifier, BSplineCurveObject *&_object)
QWidget * saveOptionsWidget(QString _currentFilter)
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
Definition: INIFileT.cc:211
#define DATA_POLYHEDRAL_MESH
MeshT * mesh()
return a pointer to the mesh
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT.cc:169
int id() const
Definition: BaseObject.cc:201
void setFromFileName(const QString &_filename)
Definition: BaseObject.cc:727
#define DATA_HEXAHEDRAL_MESH
QWidget * loadOptionsWidget(QString _currentFilter)
HexahedralMeshObject * hexahedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an HexahedralMeshObject if possible.
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
Class for the handling of simple configuration files.
Definition: INIFile.hh:105
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:741
PolyhedralMeshObject * polyhedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an PolyhedralMeshObject if possible.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.