3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GEOM_VTKTrihedron.cxx
25 // Author : Sergey LITONIN
29 #include "GEOM_VTKTrihedron.hxx"
30 #include "utilities.h"
31 #include "GEOM_OCCReader.h"
33 #include <Geom_Axis2Placement.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeEdge.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Compound.hxx>
43 #include <vtkMapper.h>
44 #include <vtkDataSet.h>
45 #include <vtkRenderer.h>
46 #include <vtkFollower.h>
47 #include <vtkRenderWindow.h>
48 #include <vtkObjectFactory.h>
49 #include <vtkPolyData.h>
51 #include <vtkProperty.h>
52 #include <vtkLineSource.h>
53 #include <vtkConeSource.h>
54 #include <vtkPolyDataMapper.h>
55 #include <vtkAppendPolyData.h>
56 #include <vtkMatrix4x4.h>
57 #include <vtkTransform.h>
59 #include "VTKViewer_VectorText.h"
64 Class : GEOM_VTKTrihedron
65 Description : Class for displaying axis of trihedron in VTK viewer.
66 Placement of trihedron is chahged with SetPlacement() method
68 class GEOM_VTKTrihedronAxis : public VTKViewer_Axis
72 GEOM_VTKTrihedronAxis();
73 GEOM_VTKTrihedronAxis( const GEOM_VTKTrihedronAxis& );
74 ~GEOM_VTKTrihedronAxis();
78 vtkTypeMacro( GEOM_VTKTrihedronAxis, VTKViewer_Axis );
79 static GEOM_VTKTrihedronAxis* New();
81 void SetAxis( const gp_Ax1& theAxis, const int theRot, float theColor[ 3 ] );
82 virtual void Render( vtkRenderer* theRenderer );
83 virtual void SetSize( float theSize );
84 gp_Pnt GetOri() const;
85 void SetColor( const float theColor[ 3 ] );
90 vtkMatrix4x4* myMatrix;
94 vtkStandardNewMacro( GEOM_VTKTrihedronAxis );
96 GEOM_VTKTrihedronAxis::GEOM_VTKTrihedronAxis()
98 vtkProperty* aProperty = vtkProperty::New();
99 aProperty->SetColor( 0.0, 0.0, 1.0 );
100 SetProperty( aProperty );
102 myMatrix = vtkMatrix4x4::New();
103 myTrsf = vtkTransform::New();
106 GEOM_VTKTrihedronAxis::~GEOM_VTKTrihedronAxis()
112 void GEOM_VTKTrihedronAxis::SetSize( float theSize )
114 float aPosition[ 3 ] = { myOri[ 0 ] + myDir[ 0 ] * theSize,
115 myOri[ 1 ] + myDir[ 1 ] * theSize,
116 myOri[ 2 ] + myDir[ 2 ] * theSize };
118 myLineSource->SetPoint1( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
119 myLineSource->SetPoint2( aPosition );
121 myArrowActor->SetPosition( 0, 0, 0 );
122 myArrowActor->SetOrientation( 0, 0, 0 );
123 myArrowActor->SetUserMatrix( 0 );
127 aTrsf.SetDisplacement( gp_Ax3( gp_Pnt( 0, 0, 0 ), gp_Dir( 1, 0, 0 ) ),
128 gp_Ax3( GetOri(), gp_Dir( myDir[ 0 ], myDir[ 1 ], myDir[ 2 ] ) ) );
130 const gp_XYZ& aTrans = aTrsf.TranslationPart();
131 gp_Mat aRot = aTrsf.VectorialPart();
133 for ( int i = 1; i <= 3; i++ )
134 for ( int j = 1; j <= 3; j++ )
135 myMatrix->SetElement( i - 1, j - 1, aRot( i, j ) );
137 myArrowActor->SetUserMatrix( myMatrix );
138 myArrowActor->SetPosition( aPosition );
140 myLabelActor->SetPosition( 0, 0, 0 );
141 myLabelActor->AddPosition( aPosition );
144 void GEOM_VTKTrihedronAxis::Render(vtkRenderer* theRenderer)
146 myLineActor->Render( theRenderer );
147 myArrowActor->Render( theRenderer );
148 myLabelActor->Render( theRenderer );
150 vtkCamera* aCamera = theRenderer->GetActiveCamera();
151 SetCamera( aCamera );
154 gp_Pnt GEOM_VTKTrihedronAxis::GetOri() const
156 return gp_Pnt( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
159 void GEOM_VTKTrihedronAxis::SetAxis( const gp_Ax1& theAxis,
161 float theColor[ 3 ] )
163 gp_Pnt aLoc = theAxis.Location();
164 gp_Dir aDir = theAxis.Direction();
166 myOri[ 0 ] = aLoc.X();
167 myOri[ 1 ] = aLoc.Y();
168 myOri[ 2 ] = aLoc.Z();
170 myDir[ 0 ] = aDir.X();
171 myDir[ 1 ] = aDir.Y();
172 myDir[ 2 ] = aDir.Z();
174 float aColor[ 3 ] = { 0, 0, 0 };
175 aColor[ theRot ] = 1;
176 vtkProperty* aProperty = vtkProperty::New();
177 if ( theColor[ 0 ] == -1 )
178 aProperty->SetColor( aColor[ 0 ], aColor[ 1 ], aColor[ 2 ] );
180 aProperty->SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
181 SetProperty( aProperty );
184 if ( theRot == 0 ) myVectorText->SetText( "X" );
185 else if ( theRot == 1 ) myVectorText->SetText( "Y" );
186 else if ( theRot == 2 ) myVectorText->SetText( "Z" );
189 void GEOM_VTKTrihedronAxis::SetColor( const float theColor[ 3 ] )
191 vtkProperty* aProperty = vtkProperty::New();
192 aProperty->SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
193 SetProperty( aProperty );
199 Class : GEOM_VTKTrihedron
200 Description : Class for displaying trihedron of local CS in VTK viewer.
201 Placement of trihedron is chahged with SetPlacement() method
204 vtkStandardNewMacro( GEOM_VTKTrihedron );
206 GEOM_VTKTrihedron::GEOM_VTKTrihedron()
208 myMapper = vtkPolyDataMapper::New();
209 myAxis[ 0 ] = myAxis[ 1 ] = myAxis[ 2 ] = 0;
210 myPresent = vtkActorCollection::New();
212 SetInfinitive( true );
213 myColor[ 0 ] = myColor[ 1 ] = myColor[ 1 ] = -1;
214 SetInfinitive( true );
217 GEOM_VTKTrihedron::~GEOM_VTKTrihedron()
219 myPresent->RemoveAllItems();
221 for ( int i = 0; i < 3; i++ )
228 void GEOM_VTKTrihedron::SetSize( float theSize )
231 for ( int i = 0; i < 3; i++ )
232 myAxis[i]->SetSize( theSize );
235 // Calculate new sensitive area
236 gp_Pnt anOri = ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->GetOri();
237 gp_Pnt aEndX( myLocation.XYZ() + myDirX.XYZ() * mySize );
238 gp_Pnt aEndY( myLocation.XYZ() + myDirY.XYZ() * mySize );
239 gp_Pnt aEndZ( myLocation.XYZ() + myDirZ.XYZ() * mySize );
241 vtkLineSource* aSrcX = vtkLineSource::New();
242 vtkLineSource* aSrcY = vtkLineSource::New();
243 vtkLineSource* aSrcZ = vtkLineSource::New();
245 aSrcX->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
246 aSrcX->SetPoint2( aEndX.X(), aEndX.Y(), aEndX.Z() );
248 aSrcY->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
249 aSrcY->SetPoint2( aEndY.X(), aEndY.Y(), aEndY.Z() );
251 aSrcZ->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
252 aSrcZ->SetPoint2( aEndZ.X(), aEndZ.Y(), aEndZ.Z() );
254 vtkAppendPolyData* aRes = vtkAppendPolyData::New();
255 aRes->AddInput( aSrcX->GetOutput() );
256 aRes->AddInput( aSrcY->GetOutput() );
257 aRes->AddInput( aSrcZ->GetOutput() );
259 myMapper->SetInput( aRes->GetOutput() );
260 SALOME_Actor::SetMapper( myMapper );
268 void GEOM_VTKTrihedron::SetVisibility( VTKViewer_Trihedron::TVisibility theVis )
270 for ( int i = 0; i < 3; i++ )
271 myAxis[i]->SetVisibility( theVis );
274 void GEOM_VTKTrihedron::AddToRender( vtkRenderer* theRenderer )
276 vtkCamera* aCamera = theRenderer->GetActiveCamera();
277 for(int i = 0; i < 3; i++)
279 myAxis[i]->AddToRender(theRenderer);
280 myAxis[i]->SetCamera(aCamera);
283 theRenderer->AddActor( this );
286 void GEOM_VTKTrihedron::RemoveFromRender( vtkRenderer* theRenderer )
288 myPresent->InitTraversal();
289 while(vtkActor* anActor = myPresent->GetNextActor())
290 theRenderer->RemoveActor(anActor);
293 int GEOM_VTKTrihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
295 vtkActorCollection* aCollection = theRenderer->GetActors();
296 aCollection->InitTraversal();
298 while( vtkActor* prop = aCollection->GetNextActor() )
299 if ( prop->GetVisibility() )
300 if ( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( prop ) )
301 if ( !anActor->IsInfinitive() )
304 vtkCamera* aCamera = theRenderer->GetActiveCamera();
305 for(int i = 0; i < 3; i++)
306 myAxis[i]->SetCamera( aCamera );
311 void GEOM_VTKTrihedron::SetCamera( vtkCamera* theCamera )
313 for(int i = 0; i < 3; i++)
314 myAxis[i]->SetCamera( theCamera );
317 void GEOM_VTKTrihedron::SetPlacement( const Handle(Geom_Axis2Placement)& thePlc )
319 gp_Ax2 anAx2 = thePlc->Ax2();
321 myDirX = anAx2.XDirection();
322 myDirY = anAx2.YDirection();
323 myDirZ = anAx2.Direction();
325 myLocation = anAx2.Axis().Location();
327 if ( myAxis[ 0 ] != 0 ) myAxis[ 0 ]->Delete();
328 if ( myAxis[ 1 ] != 0 ) myAxis[ 1 ]->Delete();
329 if ( myAxis[ 2 ] != 0 ) myAxis[ 2 ]->Delete();
331 myAxis[ 0 ] = GEOM_VTKTrihedronAxis::New();
332 myAxis[ 1 ] = GEOM_VTKTrihedronAxis::New();
333 myAxis[ 2 ] = GEOM_VTKTrihedronAxis::New();
335 ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetAxis( gp_Ax1( myLocation, myDirX ), 0, myColor );
336 ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetAxis( gp_Ax1( myLocation, myDirY ), 1, myColor );
337 ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetAxis( gp_Ax1( myLocation, myDirZ ), 2, myColor );
342 vtkMapper* GEOM_VTKTrihedron::GetMapper()
347 void GEOM_VTKTrihedron::Render(vtkRenderer* r, vtkMapper *)
349 ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->Render( r );
350 ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->Render( r );
351 ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->Render( r );
354 void GEOM_VTKTrihedron::SetColor( float r, float g, float b )
360 if ( myAxis[ 0 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetColor( myColor );
361 if ( myAxis[ 1 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetColor( myColor );
362 if ( myAxis[ 2 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetColor( myColor );
365 void GEOM_VTKTrihedron::GetColor( float& r, float& g, float& b )
372 bool GEOM_VTKTrihedron::IsSetCamera() const
377 bool GEOM_VTKTrihedron::IsResizable() const