Developer documentation | Axl-2.5.1

axlActorPoint.cpp
Go to the documentation of this file.
1 /* axlActorPoint.cpp ---
2  *
3  * Author: Meriadeg Perrinel
4  * Copyright (C) 2008 - Meriadeg Perrinel, Inria.
5  * Created: Fri Dec 17 10:59:21 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Mon Dec 17 15:02:23 2012 (+0100)
8  * By: Julien Wintz
9  * Update #: 30
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 #include "axlActorPoint.h"
20 
21 #include "axlActorComposite.h"
23 
24 #include <axlCore/axlPoint.h>
25 
26 #include <QVTKOpenGLWidget.h>
27 
28 #include <vtkActor.h>
29 #include <vtkAssemblyPath.h>
30 //#include <vtkCellPicker.h>
31 #include <vtkCommand.h>
32 #include <vtkGlyph3D.h>
33 #include <vtkInteractorStyleSwitch.h>
34 #include <vtkLight.h>
35 #include <vtkLightCollection.h>
36 #include <vtkObjectFactory.h>
37 #include <vtkPoints.h>
38 #include <vtkPointWidget.h>
39 #include <vtkPolyData.h>
40 #include <vtkPolyDataMapper.h>
41 #include <vtkProperty.h>
42 #include <vtkRenderer.h>
43 #include <vtkRendererCollection.h>
44 #include <vtkRenderWindow.h>
45 #include <vtkRenderWindowInteractor.h>
46 #include <vtkSmartPointer.h>
47 #include <vtkSphereSource.h>
48 #include <vtkTransform.h>
49 
50 
51 class axlActorPointObserver : public vtkCommand
52 {
53 public:
55 // vtkCellPicker *axlPointPicker;
58  vtkSphereSource *observerData_sphereSource;
59 
60  vtkPointWidget *pointWidget = nullptr;
61 
62 public:
63  static axlActorPointObserver *New(void)
64  {
65  return new axlActorPointObserver;
66  }
67 
68  virtual void Execute(vtkObject *caller, unsigned long event, void *)
69  {
70  vtkRenderWindowInteractor *interactor = observerDataAssembly->getInteractor(); //interactor->Render();
71 
72  if(event == vtkCommand::InteractionEvent) {
73  observerData_sphereSource->Update();
74  observerData_sphereSource->Modified();
75  interactor->Render();
76 
77  if (!pointWidget)
78  return;
79 
80  if(caller == pointWidget) {
81  observerData_point->setCoordinates(pointWidget->GetPosition()[0], pointWidget->GetPosition()[1], pointWidget->GetPosition()[2]);
82  observerData_point->touchGeometry();
83  observerDataAssembly->onUpdateGeometry();
84  }
85  }
86  }
87 };
88 
89 // /////////////////////////////////////////////////////////////////
90 // axlActorPointPrivate
91 // /////////////////////////////////////////////////////////////////
92 
93 class axlActorPointPrivate
94 {
95 public:
96  axlPoint *point;
97  vtkSphereSource *sphereSource;
98  vtkPointWidget *pointWidget;
99  axlActorPointObserver *pointObserver;
100  QVTKOpenGLWidget *widget;
101 // vtkCellPicker *axlPointPicker ;
102 };
103 
104 #if (VTK_MAJOR_VERSION <= 5)
105 vtkCxxRevisionMacro(axlActorPoint, "$Revision: 0.0.1 $");
106 #endif
107 
109 
110 dtkAbstractData *axlActorPoint::data(void)
111 {
112  return d->point;
113 }
114 
115 vtkSphereSource *axlActorPoint::sphere(void)
116 {
117  return d->sphereSource;
118 }
119 
120 void axlActorPoint::setQVTKWidget(QVTKOpenGLWidget *widget)
121 {
122  d->widget = widget;
123 }
124 
125 
126 void axlActorPoint::setData(dtkAbstractData *point1)
127 {
128  axlPoint *point = dynamic_cast<axlPoint *>(point1);
129 
130  if(point)
131  {
132  d->point = point;
133  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
134  this->setActor(vtkSmartPointer<vtkActor>::New());
135 
136  d->sphereSource = vtkSphereSource::New();
137  d->sphereSource->SetPhiResolution(20);
138  d->sphereSource->SetThetaResolution(20);
139 
140  // connection of data to actor
141 #if (VTK_MAJOR_VERSION <= 5)
142  this->getMapper()->SetInput(d->sphereSource->GetOutput());
143 #else
144  this->getMapper()->SetInputData(d->sphereSource->GetOutput());
145 #endif
146  this->getActor()->SetMapper(this->getMapper());
147 
148  this->AddPart(this->getActor());
149 
150  if(!d->pointObserver)
151  {
152 // d->axlPointPicker = vtkCellPicker::New();
153  d->pointObserver = axlActorPointObserver::New() ;
154  this->getInteractor()->AddObserver(vtkCommand::InteractionEvent, d->pointObserver);
155  this->getInteractor()->AddObserver(vtkCommand::MouseMoveEvent, d->pointObserver);
156 
157  d->pointObserver->observerDataAssembly = this;
158 // d->pointObserver->axlPointPicker = d->axlPointPicker;
159  d->pointObserver->observerData_sphereSource = d->sphereSource;
160  d->pointObserver->observerData_point = d->point;
161  d->pointObserver->axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *> (this->getInteractor()->GetInteractorStyle());
162  }
163 
164  QColor color = d->point->color();
165  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
166 
167  QString shader = d->point->shader();
168  if(!shader.isEmpty())
169  this->setShader(shader);
170 
171  this->setOpacity(d->point->opacity());
172 
173  this->onUpdateGeometry();
174  connect(d->point, SIGNAL(modifiedGeometry()), this, SLOT(onUpdateGeometry()));
175  connect(d->point, SIGNAL(modifiedProperty()), this, SLOT(onUpdateProperty()));
176  }
177  else
178  qDebug()<< "no axlPoint available";
179 }
180 
181 void axlActorPoint::setDisplay(bool display)
182 {
183  axlActor::setDisplay(display);
184 
185  if (display && d->pointWidget) {
186  this->showPointWidget(true);
187  }
188 
189  if (!display && d->pointWidget) {
190  this->showPointWidget(false);
191  }
192 }
193 
195 {
196  if (!d->pointWidget) {
197  qDebug() << "No tet actor computed for this axlActorPoint.";
198  return;
199  }
200 
201  if (show) {
202  d->pointWidget->SetEnabled(1);
203  }
204 
205  if (!show) {
206  d->pointWidget->SetEnabled(0);
207  }
208 }
209 
210 void axlActorPoint::setPointWidget(bool pointWidget)
211 {
212  if (pointWidget) {
213  if (!d->pointWidget) {
214  //widget drawing
215  d->pointWidget = vtkPointWidget::New();
216  d->pointWidget->SetInteractor(this->getInteractor());
217  d->pointWidget->SetProp3D(this->getActor());
218  d->pointWidget->PlaceWidget();
219  d->pointWidget->AllOff();
220  d->pointWidget->SetTranslationMode(1);
221 
222  d->pointWidget->SetPosition(d->sphereSource->GetCenter());
223 
224  }
225 
226  if(d->pointObserver) {
227  d->pointWidget->AddObserver(vtkCommand::InteractionEvent, d->pointObserver);
228  d->pointObserver->pointWidget = d->pointWidget;
229  }
230 
231  // there is always the controlPoints there
232  d->pointWidget->SetEnabled(true);
233  }
234  else {// (!pointWidget)
235  if (this->getActor()) {
236  // this->getMapper()->SetInput(this->getPolyData());
237 
238  if(d->pointWidget) {
239  d->pointWidget->RemoveAllObservers();
240  d->pointWidget->SetEnabled(false);
241  d->pointWidget->Delete(); // warning not sure
242  d->pointWidget = NULL;
243  }
244  }
245  }
246  if (d->pointObserver)
247  d->pointObserver->pointWidget = d->pointWidget;
248 }
249 
250 
251 
253 {
254 
255  if(!d->pointWidget) {
256  qDebug() << "No tet actor computed for this axlActorBSpline.";
257  return false;
258  }
259 
260  return d->pointWidget->GetEnabled();
261 }
262 
263 
264 void axlActorPoint::setMode(int state)
265 {
266  this->onModeChanged(state);
267 
268  emit stateChanged(this->data(), state);
269 }
270 
272 {
273  if(state == 0)
274  {
276  this->getActor()->SetPickable(1);
277 
278  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
279  {
280  QColor color = data->color();
281  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
282  }
283  this->setPointWidget(false);
284 
285 
286  }
287  else if(state == 1)
288  {
289 
291  this->setPointWidget(false);
292  this->getActor()->SetPickable(1);
293  vtkProperty *prop = this->getActor()->GetProperty();
294 
295  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
296  {
297  QColor color = data->color();
298  qreal *h = new qreal(0.0);
299  qreal *s = new qreal(0.0);
300  qreal *l = new qreal(0.0);
301  color.getHslF(h, s, l);
302  color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
303  prop->SetColor(color.redF(), color.greenF(), color.blueF());
304  delete l;
305  delete s;
306  delete h;
307  }
308 
309  }
310  else if(state == 2)
311  {
313  this->setPointWidget(true);
314  this->getActor()->SetPickable(0);
315  vtkProperty *prop = this->getActor()->GetProperty();
316  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
317  {
318  QColor color = data->color();
319  qreal *h = new qreal(0.0);
320  qreal *s = new qreal(0.0);
321  qreal *l = new qreal(0.0);
322  color.getHslF(h, s, l);
323  color.setHslF(*h, *s, *l + (1.0 - *l)/2.0);
324  prop->SetColor(color.redF(), color.greenF(), color.blueF());
325  delete l;
326  delete s;
327  delete h;
328  }
329  }
330 
331  this->Modified();
332 }
333 
334 
336 {
337  // if on edit mode, change to selection mode (for stability)
338  if (this->getState() == 2)
339  setMode(1);
340 
341  //remove point specificity
342  if(d->pointObserver)
343  {
344  this->getInteractor()->RemoveObservers(vtkCommand::InteractionEvent, d->pointObserver);
345  this->getInteractor()->RemoveObservers(vtkCommand::MouseMoveEvent, d->pointObserver);
346  d->pointObserver->observerDataAssembly = NULL;
347 // d->pointObserver->axlPointPicker = NULL;
348  d->pointObserver->observerData_sphereSource = NULL;
349  d->pointObserver->observerData_point = NULL;
350  d->pointObserver->axlInteractorStyle = NULL;
351  d->pointObserver->Delete();
352  d->pointObserver = NULL;
353 
354 // d->axlPointPicker->Delete();
355 // d->axlPointPicker = NULL;
356 
357  }
358  if(d->sphereSource)
359  {
360  d->sphereSource->Delete();
361  d->sphereSource = NULL;
362  }
363  if(d->widget)
364  {
365  d->widget = NULL;
366  }
367  if(d->point)
368  {
369  d->point = NULL;
370  }
371  if(d->pointWidget)
372  {
373  this->setPointWidget(false);
374  d->pointWidget = NULL;
375  }
376 
377  //remove actor specificity
378  this->RemoveAllObservers();
379  this->RemovePart(this->getActor());
380  this->getActor()->RemoveAllObservers();
381 
382  if(axlActorComposite *actorComposite = dynamic_cast<axlActorComposite *>(this->parent()) )
383  actorComposite->removeActorReference(this);
384 }
385 
387 {
388  d->sphereSource->SetCenter(d->point->coordinates());
389  d->sphereSource->SetRadius(d->point->size());
390  d->sphereSource->Modified();
391  d->sphereSource->Update();
392 
393  if(d->pointWidget){
394  d->pointWidget->SetPosition(d->sphereSource->GetCenter());
395  }
396 
397  if(!d->point->fields().isEmpty())
398  d->point->touchField();
399 
400  if(d->point->updateView())
401  emit updated();
402 }
403 
404 void axlActorPoint::setSize(double size){
405 
406  d->sphereSource->SetRadius(d->point->size());
407 
408  if(!d->point->fields().isEmpty())
409  d->point->touchField();
410 }
411 
412 
413 axlActorPoint::axlActorPoint(void) : axlActor(), d(new axlActorPointPrivate)
414 {
415  d->point = NULL;
416  d->pointWidget = NULL;
417 // d->axlPointPicker = NULL;
418  d->pointObserver = NULL;
419  d->sphereSource =NULL;
420  d->widget = NULL;
421 }
422 
424 {
425  //disconnect(d->point, SIGNAL(modified()), this, SLOT(onUpdateGeometry()));
426  delete d;
427 
428  d = NULL;
429 }
430 
431 
435 
437 {
438  return axlActorPoint::New();
439 }
void touchGeometry(void)
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
bool isShowPointWidget()
virtual void Execute(vtkObject *caller, unsigned long event, void *)
void showPointWidget(bool show)
virtual void setDisplay(bool display)
Definition: axlActor.cpp:440
void stateChanged(dtkAbstractData *data, int mode)
axlActorPoint * observerDataAssembly
void onUpdateGeometry(void)
dtkAbstractData * data(void)
virtual void setState(int state)
Definition: axlActor.cpp:434
virtual void setData(dtkAbstractData *point1)
vtkSphereSource * observerData_sphereSource
axlInteractorStyleSwitch * axlInteractorStyle
virtual int getState(void)
Definition: axlActor.cpp:429
void updated(void)
void setPointWidget(bool pointWidget)
vtkCxxRevisionMacro(axlActorPoint,"$Revision: 0.0.1 $")
vtkSphereSource * sphere(void)
static axlActorPointObserver * New(void)
virtual void setShader(QString vsfile)
Definition: axlActor.cpp:517
virtual axlAbstractActor * parent(void)
virtual void setOpacity(double opacity)
Definition: axlActor.cpp:461
void setDisplay(bool display)
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
vtkPointWidget * pointWidget
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
virtual void onUpdateProperty(void)
void setSize(double size)
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
axlAbstractActor * createAxlActorPoint(void)
void setMode(int state)
vtkStandardNewMacro(axlActorPoint)
void onModeChanged(int state)
void setQVTKWidget(QVTKOpenGLWidget *widget)
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
static axlActorPoint * New(void)
void setCoordinates(double x, double y, double z)
Change coordinates of this point.
Definition: axlPoint.cpp:370
Class axlAbstractData defines an API for all type of axel data.
axlPoint * observerData_point