Salome HOME
Update Help for VISU module.
[modules/visu.git] / src / OBJECT / VISU_MeshAct.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
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   : VISU_MeshAct.cxx
25 //  Author : Laurent CORNABE with the help of Nicolas REJNERI
26 //  Module : VISU
27 //  $Header$
28
29 #include "VISU_MeshAct.h"
30 #include "VTKViewer_PassThroughFilter.h"
31
32 #include <vtkObjectFactory.h>
33 #include <vtkRenderer.h>
34 #include <vtkTexture.h>
35
36 #include <vtkFeatureEdges.h>
37 #include <vtkDataSetMapper.h>
38 #include <vtkDataSet.h>
39 #include <vtkMatrix4x4.h>
40 #include <vtkMapperCollection.h>
41
42 vtkStandardNewMacro(VISU_MeshAct);
43
44 VISU_MeshAct::VISU_MeshAct(){
45
46   vtkMatrix4x4 *m;
47   m = vtkMatrix4x4::New();
48
49   mySurfaceActor = VISU_Actor::New();
50   mySurfaceActor->SetParent(this);
51   mySurfaceActor->GetProperty()->FrontfaceCullingOff();
52   mySurfaceActor->GetProperty()->BackfaceCullingOff();
53   mySurfaceActor->SetUserMatrix(m);
54   mySurfaceActor->SetRepresentation(2); //SURFACE
55
56   myEdgeActor = VISU_Actor::New();
57   myEdgeActor->SetParent(this);
58   myEdgeActor->PickableOff();
59   myEdgeActor->GetProperty()->FrontfaceCullingOff();
60   myEdgeActor->GetProperty()->BackfaceCullingOff();
61   myEdgeActor->SetUserMatrix(m);
62   myEdgeActor->SetRepresentation(1); //WIREFRAME
63
64   myNodeActor = VISU_Actor::New();
65   myNodeActor->SetParent(this);
66   myNodeActor->PickableOff();
67   myNodeActor->GetProperty()->SetPointSize(SALOME_POINT_SIZE);
68   myNodeActor->GetProperty()->FrontfaceCullingOff();
69   myNodeActor->GetProperty()->BackfaceCullingOff();
70   myNodeActor->SetUserMatrix(m);
71   myNodeActor->SetRepresentation(0); //POINT
72
73   m->Delete();
74   SetRepresentation(2); //SURFACE
75 }
76
77 VISU_MeshAct::~VISU_MeshAct(){
78   mySurfaceActor->Delete();
79   myEdgeActor->Delete();
80   myNodeActor->Delete();
81 }
82
83 void VISU_MeshAct::setIO(const Handle(SALOME_InteractiveObject)& theIO){
84   VISU_Actor::setIO(theIO);
85   mySurfaceActor->setIO(theIO);
86   myEdgeActor->setIO(theIO);
87   myNodeActor->setIO(theIO);
88 }
89
90 void VISU_MeshAct::SetPrs3d(VISU::Prs3d_i* thePrs3d){
91   if(thePrs3d){
92     VISU_Actor::SetPrs3d(thePrs3d);
93     mySurfaceActor->SetPrs3d(thePrs3d);
94     myEdgeActor->SetPrs3d(thePrs3d);
95     myNodeActor->SetPrs3d(thePrs3d);
96   }
97 }
98
99 void VISU_MeshAct::AddToRender(vtkRenderer* theRenderer){
100   VISU_Actor::AddToRender(theRenderer);
101   theRenderer->AddActor(myEdgeActor);
102 }
103
104 void VISU_MeshAct::RemoveFromRender(vtkRenderer* theRenderer){
105   VISU_Actor::RemoveFromRender(theRenderer);
106   theRenderer->RemoveActor(myEdgeActor);
107 }
108
109 void VISU_MeshAct::SetPipeLine(VISU_PipeLine* thePipeLine) {
110   VISU_Actor::SetPipeLine(thePipeLine);
111   mySurfaceActor->SetPipeLine(thePipeLine);
112   myEdgeActor->SetPipeLine(thePipeLine);
113   myNodeActor->SetPipeLine(thePipeLine);
114 }
115
116 void VISU_MeshAct::SetTransform(VTKViewer_Transform* theTransform){
117   VISU_Actor::SetTransform(theTransform);
118   mySurfaceActor->SetTransform(theTransform);
119   myEdgeActor->SetTransform(theTransform);
120   myNodeActor->SetTransform(theTransform);
121 }
122
123 void VISU_MeshAct::SetShrinkable(bool theIsShrinkable){
124   VISU_Actor::SetShrinkable(theIsShrinkable);
125   mySurfaceActor->SetShrinkable(theIsShrinkable);
126   myEdgeActor->SetShrinkable(theIsShrinkable);
127   myNodeActor->SetShrinkable(theIsShrinkable);
128 }
129
130 void VISU_MeshAct::SetShrinkFactor(float theValue){
131   VISU_Actor::SetShrinkFactor(theValue);
132   mySurfaceActor->SetShrinkFactor(theValue);
133   myEdgeActor->SetShrinkFactor(theValue);
134 }
135
136 vtkProperty* VISU_MeshAct::GetSurfaceProperty(){
137   return mySurfaceActor->GetProperty();
138 }
139
140 void VISU_MeshAct::SetSurfaceProperty(vtkProperty* theProperty){
141   mySurfaceActor->SetProperty(theProperty);
142 }
143
144 vtkProperty* VISU_MeshAct::GetEdgeProperty(){
145   return myEdgeActor->GetProperty();
146 }
147
148 void VISU_MeshAct::SetEdgeProperty(vtkProperty* theProperty){
149   myEdgeActor->SetProperty(theProperty);
150 }
151
152 vtkProperty* VISU_MeshAct::GetNodeProperty(){
153   return myNodeActor->GetProperty();
154 }
155
156 void VISU_MeshAct::SetNodeProperty(vtkProperty* theProperty){
157   myNodeActor->SetProperty(theProperty);
158 }
159
160 void VISU_MeshAct::SetOpacity(float theValue){
161   GetSurfaceProperty()->SetOpacity(theValue);
162 }
163
164 float VISU_MeshAct::GetOpacity(){
165   return GetSurfaceProperty()->GetOpacity();
166 }
167
168 void VISU_MeshAct::SetLineWidth(float theLineWidth){
169   GetEdgeProperty()->SetLineWidth(theLineWidth);
170 }
171
172 float VISU_MeshAct::GetLineWidth(){
173   return GetEdgeProperty()->GetLineWidth();
174 }
175
176 void VISU_MeshAct::SetShrink(){
177   if(myRepresentation == VTK_POINTS)
178     return;
179   VISU_Actor::SetShrink();
180   mySurfaceActor->SetShrink();
181   myEdgeActor->SetShrink();
182   //SetRepresentation(GetRepresentation());
183 }
184
185 void VISU_MeshAct::UnShrink(){
186   VISU_Actor::UnShrink();
187   mySurfaceActor->UnShrink();
188   myEdgeActor->UnShrink();
189   //SetRepresentation(GetRepresentation());
190 }
191
192 void VISU_MeshAct::SetRepresentation(int theMode)
193 {
194   SALOME_Actor::SetRepresentation(theMode);
195
196   if (!GetVisibility()) return; // Fix IPAL9555
197
198   mySurfaceActor->VisibilityOff();
199   myEdgeActor->VisibilityOff();
200   myNodeActor->VisibilityOff();
201   myEdgeActor->PickableOff();
202   myNodeActor->PickableOff();
203   switch (theMode) {
204   case VTK_POINTS:
205     myNodeActor->VisibilityOn();
206     myNodeActor->PickableOn();
207     break;
208   case VTK_WIREFRAME:
209     myEdgeActor->VisibilityOn();
210     myEdgeActor->SetRepresentation(1);
211     myEdgeActor->PickableOn();
212     break;
213   case VTK_SURFACE:
214     mySurfaceActor->VisibilityOn();
215     break;
216   case 3: //INSIDEFRAME
217     myEdgeActor->VisibilityOn();
218     myEdgeActor->SetRepresentation(3);
219     myEdgeActor->PickableOn();
220     break;
221   case 4: //SURFACEFRAME
222     myEdgeActor->VisibilityOn();
223     myEdgeActor->SetRepresentation(1);
224     mySurfaceActor->VisibilityOn();
225     break;
226   }
227   if (myRepresentation == VTK_POINTS)
228     UnShrink();
229 }
230
231 void VISU_MeshAct::SetVisibility(int theMode){
232   VISU_Actor::SetVisibility(theMode);
233   if(GetVisibility())
234     SetRepresentation(GetRepresentation());
235   else{
236     myNodeActor->VisibilityOff();
237     myEdgeActor->VisibilityOff();
238     mySurfaceActor->VisibilityOff();
239   }
240 }
241
242 // From vtkLODActor
243 void VISU_MeshAct::Render(vtkRenderer *ren, vtkMapper *vtkNotUsed(m))
244 {
245   float myTime, bestTime, tempTime;
246   vtkMatrix4x4 *matrix;
247   vtkMapper *mapper, *bestMapper;
248
249   if (this->Mapper == NULL)
250     {
251     vtkErrorMacro("No mapper for actor.");
252     return;
253     }
254
255   // first time through create lods if non have been added
256   if (this->LODMappers->GetNumberOfItems() == 0)
257     {
258     this->CreateOwnLODs();
259     }
260
261   // If the actor has changed or the primary mapper has changed ...
262   // Is this the correct test?
263   if (this->MediumMapper)
264     {
265     if (this->GetMTime() > this->BuildTime ||
266         this->Mapper->GetMTime() > this->BuildTime)
267       {
268       this->UpdateOwnLODs();
269       }
270     }
271
272   // figure out how much time we have to render
273   myTime = this->AllocatedRenderTime;
274
275   // Figure out which resolution to use
276   // none is a valid resolution. Do we want to have a lowest:
277   // bbox, single point, ...
278   // There is no order to the list, so it is assumed that mappers that take
279   // longer to render are better quality.
280   // Timings might become out of date, but we rely on
281
282   bestMapper = this->Mapper;
283   bestTime = bestMapper->GetTimeToDraw();
284   if (bestTime > myTime)
285     {
286     this->LODMappers->InitTraversal();
287     while ((mapper = this->LODMappers->GetNextItem()) != NULL &&
288            bestTime != 0.0)
289       {
290       tempTime = mapper->GetTimeToDraw();
291
292       // If the LOD has never been rendered, select it!
293       if (tempTime == 0.0)
294         {
295         bestMapper = mapper;
296         bestTime = 0.0;
297         }
298       else
299         {
300         if (bestTime > myTime && tempTime < bestTime)
301           {
302           bestMapper = mapper;
303           bestTime = tempTime;
304           }
305         if (tempTime > bestTime && tempTime < myTime)
306           {
307           bestMapper = mapper;
308           bestTime = tempTime;
309           }
310         }
311       }
312     }
313
314   switch(GetRepresentation()){
315   case 0: //POINTS
316     myNodeActor->GetProperty()->Render(this, ren);
317     break;
318   case 1: //WIREFRAME
319     //PAL5268: myEdgeActor->GetProperty()->Render(this, ren);
320     break;
321   case 2: //SURFACE
322     mySurfaceActor->GetProperty()->Render(this, ren);
323     break;
324   case 3: //INSIDEFRAME
325     //PAL5268: myEdgeActor->GetProperty()->Render(this, ren);
326     break;
327   case 4: //SURFACEFRAME
328     //PAL5268: myEdgeActor->GetProperty()->Render(this, ren);
329     mySurfaceActor->GetProperty()->Render(this, ren);
330     break;
331   }
332
333   // render the texture
334   if (this->Texture)
335     {
336     this->Texture->Render(ren);
337     }
338
339   switch(GetRepresentation()){
340   case 0: //POINTS
341     matrix = myNodeActor->GetUserMatrix();
342     break;
343   case 1: //WIREFRAME
344     matrix = myEdgeActor->GetUserMatrix();
345     break;
346   case 2: //SURFACE
347     matrix = mySurfaceActor->GetUserMatrix();
348     break;
349   case 3: //INSIDEFRAME
350     matrix = myEdgeActor->GetUserMatrix();
351     break;
352   case 4: //SURFACEFRAME
353     matrix = myEdgeActor->GetUserMatrix();
354     this->GetMatrix(matrix);
355     matrix = mySurfaceActor->GetUserMatrix();
356     break;
357   }
358   this->GetMatrix(matrix);
359
360   switch(GetRepresentation()){
361   case 0: //POINTS
362     myNodeActor->Render(ren,bestMapper);
363     break;
364   case 1: //WIREFRAME
365     //PAL5268: myEdgeActor->Render(ren,bestMapper);
366     break;
367   case 2: //SURFACE
368     mySurfaceActor->Render(ren,bestMapper);
369     break;
370   case 3: //INSIDEFRAME
371     //PAL5268: myEdgeActor->Render(ren,bestMapper);
372     break;
373   case 4: //SURFACEFRAME
374     //PAL5268: myEdgeActor->Render(ren,bestMapper);
375     mySurfaceActor->Render(ren,bestMapper);
376     break;
377   }
378
379   this->EstimatedRenderTime = bestMapper->GetTimeToDraw();
380 }
381
382 // From vtkLODActor
383 void VISU_MeshAct::Modified()
384 {
385   myNodeActor->Modified();
386   //PAL5268: myEdgeActor->Modified();
387   mySurfaceActor->Modified();
388   this->vtkActor::Modified();
389 }