Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / OBJECT / SMESH_DeviceActor.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH OBJECT : interactive object for SMESH visualization
24 //  File   : SMESH_DeviceActor.cxx
25 //  Author : 
26 //  Module : SMESH
27 //
28 #include "SMESH_DeviceActor.h"
29 #include "SMESH_ScalarBarActor.h"
30 #include "SMESH_ExtractGeometry.h"
31 #include "SMESH_ControlsDef.hxx"
32 #include "SMESH_ActorUtils.h"
33 #include "SMESH_FaceOrientationFilter.h"
34 //#include "VTKViewer_CellLocationsArray.h"
35 #include "VTKViewer_PolyDataMapper.h"
36
37 #include <VTKViewer_Transform.h>
38 #include <VTKViewer_TransformFilter.h>
39 #include <VTKViewer_ExtractUnstructuredGrid.h>
40 #include <VTKViewer_Actor.h>
41
42 // VTK Includes
43 #include <vtkObjectFactory.h>
44 #include <vtkShrinkFilter.h>
45 #include <vtkShrinkPolyData.h>
46
47 #include <vtkProperty.h>
48 #include <vtkPolyData.h>
49 #include <vtkMergeFilter.h>
50 #include <vtkPolyDataMapper.h>
51 #include <vtkUnstructuredGrid.h>
52
53 #include <vtkLookupTable.h>
54 #include <vtkDoubleArray.h>
55 #include <vtkCellData.h>
56
57 #include <vtkCell.h>
58 #include <vtkIdList.h>
59 #include <vtkCellArray.h>
60 #include <vtkUnsignedCharArray.h>
61
62 #include <vtkImplicitBoolean.h>
63 #include <vtkPassThroughFilter.h>
64
65 #include <vtkRenderer.h>
66
67 #include <vtkPlaneCollection.h>
68
69 #include "utilities.h"
70
71 #ifdef _DEBUG_
72 static int MYDEBUG = 0;
73 #else
74 static int MYDEBUG = 0;
75 #endif
76
77 using namespace std;
78
79
80 vtkStandardNewMacro(SMESH_DeviceActor)
81
82
83 SMESH_DeviceActor
84 ::SMESH_DeviceActor()
85 {
86   if(MYDEBUG) MESSAGE("SMESH_DeviceActor - "<<this);
87
88   myIsShrinkable = false;
89   myIsShrunk = false;
90   myIsHighlited = false;
91
92   myRepresentation = SMESH_DeviceActor::EReperesent(-1);
93
94   myProperty = vtkProperty::New();
95   myMapper = VTKViewer_PolyDataMapper::New();
96   myPlaneCollection = vtkPlaneCollection::New();
97
98   VTKViewer_Actor::GetDefaultPolygonOffsetParameters(myPolygonOffsetFactor,
99                                                      myPolygonOffsetUnits);
100
101   myMapper->UseLookupTableScalarRangeOn();
102   myMapper->SetColorModeToMapScalars();
103
104   myShrinkFilter = vtkShrinkFilter::New();
105
106   myStoreClippingMapping = false;
107
108   myExtractGeometry = SMESH_ExtractGeometry::New();
109   myExtractGeometry->SetReleaseDataFlag(true);
110   myIsImplicitFunctionUsed = false;
111
112   myExtractUnstructuredGrid = VTKViewer_ExtractUnstructuredGrid::New();
113     
114   myMergeFilter = vtkMergeFilter::New();
115
116   myGeomFilter = VTKViewer_GeometryFilter::New();
117
118   myTransformFilter = VTKViewer_TransformFilter::New();
119
120   for(int i = 0; i < 6; i++)
121     myPassFilter.push_back(vtkPassThroughFilter::New());
122
123   // Orientation of faces
124   myIsFacesOriented = false;
125
126   double anRGB[3] = { 1, 1, 1 };
127   SMESH::GetColor( "SMESH", "orientation_color", anRGB[0], anRGB[1], anRGB[2], QColor( 255, 255, 255 ) );
128
129   myFaceOrientationFilter = SMESH_FaceOrientationFilter::New();
130
131   myFaceOrientationDataMapper = vtkPolyDataMapper::New();
132   myFaceOrientationDataMapper->SetInputConnection(myFaceOrientationFilter->GetOutputPort());
133
134   myFaceOrientation = vtkActor::New();
135   myFaceOrientation->SetMapper(myFaceOrientationDataMapper);
136   myFaceOrientation->GetProperty()->SetColor(anRGB[0], anRGB[1], anRGB[2]);
137 }
138
139
140 SMESH_DeviceActor
141 ::~SMESH_DeviceActor()
142 {
143   if(MYDEBUG) MESSAGE("~SMESH_DeviceActor - "<<this);
144
145   myMapper->Delete();
146   // myPlaneCollection->Delete(); -- it is vtkSmartPointer
147   myProperty->Delete();
148
149   myExtractGeometry->Delete();
150
151   myMergeFilter->Delete();
152   myExtractUnstructuredGrid->Delete();
153
154   // Orientation of faces
155   myFaceOrientationFilter->Delete();
156   myFaceOrientationDataMapper->RemoveAllInputs();
157   myFaceOrientationDataMapper->Delete();
158   myFaceOrientation->Delete();
159
160   myGeomFilter->Delete();
161
162   myTransformFilter->Delete();
163
164   for(size_t i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
165     myPassFilter[i]->Delete();
166
167   myShrinkFilter->Delete();
168 }
169
170
171 void
172 SMESH_DeviceActor
173 ::SetStoreGemetryMapping(bool theStoreMapping)
174 {
175   myGeomFilter->SetStoreMapping(theStoreMapping);
176   // for optimization, switch the mapping explicitly in each filter/algorithm
177   //SetStoreClippingMapping(theStoreMapping);
178 }
179
180
181 void
182 SMESH_DeviceActor
183 ::SetStoreClippingMapping(bool theStoreMapping)
184 {
185   myStoreClippingMapping = theStoreMapping;
186   myExtractGeometry->SetStoreMapping(theStoreMapping && myIsImplicitFunctionUsed);
187   // EAP, 23315
188   // Mapping in myExtractUnstructuredGrid and myGeomFilter is ON in the pickable DeviceActor only.
189   // To show labels, the mapping is computed explicitly via myExtractUnstructuredGrid->BuildOut2InMap();
190   //SetStoreIDMapping(theStoreMapping);
191 }
192
193
194 void
195 SMESH_DeviceActor
196 ::SetStoreIDMapping(bool theStoreMapping)
197 {
198   myExtractUnstructuredGrid->SetStoreMapping(theStoreMapping);
199 }
200
201
202 void 
203 SMESH_DeviceActor
204 ::Init(TVisualObjPtr theVisualObj, 
205        vtkImplicitBoolean* theImplicitBoolean)
206 {
207   myVisualObj = theVisualObj;
208   myExtractGeometry->SetImplicitFunction(theImplicitBoolean);
209   SetUnstructuredGrid(myVisualObj->GetUnstructuredGrid());
210 }
211
212 void
213 SMESH_DeviceActor
214 ::SetImplicitFunctionUsed(bool theIsImplicitFunctionUsed)
215 {
216   int anId = 0;
217   if(theIsImplicitFunctionUsed)
218     myPassFilter[ anId ]->SetInputConnection( myExtractGeometry->GetOutputPort() );
219   else
220     myPassFilter[ anId ]->SetInputConnection( myMergeFilter->GetOutputPort() );
221     
222   myIsImplicitFunctionUsed = theIsImplicitFunctionUsed;
223   SetStoreClippingMapping(myStoreClippingMapping);
224 }
225
226
227 void
228 SMESH_DeviceActor
229 ::SetUnstructuredGrid(vtkUnstructuredGrid* theGrid)
230 {
231   myExtractUnstructuredGrid->SetInputData(theGrid);
232
233   if ( theGrid )
234   {
235     myIsShrinkable = true;
236
237     myMergeFilter->SetGeometryConnection(myExtractUnstructuredGrid->GetOutputPort());
238
239     //Pass diameters of the balls
240     if(myMapper->GetBallEnabled()) {
241       myMergeFilter->SetScalarsConnection(myExtractUnstructuredGrid->GetOutputPort());
242     }
243
244     myExtractGeometry->SetInputConnection(myMergeFilter->GetOutputPort());
245
246     int anId = 0;
247     SetImplicitFunctionUsed(myIsImplicitFunctionUsed);
248     myPassFilter[ anId + 1]->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
249
250     anId++; // 1
251     myTransformFilter->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
252
253     anId++; // 2
254     myPassFilter[ anId ]->SetInputConnection( myTransformFilter->GetOutputPort() );
255     myPassFilter[ anId + 1 ]->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
256
257     anId++; // 3
258     myGeomFilter->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
259
260     anId++; // 4
261     myPassFilter[ anId ]->SetInputConnection( myGeomFilter->GetOutputPort() );
262     myPassFilter[ anId + 1 ]->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
263
264     anId++; // 5
265     myMapper->SetInputConnection( myPassFilter[ anId ]->GetOutputPort() );
266     if( myPlaneCollection->GetNumberOfItems() )
267       myMapper->SetClippingPlanes( myPlaneCollection );
268
269     vtkLODActor::SetMapper( myMapper );
270   }
271   Modified();
272 }
273
274 void
275 SMESH_DeviceActor
276 ::SetPlaneCollection( vtkPlaneCollection* theCollection )
277 {
278   myPlaneCollection = theCollection;
279 }
280
281 VTKViewer_ExtractUnstructuredGrid* 
282 SMESH_DeviceActor
283 ::GetExtractUnstructuredGrid()
284 {
285   return myExtractUnstructuredGrid;
286 }
287
288 #include "SMDS_Mesh.hxx"
289
290 vtkUnstructuredGrid* 
291 SMESH_DeviceActor
292 ::GetUnstructuredGrid()
293 {
294   myExtractUnstructuredGrid->Update();
295   return myExtractUnstructuredGrid->GetOutput();
296 }
297
298
299 void
300 SMESH_DeviceActor
301 ::SetControlMode(SMESH::Controls::FunctorPtr theFunctor,
302                  SMESH_ScalarBarActor* theScalarBarActor,
303                  vtkLookupTable* theLookupTable)
304 {
305   bool anIsInitialized = theFunctor != NULL;
306   if(anIsInitialized){
307     vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
308
309     // SetStoreIDMapping(true);
310     myExtractUnstructuredGrid->Update();
311     vtkUnstructuredGrid* aGrid = myExtractUnstructuredGrid->GetOutput();
312
313     aDataSet->ShallowCopy(aGrid);
314     
315     vtkDoubleArray *aScalars = vtkDoubleArray::New();
316     vtkIdType aNbCells = aGrid->GetNumberOfCells();
317     aScalars->SetNumberOfComponents(1);
318     aScalars->SetNumberOfTuples(aNbCells);
319     double* range = 0;// = aScalars->GetRange();
320     
321     myVisualObj->UpdateFunctor(theFunctor);
322
323     using namespace SMESH::Controls;
324     if(NumericalFunctor* aNumericalFunctor = dynamic_cast<NumericalFunctor*>(theFunctor.get()))
325     {
326       myExtractUnstructuredGrid->BuildOut2InMap();
327       for(vtkIdType i = 0; i < aNbCells; i++)
328       {
329         vtkIdType anId = myExtractUnstructuredGrid->GetInputId(i);
330         vtkIdType anObjId = myVisualObj->GetElemObjId(anId);
331         double aValue = aNumericalFunctor->GetValue(anObjId);
332         aScalars->SetValue(i,aValue);
333       }
334       range = aScalars->GetRange();
335       if ( range[1] - range[0] < ( qMax(qAbs(range[0]),qAbs(range[1])) + 1e-100 ) * 1e-6 )
336       {
337         range[1] = range[0];
338         for(vtkIdType i = 0; i < aNbCells; i++)
339           aScalars->SetValue(i,range[0]);
340       }
341     }
342     else if(Predicate* aPredicate = dynamic_cast<Predicate*>(theFunctor.get()))
343     {
344       myExtractUnstructuredGrid->BuildOut2InMap();
345       for(vtkIdType i = 0; i < aNbCells; i++)
346       {
347         vtkIdType anId = myExtractUnstructuredGrid->GetInputId(i);
348         vtkIdType anObjId = myVisualObj->GetElemObjId(anId);
349         bool aValue = aPredicate->IsSatisfy(anObjId);
350         aScalars->SetValue(i,aValue);
351       }
352       range = aScalars->GetRange();
353     }
354
355     aDataSet->GetCellData()->SetScalars(aScalars);
356     aScalars->Delete();
357
358     theLookupTable->SetRange( range );
359     theLookupTable->SetNumberOfTableValues(theScalarBarActor->GetMaximumNumberOfColors());
360     theLookupTable->Build();
361     
362     myMergeFilter->SetScalarsData(aDataSet);
363     aDataSet->Delete();
364   }
365   GetMapper()->SetScalarVisibility(anIsInitialized);
366   theScalarBarActor->SetVisibility(anIsInitialized);
367 }
368
369 void
370 SMESH_DeviceActor
371 ::SetExtControlMode(SMESH::Controls::FunctorPtr theFunctor,
372                     SMESH_ScalarBarActor* theScalarBarActor,
373                     vtkLookupTable* theLookupTable)
374 {
375   bool anIsInitialized = theFunctor != NULL;
376   myExtractUnstructuredGrid->ClearRegisteredCells();
377   myExtractUnstructuredGrid->ClearRegisteredCellsWithType();
378   myExtractUnstructuredGrid->SetModeOfChanging(VTKViewer_ExtractUnstructuredGrid::ePassAll);
379   myVisualObj->UpdateFunctor(theFunctor);
380
381   using namespace SMESH::Controls;
382   if (anIsInitialized){
383     if (Length2D* aLength2D = dynamic_cast<Length2D*>(theFunctor.get())){
384       SMESH::Controls::Length2D::TValues aValues;
385
386       aLength2D->GetValues(aValues);
387       vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
388       vtkUnstructuredGrid* aGrid = myVisualObj->GetUnstructuredGrid();
389
390       aDataSet->SetPoints(aGrid->GetPoints());
391       
392       vtkIdType aNbCells = aValues.size();
393       
394       vtkDoubleArray *aScalars = vtkDoubleArray::New();
395       aScalars->SetNumberOfComponents(1);
396       aScalars->SetNumberOfTuples(aNbCells);
397
398       vtkIdType aCellsSize = 3*aNbCells;
399       vtkCellArray* aConnectivity = vtkCellArray::New();
400       aConnectivity->Allocate( aCellsSize, 0 );
401       
402       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
403       aCellTypesArray->SetNumberOfComponents( 1 );
404       aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
405
406       vtkIdList *anIdList = vtkIdList::New();
407       anIdList->SetNumberOfIds(2);
408
409       Length2D::TValues::const_iterator anIter = aValues.begin();
410       aNbCells = 0;
411       for(; anIter != aValues.end(); anIter++){
412         const Length2D::Value& aValue = *anIter;
413         vtkIdType aNode[2] = {
414           myVisualObj->GetNodeVTKId(aValue.myPntId[0]),
415           myVisualObj->GetNodeVTKId(aValue.myPntId[1])
416         };
417         if(aNode[0] >= 0 && aNode[1] >= 0){
418           anIdList->SetId( 0, aNode[0] );
419           anIdList->SetId( 1, aNode[1] );
420           aConnectivity->InsertNextCell( anIdList );
421           aCellTypesArray->InsertNextValue( VTK_LINE );
422           aScalars->SetValue(aNbCells,aValue.myLength);
423           aNbCells++;
424         }
425       }
426       aCellTypesArray->SetNumberOfTuples( aNbCells );
427       aScalars->SetNumberOfTuples( aNbCells );
428
429       vtkIdTypeArray* aCellLocationsArray = vtkIdTypeArray::New();
430       aCellLocationsArray->SetNumberOfComponents( 1 );
431       aCellLocationsArray->SetNumberOfTuples( aNbCells );
432
433       aConnectivity->InitTraversal();
434       vtkIdType const *pts(nullptr);
435       for( vtkIdType idType = 0, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
436         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
437
438       aDataSet->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
439       SetUnstructuredGrid(aDataSet);
440
441       aDataSet->GetCellData()->SetScalars(aScalars);
442       aScalars->Delete();
443
444       theLookupTable->SetRange(aScalars->GetRange());
445       theLookupTable->Build();
446
447       myMergeFilter->SetScalarsData(aDataSet);
448       aDataSet->Delete();
449     }
450     else if (MultiConnection2D* aMultiConnection2D = dynamic_cast<MultiConnection2D*>(theFunctor.get())){
451       SMESH::Controls::MultiConnection2D::MValues aValues;
452
453       aMultiConnection2D->GetValues(aValues);
454       vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
455       vtkUnstructuredGrid* aGrid = myVisualObj->GetUnstructuredGrid();
456       aDataSet->SetPoints(aGrid->GetPoints());
457       
458       vtkIdType aNbCells = aValues.size();
459       vtkDoubleArray *aScalars = vtkDoubleArray::New();
460       aScalars->SetNumberOfComponents(1);
461       aScalars->SetNumberOfTuples(aNbCells);
462
463       vtkIdType aCellsSize = 3*aNbCells;
464       vtkCellArray* aConnectivity = vtkCellArray::New();
465       aConnectivity->Allocate( aCellsSize, 0 );
466
467       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
468       aCellTypesArray->SetNumberOfComponents( 1 );
469       aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
470
471       vtkIdList *anIdList = vtkIdList::New();
472       anIdList->SetNumberOfIds(2);
473
474       MultiConnection2D::MValues::const_iterator anIter = aValues.begin();
475       aNbCells = 0;
476       for(; anIter != aValues.end(); anIter++){
477         const MultiConnection2D::Value& aValue = (*anIter).first;
478         vtkIdType aNode[2] = {
479           myVisualObj->GetNodeVTKId(aValue.myPntId[0]),
480           myVisualObj->GetNodeVTKId(aValue.myPntId[1])
481         };
482         if(aNode[0] >= 0 && aNode[1] >= 0){
483           anIdList->SetId( 0, aNode[0] );
484           anIdList->SetId( 1, aNode[1] );
485           aConnectivity->InsertNextCell( anIdList );
486           aCellTypesArray->InsertNextValue( VTK_LINE );
487           aScalars->SetValue( aNbCells,(*anIter).second);
488           aNbCells++;
489         }
490       }
491       aCellTypesArray->SetNumberOfTuples( aNbCells );
492       aScalars->SetNumberOfTuples( aNbCells );
493
494       vtkIdTypeArray* aCellLocationsArray = vtkIdTypeArray::New();
495       aCellLocationsArray->SetNumberOfComponents( 1 );
496       aCellLocationsArray->SetNumberOfTuples( aNbCells );
497
498       aConnectivity->InitTraversal();
499       vtkIdType const *pts(nullptr);
500       for( vtkIdType idType = 0, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
501         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
502
503       aDataSet->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
504       SetUnstructuredGrid(aDataSet);
505
506       aDataSet->GetCellData()->SetScalars(aScalars);
507       aScalars->Delete();
508
509       theLookupTable->SetRange(aScalars->GetRange());
510       theLookupTable->Build();
511
512       myMergeFilter->SetScalarsData(aDataSet);
513       aDataSet->Delete();
514     }
515   }
516   GetMapper()->SetScalarVisibility(anIsInitialized);
517   theScalarBarActor->SetVisibility(anIsInitialized);
518 }
519
520 void
521 SMESH_DeviceActor
522 ::SetExtControlMode(SMESH::Controls::FunctorPtr theFunctor)
523 {
524   myExtractUnstructuredGrid->ClearRegisteredCells();
525   myExtractUnstructuredGrid->ClearRegisteredCellsWithType();
526   myExtractUnstructuredGrid->SetModeOfChanging(VTKViewer_ExtractUnstructuredGrid::ePassAll);
527   myVisualObj->UpdateFunctor(theFunctor);
528
529   using namespace SMESH::Controls;
530   Predicate* aPredicate = 0;
531   if (( aPredicate =  dynamic_cast<FreeBorders          *>(theFunctor.get())) ||
532       ( aPredicate =  dynamic_cast<FreeFaces            *>(theFunctor.get())) ||
533       ( aPredicate =  dynamic_cast<BareBorderVolume     *>(theFunctor.get())) ||
534       ( aPredicate =  dynamic_cast<BareBorderFace       *>(theFunctor.get())) ||
535       ( aPredicate =  dynamic_cast<OverConstrainedVolume*>(theFunctor.get())) ||
536       ( aPredicate =  dynamic_cast<CoincidentElements1D *>(theFunctor.get())) ||
537       ( aPredicate =  dynamic_cast<CoincidentElements2D *>(theFunctor.get())) ||
538       ( aPredicate =  dynamic_cast<CoincidentElements3D *>(theFunctor.get())) ||
539       ( aPredicate =  dynamic_cast<OverConstrainedFace  *>(theFunctor.get())))
540   {
541     myExtractUnstructuredGrid->SetModeOfChanging(VTKViewer_ExtractUnstructuredGrid::eAdding);
542     vtkUnstructuredGrid* aGrid = myVisualObj->GetUnstructuredGrid();
543     vtkIdType aNbCells = aGrid->GetNumberOfCells();
544     for( vtkIdType i = 0; i < aNbCells; i++ ){
545       vtkIdType anObjId = myVisualObj->GetElemObjId(i);
546       if(aPredicate->IsSatisfy(anObjId))
547         myExtractUnstructuredGrid->RegisterCell(i);
548     }
549     if(!myExtractUnstructuredGrid->IsCellsRegistered())
550       myExtractUnstructuredGrid->RegisterCell(-1);
551     SetUnstructuredGrid(myVisualObj->GetUnstructuredGrid());
552   }
553   else if(FreeEdges* aFreeEdges = dynamic_cast<FreeEdges*>(theFunctor.get()))
554   {
555     SMESH::Controls::FreeEdges::TBorders aBorders;
556     aFreeEdges->GetBoreders(aBorders);
557     vtkUnstructuredGrid* aDataSet = vtkUnstructuredGrid::New();
558     vtkUnstructuredGrid* aGrid = myVisualObj->GetUnstructuredGrid();
559     aDataSet->SetPoints(aGrid->GetPoints());
560
561     vtkIdType aNbCells = aBorders.size();
562     vtkIdType aCellsSize = 3*aNbCells;
563     vtkCellArray* aConnectivity = vtkCellArray::New();
564     aConnectivity->Allocate( aCellsSize, 0 );
565     
566     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
567     aCellTypesArray->SetNumberOfComponents( 1 );
568     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
569     
570     vtkIdList *anIdList = vtkIdList::New();
571     anIdList->SetNumberOfIds(2);
572     
573     FreeEdges::TBorders::const_iterator anIter = aBorders.begin();
574     for(; anIter != aBorders.end(); anIter++){
575       const FreeEdges::Border& aBorder = *anIter;
576       vtkIdType aNode[2] = {
577         myVisualObj->GetNodeVTKId(aBorder.myPntId[0]),
578         myVisualObj->GetNodeVTKId(aBorder.myPntId[1])
579       };
580       //cout<<"aNode = "<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<endl;
581       if(aNode[0] >= 0 && aNode[1] >= 0){
582         anIdList->SetId( 0, aNode[0] );
583         anIdList->SetId( 1, aNode[1] );
584         aConnectivity->InsertNextCell( anIdList );
585         aCellTypesArray->InsertNextValue( VTK_LINE );
586       }
587     }
588     
589     vtkIdTypeArray* aCellLocationsArray = vtkIdTypeArray::New();
590     aCellLocationsArray->SetNumberOfComponents( 1 );
591     aCellLocationsArray->SetNumberOfTuples( aNbCells );
592     
593     aConnectivity->InitTraversal();
594     vtkIdType const *pts(nullptr);
595     for( vtkIdType idType = 0, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
596       aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
597     
598     aDataSet->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
599
600     SetUnstructuredGrid(aDataSet);
601     aDataSet->Delete();
602   }
603   else if (( aPredicate = dynamic_cast<FreeNodes      *>(theFunctor.get())) ||
604            ( aPredicate = dynamic_cast<CoincidentNodes*>(theFunctor.get())))
605   {
606     myExtractUnstructuredGrid->SetModeOfChanging(VTKViewer_ExtractUnstructuredGrid::eAdding);
607     vtkIdType aNbNodes = FromSmIdType<vtkIdType>(myVisualObj->GetNbEntities(SMDSAbs_Node));
608     for( vtkIdType i = 0; i < aNbNodes; i++ ){
609       vtkIdType anObjId = myVisualObj->GetNodeObjId(i);
610       if(aPredicate->IsSatisfy(anObjId))
611         myExtractUnstructuredGrid->RegisterCell(i);
612     }
613     if(!myExtractUnstructuredGrid->IsCellsRegistered())
614       myExtractUnstructuredGrid->RegisterCell(-1);
615     SetUnstructuredGrid(myVisualObj->GetUnstructuredGrid());
616   }
617 }
618
619
620
621
622 vtkMTimeType
623 SMESH_DeviceActor
624 ::GetMTime()
625 {
626   // cout << "DA " << this
627   //      << " GF " << myGeomFilter;
628   // if ( this->Property )
629   //   cout << " P " << this->Property->GetMTime();
630   // if ( this->BackfaceProperty != NULL )
631   //   cout << " BP " << BackfaceProperty->GetMTime();
632   // if ( this->Texture != NULL )
633   //   cout << " T " << this->Texture->GetMTime();
634   // cout << " U " << this->GetUserTransformMatrixMTime()
635   //      << " M " << this->MTime.GetMTime() << endl;
636
637   // cout << "DA " << this
638   //      << " GF " << myGeomFilter
639   //      << " " << this->Superclass::GetMTime()
640   //      << " " << myExtractGeometry->GetMTime()
641   //      << " " << myExtractUnstructuredGrid->GetMTime()
642   //      << " " << myMergeFilter->GetMTime()
643   //      << " " << myGeomFilter->GetMTime()
644   //      << " " << myTransformFilter->GetMTime()
645   //      << " " << myFaceOrientationFilter->GetMTime() << endl;
646
647   vtkMTimeType mTime = this->Superclass::GetMTime();
648   mTime = max(mTime,myExtractGeometry->GetMTime());
649   mTime = max(mTime,myExtractUnstructuredGrid->GetMTime());
650   mTime = max(mTime,myMergeFilter->GetMTime());
651   mTime = max(mTime,myGeomFilter->GetMTime());
652   mTime = max(mTime,myTransformFilter->GetMTime());
653   mTime = max(mTime,myFaceOrientationFilter->GetMTime());
654   return mTime;
655 }
656
657
658 void
659 SMESH_DeviceActor
660 ::SetTransform(VTKViewer_Transform* theTransform)
661 {
662   myTransformFilter->SetTransform(theTransform);
663 }
664
665
666 void
667 SMESH_DeviceActor
668 ::SetShrink() 
669 {
670   if ( !myIsShrinkable ) return;
671   if ( vtkAlgorithmOutput* aDataSet = myPassFilter[ 0 ]->GetOutputPort() )
672   {
673     myShrinkFilter->SetInputConnection( aDataSet );
674     myPassFilter[ 1 ]->SetInputConnection( myShrinkFilter->GetOutputPort() );
675     myIsShrunk = true;
676   }
677 }
678
679 void
680 SMESH_DeviceActor
681 ::UnShrink() 
682 {
683   if ( !myIsShrunk ) return;
684   if ( vtkAlgorithmOutput* aDataSet = myPassFilter[ 0 ]->GetOutputPort() )
685   {    
686     myPassFilter[ 1 ]->SetInputConnection( aDataSet );
687     myPassFilter[ 1 ]->Modified();
688     myIsShrunk = false;
689     Modified();
690   }
691 }
692
693
694 void
695 SMESH_DeviceActor
696 ::SetFacesOriented(bool theIsFacesOriented) 
697 {
698   if ( vtkAlgorithmOutput* aDataSet = myTransformFilter->GetOutputPort() )
699   {
700     myIsFacesOriented = theIsFacesOriented;
701     if( theIsFacesOriented )
702       myFaceOrientationFilter->SetInputConnection( aDataSet );
703     UpdateFaceOrientation();
704   }
705 }
706
707 void
708 SMESH_DeviceActor
709 ::SetFacesOrientationColor(double r,double g,double b)
710 {
711   myFaceOrientation->GetProperty()->SetColor( r, g, b );
712 }
713
714 void
715 SMESH_DeviceActor
716 ::GetFacesOrientationColor(double& r,double& g,double& b)
717 {
718   myFaceOrientation->GetProperty()->GetColor( r, g, b );
719 }
720
721 void
722 SMESH_DeviceActor
723 ::SetFacesOrientationScale(double theScale)
724 {
725   myFaceOrientationFilter->SetOrientationScale( theScale );
726 }
727
728 double
729 SMESH_DeviceActor
730 ::GetFacesOrientationScale()
731 {
732   return myFaceOrientationFilter->GetOrientationScale();
733 }
734
735 void
736 SMESH_DeviceActor
737 ::SetFacesOrientation3DVectors(bool theState)
738 {
739   myFaceOrientationFilter->Set3dVectors( theState );
740 }
741
742 bool
743 SMESH_DeviceActor
744 ::GetFacesOrientation3DVectors()
745 {
746   return myFaceOrientationFilter->Get3dVectors();
747 }
748
749 void
750 SMESH_DeviceActor
751 ::UpdateFaceOrientation()
752 {
753   bool aShowFaceOrientation = myIsFacesOriented;
754   aShowFaceOrientation &= vtkLODActor::GetVisibility(); //GetVisibility(); -- avoid calling GetUnstructuredGrid()  
755   aShowFaceOrientation &= ( myRepresentation != ePoint );
756   myFaceOrientation->SetVisibility(aShowFaceOrientation);
757 }
758
759
760 void
761 SMESH_DeviceActor
762 ::SetRepresentation(EReperesent theMode)
763 {
764   if ( myRepresentation == theMode )
765     return;
766   switch(theMode){
767   case ePoint:
768     myGeomFilter->SetInside(true);
769     myGeomFilter->SetWireframeMode(false);
770     GetProperty()->SetRepresentation(0);
771     break;
772   case eWireframe:
773     myGeomFilter->SetInside(false);
774     myGeomFilter->SetWireframeMode(true);
775     GetProperty()->SetRepresentation(theMode);
776     break;
777   case eInsideframe:
778     myGeomFilter->SetInside(true);
779     myGeomFilter->SetWireframeMode(true);
780     GetProperty()->SetRepresentation(1);
781     break;
782   case eSurface:
783     myGeomFilter->SetInside(false);
784     myGeomFilter->SetWireframeMode(false);
785     GetProperty()->SetRepresentation(theMode);
786   case eNoneRepr:
787     return;
788   }
789   SetMarkerEnabled(theMode == ePoint);
790   myRepresentation = theMode;
791   UpdateFaceOrientation();
792   GetProperty()->Modified();
793   myMapper->Modified();
794   Modified();
795 }
796
797
798 void
799 SMESH_DeviceActor
800 ::SetVisibility(int theMode)
801 {
802   if(( theMode ) &&
803      ( !myExtractUnstructuredGrid->GetInput() || 
804        GetUnstructuredGrid()->GetNumberOfCells()))
805   {
806     vtkLODActor::SetVisibility(theMode);
807   }else{
808     vtkLODActor::SetVisibility(false);
809   }
810   UpdateFaceOrientation();
811 }
812
813
814 int
815 SMESH_DeviceActor
816 ::GetVisibility()
817 {
818   int visibi = vtkLODActor::GetVisibility();
819   if(visibi && !GetUnstructuredGrid()->GetNumberOfCells()){
820     vtkLODActor::SetVisibility(false);
821     visibi = 0;
822   }
823   return visibi;
824 }
825
826
827 void
828 SMESH_DeviceActor
829 ::AddToRender(vtkRenderer* theRenderer)
830 {
831   theRenderer->AddActor(this);
832   theRenderer->AddActor(myFaceOrientation);
833 }
834
835 void
836 SMESH_DeviceActor
837 ::RemoveFromRender(vtkRenderer* theRenderer)
838 {
839   theRenderer->RemoveActor(this);
840   theRenderer->RemoveActor(myFaceOrientation);
841 }
842
843
844 vtkIdType
845 SMESH_DeviceActor
846 ::GetNodeObjId(vtkIdType theVtkID)
847 {
848   vtkIdType anID = theVtkID;
849
850   if(IsImplicitFunctionUsed())
851     anID = myExtractGeometry->GetNodeObjId(theVtkID);
852
853   vtkIdType aRetID = myVisualObj->GetNodeObjId(anID);
854   if(MYDEBUG) MESSAGE("GetNodeObjId - theVtkID = "<<theVtkID<<"; anID = "<<anID<<"; aRetID = "<<aRetID);
855   return aRetID;
856 }
857
858 double* 
859 SMESH_DeviceActor
860 ::GetNodeCoord(vtkIdType theObjID)
861 {
862   vtkDataSet* aDataSet = myMergeFilter->GetOutput();
863   vtkIdType anID = myVisualObj->GetNodeVTKId(theObjID);
864   double* aCoord = (anID >=0 && anID < aDataSet->GetNumberOfPoints()) ? aDataSet->GetPoint(anID) : NULL;
865   if(MYDEBUG) MESSAGE("GetNodeCoord - theObjID = "<<theObjID<<"; anID = "<<anID);
866   return aCoord;
867 }
868
869 vtkIdType
870 SMESH_DeviceActor
871 ::GetNodeVtkId(vtkIdType theObjID) 
872 {
873   return myVisualObj->GetNodeVTKId(theObjID);
874 }
875
876 vtkIdType
877 SMESH_DeviceActor
878 ::GetElemObjId(vtkIdType theVtkID)
879 {
880   vtkIdType anId = myGeomFilter->GetElemObjId(theVtkID);
881   if(anId < 0) 
882     return -1;
883
884   vtkIdType anId2 = anId;
885   if(IsImplicitFunctionUsed())
886     anId2 = myExtractGeometry->GetElemObjId(anId);
887   if(anId2 < 0) 
888     return -1;
889
890   vtkIdType anId3 = myExtractUnstructuredGrid->GetInputId(anId2);
891   if(anId3 < 0) 
892     return -1;
893
894   vtkIdType aRetID = myVisualObj->GetElemObjId(anId3);
895   if(MYDEBUG) 
896      MESSAGE("GetElemObjId - theVtkID = "<<theVtkID<<"; anId2 = "<<anId2<<"; anId3 = "<<anId3<<"; aRetID = "<<aRetID);
897   return aRetID;
898 }
899
900 vtkCell* 
901 SMESH_DeviceActor
902 ::GetElemCell(vtkIdType theObjID)
903 {
904   vtkDataSet* aDataSet = myVisualObj->GetUnstructuredGrid();
905   vtkIdType aGridID = myVisualObj->GetElemVTKId(theObjID);
906   vtkCell* aCell = (aGridID >= 0 ) ? aDataSet->GetCell(aGridID) : NULL;
907   if(MYDEBUG) 
908     MESSAGE("GetElemCell - theObjID = "<<theObjID<<"; aGridID = "<<aGridID);
909   return aCell;
910 }
911
912
913 double 
914 SMESH_DeviceActor
915 ::GetShrinkFactor()
916 {
917   return myShrinkFilter->GetShrinkFactor();
918 }
919
920 void
921 SMESH_DeviceActor
922 ::SetShrinkFactor(double theValue)
923 {
924   theValue = theValue > 0.1? theValue: 0.8;
925   myShrinkFilter->SetShrinkFactor(theValue);
926   Modified();
927 }
928
929
930 void
931 SMESH_DeviceActor
932 ::SetHighlited(bool theIsHighlited)
933 {
934   if ( myIsHighlited == theIsHighlited )
935     return;
936   myIsHighlited = theIsHighlited;
937   Modified();
938 }
939
940 void
941 SMESH_DeviceActor
942 ::Render(vtkRenderer *ren, vtkMapper* m)
943 {
944   int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
945   double aStoredFactor, aStoredUnit; 
946   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
947
948   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
949   double aFactor = myPolygonOffsetFactor, aUnits = myPolygonOffsetUnits;
950   if(myIsHighlited){
951     static double EPS = .01;
952     aUnits *= (1.0-EPS);
953   }
954   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnits);
955   vtkLODActor::Render(ren,m);
956
957   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
958   vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
959 }
960
961
962 void
963 SMESH_DeviceActor
964 ::SetPolygonOffsetParameters(double factor, 
965                              double units)
966 {
967   myPolygonOffsetFactor = factor;
968   myPolygonOffsetUnits = units;
969 }
970
971 /*!
972  * On/Off representation 2D quadratic element as arked polygon
973  */
974 void SMESH_DeviceActor::SetQuadraticArcMode(bool theFlag){
975   myGeomFilter->SetQuadraticArcMode(theFlag);
976 }
977
978 /*!
979  * Return true if 2D quadratic element displayed as arked polygon
980  */
981 bool SMESH_DeviceActor::GetQuadraticArcMode(){
982   return myGeomFilter->GetQuadraticArcMode();
983 }
984 /*!
985  * Set Max angle for representation 2D quadratic element as arked polygon
986  */
987 void SMESH_DeviceActor::SetQuadraticArcAngle(double theMaxAngle){
988   myGeomFilter->SetQuadraticArcAngle(theMaxAngle);
989 }
990
991 /*!
992  * Return Max angle of the representation 2D quadratic element as arked polygon
993  */
994 double SMESH_DeviceActor::GetQuadraticArcAngle(){
995   return myGeomFilter->GetQuadraticArcAngle();
996 }
997
998 /*!
999  * Set point marker enabled
1000  * \param theMarkerEnabled flag to enable/disable point marker
1001  */
1002 void SMESH_DeviceActor::SetMarkerEnabled( bool theMarkerEnabled )
1003 {
1004   myMapper->SetMarkerEnabled( theMarkerEnabled );
1005 }
1006
1007 /*!
1008  * Set point marker enabled
1009  * \param theBallEnabled flag to enable/disable ball drawing
1010  */
1011 void SMESH_DeviceActor::SetBallEnabled( bool theBallEnabled ) {
1012   myMapper->SetBallEnabled( theBallEnabled );
1013 }
1014
1015 /*!
1016  * Set point marker scale factor
1017  * \param theBallScale double value which specifies a scale factor of ball element
1018  */
1019 void SMESH_DeviceActor::SetBallScale( double theBallScale )
1020 {
1021   myMapper->SetBallScale( theBallScale );
1022   myMapper->Modified();
1023 }
1024
1025 /*!
1026  * Set standard point marker
1027  * \param theMarkerType type of the marker
1028  */
1029 void SMESH_DeviceActor::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
1030 {
1031   myMapper->SetMarkerStd( theMarkerType, theMarkerScale );
1032 }
1033
1034 /*!
1035  * Set custom point marker
1036  * \param theMarkerId id of the marker texture
1037  * \param theMarkerTexture marker texture
1038  */
1039 void SMESH_DeviceActor::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
1040 {
1041   myMapper->SetMarkerTexture( theMarkerId, theMarkerTexture );
1042 }
1043
1044 /*!
1045  * Get type of the point marker
1046  * \return type of the point marker
1047  */
1048 VTK::MarkerType SMESH_DeviceActor::GetMarkerType()
1049 {
1050   return myMapper->GetMarkerType();
1051 }
1052
1053 /*!
1054   Get scale of the point marker
1055   \return scale of the point marker
1056 */
1057 VTK::MarkerScale SMESH_DeviceActor::GetMarkerScale()
1058 {
1059   return myMapper->GetMarkerScale();
1060 }
1061
1062 /*!
1063  * Get texture identifier of the point marker
1064  * \return texture identifier of the point marker
1065  */
1066 int SMESH_DeviceActor::GetMarkerTexture()
1067 {
1068   return myMapper->GetMarkerTexture();
1069 }
1070
1071 /*!
1072  * Get scale factor of ball element
1073  * \return scale factor of ball element
1074  */
1075 double SMESH_DeviceActor::GetBallScale()
1076 {
1077   return myMapper->GetBallScale();
1078 }
1079
1080 void SMESH_DeviceActor::SetCoincident3DAllowed(bool theFlag) {
1081   myGeomFilter->SetAppendCoincident3D(theFlag);
1082 }
1083
1084 bool SMESH_DeviceActor::IsCoincident3DAllowed() const {
1085   return myGeomFilter->GetAppendCoincident3D();
1086 }