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
30 #include "SMESH_DeviceActor.h"
31 #include "SMESH_ExtractGeometry.h"
33 #include "SALOME_Transform.h"
34 #include "SALOME_TransformFilter.h"
35 #include "SALOME_PassThroughFilter.h"
36 #include "SALOME_ExtractUnstructuredGrid.h"
38 #include "utilities.h"
41 #include <vtkObjectFactory.h>
42 #include <vtkShrinkFilter.h>
43 #include <vtkShrinkPolyData.h>
45 #include <vtkProperty.h>
46 #include <vtkPolyData.h>
47 #include <vtkMergeFilter.h>
48 #include <vtkPolyDataMapper.h>
49 #include <vtkUnstructuredGrid.h>
51 #include <vtkScalarBarActor.h>
52 #include <vtkLookupTable.h>
53 #include <vtkDoubleArray.h>
54 #include <vtkCellData.h>
57 #include <vtkIdList.h>
58 #include <vtkIntArray.h>
59 #include <vtkCellArray.h>
60 #include <vtkUnsignedCharArray.h>
62 #include <vtkImplicitBoolean.h>
65 static int MYDEBUG = 0;
66 static int MYDEBUGWITHFILES = 0;
68 static int MYDEBUG = 0;
69 static int MYDEBUGWITHFILES = 0;
75 vtkStandardNewMacro(SMESH_DeviceActor);
78 SMESH_DeviceActor::SMESH_DeviceActor(){
79 if(MYDEBUG) MESSAGE("SMESH_DeviceActor");
81 myIsShrinkable = false;
82 myRepresentation = eSurface;
84 myProperty = vtkProperty::New();
85 myMapper = vtkPolyDataMapper::New();
87 vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
88 myPolygonOffsetUnits);
90 myMapper->UseLookupTableScalarRangeOn();
91 myMapper->SetColorModeToMapScalars();
93 myShrinkFilter = vtkShrinkFilter::New();
95 myExtractGeometry = SMESH_ExtractGeometry::New();
96 myExtractGeometry->SetStoreMapping(true);
98 myExtractUnstructuredGrid = SALOME_ExtractUnstructuredGrid::New();
99 myExtractUnstructuredGrid->SetStoreMapping(true);
101 myMergeFilter = vtkMergeFilter::New();
103 myStoreMapping = false;
104 myGeomFilter = SALOME_GeometryFilter::New();
106 myTransformFilter = SALOME_TransformFilter::New();
108 for(int i = 0; i < 6; i++)
109 myPassFilter.push_back(SALOME_PassThroughFilter::New());
113 SMESH_DeviceActor::~SMESH_DeviceActor(){
114 if(MYDEBUG) MESSAGE("~SMESH_DeviceActor");
115 myProperty->Delete();
117 myMapper->RemoveAllInputs();
120 myShrinkFilter->UnRegisterAllOutputs();
121 myShrinkFilter->Delete();
123 myExtractUnstructuredGrid->UnRegisterAllOutputs();
124 myExtractUnstructuredGrid->Delete();
126 myMergeFilter->UnRegisterAllOutputs();
127 myMergeFilter->Delete();
129 myGeomFilter->UnRegisterAllOutputs();
130 myGeomFilter->Delete();
132 myExtractGeometry->UnRegisterAllOutputs();
133 myExtractGeometry->Delete();
135 myTransformFilter->UnRegisterAllOutputs();
136 myTransformFilter->Delete();
138 for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++){
139 myPassFilter[i]->UnRegisterAllOutputs();
140 myPassFilter[i]->Delete();
145 void SMESH_DeviceActor::SetStoreMapping(int theStoreMapping){
146 myStoreMapping = theStoreMapping;
151 void SMESH_DeviceActor::Init(TVisualObjPtr theVisualObj,
152 vtkImplicitBoolean* theImplicitBoolean)
154 myVisualObj = theVisualObj;
155 myExtractGeometry->SetImplicitFunction(theImplicitBoolean);
156 SetUnstructuredGrid(myVisualObj->GetUnstructuredGrid());
160 void SMESH_DeviceActor::SetUnstructuredGrid(vtkUnstructuredGrid* theGrid){
162 //myIsShrinkable = theGrid->GetNumberOfCells() > 10;
163 myIsShrinkable = true;
165 myExtractGeometry->SetInput(theGrid);
167 myExtractUnstructuredGrid->SetInput(myExtractGeometry->GetOutput());
168 myMergeFilter->SetGeometry(myExtractUnstructuredGrid->GetOutput());
170 theGrid = static_cast<vtkUnstructuredGrid*>(myMergeFilter->GetOutput());
173 myPassFilter[ anId ]->SetInput( theGrid );
174 myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
177 myGeomFilter->SetStoreMapping( myStoreMapping );
178 myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
181 myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() );
182 myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
185 myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
186 myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
189 myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
190 myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
191 myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
192 myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
195 myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
196 myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
198 vtkLODActor::SetMapper( myMapper );
204 SALOME_ExtractUnstructuredGrid* SMESH_DeviceActor::GetExtractUnstructuredGrid(){
205 return myExtractUnstructuredGrid;
209 vtkUnstructuredGrid* SMESH_DeviceActor::GetUnstructuredGrid(){
210 myExtractUnstructuredGrid->Update();
211 return myExtractUnstructuredGrid->GetOutput();
215 void SMESH_DeviceActor::SetControlMode(SMESH::Controls::FunctorPtr theFunctor,
216 vtkScalarBarActor* theScalarBarActor,
217 vtkLookupTable* theLookupTable)
219 bool anIsInitialized = theFunctor;
221 vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
222 vtkUnstructuredGrid* aGrid = myExtractUnstructuredGrid->GetOutput();
223 aDataSet->ShallowCopy(aGrid);
225 vtkDoubleArray *aScalars = vtkDoubleArray::New();
226 vtkIdType aNbCells = aGrid->GetNumberOfCells();
227 aScalars->SetNumberOfComponents(1);
228 aScalars->SetNumberOfTuples(aNbCells);
230 myVisualObj->UpdateFunctor(theFunctor);
232 using namespace SMESH::Controls;
233 if(NumericalFunctor* aNumericalFunctor = dynamic_cast<NumericalFunctor*>(theFunctor.get())){
234 for(vtkIdType i = 0; i < aNbCells; i++){
235 vtkIdType anId = myExtractUnstructuredGrid->GetInputId(i);
236 vtkIdType anId2 = myExtractGeometry->GetElemObjId(anId);
237 vtkIdType anObjId = myVisualObj->GetElemObjId(anId2);
238 double aValue = aNumericalFunctor->GetValue(anObjId);
239 aScalars->SetValue(i,aValue);
241 }else if(Predicate* aPredicate = dynamic_cast<Predicate*>(theFunctor.get())){
242 for(vtkIdType i = 0; i < aNbCells; i++){
243 vtkIdType anId = myExtractUnstructuredGrid->GetInputId(i);
244 vtkIdType anId2 = myExtractGeometry->GetElemObjId(anId);
245 vtkIdType anObjId = myVisualObj->GetElemObjId(anId2);
246 bool aValue = aPredicate->IsSatisfy(anObjId);
247 aScalars->SetValue(i,aValue);
251 aDataSet->GetCellData()->SetScalars(aScalars);
254 theLookupTable->SetRange(aScalars->GetRange());
255 theLookupTable->Build();
257 myMergeFilter->SetScalars(aDataSet);
260 GetMapper()->SetScalarVisibility(anIsInitialized);
261 theScalarBarActor->SetVisibility(anIsInitialized);
265 void SMESH_DeviceActor::SetExtControlMode(SMESH::Controls::FunctorPtr theFunctor,
266 SMESH_DeviceActor* theDeviceActor)
268 myExtractUnstructuredGrid->ClearRegisteredCells();
269 myExtractUnstructuredGrid->ClearRegisteredCellsWithType();
270 myExtractUnstructuredGrid->SetModeOfChanging(SALOME_ExtractUnstructuredGrid::ePassAll);
271 myVisualObj->UpdateFunctor(theFunctor);
273 using namespace SMESH::Controls;
274 if(FreeBorders* aFreeBorders = dynamic_cast<FreeBorders*>(theFunctor.get())){
275 myExtractUnstructuredGrid->SetModeOfChanging(SALOME_ExtractUnstructuredGrid::eAdding);
276 myExtractUnstructuredGrid->ClearRegisteredCells();
277 vtkUnstructuredGrid* aGrid = theDeviceActor->GetUnstructuredGrid();
278 vtkIdType aNbCells = aGrid->GetNumberOfCells();
279 for( vtkIdType i = 0; i < aNbCells; i++ ){
280 vtkIdType anObjId = theDeviceActor->GetElemObjId(i);
281 if(aFreeBorders->IsSatisfy(anObjId))
282 myExtractUnstructuredGrid->RegisterCell(i);
284 if(!myExtractUnstructuredGrid->IsCellsRegistered())
285 myExtractUnstructuredGrid->RegisterCell(-1);
286 SetUnstructuredGrid(myVisualObj->GetUnstructuredGrid());
287 }else if(FreeEdges* aFreeEdges = dynamic_cast<FreeEdges*>(theFunctor.get())){
288 SMESH::Controls::FreeEdges::TBorders aBorders;
289 aFreeEdges->GetBoreders(aBorders);
290 vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
291 vtkUnstructuredGrid* aGrid = myVisualObj->GetUnstructuredGrid();
292 aDataSet->SetPoints(aGrid->GetPoints());
294 vtkIdType aNbCells = aBorders.size();
295 vtkIdType aCellsSize = 3*aNbCells;
296 vtkCellArray* aConnectivity = vtkCellArray::New();
297 aConnectivity->Allocate( aCellsSize, 0 );
299 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
300 aCellTypesArray->SetNumberOfComponents( 1 );
301 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
303 vtkIdList *anIdList = vtkIdList::New();
304 anIdList->SetNumberOfIds(2);
306 FreeEdges::TBorders::const_iterator anIter = aBorders.begin();
307 for(vtkIdType aVtkId; anIter != aBorders.end(); anIter++){
308 const FreeEdges::Border& aBorder = *anIter;
310 myVisualObj->GetNodeVTKId(aBorder.myPntId[0]),
311 myVisualObj->GetNodeVTKId(aBorder.myPntId[1])
313 //cout<<"aNode = "<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<endl;
314 if(aNode[0] >= 0 && aNode[1] >= 0){
315 anIdList->SetId( 0, aNode[0] );
316 anIdList->SetId( 1, aNode[1] );
317 aConnectivity->InsertNextCell( anIdList );
318 aCellTypesArray->InsertNextValue( VTK_LINE );
322 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
323 aCellLocationsArray->SetNumberOfComponents( 1 );
324 aCellLocationsArray->SetNumberOfTuples( aNbCells );
326 aConnectivity->InitTraversal();
327 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
328 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
330 aDataSet->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
332 SetUnstructuredGrid(aDataSet);
338 unsigned long int SMESH_DeviceActor::GetMTime(){
339 unsigned long mTime = this->Superclass::GetMTime();
340 mTime = max(mTime,myExtractGeometry->GetMTime());
341 mTime = max(mTime,myExtractUnstructuredGrid->GetMTime());
342 mTime = max(mTime,myMergeFilter->GetMTime());
343 mTime = max(mTime,myGeomFilter->GetMTime());
344 mTime = max(mTime,myTransformFilter->GetMTime());
349 void SMESH_DeviceActor::SetTransform(SALOME_Transform* theTransform){
350 myTransformFilter->SetTransform(theTransform);
354 void SMESH_DeviceActor::SetShrink() {
355 if ( !myIsShrinkable ) return;
356 if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
358 myShrinkFilter->SetInput( aDataSet );
359 myPassFilter[ 1 ]->SetInput( myShrinkFilter->GetOutput() );
364 void SMESH_DeviceActor::UnShrink() {
365 if ( !myIsShrunk ) return;
366 if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
368 myPassFilter[ 1 ]->SetInput( aDataSet );
369 myPassFilter[ 1 ]->Modified();
376 void SMESH_DeviceActor::SetRepresentation(EReperesent theMode){
379 myGeomFilter->SetInside(true);
380 GetProperty()->SetRepresentation(0);
383 myGeomFilter->SetInside(true);
384 GetProperty()->SetRepresentation(1);
387 GetProperty()->SetRepresentation(theMode);
388 myGeomFilter->SetInside(false);
390 myRepresentation = theMode;
391 GetProperty()->Modified();
392 myMapper->Modified();
397 void SMESH_DeviceActor::SetVisibility(int theMode){
398 if(!myExtractUnstructuredGrid->GetInput() || GetUnstructuredGrid()->GetNumberOfCells()){
399 vtkLODActor::SetVisibility(theMode);
401 vtkLODActor::SetVisibility(false);
406 int SMESH_DeviceActor::GetVisibility(){
407 if(!GetUnstructuredGrid()->GetNumberOfCells()){
408 vtkLODActor::SetVisibility(false);
410 return vtkLODActor::GetVisibility();
414 int SMESH_DeviceActor::GetNodeObjId(int theVtkID){
415 vtkIdType anID = myExtractGeometry->GetNodeObjId(theVtkID);
416 vtkIdType aRetID = myVisualObj->GetNodeObjId(anID);
417 if(MYDEBUG) MESSAGE("GetNodeObjId - theVtkID = "<<theVtkID<<"; aRetID = "<<aRetID);
421 float* SMESH_DeviceActor::GetNodeCoord(int theObjID){
422 vtkDataSet* aDataSet = myExtractGeometry->GetInput();
423 vtkIdType anID = myVisualObj->GetNodeVTKId(theObjID);
424 float* aCoord = aDataSet->GetPoint(anID);
425 if(MYDEBUG) MESSAGE("GetNodeCoord - theObjID = "<<theObjID<<"; anID = "<<anID);
430 int SMESH_DeviceActor::GetElemObjId(int theVtkID){
431 vtkIdType anId = myGeomFilter->GetElemObjId(theVtkID);
434 vtkIdType anId2 = myExtractUnstructuredGrid->GetInputId(anId);
437 vtkIdType anId3 = myExtractGeometry->GetElemObjId(anId2);
440 vtkIdType aRetID = myVisualObj->GetElemObjId(anId3);
442 MESSAGE("GetElemObjId - theVtkID = "<<theVtkID<<"; anId2 = "<<anId2<<"; anId3 = "<<anId3<<"; aRetID = "<<aRetID);
446 vtkCell* SMESH_DeviceActor::GetElemCell(int theObjID){
447 vtkDataSet* aDataSet = myExtractGeometry->GetInput();
448 vtkIdType aGridID = myVisualObj->GetElemVTKId(theObjID);
449 vtkCell* aCell = aDataSet->GetCell(aGridID);
451 MESSAGE("GetElemCell - theObjID = "<<theObjID<<"; aGridID = "<<aGridID);
456 float SMESH_DeviceActor::GetShrinkFactor(){
457 return myShrinkFilter->GetShrinkFactor();
460 void SMESH_DeviceActor::SetShrinkFactor(float theValue){
461 theValue = theValue > 0.1? theValue: 0.8;
462 myShrinkFilter->SetShrinkFactor(theValue);
467 void SMESH_DeviceActor::SetHighlited(bool theIsHighlited){
468 myIsHighlited = theIsHighlited;
472 void SMESH_DeviceActor::Render(vtkRenderer *ren, vtkMapper* m){
473 int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
474 float aStoredFactor, aStoredUnit;
475 vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
477 vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
478 float aFactor = myPolygonOffsetFactor, aUnits = myPolygonOffsetUnits;
480 static float EPS = .01;
483 vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnits);
484 vtkLODActor::Render(ren,m);
486 vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
487 vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
491 void SMESH_DeviceActor::SetPolygonOffsetParameters(float factor, float units){
492 myPolygonOffsetFactor = factor;
493 myPolygonOffsetUnits = units;