Developer Documentation
TypeBSplineCurve.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 //=============================================================================
45 //
46 // CLASS Type BSpline Curve Plugin - IMPLEMENTATION
47 //
48 // Author: Ellen Dekkers <dekkers@cs.rwth-aachen.de>
49 //
50 // Date: 2010-02-02 11:02:14 +0100 (Di, 02. Feb 2010)
51 //
52 //=============================================================================
53 
54 
55 #include "TypeBSplineCurve.hh"
56 
58 #include <OpenFlipper/common/BackupData.hh>
60 #include "BSplineCurveBackup.hh"
61 
62 #include <QMenu>
63 
64 //-----------------------------------------------------------------------------
65 
66 TypeBSplineCurvePlugin::
67 TypeBSplineCurvePlugin():
68 renderControlPolygonAction_(0),
69 renderCurveAction_(0),
70 renderCPSelectionAction_(0),
71 renderKnotSelectionAction_(0),
72 renderNoSelectionAction_(0)
73 {
74 }
75 
76 
77 //-----------------------------------------------------------------------------
78 
83 {
84 
85  if ( OpenFlipper::Options::gui() ){
86 
87  QMenu* contextMenu = new QMenu("Rendering");
88 
89  QString iconPath = OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator();
90 
91  // Render Control Net
92  renderControlPolygonAction_ = new QAction(tr("Render Control Polygon"), this);
93  renderControlPolygonAction_->setStatusTip(tr("Render Control Polygon"));
94 // renderControlPolygonAction_->setIcon( QIcon(iconPath + "showIndices.png") );
95  renderControlPolygonAction_->setCheckable(true);
96  renderControlPolygonAction_->setChecked(false);
97 
98  // Render Surface
99  renderCurveAction_ = new QAction(tr("Render Curve"), this);
100  renderCurveAction_->setStatusTip(tr("Render Curve"));
101 // renderCurveAction_->setIcon( QIcon(iconPath + "coordsys.png") );
102  renderCurveAction_->setCheckable(true);
103  renderCurveAction_->setChecked(true);
104 
105  QActionGroup* group = new QActionGroup(this);
106  group->setExclusive(true);
107 
108  // Render Control Point Selection
109  renderCPSelectionAction_ = new QAction(tr("Render Control Point Selection"), group);
110  renderCPSelectionAction_->setStatusTip(tr("Render Control Point Selection"));
111 // renderCPSelectionAction_->setIcon( QIcon(iconPath + "coordsys.png") );
112  renderCPSelectionAction_->setCheckable(true);
113  renderCPSelectionAction_->setChecked(true);
114 
115  // Render Knot Selection
116  renderKnotSelectionAction_ = new QAction(tr("Render Knot Selection"), group);
117  renderKnotSelectionAction_->setStatusTip(tr("Render Knot Selection"));
118 // renderKnotSelectionAction_->setIcon( QIcon(iconPath + "coordsys.png") );
119  renderKnotSelectionAction_->setCheckable(true);
120  renderKnotSelectionAction_->setChecked(true);
121 
122  // Render No Selection
123  renderNoSelectionAction_ = new QAction(tr("Don't Render Selection"), group);
124  renderNoSelectionAction_->setStatusTip(tr("Don't Render Selection"));
125 // renderNoSelectionAction_->setIcon( QIcon(iconPath + "coordsys.png") );
126  renderNoSelectionAction_->setCheckable(true);
127  renderNoSelectionAction_->setChecked(true);
128 
129 
130  connect(renderControlPolygonAction_, SIGNAL(triggered()), this, SLOT(slotRenderControlPolygon()) );
131  connect(renderCurveAction_, SIGNAL(triggered()), this, SLOT(slotRenderCurve()) );
132 
133  connect(group, SIGNAL(triggered(QAction*)), this, SLOT(slotRenderSelection(QAction*)));
134 
135  contextMenu->addAction(renderControlPolygonAction_);
136  contextMenu->addAction(renderCurveAction_);
137  contextMenu->addSeparator();
138  contextMenu->addAction(renderCPSelectionAction_);
139  contextMenu->addAction(renderKnotSelectionAction_);
140  contextMenu->addAction(renderNoSelectionAction_);
141 
142  emit addContextMenuItem(contextMenu->menuAction(), DATA_BSPLINE_CURVE, CONTEXTOBJECTMENU);
143  }
144 }
145 
146 //-----------------------------------------------------------------------------
147 
148 void
150 slotUpdateContextMenu( int _objectId ) {
151  if ( _objectId == -1)
152  return;
153 
154  BaseObjectData* object;
155  if ( !PluginFunctions::getObject(_objectId,object) )
156  return;
157 
158  BSplineCurveObject* bsplineCurveObject = dynamic_cast<BSplineCurveObject*>(object);
159 
160  if(bsplineCurveObject != 0){
161  renderControlPolygonAction_->setChecked( bsplineCurveObject->splineCurveNode()->render_control_polygon() );
162  renderCurveAction_->setChecked( bsplineCurveObject->splineCurveNode()->render_bspline_curve() );
163  renderCPSelectionAction_->setChecked( bsplineCurveObject->splineCurveNode()->get_selection_draw_mode() == ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::CONTROLPOINT );
164  renderKnotSelectionAction_->setChecked( bsplineCurveObject->splineCurveNode()->get_selection_draw_mode() == ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::KNOTVECTOR );
165  renderNoSelectionAction_->setChecked( bsplineCurveObject->splineCurveNode()->get_selection_draw_mode() == ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::NONE );
166  }
167 }
168 
169 //-----------------------------------------------------------------------------
170 
172 
173  QVariant contextObject = renderControlPolygonAction_->data();
174  int objectId = contextObject.toInt();
175 
176  if ( objectId == -1)
177  return;
178 
179  BaseObjectData* object;
180  if ( !PluginFunctions::getObject(objectId,object) )
181  return;
182 
183  BSplineCurveObject* bsplineCurveObject = dynamic_cast<BSplineCurveObject*>(object);
184 
185  if(bsplineCurveObject != 0){
186  bsplineCurveObject->splineCurveNode()->render_control_polygon(renderControlPolygonAction_->isChecked());
187  emit updatedObject( objectId, UPDATE_ALL );
188  }
189 }
190 
191 //-----------------------------------------------------------------------------
192 
194 
195  QVariant contextObject = renderCurveAction_->data();
196  int objectId = contextObject.toInt();
197 
198  if ( objectId == -1)
199  return;
200 
201  BaseObjectData* object;
202  if ( !PluginFunctions::getObject(objectId,object) )
203  return;
204 
205  BSplineCurveObject* bsplineCurveObject = dynamic_cast<BSplineCurveObject*>(object);
206 
207  if(bsplineCurveObject != 0){
208  bsplineCurveObject->splineCurveNode()->render_bspline_curve(renderCurveAction_->isChecked());
209  emit updatedObject( objectId, UPDATE_ALL );
210  }
211 }
212 
213 //-----------------------------------------------------------------------------
214 
216 
217  QVariant contextObject = _action->data();
218  int objectId = contextObject.toInt();
219 
220  if ( objectId == -1)
221  return;
222 
223  BaseObjectData* object;
224  if ( !PluginFunctions::getObject(objectId,object) )
225  return;
226 
227  BSplineCurveObject* bsplineCurveObject = dynamic_cast<BSplineCurveObject*>(object);
228 
229  if(bsplineCurveObject != 0){
230  if(_action == renderCPSelectionAction_) {
231  bsplineCurveObject->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::CONTROLPOINT);
232  emit updatedObject( objectId, UPDATE_ALL );
233  } else if(_action == renderKnotSelectionAction_) {
234  bsplineCurveObject->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::KNOTVECTOR);
235  emit updatedObject( objectId, UPDATE_ALL );
236  } else if(_action == renderNoSelectionAction_) {
237  bsplineCurveObject->splineCurveNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineCurveNodeT<BSplineCurve>::NONE);
238  emit updatedObject( objectId, UPDATE_ALL );
239  }
240  }
241 }
242 
243 //-----------------------------------------------------------------------------
244 
245 bool
246 TypeBSplineCurvePlugin::
247 registerType()
248 {
249  addDataType("BSplineCurve",tr("B-Spline Curve"));
250  setTypeIcon("BSplineCurve", "BSplineCurveType.png");
251  return true;
252 }
253 
254 //-----------------------------------------------------------------------------
255 
256 DataType
259 {
261  return type;
262 }
263 
264 //-----------------------------------------------------------------------------
265 
266 int
269 {
270  // new object data struct
271  BSplineCurveObject * object = new BSplineCurveObject();
272 
273 
274  if ( OpenFlipperSettings().value("Core/File/AllTarget",false).toBool() )
275  object->target(true);
276  else {
277 
278  // Only the first object in the scene will be target
279  if ( PluginFunctions::objectCount() == 1 )
280  object->target(true);
281 
282  // If no target is available, we set the new object as target
283  if (PluginFunctions::targetCount() == 0 )
284  object->target(true);
285  }
286 
287  QString name = "BSplineCurve_" + QString::number( object->id() ) + ".bsc";
288 
289  // call the local function to update names
290  QFileInfo f(name);
291  object->setName( f.fileName() );
292 
293  // set the default colors
294  const QColor color = OpenFlipper::Options::defaultColor();
295  const ACG::Vec4f default_color(color.redF(), color.greenF(), color.blueF(), color.alphaF());
296  object->materialNode()->set_color(default_color);
297 
298  object->update();
299 
300  object->show();
301 
302  emit log(LOGINFO,object->getObjectinfo());
303 
304  emit emptyObjectAdded (object->id() );
305 
306  return object->id();
307 }
308 
309 //-----------------------------------------------------------------------------
310 
311 void TypeBSplineCurvePlugin::generateBackup( int _id, QString _name, UpdateType _type ){
312 
313  BaseObjectData* object = 0;
314  PluginFunctions::getObject(_id, object);
315 
317 
318  if ( splineObj != 0 ){
319 
320  //get backup object data
321  BackupData* backupData = 0;
322 
323  if ( object->hasObjectData( OBJECT_BACKUPS ) )
324  backupData = dynamic_cast< BackupData* >(object->objectData(OBJECT_BACKUPS));
325  else{
326  //add backup data
327  backupData = new BackupData(object);
328  object->setObjectData(OBJECT_BACKUPS, backupData);
329  }
330 
331  //store a new backup
332  BSplineCurveBackup* backup = new BSplineCurveBackup(splineObj, _name, _type);
333  backupData->storeBackup( backup );
334  }
335 }
336 
337 //-----------------------------------------------------------------------------
338 
339 
340 
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
Update type class.
Definition: UpdateType.hh:60
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
int id() const
Definition: BaseObject.cc:190
QString getObjectinfo()
Get all Info for the Object as a string.
QString name()
Get the backups name)
Definition: BaseBackup.cc:136
void slotUpdateContextMenu(int _objectId)
BSplineCurveObject * bsplineCurveObject(BaseObjectData *_object)
Cast an BaseObject to a BSplineCurveObject if possible.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:223
void pluginsInitialized()
Second initialization phase.
ACG::SceneGraph::BSplineCurveNodeT< BSplineCurve > * splineCurveNode()
Get the scenegraph Node.
Class that encapsulates a backup.
Abstract class that is used to store backups.
Definition: BackupData.hh:57
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:117
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
void storeBackup(BaseBackup *_backup)
store a backup
Definition: BackupData.cc:72
int addEmpty()
Create an empty object.
Predefined datatypes.
Definition: DataTypes.hh:83
bool hasObjectData(QString _dataName)
Checks if object data with given name is available.
Definition: BaseObject.cc:795
void slotRenderSelection(QAction *_action)
Slot triggered from context menu, if the selection rendering should be altered.
PerObjectData * objectData(QString _dataName)
Returns the object data pointer.
Definition: BaseObject.cc:803
int objectCount()
Get the number of available objects.
void slotRenderCurve()
Slot triggered from context menu, if the curve should be rendered.
int targetCount()
Get the number of target objects.
#define DATA_BSPLINE_CURVE
Definition: BSplineCurve.hh:67
The Menu will be shown when an object was picked.
void slotRenderControlPolygon()
Slot triggered from context menu, if the control polygon should be rendered.