1 #include "VTKViewer_Trihedron.h"
2 #include "VTKViewer_Actor.h"
7 #include <vtkDataSet.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkObjectFactory.h>
13 #include <vtkProperty.h>
14 #include <vtkLineSource.h>
15 #include <vtkConeSource.h>
16 #include <vtkPolyDataMapper.h>
18 #include "VTKViewer_VectorText.h"
20 vtkStandardNewMacro(VTKViewer_UnScaledActor);
23 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor()
25 Bounds[0] = Bounds[2] = Bounds[4] = VTK_LARGE_FLOAT;
26 Bounds[1] = Bounds[3] = Bounds[5] = -VTK_LARGE_FLOAT;
30 VTKViewer_UnScaledActor
36 /*! Sets \a mySize= \a theSize variable.
37 * \param theSize - integer size
39 void VTKViewer_UnScaledActor::SetSize(int theSize)
44 /*!This causes the actor to be rendered.
45 * Set new scale for actor.
47 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
50 float P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
51 theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
52 theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
53 float aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
54 (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
55 (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
56 int* aSize = theRenderer->GetRenderWindow()->GetSize();
57 float aWinDiag = sqrt(float(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
58 vtkDataSet* aDataSet = GetMapper()->GetInput();
59 float aLength = aDataSet->GetLength();
60 float aPrecision = 1.0E-3;
61 float anOldScale = GetScale()[0];
62 float aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(float(aSize[0])/float(aSize[1]));
63 if(fabs(aScale - anOldScale)/aScale > aPrecision){
67 vtkFollower::Render(theRenderer);
70 vtkStandardNewMacro(VTKViewer_LineActor);
72 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor);
73 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,VTKViewer_UnScaledActor);
75 /*!Adds Label and Arrow actors to \a theRenderer.*/
76 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
78 if(LabelActor && LabelActor->GetVisibility()){
79 LabelActor->Modified();
80 LabelActor->Render(theRenderer);
82 if(ArrowActor && ArrowActor->GetVisibility()){
83 ArrowActor->Modified();
84 ArrowActor->Render(theRenderer);
86 vtkFollower::Render(theRenderer);
89 VTKViewer_Axis::VTKViewer_Axis()
91 /*! \li Initialize the Line pipe-line representation*/
92 myLineSource = vtkLineSource::New();
93 myLineSource->SetPoint1(0.0,0.0,0.0);
95 myMapper[0] = vtkPolyDataMapper::New();
96 myMapper[0]->SetInput(myLineSource->GetOutput());
98 myLineActor = VTKViewer_LineActor::New();
99 myLineActor->SetMapper(myMapper[0]);
100 myLineActor->PickableOff();
102 /*! \li Initialize the Arrow pipe-line representation*/
103 myConeSource = vtkConeSource::New();
104 myConeSource->SetResolution(2);
105 myConeSource->SetAngle(10);
107 myMapper[1] = vtkPolyDataMapper::New();
108 myMapper[1]->SetInput(myConeSource->GetOutput());
110 myArrowActor = VTKViewer_UnScaledActor::New();
111 myArrowActor->SetMapper(myMapper[1]);
112 static int aArrowActorSize = 24;
113 myArrowActor->SetSize(aArrowActorSize);
114 myArrowActor->PickableOff();
116 myLineActor->SetArrowActor(myArrowActor);
118 /*! \li Initialize the Label pipe-line representation */
119 myVectorText = VTKViewer_VectorText::New();
121 myMapper[2] = vtkPolyDataMapper::New();
122 myMapper[2]->SetInput(myVectorText->GetOutput());
124 myLabelActor = VTKViewer_UnScaledActor::New();
125 myLabelActor->SetMapper(myMapper[2]);
126 static int aLabelActorSize = 12;
127 myLabelActor->SetSize(aLabelActorSize);
128 myLabelActor->PickableOff();
129 //myLabelActor->DebugOn();
131 myLineActor->SetLabelActor(myLabelActor);
133 /*! \li Initialise visibility param.*/
134 myVisibility = VTKViewer_Trihedron::eOn;
137 VTKViewer_Axis::~VTKViewer_Axis()
139 /*! \li Destroy of the Label pipe-line representation */
140 myLabelActor->Delete();
142 myMapper[2]->RemoveAllInputs();
143 myMapper[2]->Delete();
145 myVectorText->Delete();
147 /*! \li Destroy of the Arrow pipe-line representation */
148 myArrowActor->Delete();
150 myMapper[1]->RemoveAllInputs();
151 myMapper[1]->Delete();
153 myConeSource->Delete();
155 /*! \li Destroy of the Line pipe-line representation */
156 myLineActor->Delete();
158 myMapper[2]->RemoveAllInputs();
159 myMapper[2]->Delete();
161 myLineSource->Delete();
164 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
165 /*! \li Order of the calls are important*/
166 theRenderer->AddActor(myLineActor);
167 theRenderer->AddActor(myLabelActor);
168 theRenderer->AddActor(myArrowActor);
171 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
174 case VTKViewer_Trihedron::eOff:
175 case VTKViewer_Trihedron::eOn:
176 myLabelActor->SetVisibility(theVis);
177 myArrowActor->SetVisibility(theVis);
178 myLineActor->SetVisibility(theVis);
180 case VTKViewer_Trihedron::eOnlyLineOn:
181 myLabelActor->VisibilityOff();
182 myArrowActor->VisibilityOff();
183 myLineActor->VisibilityOn();
188 myVisibility = theVis;
191 //****************************************************************
192 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
193 myLabelActor->SetCamera(theCamera);
196 void VTKViewer_Axis::SetProperty(vtkProperty* theProperty){
197 myLabelActor->SetProperty(theProperty);
198 myArrowActor->SetProperty(theProperty);
199 myLineActor->SetProperty(theProperty);
202 void VTKViewer_Axis::SetSize(float theSize)
204 float aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
205 myLineSource->SetPoint2(aPosition);
207 myArrowActor->SetPosition(0.0,0.0,0.0);
208 myArrowActor->AddPosition(aPosition);
209 myArrowActor->SetOrientation(myRot);
211 myLabelActor->SetPosition(0.0,0.0,0.0);
212 myLabelActor->AddPosition(aPosition);
215 /*! \class VTKViewer_XAxis
216 * \brief X Axis actor
218 class VTKViewer_XAxis : public VTKViewer_Axis
222 VTKViewer_XAxis(const VTKViewer_XAxis&);
224 vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
225 static VTKViewer_XAxis *New();
228 vtkStandardNewMacro(VTKViewer_XAxis);
230 /*!Initialize X Axis*/
231 VTKViewer_XAxis::VTKViewer_XAxis(){
232 myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
233 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
234 myVectorText->SetText("X");
235 vtkProperty* aProperty = vtkProperty::New();
236 aProperty->SetColor(1.0,0.0,0.0);
237 SetProperty(aProperty);
241 /*! \class VTKViewer_YAxis
242 * \brief Y Axis actor
244 class VTKViewer_YAxis : public VTKViewer_Axis{
247 VTKViewer_YAxis(const VTKViewer_YAxis&);
249 vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
250 static VTKViewer_YAxis *New();
253 vtkStandardNewMacro(VTKViewer_YAxis);
255 /*!Initialize Y Axis*/
256 VTKViewer_YAxis::VTKViewer_YAxis()
258 myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
259 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
260 myVectorText->SetText("Y");
261 vtkProperty* aProperty = vtkProperty::New();
262 aProperty->SetColor(0.0,1.0,0.0);
263 SetProperty(aProperty);
267 /*! \class VTKViewer_ZAxis
268 * \brief Z Axis actor
270 class VTKViewer_ZAxis : public VTKViewer_Axis
274 VTKViewer_ZAxis(const VTKViewer_ZAxis&);
276 vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
277 static VTKViewer_ZAxis *New();
280 vtkStandardNewMacro(VTKViewer_ZAxis);
282 /*!Initialize Z Axis*/
283 VTKViewer_ZAxis::VTKViewer_ZAxis()
285 myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
286 myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
287 myVectorText->SetText("Z");
288 vtkProperty* aProperty = vtkProperty::New();
289 aProperty->SetColor(0.0,0.0,1.0);
290 SetProperty(aProperty);
294 vtkStandardNewMacro(VTKViewer_Trihedron);
296 VTKViewer_Trihedron::VTKViewer_Trihedron()
298 myPresent = vtkActorCollection::New();
299 myAxis[0] = VTKViewer_XAxis::New();
300 myAxis[1] = VTKViewer_YAxis::New();
301 myAxis[2] = VTKViewer_ZAxis::New();
302 static float aSize = 100;
306 VTKViewer_Trihedron::~VTKViewer_Trihedron()
308 myPresent->RemoveAllItems();
310 for(int i = 0; i < 3; i++)
314 void VTKViewer_Trihedron::SetSize(float theSize)
317 for(int i = 0; i < 3; i++)
318 myAxis[i]->SetSize(theSize);
321 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
323 for(int i = 0; i < 3; i++)
324 myAxis[i]->SetVisibility(theVis);
327 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
329 return myAxis[0]->GetVisibility();
332 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
334 vtkCamera* aCamera = theRenderer->GetActiveCamera();
335 for(int i = 0; i < 3; i++){
336 myAxis[i]->AddToRender(theRenderer);
337 myAxis[i]->SetCamera(aCamera);
341 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
343 myPresent->InitTraversal();
344 while(vtkActor* anActor = myPresent->GetNextActor())
345 theRenderer->RemoveActor(anActor);
348 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
350 //TVisibility aVis = GetVisibility();
351 //SetVisibility(eOff);
352 vtkActorCollection* aCollection = theRenderer->GetActors();
353 aCollection->InitTraversal();
355 while(vtkActor* prop = aCollection->GetNextActor()) {
356 if( prop->GetVisibility())
357 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop))
358 if(!anActor->IsInfinitive())
360 //int aCount = theRenderer->VisibleActorCount();
361 //SetVisibility(aVis);