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