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