29 #include <vtkAssemblyNode.h>
30 #include <vtkAssemblyPath.h>
31 #include <vtkCallbackCommand.h>
32 #include <vtkCamera.h>
33 #include <vtkCellArray.h>
34 #include <vtkCellPicker.h>
35 #include <vtkAreaPicker.h>
36 #include <vtkDoubleArray.h>
37 #include <vtkFloatArray.h>
40 #include <vtkLightActor.h>
41 #include <vtkLightCollection.h>
42 #include <vtkObjectFactory.h>
43 #include <vtkPlanes.h>
44 #include <vtkPoints.h>
45 #include <vtkPolyData.h>
46 #include <vtkPolyDataMapper.h>
47 #include <vtkProperty.h>
48 #include <vtkRenderWindowInteractor.h>
49 #include <vtkRenderer.h>
50 #include <vtkRendererCollection.h>
51 #include <vtkRenderWindow.h>
52 #include <vtkSphereSource.h>
53 #include <vtkTransform.h>
54 #include <vtkGlyph3D.h>
55 #include <vtkSmartPointer.h>
56 #include <vtkTransform.h>
57 #include <vtkCursor3D.h>
58 #include <vtkProp3DCollection.h>
60 #include <vtkPropAssembly.h>
61 #include <vtkCollectionIterator.h>
62 #include <vtkCommand.h>
64 #include <vtkRenderWindow.h>
68 class axlLightsWidgetPrivate
71 vtkActor **LightActor;
72 vtkLightActor **LightProp;
73 vtkPolyDataMapper **LightMapper;
74 vtkSphereSource **LightSphere;
76 vtkActor *LightCurrentActor;
77 int LightCurrentActorIndex;
78 vtkPoints *LightPoints;
79 vtkCellPicker *LightCursorPicker;
80 vtkAreaPicker *LightRectanglePicker;
82 vtkProperty *LightHandleProperty;
83 vtkProperty *LightSelectedHandleProperty;
85 vtkAssemblyNode *LightCurrentAssemblyNode;
87 vtkActorCollection *LightActorCollection;
98 vtkPolyDataMapper *LightCursorMapper;
99 vtkActor *LightCursorActor;
115 d->LightCursorMapper = vtkPolyDataMapper::New();
116 d->LightCursorActor = vtkActor::New();
117 d->LightCursorActor->VisibilityOff();
118 d->LightCursorActor->SetMapper(d->LightCursorMapper);
129 d->XStartPosition =-1;
130 d->YStartPosition =-1;
149 vtkRenderWindow *renderWindow = this->Interactor->GetRenderWindow();
150 vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
151 vtkRenderer *render = rendererCollection->GetFirstRenderer();
153 vtkLightCollection *lightCollection = render->GetLights();
154 d->NbLight = lightCollection->GetNumberOfItems();
155 lightCollection->InitTraversal();
159 d->LightPoints = vtkPoints::New();
162 d->LightCursorPicker = vtkCellPicker::New();
163 d->LightRectanglePicker = vtkAreaPicker::New();
165 this->Interactor->SetPicker(d->LightRectanglePicker);
167 d->LightMapper =
new vtkPolyDataMapper *[d->NbLight];
168 d->LightSphere =
new vtkSphereSource *[d->NbLight];
169 d->LightActor =
new vtkActor *[d->NbLight];
170 d->LightProp =
new vtkLightActor *[d->NbLight];
172 d->LightActorCollection = vtkActorCollection::New();
174 d->LightPoints->SetNumberOfPoints(d->NbLight);
176 vtkLight *currentLight = NULL;
178 d->LightCursorPicker->SetTolerance(0.01);
180 for (
int i = 0; i < d->NbLight; i++)
182 currentLight = lightCollection->GetNextItem();
184 d->LightPoints->SetPoint(i, currentLight->GetPosition());
187 d->LightMapper[i] = vtkPolyDataMapper::New();
188 d->LightSphere[i] =vtkSphereSource::New();
189 d->LightSphere[i]->SetPhiResolution(15);
190 d->LightSphere[i]->SetThetaResolution(15);
191 d->LightSphere[i]->SetRadius(0.01);
192 d->LightSphere[i]->SetCenter(d->LightPoints->GetPoint(i));
193 #if (VTK_MAJOR_VERSION <= 5)
194 d->LightMapper[i]->SetInput( d->LightSphere[i]->GetOutput());
196 d->LightMapper[i]->SetInputData( d->LightSphere[i]->GetOutput());
198 d->LightActor[i] = vtkActor::New();
200 d->LightActor[i]->SetMapper(d->LightMapper[i]);
201 d->LightActor[i]->VisibilityOn();
203 d->LightActorCollection->AddItem(d->LightActor[i]);
206 d->LightProp[i] = vtkLightActor::New();
207 d->LightProp[i]->SetLight(currentLight);
208 d->LightProp[i]->SetPosition(currentLight->GetPosition());
211 render->AddActor(d->LightProp[i]);
218 for (
int i = 0; i <d->NbLight; i++)
219 d->LightActor[i]->SetProperty(d->LightHandleProperty);
225 return d->LightActorCollection;
231 if ( ! this->Interactor )
233 vtkErrorMacro(<<
"The interactor must be set prior to enabling/disabling widget");
239 vtkDebugMacro(<<
"Enabling widget");
246 if ( ! this->CurrentRenderer )
248 this->SetCurrentRenderer(this->Interactor->FindPokedRenderer(
249 this->Interactor->GetLastEventPosition()[0],
250 this->Interactor->GetLastEventPosition()[1]));
251 if (this->CurrentRenderer == NULL)
260 vtkRenderWindowInteractor *i = this->Interactor;
261 i->AddObserver(vtkCommand::MouseMoveEvent, this->EventCallbackCommand,
263 i->AddObserver(vtkCommand::LeftButtonPressEvent,
264 this->EventCallbackCommand, this->Priority);
265 i->AddObserver(vtkCommand::LeftButtonReleaseEvent,
266 this->EventCallbackCommand, this->Priority);
269 for (
int i = 0; i <d->NbLight; i++)
271 d->LightActor[i]->SetVisibility(
true);
272 d->LightProp[i]->SetVisibility(
true);
275 d->LightCurrentActorIndex =-1;
276 d->XStartPosition =-1;
277 d->YStartPosition =-1;
280 this->InvokeEvent(vtkCommand::EnableEvent,NULL);
285 vtkDebugMacro(<<
"Disabling widget");
287 if ( ! this->Enabled )
295 this->Interactor->RemoveObserver(this->EventCallbackCommand);
298 for (
int i = 0; i <d->NbLight; i++)
300 d->LightActor[i]->SetVisibility(
false);
301 d->LightProp[i]->SetVisibility(
false);
304 d->LightCurrentActorIndex =-1;
305 d->XStartPosition =-1;
306 d->YStartPosition =-1;
308 this->InvokeEvent(vtkCommand::DisableEvent,NULL);
309 this->SetCurrentRenderer(NULL);
312 this->Interactor->Render();
323 d->LightCurrentActor =
static_cast<vtkActor *
>(prop);
325 if ( d->LightCurrentActor )
327 for (
int i = 0; i <d->NbLight; i++)
329 if ( d->LightCurrentActor == d->LightActor[i] )
331 d->LightCurrentActor->SetProperty(d->LightSelectedHandleProperty);
332 d->LightCursorPicker->GetPickPosition(this->LastPickPosition);
333 d->LightRectanglePicker->GetPickPosition(this->LastPickPosition);
362 case vtkCommand::LeftButtonPressEvent:
365 case vtkCommand::LeftButtonReleaseEvent:
366 self->OnLeftButtonUp();
368 case vtkCommand::MouseMoveEvent:
377 double bounds[6], center[3];
379 this->AdjustBounds(bds, bounds, center);
383 this->InitialBounds[i] = bounds[i];
386 this->InitialLength = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
387 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
388 (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
393 this->Superclass::PrintSelf(os,indent);
396 #define VTK_AVERAGE(a,b,c) \
397 c[0] = (a[0] + b[0])/2.0; \
398 c[1] = (a[1] + b[1])/2.0; \
399 c[2] = (a[2] + b[2])/2.0;
406 int X = this->Interactor->GetEventPosition()[0];
407 int Y = this->Interactor->GetEventPosition()[1];
411 if (!this->CurrentRenderer || !this->CurrentRenderer->IsInViewport(X, Y))
417 d->XStartPosition =X;
418 d->YStartPosition = Y;
420 vtkAssemblyPath *path = NULL;
421 d->LightCursorPicker->Pick(X, Y, 0.0, this->CurrentRenderer);
423 path = d->LightCursorPicker->GetPath();
428 d->LightCurrentActorIndex=this->
HighlightHandle(path->GetLastNode()->GetViewProp());
429 d->LightCurrentAssemblyNode = path->GetLastNode();
430 d->LightCursorPicker->GetPickPosition(this->LastPickPosition);
432 if( d->LightCurrentActorIndex == -1)
447 this->EventCallbackCommand->SetAbortFlag(1);
448 this->StartInteraction();
456 int X = this->Interactor->GetEventPosition()[0];
457 int Y = this->Interactor->GetEventPosition()[1];
460 if (!this->CurrentRenderer || !this->CurrentRenderer->IsInViewport(X, Y))
492 this->EventCallbackCommand->SetAbortFlag(0);
493 this->EndInteraction();
494 this->InvokeEvent(vtkCommand::EndInteractionEvent, NULL);
506 int X = this->Interactor->GetEventPosition()[0];
507 int Y = this->Interactor->GetEventPosition()[1];
511 double focalPoint[4], pickPoint[4], prevPickPoint[4], motionVector[4];
514 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
521 this->ComputeWorldToDisplay(this->LastPickPosition[0], this->LastPickPosition[1], this->LastPickPosition[2], focalPoint);
523 this->ComputeDisplayToWorld(
double(this->Interactor->GetLastEventPosition()[0]),
double(this->Interactor->GetLastEventPosition()[1]),
525 this->ComputeDisplayToWorld(
double(X),
double(Y), z, pickPoint);
528 motionVector[i]=pickPoint[i]-prevPickPoint[i];
531 vtkMatrix4x4 *motion = vtkMatrix4x4::New();
533 if(d->LightCurrentAssemblyNode)
534 motion->DeepCopy(d->LightCurrentAssemblyNode->GetMatrix());
540 vtkRenderWindow *renderWindow = this->Interactor->GetRenderWindow();
541 vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
542 vtkRenderer *render = rendererCollection->GetFirstRenderer();
544 vtkLightCollection *lightCollection = render->GetLights();
545 lightCollection->InitTraversal();
547 vtkLight *currentLight = NULL;
550 for(
int i = 0; i<= d->LightCurrentActorIndex; i++)
551 currentLight = lightCollection->GetNextItem();
554 double *newMotion = motion->MultiplyDoublePoint(motionVector);
555 double newPosition[4];
556 for(
int j = 0 ;j < 4; j++)
558 newPosition[j]=d->LightPoints->GetPoint(d->LightCurrentActorIndex)[j]+newMotion[j];
561 d->LightPoints->SetPoint(d->LightCurrentActorIndex, newPosition[0], newPosition[1], newPosition[2]);
562 d->LightPoints->Modified();
563 d->LightSphere[d->LightCurrentActorIndex]->SetCenter(d->LightPoints->GetPoint(d->LightCurrentActorIndex));
564 currentLight->SetPosition(newPosition[0], newPosition[1], newPosition[2]);
566 d->LightProp[d->LightCurrentActorIndex]->SetPosition(newPosition[0], newPosition[1], newPosition[2]);
567 d->LightSphere[d->LightCurrentActorIndex]->Update();
568 d->LightProp[d->LightCurrentActorIndex]->Modified();
570 d->view->onLightPositionChanged(newPosition[0], newPosition[1], newPosition[2]);
580 this->EventCallbackCommand->SetAbortFlag(1);
581 this->InvokeEvent(vtkCommand::InteractionEvent,NULL);
582 this->Interactor->Render();
588 vtkRenderWindow *renderWindow = this->Interactor->GetRenderWindow();
589 vtkRendererCollection * rendererCollection = renderWindow->GetRenderers();
590 vtkRenderer *render = rendererCollection->GetFirstRenderer();
592 vtkLightCollection *lightCollection = render->GetLights();
593 lightCollection->InitTraversal();
595 vtkLight *currentLight = NULL;
597 for(
int i = 0 ; i < d->NbLight ; i++)
599 d->LightCurrentActorIndex = i;
601 currentLight = lightCollection->GetNextItem();
604 double newPosition[4];
605 for(
int j = 0 ;j < 4; j++)
607 newPosition[j]=currentLight->GetPosition()[j];
610 d->LightPoints->SetPoint(d->LightCurrentActorIndex, newPosition[0], newPosition[1], newPosition[2]);
611 d->LightPoints->Modified();
612 d->LightSphere[d->LightCurrentActorIndex]->SetCenter(d->LightPoints->GetPoint(d->LightCurrentActorIndex));
613 currentLight->SetPosition(newPosition[0], newPosition[1], newPosition[2]);
614 d->LightSphere[d->LightCurrentActorIndex]->Update();
615 d->LightSphere[d->LightCurrentActorIndex]->Modified();
616 d->LightProp[d->LightCurrentActorIndex]->SetPosition(newPosition[0], newPosition[1], newPosition[2]);
618 d->LightActor[d->LightCurrentActorIndex]->Modified();
621 this->Interactor->Render();
627 d->LightHandleProperty = vtkProperty::New();
628 d->LightHandleProperty->SetColor(1,1,1);
630 d->LightSelectedHandleProperty = vtkProperty::New();
631 d->LightSelectedHandleProperty->SetColor(1,0,0);