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