Developer Documentation
TypeHexahedralMesh.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 "TypeHexahedralMesh.hh"
47 
48 #include <QInputDialog>
49 #include <QMenu>
50 
51 TypeHexahedralMeshPlugin::TypeHexahedralMeshPlugin() :
52 render_switch_(0),
53 translucency_factor_action_(0)
54 {
55 }
56 
57 bool TypeHexahedralMeshPlugin::registerType() {
58 
59  addDataType("HexahedralMesh", tr("Hexahedral Volume Mesh"));
60  setTypeIcon("HexahedralMesh", "PolyVolMeshType.png");
61  return true;
62 }
63 
64 //----------------------------------------------------------------------------
65 
66 void TypeHexahedralMeshPlugin::pluginsInitialized() {
67 
68  if(OpenFlipper::Options::nogui()) return;
69 
70  emit registerKey(Qt::Key_F8, Qt::ShiftModifier, "Set scaling of Hex shrinkage");
71 
72  QMenu* menu = new QMenu("Hexahedral Mesh Options");
73 
74  // scaling action in context menu
75  QAction* act_scale_cells = new QAction(tr("Scale cells (Shift-F8)"), this);
76  act_scale_cells->setStatusTip(tr("Scale cells (Shift-F8)"));
77  connect(act_scale_cells, SIGNAL( triggered() ), this, SLOT( slot_change_shrinkage() ));
78  menu->addAction(act_scale_cells);
79 
80  // Change rendering
81  render_switch_ = new QAction(tr("Render Boundary Only"), this);
82  render_switch_->setStatusTip(tr("Render Boundary Only"));
83  render_switch_->setCheckable(true);
84  render_switch_->setChecked(false);
85  connect(render_switch_, SIGNAL( triggered() ), this, SLOT( switchRendering() ));
86  menu->addAction(render_switch_);
87 
88  translucency_factor_action_ = new QAction(tr("Set Translucency Factor"), this);
89  translucency_factor_action_->setStatusTip(tr("Set Translucency Factor"));
90  translucency_factor_action_->setCheckable(false);
91  connect(translucency_factor_action_, SIGNAL( triggered() ), this, SLOT( setTranslucencyFactor() ));
92  menu->addAction(translucency_factor_action_);
93 
94  emit addContextMenuItem(menu->menuAction(), DATA_HEXAHEDRAL_MESH, CONTEXTOBJECTMENU);
95 }
96 
97 //----------------------------------------------------------------------------
98 
99 int TypeHexahedralMeshPlugin::addEmpty() {
100 
101  // New object data struct
103 
104  if ( OpenFlipperSettings().value("Core/File/AllTarget",false).toBool() )
105  object->target(true);
106  else {
107 
108  // Only the first object in the scene will be target
109  if ( PluginFunctions::objectCount() == 1 )
110  object->target(true);
111 
112  // If no target is available, we set the new object as target
113  if (PluginFunctions::targetCount() == 0 )
114  object->target(true);
115  }
116 
117  QString name = QString(tr("New Hexahedral Mesh %1.ovm").arg( object->id() ));
118 
119  // call the local function to update names
120  QFileInfo f(name);
121  object->setName(f.fileName());
122 
123  // enable backface culling
124  object->materialNode()->applyProperties(ACG::SceneGraph::MaterialNode::All);
125  //object->materialNode()->enable_backface_culling();
126 
127  // set the default colors
128  const QColor color = OpenFlipper::Options::defaultColor();
129  const ACG::Vec4f default_color(color.redF(), color.greenF(), color.blueF(), color.alphaF());
130  object->materialNode()->set_color(default_color);
131 
132  // Set rendering props
133  if(OpenFlipper::Options::gui()) {
134  object->meshNode()->set_scaling(0.8);
135 
136  object->update();
137 
138  object->show();
139  }
140 
141  emit log(LOGINFO, object->getObjectinfo());
142 
143  emit emptyObjectAdded(object->id());
144 
145  return object->id();
146 }
147 
148 //----------------------------------------------------------------------------
149 
150 void TypeHexahedralMeshPlugin::slotKeyEvent(QKeyEvent* _event) {
151 
152  switch (_event->key()) {
153  case Qt::Key_F8:
154  if (_event->modifiers() & Qt::ShiftModifier)
155  slot_change_shrinkage();
156  break;
157  default:
158  break;
159  }
160 }
161 
162 //----------------------------------------------------------------------------
163 
164 void TypeHexahedralMeshPlugin::slotObjectUpdated(int _identifier, const UpdateType& _type) {
165 
166  if( !_type.contains(UPDATE_ALL) && !_type.contains(UPDATE_GEOMETRY))
167  return;
168 
169  PlaneObject* pobj;
170  if (PluginFunctions::getObject(_identifier, pobj))
171  slot_update_planes_in_scenegraph_node();
172 }
173 
174 //----------------------------------------------------------------------------
175 
176 void TypeHexahedralMeshPlugin::objectDeleted(int _identifier) {
177 
178  PlaneObject* pobj;
179  if (PluginFunctions::getObject(_identifier, pobj)) {
180  slot_update_planes_in_scenegraph_node(_identifier);
181  }
182 }
183 
184 //----------------------------------------------------------------------------
185 
186 void TypeHexahedralMeshPlugin::slotUpdateContextMenu(int _objectId) {
187 
188  HexahedralMeshObject* hmobj;
189  if (PluginFunctions::getObject(_objectId, hmobj)) {
190  render_switch_->setChecked(hmobj->meshNode()->boundary_only());
191  }
192 }
193 
194 //----------------------------------------------------------------------------
195 
196 void TypeHexahedralMeshPlugin::slot_update_planes_in_scenegraph_node(int _deletedObject) {
197 
198  std::vector<Plane> planes;
199 
200  // collect planes
202  != PluginFunctions::objectsEnd(); ++o_it) {
203 
204  if(o_it->id() == _deletedObject) continue;
205 
210  x /= x.sqrnorm();
211  y /= y.sqrnorm();
212 
213  planes.push_back(Plane(p, n, x, y));
214  }
215 
216  // iterate over all target polyvolmeshes
218  != PluginFunctions::objectsEnd(); ++o_it) {
219 
220  PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->clear_cut_planes();
221  for (unsigned int i = 0; i < planes.size(); ++i) {
222  PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->add_cut_plane(planes[i]);
223  }
224  PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->set_geometry_changed(true);
225  }
226 
227  emit updateView();
228 }
229 
230 //----------------------------------------------------------------------------
231 
232 void TypeHexahedralMeshPlugin::switchRendering() {
233 
234  QVariant contextObject = render_switch_->data();
235  int objectId = contextObject.toInt();
236 
237  if(objectId == -1)
238  return;
239 
240  BaseObjectData* bod = 0;
241  if(!PluginFunctions::getObject(objectId, bod))
242  return;
243 
245 
246  if(hexMeshObject) {
247  hexMeshObject->meshNode()->set_boundary_only(render_switch_->isChecked());
248  hexMeshObject->meshNode()->set_geometry_changed(true);
249  }
250 }
251 
252 //----------------------------------------------------------------------------
253 
254 void TypeHexahedralMeshPlugin::setTranslucencyFactor() {
255 
256  QVariant contextObject = translucency_factor_action_->data();
257  int objectId = contextObject.toInt();
258 
259  if(objectId == -1)
260  return;
261 
262  BaseObjectData* bod = 0;
263  if(!PluginFunctions::getObject(objectId, bod))
264  return;
265 
267 
268  if(hexMeshObject) {
269 
270  bool ok;
271  float val = hexMeshObject->meshNode()->translucency_factor();
272  double factor = QInputDialog::getDouble(0, tr("Set translucency factor"), tr("Factor [0, 1]:"), val,
273  0.0, 1.0, 2, &ok);
274 
275  hexMeshObject->meshNode()->set_translucency_factor((float)factor);
276  }
277 }
278 
279 //----------------------------------------------------------------------------
280 
281 void TypeHexahedralMeshPlugin::slot_change_shrinkage() {
282 
284  != PluginFunctions::objectsEnd(); ++o_it) {
285  // Popup dialog
286  bool ok;
287  double val = PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->scaling();
288  double scale = QInputDialog::getDouble(0, tr("Set singularity scaling for hex shrinkage"), tr("Size * :"), val,
289  0.0, 1.0, 2, &ok);
290 
291  PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->set_boundary_only(false);
292  PluginFunctions::hexahedralMeshObject(*o_it)->meshNode()->set_scaling(scale);
293  }
294  emit updateView();
295 }
296 
ACG::SceneGraph::VolumeMeshNodeT< MeshT > * meshNode()
Get the Scenegraph Mesh Node.
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
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.
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:397
int id() const
Definition: BaseObject.cc:190
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.
const QStringList ALL_OBJECTS
Iterable object range.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
#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
HexahedralMeshObject * hexahedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an HexahedralMeshObject if possible.
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.
#define DATA_HEXAHEDRAL_MESH
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.
VolumeMeshObject< HexahedralMesh > HexahedralMeshObject
Typedef for a mesh object containing a polyhedral mesh.
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