Salome HOME
This commit was generated by cvs2git to create branch 'IMPORT'.
[modules/smesh.git] / src / OBJECT / SMESH_Actor.cxx
1 using namespace std;
2 //  File      : SMESH_Actor.cxx
3 //  Created   : Mon May 13 22:31:18 2002
4 //  Author    : Nicolas REJNERI
5
6 //  Project   : SALOME
7 //  Module    : SMESH
8 //  Copyright : Open CASCADE 2002
9 //  $Header$
10
11 /*!
12   \class SMESH_Actor SMESH_Actor.h
13   \brief ...
14 */
15
16 #include "SMESH_Actor.h"
17 #include "SMESH_Grid.h"
18 #include "utilities.h"
19
20 // VTK Includes
21 #include <vtkObjectFactory.h>
22 #include <vtkMergePoints.h>
23 #include <vtkDataSetMapper.h>
24 #include <vtkFeatureEdges.h>
25 #include <vtkGeometryFilter.h>
26
27 //-------------------------------------------------------------
28 // Main methods
29 //-------------------------------------------------------------
30
31 SMESH_Actor* SMESH_Actor::New()
32 {
33   // First try to create the object from the vtkObjectFactory
34   vtkObject* ret = vtkObjectFactory::CreateInstance("SMESH_Actor");
35   if(ret)
36     {
37       return (SMESH_Actor*)ret;
38     }
39   // If the factory was unable to create the object, then create it here.
40   return new SMESH_Actor;
41 }
42
43
44 SMESH_Actor::SMESH_Actor()
45 {
46   this->Device = vtkActor::New();
47
48   this->EdgeDevice = vtkActor::New();
49   EdgeDevice->VisibilityOff();
50   EdgeDevice->PickableOff();
51
52   this->EdgeShrinkDevice = vtkActor::New();
53   EdgeShrinkDevice->VisibilityOff();
54   EdgeShrinkDevice->PickableOff();
55
56   myIO = NULL;
57   myName = "";
58   myDisplayMode = 0;
59
60   ishighlighted = false;
61   ispreselected = false;
62
63   edgeColor.r = 0.;
64   edgeColor.g = 0.;
65   edgeColor.b = 0.;
66   
67   edgeHighlightColor.r = 1.;
68   edgeHighlightColor.g = 1.;
69   edgeHighlightColor.b = 1.;
70
71   edgePreselectedColor.r = 0.;
72   edgePreselectedColor.g = 1.;
73   edgePreselectedColor.b = 1.;
74
75   actorColor.r = 1.;
76   actorColor.g = 1.;
77   actorColor.b = 0.;
78
79   actorHighlightColor.r = 1.;
80   actorHighlightColor.g = 1.;
81   actorHighlightColor.b = 1.;
82
83   actorPreselectedColor.r = 0.;
84   actorPreselectedColor.g = 1.;
85   actorPreselectedColor.b = 1.;
86
87   actorNodeColor.r = 1.;
88   actorNodeColor.g = 1.;
89   actorNodeColor.b = 0.;
90
91   actorNodeSize = 2 ;
92   
93 }
94
95 SMESH_Actor::~SMESH_Actor()
96 {
97   this->EdgeDevice->Delete();
98   this->EdgeShrinkDevice->Delete();
99 }
100
101 void SMESH_Actor::setReader(vtkUnstructuredGridReader* r) {
102   myReader=r;
103 }
104
105 vtkUnstructuredGridReader* SMESH_Actor::getReader() {
106   return (myReader);
107 }
108
109 vtkMapper* SMESH_Actor::getMapper() {
110   return (this->Mapper);
111 }
112
113 void SMESH_Actor::ShallowCopy(vtkProp *prop)
114 {
115   SMESH_Actor *f = SMESH_Actor::SafeDownCast(prop);
116   if ( f != NULL )
117     {
118       this->setName( f->getName() );
119       if ( f->hasIO() )
120         this->setIO( f->getIO() );
121       this->setDisplayMode( f->getDisplayMode() );
122
123       // Copy devices
124       vtkActor* tempDev = vtkActor::New();
125       tempDev->ShallowCopy(f->Device);
126       vtkProperty* prp = vtkProperty::New();
127       prp->DeepCopy(f->Device->GetProperty());
128       tempDev->SetProperty(prp);
129       prp = vtkProperty::New();
130       prp->DeepCopy(f->Device->GetBackfaceProperty());
131       tempDev->SetBackfaceProperty(prp);
132       this->Device = tempDev;
133       
134       tempDev = vtkActor::New();
135       tempDev->ShallowCopy(f->EdgeDevice);
136       prp = vtkProperty::New();
137       prp->DeepCopy(f->EdgeDevice->GetProperty());
138       tempDev->SetProperty(prp);
139       prp = vtkProperty::New();
140       prp->DeepCopy(f->EdgeDevice->GetBackfaceProperty());
141       tempDev->SetBackfaceProperty(prp);
142       this->EdgeDevice = tempDev;
143
144       tempDev = vtkActor::New();
145       tempDev->ShallowCopy(f->EdgeShrinkDevice);
146       prp = vtkProperty::New();
147       prp->DeepCopy(f->EdgeShrinkDevice->GetProperty());
148       tempDev->SetProperty(prp);
149       prp = vtkProperty::New();
150       prp->DeepCopy(f->EdgeShrinkDevice->GetBackfaceProperty());
151       tempDev->SetBackfaceProperty(prp);
152       this->EdgeShrinkDevice = tempDev;
153
154       // Copy data source
155       this->DataSource = f->DataSource;
156
157       this->myReader   = f->myReader;
158     }
159
160   // Now do superclass
161   this->SALOME_Actor::ShallowCopy(prop);
162
163   // Here we need to modify default ShallowCopy() results
164   // Create copies of properties
165   if ( f != NULL ) {
166     vtkProperty* prp = vtkProperty::New();
167     prp->DeepCopy(f->GetProperty());
168     this->SetProperty(prp);
169
170     prp = vtkProperty::New();
171     prp->DeepCopy(f->GetBackfaceProperty());
172     this->SetBackfaceProperty(prp);
173
174     // Copy the mapper
175     vtkDataSetMapper* mpr = vtkDataSetMapper::New();
176     mpr->ShallowCopy(f->GetMapper());
177     mpr->SetInput(f->DataSource);
178     this->SetMapper(mpr);
179   }
180 }
181
182 void SMESH_Actor::Render(vtkRenderer *ren, vtkMapper *Mapper )
183 {
184    if (this->Mapper == NULL) {
185     MESSAGE ("No mapper for actor.")
186     return;
187   }
188
189    if ( myDisplayMode == 1 ) {
190      EdgeDevice->VisibilityOn();
191      EdgeShrinkDevice->VisibilityOff();
192    } else if ( myDisplayMode == 2 ) {
193      EdgeShrinkDevice->VisibilityOn();
194      EdgeDevice->VisibilityOff();
195    } else {
196      EdgeShrinkDevice->VisibilityOff();
197      EdgeDevice->VisibilityOff();
198    }
199      
200
201   vtkMapper *bestMapper;
202   bestMapper = this->Mapper;
203
204   /* render the property */
205   if (!this->Property) {
206     // force creation of a property
207     this->GetProperty();
208   }
209
210   if ( ishighlighted ) {
211    if ( myDisplayMode == 1 ) {
212      EdgeDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
213      this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
214    } else if ( myDisplayMode == 2 ) {
215      EdgeShrinkDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
216    } else {
217      this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
218    }
219   } else if (! ispreselected ) {
220     if ( myDisplayMode == 1 ) {
221       EdgeDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
222       this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
223     }
224     else if ( myDisplayMode == 2 ) 
225       EdgeShrinkDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
226     else
227       this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
228   }
229   else {
230     if ( myDisplayMode == 1 )
231       EdgeDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
232     else if ( myDisplayMode == 2 ) 
233       EdgeShrinkDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
234     else
235       this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);
236   }
237
238   this->Property->Render(this, ren);
239   if (this->BackfaceProperty) {
240     this->BackfaceProperty->BackfaceRender(this, ren);
241     this->Device->SetBackfaceProperty(this->BackfaceProperty);
242   }
243   this->Device->SetProperty(this->Property);
244   
245   /* render the texture */
246   if (this->Texture) {
247     this->Texture->Render(ren);
248   }
249   
250   
251   // Store information on time it takes to render.
252   // We might want to estimate time from the number of polygons in mapper.
253   this->Device->Render(ren,bestMapper);
254   this->EstimatedRenderTime = bestMapper->GetTimeToDraw();
255 }
256
257 int SMESH_Actor::RenderOpaqueGeometry(vtkViewport *vp)
258 {
259   int          renderedSomething = 0; 
260   vtkRenderer  *ren = (vtkRenderer *)vp;
261   
262   if ( ! this->Mapper ) {
263     return 0;
264   }
265   
266   // make sure we have a property
267   if (!this->Property) {
268     // force creation of a property
269     this->GetProperty();
270   }
271   
272   if ( ishighlighted ) {
273    if ( myDisplayMode == 1 ) {
274      EdgeDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
275    } else if ( myDisplayMode == 2 ) {
276      EdgeShrinkDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
277    } else {
278      this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
279    }
280   } else if (! ispreselected ) {
281     if ( myDisplayMode == 1 )
282       EdgeDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
283     else if ( myDisplayMode == 2 ) 
284       EdgeShrinkDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
285     else
286       this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
287   }
288   else {
289     if ( myDisplayMode == 1 )
290       EdgeDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
291     else if ( myDisplayMode == 2 ) 
292       EdgeShrinkDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
293     else
294       this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);    
295   }
296
297   // is this actor opaque ?
298   if (this->GetIsOpaque()) {
299     this->Property->Render(this, ren);
300     
301     // render the backface property
302     if (this->BackfaceProperty) {
303       this->BackfaceProperty->BackfaceRender(this, ren);
304     }
305     
306     // render the texture 
307     if (this->Texture) {
308       this->Texture->Render(ren);
309     }
310     this->Render(ren,this->Mapper);
311     
312     renderedSomething = 1;
313   }
314   
315   return renderedSomething; 
316 }
317
318
319 void SMESH_Actor::SetColor(float r,float g,float b)
320 {
321   actorColor.r = r;
322   actorColor.g = g;
323   actorColor.b = b;
324 }
325
326 void SMESH_Actor::GetColor(float& r,float& g,float& b)
327 {
328   r = actorColor.r;
329   g = actorColor.g;
330   b = actorColor.b;
331 }
332
333 void SMESH_Actor::SetPreselectedColor(float r,float g,float b)
334 {
335   actorPreselectedColor.r = r;
336   actorPreselectedColor.g = g;
337   actorPreselectedColor.b = b;
338 }
339
340 void SMESH_Actor::GetPreselectedColor(float& r,float& g,float& b)
341 {
342   r = actorPreselectedColor.r;
343   g = actorPreselectedColor.g;
344   b = actorPreselectedColor.b;
345 }
346
347 void SMESH_Actor::SetHighlightColor(float r,float g,float b)
348 {
349   actorHighlightColor.r = r;
350   actorHighlightColor.g = g;
351   actorHighlightColor.b = b;
352 }
353
354 void SMESH_Actor::GetHighlightColor(float& r,float& g,float& b)
355 {
356   r = actorHighlightColor.r;
357   g = actorHighlightColor.g;
358   b = actorHighlightColor.b;
359 }
360
361 void SMESH_Actor::SetEdgeColor(float r,float g,float b)
362 {
363   edgeColor.r = r;
364   edgeColor.g = g;
365   edgeColor.b = b;
366 }
367
368 void SMESH_Actor::GetEdgeColor(float& r,float& g,float& b)
369 {
370   r = edgeColor.r;
371   g = edgeColor.g;
372   b = edgeColor.b;
373 }
374
375 void SMESH_Actor::SetEdgeHighlightColor(float r,float g,float b)
376 {
377   edgeHighlightColor.r = r;
378   edgeHighlightColor.g = g;
379   edgeHighlightColor.b = b;
380 }
381
382 void SMESH_Actor::GetEdgeHighlightColor(float& r,float& g,float& b)
383 {
384   r = edgeHighlightColor.r;
385   g = edgeHighlightColor.g;
386   b = edgeHighlightColor.b;
387 }
388
389 void SMESH_Actor::SetEdgePreselectedColor(float r,float g,float b)
390 {
391   edgePreselectedColor.r = r;
392   edgePreselectedColor.g = g;
393   edgePreselectedColor.b = b;
394 }
395
396 void SMESH_Actor::GetEdgePreselectedColor(float& r,float& g,float& b)
397 {
398   r = edgePreselectedColor.r;
399   g = edgePreselectedColor.g;
400   b = edgePreselectedColor.b;
401 }
402
403
404 void SMESH_Actor::SetNodeColor(float r,float g,float b)
405
406   actorNodeColor.r = r ;
407   actorNodeColor.g = g ;
408   actorNodeColor.b = b ;
409 }
410
411 void SMESH_Actor::GetNodeColor(float& r,float& g,float& b)
412
413   r = actorNodeColor.r ;
414   g = actorNodeColor.g ;
415   b = actorNodeColor.b ;
416 }
417
418 void SMESH_Actor::SetNodeSize(int size)
419 {
420   actorNodeSize = size ;
421 }
422
423 int SMESH_Actor::GetNodeSize()
424 {
425   return actorNodeSize ;
426 }
427
428
429 void SMESH_Actor::AddNode(int idSMESHDSnode,int idVTKnode)
430 {
431   if (DataSource->IsA("SMESH_Grid")) {
432     ((SMESH_Grid*)DataSource)->AddNode(idSMESHDSnode, idVTKnode);
433   } else 
434     MESSAGE("AddNode() method has been moved to SMESH_Grid class");
435 }
436 void SMESH_Actor::AddElement(int idSMESHDSelement, int idVTKelement)
437 {
438   if (DataSource->IsA("SMESH_Grid")) {
439     ((SMESH_Grid*)DataSource)->AddElement(idSMESHDSelement, idVTKelement);
440   } else 
441     MESSAGE("AddElement() method has been moved to SMESH_Grid class");
442 }
443
444 void SMESH_Actor::SetIdsVTKNode(const TColStd_DataMapOfIntegerInteger& mapVTK)
445 {
446   if (DataSource->IsA("SMESH_Grid")) {
447     ((SMESH_Grid*)DataSource)->SetIdsVTKNode(mapVTK);
448   } else 
449     MESSAGE("SetIdsVTKNode() method has been moved to SMESH_Grid class");
450 }
451 void SMESH_Actor::SetIdsSMESHDSNode(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
452 {
453   if (DataSource->IsA("SMESH_Grid")) {
454     ((SMESH_Grid*)DataSource)->SetIdsSMESHDSNode(mapSMESHDS);
455   } else 
456     MESSAGE("SetIdsSMESHDSNode() method has been moved to SMESH_Grid class");
457 }
458
459 void SMESH_Actor::SetIdsVTKElement(const TColStd_DataMapOfIntegerInteger& mapVTK)
460 {
461   if (DataSource->IsA("SMESH_Grid")) {
462     ((SMESH_Grid*)DataSource)->SetIdsVTKElement(mapVTK);
463   } else 
464     MESSAGE("SetIdsVTKElement() method has been moved to SMESH_Grid class");
465 }
466 void SMESH_Actor::SetIdsSMESHDSElement(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
467 {
468   if (DataSource->IsA("SMESH_Grid")) {
469     ((SMESH_Grid*)DataSource)->SetIdsSMESHDSElement(mapSMESHDS);
470   } else 
471     MESSAGE("SetIdsSMESHDSElement() method has been moved to SMESH_Grid class");
472 }
473
474 int SMESH_Actor::GetIdVTKNode(int idSMESHDSnode)
475 {
476   if (DataSource->IsA("SMESH_Grid")) {
477     return ((SMESH_Grid*)DataSource)->GetIdVTKNode(idSMESHDSnode);
478   } else {
479     MESSAGE("GetIdVTKNode() method has been moved to SMESH_Grid class");
480     return -1;
481   }
482 }
483 int SMESH_Actor::GetIdVTKElement(int idSMESHDSelement)
484 {
485   if (DataSource->IsA("SMESH_Grid")) {
486     return ((SMESH_Grid*)DataSource)->GetIdVTKElement(idSMESHDSelement);
487   } else {
488     MESSAGE("GetIdVTKElement() method has been moved to SMESH_Grid class");
489     return -1;
490   }
491
492 }
493
494 int SMESH_Actor::GetIdSMESHDSNode(int idVTKnode)
495 {
496   if (DataSource->IsA("SMESH_Grid")) {
497     return ((SMESH_Grid*)DataSource)->GetIdSMESHDSNode(idVTKnode);
498   } else {
499     MESSAGE("GetIdSMESHDSNode() method has been moved to SMESH_Grid class");
500     return -1;
501   }
502 }
503
504 int SMESH_Actor::GetIdSMESHDSElement(int idVTKelement)
505 {
506   if (DataSource->IsA("SMESH_Grid")) {
507     return ((SMESH_Grid*)DataSource)->GetIdSMESHDSElement(idVTKelement);
508   } else {
509     MESSAGE("AddNode() method has been moved to SMESH_Grid class");
510     return -1;
511   }
512 }
513
514 void SMESH_Actor::ClearNode()
515 {
516   if (DataSource->IsA("SMESH_Grid")) {
517     ((SMESH_Grid*)DataSource)->ClearNode();
518   } else 
519     MESSAGE("ClearNode() method has been moved to SMESH_Grid class");
520 }
521
522 void SMESH_Actor::ClearElement()
523 {
524   if (DataSource->IsA("SMESH_Grid")) {
525     ((SMESH_Grid*)DataSource)->ClearElement();
526   } else 
527     MESSAGE("ClearElement() method has been moved to SMESH_Grid class");
528 }
529
530 void SMESH_Actor::RemoveNode(int id)
531 {
532   if (DataSource->IsA("SMESH_Grid")) {
533     ((SMESH_Grid*)DataSource)->RemoveNode(id);
534   } else 
535     MESSAGE("RemoveNode() method has been moved to SMESH_Grid class");
536 }
537 void SMESH_Actor::RemoveElement(int id)
538 {
539   if (DataSource->IsA("SMESH_Grid")) {
540     ((SMESH_Grid*)DataSource)->RemoveElement(id);
541   } else 
542     MESSAGE("RemoveElement() method has been moved to SMESH_Grid class");
543 }
544
545 void SMESH_Actor::setDisplayMode(int thenewmode) {
546   myDisplayMode = thenewmode;
547   if ( myDisplayMode == 1 ) {
548     EdgeDevice->VisibilityOn();
549     EdgeShrinkDevice->VisibilityOff();
550   } else if ( myDisplayMode == 2 ) {
551     EdgeDevice->VisibilityOff();
552     EdgeShrinkDevice->VisibilityOn();
553   } else {
554     EdgeDevice->VisibilityOff();
555     EdgeShrinkDevice->VisibilityOff();
556   }
557 }
558
559 float SMESH_Actor::GetShrinkFactor()
560 {
561   return myShrinkFactor;
562 }
563
564 void SMESH_Actor::SetShrinkFactor(float value )
565 {
566   if ( value <= 0.1 ) 
567     value = 0.8;
568
569   myShrinkFactor = value;
570 }
571
572 void SMESH_Actor::GetChildActors(vtkActorCollection* actors)
573 {
574   actors->AddItem(EdgeDevice);
575   actors->AddItem(EdgeShrinkDevice);
576 }
577
578 void SMESH_Actor::SetVisibility(bool visibility) 
579 {
580   if ( visibility ) {
581     this->VisibilityOn();
582     if ( myDisplayMode == 1 ) {
583       EdgeDevice->VisibilityOn();
584       EdgeShrinkDevice->VisibilityOff();
585     } else if ( myDisplayMode == 2 ) {
586       EdgeDevice->VisibilityOff();
587       EdgeShrinkDevice->VisibilityOn();
588     } else {
589       EdgeDevice->VisibilityOff();
590       EdgeShrinkDevice->VisibilityOff();
591     }
592   } else {
593     this->VisibilityOff();
594     EdgeDevice->VisibilityOff();
595     EdgeShrinkDevice->VisibilityOff();    
596   }
597 }
598