Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  typeCheck_->setCurrentIndex(0);
89  loadCompMode_ = new QCheckBox("Load PolyVolMesh format");
90  loadTopCheck_ = new QCheckBox("Perform topology checks");
91  llayout->addWidget(typeCheck_);
92  llayout->addWidget(loadCompMode_);
93  llayout->addWidget(loadTopCheck_);
94 
95  loadOptions_->setLayout(llayout);
96 
97  saveOptions_ = new QWidget();
98 
99  QVBoxLayout* slayout = new QVBoxLayout();
100  slayout->setAlignment(Qt::AlignTop);
101 
102  saveCompMode_ = new QCheckBox("Save in PolyVolMesh format");
103  slayout->addWidget(saveCompMode_);
104 
105  saveOptions_->setLayout(slayout);
106 }
107 
108 //----------------------------------------------------------------------------
109 
110 
112  return QString(tr("Polyhedral Volume Mesh files ( *.ovm *.polyvolmesh *.tetmesh )"));
113 }
114 ;
115 
116 //----------------------------------------------------------------------------
117 
118 
120  return QString(tr("Polyhedral Volume Mesh files ( *.ovm )"));
121 }
122 ;
123 
124 //----------------------------------------------------------------------------
125 
126 
128 
130  return type;
131 }
132 
133 //----------------------------------------------------------------------------
134 
135 
136 int FileOpenVolumeMeshPlugin::loadObject(QString _filename) {
137 
138  bool compatibility_mode = false;
139  if(!OpenFlipper::Options::nogui()) {
140  compatibility_mode = loadCompMode_->isChecked();
141  }
142 
143  bool topology_checks = true;
144  if(!OpenFlipper::Options::nogui()) {
145  topology_checks = loadTopCheck_->isChecked();
146  }
147 
148  int id = -1;
149  bool hexMesh = false;
150 
151  if(!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 0) {
152  hexMesh = fileManager_.isHexahedralMesh(_filename.toStdString());
153  } else if (!OpenFlipper::Options::nogui() && typeCheck_->currentIndex() == 2) {
154  hexMesh = true;
155  }
156 
157  BaseObjectData* baseObj = 0;
158 
159  if(hexMesh) {
160 
161  emit addEmptyObject(DATA_HEXAHEDRAL_MESH, id);
162  HexahedralMeshObject* obj(0);
163 
164  if (PluginFunctions::getObject(id, obj)) {
165  baseObj = obj;
166 
167  if(compatibility_mode) {
168 
169  loadMesh((const char*) _filename.toLatin1(), *(obj->mesh()), compatibility_mode,
170  topology_checks);
171 
172  } else {
173  if(!fileManager_.readFile(_filename.toStdString(), *(obj->mesh()),
174  topology_checks,true)) {
175  emit log(LOGERR, QString("Could not open file %1!").arg(_filename));
176  }
177  }
178 
179  }
180 
181  // Scale polyhedra a bit
182  obj->meshNode()->set_scaling(0.8);
183 
184  } else {
185 
186  emit addEmptyObject(DATA_POLYHEDRAL_MESH, id);
187  PolyhedralMeshObject* obj(0);
188 
189  if (PluginFunctions::getObject(id, obj)) {
190  baseObj = obj;
191 
192  if(compatibility_mode) {
193 
194  loadMesh((const char*) _filename.toLatin1(), *(obj->mesh()), compatibility_mode,
195  topology_checks);
196 
197  } else {
198  if(!fileManager_.readFile(_filename.toStdString(), *(obj->mesh()),
199  topology_checks,true)) {
200  emit log(LOGERR, QString("Could not open file %1!").arg(_filename));
201  }
202  }
203 
204  }
205 
206  // Scale polyhedra a bit
207  obj->meshNode()->set_scaling(0.8);
208  }
209 
210  if (baseObj)
211  {
212  baseObj->setFromFileName(_filename);
213  baseObj->setName(baseObj->filename());
214 
215  // Go into solid flat shaded mode
216  baseObj->setObjectDrawMode(ACG::SceneGraph::DrawModes::getDrawMode("Cells (flat shaded)"));
217 
218  // Compute face normals
219  emit updatedObject(baseObj->id(), UPDATE_ALL);
220 
221  emit openedFile(baseObj->id());
222  }
223 
224  return id;
225 }
226 
227 //----------------------------------------------------------------------------
228 
229 
230 bool FileOpenVolumeMeshPlugin::saveObject(int _id, QString _filename) {
231 
232  BaseObjectData* obj(0);
233  if (PluginFunctions::getObject(_id, obj)) {
234 
237  if (mesh_obj) {
238 
239  obj->setFromFileName(_filename);
240  obj->setName(obj->filename());
241  if(!fileManager_.writeFile(_filename.toStdString(), *(mesh_obj->mesh()))) {
242  emit log(LOGERR, tr("Unable to save ") + _filename);
243  return false;
244  }
245  }
246  else if (hex_mesh_obj) {
247 
248  obj->setFromFileName(_filename);
249  obj->setName(obj->filename());
250  if (!fileManager_.writeFile(_filename.toStdString(), *(hex_mesh_obj->mesh()))) {
251  emit log(LOGERR, tr("Unable to save ") + _filename);
252  return false;
253  }
254  }
255 
256  return true;
257 
258  } else {
259  emit log(LOGERR, tr("saveObject : cannot get object id %1 for save name %2").arg(_id).arg(_filename) );
260  return false;
261  }
262 
263 
264 }
265 
266 //----------------------------------------------------------------------------
267 
268 
269 void FileOpenVolumeMeshPlugin::loadIniFileLast(INIFile& _ini, int _id) {
270 
271  BaseObjectData* baseObject;
272  if (!PluginFunctions::getObject(_id, baseObject)) {
273  emit log(LOGERR, tr("Cannot find object for id %1 in saveFile!").arg(_id));
274  return;
275  }
276 
278 
279  if (object) {
280  ACG::Vec4f col(0.0, 0.0, 0.0, 0.0);
281 
282  if (_ini.get_entryVecf(col, object->name(), "BaseColor"))
283  object->materialNode()->set_base_color(col);
284  }
285 
286 }
287 
288 //----------------------------------------------------------------------------
289 
290 void FileOpenVolumeMeshPlugin::saveIniFile(INIFile& _ini, int _id) {
291 
292  BaseObjectData* baseObject;
293  if (!PluginFunctions::getObject(_id, baseObject)) {
294  emit log(LOGERR, tr("Cannot find object for id %1 in saveFile!").arg(_id));
295  return;
296  }
297 
299 
300  if (object) {
301  _ini.add_entryVec(object->name(), "BaseColor", object->materialNode()->base_color());
302  }
303 }
304 
305 //----------------------------------------------------------------------------
306 
307 QWidget* FileOpenVolumeMeshPlugin::saveOptionsWidget(QString _currentFilter) {
308 
309  return saveOptions_;
310 }
311 
312 //----------------------------------------------------------------------------
313 
314 QWidget* FileOpenVolumeMeshPlugin::loadOptionsWidget(QString _currentFilter) {
315 
316  return loadOptions_;
317 }
318 #if QT_VERSION < 0x050000
319  Q_EXPORT_PLUGIN2(fileopenvolumemeshplugin, FileOpenVolumeMeshPlugin)
320 #endif
321 
bool readFile(const std::string &_filename, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from a file.
Definition: FileManagerT.cc:66
MeshT * mesh()
return a pointer to the mesh
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:741
QString filename() const
return the filename of the object
Definition: BaseObject.cc:717
#define DATA_POLYHEDRAL_MESH
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool getObject(int _identifier, BSplineCurveObject *&_object)
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
void setFromFileName(const QString &_filename)
Definition: BaseObject.cc:727
const DrawMode & getDrawMode(const std::string &_name)
Get a custom DrawMode.
Definition: DrawModes.cc:813
QWidget * loadOptionsWidget(QString _currentFilter)
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
Definition: INIFileT.cc:211
PolyhedralMeshObject * polyhedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an PolyhedralMeshObject if possible.
Class for the handling of simple configuration files.
Definition: INIFile.hh:105
HexahedralMeshObject * hexahedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an HexahedralMeshObject if possible.
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
#define DATA_HEXAHEDRAL_MESH
QWidget * saveOptionsWidget(QString _currentFilter)
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
Definition: FileManager.cc:139
Predefined datatypes.
Definition: DataTypes.hh:96
void setObjectDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, const bool &_force=false)
Set the draw mode for the object.
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.
int id() const
Definition: BaseObject.cc:201
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT.cc:169