Developer Documentation
TypeTetrahedralMesh.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 #include "TypeTetrahedralMesh.hh"
46 
47 #include <QInputDialog>
48 #include <QMenu>
49 
50 TypeTetrahedralMeshPlugin::TypeTetrahedralMeshPlugin() :
51 render_switch_(0),
52 translucency_factor_action_(0)
53 {
54 }
55 
56 bool TypeTetrahedralMeshPlugin::registerType() {
57 
58  addDataType("TetrahedralMesh", tr("Tetrahedral Volume Mesh"));
59  setTypeIcon("TetrahedralMesh", "PolyVolMeshType.png");
60  return true;
61 }
62 
63 //----------------------------------------------------------------------------
64 
65 void TypeTetrahedralMeshPlugin::pluginsInitialized() {
66 
67  if(OpenFlipper::Options::nogui()) return;
68 
69  emit registerKey(Qt::Key_F8, Qt::ShiftModifier, "Set scaling of Tet shrinkage");
70 
71  QMenu* menu = new QMenu("Tetrahedral Mesh Options");
72 
73  // scaling action in context menu
74  QAction* act_scale_cells = new QAction(tr("Scale cells (Shift-F8)"), this);
75  act_scale_cells->setStatusTip(tr("Scale cells (Shift-F8)"));
76  connect(act_scale_cells, SIGNAL( triggered() ), this, SLOT( slot_change_shrinkage() ));
77  menu->addAction(act_scale_cells);
78 
79  // Change rendering
80  render_switch_ = new QAction(tr("Render Boundary Only"), this);
81  render_switch_->setStatusTip(tr("Render Boundary Only"));
82  render_switch_->setCheckable(true);
83  render_switch_->setChecked(false);
84  connect(render_switch_, SIGNAL( triggered() ), this, SLOT( switchRendering() ));
85  menu->addAction(render_switch_);
86 
87  translucency_factor_action_ = new QAction(tr("Set Translucency Factor"), this);
88  translucency_factor_action_->setStatusTip(tr("Set Translucency Factor"));
89  translucency_factor_action_->setCheckable(false);
90  connect(translucency_factor_action_, SIGNAL( triggered() ), this, SLOT( setTranslucencyFactor() ));
91  menu->addAction(translucency_factor_action_);
92 
93  emit addContextMenuItem(menu->menuAction(), DATA_TETRAHEDRAL_MESH, CONTEXTOBJECTMENU);
94 }
95 
96 //----------------------------------------------------------------------------
97 
98 int TypeTetrahedralMeshPlugin::addEmpty() {
99 
100  // New object data struct
102 
103  if (PluginFunctions::objectCount() == 1)
104  object->target(true);
105 
106  if (PluginFunctions::targetCount() == 0)
107  object->target(true);
108 
109  QString name = QString(tr("New Tetrahedral Mesh %1.ovm").arg( object->id() ));
110 
111  // call the local function to update names
112  QFileInfo f(name);
113  object->setName(f.fileName());
114 
115  // enable backface culling
116  object->materialNode()->applyProperties(ACG::SceneGraph::MaterialNode::All);
117  //object->materialNode()->enable_backface_culling();
118 
119  // set the default colors
120  const QColor color = OpenFlipper::Options::defaultColor();
121  const ACG::Vec4f default_color(color.redF(), color.greenF(), color.blueF(), color.alphaF());
122  object->materialNode()->set_color(default_color);
123 
124  // Set rendering props
125  if(OpenFlipper::Options::gui()) {
126  object->meshNode()->set_scaling(0.8);
127 
128  object->update();
129 
130  object->show();
131  }
132 
133  emit log(LOGINFO, object->getObjectinfo());
134 
135  emit emptyObjectAdded(object->id());
136 
137  return object->id();
138 }
139 
140 //----------------------------------------------------------------------------
141 
142 void TypeTetrahedralMeshPlugin::slotKeyEvent(QKeyEvent* _event) {
143 
144  switch (_event->key()) {
145  case Qt::Key_F8:
146  if (_event->modifiers() & Qt::ShiftModifier)
147  slot_change_shrinkage();
148  break;
149  default:
150  break;
151  }
152 }
153 
154 //----------------------------------------------------------------------------
155 
156 void TypeTetrahedralMeshPlugin::slotObjectUpdated(int _identifier, const UpdateType& _type) {
157 
158  if( !_type.contains(UPDATE_ALL) && !_type.contains(UPDATE_GEOMETRY))
159  return;
160 
161  PlaneObject* pobj;
162  if (PluginFunctions::getObject(_identifier, pobj))
163  slot_update_planes_in_scenegraph_node();
164 }
165 
166 //----------------------------------------------------------------------------
167 
168 void TypeTetrahedralMeshPlugin::objectDeleted(int _identifier) {
169 
170  PlaneObject* pobj;
171  if (PluginFunctions::getObject(_identifier, pobj)) {
172  slot_update_planes_in_scenegraph_node(_identifier);
173  }
174 }
175 
176 //----------------------------------------------------------------------------
177 
178 void TypeTetrahedralMeshPlugin::slotUpdateContextMenu(int _objectId) {
179 
180  TetrahedralMeshObject* hmobj;
181  if (PluginFunctions::getObject(_objectId, hmobj)) {
182  render_switch_->setChecked(hmobj->meshNode()->boundary_only());
183  }
184 }
185 
186 //----------------------------------------------------------------------------
187 
188 void TypeTetrahedralMeshPlugin::slot_update_planes_in_scenegraph_node(int _deletedObject) {
189 
190  std::vector<Plane> planes;
191 
192  // collect planes
194  != PluginFunctions::objectsEnd(); ++o_it) {
195 
196  if(o_it->id() == _deletedObject) continue;
197 
202  x /= x.sqrnorm();
203  y /= y.sqrnorm();
204 
205  planes.push_back(Plane(p, n, x, y));
206  }
207 
208  // iterate over all target polyvolmeshes
210  != PluginFunctions::objectsEnd(); ++o_it) {
211 
212  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->clear_cut_planes();
213  for (unsigned int i = 0; i < planes.size(); ++i) {
214  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->add_cut_plane(planes[i]);
215  }
216  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_geometry_changed(true);
217  }
218 
219  emit updateView();
220 }
221 
222 //----------------------------------------------------------------------------
223 
224 void TypeTetrahedralMeshPlugin::switchRendering() {
225 
226  QVariant contextObject = render_switch_->data();
227  int objectId = contextObject.toInt();
228 
229  if(objectId == -1)
230  return;
231 
232  BaseObjectData* bod = 0;
233  if(!PluginFunctions::getObject(objectId, bod))
234  return;
235 
237 
238  if(tetMeshObject) {
239  tetMeshObject->meshNode()->set_boundary_only(render_switch_->isChecked());
240  tetMeshObject->meshNode()->set_geometry_changed(true);
241  }
242 }
243 
244 //----------------------------------------------------------------------------
245 
246 void TypeTetrahedralMeshPlugin::setTranslucencyFactor() {
247 
248  QVariant contextObject = translucency_factor_action_->data();
249  int objectId = contextObject.toInt();
250 
251  if(objectId == -1)
252  return;
253 
254  BaseObjectData* bod = 0;
255  if(!PluginFunctions::getObject(objectId, bod))
256  return;
257 
259 
260 
261  if(tetMeshObject) {
262 
263  bool ok;
264  float val = tetMeshObject->meshNode()->translucency_factor();
265  double factor = QInputDialog::getDouble(0, tr("Set translucency factor"), tr("Factor [0, 1]:"), val,
266  0.0, 1.0, 2, &ok);
267 
268  tetMeshObject->meshNode()->set_translucency_factor((float)factor);
269  }
270 }
271 
272 //----------------------------------------------------------------------------
273 
274 void TypeTetrahedralMeshPlugin::slot_change_shrinkage() {
275 
277  != PluginFunctions::objectsEnd(); ++o_it) {
278  // Popup dialog
279  bool ok;
280  double val = PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->scaling();
281  double scale = QInputDialog::getDouble(0, tr("Set singularity scaling for tet shrinkage"), tr("Size * :"), val,
282  0.0, 1.0, 2, &ok);
283 
284  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_boundary_only(false);
285  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_scaling(scale);
286  }
287  emit updateView();
288 }
289 
ACG::SceneGraph::VolumeMeshNodeT< MeshT > * meshNode()
Get the Scenegraph Mesh Node.
ACG::Vec3d yDirection()
local y direction (multiplied with height)
Definition: PlaneNode.cc:279
Update type class.
Definition: UpdateType.hh:60
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
QString name()
Return a name for the plugin.
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:397
int id() const
Definition: BaseObject.cc:190
VolumeMeshObject< TetrahedralMesh > TetrahedralMeshObject
Typedef for a mesh object containing a polyhedral mesh.
virtual void registerKey(int _key, Qt::KeyboardModifiers _modifiers, QString _description, bool _multiUse=false)
Register a key-combination for your plugin.
Definition: KeyInterface.hh:71
QString getObjectinfo()
Get all Info for the Object as a string.
TetrahedralMeshObject * tetrahedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an TetrahedralMeshObject if possible.
const QStringList ALL_OBJECTS
Iterable object range.
#define DATA_TETRAHEDRAL_MESH
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
#define DATA_PLANE
Definition: Plane.hh:58
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:223
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(1)<< 2)
Geometry updated.
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121
bool contains(const UpdateType &_type) const
Check if this update contains the given UpdateType.
Definition: UpdateType.cc:104
PlaneNode * planeNode(BaseObjectData *_object)
Get a PlaneNode from an object.
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:117
ACG::Vec3d position()
get center position of the plane
Definition: PlaneNode.cc:258
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
virtual void updateView()
Update current view in Main Application.
ACG::Vec3d normal()
get current normal
Definition: PlaneNode.cc:265
int objectCount()
Get the number of available objects.
int targetCount()
Get the number of target objects.
The Menu will be shown when an object was picked.
ACG::Vec3d xDirection()
local x direction (multiplied with width)
Definition: PlaneNode.cc:272