1 // Copyright (C) 2007-2023 CEA, EDF, 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, or (at your option) any later version.
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"
29 #include <vtkMapper.h>
30 #include <vtkDataSet.h>
31 #include <vtkRenderer.h>
32 #include <vtkRenderWindow.h>
33 #include <vtkObjectFactory.h>
36 #include <vtkProperty.h>
37 #include <vtkLineSource.h>
38 #include <vtkConeSource.h>
39 #include <vtkPolyDataMapper.h>
40 #include <vtkPolyDataMapper2D.h>
41 #include <vtkVectorText.h>
42 #include <vtkTextActor.h>
43 #include <vtkTextMapper.h>
44 #include <vtkTextProperty.h>
49 vtkStandardNewMacro(VTKViewer_UnScaledActor)
52 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor()
54 Bounds[0] = Bounds[2] = Bounds[4] = VTK_FLOAT_MAX;
55 Bounds[1] = Bounds[3] = Bounds[5] = -VTK_FLOAT_MAX;
62 VTKViewer_UnScaledActor
68 /*! Sets \a mySize= \a theSize variable.
69 * \param theSize - integer size
71 void VTKViewer_UnScaledActor::SetSize(int theSize)
76 /*!This causes the actor to be rendered.
77 * Set new scale for actor.
79 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
82 double P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
83 theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
84 theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
85 double aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
86 (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
87 (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
88 int* aSize = theRenderer->GetRenderWindow()->GetSize();
89 double aWinDiag = sqrt(double(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
90 vtkDataSet* aDataSet = GetMapper()->GetInput();
91 GetMapper()->Update();
92 double aLength = aDataSet->GetLength();
93 double aPrecision = 1.0E-3;
94 double aZeroTol = 1.0E-12;
95 double anOldScale = GetScale()[0];
96 double aScale = anOldScale;
97 double aMaxSize = (double)qMax(aSize[1],aSize[0]);
98 if (qAbs(aWinDiag) > aZeroTol && qAbs(aLength) > aZeroTol && qAbs(aMaxSize) > aZeroTol)
99 aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(double(qMin(aSize[1],aSize[0]))/aMaxSize);
100 if(qAbs(aScale) > aZeroTol && qAbs(aScale - anOldScale)/aScale > aPrecision){
104 vtkFollower::Render(theRenderer);
107 vtkStandardNewMacro(VTKViewer_LineActor)
110 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,vtkTextActor);
112 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor)
114 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,vtkFollower)
116 /*!Adds Label and Arrow actors to \a theRenderer.*/
117 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
120 if(LabelActor && LabelActor->GetVisibility()){
121 LabelActor->Modified();
122 LabelActor->Render(theRenderer);
125 if(ArrowActor && ArrowActor->GetVisibility()){
126 ArrowActor->Modified();
127 ArrowActor->Render(theRenderer);
129 vtkFollower::Render(theRenderer);
135 VTKViewer_Axis::VTKViewer_Axis()
137 /*! \li Initialize the Line pipe-line representation*/
138 myLineSource = vtkLineSource::New();
139 myLineSource->SetPoint1(0.0,0.0,0.0);
141 myMapper[0] = vtkPolyDataMapper::New();
142 myMapper[0]->SetInputConnection(myLineSource->GetOutputPort());
144 myLineActor = VTKViewer_LineActor::New();
145 myLineActor->SetMapper(myMapper[0]);
146 myLineActor->PickableOff();
148 /*! \li Initialize the Arrow pipe-line representation*/
149 myConeSource = vtkConeSource::New();
150 myConeSource->SetResolution(16);
151 myConeSource->SetAngle(10);
152 myConeSource->SetCenter(-0.5,0.0,0.0);
154 myMapper[1] = vtkPolyDataMapper::New();
155 myMapper[1]->SetInputConnection(myConeSource->GetOutputPort());
157 myArrowActor = vtkFollower::New();
158 myArrowActor->SetMapper(myMapper[1]);
159 static int aArrowActorSize = 16;
160 myArrowActor->SetScale(aArrowActorSize);
161 myArrowActor->PickableOff();
163 myLineActor->SetArrowActor(myArrowActor);
165 /*! \li Initialize the Label pipe-line representation */
167 myTextMapper = vtkTextMapper::New();
169 myLabelActor = vtkTextActor::New();
170 myLabelActor->SetMapper(myTextMapper);
171 myLabelActor->SetTextScaleModeToNone();
172 myLabelActor->PickableOff();
174 vtkCoordinate* aCoord = vtkCoordinate::New();
175 myLabelActor->GetPositionCoordinate()->SetReferenceCoordinate( aCoord );
178 myVectorText = vtkVectorText::New();
180 myMapper[2] = vtkPolyDataMapper::New();
181 myMapper[2]->SetInputConnection(myVectorText->GetOutputPort());
183 myLabelActor = VTKViewer_UnScaledActor::New();
184 myLabelActor->SetMapper(myMapper[2]);
185 static int aLabelActorSize = 12;
186 myLabelActor->SetSize(aLabelActorSize);
187 myLabelActor->PickableOff();
188 //myLabelActor->DebugOn();
191 myLineActor->SetLabelActor(myLabelActor);
193 /*! \li Initialise visibility param.*/
194 myVisibility = VTKViewer_Trihedron::eOn;
200 VTKViewer_Axis::~VTKViewer_Axis()
202 /*! \li Destroy of the Label pipe-line representation */
203 myLabelActor->Delete();
205 myMapper[0]->RemoveAllInputs();
206 myMapper[0]->Delete();
208 /*! \li Destroy of the Arrow pipe-line representation */
209 myArrowActor->Delete();
211 myMapper[1]->RemoveAllInputs();
212 myMapper[1]->Delete();
214 myConeSource->Delete();
216 /*! \li Destroy of the Line pipe-line representation */
217 myLineActor->Delete();
220 myTextMapper->RemoveAllInputs();
221 myTextMapper->Delete();
223 myVectorText->Delete();
225 myMapper[2]->RemoveAllInputs();
226 myMapper[2]->Delete();
229 myLineSource->Delete();
233 * \param theRenderer - vtkRenderer pointer
235 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
236 /*! \li Order of the calls are important*/
237 theRenderer->AddActor(myLineActor);
238 theRenderer->AddActor(myLabelActor);
239 theRenderer->AddActor(myArrowActor);
242 /*! Remove actor of acis from \a theRenderer which are in myPresent.
243 * \param theRenderer - vtkRenderer pointer
245 void VTKViewer_Axis::RemoveFromRender(vtkRenderer* theRenderer){
246 /*! \li Order of the calls are important*/
247 theRenderer->RemoveActor(myLineActor);
248 theRenderer->RemoveActor(myLabelActor);
249 theRenderer->RemoveActor(myArrowActor);
252 /*! Sets visibility for all Axis to \a theVis*/
253 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
256 case VTKViewer_Trihedron::eOff:
257 case VTKViewer_Trihedron::eOn:
258 myLabelActor->SetVisibility(theVis);
259 myArrowActor->SetVisibility(theVis);
260 myLineActor->SetVisibility(theVis);
262 case VTKViewer_Trihedron::eOnlyLineOn:
263 myLabelActor->VisibilityOff();
264 myArrowActor->VisibilityOff();
265 myLineActor->VisibilityOn();
270 myVisibility = theVis;
273 /*! Set camera for myLabelActor
275 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
277 myLabelActor->SetCamera(theCamera);
281 /*! Sets color for actors: myLineActor,myLabelActor,myArrowActor
283 void VTKViewer_Axis::SetColor(double theRed, double theGreen, double theBlue)
285 // Set color property for arrow and line actors
286 vtkProperty* aProperty = vtkProperty::New();
287 aProperty->SetColor(theRed, theGreen, theBlue);
289 myArrowActor->SetProperty(aProperty);
290 myLineActor->SetProperty(aProperty);
292 myLabelActor->SetProperty(aProperty);
297 // Set color property for label actor
299 vtkTextProperty* aTextProperty = vtkTextProperty::New();
300 aTextProperty->SetColor(theRed, theGreen, theBlue);
302 myLabelActor->SetTextProperty(aTextProperty);
304 aTextProperty->Delete();
308 /*! Set size of VTKViewer_Axis
310 void VTKViewer_Axis::SetSize(double theSize)
312 double aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
315 double aLinePosition[3] = {aPosition[0]*aCoef, aPosition[1]*aCoef, aPosition[2]*aCoef};
316 myLineSource->SetPoint2(aLinePosition);
318 myArrowActor->SetPosition(0.0,0.0,0.0);
319 myArrowActor->AddPosition(aPosition);
320 myArrowActor->SetOrientation(myRot);
321 myArrowActor->SetScale(theSize / 10.);
324 if( vtkCoordinate* aCoord = myLabelActor->GetPositionCoordinate()->GetReferenceCoordinate() )
325 aCoord->SetValue( aPosition );
327 myLabelActor->SetPosition(0.0,0.0,0.0);
328 myLabelActor->AddPosition(aPosition);
332 /*! Check if actor belongs to the axis object
333 * \param theActor - vtkActor pointer
334 * \retval Return true if the actor belongs to the axis object
336 bool VTKViewer_Axis::OwnActor(const vtkActor* theActor)
338 return theActor == myLineActor ||
339 theActor == myArrowActor ||
343 theActor == myLabelActor;
347 /*! \class VTKViewer_XAxis
348 * \brief X Axis actor
350 class VTKViewer_XAxis : public VTKViewer_Axis
354 VTKViewer_XAxis(const VTKViewer_XAxis&);
356 vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis)
357 static VTKViewer_XAxis *New();
360 vtkStandardNewMacro(VTKViewer_XAxis)
362 /*!Initialize X Axis*/
363 VTKViewer_XAxis::VTKViewer_XAxis(){
364 myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
365 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
367 myTextMapper->SetInput("X");
369 myVectorText->SetText("X");
371 SetColor(1.0,0.0,0.0);
374 /*! \class VTKViewer_YAxis
375 * \brief Y Axis actor
377 class VTKViewer_YAxis : public VTKViewer_Axis{
380 VTKViewer_YAxis(const VTKViewer_YAxis&);
382 vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis)
383 static VTKViewer_YAxis *New();
386 vtkStandardNewMacro(VTKViewer_YAxis)
388 /*!Initialize Y Axis*/
389 VTKViewer_YAxis::VTKViewer_YAxis()
391 myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
392 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
394 myTextMapper->SetInput("Y");
396 myVectorText->SetText("Y");
398 SetColor(0.0,1.0,0.0);
401 /*! \class VTKViewer_ZAxis
402 * \brief Z Axis actor
404 class VTKViewer_ZAxis : public VTKViewer_Axis
408 VTKViewer_ZAxis(const VTKViewer_ZAxis&);
410 vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis)
411 static VTKViewer_ZAxis *New();
414 vtkStandardNewMacro(VTKViewer_ZAxis)
416 /*!Initialize Z Axis*/
417 VTKViewer_ZAxis::VTKViewer_ZAxis()
419 myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
420 myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
422 myTextMapper->SetInput("Z");
424 myVectorText->SetText("Z");
426 SetColor(0.0,0.0,1.0);
429 vtkStandardNewMacro(VTKViewer_Trihedron)
434 VTKViewer_Trihedron::VTKViewer_Trihedron()
436 myPresent = vtkActorCollection::New();
437 myAxis[0] = VTKViewer_XAxis::New();
438 myAxis[1] = VTKViewer_YAxis::New();
439 myAxis[2] = VTKViewer_ZAxis::New();
440 static double aSize = 100;
447 VTKViewer_Trihedron::~VTKViewer_Trihedron()
449 myPresent->RemoveAllItems();
451 for(int i = 0; i < 3; i++)
457 void VTKViewer_Trihedron::SetSize(double theSize)
460 for(int i = 0; i < 3; i++)
461 myAxis[i]->SetSize(theSize);
464 /*! Set visibility of axes
466 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
468 for(int i = 0; i < 3; i++)
469 myAxis[i]->SetVisibility(theVis);
473 \return visibility of first axis
475 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
477 return myAxis[0]->GetVisibility();
480 /*! Add to render all Axis
481 * \param theRenderer - vtkRenderer pointer
483 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
485 vtkCamera* aCamera = theRenderer->GetActiveCamera();
486 for(int i = 0; i < 3; i++){
487 myAxis[i]->AddToRender(theRenderer);
488 myAxis[i]->SetCamera(aCamera);
492 /*! Remove all actors from \a theRenderer which are in myPresent.
493 * \param theRenderer - vtkRenderer pointer
495 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
497 myPresent->InitTraversal();
498 while(vtkActor* anActor = myPresent->GetNextActor())
499 theRenderer->RemoveActor(anActor);
500 for(int i = 0; i < 3; i++)
501 myAxis[i]->RemoveFromRender(theRenderer);
504 /*! Return count of visible actors.
505 * \param theRenderer - vtkRenderer pointer
507 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
509 //TVisibility aVis = GetVisibility();
510 //SetVisibility(eOff);
511 VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
512 vtkActorCollection* aCollection = aCopy.GetActors();
513 aCollection->InitTraversal();
515 while(vtkActor* prop = aCollection->GetNextActor()) {
516 if( prop->GetVisibility()) {
517 if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop)) {
518 if(!anActor->IsInfinitive())
521 else if ( !OwnActor( anActor ) ) {
524 //int aCount = theRenderer->VisibleActorCount();
525 //SetVisibility(aVis);
531 /*! Check if actor belongs to the axis object
532 * \param theActor - vtkActor pointer
533 * \retval Return true if the actor belongs to the axis object
535 bool VTKViewer_Trihedron::OwnActor(const vtkActor* theActor)
537 myPresent->InitTraversal();
538 while(vtkActor* anActor = myPresent->GetNextActor()) {
539 if ( anActor == theActor ) return true;
541 for(int i = 0; i < 3; i++) {
542 if ( myAxis[i]->OwnActor(theActor) ) return true;