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