Developer documentation | Axl-2.5.1

axlActorSphere.cpp
Go to the documentation of this file.
1 /* axlActorSphere.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:46 2012 (+0100)
8  * By: Julien Wintz
9  * Update #: 31
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 #include "axlActorSphere.h"
20 
21 #include "axlActorComposite.h"
23 
24 #include <axlCore/axlSphere.h>
25 #include <axlCore/axlPoint.h>
26 
27 #include <QVTKOpenGLWidget.h>
28 
29 #include <vtkActor.h>
30 #include <vtkAssemblyPath.h>
31 //#include <vtkCellPicker.h>
32 #include <vtkCommand.h>
33 #include <vtkGlyph3D.h>
34 #include <vtkInteractorStyleSwitch.h>
35 #include <vtkLight.h>
36 #include <vtkLightCollection.h>
37 #include <vtkObjectFactory.h>
38 #include <vtkPoints.h>
39 #include <vtkPointWidget.h>
40 #include <vtkPolyData.h>
41 #include <vtkPolyDataMapper.h>
42 #include <vtkProperty.h>
43 #include <vtkRenderer.h>
44 #include <vtkRendererCollection.h>
45 #include <vtkRenderWindow.h>
46 #include <vtkRenderWindowInteractor.h>
47 #include <vtkSmartPointer.h>
48 #include <vtkSphereSource.h>
49 #include <vtkTransform.h>
50 
51 
52 class axlActorSphereObserver : public vtkCommand
53 {
54 public:
56  {
57  return new axlActorSphereObserver;
58  }
59 
60  virtual void Execute(vtkObject *caller, unsigned long event, void *)
61  {
62  vtkRenderWindowInteractor *interactor = observerDataAssembly->getInteractor();
63 
64  if(event == vtkCommand::InteractionEvent)
65  {
66  observerData_sphereSource->Update();
67  observerData_sphereSource->Modified();
68  interactor->Render();
69 
70  if(!pointWidget || !radiusWidget)
71  return;
72 
73  if (caller == pointWidget)
74  {
75 // axlPoint p1(pointWidget->GetPosition()[0], pointWidget->GetPosition()[1], pointWidget->GetPosition()[2]);
76 // axlPoint center(observerData_sphere->coordinates()[0], observerData_sphere->coordinates()[1], observerData_sphere->coordinates()[2]);
77 
78 // if (!(axlPoint::distance(&p1, &center) < 0.0001)) {// There we move the point widget
79  observerData_sphere->setCenter(pointWidget->GetPosition()[0], pointWidget->GetPosition()[1], pointWidget->GetPosition()[2]);
81  // we want to the radius widget to be positioned on the x axis of the point widget
82  radiusWidget->SetPosition(pointWidget->GetPosition()[0] + observerData_sphere->radius() * 1.1, pointWidget->GetPosition()[1], pointWidget->GetPosition()[2]);
83 
84 // }
85  }
86  else if (caller == radiusWidget)
87  {// There we move the radius widget
89 
90  // we want to the radius widget to be positioned on the x axis of the point widget
91  axlPoint c(radiusWidget->GetPosition()[0], pointWidget->GetPosition()[1], pointWidget->GetPosition()[2]);
92 
93  radiusWidget->SetPosition(c.coordinates());
94  double r = axlPoint::distance(&c, &center);
95 
97  observerData_sphereSource->SetRadius(0.9 * r);
98  observerData_sphereSource->Modified();
99  observerData_sphereSource->Update();
100  if(!observerData_sphere->fields().isEmpty())
102  }
103  }
104 
105 // if(event == vtkCommand::MouseMoveEvent)
106 // {
107 
108 // if(observerData_sphere)
109 // {
110 // // we need the matrix transform of this actor
111 
112 // vtkAssemblyPath *path;
113 // vtkRenderWindow *renderWindow = interactor->GetRenderWindow();
114 // vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
115 // vtkRenderer *render = rendererCollection->GetFirstRenderer();
116 // axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *>(interactor->GetInteractorStyle());
117 
118 // int X = observerDataAssembly->getInteractor()->GetEventPosition()[0];
119 // int Y = observerDataAssembly->getInteractor()->GetEventPosition()[1];
120 
121 // if (!render || !render->IsInViewport(X, Y))
122 // {
123 // //qDebug()<<" No renderer or bad cursor coordonates";
124 // }
125 
126 // axlSpherePicker->Pick(X,Y,0.0,render);
127 // path = axlSpherePicker->GetPath();
128 
129 // if ( path != NULL)
130 // {
131 // //qDebug()<< "axlActorSphereObserver::MouseMoveEvent";
132 
133 // double *position = observerDataAssembly->GetPosition();
134 // (void)position;
135 
136 // for(int j=0;j<3;j++)
137 // {
138 // //qDebug()<<"axlActorSphereObserver :: Execute "<<position[j];
139 // //observerData_sphere->coordinates()[j] = position[j];
140 // }
141 // //WARNING We considered there the scale is uniforn in all direction;
142 // // observerData_sphere->emitDataChanged();
143 // }
144 
145 // }
146 // }
147  }
148 
150 // vtkCellPicker *axlSpherePicker;
153  vtkSphereSource *observerData_sphereSource;
154 
155  vtkPointWidget *pointWidget = nullptr;
156  vtkPointWidget *radiusWidget = nullptr;
157 };
158 
159 // /////////////////////////////////////////////////////////////////
160 // axlActorSpherePrivate
161 // /////////////////////////////////////////////////////////////////
162 
163 class axlActorSpherePrivate
164 {
165 public:
166  axlSphere *sphere;
167  vtkSphereSource *sphereSource;
168  vtkPointWidget *pointWidget;
169  vtkPointWidget *radiusWidget;
170  axlActorSphereObserver *sphereObserver;
171  QVTKOpenGLWidget *widget;
172 // vtkCellPicker *axlSpherePicker ;
173 
174 };
175 
176 #if (VTK_MAJOR_VERSION <= 5)
177 vtkCxxRevisionMacro(axlActorSphere, "$Revision: 0.0.1 $");
178 #endif
179 
181 
182 dtkAbstractData *axlActorSphere::data(void)
183 {
184  return d->sphere;
185 }
186 
187 vtkSphereSource *axlActorSphere::sphere(void)
188 {
189  return d->sphereSource;
190 }
191 
192 void axlActorSphere::setQVTKWidget(QVTKOpenGLWidget *widget)
193 {
194  d->widget = widget;
195 }
196 
197 
198 void axlActorSphere::setData(dtkAbstractData *sphere1)
199 {
200  axlSphere *sphere = dynamic_cast<axlSphere *>(sphere1);
201  if(sphere)
202  {
203  d->sphere = sphere;
204  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
205  this->setActor(vtkSmartPointer<vtkActor>::New());
206 
207  d->sphereSource = vtkSphereSource::New();
208  d->sphereSource->SetPhiResolution(20);
209  d->sphereSource->SetThetaResolution(20);
210  d->sphereSource->SetRadius(d->sphere->radius());
211  d->sphereSource->SetCenter( d->sphere->coordinates());
212 
213  this->onUpdateGeometry();
214  // connection of data to actor
215 #if (VTK_MAJOR_VERSION <= 5)
216  this->getMapper()->SetInput(d->sphereSource->GetOutput());
217 #else
218  this->getMapper()->SetInputData(d->sphereSource->GetOutput());
219 #endif
220  this->getActor()->SetMapper(this->getMapper());
221 
222  this->AddPart(this->getActor());
223 
224  if(!d->sphereObserver)
225  {
226 // d->axlSpherePicker = vtkCellPicker::New();
227  d->sphereObserver = axlActorSphereObserver::New() ;
228  this->getInteractor()->AddObserver(vtkCommand::InteractionEvent, d->sphereObserver);
229  this->getInteractor()->AddObserver(vtkCommand::MouseMoveEvent, d->sphereObserver);
230 
231  d->sphereObserver->observerDataAssembly = this;
232 // d->sphereObserver->axlSpherePicker = d->axlSpherePicker;
233  d->sphereObserver->observerData_sphereSource = d->sphereSource;
234  d->sphereObserver->observerData_sphere = d->sphere;
235  d->sphereObserver->axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *> (this->getInteractor()->GetInteractorStyle());
236  }
237 
238 
239  QColor color = d->sphere->color();
240  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
241 
242  QString shader = d->sphere->shader();
243  if(!shader.isEmpty())
244  this->setShader(shader);
245 
246  //this->getActor()->SetScale(d->sphere->radius());
247 
248  this->setOpacity(d->sphere->opacity());
249 
250  // refresh the actor
251  this->onUpdateGeometry();
252 
253  // signals connecting
254  connect(d->sphere, SIGNAL(modifiedGeometry()), this, SLOT(onUpdateGeometry()));
255  connect(d->sphere, SIGNAL(modifiedProperty()), this, SLOT(onUpdateProperty()));
256  }
257  else
258  qDebug()<< "no axlSphere available";
259 
260 
261 }
262 
263 void axlActorSphere::setDisplay(bool display)
264 {
265  axlActor::setDisplay(display);
266 
267  if(display && d->pointWidget){
268  this->showSphereWidget(true);
269  }
270 
271  if(!display && d->pointWidget){
272  this->showSphereWidget(false);
273  }
274 }
275 
277 {
278  if(!d->pointWidget || !d->radiusWidget) {
279  qDebug() << "No tet actor computed for this axlActorCylinder.";
280  return;
281  }
282 
283  if(show){
284  d->pointWidget->SetEnabled(1);
285  d->radiusWidget->SetEnabled(1);
286 
287  }
288 
289  if(!show){
290  d->pointWidget->SetEnabled(0);
291  d->radiusWidget->SetEnabled(1);
292 
293  }
294 }
295 
296 void axlActorSphere::setSphereWidget(bool sphereWidget)
297 {
298 
299  if(sphereWidget) {
300  if(!d->pointWidget || !d->radiusWidget)
301  {
302  //widget drawing
303  d->pointWidget = vtkPointWidget::New();
304  d->pointWidget->SetInteractor(this->getInteractor());
305  d->pointWidget->SetProp3D(this->getActor());
306  d->pointWidget->PlaceWidget();
307  d->pointWidget->AllOff();
308  d->pointWidget->SetTranslationMode(1);
309 
310  d->pointWidget->SetPosition(d->sphereSource->GetCenter());
311 
312  d->radiusWidget = vtkPointWidget::New();
313  d->radiusWidget->SetInteractor(this->getInteractor());
314  d->radiusWidget->SetProp3D(this->getActor());
315  d->radiusWidget->PlaceWidget();
316  d->radiusWidget->AllOff();
317  d->radiusWidget->SetTranslationMode(1);
318 
319  // we want to the radius widget to be positioned on the x axis of the point widget
320  d->radiusWidget->SetPosition(d->pointWidget->GetPosition()[0] + d->sphere->radius() * 1.1 , d->pointWidget->GetPosition()[1], d->pointWidget->GetPosition()[2]);
321 
322  }
323 
324  if(d->sphereObserver)
325  {
326  d->pointWidget->AddObserver(vtkCommand::InteractionEvent, d->sphereObserver);
327  d->radiusWidget->AddObserver(vtkCommand::InteractionEvent, d->sphereObserver);
328 
329  d->sphereObserver->pointWidget = d->pointWidget;
330  d->sphereObserver->radiusWidget = d->radiusWidget;
331 
332  }
333 
334  // there is always the controlPoints there
335  d->pointWidget->SetEnabled(true);
336  d->radiusWidget->SetEnabled(true);
337 
338  }
339  else // (!pointWidget)
340  {
341  if (this->getActor()) {
342  // this->getMapper()->SetInput(this->getPolyData());
343 
344  if(d->pointWidget)
345  {
346  d->pointWidget->RemoveAllObservers();
347  d->pointWidget->SetEnabled(false);
348  d->pointWidget->Delete(); // warning not sure
349  d->pointWidget = NULL;
350  }
351 
352  if(d->radiusWidget)
353  {
354  d->radiusWidget->RemoveAllObservers();
355  d->radiusWidget->SetEnabled(false);
356  d->radiusWidget->Delete(); // warning not sure
357  d->radiusWidget = NULL;
358  }
359  }
360  }
361  if (d->sphereObserver){
362  d->sphereObserver->pointWidget = d->pointWidget;
363  d->sphereObserver->radiusWidget = d->radiusWidget;
364  }
365 }
366 
368 {
369 
370  if(!d->pointWidget || !d->radiusWidget) {
371  qDebug() << "No tet actor computed for this axlActorBSpline.";
372  return false;
373  }
374 
375  return d->pointWidget->GetEnabled() && d->radiusWidget->GetEnabled();
376 }
377 
378 
379 void axlActorSphere::setMode(int state)
380 {
381  this->onModeChanged(state);
382 
383  emit stateChanged(this->data(), state);
384 }
385 
387 {
388  if(state == 0)
389  {
391  this->getActor()->SetPickable(1);
392 
393  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
394  {
395  QColor color = data->color();
396  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
397  }
398  this->setSphereWidget(false);
399 
400  }
401  else if(state == 1)
402  {
403 
405  this->getActor()->SetPickable(1);
406  vtkProperty *prop = this->getActor()->GetProperty();
407 
408  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
409  {
410  QColor color = data->color();
411  qreal *h = new qreal(0.0);
412  qreal *s = new qreal(0.0);
413  qreal *l = new qreal(0.0);
414  color.getHslF(h, s, l);
415  color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
416  prop->SetColor(color.redF(), color.greenF(), color.blueF());
417  delete l;
418  delete s;
419  delete h;
420  }
421  this->setSphereWidget(false);
422  }
423  else if(state == 2)
424  {
426  this->getActor()->SetPickable(0);
427  vtkProperty *prop = this->getActor()->GetProperty();
428  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
429  {
430  QColor color = data->color();
431  qreal *h = new qreal(0.0);
432  qreal *s = new qreal(0.0);
433  qreal *l = new qreal(0.0);
434  color.getHslF(h, s, l);
435  color.setHslF(*h, *s, *l + (1.0 - *l)/2.0);
436  prop->SetColor(color.redF(), color.greenF(), color.blueF());
437  delete l;
438  delete s;
439  delete h;
440  }
441  this->setSphereWidget(true);
442  }
443 
444  this->Modified();
445 }
446 
447 
449 {
450  // if on edit mode, change to selection mode (for stability)
451  if (this->getState() == 2)
452  setMode(1);
453 
454  //remove sphere specificity
455  if(d->sphereObserver)
456  {
457  this->getInteractor()->RemoveObservers(vtkCommand::InteractionEvent, d->sphereObserver);
458  this->getInteractor()->RemoveObservers(vtkCommand::MouseMoveEvent, d->sphereObserver);
459  d->sphereObserver->observerDataAssembly = NULL;
460 // d->sphereObserver->axlSpherePicker = NULL;
461  d->sphereObserver->observerData_sphereSource = NULL;
462  d->sphereObserver->observerData_sphere = NULL;
463  d->sphereObserver->axlInteractorStyle = NULL;
464  d->sphereObserver->Delete();
465  d->sphereObserver = NULL;
466 
467 // d->axlSpherePicker->Delete();
468 // d->axlSpherePicker = NULL;
469 
470  }
471  if(d->sphereSource)
472  {
473  d->sphereSource->Delete();
474  d->sphereSource = NULL;
475  }
476  if(d->widget)
477  {
478  d->widget = NULL;
479  }
480  if(d->sphere)
481  {
482  d->sphere = NULL;
483  }
484  if(d->pointWidget && d->radiusWidget)
485  {
486  this->setSphereWidget(false);
487  d->pointWidget = NULL;
488  d->radiusWidget = NULL;
489  }
490 
491  //remove actor specificity
492  this->RemoveAllObservers();
493  this->RemovePart(this->getActor());
494  this->getActor()->RemoveAllObservers();
495 
496  if(axlActorComposite *actorComposite = dynamic_cast<axlActorComposite *>(this->parent()) )
497  actorComposite->removeActorReference(this);
498 }
499 
501 {
502  d->sphereSource->SetCenter(d->sphere->coordinates());
503  d->sphereSource->SetRadius(d->sphere->radius());
504  d->sphereSource->Modified();
505  d->sphereSource->Update();
506  if(d->pointWidget)
507  d->pointWidget->SetPosition(d->sphereSource->GetCenter());
508 
509  if(!d->sphere->fields().isEmpty())
510  d->sphere->touchField();
511 
512  if(d->sphere->updateView())
513  emit updated();
514 }
515 
516 
517 axlActorSphere::axlActorSphere(void) : axlActor(), d(new axlActorSpherePrivate)
518 {
519  d->sphere = NULL;
520  d->pointWidget = NULL;
521  d->radiusWidget = NULL;
522 // d->axlSpherePicker = NULL;
523  d->sphereObserver = NULL;
524  d->sphereSource =NULL;
525  d->widget = NULL;
526 }
527 
529 {
530  delete d;
531 
532  d = NULL;
533 }
534 
536 
537  return axlActorSphere::New();
538 }
static axlActorSphere * New(void)
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
bool isShowSphereWidget(void)
void setSphereWidget(bool sphereWidget)
vtkPointWidget * radiusWidget
virtual void touchField(void)
double radius
Definition: axlSphere.h:36
virtual void setDisplay(bool display)
Definition: axlActor.cpp:440
void stateChanged(dtkAbstractData *data, int mode)
virtual void setState(int state)
Definition: axlActor.cpp:434
void setCenter(double x, double y, double z)
Definition: axlSphere.cpp:302
axlActorSphere * observerDataAssembly
void onUpdateGeometry(void)
axlInteractorStyleSwitch * axlInteractorStyle
static axlActorSphereObserver * New(void)
axlAbstractActor * createAxlActorSphere(void)
virtual int getState(void)
Definition: axlActor.cpp:429
double * coordinates(void) const
Returns coordinates of this sphere.
Definition: axlSphere.cpp:325
void updated(void)
void setMode(int state)
vtkPointWidget * pointWidget
void setRadius(double radius)
Definition: axlSphere.cpp:262
Class axlSphere defines 3D spheres.
Definition: axlSphere.h:33
virtual void setShader(QString vsfile)
Definition: axlActor.cpp:517
vtkCxxRevisionMacro(axlActorSphere,"$Revision: 0.0.1 $")
virtual axlAbstractActor * parent(void)
virtual void setOpacity(double opacity)
Definition: axlActor.cpp:461
dtkAbstractData * data(void)
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
virtual void setData(dtkAbstractData *sphere1)
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
virtual void onUpdateProperty(void)
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
void setDisplay(bool display)
void showSphereWidget(bool sphereWidget)
QList< axlAbstractField * > fields(void)
static double distance(const axlPoint &lhs, const axlPoint &rhs)
Returns the distance between lhs point and rhs point.
Definition: axlPoint.cpp:459
void onModeChanged(int state)
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
vtkSphereSource * sphere(void)
virtual void Execute(vtkObject *caller, unsigned long event, void *)
Class axlAbstractData defines an API for all type of axel data.
vtkStandardNewMacro(axlActorSphere)
void setQVTKWidget(QVTKOpenGLWidget *widget)
vtkSphereSource * observerData_sphereSource