Developer Documentation
TypeTetrahedralMesh.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen *
5  * www.openflipper.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenFlipper. *
9  * *
10  * OpenFlipper is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenFlipper is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenFlipper. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33  \*===========================================================================*/
34 
35 /*===========================================================================*\
36 * *
37 * $Revision$ *
38 * $LastChangedBy$ *
39 * $Date$ *
40 * *
41 \*===========================================================================*/
42 
43 #include "TypeTetrahedralMesh.hh"
47 
48 TypeTetrahedralMeshPlugin::TypeTetrahedralMeshPlugin() :
49 render_switch_(0),
50 translucency_factor_action_(0)
51 {
52 }
53 
54 bool TypeTetrahedralMeshPlugin::registerType() {
55 
56  addDataType("TetrahedralMesh", tr("Tetrahedral Volume Mesh"));
57  setTypeIcon("TetrahedralMesh", "PolyVolMeshType.png");
58  return true;
59 }
60 
61 //----------------------------------------------------------------------------
62 
63 void TypeTetrahedralMeshPlugin::pluginsInitialized() {
64 
65  if(OpenFlipper::Options::nogui()) return;
66 
67  emit registerKey(Qt::Key_F8, Qt::ShiftModifier, "Set scaling of Tet shrinkage");
68 
69  QMenu* menu = new QMenu("Tetrahedral Mesh Options");
70 
71  // scaling action in context menu
72  QAction* act_scale_cells = new QAction(tr("Scale cells (Shift-F8)"), this);
73  act_scale_cells->setStatusTip(tr("Scale cells (Shift-F8)"));
74  connect(act_scale_cells, SIGNAL( triggered() ), this, SLOT( slot_change_shrinkage() ));
75  menu->addAction(act_scale_cells);
76 
77  // Change rendering
78  render_switch_ = new QAction(tr("Render Boundary Only"), this);
79  render_switch_->setStatusTip(tr("Render Boundary Only"));
80  render_switch_->setCheckable(true);
81  render_switch_->setChecked(false);
82  connect(render_switch_, SIGNAL( triggered() ), this, SLOT( switchRendering() ));
83  menu->addAction(render_switch_);
84 
85  translucency_factor_action_ = new QAction(tr("Set Translucency Factor"), this);
86  translucency_factor_action_->setStatusTip(tr("Set Translucency Factor"));
87  translucency_factor_action_->setCheckable(false);
88  connect(translucency_factor_action_, SIGNAL( triggered() ), this, SLOT( setTranslucencyFactor() ));
89  menu->addAction(translucency_factor_action_);
90 
91  emit addContextMenuItem(menu->menuAction(), DATA_TETRAHEDRAL_MESH, CONTEXTOBJECTMENU);
92 }
93 
94 //----------------------------------------------------------------------------
95 
96 int TypeTetrahedralMeshPlugin::addEmpty() {
97 
98  // New object data struct
99  TetrahedralMeshObject* object = new TetrahedralMeshObject(DATA_TETRAHEDRAL_MESH);
100 
101  if (PluginFunctions::objectCount() == 1)
102  object->target(true);
103 
104  if (PluginFunctions::targetCount() == 0)
105  object->target(true);
106 
107  QString name = QString(tr("New Tetrahedral Mesh %1.ovm").arg( object->id() ));
108 
109  // call the local function to update names
110  QFileInfo f(name);
111  object->setName(f.fileName());
112 
113  // enable backface culling
114  object->materialNode()->applyProperties(ACG::SceneGraph::MaterialNode::All);
115  //object->materialNode()->enable_backface_culling();
116 
117  // set the default colors
118  const QColor color = OpenFlipper::Options::defaultColor();
119  const ACG::Vec4f default_color(color.redF(), color.greenF(), color.blueF(), color.alphaF());
120  object->materialNode()->set_color(default_color);
121 
122  // Set rendering props
123  if(OpenFlipper::Options::gui()) {
124  object->meshNode()->set_scaling(0.8);
125 
126  object->update();
127 
128  object->show();
129  }
130 
131  emit log(LOGINFO, object->getObjectinfo());
132 
133  emit emptyObjectAdded(object->id());
134 
135  return object->id();
136 }
137 
138 //----------------------------------------------------------------------------
139 
140 void TypeTetrahedralMeshPlugin::slotKeyEvent(QKeyEvent* _event) {
141 
142  switch (_event->key()) {
143  case Qt::Key_F8:
144  if (_event->modifiers() & Qt::ShiftModifier)
145  slot_change_shrinkage();
146  break;
147  default:
148  break;
149  }
150 }
151 
152 //----------------------------------------------------------------------------
153 
154 void TypeTetrahedralMeshPlugin::slotObjectUpdated(int _identifier, const UpdateType& _type) {
155 
156  if( !_type.contains(UPDATE_ALL) && !_type.contains(UPDATE_GEOMETRY))
157  return;
158 
159  PlaneObject* pobj;
160  if (PluginFunctions::getObject(_identifier, pobj))
161  slot_update_planes_in_scenegraph_node();
162 }
163 
164 //----------------------------------------------------------------------------
165 
166 void TypeTetrahedralMeshPlugin::objectDeleted(int _identifier) {
167 
168  PlaneObject* pobj;
169  if (PluginFunctions::getObject(_identifier, pobj)) {
170  slot_update_planes_in_scenegraph_node(_identifier);
171  }
172 }
173 
174 //----------------------------------------------------------------------------
175 
176 void TypeTetrahedralMeshPlugin::slotUpdateContextMenu(int _objectId) {
177 
178  TetrahedralMeshObject* hmobj;
179  if (PluginFunctions::getObject(_objectId, hmobj)) {
180  render_switch_->setChecked(hmobj->meshNode()->boundary_only());
181  }
182 }
183 
184 //----------------------------------------------------------------------------
185 
186 void TypeTetrahedralMeshPlugin::slot_update_planes_in_scenegraph_node(int _deletedObject) {
187 
189  std::vector<Plane> planes;
190 
191  // collect planes
193  != PluginFunctions::objectsEnd(); ++o_it) {
194 
195  if(o_it->id() == _deletedObject) continue;
196 
201  x /= x.sqrnorm();
202  y /= y.sqrnorm();
203 
204  planes.push_back(Plane(p, n, x, y));
205  }
206 
207  // iterate over all target polyvolmeshes
208  for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DATA_TETRAHEDRAL_MESH); o_it
209  != PluginFunctions::objectsEnd(); ++o_it) {
210 
211  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->clear_cut_planes();
212  for (unsigned int i = 0; i < planes.size(); ++i) {
213  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->add_cut_plane(planes[i]);
214  }
215  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_geometry_changed(true);
216  }
217 
218  emit updateView();
219 }
220 
221 //----------------------------------------------------------------------------
222 
223 void TypeTetrahedralMeshPlugin::switchRendering() {
224 
225  QVariant contextObject = render_switch_->data();
226  int objectId = contextObject.toInt();
227 
228  if(objectId == -1)
229  return;
230 
231  BaseObjectData* bod = 0;
232  if(!PluginFunctions::getObject(objectId, bod))
233  return;
234 
235  TetrahedralMeshObject* tetMeshObject = dynamic_cast<TetrahedralMeshObject*>(bod);
236 
237  if(tetMeshObject) {
238  tetMeshObject->meshNode()->set_boundary_only(render_switch_->isChecked());
239  tetMeshObject->meshNode()->set_geometry_changed(true);
240  }
241 }
242 
243 //----------------------------------------------------------------------------
244 
245 void TypeTetrahedralMeshPlugin::setTranslucencyFactor() {
246 
247  QVariant contextObject = translucency_factor_action_->data();
248  int objectId = contextObject.toInt();
249 
250  if(objectId == -1)
251  return;
252 
253  BaseObjectData* bod = 0;
254  if(!PluginFunctions::getObject(objectId, bod))
255  return;
256 
257  TetrahedralMeshObject* tetMeshObject = dynamic_cast<TetrahedralMeshObject*>(bod);
258 
259  if(tetMeshObject) {
260 
261  bool ok;
262  float val = tetMeshObject->meshNode()->translucency_factor();
263  double factor = QInputDialog::getDouble(0, tr("Set translucency factor"), tr("Factor [0, 1]:"), val,
264  0.0, 1.0, 2, &ok);
265 
266  tetMeshObject->meshNode()->set_translucency_factor((float)factor);
267  }
268 }
269 
270 //----------------------------------------------------------------------------
271 
272 void TypeTetrahedralMeshPlugin::slot_change_shrinkage() {
273 
274  for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DATA_TETRAHEDRAL_MESH); o_it
275  != PluginFunctions::objectsEnd(); ++o_it) {
276  // Popup dialog
277  bool ok;
278  double val = PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->scaling();
279  double scale = QInputDialog::getDouble(0, tr("Set singularity scaling for tet shrinkage"), tr("Size * :"), val,
280  0.0, 1.0, 2, &ok);
281 
282  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_boundary_only(false);
283  PluginFunctions::tetrahedralMeshObject(*o_it)->meshNode()->set_scaling(scale);
284  }
285  emit updateView();
286 }
287 
288 #if QT_VERSION < 0x050000
289  Q_EXPORT_PLUGIN2( typetetrahedralmeshplugin , TypeTetrahedralMeshPlugin );
290 #endif
QString name()
Return a name for the plugin.
ACG::Vec3d xDirection()
local x direction (multiplied with width)
Definition: PlaneNode.cc:272
bool getObject(int _identifier, BSplineCurveObject *&_object)
The Menu will be shown when an object was picked.
const QStringList ALL_OBJECTS
Iterable object range.
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:128
Update type class.
Definition: UpdateType.hh:70
bool contains(const UpdateType &_type) const
Check if this update contains the given UpdateType.
Definition: UpdateType.cc:111
int objectCount()
Get the number of available objects.
PlaneNode * planeNode(BaseObjectData *_object)
Get a PlaneNode from an object.
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(1)<< 2)
Geometry updated.
ACG::Vec3d position()
get center position of the plane
Definition: PlaneNode.cc:258
ACG::Vec3d normal()
get current normal
Definition: PlaneNode.cc:265
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:234
virtual void updateView()
Update current view in Main Application.
int targetCount()
Get the number of target objects.
decltype(std::declval< S >()*std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:396
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
#define DATA_PLANE
Definition: Plane.hh:64
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:127
ACG::Vec3d yDirection()
local y direction (multiplied with height)
Definition: PlaneNode.cc:279
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.