Salome HOME
PR: merge SalomePro 1_2d = mergefrom_BRANCH_MergeV1_2d
[modules/kernel.git] / src / VTKViewer / VTKViewer_Trihedron.cxx
1 //  SALOME VTKViewer : 
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VTKViewer_Trihedron.cxx
25 //  Author : Alexey PETROV
26 //  Module : SALOME
27 //  $Header: 
28
29 #include "VTKViewer_Trihedron.h"
30 #include "utilities.h"
31 #include "SALOME_Actor.h"
32
33 // VTK Includes
34 #include <vtkMath.h>
35 #include <vtkMapper.h>
36 #include <vtkDataSet.h>
37 #include <vtkRenderer.h>
38 #include <vtkFollower.h>
39 #include <vtkRenderWindow.h>
40 #include <vtkObjectFactory.h>
41
42 #include <vtkActor.h>
43 #include <vtkProperty.h>
44 #include <vtkLineSource.h>
45 #include <vtkConeSource.h>
46 #include <vtkPolyDataMapper.h>
47
48 #include "VTKViewer_VectorText.h"
49
50 using namespace std;
51
52 //==============================================================================
53
54 class VTKViewer_UnScaledActor: public vtkFollower{
55   VTKViewer_UnScaledActor(const VTKViewer_UnScaledActor&);
56
57 public:
58   vtkTypeMacro(VTKViewer_UnScaledActor,vtkFollower);
59   static VTKViewer_UnScaledActor *New();
60
61   virtual void SetSize(int theSize);
62   virtual void Render(vtkRenderer *theRenderer);
63
64 protected:
65   VTKViewer_UnScaledActor();
66   int mySize;
67 };
68
69 vtkStandardNewMacro(VTKViewer_UnScaledActor);
70
71 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor(){}
72
73 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer){
74   if(theRenderer){
75     float P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
76     theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
77     theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
78     float aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
79                             (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
80                             (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
81     int* aSize = theRenderer->GetRenderWindow()->GetSize();
82     float aWinDiag = sqrt(float(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
83     vtkDataSet* aDataSet = GetMapper()->GetInput();
84     float aLength = aDataSet->GetLength();
85     float aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(float(aSize[0])/float(aSize[1]));
86     SetScale(aScale);
87   }
88   vtkFollower::Render(theRenderer);
89 }
90
91 void VTKViewer_UnScaledActor::SetSize(int theSize){
92   mySize = theSize;
93 }
94
95 //==============================================================================
96
97 // The base class for concreate Axis
98 // Its only duty is to give correct initialization and destruction
99 //   of its pipe-lines
100 class VTKViewer_Axis : public vtkObject{
101 protected:
102   VTKViewer_Axis();
103   VTKViewer_Axis(const VTKViewer_Axis&);
104   virtual ~VTKViewer_Axis();
105  public:
106   vtkTypeMacro(VTKViewer_Axis,vtkObject);
107   
108   virtual void AddToRender(vtkRenderer* theRenderer){
109     theRenderer->AddActor(myLabelActor);
110     theRenderer->AddActor(myArrowActor);
111     theRenderer->AddActor(myLineActor);
112   }
113
114   virtual void SetVisibility(VTKViewer_Trihedron::TVisibility theVis);
115   virtual VTKViewer_Trihedron::TVisibility GetVisibility() { 
116     return myVisibility;
117   }
118
119   virtual void SetCamera(vtkCamera* theCamera){
120     myLabelActor->SetCamera(theCamera);
121   }
122
123   virtual void SetProperty(vtkProperty* theProperty){
124     myLabelActor->SetProperty(theProperty);
125     myArrowActor->SetProperty(theProperty);
126     myLineActor->SetProperty(theProperty);
127   }
128
129   virtual void SetSize(float theSize);
130
131   virtual VTKViewer_UnScaledActor* GetLabel(){
132     return myLabelActor;
133   }
134
135   virtual VTKViewer_UnScaledActor* GetArrow(){
136     return myArrowActor;
137   }
138
139 protected:
140   VTKViewer_Trihedron::TVisibility myVisibility;
141   float myDir[3], myRot[3];
142
143   vtkActor *myLineActor;
144   VTKViewer_UnScaledActor *myArrowActor;
145   VTKViewer_UnScaledActor *myLabelActor;
146
147   vtkPolyDataMapper *myMapper[3];
148   vtkLineSource *myLineSource;
149   vtkConeSource *myConeSource;
150   VTKViewer_VectorText* myVectorText;
151 };
152
153 VTKViewer_Axis::VTKViewer_Axis(){
154   // Initialize the Line pipe-line representation
155   myLineSource = vtkLineSource::New();
156   myLineSource->SetPoint1(0.0,0.0,0.0);
157
158   myMapper[0] = vtkPolyDataMapper::New();
159   myMapper[0]->SetInput(myLineSource->GetOutput());
160
161   myLineActor = vtkActor::New();
162   myLineActor->SetMapper(myMapper[0]);
163   myLineActor->PickableOff();
164
165   // Initialize the Arrow pipe-line representation
166   myConeSource =  vtkConeSource::New();
167   myConeSource->SetResolution(2);
168   myConeSource->SetAngle(10);
169
170   myMapper[1] = vtkPolyDataMapper::New();
171   myMapper[1]->SetInput(myConeSource->GetOutput());
172   
173   myArrowActor = VTKViewer_UnScaledActor::New();
174   myArrowActor->SetMapper(myMapper[1]);
175   static int aArrowActorSize = 24;
176   myArrowActor->SetSize(aArrowActorSize);
177   myArrowActor->PickableOff();
178
179   // Initialize the Label pipe-line representation
180   myVectorText = VTKViewer_VectorText::New();
181
182   myMapper[2] = vtkPolyDataMapper::New();
183   myMapper[2]->SetInput(myVectorText->GetOutput());
184
185   myLabelActor = VTKViewer_UnScaledActor::New();
186   myLabelActor->SetMapper(myMapper[2]);
187   static int aLabelActorSize = 12;
188   myLabelActor->SetSize(aLabelActorSize);
189   myLabelActor->PickableOff();
190
191   // Initialise visibility param.
192   myVisibility = VTKViewer_Trihedron::eOn;
193 }
194
195 VTKViewer_Axis::~VTKViewer_Axis(){
196   // Destroy of the Label pipe-line representation
197   myLabelActor->Delete();
198
199   myMapper[2]->RemoveAllInputs();
200   myMapper[2]->Delete();
201
202   myVectorText->Delete();
203
204   // Destroy of the Arrow pipe-line representation
205   myArrowActor->Delete();
206
207   myMapper[1]->RemoveAllInputs();
208   myMapper[1]->Delete();
209
210   myConeSource->Delete();
211
212   // Destroy of the Arrow pipe-line representation
213   myLineActor->Delete();
214
215   myMapper[2]->RemoveAllInputs();
216   myMapper[2]->Delete();
217
218   myLineSource->Delete();
219 }
220
221 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis){
222   switch(theVis){
223   case VTKViewer_Trihedron::eOff:
224   case VTKViewer_Trihedron::eOn:
225     myLabelActor->SetVisibility(theVis);
226     myArrowActor->SetVisibility(theVis);
227     myLineActor->SetVisibility(theVis);
228     break;
229   case VTKViewer_Trihedron::eOnlyLineOn:
230     myLabelActor->VisibilityOff();
231     myArrowActor->VisibilityOff();
232     myLineActor->VisibilityOn();
233     break;
234   default:
235     return;
236   }
237   myVisibility = theVis;
238 }
239
240 void VTKViewer_Axis::SetSize(float theSize){
241   float aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
242   myLineSource->SetPoint2(aPosition);
243
244   myArrowActor->SetPosition(0.0,0.0,0.0);
245   myArrowActor->AddPosition(aPosition);
246   myArrowActor->SetOrientation(myRot);
247
248   myLabelActor->SetPosition(0.0,0.0,0.0);
249   myLabelActor->AddPosition(aPosition);
250 }
251
252 //==============================================================================
253 class VTKViewer_XAxis : public VTKViewer_Axis{
254 protected:
255   VTKViewer_XAxis();
256   VTKViewer_XAxis(const VTKViewer_XAxis&);
257 public:
258   vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
259   static VTKViewer_XAxis *New();
260 };
261
262 vtkStandardNewMacro(VTKViewer_XAxis);
263
264 VTKViewer_XAxis::VTKViewer_XAxis(){ 
265   myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
266   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
267   myVectorText->SetText("X");
268   vtkProperty* aProperty = vtkProperty::New();
269   aProperty->SetColor(1.0,0.0,0.0);
270   SetProperty(aProperty);
271   aProperty->Delete();
272 }
273
274 //==============================================================================
275 class VTKViewer_YAxis : public VTKViewer_Axis{
276 protected:
277   VTKViewer_YAxis();
278   VTKViewer_YAxis(const VTKViewer_YAxis&);
279 public:
280   vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
281   static VTKViewer_YAxis *New();
282 };
283
284 vtkStandardNewMacro(VTKViewer_YAxis);
285
286 VTKViewer_YAxis::VTKViewer_YAxis(){ 
287   myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
288   myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
289   myVectorText->SetText("Y");
290   vtkProperty* aProperty = vtkProperty::New();
291   aProperty->SetColor(0.0,1.0,0.0);
292   SetProperty(aProperty);
293   aProperty->Delete();
294 }
295
296 //==============================================================================
297 class VTKViewer_ZAxis : public VTKViewer_Axis{
298 protected:
299   VTKViewer_ZAxis();
300   VTKViewer_ZAxis(const VTKViewer_ZAxis&);
301 public:
302   vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
303   static VTKViewer_ZAxis *New();
304 };
305
306 vtkStandardNewMacro(VTKViewer_ZAxis);
307
308 VTKViewer_ZAxis::VTKViewer_ZAxis(){ 
309   myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
310   myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
311   myVectorText->SetText("Z");
312   vtkProperty* aProperty = vtkProperty::New();
313   aProperty->SetColor(0.0,0.0,1.0);
314   SetProperty(aProperty);
315   aProperty->Delete();
316 }
317
318 //==============================================================================
319
320 vtkStandardNewMacro(VTKViewer_Trihedron);
321
322 VTKViewer_Trihedron::VTKViewer_Trihedron(){
323   myPresent = vtkActorCollection::New();
324   myAxis[0] = VTKViewer_XAxis::New();
325   myAxis[1] = VTKViewer_YAxis::New();
326   myAxis[2] = VTKViewer_ZAxis::New();
327   static float aSize = 100;
328   SetSize(aSize);
329 }
330
331 VTKViewer_Trihedron::~VTKViewer_Trihedron(){
332   myPresent->RemoveAllItems();
333   myPresent->Delete();
334   for(int i = 0; i < 3; i++)
335     myAxis[i]->Delete();
336 }
337
338 void VTKViewer_Trihedron::SetSize(float theSize){
339   mySize = theSize;
340   for(int i = 0; i < 3; i++)
341     myAxis[i]->SetSize(theSize);
342 }
343
344 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis){
345   for(int i = 0; i < 3; i++)
346     myAxis[i]->SetVisibility(theVis);
347 }
348
349 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility(){
350   return myAxis[0]->GetVisibility();
351 }
352
353 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer){
354   vtkCamera* aCamera = theRenderer->GetActiveCamera();
355   for(int i = 0; i < 3; i++){
356     myAxis[i]->AddToRender(theRenderer);
357     myAxis[i]->SetCamera(aCamera);
358   }
359 }
360
361 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer){
362   myPresent->InitTraversal();
363   while(vtkActor* anActor = myPresent->GetNextActor())
364     theRenderer->RemoveActor(anActor);
365 }
366
367 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer){
368   //TVisibility aVis = GetVisibility();
369   //SetVisibility(eOff);
370   vtkActorCollection* aCollection = theRenderer->GetActors();
371   aCollection->InitTraversal();
372   int aCount = 0;
373   while(vtkActor* prop = aCollection->GetNextActor())
374     if( prop->GetVisibility())
375       if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(prop))
376         if(!anActor->IsInfinitive()) 
377           aCount++;
378   //int aCount = theRenderer->VisibleActorCount();
379   //SetVisibility(aVis);
380   return aCount;
381 }
382
383 void VTKViewer_Trihedron::Render(vtkRenderer* theRenderer){
384   for(int i = 0; i < 3; i++)
385     {
386       myAxis[i]->GetLabel()->Render(theRenderer);
387       myAxis[i]->GetArrow()->Render(theRenderer);
388     }
389 }
390