Developer Documentation
VolumeMeshConvert.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 
45 
46 #include "VolumeMeshConvert.hh"
52 
54 {
55 
56 }
57 
58 
59 void VolumeMeshConvertPlugin::pluginsInitialized()
60 {
61  //populate scripting function
62  emit setSlotDescription("convertToHexMesh(int)",
63  "Convert a polyhedral Mesh to a hexahedral mesh."
64  " Does not check if input mesh actually has the right topology."
65  " Return the ID of the new mesh or -1 in case of error."
66  " The old mesh remains unchanged.",
67  QStringList("object_id"),
68  QStringList("id of an the input object"));
69  emit setSlotDescription("convertToTetMesh(int)",
70  "Convert a polyhedral Mesh to a tetrahedral mesh."
71  " Does not check if input mesh actually has the right topology."
72  " Return the ID of the new mesh or -1 in case of error."
73  " The old mesh remains unchanged.",
74  QStringList("object_id"),
75  QStringList("id of an the input object"));
76  emit setSlotDescription("convertToPolyhedralMesh(int)",
77  "Convert a hex or tet mesh to a PolyhedralMesh."
78  " Return the ID of the new mesh or -1 in case of error."
79  " The old mesh remains unchanged.",
80  QStringList("object_id"),
81  QStringList("id of an the input object"));
82 
83  if(! OpenFlipper::Options::gui())
84  return;
85 
86  // Create your toolbar
87  toolbar_ = new QToolBar(tr("Volume Mesh conversion"));
88 
89  grp_ = new QActionGroup(toolbar_);
90 
91  // Create an action for the toolbar
92  actionToHex_ = new QAction(tr("&Convert Polyhedral to Hexahedral mesh"), grp_);
93  actionToTet_ = new QAction(tr("&Convert Polyhedral to Tetrahedral mesh"), grp_);
94  actionToPoly_ = new QAction(tr("&Convert Tet or Hex mesh to Polyhedral mesh"), grp_);
95 
96  // Create an icon which is shown for the action
97  // TODO
98  actionToHex_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"VolumeConvertToHex.png"));
99  actionToTet_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"VolumeConvertToTet.png"));
100  actionToPoly_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"VolumeConvertToPoly.png"));
101 
102  // Add the action to the toolbar
103  toolbar_->addAction(actionToHex_);
104  toolbar_->addAction(actionToTet_);
105  toolbar_->addAction(actionToPoly_);
106 
107  connect( grp_, SIGNAL( triggered(QAction*) ), this, SLOT(handleQAction(QAction*)) );
108 
109  // Integrate the new toolbar into OpenFlipper
110  emit addToolbar( toolbar_ );
111 }
112 
114 {
115  std::vector<int> _ids;
117  return;
118  for(const auto id: _ids)
119  {
120  int out_id = -1;
121  if (_action == actionToHex_)
122  out_id = convertToHexMesh(id);
123  else if (_action == actionToPoly_)
124  out_id = convertToPolyhedralMesh(id);
125  else if (_action == actionToTet_)
126  out_id = convertToTetMesh(id);
127  else {
128  assert(false);
129  }
130  if (out_id == -1) {
131  emit log(LOGERR, "VolumeMeshConvert: failed to convert object " + QString::number(id));
132  } else {
133  emit log(LOGINFO, "VolumeMeshConvert: successfully converted object"
134  + QString::number(id) + " to new object " + QString::number(out_id));
135  }
136  }
137 }
138 
139 int VolumeMeshConvertPlugin::convertToHexMesh(int _id)
140 {
142  if (!pm)
143  return -1;
144  int newID = -1;
145  emit addEmptyObject(DATA_HEXAHEDRAL_MESH, newID);
147  out->assign(pm);
148 
149  BaseObject *oldObj, *newObj;
150  PluginFunctions::getObject(_id, oldObj);
151  PluginFunctions::getObject(newID, newObj);
152  newObj->setName("Hex conv. of " + oldObj->name());
153 
154  emit updatedObject(newID, UPDATE_ALL);
155  return newID;
156 }
157 
158 int VolumeMeshConvertPlugin::convertToTetMesh(int _id)
159 {
161  if (!pm)
162  return -1;
163  int newID = -1;
164  emit addEmptyObject(DATA_TETRAHEDRAL_MESH, newID);
166  out->assign(pm);
167 
168  BaseObject *oldObj, *newObj;
169  PluginFunctions::getObject(_id, oldObj);
170  PluginFunctions::getObject(newID, newObj);
171  newObj->setName("Tet conv. of " + oldObj->name());
172 
173  emit updatedObject(newID, UPDATE_ALL);
174  return newID;
175 }
176 
177 int VolumeMeshConvertPlugin::convertToPolyhedralMesh(int _id)
178 {
181  if (!hm && !tm) {
182  return -1;
183  }
184  int newID = -1;
185  emit addEmptyObject(DATA_POLYHEDRAL_MESH, newID);
187  if (hm) {
188  out->assign(hm);
189  }
190  else if (tm) {
191  out->assign(tm);
192  }
193  BaseObject *oldObj, *newObj;
194  PluginFunctions::getObject(_id, oldObj);
195  PluginFunctions::getObject(newID, newObj);
196  newObj->setName("Polyhedral conv. of " + oldObj->name());
197  emit updatedObject(newID, UPDATE_ALL);
198 
199  return newID;
200 }
#define DATA_POLYHEDRAL_MESH
void handleQAction(QAction *)
convert Converts polyhedralmesh to hex/tet mesh and vice versa depending on the Action that was calle...
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
HexahedralMesh * hexahedralMesh(BaseObjectData *_object)
Get an HexahedralMesh from an object.
#define DATA_TETRAHEDRAL_MESH
#define DATA_HEXAHEDRAL_MESH
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
Definition: BaseObject.cc:723
void initializePlugin() override
BaseInterface.
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
TetrahedralMesh * tetrahedralMesh(BaseObjectData *_object)
Get an TetrahedralMesh from an object.
PolyhedralMesh * polyhedralMesh(BaseObjectData *_object)
Get an PolyhedralMesh from an object.
bool getTargetIdentifiers(std::vector< int > &_identifiers)
Get the identifiers of all objects marked as a target object.