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