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