Salome HOME
#18963 Minimize compiler warnings
[modules/smesh.git] / src / OBJECT / SMESH_DeviceActor.cxx
1 // Copyright (C) 2007-2020  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(int 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       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::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       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::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     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::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 = 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   }
787   SetMarkerEnabled(theMode == ePoint);
788   myRepresentation = theMode;
789   UpdateFaceOrientation();
790   GetProperty()->Modified();
791   myMapper->Modified();
792   Modified();
793 }
794
795
796 void
797 SMESH_DeviceActor
798 ::SetVisibility(int theMode)
799 {
800   if(( theMode ) &&
801      ( !myExtractUnstructuredGrid->GetInput() || 
802        GetUnstructuredGrid()->GetNumberOfCells()))
803   {
804     vtkLODActor::SetVisibility(theMode);
805   }else{
806     vtkLODActor::SetVisibility(false);
807   }
808   UpdateFaceOrientation();
809 }
810
811
812 int
813 SMESH_DeviceActor
814 ::GetVisibility()
815 {
816   int visibi = vtkLODActor::GetVisibility();
817   if(visibi && !GetUnstructuredGrid()->GetNumberOfCells()){
818     vtkLODActor::SetVisibility(false);
819     visibi = 0;
820   }
821   return visibi;
822 }
823
824
825 void
826 SMESH_DeviceActor
827 ::AddToRender(vtkRenderer* theRenderer)
828 {
829   theRenderer->AddActor(this);
830   theRenderer->AddActor(myFaceOrientation);
831 }
832
833 void
834 SMESH_DeviceActor
835 ::RemoveFromRender(vtkRenderer* theRenderer)
836 {
837   theRenderer->RemoveActor(this);
838   theRenderer->RemoveActor(myFaceOrientation);
839 }
840
841
842 int
843 SMESH_DeviceActor
844 ::GetNodeObjId(int theVtkID)
845 {
846   vtkIdType anID = theVtkID;
847
848   if(IsImplicitFunctionUsed())
849     anID = myExtractGeometry->GetNodeObjId(theVtkID);
850
851   vtkIdType aRetID = myVisualObj->GetNodeObjId(anID);
852   if(MYDEBUG) MESSAGE("GetNodeObjId - theVtkID = "<<theVtkID<<"; anID = "<<anID<<"; aRetID = "<<aRetID);
853   return aRetID;
854 }
855
856 double* 
857 SMESH_DeviceActor
858 ::GetNodeCoord(int theObjID)
859 {
860   vtkDataSet* aDataSet = myMergeFilter->GetOutput();
861   vtkIdType anID = myVisualObj->GetNodeVTKId(theObjID);
862   double* aCoord = (anID >=0 && anID < aDataSet->GetNumberOfPoints()) ? aDataSet->GetPoint(anID) : NULL;
863   if(MYDEBUG) MESSAGE("GetNodeCoord - theObjID = "<<theObjID<<"; anID = "<<anID);
864   return aCoord;
865 }
866
867 int
868 SMESH_DeviceActor
869 ::GetNodeVtkId(int theObjID) 
870 {
871   return myVisualObj->GetNodeVTKId(theObjID);
872 }
873
874 int
875 SMESH_DeviceActor
876 ::GetElemObjId(int theVtkID)
877 {
878   vtkIdType anId = myGeomFilter->GetElemObjId(theVtkID);
879   if(anId < 0) 
880     return -1;
881
882   vtkIdType anId2 = anId;
883   if(IsImplicitFunctionUsed())
884     anId2 = myExtractGeometry->GetElemObjId(anId);
885   if(anId2 < 0) 
886     return -1;
887
888   vtkIdType anId3 = myExtractUnstructuredGrid->GetInputId(anId2);
889   if(anId3 < 0) 
890     return -1;
891
892   vtkIdType aRetID = myVisualObj->GetElemObjId(anId3);
893   if(MYDEBUG) 
894      MESSAGE("GetElemObjId - theVtkID = "<<theVtkID<<"; anId2 = "<<anId2<<"; anId3 = "<<anId3<<"; aRetID = "<<aRetID);
895   return aRetID;
896 }
897
898 vtkCell* 
899 SMESH_DeviceActor
900 ::GetElemCell(int theObjID)
901 {
902   vtkDataSet* aDataSet = myVisualObj->GetUnstructuredGrid();
903   vtkIdType aGridID = myVisualObj->GetElemVTKId(theObjID);
904   vtkCell* aCell = (aGridID >= 0 ) ? aDataSet->GetCell(aGridID) : NULL;
905   if(MYDEBUG) 
906     MESSAGE("GetElemCell - theObjID = "<<theObjID<<"; aGridID = "<<aGridID);
907   return aCell;
908 }
909
910
911 double 
912 SMESH_DeviceActor
913 ::GetShrinkFactor()
914 {
915   return myShrinkFilter->GetShrinkFactor();
916 }
917
918 void
919 SMESH_DeviceActor
920 ::SetShrinkFactor(double theValue)
921 {
922   theValue = theValue > 0.1? theValue: 0.8;
923   myShrinkFilter->SetShrinkFactor(theValue);
924   Modified();
925 }
926
927
928 void
929 SMESH_DeviceActor
930 ::SetHighlited(bool theIsHighlited)
931 {
932   if ( myIsHighlited == theIsHighlited )
933     return;
934   myIsHighlited = theIsHighlited;
935   Modified();
936 }
937
938 void
939 SMESH_DeviceActor
940 ::Render(vtkRenderer *ren, vtkMapper* m)
941 {
942   int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
943   double aStoredFactor, aStoredUnit; 
944   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
945
946   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
947   double aFactor = myPolygonOffsetFactor, aUnits = myPolygonOffsetUnits;
948   if(myIsHighlited){
949     static double EPS = .01;
950     aUnits *= (1.0-EPS);
951   }
952   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnits);
953   vtkLODActor::Render(ren,m);
954
955   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aStoredFactor,aStoredUnit);
956   vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
957 }
958
959
960 void
961 SMESH_DeviceActor
962 ::SetPolygonOffsetParameters(double factor, 
963                              double units)
964 {
965   myPolygonOffsetFactor = factor;
966   myPolygonOffsetUnits = units;
967 }
968
969 /*!
970  * On/Off representation 2D quadratic element as arked polygon
971  */
972 void SMESH_DeviceActor::SetQuadraticArcMode(bool theFlag){
973   myGeomFilter->SetQuadraticArcMode(theFlag);
974 }
975
976 /*!
977  * Return true if 2D quadratic element displayed as arked polygon
978  */
979 bool SMESH_DeviceActor::GetQuadraticArcMode(){
980   return myGeomFilter->GetQuadraticArcMode();
981 }
982 /*!
983  * Set Max angle for representation 2D quadratic element as arked polygon
984  */
985 void SMESH_DeviceActor::SetQuadraticArcAngle(double theMaxAngle){
986   myGeomFilter->SetQuadraticArcAngle(theMaxAngle);
987 }
988
989 /*!
990  * Return Max angle of the representation 2D quadratic element as arked polygon
991  */
992 double SMESH_DeviceActor::GetQuadraticArcAngle(){
993   return myGeomFilter->GetQuadraticArcAngle();
994 }
995
996 /*!
997  * Set point marker enabled
998  * \param theMarkerEnabled flag to enable/disable point marker
999  */
1000 void SMESH_DeviceActor::SetMarkerEnabled( bool theMarkerEnabled )
1001 {
1002   myMapper->SetMarkerEnabled( theMarkerEnabled );
1003 }
1004
1005 /*!
1006  * Set point marker enabled
1007  * \param theBallEnabled flag to enable/disable ball drawing
1008  */
1009 void SMESH_DeviceActor::SetBallEnabled( bool theBallEnabled ) {
1010   myMapper->SetBallEnabled( theBallEnabled );
1011 }
1012
1013 /*!
1014  * Set point marker scale factor
1015  * \param theBallScale double value which specifies a scale factor of ball element
1016  */
1017 void SMESH_DeviceActor::SetBallScale( double theBallScale )
1018 {
1019   myMapper->SetBallScale( theBallScale );
1020   myMapper->Modified();
1021 }
1022
1023 /*!
1024  * Set standard point marker
1025  * \param theMarkerType type of the marker
1026  */
1027 void SMESH_DeviceActor::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
1028 {
1029   myMapper->SetMarkerStd( theMarkerType, theMarkerScale );
1030 }
1031
1032 /*!
1033  * Set custom point marker
1034  * \param theMarkerId id of the marker texture
1035  * \param theMarkerTexture marker texture
1036  */
1037 void SMESH_DeviceActor::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
1038 {
1039   myMapper->SetMarkerTexture( theMarkerId, theMarkerTexture );
1040 }
1041
1042 /*!
1043  * Get type of the point marker
1044  * \return type of the point marker
1045  */
1046 VTK::MarkerType SMESH_DeviceActor::GetMarkerType()
1047 {
1048   return myMapper->GetMarkerType();
1049 }
1050
1051 /*!
1052   Get scale of the point marker
1053   \return scale of the point marker
1054 */
1055 VTK::MarkerScale SMESH_DeviceActor::GetMarkerScale()
1056 {
1057   return myMapper->GetMarkerScale();
1058 }
1059
1060 /*!
1061  * Get texture identifier of the point marker
1062  * \return texture identifier of the point marker
1063  */
1064 int SMESH_DeviceActor::GetMarkerTexture()
1065 {
1066   return myMapper->GetMarkerTexture();
1067 }
1068
1069 /*!
1070  * Get scale factor of ball element
1071  * \return scale factor of ball element
1072  */
1073 double SMESH_DeviceActor::GetBallScale()
1074 {
1075   return myMapper->GetBallScale();
1076 }
1077
1078 void SMESH_DeviceActor::SetCoincident3DAllowed(bool theFlag) {
1079   myGeomFilter->SetAppendCoincident3D(theFlag);
1080 }
1081
1082 bool SMESH_DeviceActor::IsCoincident3DAllowed() const {
1083   return myGeomFilter->GetAppendCoincident3D();
1084 }