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