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