Developer documentation | Axl-2.5.1

axlActorCircleArc.cpp
Go to the documentation of this file.
1 /* axlActorCircleArc.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008 - Valentin Michelet, Inria.
5  * Created: Tue Nov 9 16:58:59 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Tue Nov 9 17:09:38 2010 (+0100)
8  * By: Valentin Michelet
9  * Update #: 19
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlActorCircleArc.h"
21 
22 #include "axlActorComposite.h"
24 
25 #include <axlCore/axlCircleArc.h>
26 #include <axlCore/axlPoint.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 <vtkParametricFunctionSource.h>.h>
39 #include <vtkArcSource.h>
40 #include <vtkLineWidget.h>
41 #include <vtkMath.h>
42 #include <vtkObjectFactory.h>
43 #include <vtkPoints.h>
44 #include <vtkPointWidget.h>
45 #include <vtkPolyData.h>
46 #include <vtkPolyDataMapper.h>
47 #include <vtkProperty.h>
48 #include <vtkRenderer.h>
49 #include <vtkRendererCollection.h>
50 #include <vtkRenderWindow.h>
51 #include <vtkSmartPointer.h>
52 #include <vtkTransform.h>
53 
54 
55 class axlActorCircleArcObserver : public vtkCommand {
56 public:
58  return new axlActorCircleArcObserver;
59  }
60 
61  virtual void Execute(vtkObject *caller, unsigned long event, void *) {
62  vtkRenderWindowInteractor *interactor = observerDataAssembly->getInteractor(); //interactor->Render();
63 
64  if(event == vtkCommand::InteractionEvent) {
65  observerData_arcSource->Update();
66  observerData_arcSource->Modified();
67  interactor->Render();
68 
70  axlPoint center = *(observerData_arc->center());
71  axlPoint centerPW(centerPointWidget->GetPosition());
72 
73  if (axlPoint::distance(&center, &centerPW) > 0.001) {
74  // We are moving the center
76  axlPoint M = (*(observerData_arc->point1()) + *(observerData_arc->point2())) / 2;
77  axlPoint Cp(centerPointWidget->GetPosition());
78  axlPoint MC = C-M;
79  axlPoint u(MC);
80  axlPoint MCp = Cp - M;
81  u.normalize();
82  double normMCH = (u.x()*MCp.x() + u.y()*MCp.y() + u.z()*MCp.z());
83  axlPoint MCH = u*normMCH;
84  axlPoint CH = M+MCH;
85 
87  } else {
88  // We are moving one of the edge
91  axlPoint neoNormal(axlPoint(segmentWidget->GetPoint1()[1]*segmentWidget->GetPoint1()[2] - segmentWidget->GetPoint1()[2]*segmentWidget->GetPoint1()[1],
92  segmentWidget->GetPoint1()[2]*segmentWidget->GetPoint1()[0] - segmentWidget->GetPoint1()[0]*segmentWidget->GetPoint1()[2],
93  segmentWidget->GetPoint1()[0]*segmentWidget->GetPoint1()[1] - segmentWidget->GetPoint1()[1]*segmentWidget->GetPoint1()[0]));
95  observerData_arc->calculateCenter();
96  }
97  }
99  }
100 
101  if(event == vtkCommand::MouseMoveEvent) {
102  //qDebug() << "vtkCommand::MouseMoveEvent";
103  if (observerData_arc) {
104  vtkAssemblyPath *path;
105  vtkRenderWindow *renderWindow = interactor->GetRenderWindow();
106  vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
107  vtkRenderer *render = rendererCollection->GetFirstRenderer();
108  axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *>(interactor->GetInteractorStyle());
109 
110  int X = observerDataAssembly->getInteractor()->GetEventPosition()[0];
111  int Y = observerDataAssembly->getInteractor()->GetEventPosition()[1];
112 
113  if (!render || !render->IsInViewport(X, Y)) {
114  //qDebug()<<" No renderer or bad cursor coordonates";
115  }
116 
117  axlArcPicker->Pick(X,Y,0.0,render);
118  path = axlArcPicker->GetPath();
119 
120  if (path != NULL) {
121  double *position = observerDataAssembly->GetPosition();
122 
123  for(int j=0;j<3;j++) {
124  //qDebug()<<"axlActorCylinderObserver :: Execute "<<position[j];
125  //observerData_line->coordinates()[j] = position[j];
126  }
127  }
128  }
129  }
130  }
132  vtkCellPicker *axlArcPicker;
135  vtkArcSource *observerData_arcSource;
136 
137  vtkPointWidget *centerPointWidget;
138  vtkLineWidget *segmentWidget;
139 };
140 
144 
145 class axlActorCircleArcPrivate {
146 public:
147  axlCircleArc *arc;
148  vtkArcSource *arcSource;
149  axlActorCircleArcObserver *arcObserver;
150  QVTKOpenGLWidget *widget;
151  vtkCellPicker *axlArcPicker;
152 
153  vtkPointWidget *centerPointWidget;
154  vtkLineWidget *segmentWidget;
155 };
156 
157 vtkCxxRevisionMacro(axlActorCircleArc, "$Revision: 0.0.1 $");
159 
160 dtkAbstractData *axlActorCircleArc::data(void) {
161  return d->arc;
162 }
163 
164 vtkArcSource *axlActorCircleArc::arc(void) {
165  return d->arcSource;
166 }
167 
168 void axlActorCircleArc::setQVTKWidget(QVTKOpenGLWidget *widget) {
169  d->widget = widget;
170 }
171 
173  if(arc) {
174  d->arc = arc;
175  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
176  this->setActor(vtkSmartPointer<vtkActor>::New());
177 
178  d->arcSource = vtkArcSource::New();
179 
180  d->arcSource->SetResolution(20);
181 
182  this->onUpdateGeometry();
183  this->getMapper()->SetInput(d->arcSource->GetOutput());
184  this->getActor()->SetMapper(this->getMapper());
185 
186  this->AddPart(this->getActor());
187 
188  if(!d->arcObserver) {
189  d->axlArcPicker = vtkCellPicker::New();
190  d->arcObserver = axlActorCircleArcObserver::New() ;
191  this->getInteractor()->AddObserver(vtkCommand::InteractionEvent, d->arcObserver);
192  this->getInteractor()->AddObserver(vtkCommand::MouseMoveEvent, d->arcObserver);
193 
194  d->arcObserver->observerDataAssembly = this;
195  d->arcObserver->axlArcPicker = d->axlArcPicker;
196  d->arcObserver->observerData_arcSource = d->arcSource;
197  d->arcObserver->observerData_arc = d->arc;
198  d->arcObserver->axlInteractorStyle = dynamic_cast<axlInteractorStyleSwitch *> (this->getInteractor()->GetInteractorStyle());
199  }
200 
201  QColor color = d->arc->color();
202  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
203 
204  QString shader = d->arc->shader();
205  if(!shader.isEmpty())
206  this->setShader(shader);
207 
208  this->setOpacity(d->arc->opacity());
209  }
210  else
211  qDebug()<< "no axlCircleArc available";
212 }
213 
214 void axlActorCircleArc::setDisplay(bool display) {
215  axlActor::setDisplay(display);
216 
217  if (display && d->centerPointWidget) {
218  this->showCircleArcWidget(true);
219  }
220 
221  if (!display && d->centerPointWidget) {
222  this->showCircleArcWidget(false);
223  }
224 }
225 
227  if (!d->centerPointWidget) {
228  qDebug() << "No actor computed for this axlActorCircleArc.";
229  return;
230  }
231 
232  if (show) {
233  d->centerPointWidget->SetEnabled(1);
234  d->segmentWidget->SetEnabled(1);
235  }
236 
237  if (!show) {
238  d->centerPointWidget->SetEnabled(0);
239  d->segmentWidget->SetEnabled(0);
240  }
241 }
242 
244  if(arcWidget) {
245  if (!d->centerPointWidget || !d->segmentWidget) {
246  //Center pointWidget
247  d->centerPointWidget = vtkPointWidget::New();
248  d->centerPointWidget->SetInteractor(this->getInteractor());
249  d->centerPointWidget->SetProp3D(this->getActor());
250  d->centerPointWidget->PlaceWidget();
251  d->centerPointWidget->AllOff();
252  d->centerPointWidget->SetTranslationMode(1);
253 
254  d->centerPointWidget->SetPosition(d->arc->center()->coordinates());
255 
256  //Point1 pointWidget
257  d->segmentWidget = vtkLineWidget::New();
258  d->segmentWidget->SetInteractor(this->getInteractor());
259  d->segmentWidget->SetProp3D(this->getActor());
260  d->segmentWidget->PlaceWidget();
261 
262  d->segmentWidget->SetPoint1(d->arc->point1()->coordinates());
263  d->segmentWidget->SetPoint2(d->arc->point2()->coordinates());
264  }
265 
266  if(d->arcObserver) {
267  d->centerPointWidget->AddObserver(vtkCommand::InteractionEvent, d->arcObserver);
268  d->segmentWidget->AddObserver(vtkCommand::InteractionEvent, d->arcObserver);
269 
270  d->arcObserver->centerPointWidget = d->centerPointWidget;
271  d->arcObserver->segmentWidget = d->segmentWidget;
272  }
273 
274  // there is always the controlPoints there
275  d->centerPointWidget->SetEnabled(true);
276  d->segmentWidget->SetEnabled(true);
277  }
278 
279  if (!arcWidget) {
280  if (this->getActor()) {
281  if(d->centerPointWidget) {
282  d->centerPointWidget->RemoveAllObservers();
283  d->centerPointWidget->SetEnabled(false);
284  d->centerPointWidget->Delete(); // warning not sure
285  d->centerPointWidget = NULL;
286  }
287 
288  if(d->segmentWidget) {
289  d->segmentWidget->RemoveAllObservers();
290  d->segmentWidget->SetEnabled(false);
291  d->segmentWidget->Delete(); // warning not sure
292  d->segmentWidget = NULL;
293  }
294  }
295  }
296 }
297 
299  if(!d->centerPointWidget || !d->segmentWidget) {
300  qDebug() << "No actor computed for this axlActorBSpline.";
301  return false;
302  }
303 
304  return d->centerPointWidget->GetEnabled() && d->segmentWidget->GetEnabled();
305 }
306 
307 
308 void axlActorCircleArc::setMode(int state) {
309  this->onModeChanged(state);
310  emit stateChanged(this->data(), state);
311 }
312 
314  if(state == 0) {
316  this->getActor()->SetPickable(1);
317 
318  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data())) {
319  QColor color = data->color();
320  this->getActor()->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
321  }
322  this->setCircleArcWidget(false);
323 
324  } else if(state == 1) {
325 
327  this->setCircleArcWidget(false);
328  this->getActor()->SetPickable(1);
329  vtkProperty *prop = this->getActor()->GetProperty();
330 
331  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data())) {
332  QColor color = data->color();
333  qreal *h = new qreal(0.0);
334  qreal *s = new qreal(0.0);
335  qreal *l = new qreal(0.0);
336  color.getHslF(h, s, l);
337  color.setHslF(*h, *s, *l + (1.0 - *l) / 2.0);
338  prop->SetColor(color.redF(), color.greenF(), color.blueF());
339  delete l;
340  delete s;
341  delete h;
342  }
343 
344  } else if(state == 2) {
346  this->setCircleArcWidget(true);
347  this->getActor()->SetPickable(0);
348  vtkProperty *prop = this->getActor()->GetProperty();
349  if(axlAbstractData *data = dynamic_cast<axlAbstractData *>(this->data())) {
350  QColor color = data->color();
351  qreal *h = new qreal(0.0);
352  qreal *s = new qreal(0.0);
353  qreal *l = new qreal(0.0);
354  color.getHslF(h, s, l);
355  color.setHslF(*h, *s, *l + (1.0 - *l)/2.0);
356  prop->SetColor(color.redF(), color.greenF(), color.blueF());
357  delete l;
358  delete s;
359  delete h;
360  }
361  }
362 
363  this->Modified();
364 }
365 
366 
368  //remove line specificity
369  if(d->arcObserver) {
370  this->getInteractor()->RemoveObservers(vtkCommand::InteractionEvent, d->arcObserver);
371  this->getInteractor()->RemoveObservers(vtkCommand::MouseMoveEvent, d->arcObserver);
372  d->arcObserver->observerDataAssembly = NULL;
373  d->arcObserver->axlArcPicker = NULL;
374  d->arcObserver->observerData_arcSource = NULL;
375  d->arcObserver->observerData_arc = NULL;
376  d->arcObserver->axlInteractorStyle = NULL;
377  d->arcObserver->Delete();
378  d->arcObserver = NULL;
379 
380  d->axlArcPicker->Delete();
381  d->axlArcPicker = NULL;
382 
383  }
384  if(d->arcSource) {
385  d->arcSource->Delete();
386  d->arcSource = NULL;
387  }
388  if(d->widget) {
389  d->widget = NULL;
390  }
391  if(d->arc) {
392  d->arc = NULL;
393  }
394  if(d->arcSource) {
395  d->arcSource->Delete();
396  d->arcSource = NULL;
397  }
398  if(d->centerPointWidget && d->segmentWidget) {
399  this->setCircleArcWidget(false);
400  d->centerPointWidget = NULL;
401  d->segmentWidget = NULL;
402  }
403 
404  //remove actor specificity
405  this->RemoveAllObservers();
406  this->RemovePart(this->getActor());
407  this->getActor()->RemoveAllObservers();
408 
409  if(axlActorComposite *actorComposite = dynamic_cast<axlActorComposite *>(this->parent()) )
410  actorComposite->removeActorReference(this);
411 }
412 
414  axlPoint A = *(d->arc->point1()); // First point
415  axlPoint B = *(d->arc->point2()); // Second point
416  axlPoint C = *(d->arc->center()); // Center of arc
417 
418  //Set center and points
419  d->arcSource->SetCenter(C.coordinates());
420  d->arcSource->SetPoint1(A.coordinates());
421  d->arcSource->SetPoint2(B.coordinates());
422 
423  if (d->centerPointWidget)
424  d->centerPointWidget->SetPosition(C.coordinates());
425 
426  if (d->segmentWidget) {
427  d->segmentWidget->SetPoint1(A.coordinates());
428  d->segmentWidget->SetPoint2(B.coordinates());
429  }
430 }
431 
432 axlActorCircleArc::axlActorCircleArc(void) : axlActor(), d(new axlActorCircleArcPrivate) {
433  d->arc = NULL;
434  d->axlArcPicker = NULL;
435  d->arcObserver = NULL;
436  d->arcSource = NULL;
437  d->widget = NULL;
438  d->centerPointWidget = NULL;
439  d->segmentWidget = NULL;
440 }
441 
443  delete d;
444  d = NULL;
445 }
vtkCxxRevisionMacro(axlActorCircleArc,"$Revision: 0.0.1 $")
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
void setCircleArc(axlCircleArc *arc)
virtual void setDisplay(bool display)
Definition: axlActor.cpp:440
double * coordinates(void) const
Returns coordinates of this point.
Definition: axlPoint.cpp:445
axlPoint point1(void) const
void stateChanged(dtkAbstractData *data, int mode)
void modifyCenter(double *center)
void modifyPoint2(double *point2)
virtual void setState(int state)
Definition: axlActor.cpp:434
virtual void Execute(vtkObject *caller, unsigned long event, void *)
axlPoint center(void) const
vtkPointWidget * centerPointWidget
void modifyNormal(double *normal)
vtkArcSource * arc(void)
bool isShowCircleArcWidget(void)
void showCircleArcWidget(bool show)
void onModeChanged(int state)
static axlActorCircleArcObserver * New(void)
void setMode(int state)
void modifyPoint1(double *point1)
void normalize(void)
Definition: axlPoint.cpp:410
virtual void setShader(QString vsfile)
Definition: axlActor.cpp:517
axlPoint point2(void) const
virtual axlAbstractActor * parent(void)
virtual void setOpacity(double opacity)
Definition: axlActor.cpp:461
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
dtkAbstractData * data(void)
axlInteractorStyleSwitch * axlInteractorStyle
void setQVTKWidget(QVTKOpenGLWidget *widget)
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
double y
Definition: axlPoint.h:37
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
axlActorCircleArc * observerDataAssembly
vtkStandardNewMacro(axlActorCircleArc)
void setCircleArcWidget(bool arcWidget)
double z
Definition: axlPoint.h:38
static double distance(const axlPoint &lhs, const axlPoint &rhs)
Returns the distance between lhs point and rhs point.
Definition: axlPoint.cpp:459
double x
Definition: axlPoint.h:37
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
Class axlAbstractData defines an API for all type of axel data.
void setDisplay(bool display)