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"
44 #include <ObjectTypes/Plane/Plane.hh>
45 #include <OpenFlipper/common/GlobalOptions.hh>
46 #include <OpenFlipper/BasePlugin/PluginFunctions.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
102 HexahedralMeshObject* object = new HexahedralMeshObject(DATA_HEXAHEDRAL_MESH);
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
201 for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DATA_PLANE); o_it
202 != PluginFunctions::objectsEnd(); ++o_it) {
203
204 if(o_it->id() == _deletedObject) continue;
205
206 ACG::Vec3d p = (ACG::Vec3d) PluginFunctions::planeNode(*o_it)->position();
207 ACG::Vec3d n = (ACG::Vec3d) PluginFunctions::planeNode(*o_it)->normal();
208 ACG::Vec3d x = (ACG::Vec3d) PluginFunctions::planeNode(*o_it)->xDirection();
209 ACG::Vec3d y = (ACG::Vec3d) PluginFunctions::planeNode(*o_it)->yDirection();
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
217 for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DATA_HEXAHEDRAL_MESH); o_it
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
244 HexahedralMeshObject* hexMeshObject = PluginFunctions::hexahedralMeshObject(bod);
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
266 HexahedralMeshObject* hexMeshObject = PluginFunctions::hexahedralMeshObject(bod);
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
283 for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DATA_HEXAHEDRAL_MESH); o_it
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
VolumeMeshObject::meshNode
ACG::SceneGraph::VolumeMeshNodeT< MeshT > * meshNode()
Get the Scenegraph Mesh Node.
Definition: VolumeMeshObjectT_impl.hh:325
OpenFlipperSettings
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
Definition: GlobalOptions.cc:1038
PlaneNode::yDirection
ACG::Vec3d yDirection()
local y direction (multiplied with height)
Definition: PlaneNode.cc:279
LOGINFO
Definition: LoggingInterface.hh:94
UpdateType
Update type class.
Definition: UpdateType.hh:60
UPDATE_ALL
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
OpenMesh::VectorT::sqrnorm
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:397
BaseObject::id
int id() const
Definition: BaseObject.cc:190
PluginFunctions.hh
KeyInterface::registerKey
virtual void registerKey(int _key, Qt::KeyboardModifiers _modifiers, QString _description, bool _multiUse=false)
Register a key-combination for your plugin.
Definition: KeyInterface.hh:71
VolumeMeshObject::getObjectinfo
QString getObjectinfo()
Get all Info for the Object as a string.
Definition: VolumeMeshObjectT_impl.hh:352
VolumeMeshObject
Definition: VolumeMeshObject.hh:63
PluginFunctions::ALL_OBJECTS
const QStringList ALL_OBJECTS
Iterable object range.
Definition: PluginFunctions.hh:430
GlobalOptions.hh
ACG::SceneGraph::MaterialNode::All
apply all properites
Definition: MaterialNode.hh:348
PluginFunctions::getObject
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Definition: PluginFunctions.cc:294
BaseObject::name
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
PlaneObject
Definition: PlaneObject.hh:75
DATA_PLANE
#define DATA_PLANE
Definition: Plane.hh:58
setTypeIcon
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:223
PluginFunctions::hexahedralMeshObject
HexahedralMeshObject * hexahedralMeshObject(BaseObjectData *_object)
Cast an BaseObject to an HexahedralMeshObject if possible.
Definition: PluginFunctionsHexahedralMesh.cc:133
UPDATE_GEOMETRY
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(1)<< 2)
Geometry updated.
ACG::Vec3d
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121
UpdateType::contains
bool contains(const UpdateType &_type) const
Check if this update contains the given UpdateType.
Definition: UpdateType.cc:104
PluginFunctions::planeNode
PlaneNode * planeNode(BaseObjectData *_object)
Get a PlaneNode from an object.
Definition: PluginFunctionsPlane.cc:82
DATA_HEXAHEDRAL_MESH
#define DATA_HEXAHEDRAL_MESH
Definition: HexahedralMesh.hh:56
addDataType
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:117
PlaneNode::position
ACG::Vec3d position()
get center position of the plane
Definition: PlaneNode.cc:258
PluginFunctions::ObjectIterator
Core Data Iterator.
Definition: PluginFunctions.hh:441
PluginFunctions::objectsEnd
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
Definition: PluginFunctionsIterator.cc:177
BaseInterface::updateView
virtual void updateView()
Update current view in Main Application.
Definition: BaseInterface.hh:122
OpenMesh::VectorT
Definition: Vector11T.hh:83
HexahedralMeshObject
VolumeMeshObject< HexahedralMesh > HexahedralMeshObject
Typedef for a mesh object containing a polyhedral mesh.
Definition: HexahedralMesh.hh:64
PlaneNode::normal
ACG::Vec3d normal()
get current normal
Definition: PlaneNode.cc:265
Plane.hh
PluginFunctions::objectCount
int objectCount()
Get the number of available objects.
Definition: PluginFunctions.cc:1000
BaseObjectData
Definition: BaseObjectData.hh:102
PluginFunctions::targetCount
int targetCount()
Get the number of target objects.
Definition: PluginFunctions.cc:1006
CONTEXTOBJECTMENU
The Menu will be shown when an object was picked.
Definition: ContextMenuInterface.hh:59
PlaneNode::xDirection
ACG::Vec3d xDirection()
local x direction (multiplied with width)
Definition: PlaneNode.cc:272
ACG::Geometry::Plane
Definition: PlaneType.hh:50