1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "VTKViewer_Trihedron.h"
24 #include "VTKViewer_Actor.h"
25 #include "VTKViewer_Algorithm.h"
28 #include <vtkConfigure.h>
30 #include <vtkMapper.h>
31 #include <vtkDataSet.h>
32 #include <vtkRenderer.h>
33 #include <vtkRenderWindow.h>
34 #include <vtkObjectFactory.h>
37 #include <vtkProperty.h>
38 #include <vtkLineSource.h>
39 #include <vtkConeSource.h>
40 #include <vtkPolyDataMapper.h>
41 #include <vtkPolyDataMapper2D.h>
42 #include <vtkVectorText.h>
43 #include <vtkTextActor.h>
44 #include <vtkTextMapper.h>
45 #include <vtkTextProperty.h>
50 vtkStandardNewMacro(VTKViewer_UnScaledActor);
53 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor()
55 Bounds[0] = Bounds[2] = Bounds[4] = VTK_LARGE_FLOAT;
56 Bounds[1] = Bounds[3] = Bounds[5] = -VTK_LARGE_FLOAT;
63 VTKViewer_UnScaledActor
69 /*! Sets \a mySize= \a theSize variable.
70 * \param theSize - integer size
72 void VTKViewer_UnScaledActor::SetSize(int theSize)
77 /*!This causes the actor to be rendered.
78 * Set new scale for actor.
80 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
83 vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
84 theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
85 theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
86 vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
87 (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
88 (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
89 int* aSize = theRenderer->GetRenderWindow()->GetSize();
90 vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
91 vtkDataSet* aDataSet = GetMapper()->GetInput();
93 vtkFloatingPointType aLength = aDataSet->GetLength();
94 vtkFloatingPointType aPrecision = 1.0E-3;
95 vtkFloatingPointType aZeroTol = 1.0E-12;
96 vtkFloatingPointType anOldScale = GetScale()[0];
97 vtkFloatingPointType aScale = anOldScale;
98 vtkFloatingPointType aMaxSize = (vtkFloatingPointType)qMax(aSize[1],aSize[0]);
99 if (qAbs(aWinDiag) > aZeroTol && qAbs(aLength) > aZeroTol && qAbs(aMaxSize) > aZeroTol)
100 aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(qMin(aSize[1],aSize[0]))/aMaxSize);
101 if(qAbs(aScale) > aZeroTol && qAbs(aScale - anOldScale)/aScale > aPrecision){
105 vtkFollower::Render(theRenderer);
108 vtkStandardNewMacro(VTKViewer_LineActor);
111 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,vtkTextActor);
113 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor);
115 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,vtkFollower);
117 /*!Adds Label and Arrow actors to \a theRenderer.*/
118 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
121 if(LabelActor && LabelActor->GetVisibility()){
122 LabelActor->Modified();
123 LabelActor->Render(theRenderer);
126 if(ArrowActor && ArrowActor->GetVisibility()){
127 ArrowActor->Modified();
128 ArrowActor->Render(theRenderer);
130 vtkFollower::Render(theRenderer);
136 VTKViewer_Axis::VTKViewer_Axis()
138 /*! \li Initialize the Line pipe-line representation*/
139 myLineSource = vtkLineSource::New();
140 myLineSource->SetPoint1(0.0,0.0,0.0);
142 myMapper[0] = vtkPolyDataMapper::New();
143 myMapper[0]->SetInput(myLineSource->GetOutput());
145 myLineActor = VTKViewer_LineActor::New();
146 myLineActor->SetMapper(myMapper[0]);
147 myLineActor->PickableOff();
149 /*! \li Initialize the Arrow pipe-line representation*/
150 myConeSource = vtkConeSource::New();
151 myConeSource->SetResolution(16);
152 myConeSource->SetAngle(10);
153 myConeSource->SetCenter(-0.5,0.0,0.0);
155 myMapper[1] = vtkPolyDataMapper::New();
156 myMapper[1]->SetInput(myConeSource->GetOutput());
158 myArrowActor = vtkFollower::New();
159 myArrowActor->SetMapper(myMapper[1]);
160 static int aArrowActorSize = 16;
161 myArrowActor->SetScale(aArrowActorSize);
162 myArrowActor->PickableOff();
164 myLineActor->SetArrowActor(myArrowActor);
166 /*! \li Initialize the Label pipe-line representation */
168 myTextMapper = vtkTextMapper::New();
170 myLabelActor = vtkTextActor::New();
171 myLabelActor->SetMapper(myTextMapper);
172 myLabelActor->ScaledTextOff();
173 myLabelActor->PickableOff();
175 vtkCoordinate* aCoord = vtkCoordinate::New();
176 myLabelActor->GetPositionCoordinate()->SetReferenceCoordinate( aCoord );
179 myVectorText = vtkVectorText::New();
181 myMapper[2] = vtkPolyDataMapper::New();
182 myMapper[2]->SetInput(myVectorText->GetOutput());
184 myLabelActor = VTKViewer_UnScaledActor::New();
185 myLabelActor->SetMapper(myMapper[2]);
186 static int aLabelActorSize = 12;
187 myLabelActor->SetSize(aLabelActorSize);
188 myLabelActor->PickableOff();
189 //myLabelActor->DebugOn();
192 myLineActor->SetLabelActor(myLabelActor);
194 /*! \li Initialise visibility param.*/
195 myVisibility = VTKViewer_Trihedron::eOn;
201 VTKViewer_Axis::~VTKViewer_Axis()
203 /*! \li Destroy of the Label pipe-line representation */
204 myLabelActor->Delete();
206 myMapper[0]->RemoveAllInputs();
207 myMapper[0]->Delete();
209 /*! \li Destroy of the Arrow pipe-line representation */
210 myArrowActor->Delete();
212 myMapper[1]->RemoveAllInputs();
213 myMapper[1]->Delete();
215 myConeSource->Delete();
217 /*! \li Destroy of the Line pipe-line representation */
218 myLineActor->Delete();
221 myTextMapper->RemoveAllInputs();
222 myTextMapper->Delete();
224 myVectorText->Delete();
226 myMapper[2]->RemoveAllInputs();
227 myMapper[2]->Delete();
230 myLineSource->Delete();
234 * \param theRenderer - vtkRenderer pointer
236 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
237 /*! \li Order of the calls are important*/
238 theRenderer->AddActor(myLineActor);
239 theRenderer->AddActor(myLabelActor);
240 theRenderer->AddActor(myArrowActor);
243 /*! Remove actor of acis from \a theRenderer which are in myPresent.
244 * \param theRenderer - vtkRenderer pointer
246 void VTKViewer_Axis::RemoveFromRender(vtkRenderer* theRenderer){
247 /*! \li Order of the calls are important*/
248 theRenderer->RemoveActor(myLineActor);
249 theRenderer->RemoveActor(myLabelActor);
250 theRenderer->RemoveActor(myArrowActor);
253 /*! Sets visibility for all Axis to \a theVis*/
254 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
257 case VTKViewer_Trihedron::eOff:
258 case VTKViewer_Trihedron::eOn:
259 myLabelActor->SetVisibility(theVis);
260 myArrowActor->SetVisibility(theVis);
261 myLineActor->SetVisibility(theVis);
263 case VTKViewer_Trihedron::eOnlyLineOn:
264 myLabelActor->VisibilityOff();
265 myArrowActor->VisibilityOff();
266 myLineActor->VisibilityOn();
271 myVisibility = theVis;
274 /*! Set camera for myLabelActor
276 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
278 myLabelActor->SetCamera(theCamera);
282 /*! Sets color for actors: myLineActor,myLabelActor,myArrowActor
284 void VTKViewer_Axis::SetColor(double theRed, double theGreen, double theBlue)
286 // Set color property for arrow and line actors
287 vtkProperty* aProperty = vtkProperty::New();
288 aProperty->SetColor(theRed, theGreen, theBlue);
290 myArrowActor->SetProperty(aProperty);
291 myLineActor->SetProperty(aProperty);
293 myLabelActor->SetProperty(aProperty);
298 // Set color property for label actor
300 vtkTextProperty* aTextProperty = vtkTextProperty::New();
301 aTextProperty->SetColor(theRed, theGreen, theBlue);
303 myLabelActor->SetTextProperty(aTextProperty);
305 aTextProperty->Delete();
309 /*! Set size of VTKViewer_Axis
311 void VTKViewer_Axis::SetSize(vtkFloatingPointType theSize)
313 vtkFloatingPointType aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
315 vtkFloatingPointType aCoef = 0.99;
316 vtkFloatingPointType aLinePosition[3] = {aPosition[0]*aCoef, aPosition[1]*aCoef, aPosition[2]*aCoef};
317 myLineSource->SetPoint2(aLinePosition);
319 myArrowActor->SetPosition(0.0,0.0,0.0);
320 myArrowActor->AddPosition(aPosition);
321 myArrowActor->SetOrientation(myRot);
322 myArrowActor->SetScale(theSize / 10.);
325 if( vtkCoordinate* aCoord = myLabelActor->GetPositionCoordinate()->GetReferenceCoordinate() )
326 aCoord->SetValue( aPosition );
328 myLabelActor->SetPosition(0.0,0.0,0.0);
329 myLabelActor->AddPosition(aPosition);
333 /*! Check if actor belongs to the axis object
334 * \param theActor - vtkActor pointer
335 * \retval Return true if the actor belongs to the axis object
337 bool VTKViewer_Axis::OwnActor(const vtkActor* theActor)
339 return theActor == myLineActor ||
340 theActor == myArrowActor ||
344 theActor == myLabelActor;
348 /*! \class VTKViewer_XAxis
349 * \brief X Axis actor
351 class VTKViewer_XAxis : public VTKViewer_Axis
355 VTKViewer_XAxis(const VTKViewer_XAxis&);
357 vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
358 static VTKViewer_XAxis *New();
361 vtkStandardNewMacro(VTKViewer_XAxis);
363 /*!Initialize X Axis*/
364 VTKViewer_XAxis::VTKViewer_XAxis(){
365 myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
366 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
368 myTextMapper->SetInput("X");
370 myVectorText->SetText("X");
372 SetColor(1.0,0.0,0.0);
375 /*! \class VTKViewer_YAxis
376 * \brief Y Axis actor
378 class VTKViewer_YAxis : public VTKViewer_Axis{
381 VTKViewer_YAxis(const VTKViewer_YAxis&);
383 vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
384 static VTKViewer_YAxis *New();
387 vtkStandardNewMacro(VTKViewer_YAxis);
389 /*!Initialize Y Axis*/
390 VTKViewer_YAxis::VTKViewer_YAxis()
392 myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
393 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
395 myTextMapper->SetInput("Y");
397 myVectorText->SetText("Y");
399 SetColor(0.0,1.0,0.0);
402 /*! \class VTKViewer_ZAxis
403 * \brief Z Axis actor
405 class VTKViewer_ZAxis : public VTKViewer_Axis
409 VTKViewer_ZAxis(const VTKViewer_ZAxis&);
411 vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
412 static VTKViewer_ZAxis *New();
415 vtkStandardNewMacro(VTKViewer_ZAxis);
417 /*!Initialize Z Axis*/
418 VTKViewer_ZAxis::VTKViewer_ZAxis()
420 myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
421 myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
423 myTextMapper->SetInput("Z");
425 myVectorText->SetText("Z");
427 SetColor(0.0,0.0,1.0);
430 vtkStandardNewMacro(VTKViewer_Trihedron);
435 VTKViewer_Trihedron::VTKViewer_Trihedron()
437 myPresent = vtkActorCollection::New();
438 myAxis[0] = VTKViewer_XAxis::New();
439 myAxis[1] = VTKViewer_YAxis::New();
440 myAxis[2] = VTKViewer_ZAxis::New();
441 static vtkFloatingPointType aSize = 100;
448 VTKViewer_Trihedron::~VTKViewer_Trihedron()
450 myPresent->RemoveAllItems();
452 for(int i = 0; i < 3; i++)
458 void VTKViewer_Trihedron::SetSize(vtkFloatingPointType theSize)
461 for(int i = 0; i < 3; i++)
462 myAxis[i]->SetSize(theSize);
465 /*! Set visibility of axes
467 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
469 for(int i = 0; i < 3; i++)
470 myAxis[i]->SetVisibility(theVis);
474 \return visibility of first axis
476 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
478 return myAxis[0]->GetVisibility();
481 /*! Add to render all Axis
482 * \param theRenderer - vtkRenderer pointer
484 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
486 vtkCamera* aCamera = theRenderer->GetActiveCamera();
487 for(int i = 0; i < 3; i++){
488 myAxis[i]->AddToRender(theRenderer);
489 myAxis[i]->SetCamera(aCamera);
493 /*! Remove all actors from \a theRenderer which are in myPresent.
494 * \param theRenderer - vtkRenderer pointer
496 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
498 myPresent->InitTraversal();
499 while(vtkActor* anActor = myPresent->GetNextActor())
500 theRenderer->RemoveActor(anActor);
501 for(int i = 0; i < 3; i++)
502 myAxis[i]->RemoveFromRender(theRenderer);
505 /*! Return count of visible actors.
506 * \param theRenderer - vtkRenderer pointer
508 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
510 //TVisibility aVis = GetVisibility();
511 //SetVisibility(eOff);
512 VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
513 vtkActorCollection* aCollection = aCopy.GetActors();
514 aCollection->InitTraversal();
516 while(vtkActor* prop = aCollection->GetNextActor()) {
517 if( prop->GetVisibility()) {
518 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop)) {
519 if(!anActor->IsInfinitive())
522 else if ( !OwnActor( anActor ) ) {
525 //int aCount = theRenderer->VisibleActorCount();
526 //SetVisibility(aVis);
532 /*! Check if actor belongs to the axis object
533 * \param theActor - vtkActor pointer
534 * \retval Return true if the actor belongs to the axis object
536 bool VTKViewer_Trihedron::OwnActor(const vtkActor* theActor)
538 myPresent->InitTraversal();
539 while(vtkActor* anActor = myPresent->GetNextActor()) {
540 if ( anActor == theActor ) return true;
542 for(int i = 0; i < 3; i++) {
543 if ( myAxis[i]->OwnActor(theActor) ) return true;