Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / VTKViewer / VTKViewer_Trihedron.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "VTKViewer_Trihedron.h"
20 #include "VTKViewer_Actor.h"
21
22 // VTK Includes
23 #include <vtkMath.h>
24 #include <vtkMapper.h>
25 #include <vtkDataSet.h>
26 #include <vtkRenderer.h>
27 #include <vtkRenderWindow.h>
28 #include <vtkObjectFactory.h>
29
30 #include <vtkActor.h>
31 #include <vtkProperty.h>
32 #include <vtkLineSource.h>
33 #include <vtkConeSource.h>
34 #include <vtkPolyDataMapper.h>
35
36 #include "VTKViewer_VectorText.h"
37
38 vtkStandardNewMacro(VTKViewer_UnScaledActor);
39
40 /*!Constructor*/
41 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor() 
42 {
43   Bounds[0] = Bounds[2] = Bounds[4] = VTK_LARGE_FLOAT;
44   Bounds[1] = Bounds[3] = Bounds[5] = -VTK_LARGE_FLOAT;
45 }
46
47 /*!
48   \return bounding box
49 */
50 vtkFloatingPointType* 
51 VTKViewer_UnScaledActor
52 ::GetBounds()
53 {
54   return Bounds;
55 }
56
57 /*! Sets \a mySize= \a theSize variable.
58  * \param  theSize - integer size
59  */
60 void VTKViewer_UnScaledActor::SetSize(int theSize)
61 {
62   mySize = theSize;
63 }
64
65 /*!This causes the actor to be rendered.
66  * Set new scale for actor.
67  */
68 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer)
69 {
70   if(theRenderer){
71     vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
72     theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
73     theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
74     vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
75                                            (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
76                                            (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
77     int* aSize = theRenderer->GetRenderWindow()->GetSize();
78     vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
79     vtkDataSet* aDataSet = GetMapper()->GetInput();
80     aDataSet->Update();
81     vtkFloatingPointType aLength = aDataSet->GetLength();
82     vtkFloatingPointType aPrecision = 1.0E-3;
83     vtkFloatingPointType anOldScale = GetScale()[0];
84     vtkFloatingPointType aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(aSize[0])/vtkFloatingPointType(aSize[1]));
85     if(fabs(aScale - anOldScale)/aScale > aPrecision){
86       SetScale(aScale);
87     }
88   }
89   vtkFollower::Render(theRenderer);
90 }
91
92 vtkStandardNewMacro(VTKViewer_LineActor);
93
94 vtkCxxSetObjectMacro(VTKViewer_LineActor,LabelActor,VTKViewer_UnScaledActor);
95 vtkCxxSetObjectMacro(VTKViewer_LineActor,ArrowActor,VTKViewer_UnScaledActor);
96
97 /*!Adds Label and Arrow actors to \a theRenderer.*/
98 void VTKViewer_LineActor::Render(vtkRenderer *theRenderer)
99 {
100   if(LabelActor && LabelActor->GetVisibility()){
101     LabelActor->Modified();
102     LabelActor->Render(theRenderer);
103   }
104   if(ArrowActor && ArrowActor->GetVisibility()){
105     ArrowActor->Modified();
106     ArrowActor->Render(theRenderer);
107   }
108   vtkFollower::Render(theRenderer);
109 }
110
111 /*!
112   Constructor
113 */
114 VTKViewer_Axis::VTKViewer_Axis()
115 {
116   /*! \li Initialize the Line pipe-line representation*/
117   myLineSource = vtkLineSource::New();
118   myLineSource->SetPoint1(0.0,0.0,0.0);
119   
120   myMapper[0] = vtkPolyDataMapper::New();
121   myMapper[0]->SetInput(myLineSource->GetOutput());
122   
123   myLineActor = VTKViewer_LineActor::New();
124   myLineActor->SetMapper(myMapper[0]);
125   myLineActor->PickableOff();
126   
127   /*! \li Initialize the Arrow pipe-line representation*/
128   myConeSource =  vtkConeSource::New();
129   myConeSource->SetResolution(2);
130   myConeSource->SetAngle(10);
131   
132   myMapper[1] = vtkPolyDataMapper::New();
133   myMapper[1]->SetInput(myConeSource->GetOutput());
134   
135   myArrowActor = VTKViewer_UnScaledActor::New();
136   myArrowActor->SetMapper(myMapper[1]);
137   static int aArrowActorSize = 24;
138   myArrowActor->SetSize(aArrowActorSize);
139   myArrowActor->PickableOff();
140   
141   myLineActor->SetArrowActor(myArrowActor);
142   
143   /*! \li Initialize the Label pipe-line representation */
144   myVectorText = VTKViewer_VectorText::New();
145   
146   myMapper[2] = vtkPolyDataMapper::New();
147   myMapper[2]->SetInput(myVectorText->GetOutput());
148   
149   myLabelActor = VTKViewer_UnScaledActor::New();
150   myLabelActor->SetMapper(myMapper[2]);
151   static int aLabelActorSize = 12;
152   myLabelActor->SetSize(aLabelActorSize);
153   myLabelActor->PickableOff();
154   //myLabelActor->DebugOn();
155   
156   myLineActor->SetLabelActor(myLabelActor);
157   
158   /*! \li Initialise visibility param.*/
159   myVisibility = VTKViewer_Trihedron::eOn;
160 }
161
162 /*!
163   Destructor
164 */
165 VTKViewer_Axis::~VTKViewer_Axis()
166 {
167   /*! \li Destroy of the Label pipe-line representation */
168   myLabelActor->Delete();
169   
170   myMapper[0]->RemoveAllInputs();
171   myMapper[0]->Delete();
172   
173   myVectorText->Delete();
174   
175   /*! \li Destroy of the Arrow pipe-line representation */
176   myArrowActor->Delete();
177   
178   myMapper[1]->RemoveAllInputs();
179   myMapper[1]->Delete();
180   
181   myConeSource->Delete();
182   
183   /*! \li Destroy of the Line pipe-line representation */
184   myLineActor->Delete();
185   
186   myMapper[2]->RemoveAllInputs();
187   myMapper[2]->Delete();
188   
189   myLineSource->Delete();
190 }
191
192 /*! Add to renderer
193  * \param theRenderer - vtkRenderer pointer
194  */
195 void VTKViewer_Axis::AddToRender(vtkRenderer* theRenderer){
196   /*! \li Order of the calls are important*/
197   theRenderer->AddActor(myLineActor);
198   theRenderer->AddActor(myLabelActor);
199   theRenderer->AddActor(myArrowActor);
200 }
201
202 /*! Remove actor of acis from \a theRenderer which are in myPresent.
203  * \param theRenderer - vtkRenderer pointer
204  */
205 void VTKViewer_Axis::RemoveFromRender(vtkRenderer* theRenderer){
206   /*! \li Order of the calls are important*/
207   theRenderer->RemoveActor(myLineActor);
208   theRenderer->RemoveActor(myLabelActor);
209   theRenderer->RemoveActor(myArrowActor);
210 }
211
212 /*! Sets visibility for all Axis to \a theVis*/
213 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis)
214 {
215   switch(theVis){
216   case VTKViewer_Trihedron::eOff:
217   case VTKViewer_Trihedron::eOn:
218     myLabelActor->SetVisibility(theVis);
219     myArrowActor->SetVisibility(theVis);
220     myLineActor->SetVisibility(theVis);
221     break;
222   case VTKViewer_Trihedron::eOnlyLineOn:
223     myLabelActor->VisibilityOff();
224     myArrowActor->VisibilityOff();
225     myLineActor->VisibilityOn();
226     break;
227   default:
228     return;
229   }
230   myVisibility = theVis;
231 }
232
233 /*! Set camera for myLabelActor
234  */
235 void VTKViewer_Axis::SetCamera(vtkCamera* theCamera){
236   myLabelActor->SetCamera(theCamera);
237 }
238
239 /*! Sets \a theProperty for actors: myLineActor,myLabelActor,myArrowActor
240  */
241 void VTKViewer_Axis::SetProperty(vtkProperty* theProperty){
242   myLabelActor->SetProperty(theProperty);
243   myArrowActor->SetProperty(theProperty);
244   myLineActor->SetProperty(theProperty);
245 }
246
247 /*! Set size of VTKViewer_Axis
248  */
249 void VTKViewer_Axis::SetSize(vtkFloatingPointType theSize)
250 {
251   vtkFloatingPointType aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
252   myLineSource->SetPoint2(aPosition);
253   
254   myArrowActor->SetPosition(0.0,0.0,0.0);
255   myArrowActor->AddPosition(aPosition);
256   myArrowActor->SetOrientation(myRot);
257   
258   myLabelActor->SetPosition(0.0,0.0,0.0);
259   myLabelActor->AddPosition(aPosition);
260 }
261
262 /*! \class VTKViewer_XAxis
263  * \brief X Axis actor
264  */
265 class VTKViewer_XAxis : public VTKViewer_Axis
266 {
267 protected:
268   VTKViewer_XAxis();
269   VTKViewer_XAxis(const VTKViewer_XAxis&);
270 public:
271   vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
272   static VTKViewer_XAxis *New();
273 };
274
275 vtkStandardNewMacro(VTKViewer_XAxis);
276
277 /*!Initialize X Axis*/
278 VTKViewer_XAxis::VTKViewer_XAxis(){ 
279   myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
280   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
281   myVectorText->SetText("X");
282   vtkProperty* aProperty = vtkProperty::New();
283   aProperty->SetColor(1.0,0.0,0.0);
284   SetProperty(aProperty);
285   aProperty->Delete();
286 }
287
288 /*! \class VTKViewer_YAxis
289  * \brief Y Axis actor
290  */
291 class VTKViewer_YAxis : public VTKViewer_Axis{
292 protected:
293   VTKViewer_YAxis();
294   VTKViewer_YAxis(const VTKViewer_YAxis&);
295 public:
296   vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
297   static VTKViewer_YAxis *New();
298 };
299
300 vtkStandardNewMacro(VTKViewer_YAxis);
301
302 /*!Initialize Y Axis*/
303 VTKViewer_YAxis::VTKViewer_YAxis()
304
305   myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
306   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
307   myVectorText->SetText("Y");
308   vtkProperty* aProperty = vtkProperty::New();
309   aProperty->SetColor(0.0,1.0,0.0);
310   SetProperty(aProperty);
311   aProperty->Delete();
312 }
313
314 /*! \class VTKViewer_ZAxis
315  * \brief Z Axis actor
316  */
317 class VTKViewer_ZAxis : public VTKViewer_Axis
318 {
319 protected:
320   VTKViewer_ZAxis();
321   VTKViewer_ZAxis(const VTKViewer_ZAxis&);
322 public:
323   vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
324   static VTKViewer_ZAxis *New();
325 };
326
327 vtkStandardNewMacro(VTKViewer_ZAxis);
328
329 /*!Initialize Z Axis*/
330 VTKViewer_ZAxis::VTKViewer_ZAxis()
331 {
332   myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
333   myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
334   myVectorText->SetText("Z");
335   vtkProperty* aProperty = vtkProperty::New();
336   aProperty->SetColor(0.0,0.0,1.0);
337   SetProperty(aProperty);
338   aProperty->Delete();
339 }
340
341 vtkStandardNewMacro(VTKViewer_Trihedron);
342
343 /*!
344   Constructor
345 */
346 VTKViewer_Trihedron::VTKViewer_Trihedron()
347 {
348   myPresent = vtkActorCollection::New();
349   myAxis[0] = VTKViewer_XAxis::New();
350   myAxis[1] = VTKViewer_YAxis::New();
351   myAxis[2] = VTKViewer_ZAxis::New();
352   static vtkFloatingPointType aSize = 100;
353   SetSize(aSize);
354 }
355
356 /*!
357   Destructor
358 */
359 VTKViewer_Trihedron::~VTKViewer_Trihedron()
360 {
361   myPresent->RemoveAllItems();
362   myPresent->Delete();
363   for(int i = 0; i < 3; i++)
364     myAxis[i]->Delete();
365 }
366
367 /*! Set size of axes
368  */
369 void VTKViewer_Trihedron::SetSize(vtkFloatingPointType theSize)
370 {
371   mySize = theSize;
372   for(int i = 0; i < 3; i++)
373     myAxis[i]->SetSize(theSize);
374 }
375
376 /*! Set visibility of axes
377  */
378 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis)
379 {
380   for(int i = 0; i < 3; i++)
381     myAxis[i]->SetVisibility(theVis);
382 }
383
384 /*!
385   \return visibility of first axis
386 */
387 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility()
388 {
389   return myAxis[0]->GetVisibility();
390 }
391
392 /*! Add to render all Axis
393  * \param theRenderer - vtkRenderer pointer
394  */
395 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer)
396 {
397   vtkCamera* aCamera = theRenderer->GetActiveCamera();
398   for(int i = 0; i < 3; i++){
399     myAxis[i]->AddToRender(theRenderer);
400     myAxis[i]->SetCamera(aCamera);
401   }
402 }
403
404 /*! Remove all actors from \a theRenderer which are in myPresent.
405  * \param theRenderer - vtkRenderer pointer
406  */
407 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer)
408 {
409   myPresent->InitTraversal();
410   while(vtkActor* anActor = myPresent->GetNextActor())
411     theRenderer->RemoveActor(anActor);
412   for(int i = 0; i < 3; i++)
413     myAxis[i]->RemoveFromRender(theRenderer);
414 }
415
416 /*! Return count of visible actors.
417  * \param theRenderer - vtkRenderer pointer
418  */
419 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer)
420 {
421   //TVisibility aVis = GetVisibility();
422   //SetVisibility(eOff);
423   vtkActorCollection* aCollection = theRenderer->GetActors();
424   aCollection->InitTraversal();
425   int aCount = 0;
426   while(vtkActor* prop = aCollection->GetNextActor()) {
427     if( prop->GetVisibility())
428       if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(prop))
429         if(!anActor->IsInfinitive()) 
430           aCount++;
431         //int aCount = theRenderer->VisibleActorCount();
432         //SetVisibility(aVis);
433   }
434   return aCount;
435 }