1 // SMESH OBJECT : interactive object for SMESH visualization
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_Actor.cxx
25 // Author : Nicolas REJNERI
31 \class SMESH_Actor SMESH_Actor.h
35 #include "SMESH_Actor.h"
36 #include "SMESH_Grid.h"
37 #include "utilities.h"
40 #include <vtkObjectFactory.h>
41 #include <vtkMergePoints.h>
42 #include <vtkDataSetMapper.h>
43 #include <vtkFeatureEdges.h>
44 #include <vtkGeometryFilter.h>
46 //-------------------------------------------------------------
48 //-------------------------------------------------------------
50 SMESH_Actor* SMESH_Actor::New()
52 // First try to create the object from the vtkObjectFactory
53 vtkObject* ret = vtkObjectFactory::CreateInstance("SMESH_Actor");
56 return (SMESH_Actor*)ret;
58 // If the factory was unable to create the object, then create it here.
59 return new SMESH_Actor;
63 SMESH_Actor::SMESH_Actor()
65 this->Device = vtkActor::New();
67 this->EdgeDevice = vtkActor::New();
68 EdgeDevice->VisibilityOff();
69 EdgeDevice->PickableOff();
71 this->EdgeShrinkDevice = vtkActor::New();
72 EdgeShrinkDevice->VisibilityOff();
73 EdgeShrinkDevice->PickableOff();
79 ishighlighted = false;
80 ispreselected = false;
86 edgeHighlightColor.r = 1.;
87 edgeHighlightColor.g = 1.;
88 edgeHighlightColor.b = 1.;
90 edgePreselectedColor.r = 0.;
91 edgePreselectedColor.g = 1.;
92 edgePreselectedColor.b = 1.;
98 actorHighlightColor.r = 1.;
99 actorHighlightColor.g = 1.;
100 actorHighlightColor.b = 1.;
102 actorPreselectedColor.r = 0.;
103 actorPreselectedColor.g = 1.;
104 actorPreselectedColor.b = 1.;
106 actorNodeColor.r = 1.;
107 actorNodeColor.g = 1.;
108 actorNodeColor.b = 0.;
114 SMESH_Actor::~SMESH_Actor()
116 this->EdgeDevice->Delete();
117 this->EdgeShrinkDevice->Delete();
120 void SMESH_Actor::setReader(vtkUnstructuredGridReader* r) {
124 vtkUnstructuredGridReader* SMESH_Actor::getReader() {
128 vtkMapper* SMESH_Actor::getMapper() {
129 return (this->Mapper);
132 void SMESH_Actor::ShallowCopy(vtkProp *prop)
134 SMESH_Actor *f = SMESH_Actor::SafeDownCast(prop);
137 this->setName( f->getName() );
139 this->setIO( f->getIO() );
140 this->setDisplayMode( f->getDisplayMode() );
143 vtkActor* tempDev = vtkActor::New();
144 tempDev->ShallowCopy(f->Device);
145 vtkProperty* prp = vtkProperty::New();
146 prp->DeepCopy(f->Device->GetProperty());
147 tempDev->SetProperty(prp);
148 prp = vtkProperty::New();
149 prp->DeepCopy(f->Device->GetBackfaceProperty());
150 tempDev->SetBackfaceProperty(prp);
151 this->Device = tempDev;
153 tempDev = vtkActor::New();
154 tempDev->ShallowCopy(f->EdgeDevice);
155 prp = vtkProperty::New();
156 prp->DeepCopy(f->EdgeDevice->GetProperty());
157 tempDev->SetProperty(prp);
158 prp = vtkProperty::New();
159 prp->DeepCopy(f->EdgeDevice->GetBackfaceProperty());
160 tempDev->SetBackfaceProperty(prp);
161 this->EdgeDevice = tempDev;
163 tempDev = vtkActor::New();
164 tempDev->ShallowCopy(f->EdgeShrinkDevice);
165 prp = vtkProperty::New();
166 prp->DeepCopy(f->EdgeShrinkDevice->GetProperty());
167 tempDev->SetProperty(prp);
168 prp = vtkProperty::New();
169 prp->DeepCopy(f->EdgeShrinkDevice->GetBackfaceProperty());
170 tempDev->SetBackfaceProperty(prp);
171 this->EdgeShrinkDevice = tempDev;
174 this->DataSource = f->DataSource;
176 this->myReader = f->myReader;
180 this->SALOME_Actor::ShallowCopy(prop);
182 // Here we need to modify default ShallowCopy() results
183 // Create copies of properties
185 vtkProperty* prp = vtkProperty::New();
186 prp->DeepCopy(f->GetProperty());
187 this->SetProperty(prp);
189 prp = vtkProperty::New();
190 prp->DeepCopy(f->GetBackfaceProperty());
191 this->SetBackfaceProperty(prp);
194 vtkDataSetMapper* mpr = vtkDataSetMapper::New();
195 mpr->ShallowCopy(f->GetMapper());
196 mpr->SetInput(f->DataSource);
197 this->SetMapper(mpr);
201 void SMESH_Actor::Render(vtkRenderer *ren, vtkMapper *Mapper )
203 if (this->Mapper == NULL) {
204 MESSAGE ("No mapper for actor.")
208 if ( myDisplayMode == 1 ) {
209 EdgeDevice->VisibilityOn();
210 EdgeShrinkDevice->VisibilityOff();
211 } else if ( myDisplayMode == 2 ) {
212 EdgeShrinkDevice->VisibilityOn();
213 EdgeDevice->VisibilityOff();
215 EdgeShrinkDevice->VisibilityOff();
216 EdgeDevice->VisibilityOff();
220 vtkMapper *bestMapper;
221 bestMapper = this->Mapper;
223 /* render the property */
224 if (!this->Property) {
225 // force creation of a property
229 if ( ishighlighted ) {
230 if ( myDisplayMode == 1 ) {
231 EdgeDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
232 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
233 } else if ( myDisplayMode == 2 ) {
234 EdgeShrinkDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
236 this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
238 } else if (! ispreselected ) {
239 if ( myDisplayMode == 1 ) {
240 EdgeDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
241 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
243 else if ( myDisplayMode == 2 )
244 EdgeShrinkDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
246 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
249 if ( myDisplayMode == 1 )
250 EdgeDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
251 else if ( myDisplayMode == 2 )
252 EdgeShrinkDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
254 this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);
257 this->Property->Render(this, ren);
258 if (this->BackfaceProperty) {
259 this->BackfaceProperty->BackfaceRender(this, ren);
260 this->Device->SetBackfaceProperty(this->BackfaceProperty);
262 this->Device->SetProperty(this->Property);
264 /* render the texture */
266 this->Texture->Render(ren);
270 // Store information on time it takes to render.
271 // We might want to estimate time from the number of polygons in mapper.
272 this->Device->Render(ren,bestMapper);
273 this->EstimatedRenderTime = bestMapper->GetTimeToDraw();
276 int SMESH_Actor::RenderOpaqueGeometry(vtkViewport *vp)
278 int renderedSomething = 0;
279 vtkRenderer *ren = (vtkRenderer *)vp;
281 if ( ! this->Mapper ) {
285 // make sure we have a property
286 if (!this->Property) {
287 // force creation of a property
291 if ( ishighlighted ) {
292 if ( myDisplayMode == 1 ) {
293 EdgeDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
294 } else if ( myDisplayMode == 2 ) {
295 EdgeShrinkDevice->GetProperty()->SetColor(edgeHighlightColor.r,edgeHighlightColor.g,edgeHighlightColor.b);
297 this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
299 } else if (! ispreselected ) {
300 if ( myDisplayMode == 1 )
301 EdgeDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
302 else if ( myDisplayMode == 2 )
303 EdgeShrinkDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
305 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
308 if ( myDisplayMode == 1 )
309 EdgeDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
310 else if ( myDisplayMode == 2 )
311 EdgeShrinkDevice->GetProperty()->SetColor(edgePreselectedColor.r,edgePreselectedColor.g,edgePreselectedColor.b);
313 this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);
316 // is this actor opaque ?
317 if (this->GetIsOpaque()) {
318 this->Property->Render(this, ren);
320 // render the backface property
321 if (this->BackfaceProperty) {
322 this->BackfaceProperty->BackfaceRender(this, ren);
325 // render the texture
327 this->Texture->Render(ren);
329 this->Render(ren,this->Mapper);
331 renderedSomething = 1;
334 return renderedSomething;
338 void SMESH_Actor::SetColor(float r,float g,float b)
345 void SMESH_Actor::GetColor(float& r,float& g,float& b)
352 void SMESH_Actor::SetPreselectedColor(float r,float g,float b)
354 actorPreselectedColor.r = r;
355 actorPreselectedColor.g = g;
356 actorPreselectedColor.b = b;
359 void SMESH_Actor::GetPreselectedColor(float& r,float& g,float& b)
361 r = actorPreselectedColor.r;
362 g = actorPreselectedColor.g;
363 b = actorPreselectedColor.b;
366 void SMESH_Actor::SetHighlightColor(float r,float g,float b)
368 actorHighlightColor.r = r;
369 actorHighlightColor.g = g;
370 actorHighlightColor.b = b;
373 void SMESH_Actor::GetHighlightColor(float& r,float& g,float& b)
375 r = actorHighlightColor.r;
376 g = actorHighlightColor.g;
377 b = actorHighlightColor.b;
380 void SMESH_Actor::SetEdgeColor(float r,float g,float b)
387 void SMESH_Actor::GetEdgeColor(float& r,float& g,float& b)
394 void SMESH_Actor::SetEdgeHighlightColor(float r,float g,float b)
396 edgeHighlightColor.r = r;
397 edgeHighlightColor.g = g;
398 edgeHighlightColor.b = b;
401 void SMESH_Actor::GetEdgeHighlightColor(float& r,float& g,float& b)
403 r = edgeHighlightColor.r;
404 g = edgeHighlightColor.g;
405 b = edgeHighlightColor.b;
408 void SMESH_Actor::SetEdgePreselectedColor(float r,float g,float b)
410 edgePreselectedColor.r = r;
411 edgePreselectedColor.g = g;
412 edgePreselectedColor.b = b;
415 void SMESH_Actor::GetEdgePreselectedColor(float& r,float& g,float& b)
417 r = edgePreselectedColor.r;
418 g = edgePreselectedColor.g;
419 b = edgePreselectedColor.b;
423 void SMESH_Actor::SetNodeColor(float r,float g,float b)
425 actorNodeColor.r = r ;
426 actorNodeColor.g = g ;
427 actorNodeColor.b = b ;
430 void SMESH_Actor::GetNodeColor(float& r,float& g,float& b)
432 r = actorNodeColor.r ;
433 g = actorNodeColor.g ;
434 b = actorNodeColor.b ;
437 void SMESH_Actor::SetNodeSize(int size)
439 actorNodeSize = size ;
442 int SMESH_Actor::GetNodeSize()
444 return actorNodeSize ;
448 void SMESH_Actor::AddNode(int idSMESHDSnode,int idVTKnode)
450 if (DataSource->IsA("SMESH_Grid")) {
451 ((SMESH_Grid*)DataSource)->AddNode(idSMESHDSnode, idVTKnode);
453 MESSAGE("AddNode() method has been moved to SMESH_Grid class");
455 void SMESH_Actor::AddElement(int idSMESHDSelement, int idVTKelement)
457 if (DataSource->IsA("SMESH_Grid")) {
458 ((SMESH_Grid*)DataSource)->AddElement(idSMESHDSelement, idVTKelement);
460 MESSAGE("AddElement() method has been moved to SMESH_Grid class");
463 void SMESH_Actor::SetIdsVTKNode(const TColStd_DataMapOfIntegerInteger& mapVTK)
465 if (DataSource->IsA("SMESH_Grid")) {
466 ((SMESH_Grid*)DataSource)->SetIdsVTKNode(mapVTK);
468 MESSAGE("SetIdsVTKNode() method has been moved to SMESH_Grid class");
470 void SMESH_Actor::SetIdsSMESHDSNode(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
472 if (DataSource->IsA("SMESH_Grid")) {
473 ((SMESH_Grid*)DataSource)->SetIdsSMESHDSNode(mapSMESHDS);
475 MESSAGE("SetIdsSMESHDSNode() method has been moved to SMESH_Grid class");
478 void SMESH_Actor::SetIdsVTKElement(const TColStd_DataMapOfIntegerInteger& mapVTK)
480 if (DataSource->IsA("SMESH_Grid")) {
481 ((SMESH_Grid*)DataSource)->SetIdsVTKElement(mapVTK);
483 MESSAGE("SetIdsVTKElement() method has been moved to SMESH_Grid class");
485 void SMESH_Actor::SetIdsSMESHDSElement(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
487 if (DataSource->IsA("SMESH_Grid")) {
488 ((SMESH_Grid*)DataSource)->SetIdsSMESHDSElement(mapSMESHDS);
490 MESSAGE("SetIdsSMESHDSElement() method has been moved to SMESH_Grid class");
493 int SMESH_Actor::GetIdVTKNode(int idSMESHDSnode)
495 if (DataSource->IsA("SMESH_Grid")) {
496 return ((SMESH_Grid*)DataSource)->GetIdVTKNode(idSMESHDSnode);
498 MESSAGE("GetIdVTKNode() method has been moved to SMESH_Grid class");
502 int SMESH_Actor::GetIdVTKElement(int idSMESHDSelement)
504 if (DataSource->IsA("SMESH_Grid")) {
505 return ((SMESH_Grid*)DataSource)->GetIdVTKElement(idSMESHDSelement);
507 MESSAGE("GetIdVTKElement() method has been moved to SMESH_Grid class");
513 int SMESH_Actor::GetIdSMESHDSNode(int idVTKnode)
515 if (DataSource->IsA("SMESH_Grid")) {
516 return ((SMESH_Grid*)DataSource)->GetIdSMESHDSNode(idVTKnode);
518 MESSAGE("GetIdSMESHDSNode() method has been moved to SMESH_Grid class");
523 int SMESH_Actor::GetIdSMESHDSElement(int idVTKelement)
525 if (DataSource->IsA("SMESH_Grid")) {
526 return ((SMESH_Grid*)DataSource)->GetIdSMESHDSElement(idVTKelement);
528 MESSAGE("AddNode() method has been moved to SMESH_Grid class");
533 void SMESH_Actor::ClearNode()
535 if (DataSource->IsA("SMESH_Grid")) {
536 ((SMESH_Grid*)DataSource)->ClearNode();
538 MESSAGE("ClearNode() method has been moved to SMESH_Grid class");
541 void SMESH_Actor::ClearElement()
543 if (DataSource->IsA("SMESH_Grid")) {
544 ((SMESH_Grid*)DataSource)->ClearElement();
546 MESSAGE("ClearElement() method has been moved to SMESH_Grid class");
549 void SMESH_Actor::RemoveNode(int id)
551 if (DataSource->IsA("SMESH_Grid")) {
552 ((SMESH_Grid*)DataSource)->RemoveNode(id);
554 MESSAGE("RemoveNode() method has been moved to SMESH_Grid class");
556 void SMESH_Actor::RemoveElement(int id)
558 if (DataSource->IsA("SMESH_Grid")) {
559 ((SMESH_Grid*)DataSource)->RemoveElement(id);
561 MESSAGE("RemoveElement() method has been moved to SMESH_Grid class");
564 void SMESH_Actor::setDisplayMode(int thenewmode) {
565 myDisplayMode = thenewmode;
566 if ( myDisplayMode == 1 ) {
567 EdgeDevice->VisibilityOn();
568 EdgeShrinkDevice->VisibilityOff();
569 } else if ( myDisplayMode == 2 ) {
570 EdgeDevice->VisibilityOff();
571 EdgeShrinkDevice->VisibilityOn();
573 EdgeDevice->VisibilityOff();
574 EdgeShrinkDevice->VisibilityOff();
578 float SMESH_Actor::GetShrinkFactor()
580 return myShrinkFactor;
583 void SMESH_Actor::SetShrinkFactor(float value )
588 myShrinkFactor = value;
591 void SMESH_Actor::GetChildActors(vtkActorCollection* actors)
593 actors->AddItem(EdgeDevice);
594 actors->AddItem(EdgeShrinkDevice);
597 void SMESH_Actor::SetVisibility(bool visibility)
600 this->VisibilityOn();
601 if ( myDisplayMode == 1 ) {
602 EdgeDevice->VisibilityOn();
603 EdgeShrinkDevice->VisibilityOff();
604 } else if ( myDisplayMode == 2 ) {
605 EdgeDevice->VisibilityOff();
606 EdgeShrinkDevice->VisibilityOn();
608 EdgeDevice->VisibilityOff();
609 EdgeShrinkDevice->VisibilityOff();
612 this->VisibilityOff();
613 EdgeDevice->VisibilityOff();
614 EdgeShrinkDevice->VisibilityOff();