Developer documentation | Axl-2.5.1

axlActorDataDynamic.cpp
Go to the documentation of this file.
1 /* axlActorDataDynamic.cpp ---
2  *
3  * Author: Anais Ducoffe
4  * Copyright (C) 2013 - Anais Ducoffe, Inria.
5  */
6 
7 /* Commentary:
8  *
9  */
10 
11 /* Change log:
12  *
13  */
14 #include "axlActorDataDynamic.h"
15 
17 
18 
19 //all data types
20 #include <axlCore/axlDataDynamic.h>
21 #include "axlActorPoint.h"
22 #include "axlActorComposite.h"
23 #include <axlCore/axlPoint.h>
25 
27 
28 #include <QVTKOpenGLWidget.h>
29 
30 #include <vtkActor.h>
31 #include <vtkAssemblyPath.h>
32 #include <vtkCellPicker.h>
33 #include <vtkCommand.h>
34 #include <vtkGlyph3D.h>
35 #include <vtkInteractorStyleSwitch.h>
36 #include <vtkLight.h>
37 #include <vtkLightCollection.h>
38 #include <vtkLineSource.h>
39 #include <vtkConeSource.h>
40 #include <vtkCylinderSource.h>
41 #include <vtkPlaneSource.h>
42 #include <vtkSphereSource.h>
43 #include <vtkParametricFunctionSource.h>
44 #include <vtkParametricTorus.h>
45 #include <vtkPointWidget.h>
46 #include <vtkLineWidget.h>
47 #include <vtkObjectFactory.h>
48 #include <vtkPoints.h>
49 #include <vtkPolyData.h>
50 #include <vtkPolyDataMapper.h>
51 #include <vtkProperty.h>
52 #include <vtkRenderer.h>
53 #include <vtkRendererCollection.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkRenderWindowInteractor.h>
56 #include <vtkSmartPointer.h>
57 #include <vtkTransform.h>
58 #include <vtkTubeFilter.h>
59 
60 class axlActorDataDynamicObserver : public vtkCommand
61 {
62 public:
64  {
65  return new axlActorDataDynamicObserver;
66  }
67 
68  virtual void Execute(vtkObject *caller, unsigned long event, void *)
69  {
70 // if(observerDataAssembly){
71 // vtkRenderWindowInteractor *interactor = observerDataAssembly->getInteractor();
72 
73 
74 // if(event == vtkCommand::MouseMoveEvent)
75 // {
76 // // we need the matrix transform of this actor
77 
78 // vtkAssemblyPath *path;
79 // vtkRenderWindow *renderWindow = interactor->GetRenderWindow();
80 // vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
81 // vtkRenderer *render = rendererCollection->GetFirstRenderer();
82 // axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *>(interactor->GetInteractorStyle());
83 
84 // int X = observerDataAssembly->getInteractor()->GetEventPosition()[0];
85 // int Y = observerDataAssembly->getInteractor()->GetEventPosition()[1];
86 
87 // if (!render || !render->IsInViewport(X, Y))
88 // {
89 // qDebug()<<" No renderer or bad cursor coordonates in axlActorPoint";
90 // }
91 
92 // axldynamicPicker->Pick(X,Y,0.0,render);
93 // path = axldynamicPicker->GetPath();
94 // }
95 
96 // }
97 
98  }
99 
101  vtkCellPicker *axldynamicPicker;
104 
105 };
106 
107 // /////////////////////////////////////////////////////////////////
108 // axlActorDataDynamicPrivate
109 // /////////////////////////////////////////////////////////////////
110 
111 class axlActorDataDynamicPrivate
112 {
113 public:
114  axlDataDynamic *dynamicData;
115  QList<axlAbstractActor *>outputActors;
116  axlActorDataDynamicObserver *dataObserver;
117  QVTKOpenGLWidget *widget;
118  vtkCellPicker *axlPicker ;
119 
120  axlAbstractView *view;
121  vtkRenderer *renderer;
122 
123 
124 };
125 
126 #if (VTK_MAJOR_VERSION <= 5)
127 vtkCxxRevisionMacro(axlActorDataDynamic, "$Revision: 0.0.1 $");
128 #endif
129 
131 
132 dtkAbstractData *axlActorDataDynamic::data(void)
133 {
134  return d->dynamicData;
135 }
136 
138 
139  return d->outputActors.at(0);
140 }
141 
143 
144  return d->outputActors.at(i);
145 }
146 
148 {
149  d->view = view;
150 }
151 
152 void axlActorDataDynamic::setRenderer(vtkRenderer *renderer)
153 {
154  d->renderer = renderer;
155 }
156 
157 void axlActorDataDynamic::setQVTKWidget(QVTKOpenGLWidget *widget)
158 {
159  d->widget = widget;
160 }
161 
162 
163 void axlActorDataDynamic::setData(dtkAbstractData *data1)
164 {
165  axlDataDynamic *data = dynamic_cast<axlDataDynamic *>(data1);
166  if(data){
167  d->dynamicData = data;
168 
169  int n = d->dynamicData->numberOfChannels();
170  for(int indice = 0; indice < n; indice++){
171  if(d->dynamicData->outputs(indice)){
173  d->outputActors.append(factory->create(d->dynamicData->outputs(indice)->identifier()));
174 
175  d->outputActors.last()->setInteractor(this->getInteractor());
176  axlAbstractData *currentOutput = d->dynamicData->outputs(indice);
177  d->outputActors.last()->setData(currentOutput);
178  d->outputActors.last()->setParent(this);
179  // pour creer l'actor qui va avec l'element repertorie dans les enfants dans l'inspecteur d'objet
180  d->view->insert(currentOutput, d->outputActors.last());
181 
182  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
183  this->setActor(dynamic_cast<axlActor *>(d->outputActors.last())->getActor());
184  this->getActor()->SetMapper(dynamic_cast<axlActor *>(d->outputActors.last())->getMapper());
185  //this->getMapper()->SetInput(dynamic_cast<axlActor *>(d->outputActor)->getMapper()->GetInput());
186  this->AddPart(this->getActor());
187 
188  }
189  }
190 
191  if(!d->dataObserver)
192  {
193  d->axlPicker = vtkCellPicker::New();
194  d->dataObserver = axlActorDataDynamicObserver::New() ;
195  this->getInteractor()->AddObserver(vtkCommand::InteractionEvent, d->dataObserver);
196  this->getInteractor()->AddObserver(vtkCommand::MouseMoveEvent, d->dataObserver);
197 
198 
199  d->dataObserver->observerDataAssembly = this;
200  d->dataObserver->axldynamicPicker = d->axlPicker;
201  d->dataObserver->observerData_dynamic = d->dynamicData;
202  d->dataObserver->axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *> (this->getInteractor()->GetInteractorStyle());
203  }
204 
205  // QColor color = d->dynamicData->color();
206  //dynamic_cast<axlActor *>(d->outputActor)->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
207  //this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
208 
209  QString shader = d->dynamicData->shader();
210  if(!shader.isEmpty()){
211  //d->outputActor->setShader(shader);
212  //this->setShader(shader);
213  }
214 
215  //d->outputActor->setOpacity(d->dynamicData->opacity());
216  //this->setOpacity(d->dynamicData->opacity());
217 
218 
219  }else{
220  qDebug()<< "no axlDataDynamic available";
221  }
222 
223  connect(data, SIGNAL(modifiedGeometry()), this, SLOT(onUpdateGeometry()));
224 
225 // for (int i = 0;i < d->dynamicData->numberOfChannels();i++) {
226 // connect(d->dynamicData->outputs(i), SIGNAL(modifiedGeometry()), this, SLOT(onUpdateGeometry()));
227 // }
228 }
229 
230 
232 {
233  for(int i = 0; i < d->dynamicData->numberOfChannels();i++){
234  d->outputActors.at(i)->setDisplay(display);
235  }
236 }
237 
238 
240 {
241 
242  this->onModeChanged(state);
243 
244  emit stateChanged(this->data(), state);
245 
246 }
247 
248 //void axlActorDataDynamic::onModeChanged(int state)
249 //{
250 
251 // for(int indice = 0; indice < d->dynamicData->numberOfChannels();indice++){
252 // axlAbstractData *axlData = d->dynamicData->outputs(indice);
253 // if(axlData){
254 // if((axlData->editable() == false) && (state == 2)){
255 // state = 1;
256 // }
257 // }
258 
259 // qDebug() << "final state : " << state;
260 
261 // if(d->outputActors.at(indice)){
262 // d->outputActors.at(indice)->onModeChanged(state);
263 // //this->setState(d->outputActors.at(indice)->getState());
264 // this->setState(state);
265 // vtkProperty *prop = this->getActor()->GetProperty();
266 // prop->SetColor(dynamic_cast<axlActor *>(d->outputActors.at(indice))->getActor()->GetProperty()->GetColor()[0],dynamic_cast<axlActor *>(d->outputActors.at(indice))->getActor()->GetProperty()->GetColor()[1],dynamic_cast<axlActor *>(d->outputActors.at(indice))->getActor()->GetProperty()->GetColor()[2] );
267 // this->Modified();
268 // }
269 // }
270 
271 
272 //}
273 
274 
276 {
277  axlAbstractData *axlData = d->dynamicData->outputs();
278  if(axlData){
279  if((axlData->editable() == false) && (state == 2)){
280  state = 1;
281  }
282  }
283 
284  if(state == 0)
285  {
286 
288 
289  vtkSmartPointer<vtkPropCollection> actors = vtkSmartPointer<vtkPropCollection>::New();
290  this->GetActors(actors);
291  actors->InitTraversal();
292 // QColor color;
293 // if(axlDataDynamic *composite = dynamic_cast<axlDataDynamic *>(this->data()))
294 // color = composite->color();
295 // else
296 // color.setRgb(1.0, 1.0, 1.0);
297 
298 // for(int i = 0; i< actors->GetNumberOfItems(); i++)
299 // {
300 // vtkProp *prop = actors->GetNextProp();
301 // if(vtkActor *actor = dynamic_cast<vtkActor *>(prop))
302 // {
303 // actor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
304 // }
305 // }
306 
307  // we need to add renderer the axlActorComposite
308  // and to remove from the renderer and the all of the axlActor child of the axlActorComposite
309  d->renderer->AddActor(this);
310 
311 
312  foreach(axlAbstractActor *abstractActor, d->outputActors)
313  {
314  if(axlActor *actorProp = dynamic_cast<axlActor *>(abstractActor))
315  {
316  disconnect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
317  //d->renderer->RemoveActor(actorProp);
318  }
319  if(axlActorDataDynamic *actorProp = dynamic_cast<axlActorDataDynamic *>(abstractActor))
320  {
321  disconnect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
322  //d->renderer->RemoveActor(actorProp);
323  }
324  }
325  }
326  else if(state == 1)
327  {
328 
330 
331  vtkSmartPointer<vtkPropCollection> actors = vtkSmartPointer<vtkPropCollection>::New();
332  this->GetActors(actors);
333  actors->InitTraversal();
334 
335 // QColor color;
336 // if(axlDataDynamic *composite = dynamic_cast<axlDataDynamic *>(this->data()))
337 // {
338 // color = composite->color();
339 // qreal *h = new qreal(0.0);
340 // qreal *s = new qreal(0.0);
341 // qreal *l = new qreal(0.0);
342 // color.getHslF(h, s, l);
343 // color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
344 // }
345 // else
346 // color.setRgb(0.0, 0.0, 1.0);
347 
348 // for(int i = 0; i< actors->GetNumberOfItems(); i++)
349 // {
350 // vtkProp *prop = actors->GetNextProp();
351 // if(vtkActor *actor = dynamic_cast<vtkActor *>(prop))
352 // actor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
353 // }
354 
355  if(!d->renderer->HasViewProp(this))
356  {
357  d->renderer->AddActor(this);
358 
359  foreach(axlAbstractActor *abstractActor, d->outputActors)
360  {
361  if(axlActor *actorProp = dynamic_cast<axlActor *>(abstractActor))
362  {
363  disconnect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
364  d->renderer->RemoveActor(actorProp);
365 
366  }
367  if(axlActorComposite *actorProp = dynamic_cast<axlActorComposite *>(abstractActor))
368  {
369  disconnect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
370  d->renderer->RemoveActor(actorProp);
371 
372  }
373  }
374 
375  }
376  }
377  else if(state == 2)
378  {
380  vtkSmartPointer<vtkPropCollection> actors = vtkSmartPointer<vtkPropCollection>::New();
381  this->GetActors(actors);
382  actors->InitTraversal();
383 
384 // QColor color;
385 // if(axlDataDynamic *composite = dynamic_cast<axlDataDynamic *>(this->data()))
386 // {
387 // color = composite->color();
388 // qreal *h = new qreal(0.0);
389 // qreal *s = new qreal(0.0);
390 // qreal *l = new qreal(0.0);
391 // color.getHslF(h, s, l);
392 // color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
393 // }
394 // else
395 // color.setRgb(0.0, 0.0, 1.0);
396 
397 // for(int i = 0; i< actors->GetNumberOfItems(); i++)
398 // {
399 
400 // vtkProp *prop = actors->GetNextProp();
401 // if(vtkActor *actor = dynamic_cast<vtkActor *>(prop))
402 // {
403 // actor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
404 // }
405 // }
406  // we need to add renderer all of the axlActor composing the composite
407  // and to remove from the renderer the axlActorComposite
408  d->renderer->RemoveActor(this);
409 
410  //vtkProp3DCollection *actors2 = this->GetParts();
411  //actors2->InitTraversal();
412 
413  for(int i = 0 ; i < d->dynamicData->numberOfChannels(); i ++)
414  {
415  axlAbstractData *axlData = dynamic_cast<axlAbstractData *>(d->dynamicData->outputs(i));
416  axlData->setColor(d->dynamicData->color());
417  }
418 
419  foreach(axlAbstractActor *abstractActor, d->outputActors)
420  {
421  if(axlActor *actorProp = dynamic_cast<axlActor *>(abstractActor))
422  {
423  connect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
424  d->renderer->AddActor(actorProp);
425 
426  }
427  if(axlActorComposite *actorProp = dynamic_cast<axlActorComposite *>(abstractActor))
428  {
429  connect(actorProp, SIGNAL(stateChanged(dtkAbstractData*,int)), d->view, SIGNAL(stateChanged(dtkAbstractData*,int)));
430  d->renderer->AddActor(actorProp);
431  }
432  }
433  }
434 }
435 
436 
438 {
439 
440  if(d->dataObserver)
441  {
442  this->getInteractor()->RemoveObservers(vtkCommand::LeftButtonPressEvent, d->dataObserver);
443  this->getInteractor()->RemoveObservers(vtkCommand::RightButtonPressEvent, d->dataObserver);
444  // this->getInteractor()->RemoveObservers(vtkCommand::InteractionEvent, d->dataObserver);
445  // this->getInteractor()->RemoveObservers(vtkCommand::KeyPressEvent, d->dataObserver);
446  // //this->RemoveObserver(d->dataObserver);
447  d->dataObserver->observerDataAssembly = NULL;
448  d->dataObserver->axldynamicPicker = NULL;
449  d->dataObserver->observerData_dynamic = NULL;
450  d->dataObserver->axlInteractorStyle = NULL;
451  d->dataObserver->Delete();
452  d->dataObserver = NULL;
453 
454  d->axlPicker->Delete();
455  d->axlPicker = NULL;
456 
457  }
458 
459  if(d->widget)
460  {
461  d->widget = NULL;
462  }
463  if(d->dynamicData)
464  {
465  for(int i = 0;i < d->dynamicData->numberOfChannels();i++){
466  axlAbstractData *output = d->dynamicData->outputs(i);
467  if(output){
468  disconnect(output,SIGNAL(modified()),this , SLOT(onUpdateGeometry()));
469  }
470  }
471 
472  for (int indice = 0; indice < d->dynamicData->numberOfChannels();indice++) {
473 
474  if((!(d->outputActors.at(indice) == NULL))){
475  axlActor *actor = dynamic_cast<axlActor *>(d->outputActors.at(indice));
476  if(actor){
477  actor->setState(this->getState());
478  actor->onRemoved();
479  }
480  }
481 
482  }
483 
484  d->dynamicData = NULL;
485  }
486 
487  if(axlActorComposite *actorComposite = dynamic_cast<axlActorComposite *>(this->parent()) )
488  actorComposite->removeActorReference(this);
489 }
490 
492 {
493 // for(int indice = 0; indice < d->dynamicData->numberOfChannels(); indice++) {
494 // d->outputActors.at(indice)->onUpdateGeometry();
495 }
496 
497 axlActorDataDynamic::axlActorDataDynamic(void) : axlActor(), d(new axlActorDataDynamicPrivate)
498 {
499  d->dynamicData = NULL;
500  // for(int indice =0; indice < d->dynamicData->numberOfChannels();indice++){
501  // d->outputActors.at(indice) = NULL;
502  // }
503  d->axlPicker = NULL;
504  d->dataObserver = NULL;
505  d->widget = NULL;
506  d->view = NULL;
507  d->renderer = NULL;
508 }
509 
511 {
512  delete d;
513 
514  d = NULL;
515 }
516 
518 
519  return axlActorDataDynamic::New();
520 }
void setDisplay(bool display)
axlAbstractActor * createAxlActorDataDynamic(void)
static axlActorDataDynamicObserver * New(void)
void stateChanged(dtkAbstractData *data, int mode)
axlInteractorStyleSwitch * axlInteractorStyle
void onModeChanged(int state)
vtkStandardNewMacro(axlActorDataDynamic)
virtual void setState(int state)
Definition: axlActor.cpp:434
const bool & editable(void)
axlAbstractActor * outputActors(int i)
virtual int getState(void)
Definition: axlActor.cpp:429
dtkAbstractData * data(void)
static axlInspectorActorFactory * instance(void)
void setView(axlAbstractView *view)
virtual void setData(dtkAbstractData *data1)
Class axlDataDynamic defines a dynamic object.
axlAbstractActor * outputActor(void)
virtual void Execute(vtkObject *caller, unsigned long event, void *)
virtual void onRemoved(void)
Definition: axlActor.cpp:613
virtual axlAbstractActor * parent(void)
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
void setQVTKWidget(QVTKOpenGLWidget *widget)
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
void setRenderer(vtkRenderer *renderer)
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
axlAbstractActor * create(const QString &interface_name)
vtkCxxRevisionMacro(axlActorDataDynamic,"$Revision: 0.0.1 $")
void setColor(double r, double g, double b)
axlActorDataDynamic * observerDataAssembly
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
Class axlAbstractData defines an API for all type of axel data.
static axlActorDataDynamic * New(void)