Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / OBJECT / GEOM_VTKTrihedron.cxx
1 //  SALOME GEOM : 
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : GEOM_VTKTrihedron.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SALOME
27 //  $Header:
28
29 #include "GEOM_VTKTrihedron.hxx"
30 #include "utilities.h"
31 #include "GEOM_OCCReader.h"
32
33 #include <Geom_Axis2Placement.hxx>
34 #include <gp_Ax2.hxx>
35 #include <gp_Ax3.hxx>
36 #include <gp_Pnt.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeEdge.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Compound.hxx>
41
42 #include <vtkMath.h>
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>
50 #include <vtkActor.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>
58 #include <vtkVectorText.h>
59
60 /*
61   Class       : GEOM_VTKTrihedron
62   Description : Class for displaying axis of trihedron in VTK viewer.
63                 Placement of trihedron is chahged with SetPlacement() method
64 */
65 class GEOM_VTKTrihedronAxis : public VTKViewer_Axis
66 {
67 protected:
68
69                                 GEOM_VTKTrihedronAxis();
70                                 GEOM_VTKTrihedronAxis( const GEOM_VTKTrihedronAxis& );
71                                 ~GEOM_VTKTrihedronAxis();
72
73 public:
74
75                                 vtkTypeMacro( GEOM_VTKTrihedronAxis, VTKViewer_Axis );
76   static GEOM_VTKTrihedronAxis* New();
77
78   void                          SetAxis( const gp_Ax1& theAxis, const int theRot, vtkFloatingPointType theColor[ 3 ] );
79   virtual void                  Render( vtkRenderer* theRenderer );
80   virtual void                  SetSize( vtkFloatingPointType theSize );
81   gp_Pnt                        GetOri() const;
82   void                          SetColor( const vtkFloatingPointType theColor[ 3 ] );
83
84 private:
85
86   vtkFloatingPointType          myOri[ 3 ];
87   vtkMatrix4x4*                 myMatrix;
88   vtkTransform*                 myTrsf;
89 };
90
91 vtkStandardNewMacro( GEOM_VTKTrihedronAxis );
92
93 GEOM_VTKTrihedronAxis::GEOM_VTKTrihedronAxis()
94 {
95   vtkProperty* aProperty = vtkProperty::New();
96   aProperty->SetColor( 0.0, 0.0, 1.0 );
97   SetProperty( aProperty );
98   aProperty->Delete();
99   myMatrix = vtkMatrix4x4::New();
100   myTrsf = vtkTransform::New();
101 }
102
103 GEOM_VTKTrihedronAxis::~GEOM_VTKTrihedronAxis()
104 {
105   myMatrix->Delete();
106   myTrsf->Delete();
107 }
108
109 void GEOM_VTKTrihedronAxis::SetSize( vtkFloatingPointType theSize )
110 {
111   vtkFloatingPointType aPosition[ 3 ] = { myOri[ 0 ] + myDir[ 0 ] * theSize,
112                                           myOri[ 1 ] + myDir[ 1 ] * theSize,
113                                           myOri[ 2 ] + myDir[ 2 ] * theSize };
114                            
115   myLineSource->SetPoint1( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
116   myLineSource->SetPoint2( aPosition );
117
118   myArrowActor->SetPosition( 0, 0, 0 );
119   myArrowActor->SetOrientation( 0, 0, 0 );
120   myArrowActor->SetUserMatrix( 0 );
121   
122
123   gp_Trsf aTrsf;
124   aTrsf.SetDisplacement( gp_Ax3( gp_Pnt( 0, 0, 0 ), gp_Dir( 1, 0, 0 ) ),
125                          gp_Ax3( GetOri(), gp_Dir( myDir[ 0 ], myDir[ 1 ], myDir[ 2 ] ) ) );
126
127   const gp_XYZ& aTrans = aTrsf.TranslationPart();
128   gp_Mat aRot = aTrsf.VectorialPart();
129
130   for ( int i = 1; i <= 3; i++ )
131     for ( int j = 1; j <= 3; j++ )
132       myMatrix->SetElement( i - 1, j - 1, aRot( i, j ) );
133
134   myArrowActor->SetUserMatrix( myMatrix );
135   myArrowActor->SetPosition( aPosition );
136     
137   myLabelActor->SetPosition( 0, 0, 0 );
138   myLabelActor->AddPosition( aPosition );
139 }
140
141 void GEOM_VTKTrihedronAxis::Render(vtkRenderer* theRenderer)
142 {
143   myLineActor->Render( theRenderer );
144   myArrowActor->Render( theRenderer );
145   myLabelActor->Render( theRenderer );
146
147   vtkCamera* aCamera = theRenderer->GetActiveCamera();
148   SetCamera( aCamera );
149 }
150
151 gp_Pnt GEOM_VTKTrihedronAxis::GetOri() const
152 {
153   return gp_Pnt( myOri[ 0 ], myOri[ 1 ], myOri[ 2 ] );
154 }
155
156 void GEOM_VTKTrihedronAxis::SetAxis( const gp_Ax1& theAxis,
157                                      const int     theRot,
158                                      vtkFloatingPointType theColor[ 3 ] )
159 {
160   gp_Pnt aLoc = theAxis.Location();
161   gp_Dir aDir = theAxis.Direction();
162
163   myOri[ 0 ] = aLoc.X();
164   myOri[ 1 ] = aLoc.Y();
165   myOri[ 2 ] = aLoc.Z();
166
167   myDir[ 0 ] = aDir.X();
168   myDir[ 1 ] = aDir.Y();
169   myDir[ 2 ] = aDir.Z();
170
171   vtkFloatingPointType aColor[ 3 ] = { 0, 0, 0 };
172   aColor[ theRot ] = 1;
173   vtkProperty* aProperty = vtkProperty::New();
174   if ( theColor[ 0 ] == -1 )
175     aProperty->SetColor( aColor[ 0 ], aColor[ 1 ], aColor[ 2 ] );
176   else
177     aProperty->SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
178   SetProperty( aProperty );
179   aProperty->Delete();
180
181   if      ( theRot == 0 ) myVectorText->SetText( "X" );
182   else if ( theRot == 1 ) myVectorText->SetText( "Y" );
183   else if ( theRot == 2 ) myVectorText->SetText( "Z" );
184 }
185
186 void GEOM_VTKTrihedronAxis::SetColor( const vtkFloatingPointType theColor[ 3 ] )
187 {
188   vtkProperty* aProperty = vtkProperty::New();
189   aProperty->SetColor( theColor[ 0 ], theColor[ 1 ], theColor[ 2 ] );
190   SetProperty( aProperty );
191   aProperty->Delete();
192 }
193
194
195 /*
196   Class       : GEOM_VTKTrihedron
197   Description : Class for displaying trihedron of local CS in VTK viewer.
198                 Placement of trihedron is chahged with SetPlacement() method
199 */
200
201 vtkStandardNewMacro( GEOM_VTKTrihedron );
202
203 GEOM_VTKTrihedron::GEOM_VTKTrihedron()
204 {
205   myMapper = vtkPolyDataMapper::New();
206   myAxis[ 0 ] = myAxis[ 1 ] = myAxis[ 2 ] = 0;
207   mySize = 100;
208   SetInfinitive( true );
209   myColor[ 0 ] = myColor[ 1 ] = myColor[ 1 ] = -1;
210   SetInfinitive( true );
211 }
212
213 GEOM_VTKTrihedron::~GEOM_VTKTrihedron()
214 {
215   for ( int i = 0; i < 3; i++ )
216     myAxis[i]->Delete();
217
218   if ( myMapper )
219     myMapper->Delete();
220 }
221
222 void GEOM_VTKTrihedron::SetSize( vtkFloatingPointType theSize )
223 {
224   mySize = theSize;
225   for ( int i = 0; i < 3; i++ )
226     myAxis[i]->SetSize( theSize );
227
228
229   // Calculate new sensitive area
230   gp_Pnt anOri = ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->GetOri();
231   gp_Pnt aEndX( myLocation.XYZ() + myDirX.XYZ() * mySize );
232   gp_Pnt aEndY( myLocation.XYZ() + myDirY.XYZ() * mySize );
233   gp_Pnt aEndZ( myLocation.XYZ() + myDirZ.XYZ() * mySize );
234
235   vtkLineSource* aSrcX = vtkLineSource::New();
236   vtkLineSource* aSrcY = vtkLineSource::New();
237   vtkLineSource* aSrcZ = vtkLineSource::New();
238
239   aSrcX->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
240   aSrcX->SetPoint2( aEndX.X(), aEndX.Y(), aEndX.Z() );
241
242   aSrcY->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
243   aSrcY->SetPoint2( aEndY.X(), aEndY.Y(), aEndY.Z() );
244
245   aSrcZ->SetPoint1( anOri.X(), anOri.Y(), anOri.Z() );
246   aSrcZ->SetPoint2( aEndZ.X(), aEndZ.Y(), aEndZ.Z() );
247
248   vtkAppendPolyData* aRes = vtkAppendPolyData::New();
249   aRes->AddInput( aSrcX->GetOutput() );
250   aRes->AddInput( aSrcY->GetOutput() );
251   aRes->AddInput( aSrcZ->GetOutput() );
252   
253   myMapper->SetInput( aRes->GetOutput() );
254   SALOME_Actor::SetMapper( myMapper );
255
256   aSrcX->Delete();
257   aSrcY->Delete();
258   aSrcZ->Delete();
259   aRes->Delete();
260 }
261
262 void GEOM_VTKTrihedron::SetVisibility( VTKViewer_Trihedron::TVisibility theVis )
263 {
264   for ( int i = 0; i < 3; i++ )
265     myAxis[i]->SetVisibility( theVis );
266 }
267
268 void GEOM_VTKTrihedron::AddToRender( vtkRenderer* theRenderer )
269 {
270   vtkCamera* aCamera = theRenderer->GetActiveCamera();
271   for(int i = 0; i < 3; i++)
272   {
273     myAxis[i]->AddToRender(theRenderer);
274     myAxis[i]->SetCamera(aCamera);
275   }
276
277   theRenderer->AddActor( this );
278 }
279
280 void GEOM_VTKTrihedron::RemoveFromRender( vtkRenderer* theRenderer )
281 {
282   for(int i = 0; i < 3; i++)
283   {
284     myAxis[i]->RemoveFromRender(theRenderer);
285   }
286
287   theRenderer->RemoveActor( this );
288 }
289
290 int GEOM_VTKTrihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
291 {
292   vtkActorCollection* aCollection = theRenderer->GetActors();
293   aCollection->InitTraversal();
294   int aCount = 0;
295   while( vtkActor* prop = aCollection->GetNextActor() )
296     if ( prop->GetVisibility() )
297       if ( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( prop ) )
298     if ( !anActor->IsInfinitive() )
299       aCount++;
300
301   vtkCamera* aCamera = theRenderer->GetActiveCamera();
302   for(int i = 0; i < 3; i++)
303     myAxis[i]->SetCamera( aCamera );
304
305   return aCount;
306 }
307
308 void GEOM_VTKTrihedron::SetCamera( vtkCamera* theCamera )
309 {
310   for(int i = 0; i < 3; i++)
311     myAxis[i]->SetCamera( theCamera );
312 }
313
314 void GEOM_VTKTrihedron::SetPlacement( const Handle(Geom_Axis2Placement)& thePlc )
315 {
316   gp_Ax2 anAx2 = thePlc->Ax2();
317
318   myDirX = anAx2.XDirection();
319   myDirY = anAx2.YDirection();
320   myDirZ = anAx2.Direction();
321
322   myLocation = anAx2.Axis().Location();
323
324   if ( myAxis[ 0 ] != 0 ) myAxis[ 0 ]->Delete();
325   if ( myAxis[ 1 ] != 0 ) myAxis[ 1 ]->Delete();
326   if ( myAxis[ 2 ] != 0 ) myAxis[ 2 ]->Delete();
327
328   myAxis[ 0 ] = GEOM_VTKTrihedronAxis::New();
329   myAxis[ 1 ] = GEOM_VTKTrihedronAxis::New();
330   myAxis[ 2 ] = GEOM_VTKTrihedronAxis::New();
331
332   ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetAxis( gp_Ax1( myLocation, myDirX ), 0, myColor );
333   ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetAxis( gp_Ax1( myLocation, myDirY ), 1, myColor );
334   ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetAxis( gp_Ax1( myLocation, myDirZ ), 2, myColor );
335
336   SetSize( mySize );
337 }
338
339 vtkMapper* GEOM_VTKTrihedron::GetMapper()
340 {
341   return myMapper;
342 }
343
344 void GEOM_VTKTrihedron::Render(vtkRenderer* r, vtkMapper *)
345 {
346   ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->Render( r );
347   ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->Render( r );
348   ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->Render( r );
349 }
350
351 void GEOM_VTKTrihedron::SetColor( vtkFloatingPointType r, vtkFloatingPointType g, vtkFloatingPointType b )
352 {
353   myColor[ 0 ] = r;
354   myColor[ 1 ] = g;
355   myColor[ 2 ] = b;
356
357   if ( myAxis[ 0 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 0 ] )->SetColor( myColor );
358   if ( myAxis[ 1 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 1 ] )->SetColor( myColor );
359   if ( myAxis[ 2 ] ) ( (GEOM_VTKTrihedronAxis*)myAxis[ 2 ] )->SetColor( myColor );
360 }
361
362 void GEOM_VTKTrihedron::GetColor( vtkFloatingPointType& r, vtkFloatingPointType& g, vtkFloatingPointType& b )
363 {
364   r = myColor[ 0 ];
365   g = myColor[ 1 ];
366   b = myColor[ 2 ];
367 }
368
369 bool GEOM_VTKTrihedron::IsSetCamera() const
370 {
371   return true;
372 }
373
374 bool GEOM_VTKTrihedron::IsResizable() const
375 {
376   return true;
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393