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