1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "VTKViewer_Trihedron.h"
20 #include "VTKViewer_Actor.h"
24 #include <vtkMapper.h>
25 #include <vtkDataSet.h>
26 #include <vtkRenderer.h>
27 #include <vtkRenderWindow.h>
28 #include <vtkObjectFactory.h>
31 #include <vtkProperty.h>
32 #include <vtkLineSource.h>
33 #include <vtkConeSource.h>
34 #include <vtkPolyDataMapper.h>
36 #include "VTKViewer_VectorText.h"
38 vtkStandardNewMacro(VTKViewer_UnScaledActor);
41 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor()
43 Bounds[0] = Bounds[2] = Bounds[4] = VTK_LARGE_FLOAT;
44 Bounds[1] = Bounds[3] = Bounds[5] = -VTK_LARGE_FLOAT;
51 VTKViewer_UnScaledActor
57 /*! Sets \a mySize= \a theSize variable.
58 * \param theSize - integer size
60 void VTKViewer_UnScaledActor::SetSize(int theSize)
65 /*!This causes the actor to be rendered.
66 * Set new scale for actor.
68 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
71 vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
72 theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
73 theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
74 vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
75 (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
76 (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
77 int* aSize = theRenderer->GetRenderWindow()->GetSize();
78 vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
79 vtkDataSet* aDataSet = GetMapper()->GetInput();
81 vtkFloatingPointType aLength = aDataSet->GetLength();
82 vtkFloatingPointType aPrecision = 1.0E-3;
83 vtkFloatingPointType anOldScale = GetScale()[0];
84 vtkFloatingPointType aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(aSize[0])/vtkFloatingPointType(aSize[1]));
85 if(fabs(aScale - anOldScale)/aScale > aPrecision){
89 vtkFollower::Render(theRenderer);
92 vtkStandardNewMacro(VTKViewer_LineActor);
94 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor);
95 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,VTKViewer_UnScaledActor);
97 /*!Adds Label and Arrow actors to \a theRenderer.*/
98 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
100 if(LabelActor && LabelActor->GetVisibility()){
101 LabelActor->Modified();
102 LabelActor->Render(theRenderer);
104 if(ArrowActor && ArrowActor->GetVisibility()){
105 ArrowActor->Modified();
106 ArrowActor->Render(theRenderer);
108 vtkFollower::Render(theRenderer);
114 VTKViewer_Axis::VTKViewer_Axis()
116 /*! \li Initialize the Line pipe-line representation*/
117 myLineSource = vtkLineSource::New();
118 myLineSource->SetPoint1(0.0,0.0,0.0);
120 myMapper[0] = vtkPolyDataMapper::New();
121 myMapper[0]->SetInput(myLineSource->GetOutput());
123 myLineActor = VTKViewer_LineActor::New();
124 myLineActor->SetMapper(myMapper[0]);
125 myLineActor->PickableOff();
127 /*! \li Initialize the Arrow pipe-line representation*/
128 myConeSource = vtkConeSource::New();
129 myConeSource->SetResolution(2);
130 myConeSource->SetAngle(10);
132 myMapper[1] = vtkPolyDataMapper::New();
133 myMapper[1]->SetInput(myConeSource->GetOutput());
135 myArrowActor = VTKViewer_UnScaledActor::New();
136 myArrowActor->SetMapper(myMapper[1]);
137 static int aArrowActorSize = 24;
138 myArrowActor->SetSize(aArrowActorSize);
139 myArrowActor->PickableOff();
141 myLineActor->SetArrowActor(myArrowActor);
143 /*! \li Initialize the Label pipe-line representation */
144 myVectorText = VTKViewer_VectorText::New();
146 myMapper[2] = vtkPolyDataMapper::New();
147 myMapper[2]->SetInput(myVectorText->GetOutput());
149 myLabelActor = VTKViewer_UnScaledActor::New();
150 myLabelActor->SetMapper(myMapper[2]);
151 static int aLabelActorSize = 12;
152 myLabelActor->SetSize(aLabelActorSize);
153 myLabelActor->PickableOff();
154 //myLabelActor->DebugOn();
156 myLineActor->SetLabelActor(myLabelActor);
158 /*! \li Initialise visibility param.*/
159 myVisibility = VTKViewer_Trihedron::eOn;
165 VTKViewer_Axis::~VTKViewer_Axis()
167 /*! \li Destroy of the Label pipe-line representation */
168 myLabelActor->Delete();
170 myMapper[0]->RemoveAllInputs();
171 myMapper[0]->Delete();
173 myVectorText->Delete();
175 /*! \li Destroy of the Arrow pipe-line representation */
176 myArrowActor->Delete();
178 myMapper[1]->RemoveAllInputs();
179 myMapper[1]->Delete();
181 myConeSource->Delete();
183 /*! \li Destroy of the Line pipe-line representation */
184 myLineActor->Delete();
186 myMapper[2]->RemoveAllInputs();
187 myMapper[2]->Delete();
189 myLineSource->Delete();
193 * \param theRenderer - vtkRenderer pointer
195 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
196 /*! \li Order of the calls are important*/
197 theRenderer->AddActor(myLineActor);
198 theRenderer->AddActor(myLabelActor);
199 theRenderer->AddActor(myArrowActor);
202 /*! Remove actor of acis from \a theRenderer which are in myPresent.
203 * \param theRenderer - vtkRenderer pointer
205 void VTKViewer_Axis::RemoveFromRender(vtkRenderer* theRenderer){
206 /*! \li Order of the calls are important*/
207 theRenderer->RemoveActor(myLineActor);
208 theRenderer->RemoveActor(myLabelActor);
209 theRenderer->RemoveActor(myArrowActor);
212 /*! Sets visibility for all Axis to \a theVis*/
213 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
216 case VTKViewer_Trihedron::eOff:
217 case VTKViewer_Trihedron::eOn:
218 myLabelActor->SetVisibility(theVis);
219 myArrowActor->SetVisibility(theVis);
220 myLineActor->SetVisibility(theVis);
222 case VTKViewer_Trihedron::eOnlyLineOn:
223 myLabelActor->VisibilityOff();
224 myArrowActor->VisibilityOff();
225 myLineActor->VisibilityOn();
230 myVisibility = theVis;
233 /*! Set camera for myLabelActor
235 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
236 myLabelActor->SetCamera(theCamera);
239 /*! Sets \a theProperty for actors: myLineActor,myLabelActor,myArrowActor
241 void VTKViewer_Axis::SetProperty(vtkProperty* theProperty){
242 myLabelActor->SetProperty(theProperty);
243 myArrowActor->SetProperty(theProperty);
244 myLineActor->SetProperty(theProperty);
247 /*! Set size of VTKViewer_Axis
249 void VTKViewer_Axis::SetSize(vtkFloatingPointType theSize)
251 vtkFloatingPointType aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
252 myLineSource->SetPoint2(aPosition);
254 myArrowActor->SetPosition(0.0,0.0,0.0);
255 myArrowActor->AddPosition(aPosition);
256 myArrowActor->SetOrientation(myRot);
258 myLabelActor->SetPosition(0.0,0.0,0.0);
259 myLabelActor->AddPosition(aPosition);
262 /*! \class VTKViewer_XAxis
263 * \brief X Axis actor
265 class VTKViewer_XAxis : public VTKViewer_Axis
269 VTKViewer_XAxis(const VTKViewer_XAxis&);
271 vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
272 static VTKViewer_XAxis *New();
275 vtkStandardNewMacro(VTKViewer_XAxis);
277 /*!Initialize X Axis*/
278 VTKViewer_XAxis::VTKViewer_XAxis(){
279 myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
280 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
281 myVectorText->SetText("X");
282 vtkProperty* aProperty = vtkProperty::New();
283 aProperty->SetColor(1.0,0.0,0.0);
284 SetProperty(aProperty);
288 /*! \class VTKViewer_YAxis
289 * \brief Y Axis actor
291 class VTKViewer_YAxis : public VTKViewer_Axis{
294 VTKViewer_YAxis(const VTKViewer_YAxis&);
296 vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
297 static VTKViewer_YAxis *New();
300 vtkStandardNewMacro(VTKViewer_YAxis);
302 /*!Initialize Y Axis*/
303 VTKViewer_YAxis::VTKViewer_YAxis()
305 myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
306 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
307 myVectorText->SetText("Y");
308 vtkProperty* aProperty = vtkProperty::New();
309 aProperty->SetColor(0.0,1.0,0.0);
310 SetProperty(aProperty);
314 /*! \class VTKViewer_ZAxis
315 * \brief Z Axis actor
317 class VTKViewer_ZAxis : public VTKViewer_Axis
321 VTKViewer_ZAxis(const VTKViewer_ZAxis&);
323 vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
324 static VTKViewer_ZAxis *New();
327 vtkStandardNewMacro(VTKViewer_ZAxis);
329 /*!Initialize Z Axis*/
330 VTKViewer_ZAxis::VTKViewer_ZAxis()
332 myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
333 myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
334 myVectorText->SetText("Z");
335 vtkProperty* aProperty = vtkProperty::New();
336 aProperty->SetColor(0.0,0.0,1.0);
337 SetProperty(aProperty);
341 vtkStandardNewMacro(VTKViewer_Trihedron);
346 VTKViewer_Trihedron::VTKViewer_Trihedron()
348 myPresent = vtkActorCollection::New();
349 myAxis[0] = VTKViewer_XAxis::New();
350 myAxis[1] = VTKViewer_YAxis::New();
351 myAxis[2] = VTKViewer_ZAxis::New();
352 static vtkFloatingPointType aSize = 100;
359 VTKViewer_Trihedron::~VTKViewer_Trihedron()
361 myPresent->RemoveAllItems();
363 for(int i = 0; i < 3; i++)
369 void VTKViewer_Trihedron::SetSize(vtkFloatingPointType theSize)
372 for(int i = 0; i < 3; i++)
373 myAxis[i]->SetSize(theSize);
376 /*! Set visibility of axes
378 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
380 for(int i = 0; i < 3; i++)
381 myAxis[i]->SetVisibility(theVis);
385 \return visibility of first axis
387 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
389 return myAxis[0]->GetVisibility();
392 /*! Add to render all Axis
393 * \param theRenderer - vtkRenderer pointer
395 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
397 vtkCamera* aCamera = theRenderer->GetActiveCamera();
398 for(int i = 0; i < 3; i++){
399 myAxis[i]->AddToRender(theRenderer);
400 myAxis[i]->SetCamera(aCamera);
404 /*! Remove all actors from \a theRenderer which are in myPresent.
405 * \param theRenderer - vtkRenderer pointer
407 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
409 myPresent->InitTraversal();
410 while(vtkActor* anActor = myPresent->GetNextActor())
411 theRenderer->RemoveActor(anActor);
412 for(int i = 0; i < 3; i++)
413 myAxis[i]->RemoveFromRender(theRenderer);
416 /*! Return count of visible actors.
417 * \param theRenderer - vtkRenderer pointer
419 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
421 //TVisibility aVis = GetVisibility();
422 //SetVisibility(eOff);
423 vtkActorCollection* aCollection = theRenderer->GetActors();
424 aCollection->InitTraversal();
426 while(vtkActor* prop = aCollection->GetNextActor()) {
427 if( prop->GetVisibility())
428 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop))
429 if(!anActor->IsInfinitive())
431 //int aCount = theRenderer->VisibleActorCount();
432 //SetVisibility(aVis);