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();
81 ishighlighted = false;
82 ispreselected = false;
88 edgeHighlightColor.r = 1.;
89 edgeHighlightColor.g = 1.;
90 edgeHighlightColor.b = 1.;
92 edgePreselectedColor.r = 0.;
93 edgePreselectedColor.g = 1.;
94 edgePreselectedColor.b = 1.;
100 actorHighlightColor.r = 1.;
101 actorHighlightColor.g = 1.;
102 actorHighlightColor.b = 1.;
104 actorPreselectedColor.r = 0.;
105 actorPreselectedColor.g = 1.;
106 actorPreselectedColor.b = 1.;
108 actorNodeColor.r = 1.;
109 actorNodeColor.g = 1.;
110 actorNodeColor.b = 0.;
116 SMESH_Actor::~SMESH_Actor()
118 this->EdgeDevice->Delete();
119 this->EdgeShrinkDevice->Delete();
122 void SMESH_Actor::setReader(vtkUnstructuredGridReader* r) {
126 vtkUnstructuredGridReader* SMESH_Actor::getReader() {
130 vtkMapper* SMESH_Actor::getMapper() {
131 return (this->Mapper);
134 void SMESH_Actor::ShallowCopy(vtkProp *prop)
136 SMESH_Actor *f = SMESH_Actor::SafeDownCast(prop);
139 this->setName( f->getName() );
141 this->setIO( f->getIO() );
142 this->setDisplayMode( f->getDisplayMode() );
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;
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;
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;
176 this->DataSource = f->DataSource;
178 this->myReader = f->myReader;
182 this->SALOME_Actor::ShallowCopy(prop);
184 // Here we need to modify default ShallowCopy() results
185 // Create copies of properties
187 vtkProperty* prp = vtkProperty::New();
188 prp->DeepCopy(f->GetProperty());
189 this->SetProperty(prp);
191 prp = vtkProperty::New();
192 prp->DeepCopy(f->GetBackfaceProperty());
193 this->SetBackfaceProperty(prp);
196 vtkDataSetMapper* mpr = vtkDataSetMapper::New();
197 mpr->ShallowCopy(f->GetMapper());
198 mpr->SetInput(f->DataSource);
199 this->SetMapper(mpr);
203 void SMESH_Actor::Render(vtkRenderer *ren, vtkMapper *Mapper )
205 if (this->Mapper == NULL) {
206 MESSAGE ("No mapper for actor.")
210 if ( myDisplayMode == 1 ) {
211 EdgeDevice->VisibilityOn();
212 EdgeShrinkDevice->VisibilityOff();
213 } else if ( myDisplayMode == 2 ) {
214 EdgeShrinkDevice->VisibilityOn();
215 EdgeDevice->VisibilityOff();
217 EdgeShrinkDevice->VisibilityOff();
218 EdgeDevice->VisibilityOff();
222 vtkMapper *bestMapper;
223 bestMapper = this->Mapper;
225 /* render the property */
226 if (!this->Property) {
227 // force creation of a property
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);
238 this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
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);
245 else if ( myDisplayMode == 2 )
246 EdgeShrinkDevice->GetProperty()->SetColor(edgeColor.r,edgeColor.g,edgeColor.b);
248 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
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);
256 this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);
259 this->Property->Render(this, ren);
260 if (this->BackfaceProperty) {
261 this->BackfaceProperty->BackfaceRender(this, ren);
262 this->Device->SetBackfaceProperty(this->BackfaceProperty);
264 this->Device->SetProperty(this->Property);
266 /* render the texture */
268 this->Texture->Render(ren);
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();
278 int SMESH_Actor::RenderOpaqueGeometry(vtkViewport *vp)
280 int renderedSomething = 0;
281 vtkRenderer *ren = (vtkRenderer *)vp;
283 if ( ! this->Mapper ) {
287 // make sure we have a property
288 if (!this->Property) {
289 // force creation of a property
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);
299 this->GetProperty()->SetColor(actorHighlightColor.r,actorHighlightColor.g,actorHighlightColor.b);
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);
307 this->GetProperty()->SetColor(actorColor.r,actorColor.g,actorColor.b);
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);
315 this->GetProperty()->SetColor(actorPreselectedColor.r,actorPreselectedColor.g,actorPreselectedColor.b);
318 // is this actor opaque ?
319 if (this->GetIsOpaque()) {
320 this->Property->Render(this, ren);
322 // render the backface property
323 if (this->BackfaceProperty) {
324 this->BackfaceProperty->BackfaceRender(this, ren);
327 // render the texture
329 this->Texture->Render(ren);
331 this->Render(ren,this->Mapper);
333 renderedSomething = 1;
336 return renderedSomething;
340 void SMESH_Actor::SetColor(float r,float g,float b)
347 void SMESH_Actor::GetColor(float& r,float& g,float& b)
354 void SMESH_Actor::SetPreselectedColor(float r,float g,float b)
356 actorPreselectedColor.r = r;
357 actorPreselectedColor.g = g;
358 actorPreselectedColor.b = b;
361 void SMESH_Actor::GetPreselectedColor(float& r,float& g,float& b)
363 r = actorPreselectedColor.r;
364 g = actorPreselectedColor.g;
365 b = actorPreselectedColor.b;
368 void SMESH_Actor::SetHighlightColor(float r,float g,float b)
370 actorHighlightColor.r = r;
371 actorHighlightColor.g = g;
372 actorHighlightColor.b = b;
375 void SMESH_Actor::GetHighlightColor(float& r,float& g,float& b)
377 r = actorHighlightColor.r;
378 g = actorHighlightColor.g;
379 b = actorHighlightColor.b;
382 void SMESH_Actor::SetEdgeColor(float r,float g,float b)
389 void SMESH_Actor::GetEdgeColor(float& r,float& g,float& b)
396 void SMESH_Actor::SetEdgeHighlightColor(float r,float g,float b)
398 edgeHighlightColor.r = r;
399 edgeHighlightColor.g = g;
400 edgeHighlightColor.b = b;
403 void SMESH_Actor::GetEdgeHighlightColor(float& r,float& g,float& b)
405 r = edgeHighlightColor.r;
406 g = edgeHighlightColor.g;
407 b = edgeHighlightColor.b;
410 void SMESH_Actor::SetEdgePreselectedColor(float r,float g,float b)
412 edgePreselectedColor.r = r;
413 edgePreselectedColor.g = g;
414 edgePreselectedColor.b = b;
417 void SMESH_Actor::GetEdgePreselectedColor(float& r,float& g,float& b)
419 r = edgePreselectedColor.r;
420 g = edgePreselectedColor.g;
421 b = edgePreselectedColor.b;
425 void SMESH_Actor::SetNodeColor(float r,float g,float b)
427 actorNodeColor.r = r ;
428 actorNodeColor.g = g ;
429 actorNodeColor.b = b ;
432 void SMESH_Actor::GetNodeColor(float& r,float& g,float& b)
434 r = actorNodeColor.r ;
435 g = actorNodeColor.g ;
436 b = actorNodeColor.b ;
439 void SMESH_Actor::SetNodeSize(int size)
441 actorNodeSize = size ;
444 int SMESH_Actor::GetNodeSize()
446 return actorNodeSize ;
450 void SMESH_Actor::AddNode(int idSMESHDSnode,int idVTKnode)
452 if (DataSource->IsA("SMESH_Grid")) {
453 ((SMESH_Grid*)DataSource)->AddNode(idSMESHDSnode, idVTKnode);
455 MESSAGE("AddNode() method has been moved to SMESH_Grid class");
457 void SMESH_Actor::AddElement(int idSMESHDSelement, int idVTKelement)
459 if (DataSource->IsA("SMESH_Grid")) {
460 ((SMESH_Grid*)DataSource)->AddElement(idSMESHDSelement, idVTKelement);
462 MESSAGE("AddElement() method has been moved to SMESH_Grid class");
465 void SMESH_Actor::SetIdsVTKNode(const TColStd_DataMapOfIntegerInteger& mapVTK)
467 if (DataSource->IsA("SMESH_Grid")) {
468 ((SMESH_Grid*)DataSource)->SetIdsVTKNode(mapVTK);
470 MESSAGE("SetIdsVTKNode() method has been moved to SMESH_Grid class");
472 void SMESH_Actor::SetIdsSMESHDSNode(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
474 if (DataSource->IsA("SMESH_Grid")) {
475 ((SMESH_Grid*)DataSource)->SetIdsSMESHDSNode(mapSMESHDS);
477 MESSAGE("SetIdsSMESHDSNode() method has been moved to SMESH_Grid class");
480 void SMESH_Actor::SetIdsVTKElement(const TColStd_DataMapOfIntegerInteger& mapVTK)
482 if (DataSource->IsA("SMESH_Grid")) {
483 ((SMESH_Grid*)DataSource)->SetIdsVTKElement(mapVTK);
485 MESSAGE("SetIdsVTKElement() method has been moved to SMESH_Grid class");
487 void SMESH_Actor::SetIdsSMESHDSElement(const TColStd_DataMapOfIntegerInteger& mapSMESHDS)
489 if (DataSource->IsA("SMESH_Grid")) {
490 ((SMESH_Grid*)DataSource)->SetIdsSMESHDSElement(mapSMESHDS);
492 MESSAGE("SetIdsSMESHDSElement() method has been moved to SMESH_Grid class");
495 int SMESH_Actor::GetIdVTKNode(int idSMESHDSnode)
497 if (DataSource->IsA("SMESH_Grid")) {
498 return ((SMESH_Grid*)DataSource)->GetIdVTKNode(idSMESHDSnode);
500 MESSAGE("GetIdVTKNode() method has been moved to SMESH_Grid class");
504 int SMESH_Actor::GetIdVTKElement(int idSMESHDSelement)
506 if (DataSource->IsA("SMESH_Grid")) {
507 return ((SMESH_Grid*)DataSource)->GetIdVTKElement(idSMESHDSelement);
509 MESSAGE("GetIdVTKElement() method has been moved to SMESH_Grid class");
515 int SMESH_Actor::GetIdSMESHDSNode(int idVTKnode)
517 if (DataSource->IsA("SMESH_Grid")) {
518 return ((SMESH_Grid*)DataSource)->GetIdSMESHDSNode(idVTKnode);
520 MESSAGE("GetIdSMESHDSNode() method has been moved to SMESH_Grid class");
525 int SMESH_Actor::GetIdSMESHDSElement(int idVTKelement)
527 if (DataSource->IsA("SMESH_Grid")) {
528 return ((SMESH_Grid*)DataSource)->GetIdSMESHDSElement(idVTKelement);
530 MESSAGE("AddNode() method has been moved to SMESH_Grid class");
535 void SMESH_Actor::ClearNode()
537 if (DataSource->IsA("SMESH_Grid")) {
538 ((SMESH_Grid*)DataSource)->ClearNode();
540 MESSAGE("ClearNode() method has been moved to SMESH_Grid class");
543 void SMESH_Actor::ClearElement()
545 if (DataSource->IsA("SMESH_Grid")) {
546 ((SMESH_Grid*)DataSource)->ClearElement();
548 MESSAGE("ClearElement() method has been moved to SMESH_Grid class");
551 void SMESH_Actor::RemoveNode(int id)
553 if (DataSource->IsA("SMESH_Grid")) {
554 ((SMESH_Grid*)DataSource)->RemoveNode(id);
556 MESSAGE("RemoveNode() method has been moved to SMESH_Grid class");
558 void SMESH_Actor::RemoveElement(int id)
560 if (DataSource->IsA("SMESH_Grid")) {
561 ((SMESH_Grid*)DataSource)->RemoveElement(id);
563 MESSAGE("RemoveElement() method has been moved to SMESH_Grid class");
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();
575 EdgeDevice->VisibilityOff();
576 EdgeShrinkDevice->VisibilityOff();
580 float SMESH_Actor::GetShrinkFactor()
582 return myShrinkFactor;
585 void SMESH_Actor::SetShrinkFactor(float value )
590 myShrinkFactor = value;
593 void SMESH_Actor::GetChildActors(vtkActorCollection* actors)
595 actors->AddItem(EdgeDevice);
596 actors->AddItem(EdgeShrinkDevice);
599 void SMESH_Actor::SetVisibility(bool 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();
610 EdgeDevice->VisibilityOff();
611 EdgeShrinkDevice->VisibilityOff();
614 this->VisibilityOff();
615 EdgeDevice->VisibilityOff();
616 EdgeShrinkDevice->VisibilityOff();