Developer documentation | Axl-2.5.1

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