Back to index

salome-geom  6.5.0
GEOM_VTKTrihedron.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 //  SALOME GEOM : 
00024 //  File   : GEOM_VTKTrihedron.cxx
00025 //  Author : Sergey LITONIN
00026 //  Module : SALOME
00027 //  $Header:
00028 //
00029 #include "GEOM_VTKTrihedron.hxx"
00030 
00031 #include <Geom_Axis2Placement.hxx>
00032 #include <gp_Ax2.hxx>
00033 #include <gp_Ax3.hxx>
00034 #include <gp_Pnt.hxx>
00035 
00036 #include <vtkMapper.h>
00037 #include <vtkRenderer.h>
00038 #include <vtkObjectFactory.h>
00039 #include <vtkActor.h>
00040 #include <vtkActorCollection.h>
00041 #include <vtkProperty.h>
00042 #include <vtkLineSource.h>
00043 #include <vtkPolyDataMapper.h>
00044 #include <vtkAppendPolyData.h>
00045 #include <vtkMatrix4x4.h>
00046 #include <vtkTransform.h>
00047 #include <vtkVectorText.h>
00048 #include <vtkCoordinate.h>
00049 #include <vtkTextActor.h>
00050 #include <vtkTextMapper.h>
00051 
00052 /*
00053   Class       : GEOM_VTKTrihedron
00054   Description : Class for displaying axis of trihedron in VTK viewer.
00055                 Placement of trihedron is chahged with SetPlacement() method
00056 */
00057 class GEOM_VTKTrihedronAxis : public VTKViewer_Axis
00058 {
00059 protected:
00060 
00061                                 GEOM_VTKTrihedronAxis();
00062                                 GEOM_VTKTrihedronAxis( const GEOM_VTKTrihedronAxis& );
00063                                 ~GEOM_VTKTrihedronAxis();
00064 
00065 public:
00066 
00067                                 vtkTypeMacro( GEOM_VTKTrihedronAxis, VTKViewer_Axis );
00068   static GEOM_VTKTrihedronAxis* New();
00069 
00070   void                          SetAxis( const gp_Ax1& theAxis, const int theRot, vtkFloatingPointType theColor[ 3 ] );
00071   virtual void                  Render( vtkRenderer* theRenderer );
00072   virtual void                  SetSize( vtkFloatingPointType theSize );
00073   gp_Pnt                        GetOri() const;
00074   void                          SetColor( const vtkFloatingPointType theColor[ 3 ] );
00075 
00076 private:
00077 
00078   vtkFloatingPointType          myOri[ 3 ];
00079   vtkMatrix4x4*                 myMatrix;
00080   vtkTransform*                 myTrsf;
00081 };
00082 
00083 vtkStandardNewMacro( GEOM_VTKTrihedronAxis );
00084 
00085 GEOM_VTKTrihedronAxis::GEOM_VTKTrihedronAxis()
00086 {
00087   VTKViewer_Axis::SetColor( 0.0, 0.0, 1.0 );
00088   myMatrix = vtkMatrix4x4::New();
00089   myTrsf = vtkTransform::New();
00090 }
00091 
00092 GEOM_VTKTrihedronAxis::~GEOM_VTKTrihedronAxis()
00093 {
00094   myMatrix->Delete();
00095   myTrsf->Delete();
00096 }
00097 
00098 void GEOM_VTKTrihedronAxis::SetSize( vtkFloatingPointType theSize )
00099 {
00100   vtkFloatingPointType aPosition[ 3 ] = { myOri[ 0 ] + myDir[ 0 ] * theSize,
00101                                           myOri[ 1 ] + myDir[ 1 ] * theSize,
00102                                           myOri[ 2 ] + myDir[ 2 ] * theSize };
00103                            
00104   myLineSource->SetPoint1( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
00105   myLineSource->SetPoint2( aPosition );
00106 
00107   myArrowActor->SetPosition( 0, 0, 0 );
00108   myArrowActor->SetOrientation( 0, 0, 0 );
00109   myArrowActor->SetUserMatrix( 0 );
00110   
00111 
00112   gp_Trsf aTrsf;
00113   aTrsf.SetDisplacement( gp_Ax3( gp_Pnt( 0, 0, 0 ), gp_Dir( 1, 0, 0 ) ),
00114                          gp_Ax3( GetOri(), gp_Dir( myDir[ 0 ], myDir[ 1 ], myDir[ 2 ] ) ) );
00115 
00116 //   const gp_XYZ& aTrans = aTrsf.TranslationPart();
00117   gp_Mat aRot = aTrsf.VectorialPart();
00118 
00119   for ( int i = 1; i <= 3; i++ )
00120     for ( int j = 1; j <= 3; j++ )
00121       myMatrix->SetElement( i - 1, j - 1, aRot( i, j ) );
00122 
00123   myArrowActor->SetUserMatrix( myMatrix );
00124   myArrowActor->SetPosition( aPosition );
00125     
00126 #ifdef IPAL21440
00127   if( vtkCoordinate* aCoord = myLabelActor->GetPositionCoordinate()->GetReferenceCoordinate() )
00128     aCoord->SetValue( aPosition );
00129 #else
00130   myLabelActor->SetPosition( 0, 0, 0 );
00131   myLabelActor->AddPosition( aPosition );
00132 #endif
00133 }
00134 
00135 void GEOM_VTKTrihedronAxis::Render(vtkRenderer* theRenderer)
00136 {
00137   myLineActor->Render( theRenderer );
00138   myArrowActor->Render( theRenderer );
00139 #ifndef IPAL21440
00140   myLabelActor->Render( theRenderer );
00141 #endif
00142 
00143   vtkCamera* aCamera = theRenderer->GetActiveCamera();
00144   SetCamera( aCamera );
00145 }
00146 
00147 gp_Pnt GEOM_VTKTrihedronAxis::GetOri() const
00148 {
00149   return gp_Pnt( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
00150 }
00151 
00152 void GEOM_VTKTrihedronAxis::SetAxis( const gp_Ax1& theAxis,
00153                                      const int     theRot,
00154                                      vtkFloatingPointType theColor[ 3 ] )
00155 {
00156   gp_Pnt aLoc = theAxis.Location();
00157   gp_Dir aDir = theAxis.Direction();
00158 
00159   myOri[ 0 ] = aLoc.X();
00160   myOri[ 1 ] = aLoc.Y();
00161   myOri[ 2 ] = aLoc.Z();
00162 
00163   myDir[ 0 ] = aDir.X();
00164   myDir[ 1 ] = aDir.Y();
00165   myDir[ 2 ] = aDir.Z();
00166 
00167   vtkFloatingPointType aColor[ 3 ] = { 0, 0, 0 };
00168   aColor[ theRot ] = 1;
00169   if ( theColor[ 0 ] == -1 )
00170     VTKViewer_Axis::SetColor( aColor[ 0 ], aColor[ 1 ], aColor[ 2 ] );
00171   else
00172     VTKViewer_Axis::SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
00173 
00174 #ifdef IPAL21440
00175   if      ( theRot == 0 ) myTextMapper->SetInput( "X" );
00176   else if ( theRot == 1 ) myTextMapper->SetInput( "Y" );
00177   else if ( theRot == 2 ) myTextMapper->SetInput( "Z" );
00178 #else
00179   if      ( theRot == 0 ) myVectorText->SetText( "X" );
00180   else if ( theRot == 1 ) myVectorText->SetText( "Y" );
00181   else if ( theRot == 2 ) myVectorText->SetText( "Z" );
00182 #endif
00183 }
00184 
00185 void GEOM_VTKTrihedronAxis::SetColor( const vtkFloatingPointType theColor[ 3 ] )
00186 {
00187   VTKViewer_Axis::SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
00188 }
00189 
00190 
00191 /*
00192   Class       : GEOM_VTKTrihedron
00193   Description : Class for displaying trihedron of local CS in VTK viewer.
00194                 Placement of trihedron is chahged with SetPlacement() method
00195 */
00196 
00197 vtkStandardNewMacro( GEOM_VTKTrihedron );
00198 
00199 GEOM_VTKTrihedron::GEOM_VTKTrihedron()
00200 {
00201   myMapper = vtkPolyDataMapper::New();
00202   myAxis[ 0 ] = myAxis[ 1 ] = myAxis[ 2 ] = 0;
00203   mySize = 100;
00204 
00205   myColor[ 0 ] = myColor[ 1 ] = myColor[ 2 ] = -1;
00206 
00207   myDefaultColor[ 0 ] = myDefaultColor[ 1 ] = myDefaultColor[ 2 ] = 1;
00208 
00209   myPreHighlightColor[ 0 ] = 0;
00210   myPreHighlightColor[ 1 ] = myPreHighlightColor[ 2 ] = 1;
00211 
00212   myHighlightColor[ 0 ] = myHighlightColor[ 1 ] = myHighlightColor[ 2 ] = 1;
00213 
00214   //SetInfinitive( true );
00215   SetPickable( true );
00216 }
00217 
00218 GEOM_VTKTrihedron::~GEOM_VTKTrihedron()
00219 {
00220   for ( int i = 0; i < 3; i++ )
00221     myAxis[i]->Delete();
00222 
00223   if ( myMapper )
00224     myMapper->Delete();
00225 }
00226 
00227 void GEOM_VTKTrihedron::SetSize( vtkFloatingPointType theSize )
00228 {
00229   mySize = theSize;
00230   for ( int i = 0; i < 3; i++ )
00231     myAxis[i]->SetSize( theSize );
00232 
00233 
00234   // Calculate new sensitive area
00235   gp_Pnt anOri = ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->GetOri();
00236   gp_Pnt aEndX( myLocation.XYZ() + myDirX.XYZ() * mySize );
00237   gp_Pnt aEndY( myLocation.XYZ() + myDirY.XYZ() * mySize );
00238   gp_Pnt aEndZ( myLocation.XYZ() + myDirZ.XYZ() * mySize );
00239 
00240   vtkLineSource* aSrcX = vtkLineSource::New();
00241   vtkLineSource* aSrcY = vtkLineSource::New();
00242   vtkLineSource* aSrcZ = vtkLineSource::New();
00243 
00244   aSrcX->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
00245   aSrcX->SetPoint2( aEndX.X(), aEndX.Y(), aEndX.Z() );
00246 
00247   aSrcY->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
00248   aSrcY->SetPoint2( aEndY.X(), aEndY.Y(), aEndY.Z() );
00249 
00250   aSrcZ->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
00251   aSrcZ->SetPoint2( aEndZ.X(), aEndZ.Y(), aEndZ.Z() );
00252 
00253   vtkAppendPolyData* aRes = vtkAppendPolyData::New();
00254   aRes->AddInput( aSrcX->GetOutput() );
00255   aRes->AddInput( aSrcY->GetOutput() );
00256   aRes->AddInput( aSrcZ->GetOutput() );
00257   
00258   myMapper->SetInput( aRes->GetOutput() );
00259   SALOME_Actor::SetMapper( myMapper );
00260 
00261   aSrcX->Delete();
00262   aSrcY->Delete();
00263   aSrcZ->Delete();
00264   aRes->Delete();
00265 }
00266 
00267 void GEOM_VTKTrihedron::SetVisibility( int theVisibility )
00268 {
00269   Superclass::SetVisibility( theVisibility );
00270   SetVisibility( theVisibility == 1 ? VTKViewer_Trihedron::eOn : VTKViewer_Trihedron::eOff );
00271 }
00272 
00273 void GEOM_VTKTrihedron::SetVisibility( VTKViewer_Trihedron::TVisibility theVis )
00274 {
00275   for ( int i = 0; i < 3; i++ )
00276     myAxis[i]->SetVisibility( theVis );
00277 }
00278 
00279 void GEOM_VTKTrihedron::AddToRender( vtkRenderer* theRenderer )
00280 {
00281   vtkCamera* aCamera = theRenderer->GetActiveCamera();
00282   for(int i = 0; i < 3; i++)
00283   {
00284     myAxis[i]->AddToRender(theRenderer);
00285     myAxis[i]->SetCamera(aCamera);
00286   }
00287 
00288   theRenderer->AddActor( this );
00289 }
00290 
00291 void GEOM_VTKTrihedron::RemoveFromRender( vtkRenderer* theRenderer )
00292 {
00293   for(int i = 0; i < 3; i++)
00294   {
00295     myAxis[i]->RemoveFromRender(theRenderer);
00296   }
00297 
00298   theRenderer->RemoveActor( this );
00299 }
00300 
00301 int GEOM_VTKTrihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
00302 {
00303   vtkActorCollection* aCollection = theRenderer->GetActors();
00304   aCollection->InitTraversal();
00305   int aCount = 0;
00306   while( vtkActor* prop = aCollection->GetNextActor() )
00307     if ( prop->GetVisibility() )
00308       if ( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( prop ) )
00309     if ( !anActor->IsInfinitive() )
00310       aCount++;
00311 
00312   vtkCamera* aCamera = theRenderer->GetActiveCamera();
00313   for(int i = 0; i < 3; i++)
00314     myAxis[i]->SetCamera( aCamera );
00315 
00316   return aCount;
00317 }
00318 
00319 void GEOM_VTKTrihedron::SetCamera( vtkCamera* theCamera )
00320 {
00321   for(int i = 0; i < 3; i++)
00322     myAxis[i]->SetCamera( theCamera );
00323 }
00324 
00325 void GEOM_VTKTrihedron::SetPlacement( const Handle(Geom_Axis2Placement)& thePlc )
00326 {
00327   gp_Ax2 anAx2 = thePlc->Ax2();
00328 
00329   myDirX = anAx2.XDirection();
00330   myDirY = anAx2.YDirection();
00331   myDirZ = anAx2.Direction();
00332 
00333   myLocation = anAx2.Axis().Location();
00334 
00335   if ( myAxis[ 0 ] != 0 ) myAxis[ 0 ]->Delete();
00336   if ( myAxis[ 1 ] != 0 ) myAxis[ 1 ]->Delete();
00337   if ( myAxis[ 2 ] != 0 ) myAxis[ 2 ]->Delete();
00338 
00339   myAxis[ 0 ] = GEOM_VTKTrihedronAxis::New();
00340   myAxis[ 1 ] = GEOM_VTKTrihedronAxis::New();
00341   myAxis[ 2 ] = GEOM_VTKTrihedronAxis::New();
00342 
00343   ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetAxis( gp_Ax1( myLocation, myDirX ), 0, myColor );
00344   ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetAxis( gp_Ax1( myLocation, myDirY ), 1, myColor );
00345   ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetAxis( gp_Ax1( myLocation, myDirZ ), 2, myColor );
00346 
00347   SetSize( mySize );
00348 }
00349 
00350 vtkMapper* GEOM_VTKTrihedron::GetMapper()
00351 {
00352   return myMapper;
00353 }
00354 
00355 void GEOM_VTKTrihedron::Render(vtkRenderer* r, vtkMapper *)
00356 {
00357   ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->Render( r );
00358   ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->Render( r );
00359   ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->Render( r );
00360 }
00361 
00362 void GEOM_VTKTrihedron::SetColor( vtkFloatingPointType r, vtkFloatingPointType g, vtkFloatingPointType b )
00363 {
00364   myColor[ 0 ] = r;
00365   myColor[ 1 ] = g;
00366   myColor[ 2 ] = b;
00367 
00368   if ( myAxis[ 0 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetColor( myColor );
00369   if ( myAxis[ 1 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetColor( myColor );
00370   if ( myAxis[ 2 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetColor( myColor );
00371 }
00372 
00373 void GEOM_VTKTrihedron::GetColor( vtkFloatingPointType& r, vtkFloatingPointType& g, vtkFloatingPointType& b )
00374 {
00375   r = myColor[ 0 ];
00376   g = myColor[ 1 ];
00377   b = myColor[ 2 ];
00378 }
00379 
00380 bool GEOM_VTKTrihedron::IsSetCamera() const
00381 {
00382   return true;
00383 }
00384 
00385 bool GEOM_VTKTrihedron::IsResizable() const
00386 {
00387   return false;
00388 }
00389 
00390 void GEOM_VTKTrihedron::Highlight( bool theIsHighlight )
00391 {
00392   if( theIsHighlight )
00393     SetAxesColors( myHighlightColor );
00394   else
00395     ResetAxesColors();
00396 
00397   Superclass::Highlight( theIsHighlight );
00398 }
00399 
00400 bool GEOM_VTKTrihedron::PreHighlight( vtkInteractorStyle *theInteractorStyle, 
00401                                       SVTK_SelectionEvent* theSelectionEvent,
00402                                       bool theIsHighlight )
00403 {
00404   if ( !GetPickable() )
00405     return false;  
00406 
00407   if ( !isHighlighted() )
00408   {
00409     if( theIsHighlight )
00410       SetAxesColors( myPreHighlightColor );
00411     else
00412       ResetAxesColors();
00413   }
00414 
00415   return Superclass::PreHighlight( theInteractorStyle, theSelectionEvent, theIsHighlight );
00416 }
00417 
00418 void GEOM_VTKTrihedron::ResetAxesColors()
00419 {
00420   if( myColor[0] != -1 )
00421     SetAxesColors( myColor );
00422   else
00423     SetAxesColors( myDefaultColor, true );
00424 }
00425 
00426 void GEOM_VTKTrihedron::SetAxesColors( vtkFloatingPointType theColor[3], bool theIsDiffuse )
00427 {
00428   myAxis[ 0 ]->SetColor( theColor[0], theIsDiffuse ? 0.0 : theColor[1], theIsDiffuse ? 0.0 : theColor[2] );
00429   myAxis[ 1 ]->SetColor( theIsDiffuse ? 0.0 : theColor[0], theColor[1], theIsDiffuse ? 0.0 : theColor[2] );
00430   myAxis[ 2 ]->SetColor( theIsDiffuse ? 0.0 : theColor[0], theIsDiffuse ? 0.0 : theColor[1], theColor[2] );
00431 }