Back to index

salome-gui  6.5.0
VTKViewer_Trihedron.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "VTKViewer_Trihedron.h"
00024 #include "VTKViewer_Actor.h"
00025 #include "VTKViewer_Algorithm.h"
00026 
00027 // VTK Includes
00028 #include <vtkConfigure.h>
00029 #include <vtkMath.h>
00030 #include <vtkMapper.h>
00031 #include <vtkDataSet.h>
00032 #include <vtkRenderer.h>
00033 #include <vtkRenderWindow.h>
00034 #include <vtkObjectFactory.h>
00035 
00036 #include <vtkActor.h>
00037 #include <vtkProperty.h>
00038 #include <vtkLineSource.h>
00039 #include <vtkConeSource.h>
00040 #include <vtkPolyDataMapper.h>
00041 #include <vtkPolyDataMapper2D.h>
00042 #include <vtkVectorText.h>
00043 #include <vtkTextActor.h>
00044 #include <vtkTextMapper.h>
00045 #include <vtkTextProperty.h>
00046 
00047 // QT includes
00048 #include <QtGlobal>
00049 
00050 vtkStandardNewMacro(VTKViewer_UnScaledActor);
00051 
00053 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor() 
00054 {
00055   Bounds[0] = Bounds[2] = Bounds[4] = VTK_LARGE_FLOAT;
00056   Bounds[1] = Bounds[3] = Bounds[5] = -VTK_LARGE_FLOAT;
00057 }
00058 
00062 vtkFloatingPointType* 
00063 VTKViewer_UnScaledActor
00064 ::GetBounds()
00065 {
00066   return Bounds;
00067 }
00068 
00072 void VTKViewer_UnScaledActor::SetSize(int theSize)
00073 {
00074   mySize = theSize;
00075 }
00076 
00080 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
00081 {
00082   if(theRenderer){
00083     vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
00084     theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
00085     theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
00086     vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
00087                                            (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
00088                                            (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
00089     int* aSize = theRenderer->GetRenderWindow()->GetSize();
00090     vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
00091     vtkDataSet* aDataSet = GetMapper()->GetInput();
00092     aDataSet->Update();
00093     vtkFloatingPointType aLength = aDataSet->GetLength();
00094     vtkFloatingPointType aPrecision = 1.0E-3;
00095     vtkFloatingPointType aZeroTol   = 1.0E-12;
00096     vtkFloatingPointType anOldScale = GetScale()[0];
00097     vtkFloatingPointType aScale = anOldScale;
00098     vtkFloatingPointType aMaxSize = (vtkFloatingPointType)qMax(aSize[1],aSize[0]);
00099     if (qAbs(aWinDiag) > aZeroTol && qAbs(aLength) > aZeroTol && qAbs(aMaxSize) > aZeroTol)
00100       aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(qMin(aSize[1],aSize[0]))/aMaxSize);
00101     if(qAbs(aScale) > aZeroTol && qAbs(aScale - anOldScale)/aScale > aPrecision){
00102       SetScale(aScale);
00103     }
00104   }
00105   vtkFollower::Render(theRenderer);
00106 }
00107 
00108 vtkStandardNewMacro(VTKViewer_LineActor);
00109 
00110 #ifdef IPAL21440
00111 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,vtkTextActor);
00112 #else
00113 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor);
00114 #endif
00115 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,vtkFollower);
00116 
00118 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
00119 {
00120 #ifndef IPAL21440
00121   if(LabelActor && LabelActor->GetVisibility()){
00122     LabelActor->Modified();
00123     LabelActor->Render(theRenderer);
00124   }
00125 #endif
00126   if(ArrowActor && ArrowActor->GetVisibility()){
00127     ArrowActor->Modified();
00128     ArrowActor->Render(theRenderer);
00129   }
00130   vtkFollower::Render(theRenderer);
00131 }
00132 
00136 VTKViewer_Axis::VTKViewer_Axis()
00137 {
00139   myLineSource = vtkLineSource::New();
00140   myLineSource->SetPoint1(0.0,0.0,0.0);
00141   
00142   myMapper[0] = vtkPolyDataMapper::New();
00143   myMapper[0]->SetInput(myLineSource->GetOutput());
00144   
00145   myLineActor = VTKViewer_LineActor::New();
00146   myLineActor->SetMapper(myMapper[0]);
00147   myLineActor->PickableOff();
00148   
00150   myConeSource =  vtkConeSource::New();
00151   myConeSource->SetResolution(16);
00152   myConeSource->SetAngle(10);
00153   myConeSource->SetCenter(-0.5,0.0,0.0);
00154   
00155   myMapper[1] = vtkPolyDataMapper::New();
00156   myMapper[1]->SetInput(myConeSource->GetOutput());
00157   
00158   myArrowActor = vtkFollower::New();
00159   myArrowActor->SetMapper(myMapper[1]);
00160   static int aArrowActorSize = 16;
00161   myArrowActor->SetScale(aArrowActorSize);
00162   myArrowActor->PickableOff();
00163   
00164   myLineActor->SetArrowActor(myArrowActor);
00165   
00167 #ifdef IPAL21440
00168   myTextMapper = vtkTextMapper::New();
00169   
00170   myLabelActor = vtkTextActor::New();
00171   myLabelActor->SetMapper(myTextMapper);
00172   myLabelActor->ScaledTextOff();
00173   myLabelActor->PickableOff();
00174   
00175   vtkCoordinate* aCoord = vtkCoordinate::New();
00176   myLabelActor->GetPositionCoordinate()->SetReferenceCoordinate( aCoord );
00177   aCoord->Delete();
00178 #else
00179   myVectorText = vtkVectorText::New();
00180   
00181   myMapper[2] = vtkPolyDataMapper::New();
00182   myMapper[2]->SetInput(myVectorText->GetOutput());
00183   
00184   myLabelActor = VTKViewer_UnScaledActor::New();
00185   myLabelActor->SetMapper(myMapper[2]);
00186   static int aLabelActorSize = 12;
00187   myLabelActor->SetSize(aLabelActorSize);
00188   myLabelActor->PickableOff();
00189   //myLabelActor->DebugOn();
00190 #endif
00191   
00192   myLineActor->SetLabelActor(myLabelActor);
00193   
00195   myVisibility = VTKViewer_Trihedron::eOn;
00196 }
00197 
00201 VTKViewer_Axis::~VTKViewer_Axis()
00202 {
00204   myLabelActor->Delete();
00205   
00206   myMapper[0]->RemoveAllInputs();
00207   myMapper[0]->Delete();
00208   
00210   myArrowActor->Delete();
00211   
00212   myMapper[1]->RemoveAllInputs();
00213   myMapper[1]->Delete();
00214   
00215   myConeSource->Delete();
00216   
00218   myLineActor->Delete();
00219   
00220 #ifdef IPAL21440
00221   myTextMapper->RemoveAllInputs();
00222   myTextMapper->Delete();
00223 #else
00224   myVectorText->Delete();
00225   
00226   myMapper[2]->RemoveAllInputs();
00227   myMapper[2]->Delete();
00228 #endif
00229   
00230   myLineSource->Delete();
00231 }
00232 
00236 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
00238   theRenderer->AddActor(myLineActor);
00239   theRenderer->AddActor(myLabelActor);
00240   theRenderer->AddActor(myArrowActor);
00241 }
00242 
00246 void VTKViewer_Axis::RemoveFromRender(vtkRenderer* theRenderer){
00248   theRenderer->RemoveActor(myLineActor);
00249   theRenderer->RemoveActor(myLabelActor);
00250   theRenderer->RemoveActor(myArrowActor);
00251 }
00252 
00254 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
00255 {
00256   switch(theVis){
00257   case VTKViewer_Trihedron::eOff:
00258   case VTKViewer_Trihedron::eOn:
00259     myLabelActor->SetVisibility(theVis);
00260     myArrowActor->SetVisibility(theVis);
00261     myLineActor->SetVisibility(theVis);
00262     break;
00263   case VTKViewer_Trihedron::eOnlyLineOn:
00264     myLabelActor->VisibilityOff();
00265     myArrowActor->VisibilityOff();
00266     myLineActor->VisibilityOn();
00267     break;
00268   default:
00269     return;
00270   }
00271   myVisibility = theVis;
00272 }
00273 
00276 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
00277 #ifndef IPAL21440
00278   myLabelActor->SetCamera(theCamera);
00279 #endif
00280 }
00281 
00284 void VTKViewer_Axis::SetColor(double theRed, double theGreen, double theBlue)
00285 {
00286   // Set color property for arrow and line actors
00287   vtkProperty* aProperty = vtkProperty::New();
00288   aProperty->SetColor(theRed, theGreen, theBlue);
00289 
00290   myArrowActor->SetProperty(aProperty);
00291   myLineActor->SetProperty(aProperty);
00292 #ifndef IPAL21440
00293   myLabelActor->SetProperty(aProperty);
00294 #endif
00295 
00296   aProperty->Delete();
00297   
00298   // Set color property for label actor
00299 #ifdef IPAL21440
00300   vtkTextProperty* aTextProperty = vtkTextProperty::New();
00301   aTextProperty->SetColor(theRed, theGreen, theBlue);
00302 
00303   myLabelActor->SetTextProperty(aTextProperty);
00304 
00305   aTextProperty->Delete();
00306 #endif
00307 }
00308 
00311 void VTKViewer_Axis::SetSize(vtkFloatingPointType theSize)
00312 {
00313   vtkFloatingPointType aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
00314 
00315   vtkFloatingPointType aCoef = 0.99;
00316   vtkFloatingPointType aLinePosition[3] = {aPosition[0]*aCoef, aPosition[1]*aCoef, aPosition[2]*aCoef};
00317   myLineSource->SetPoint2(aLinePosition);
00318   
00319   myArrowActor->SetPosition(0.0,0.0,0.0);
00320   myArrowActor->AddPosition(aPosition);
00321   myArrowActor->SetOrientation(myRot);
00322   myArrowActor->SetScale(theSize / 10.);
00323   
00324 #ifdef IPAL21440
00325   if( vtkCoordinate* aCoord = myLabelActor->GetPositionCoordinate()->GetReferenceCoordinate() )
00326     aCoord->SetValue( aPosition );
00327 #else
00328   myLabelActor->SetPosition(0.0,0.0,0.0);
00329   myLabelActor->AddPosition(aPosition);
00330 #endif
00331 }
00332 
00337 bool VTKViewer_Axis::OwnActor(const vtkActor* theActor)
00338 {
00339   return theActor == myLineActor  || 
00340          theActor == myArrowActor ||
00341 #ifdef IPAL21440
00342          false;
00343 #else
00344          theActor == myLabelActor;
00345 #endif
00346 }
00347 
00351 class VTKViewer_XAxis : public VTKViewer_Axis
00352 {
00353 protected:
00354   VTKViewer_XAxis();
00355   VTKViewer_XAxis(const VTKViewer_XAxis&);
00356 public:
00357   vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
00358   static VTKViewer_XAxis *New();
00359 };
00360 
00361 vtkStandardNewMacro(VTKViewer_XAxis);
00362 
00364 VTKViewer_XAxis::VTKViewer_XAxis(){ 
00365   myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
00366   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
00367 #ifdef IPAL21440
00368   myTextMapper->SetInput("X");
00369 #else
00370   myVectorText->SetText("X");
00371 #endif
00372   SetColor(1.0,0.0,0.0);
00373 }
00374 
00378 class VTKViewer_YAxis : public VTKViewer_Axis{
00379 protected:
00380   VTKViewer_YAxis();
00381   VTKViewer_YAxis(const VTKViewer_YAxis&);
00382 public:
00383   vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
00384   static VTKViewer_YAxis *New();
00385 };
00386 
00387 vtkStandardNewMacro(VTKViewer_YAxis);
00388 
00390 VTKViewer_YAxis::VTKViewer_YAxis()
00391 { 
00392   myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
00393   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
00394 #ifdef IPAL21440
00395   myTextMapper->SetInput("Y");
00396 #else
00397   myVectorText->SetText("Y");
00398 #endif
00399   SetColor(0.0,1.0,0.0);
00400 }
00401 
00405 class VTKViewer_ZAxis : public VTKViewer_Axis
00406 {
00407 protected:
00408   VTKViewer_ZAxis();
00409   VTKViewer_ZAxis(const VTKViewer_ZAxis&);
00410 public:
00411   vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
00412   static VTKViewer_ZAxis *New();
00413 };
00414 
00415 vtkStandardNewMacro(VTKViewer_ZAxis);
00416 
00418 VTKViewer_ZAxis::VTKViewer_ZAxis()
00419 {
00420   myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
00421   myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
00422 #ifdef IPAL21440
00423   myTextMapper->SetInput("Z");
00424 #else
00425   myVectorText->SetText("Z");
00426 #endif
00427   SetColor(0.0,0.0,1.0);
00428 }
00429 
00430 vtkStandardNewMacro(VTKViewer_Trihedron);
00431 
00435 VTKViewer_Trihedron::VTKViewer_Trihedron()
00436 {
00437   myPresent = vtkActorCollection::New();
00438   myAxis[0] = VTKViewer_XAxis::New();
00439   myAxis[1] = VTKViewer_YAxis::New();
00440   myAxis[2] = VTKViewer_ZAxis::New();
00441   static vtkFloatingPointType aSize = 100;
00442   SetSize(aSize);
00443 }
00444 
00448 VTKViewer_Trihedron::~VTKViewer_Trihedron()
00449 {
00450   myPresent->RemoveAllItems();
00451   myPresent->Delete();
00452   for(int i = 0; i < 3; i++)
00453     myAxis[i]->Delete();
00454 }
00455 
00458 void VTKViewer_Trihedron::SetSize(vtkFloatingPointType theSize)
00459 {
00460   mySize = theSize;
00461   for(int i = 0; i < 3; i++)
00462     myAxis[i]->SetSize(theSize);
00463 }
00464 
00467 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
00468 {
00469   for(int i = 0; i < 3; i++)
00470     myAxis[i]->SetVisibility(theVis);
00471 }
00472 
00476 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
00477 {
00478   return myAxis[0]->GetVisibility();
00479 }
00480 
00484 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
00485 {
00486   vtkCamera* aCamera = theRenderer->GetActiveCamera();
00487   for(int i = 0; i < 3; i++){
00488     myAxis[i]->AddToRender(theRenderer);
00489     myAxis[i]->SetCamera(aCamera);
00490   }
00491 }
00492 
00496 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
00497 {
00498   myPresent->InitTraversal();
00499   while(vtkActor* anActor = myPresent->GetNextActor())
00500     theRenderer->RemoveActor(anActor);
00501   for(int i = 0; i < 3; i++)
00502     myAxis[i]->RemoveFromRender(theRenderer);
00503 }
00504 
00508 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
00509 {
00510   //TVisibility aVis = GetVisibility();
00511   //SetVisibility(eOff);
00512   VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
00513   vtkActorCollection* aCollection = aCopy.GetActors();
00514   aCollection->InitTraversal();
00515   int aCount = 0;
00516   while(vtkActor* prop = aCollection->GetNextActor()) {
00517     if( prop->GetVisibility()) {
00518       if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop)) {
00519         if(!anActor->IsInfinitive()) 
00520           aCount++;
00521       }
00522       else if ( !OwnActor( anActor ) ) {
00523         aCount++;
00524       }
00525         //int aCount = theRenderer->VisibleActorCount();
00526         //SetVisibility(aVis);
00527     }
00528   }
00529   return aCount;
00530 }
00531 
00536 bool VTKViewer_Trihedron::OwnActor(const vtkActor* theActor)
00537 {
00538   myPresent->InitTraversal();
00539   while(vtkActor* anActor = myPresent->GetNextActor()) {
00540     if ( anActor == theActor ) return true;
00541   }
00542   for(int i = 0; i < 3; i++) {
00543     if ( myAxis[i]->OwnActor(theActor) ) return true;
00544   }
00545   return false;
00546 }