Developer documentation | Axl-2.5.1

axlActorPlane.cpp
Go to the documentation of this file.
1 /* axlActorPlane.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:10 2012 (+0100)
8  * By: Julien Wintz
9  * Update #: 32
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 #include "axlActorPlane.h"
20 
21 #include "axlActorComposite.h"
23 
24 #include <axlCore/axlPlane.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 <vtkLineWidget.h>
38 #include <vtkObjectFactory.h>
39 #include <vtkPoints.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 <vtkPlaneSource.h>
49 #include <vtkTransform.h>
50 
51 
52 class axlActorPlaneObserver : public vtkCommand
53 {
54 public:
55  static axlActorPlaneObserver *New(void)
56  {
57  return new axlActorPlaneObserver;
58  }
59 
60  virtual void Execute(vtkObject *caller, unsigned long event, void *)
61  {
62  vtkRenderWindowInteractor *interactor = observerDataAssembly->getInteractor(); //interactor->Render();
63  (void)interactor;
64 
65  if(event == vtkCommand::InteractionEvent)
66  {
67 
68  if(!lineWidget)
69  return;
70 
71  if (caller == lineWidget)
72  {
73  observerData_planeSource->Modified();
74  observerData_planeSource->Update();
75  axlPoint p1(lineWidget->GetPoint1()[0], lineWidget->GetPoint1()[1], lineWidget->GetPoint1()[2]);
76  axlPoint p2(lineWidget->GetPoint2()[0], lineWidget->GetPoint2()[1], lineWidget->GetPoint2()[2]);
77  axlPoint normal = p2 - p1;
78  observerData_plane->setPoint(lineWidget->GetPoint1());
79  observerData_plane->setNormal(normal.coordinates());
82 
83  }
84 
85  }
86 
87 
88  }
89 
91 // vtkCellPicker *axlPlanePicker;
94  vtkPlaneSource *observerData_planeSource;
95 
96  vtkLineWidget *lineWidget = nullptr;
97 };
98 
99 // /////////////////////////////////////////////////////////////////
100 // axlActorPlanePrivate
101 // /////////////////////////////////////////////////////////////////
102 
103 class axlActorPlanePrivate
104 {
105 public:
106  axlPlane *plane;
107  vtkPlaneSource *planeSource;
108  vtkLineWidget *lineWidget;
109  axlActorPlaneObserver *planeObserver;
110  QVTKOpenGLWidget *widget;
111 // vtkCellPicker *axlPlanePicker ;
112 
113  vtkSmartPointer<vtkRenderer> renderer;
114 };
115 
116 #if (VTK_MAJOR_VERSION <= 5)
117 vtkCxxRevisionMacro(axlActorPlane, "$Revision: 0.0.1 $");
118 #endif
119 
121 
122 dtkAbstractData *axlActorPlane::data(void)
123 {
124  return d->plane;
125 }
126 
127 vtkPlaneSource *axlActorPlane::plane(void)
128 {
129  return d->planeSource;
130 }
131 
132 void axlActorPlane::setQVTKWidget(QVTKOpenGLWidget *widget)
133 {
134  d->widget = widget;
135 }
136 
137 
138 void axlActorPlane::setDisplay(bool display)
139 {
140  axlActor::setDisplay(display);
141 
142  if(display && d->lineWidget){
143  this->showPlaneWidget(true);
144  }
145 
146  if(!display && d->lineWidget){
147  this->showPlaneWidget(false);
148  }
149 }
150 
152 {
153  if(!d->lineWidget ) {
154  qDebug() << "No tet actor computed for this axlActorPlane.";
155  return;
156  }
157 
158  if(show){
159  d->lineWidget->SetEnabled(1);
160  }
161 
162  if(!show){
163  d->lineWidget->SetEnabled(0);
164 
165  }
166 
167 }
168 
169 void axlActorPlane::setPlaneWidget(bool planeWidget)
170 {
171  if(planeWidget) {
172  if(!d->lineWidget)
173  {
174  //widget drawing
175 
176  d->lineWidget = vtkLineWidget::New();
177  d->lineWidget->SetInteractor(this->getInteractor());
178  d->lineWidget->SetProp3D(this->getActor());
179  d->lineWidget->PlaceWidget();
180  d->lineWidget->SetPoint1(d->planeSource->GetCenter());
181  axlPoint center(d->planeSource->GetCenter()[0], d->planeSource->GetCenter()[1], d->planeSource->GetCenter()[2]);
182  axlPoint normal(d->planeSource->GetNormal()[0], d->planeSource->GetNormal()[1], d->planeSource->GetNormal()[2]);
183  center += normal;
184  d->lineWidget->SetPoint2(center.coordinates());
185  }
186 
187  if(d->planeObserver)
188  {
189  d->lineWidget->AddObserver(vtkCommand::InteractionEvent, d->planeObserver);
190 
191 
192  d->planeObserver->lineWidget = d->lineWidget;
193  }
194 
195  // there is always the controlPoints there
196  d->lineWidget->SetEnabled(true);
197 
198  }
199 
200  if (!planeWidget)
201  {
202  if (this->getActor()) {
203  // this->getMapper()->SetInput(this->getPolyData());
204 
205  if(d->lineWidget)
206  {
207  d->lineWidget->RemoveAllObservers();
208  d->lineWidget->SetEnabled(false);
209  d->lineWidget->Delete(); // warning not sure
210  d->lineWidget = NULL;
211  }
212  }
213  }
214  if (d->planeObserver)
215  d->planeObserver->lineWidget = d->lineWidget;
216 }
217 
218 void axlActorPlane::setData(dtkAbstractData *plane1)
219 {
220  axlPlane *plane = dynamic_cast<axlPlane *>(plane1);
221  if(plane)
222  {
223  d->plane = plane;
224  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
225  this->setActor(vtkSmartPointer<vtkActor>::New());
226 
227  d->planeSource = vtkPlaneSource::New();
228  d->planeSource->SetResolution(1, 1);
229 
230  //this->onUpdateGeometry();
231 #if (VTK_MAJOR_VERSION <= 5)
232  this->getMapper()->SetInput(d->planeSource->GetOutput());
233 #else
234  this->getMapper()->SetInputData(d->planeSource->GetOutput());
235 #endif
236 
237  this->getActor()->SetMapper(this->getMapper());
238 
239  this->AddPart(this->getActor());
240 
241  if(!d->planeObserver)
242  {
243 // d->axlPlanePicker = vtkCellPicker::New();
244  d->planeObserver = axlActorPlaneObserver::New() ;
245  this->getInteractor()->AddObserver(vtkCommand::InteractionEvent, d->planeObserver);
246  //this->getInteractor()->AddObserver(vtkCommand::MouseMoveEvent, d->planeObserver);
247 
248  d->planeObserver->observerDataAssembly = this;
249 // d->planeObserver->axlPlanePicker = d->axlPlanePicker;
250  d->planeObserver->observerData_planeSource = d->planeSource;
251  d->planeObserver->observerData_plane = d->plane;
252  d->planeObserver->axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *> (this->getInteractor()->GetInteractorStyle());
253  }
254 
255  QColor color = d->plane->color();
256  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
257 
258  QString shader = d->plane->shader();
259  if(!shader.isEmpty())
260  this->setShader(shader);
261 
262  this->setOpacity(d->plane->opacity());
263 
264  // update the actor
265  this->onUpdateGeometry();
266  // signals connecting
267  connect(d->plane, SIGNAL(modifiedGeometry()), this, SLOT(onUpdateGeometry()));
268  connect(d->plane, SIGNAL(modifiedProperty()), this, SLOT(onUpdateProperty()));
269  }
270  else
271  qDebug()<< "no axlPlane available";
272 
273 }
274 
275 void axlActorPlane::setRenderer(vtkRenderer *renderer)
276 {
277  d->renderer = renderer;
278 }
279 
280 
281 void axlActorPlane::setMode(int state)
282 {
283  this->onModeChanged(state);
284 
285  emit stateChanged(this->data(), state);
286 }
287 
289 {
290  if(state == 0)
291  {
293  this->getActor()->SetPickable(1);
294 
295  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
296  {
297  QColor color = data->color();
298  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
299  }
300  this->setPlaneWidget(false);
301 
302  }
303  else if(state == 1)
304  {
305 
307  this->setPlaneWidget(false);
308  this->getActor()->SetPickable(1);
309  vtkProperty *prop = this->getActor()->GetProperty();
310 
311  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
312  {
313  QColor color = data->color();
314  qreal *h = new qreal(0.0);
315  qreal *s = new qreal(0.0);
316  qreal *l = new qreal(0.0);
317  color.getHslF(h, s, l);
318  color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
319  prop->SetColor(color.redF(), color.greenF(), color.blueF());
320  delete l;
321  delete s;
322  delete h;
323  }
324 
325  }
326  else if(state == 2)
327  {
329  this->setPlaneWidget(true);
330 
331  this->getActor()->SetPickable(0);
332  vtkProperty *prop = this->getActor()->GetProperty();
333  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data()))
334  {
335  QColor color = data->color();
336  qreal *h = new qreal(0.0);
337  qreal *s = new qreal(0.0);
338  qreal *l = new qreal(0.0);
339  color.getHslF(h, s, l);
340  color.setHslF(*h, *s, *l + (1.0 - *l)/2.0);
341  prop->SetColor(color.redF(), color.greenF(), color.blueF());
342  delete l;
343  delete s;
344  delete h;
345  }
346  }
347 
348  this->Modified();
349 }
350 
351 
353 {
354  // if on edit mode, change to selection mode (for stability)
355  if (this->getState() == 2)
356  setMode(1);
357 
358  //remove plane specificity
359  if(d->planeObserver)
360  {
361  this->getInteractor()->RemoveObservers(vtkCommand::InteractionEvent, d->planeObserver);
362  this->getInteractor()->RemoveObservers(vtkCommand::MouseMoveEvent, d->planeObserver);
363  d->planeObserver->observerDataAssembly = NULL;
364 // d->planeObserver->axlPlanePicker = NULL;
365  d->planeObserver->observerData_planeSource = NULL;
366  d->planeObserver->observerData_plane = NULL;
367  d->planeObserver->axlInteractorStyle = NULL;
368  d->planeObserver->Delete();
369  d->planeObserver = NULL;
370 
371 // d->axlPlanePicker->Delete();
372 // d->axlPlanePicker = NULL;
373 
374  }
375  if(d->planeSource)
376  {
377  d->planeSource->Delete();
378  d->planeSource = NULL;
379  }
380  if(d->widget)
381  {
382  d->widget = NULL;
383  }
384  if(d->plane)
385  {
386  d->plane = NULL;
387  }
388  if(d->lineWidget)
389  {
390  this->setPlaneWidget(false);
391  d->lineWidget = NULL;
392  }
393 
394  //remove actor specificity
395  this->RemoveAllObservers();
396  this->RemovePart(this->getActor());
397  this->getActor()->RemoveAllObservers();
398 
399  if(axlActorComposite *actorComposite = dynamic_cast<axlActorComposite *>(this->parent()) )
400  actorComposite->removeActorReference(this);
401 }
402 
404 {
405 
406  // vtk need to reinitialize Origin and the two point before changing them again
407  d->planeSource->SetOrigin(0.0, 0.0, 0.0);
408  d->planeSource->SetPoint1(d->plane->size() * 100, 0.0, 0.0);
409  d->planeSource->SetPoint2(0.0, d->plane->size() * 100, 0.0);
410 
411  d->planeSource->SetCenter(d->plane->point()->coordinates());
412  d->planeSource->SetNormal(d->plane->normal()->coordinates());
413  d->planeSource->Update();
414  d->planeSource->Modified();
415 
416  if(d->lineWidget)
417  {
418  d->lineWidget->SetPoint1(d->plane->point()->coordinates());
419  axlPoint p2 = (*(d->plane->point())) + (*(d->plane->normal()));
420  d->lineWidget->SetPoint2(p2.coordinates());
421  }
422 
423 
424  if(!d->plane->fields().isEmpty())
425  d->plane->touchField();
426 
427  if(d->plane->updateView())
428  emit updated();
429 }
430 
431 
432 void axlActorPlane::setSize(double size){
433 
434  // vtk need to reinitialize Origin and the two point before changing them again
435  d->planeSource->SetOrigin(0.0, 0.0, 0.0);
436  d->planeSource->SetPoint1(d->plane->size() * 100, 0.0, 0.0);
437  d->planeSource->SetPoint2(0.0, d->plane->size() * 100, 0.0);
438 
439  d->planeSource->SetCenter(d->plane->point()->coordinates());
440  d->planeSource->SetNormal(d->plane->normal()->coordinates());
441  d->planeSource->Update();
442  d->planeSource->Modified();
443 
444  if(!d->plane->fields().isEmpty())
445  d->plane->touchField();
446 }
447 
448 axlActorPlane::axlActorPlane(void) : axlActor(), d(new axlActorPlanePrivate)
449 {
450  d->plane = NULL;
451  d->lineWidget = NULL;
452 // d->axlPlanePicker = NULL;
453  d->planeObserver = NULL;
454  d->planeSource =NULL;
455  d->widget = NULL;
456 }
457 
459 {
460  delete d;
461 
462  d = NULL;
463 }
464 
466 {
467 
468  if(!d->lineWidget) {
469  qDebug() << "No tet actor computed for this axlActorBSpline.";
470  return false;
471  }
472 
473  return d->lineWidget->GetEnabled();
474 }
475 
476 
478 
479  return axlActorPlane::New();
480 }
void touchGeometry(void)
void setNormal(axlPoint *normal)
Change second point of this plane.
Definition: axlPlane.cpp:181
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
void showPlaneWidget(bool show)
void setPlaneWidget(bool planeWidget)
virtual void setDisplay(bool display)
Definition: axlActor.cpp:440
double * coordinates(void) const
Returns coordinates of this point.
Definition: axlPoint.cpp:445
void stateChanged(dtkAbstractData *data, int mode)
void onUpdateGeometry()
Class axlPlane defines 3D planes.
Definition: axlPlane.h:34
virtual void setState(int state)
Definition: axlActor.cpp:434
vtkPlaneSource * observerData_planeSource
static axlActorPlane * New(void)
virtual int getState(void)
Definition: axlActor.cpp:429
axlActorPlane * observerDataAssembly
void updated(void)
void setPoint(axlPoint *point)
Change first point of this plane.
Definition: axlPlane.cpp:163
void setSize(double size)
void onModeChanged(int state)
vtkStandardNewMacro(axlActorPlane)
void setDisplay(bool display)
virtual void Execute(vtkObject *caller, unsigned long event, void *)
bool isShowPlaneWidget(void)
void setMode(int state)
virtual void setShader(QString vsfile)
Definition: axlActor.cpp:517
virtual axlAbstractActor * parent(void)
virtual void setOpacity(double opacity)
Definition: axlActor.cpp:461
void setRenderer(vtkRenderer *renderer)
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
vtkLineWidget * lineWidget
axlAbstractActor * createAxlActorPlane(void)
void setQVTKWidget(QVTKOpenGLWidget *widget)
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
vtkCxxRevisionMacro(axlActorPlane,"$Revision: 0.0.1 $")
virtual void onUpdateProperty(void)
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
axlInteractorStyleSwitch * axlInteractorStyle
virtual void setData(dtkAbstractData *plane1)
vtkPlaneSource * plane(void)
dtkAbstractData * data(void)
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
axlPlane * observerData_plane
Class axlAbstractData defines an API for all type of axel data.
static axlActorPlaneObserver * New(void)