Back to index

salome-gui  6.5.0
VTKViewer_Actor.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME OBJECT : implementation of interactive object visualization for OCC and VTK viewers
00024 //  File   : SALOME_Actor.cxx
00025 //  Author : Nicolas REJNERI
00026 
00032 #include "VTKViewer_Actor.h"
00033 
00034 #include "VTKViewer_Transform.h"
00035 #include "VTKViewer_TransformFilter.h"
00036 #include "VTKViewer_GeometryFilter.h"
00037 
00038 // VTK Includes
00039 #include <vtkCell.h>
00040 #include <vtkPolyData.h>
00041 #include <vtkObjectFactory.h>
00042 #include <vtkDataSetMapper.h>
00043 #include <vtkPolyDataMapper.h>
00044 #include <vtkRenderer.h>
00045 #include <vtkPassThroughFilter.h>
00046 
00047 #if defined __GNUC__
00048   #if __GNUC__ == 2
00049     #define __GNUC_2__
00050   #endif
00051 #endif
00052 
00053 int VTKViewer_POINT_SIZE = 5;
00054 int VTKViewer_LINE_WIDTH = 3;
00055 
00056 
00057 vtkStandardNewMacro(VTKViewer_Actor);
00058 
00062 VTKViewer_Actor
00063 ::VTKViewer_Actor():
00064   myOpacity(1.0),
00065   myIsHighlighted(false),
00066   myIsPreselected(false),
00067   myRepresentation(VTKViewer::Representation::Surface),
00068   myDisplayMode(1),
00069   myProperty(vtkProperty::New()),
00070   PreviewProperty(NULL),
00071   myIsInfinite(false),
00072   myIsResolveCoincidentTopology(true),
00073   myStoreMapping(false),
00074   myGeomFilter(VTKViewer_GeometryFilter::New()),
00075   myTransformFilter(VTKViewer_TransformFilter::New())
00076 {
00077   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
00078                                                                  myPolygonOffsetUnits);
00079 
00080   for(int i = 0; i < 6; i++)
00081     myPassFilter.push_back(vtkPassThroughFilter::New());
00082 }
00083 
00087 VTKViewer_Actor
00088 ::~VTKViewer_Actor()
00089 {
00090   SetPreviewProperty(NULL);
00091 
00092   myGeomFilter->Delete();
00093 
00094   myTransformFilter->Delete();
00095 
00096   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
00097     if(myPassFilter[i])
00098       myPassFilter[i]->Delete();
00099   
00100   myProperty->Delete();
00101 }
00102 
00106 const char* 
00107 VTKViewer_Actor
00108 ::getName() 
00109 { 
00110   return myName.c_str(); 
00111 }
00112 
00117 void
00118 VTKViewer_Actor
00119 ::setName(const char* theName)
00120 {
00121   myName = theName;
00122 }
00123 
00127 void
00128 VTKViewer_Actor
00129 ::AddToRender(vtkRenderer* theRenderer)
00130 {
00131   theRenderer->AddActor(this);
00132 }
00133 
00137 void 
00138 VTKViewer_Actor
00139 ::RemoveFromRender(vtkRenderer* theRenderer)
00140 {
00141   theRenderer->RemoveActor(this);
00142 }
00143 
00147 void
00148 VTKViewer_Actor
00149 ::GetChildActors(vtkActorCollection*) 
00150 {}
00151 
00156 void
00157 VTKViewer_Actor
00158 ::SetTransform(VTKViewer_Transform* theTransform)
00159 {
00160   myTransformFilter->SetTransform(theTransform);
00161 }
00162 
00163 
00167 void
00168 VTKViewer_Actor
00169 ::SetMapper(vtkMapper* theMapper)
00170 {
00171   InitPipeLine(theMapper);
00172 }
00173 
00177 void
00178 VTKViewer_Actor
00179 ::InitPipeLine(vtkMapper* theMapper)
00180 {
00181   if(theMapper){
00182     int anId = 0;
00183     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
00184     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
00185     
00186     anId++; // 1
00187     myGeomFilter->SetStoreMapping( myStoreMapping );
00188     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
00189 
00190     anId++; // 2
00191     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
00192     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
00193 
00194     anId++; // 3
00195     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
00196 
00197     anId++; // 4
00198     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
00199     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
00200 
00201     anId++; // 5
00202     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
00203       aMapper->SetInput(myPassFilter[anId]->GetOutput());
00204     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
00205       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
00206     }
00207   }
00208   Superclass::SetMapper(theMapper);
00209 }
00210 
00214 void
00215 VTKViewer_Actor
00216 ::Render(vtkRenderer *ren, vtkMapper* m)
00217 {
00218   if(vtkDataSet* aDataSet = GetInput()){
00219     static vtkFloatingPointType PERCENTS_OF_DETAILS = 0.50;
00220     vtkIdType aNbOfPoints = vtkIdType(aDataSet->GetNumberOfPoints()*PERCENTS_OF_DETAILS);
00221     if(aNbOfPoints > 0)
00222       SetNumberOfCloudPoints(aNbOfPoints);
00223   }
00224 
00225   if(myIsResolveCoincidentTopology){
00226     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
00227     vtkFloatingPointType aFactor, aUnit; 
00228     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
00229     
00230     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
00231     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
00232                                                                    myPolygonOffsetUnits);
00233     Superclass::Render(ren,m);
00234     
00235     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
00236     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
00237   }else{
00238     Superclass::Render(ren,m);
00239   }
00240 }
00241 
00246 void
00247 VTKViewer_Actor
00248 ::SetResolveCoincidentTopology(bool theIsResolve) 
00249 {
00250   myIsResolveCoincidentTopology = theIsResolve;
00251 }
00252 
00257 void
00258 VTKViewer_Actor
00259 ::SetPolygonOffsetParameters(vtkFloatingPointType factor, 
00260                              vtkFloatingPointType units)
00261 {
00262   myPolygonOffsetFactor = factor;
00263   myPolygonOffsetUnits = units;
00264 }
00265 
00270 void
00271 VTKViewer_Actor
00272 ::GetPolygonOffsetParameters(vtkFloatingPointType& factor, 
00273                              vtkFloatingPointType& units)
00274 {
00275   factor = myPolygonOffsetFactor;
00276   units = myPolygonOffsetUnits;
00277 }
00278 
00282 vtkFloatingPointType
00283 VTKViewer_Actor
00284 ::GetShrinkFactor() 
00285 { 
00286   return 1.0;
00287 }
00288 
00292 bool
00293 VTKViewer_Actor
00294 ::IsShrunkable() 
00295 { 
00296   return false;
00297 }
00298 
00302 bool
00303 VTKViewer_Actor
00304 ::IsShrunk() 
00305 { 
00306   return false;
00307 }
00308 
00312 void
00313 VTKViewer_Actor
00314 ::SetShrink() 
00315 {} 
00316 
00320 void
00321 VTKViewer_Actor
00322 ::UnShrink() 
00323 {}
00324 
00328 vtkDataSet* 
00329 VTKViewer_Actor
00330 ::GetInput()
00331 {
00332   return myPassFilter.front()->GetOutput();
00333 }
00334 
00338 unsigned long int
00339 VTKViewer_Actor
00340 ::GetMTime()
00341 {
00342   unsigned long mTime = this->Superclass::GetMTime();
00343   unsigned long time = myTransformFilter->GetMTime();
00344   mTime = ( time > mTime ? time : mTime );
00345   if(vtkDataSet *aDataSet = dynamic_cast<vtkDataSet*>(myPassFilter[0]->GetInput())){ // bad usage of GetInput
00346     time = aDataSet->GetMTime();
00347     mTime = ( time > mTime ? time : mTime );
00348   }
00349   return mTime;
00350 }
00351 
00356 void
00357 VTKViewer_Actor
00358 ::SetRepresentation(int theMode) 
00359 { 
00360   using namespace VTKViewer::Representation;
00361   switch(myRepresentation){
00362   case Points : 
00363   case Surface : 
00364   case SurfaceWithEdges :
00365     myProperty->SetAmbient(GetProperty()->GetAmbient());
00366     myProperty->SetDiffuse(GetProperty()->GetDiffuse());
00367     myProperty->SetSpecular(GetProperty()->GetSpecular());
00368     break;
00369   }    
00370 
00371   switch(theMode){
00372   case Points : 
00373   case Surface : 
00374   case SurfaceWithEdges :
00375     GetProperty()->SetAmbient(myProperty->GetAmbient());
00376     GetProperty()->SetDiffuse(myProperty->GetDiffuse());
00377     GetProperty()->SetSpecular(myProperty->GetSpecular());
00378     break;
00379   default:
00380     GetProperty()->SetAmbient(1.0);
00381     GetProperty()->SetDiffuse(0.0);
00382     GetProperty()->SetSpecular(0.0);
00383   }
00384 
00385   switch(theMode){
00386   case Insideframe : 
00387     myGeomFilter->SetInside(true);
00388     myGeomFilter->SetWireframeMode(true);
00389     GetProperty()->SetRepresentation(VTK_WIREFRAME);
00390     break;
00391   case Points : 
00392     GetProperty()->SetPointSize(VTKViewer_POINT_SIZE);  
00393     GetProperty()->SetRepresentation(theMode);
00394     myGeomFilter->SetWireframeMode(false);
00395     myGeomFilter->SetInside(false);
00396     break;
00397   case Wireframe : 
00398     GetProperty()->SetRepresentation(theMode);
00399     myGeomFilter->SetWireframeMode(true);
00400     myGeomFilter->SetInside(false);
00401     break;
00402   case Surface : 
00403   case SurfaceWithEdges :
00404     GetProperty()->SetRepresentation(theMode);
00405     myGeomFilter->SetWireframeMode(false);
00406     myGeomFilter->SetInside(false);
00407     break;
00408   }
00409 
00410   myRepresentation = theMode;
00411 }
00412 
00416 int
00417 VTKViewer_Actor
00418 ::GetRepresentation()
00419 { 
00420   return myRepresentation;
00421 }
00422 
00426 int 
00427 VTKViewer_Actor
00428 ::GetNodeObjId(int theVtkID)
00429 { 
00430   return theVtkID;
00431 }
00432 
00436 vtkFloatingPointType* 
00437 VTKViewer_Actor
00438 ::GetNodeCoord(int theObjID)
00439 {
00440   return GetInput()->GetPoint(theObjID);
00441 }
00442 
00446 vtkCell* 
00447 VTKViewer_Actor
00448 ::GetElemCell(int theObjID)
00449 {
00450   return GetInput()->GetCell(theObjID);
00451 }
00452 
00456 int
00457 VTKViewer_Actor
00458 ::GetElemObjId(int theVtkID) 
00459 { 
00460   return theVtkID;
00461 }
00462 
00463 
00467 int
00468 VTKViewer_Actor
00469 ::GetObjDimension( const int theObjId )
00470 {
00471   if ( vtkCell* aCell = GetElemCell(theObjId) )
00472     return aCell->GetCellDimension();
00473   return 0;
00474 }
00475 
00480 void
00481 VTKViewer_Actor
00482 ::SetInfinitive(bool theIsInfinite)
00483 { 
00484   myIsInfinite = theIsInfinite;
00485 }
00486 
00490 bool
00491 VTKViewer_Actor
00492 ::IsInfinitive()
00493 { 
00494   if(myIsInfinite)
00495     return true;
00496 
00497   static vtkFloatingPointType MAX_DISTANCE = 0.9*VTK_LARGE_FLOAT;
00498   vtkFloatingPointType aBounds[6];
00499   GetBounds(aBounds);
00500   for(int i = 0; i < 6; i++)
00501     if(fabs(aBounds[i]) > MAX_DISTANCE)
00502       return true;
00503   
00504   static vtkFloatingPointType MIN_DISTANCE = 1.0/VTK_LARGE_FLOAT;
00505   if(GetLength() < MIN_DISTANCE)
00506     return true;
00507   
00508   return false;
00509 }
00510 
00514 vtkFloatingPointType* 
00515 VTKViewer_Actor
00516 ::GetBounds()
00517 {
00518   return Superclass::GetBounds();
00519 }
00520 
00521 
00525 void
00526 VTKViewer_Actor
00527 ::GetBounds(vtkFloatingPointType theBounds[6])
00528 {
00529   Superclass::GetBounds(theBounds);
00530 }
00531 
00532 
00533 bool
00534 VTKViewer_Actor
00535 ::IsSetCamera() const 
00536 { 
00537   return false; 
00538 }
00539 
00540 bool
00541 VTKViewer_Actor
00542 ::IsResizable() const 
00543 { 
00544   return false; 
00545 }
00546 
00547 void
00548 VTKViewer_Actor
00549 ::SetSize( const vtkFloatingPointType ) 
00550 {}
00551 
00552 
00553 void 
00554 VTKViewer_Actor
00555 ::SetCamera( vtkCamera* ) 
00556 {}
00557 
00558 
00559 void
00560 VTKViewer_Actor
00561 ::SetOpacity(vtkFloatingPointType theOpacity)
00562 { 
00563   myOpacity = theOpacity;
00564   GetProperty()->SetOpacity(theOpacity);
00565 }
00566 
00567 vtkFloatingPointType
00568 VTKViewer_Actor
00569 ::GetOpacity()
00570 {
00571   return myOpacity;
00572 }
00573 
00574 
00578 void
00579 VTKViewer_Actor
00580 ::SetColor(vtkFloatingPointType r,
00581            vtkFloatingPointType g,
00582            vtkFloatingPointType b)
00583 {
00584   GetProperty()->SetColor(r,g,b);
00585 }
00586 
00590 void
00591 VTKViewer_Actor
00592 ::SetColor(const vtkFloatingPointType theRGB[3])
00593 { 
00594   SetColor(theRGB[0],theRGB[1],theRGB[2]);
00595 }
00596 
00600 void
00601 VTKViewer_Actor
00602 ::GetColor(vtkFloatingPointType& r,
00603            vtkFloatingPointType& g,
00604            vtkFloatingPointType& b)
00605 {
00606   vtkFloatingPointType aColor[3];
00607   GetProperty()->GetColor(aColor);
00608   r = aColor[0];
00609   g = aColor[1];
00610   b = aColor[2];
00611 }
00612 
00613 
00617 void
00618 VTKViewer_Actor
00619 ::SetMaterial(std::vector<vtkProperty*> theProps)
00620 {
00621 }
00622 
00626 vtkProperty* 
00627 VTKViewer_Actor
00628 ::GetMaterial()
00629 {
00630   return NULL;
00631 }
00632 
00636 int
00637 VTKViewer_Actor
00638 ::getDisplayMode()
00639 { 
00640   return myDisplayMode; 
00641 }
00642 
00646 void
00647 VTKViewer_Actor
00648 ::setDisplayMode(int theMode)
00649 { 
00650   SetRepresentation(theMode + 1); 
00651   myDisplayMode = GetRepresentation() - 1;
00652 }
00653 
00654 
00658 bool
00659 VTKViewer_Actor
00660 ::hasHighlight() 
00661 { 
00662   return false; 
00663 } 
00664 
00668 bool
00669 VTKViewer_Actor
00670 ::isHighlighted() 
00671 { 
00672   return myIsHighlighted; 
00673 }
00674 
00678 bool
00679 VTKViewer_Actor
00680 ::isPreselected() 
00681 { 
00682   return myIsPreselected; 
00683 }
00684 
00688 void
00689 VTKViewer_Actor
00690 ::SetPreSelected(bool thePreselect) 
00691 { 
00692   myIsPreselected = thePreselect;
00693 }
00694 
00698 void
00699 VTKViewer_Actor
00700 ::highlight(bool theIsHighlight)
00701 {
00702   myIsHighlighted = theIsHighlight; 
00703 }
00704 
00708 void VTKViewer_Actor::SetQuadraticArcMode(bool theFlag){
00709   myGeomFilter->SetQuadraticArcMode(theFlag);
00710 }
00711 
00715 bool VTKViewer_Actor::GetQuadraticArcMode() const{
00716   return myGeomFilter->GetQuadraticArcMode();
00717 }
00721 void VTKViewer_Actor::SetQuadraticArcAngle(vtkFloatingPointType theMaxAngle){
00722   myGeomFilter->SetQuadraticArcAngle(theMaxAngle);
00723 }
00724 
00728 vtkFloatingPointType VTKViewer_Actor::GetQuadraticArcAngle() const{
00729   return myGeomFilter->GetQuadraticArcAngle();
00730 }
00731 
00736 vtkDataSet* VTKViewer_Actor::GetHighlightedDataSet() {
00737   return GetInput();
00738 }
00739 
00740 
00741 
00742 vtkCxxSetObjectMacro(VTKViewer_Actor,PreviewProperty,vtkProperty);