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