Salome HOME
53225: Viscous Layers hyp: problem with selection of input/output faces
[modules/smesh.git] / src / OBJECT / SMESH_Object.cxx
1 // Copyright (C) 2007-2016  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_Grid.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //
28 #include "SMESH_ObjectDef.h"
29 #include "SMESH_ActorUtils.h"
30
31 #include "SMDS_BallElement.hxx"
32 #include "SMDS_Mesh.hxx"
33 #include "SMDS_MeshCell.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "SMESH_Actor.h"
36 #include "SMESH_ControlsDef.hxx"
37
38 #include <SalomeApp_Application.h>
39 #include <VTKViewer_ExtractUnstructuredGrid.h>
40 #include <VTKViewer_CellLocationsArray.h>
41
42 #include CORBA_SERVER_HEADER(SMESH_Gen)
43 #include CORBA_SERVER_HEADER(SALOME_Exception)
44
45 #include <vtkCell.h>
46 #include <vtkIdList.h>
47 #include <vtkCellArray.h>
48 #include <vtkUnsignedCharArray.h>
49 #include <vtkCellData.h>
50 #include <vtkUnstructuredGrid.h>
51
52 #include <memory>
53 #include <sstream>      
54 #include <stdexcept>
55 #include <set>
56
57 #include "utilities.h"
58
59 using namespace std;
60
61 #ifndef EXCEPTION
62 #define EXCEPTION(TYPE, MSG) {\
63   std::ostringstream aStream;\
64   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
65   throw TYPE(aStream.str());\
66 }
67 #endif
68
69 #ifdef _DEBUG_
70 static int MYDEBUG = 0;
71 static int MYDEBUGWITHFILES = 0;//1;
72 #else
73 static int MYDEBUG = 0;
74 static int MYDEBUGWITHFILES = 0;
75 #endif
76
77
78 /*
79   Class       : SMESH_VisualObjDef
80   Description : Base class for all mesh objects to be visuilised
81 */
82
83 //=================================================================================
84 // function : getCellType
85 // purpose  : Get type of VTK cell
86 //=================================================================================
87 // static inline vtkIdType getCellType( const SMDSAbs_ElementType theType,
88 //                                      const bool                thePoly,
89 //                                      const int                 theNbNodes )
90 // {
91 //   switch( theType )
92 //   {
93 //     case SMDSAbs_0DElement:         return VTK_VERTEX;
94
95 //     case SMDSAbs_Ball:              return VTK_POLY_VERTEX;
96
97 //     case SMDSAbs_Edge: 
98 //       if( theNbNodes == 2 )         return VTK_LINE;
99 //       else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
100 //       else return VTK_EMPTY_CELL;
101
102 //     case SMDSAbs_Face  :
103 //       if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
104 //       else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
105 //       else if ( theNbNodes == 4 )   return VTK_QUAD;
106 //       else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
107 //       else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
108 //       else if ( theNbNodes == 9 )   return VTK_BIQUADRATIC_QUAD;
109 //       else if ( theNbNodes == 7 )   return VTK_BIQUADRATIC_TRIANGLE;
110 //       else return VTK_EMPTY_CELL;
111       
112 //     case SMDSAbs_Volume:
113 //       if (thePoly && theNbNodes>3 ) return VTK_POLYHEDRON; //VTK_CONVEX_POINT_SET;
114 //       else if ( theNbNodes == 4 )   return VTK_TETRA;
115 //       else if ( theNbNodes == 5 )   return VTK_PYRAMID;
116 //       else if ( theNbNodes == 6 )   return VTK_WEDGE;
117 //       else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
118 //       else if ( theNbNodes == 12 )  return VTK_HEXAGONAL_PRISM;
119 //       else if ( theNbNodes == 10 )  return VTK_QUADRATIC_TETRA;
120 //       else if ( theNbNodes == 20 )  return VTK_QUADRATIC_HEXAHEDRON;
121 //       else if ( theNbNodes == 27 )  return VTK_TRIQUADRATIC_HEXAHEDRON;
122 //       else if ( theNbNodes == 15 )  return VTK_QUADRATIC_WEDGE;
123 //       else if ( theNbNodes == 13 )  return VTK_QUADRATIC_PYRAMID; //VTK_CONVEX_POINT_SET;
124 //       else return VTK_EMPTY_CELL;
125
126 //     default: return VTK_EMPTY_CELL;
127 //   }
128 // }
129
130 //=================================================================================
131 // functions : SMESH_VisualObjDef
132 // purpose   : Constructor
133 //=================================================================================
134 SMESH_VisualObjDef::SMESH_VisualObjDef()
135 {
136   if ( MYDEBUG ) MESSAGE("-------------------------------SMESH_VisualObjDef::SMESH_VisualObjDef");
137   myGrid = vtkUnstructuredGrid::New();
138   myLocalGrid = false;
139   ClearEntitiesFlags();
140   SMESH::GetEntitiesFromObject(NULL);
141 }
142 SMESH_VisualObjDef::~SMESH_VisualObjDef()
143 {
144   if ( MYDEBUG ) MESSAGE("--------------------------------SMESH_VisualObjDef::~SMESH_VisualObjDef");
145   if ( MYDEBUG ) MESSAGE( "myGrid->GetReferenceCount() = " << myGrid->GetReferenceCount() );
146   myGrid->Delete();
147 }
148
149 //=================================================================================
150 // functions : GetNodeObjId, GetNodeVTKId, GetElemObjId, GetElemVTKId
151 // purpose   : Methods for retrieving VTK IDs by SMDS IDs and  vice versa
152 //=================================================================================
153 vtkIdType SMESH_VisualObjDef::GetNodeObjId( int theVTKID )
154 {
155   if (myLocalGrid)
156   {
157     TMapOfIds::const_iterator i = myVTK2SMDSNodes.find(theVTKID);
158     return i == myVTK2SMDSNodes.end() ? -1 : i->second;
159   }
160   const SMDS_MeshNode* aNode = 0;
161   if( this->GetMesh() )
162     aNode = this->GetMesh()->FindNodeVtk( theVTKID );
163
164   return aNode ? aNode->GetID() : -1;
165 }
166
167 vtkIdType SMESH_VisualObjDef::GetNodeVTKId( int theObjID )
168 {
169   if (myLocalGrid)
170   {
171     TMapOfIds::const_iterator i = mySMDS2VTKNodes.find(theObjID);
172     return i == mySMDS2VTKNodes.end() ? -1 : i->second;
173   }
174
175   const SMDS_MeshNode* aNode = 0;
176   if( this->GetMesh() ) {
177     aNode = this->GetMesh()->FindNode(theObjID);
178   }
179   return aNode ? aNode->getVtkId() : -1;
180 }
181
182 vtkIdType SMESH_VisualObjDef::GetElemObjId( int theVTKID )
183 {
184   if (myLocalGrid)
185   {
186     TMapOfIds::const_iterator i = myVTK2SMDSElems.find(theVTKID);
187     return i == myVTK2SMDSElems.end() ? -1 : i->second;
188   }
189   return this->GetMesh()->fromVtkToSmds(theVTKID);
190 }
191
192 vtkIdType SMESH_VisualObjDef::GetElemVTKId( int theObjID )
193 {
194   if (myLocalGrid)
195   {
196     TMapOfIds::const_iterator i = mySMDS2VTKElems.find(theObjID);
197     return i == mySMDS2VTKElems.end() ? -1 : i->second;
198   }
199
200   const SMDS_MeshElement* e = 0;
201   if ( this->GetMesh() )
202     e = this->GetMesh()->FindElement(theObjID);
203
204   return e ? e->getVtkId() : -1;
205 }
206
207 //=================================================================================
208 // function : SMESH_VisualObjDef::createPoints
209 // purpose  : Create points from nodes
210 //=================================================================================
211 /*! fills a vtkPoints structure for a submesh.
212  *  fills a std::list of SMDS_MeshElements*, then extract the points.
213  *  fills also conversion id maps between SMDS and VTK.
214  */
215 void SMESH_VisualObjDef::createPoints( vtkPoints* thePoints )
216 {
217   if ( thePoints == 0 )
218     return;
219
220   TEntityList aNodes;
221   vtkIdType nbNodes = GetEntities( SMDSAbs_Node, aNodes );
222   thePoints->SetNumberOfPoints( nbNodes );
223
224   int nbPoints = 0;
225
226   TEntityList::const_iterator anIter;
227   for ( anIter = aNodes.begin(); anIter != aNodes.end(); ++anIter )
228   {
229     const SMDS_MeshNode* aNode = ( const SMDS_MeshNode* )(*anIter);
230     if ( aNode != 0 )
231     {
232       thePoints->SetPoint( nbPoints, aNode->X(), aNode->Y(), aNode->Z() );
233       int anId = aNode->GetID();
234       mySMDS2VTKNodes.insert( mySMDS2VTKNodes.end(), std::make_pair( anId, nbPoints ));
235       myVTK2SMDSNodes.insert( myVTK2SMDSNodes.end(), std::make_pair( nbPoints, anId ));
236       nbPoints++;
237     }
238   }
239
240   if ( nbPoints != nbNodes )
241     thePoints->SetNumberOfPoints( nbPoints );
242 }
243
244 //=================================================================================
245 // function : buildPrs
246 // purpose  : create VTK cells( fill unstructured grid )
247 //=================================================================================
248 void SMESH_VisualObjDef::buildPrs(bool buildGrid)
249 {
250   if ( MYDEBUG ) MESSAGE("---------------------------SMESH_VisualObjDef::buildPrs " << buildGrid);
251   if (buildGrid)
252   {
253     myLocalGrid = true;
254     try
255     {
256       mySMDS2VTKNodes.clear();
257       myVTK2SMDSNodes.clear();
258       mySMDS2VTKElems.clear();
259       myVTK2SMDSElems.clear();
260
261       if ( IsNodePrs() )
262         buildNodePrs();
263       else
264         buildElemPrs();
265     }
266     catch(...)
267     {
268       mySMDS2VTKNodes.clear();
269       myVTK2SMDSNodes.clear();
270       mySMDS2VTKElems.clear();
271       myVTK2SMDSElems.clear();
272
273       myGrid->SetPoints( 0 );
274       myGrid->SetCells( 0, 0, 0, 0, 0 );
275       throw;
276     }
277   }
278   else
279   {
280     myLocalGrid = false;
281     if (!GetMesh()->isCompacted())
282     {
283       if ( MYDEBUG ) MESSAGE("*** buildPrs ==> compactMesh!");
284       GetMesh()->compactMesh();
285     }
286     vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
287     updateEntitiesFlags();
288     myGrid->ShallowCopy(theGrid);
289     //MESSAGE(myGrid->GetReferenceCount());
290     //MESSAGE( "Update - myGrid->GetNumberOfCells() = "<<myGrid->GetNumberOfCells() );
291     //MESSAGE( "Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints() );
292     if( MYDEBUGWITHFILES ) {
293       SMESH::WriteUnstructuredGrid( myGrid,"myPrs.vtu" );
294     }
295   }
296 }
297
298 //=================================================================================
299 // function : buildNodePrs
300 // purpose  : create VTK cells for nodes
301 //=================================================================================
302
303 void SMESH_VisualObjDef::buildNodePrs()
304 {
305   // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
306   // so check remaining memory size for safety
307   SMDS_Mesh::CheckMemory(); // PAL16631
308   vtkPoints* aPoints = vtkPoints::New();
309   createPoints( aPoints );
310   SMDS_Mesh::CheckMemory();
311   myGrid->SetPoints( aPoints );
312   aPoints->Delete();
313
314   myGrid->SetCells( 0, 0, 0, 0, 0 );
315 }
316
317 //=================================================================================
318 // function : buildElemPrs
319 // purpose  : Create VTK cells for elements
320 //=================================================================================
321
322 namespace{
323   typedef std::vector<const SMDS_MeshElement*> TConnect;
324
325   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
326                  TConnect& theConnect)
327   {
328     theConnect.clear();
329     for(; theNodesIter->more();)
330       theConnect.push_back(theNodesIter->next());
331     return theConnect.size();
332   }
333   
334   inline 
335   void SetId(vtkIdList *theIdList, 
336              const SMESH_VisualObjDef::TMapOfIds& theSMDS2VTKNodes, 
337              const TConnect& theConnect, 
338              int thePosition,
339              int theId)
340   {
341     theIdList->SetId(thePosition,theSMDS2VTKNodes.find(theConnect[theId]->GetID())->second);
342   }
343
344 }
345
346
347 void SMESH_VisualObjDef::buildElemPrs()
348 {
349   // Create points
350
351   vtkPoints* aPoints = vtkPoints::New();
352   createPoints( aPoints );
353   myGrid->SetPoints( aPoints );
354   aPoints->Delete();
355
356   if ( MYDEBUG )
357     MESSAGE("Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints());
358
359   // Calculate cells size
360
361   const int nbTypes = 5;
362   static SMDSAbs_ElementType aTypes[ nbTypes ] =
363     { SMDSAbs_Edge, SMDSAbs_Face, SMDSAbs_Volume, SMDSAbs_Ball, SMDSAbs_0DElement };
364
365   // get entity data
366   map<SMDSAbs_ElementType,int> nbEnts;
367   map<SMDSAbs_ElementType,TEntityList> anEnts;
368
369   vtkIdType aNbCells = 0;
370
371   for ( int i = 0; i < nbTypes; i++ )
372   {
373     nbEnts[ aTypes[ i ] ] = GetEntities( aTypes[ i ], anEnts[ aTypes[ i ] ] );
374     aNbCells += nbEnts[ aTypes [ i ]];
375   }
376   // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
377   // so check remaining memory size for safety
378   SMDS_Mesh::CheckMemory(); // PAL16631
379
380   vtkIdType aCellsSize =  2 * nbEnts[ SMDSAbs_0DElement ] + 3 * nbEnts[ SMDSAbs_Edge ];
381   aCellsSize += 2 * nbEnts[ SMDSAbs_Ball ];
382
383   for ( int i = 1; i <= 2; i++ ) // iterate through faces and volumes
384   {
385     if ( nbEnts[ aTypes[ i ] ] )
386     {
387       const TEntityList& aList = anEnts[ aTypes[ i ] ];
388       TEntityList::const_iterator anIter;
389       for ( anIter = aList.begin(); anIter != aList.end(); ++anIter ) {
390         if((*anIter)->GetEntityType() != SMDSEntity_Polyhedra &&
391            (*anIter)->GetEntityType() != SMDSEntity_Quad_Polyhedra) {
392           aCellsSize += (*anIter)->NbNodes() + 1;
393         } 
394         // Special case for the VTK_POLYHEDRON:
395         // itsinput cellArray is of special format.
396         //  [nCellFaces, nFace0Pts, i, j, k, nFace1Pts, i, j, k, ...]   
397         else {
398           if( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(*anIter) ) {
399             int nbFaces = ph->NbFaces();
400             aCellsSize += (1 + ph->NbFaces());
401             for( int i = 1; i <= nbFaces; i++ ) {
402               aCellsSize += ph->NbFaceNodes(i);
403             }
404           }
405         }
406       }
407     }
408   }
409   if ( MYDEBUG )
410     MESSAGE( "Update - aNbCells = "<<aNbCells<<"; aCellsSize = "<<aCellsSize );
411
412   // Create cells
413
414   vtkCellArray* aConnectivity = vtkCellArray::New();
415   aConnectivity->Allocate( aCellsSize, 0 );
416
417   SMDS_Mesh::CheckMemory(); // PAL16631
418
419   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
420   aCellTypesArray->SetNumberOfComponents( 1 );
421   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
422
423   SMDS_Mesh::CheckMemory(); // PAL16631
424
425   vtkIdList *anIdList = vtkIdList::New();
426   vtkIdType iElem = 0;
427
428   TConnect aConnect;
429   aConnect.reserve(VTK_CELL_SIZE);
430
431   SMDS_Mesh::CheckMemory(); // PAL16631
432   bool hasBalls = nbEnts[ SMDSAbs_Ball ] > 0;
433   vtkDataArray* aScalars = 0;
434   if(hasBalls) {
435     aScalars = vtkDataArray::CreateDataArray(VTK_DOUBLE);
436     aScalars->SetNumberOfComponents(1);
437     aScalars->SetNumberOfTuples(aNbCells);
438   }
439   for ( int i = 0; i < nbTypes; i++ ) // iterate through all types of elements
440   {
441     if ( nbEnts[ aTypes[ i ] ] > 0 ) {
442
443       const SMDSAbs_ElementType& aType = aTypes[ i ];
444       const TEntityList& aList = anEnts[ aType ];
445       TEntityList::const_iterator anIter;
446       for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
447       {
448         const SMDS_MeshElement* anElem = *anIter;
449
450         vtkIdType aNbNodes = anElem->NbNodes();
451         anIdList->SetNumberOfIds( aNbNodes );
452         const vtkIdType vtkElemType = SMDS_MeshCell::toVtkType( anElem->GetEntityType() );
453
454         int anId = anElem->GetID();
455
456         mySMDS2VTKElems.insert( mySMDS2VTKElems.end(), std::make_pair( anId, iElem ));
457         myVTK2SMDSElems.insert( myVTK2SMDSElems.end(), std::make_pair( iElem, anId ));
458
459         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
460         {
461           // Convertions connectivities from SMDS to VTK
462
463           if (aType == SMDSAbs_Volume && anElem->IsPoly() && aNbNodes > 3) { // POLYEDRE
464             anIdList->Reset();
465             if ( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(anElem) ) {
466               int nbFaces = ph->NbFaces();
467               anIdList->InsertNextId(nbFaces);
468               for( int i = 1; i <= nbFaces; i++ ) {
469                 anIdList->InsertNextId(ph->NbFaceNodes(i));
470                 for(int j = 1; j <= ph->NbFaceNodes(i); j++) {
471                   const SMDS_MeshNode* n = ph->GetFaceNode(i,j);
472                   if(n) {
473                     anIdList->InsertNextId(mySMDS2VTKNodes[n->GetID()]);
474                   }
475                 }
476               }
477             }
478           }
479           else {
480             const std::vector<int>& aConnectivities =
481               SMDS_MeshCell::toVtkOrder( VTKCellType( vtkElemType ));
482             if (aConnectivities.size() > 0) {
483               aConnect.clear();
484               GetConnect(aNodesIter,aConnect);
485               for (vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++)
486                 SetId(anIdList,mySMDS2VTKNodes,aConnect,aNodeId,aConnectivities[aNodeId]);
487             }
488             else {
489               for( vtkIdType aNodeId = 0; aNodesIter->more(); aNodeId++ ){
490                 const SMDS_MeshElement* aNode = aNodesIter->next();
491                 anIdList->SetId( aNodeId, mySMDS2VTKNodes[aNode->GetID()] );
492               }
493             }
494           }
495         }
496         vtkIdType aCurId = aConnectivity->InsertNextCell( anIdList );
497         aCellTypesArray->InsertNextValue( vtkElemType );
498         
499         //Store diameters of the balls
500         if(aScalars) {
501           double aDiam = 0;
502           if(aType == SMDSAbs_Ball) {
503             if (const SMDS_BallElement* ball = dynamic_cast<const SMDS_BallElement*>(anElem) ) {
504               aDiam = ball->GetDiameter();
505             }
506           }
507           aScalars->SetTuple(aCurId,&aDiam);
508         }
509
510         iElem++;
511       }
512     }
513     SMDS_Mesh::CheckMemory(); // PAL16631
514   }
515
516   // Insert cells in grid
517
518   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
519   aCellLocationsArray->SetNumberOfComponents( 1 );
520   aCellLocationsArray->SetNumberOfTuples( aNbCells );
521
522   SMDS_Mesh::CheckMemory(); // PAL16631
523
524   aConnectivity->InitTraversal();
525   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
526     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
527
528   myGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
529   myGrid->GetCellData()->SetScalars(aScalars);
530
531   aCellLocationsArray->Delete();
532   aCellTypesArray->Delete();
533   aConnectivity->Delete();
534   anIdList->Delete();
535
536   SMDS_Mesh::CheckMemory(); // PAL16631
537 }
538
539 //=================================================================================
540 // function : GetEdgeNodes
541 // purpose  : Retrieve ids of nodes from edge of elements ( edge is numbered from 0 )
542 //=================================================================================
543 bool SMESH_VisualObjDef::GetEdgeNodes( const int theElemId,
544                                        const int theEdgeNum,
545                                        int&      theNodeId1,
546                                        int&      theNodeId2 ) const
547 {
548   const SMDS_Mesh* aMesh = GetMesh();
549   if ( aMesh == 0 )
550     return false;
551     
552   const SMDS_MeshElement* anElem = aMesh->FindElement( theElemId );
553   if ( anElem == 0 )
554     return false;
555     
556   int nbNodes = anElem->NbCornerNodes();
557
558   if (( theEdgeNum < 0 || theEdgeNum > 3 ) ||
559       ( nbNodes != 3 && nbNodes != 4 ) ||
560       ( theEdgeNum >= nbNodes ))
561     return false;
562
563   theNodeId1 = anElem->GetNode(  theEdgeNum                 )->GetID();
564   theNodeId2 = anElem->GetNode(( theEdgeNum + 1 ) % nbNodes )->GetID();
565
566   return true;
567 }
568
569 vtkUnstructuredGrid* SMESH_VisualObjDef::GetUnstructuredGrid()
570 {
571   if ( !myLocalGrid && !GetMesh()->isCompacted() )
572   {
573     GetMesh()->compactMesh();
574     updateEntitiesFlags();
575     vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
576     myGrid->ShallowCopy(theGrid);
577   }
578   return myGrid;
579 }
580
581
582 //=================================================================================
583 // function : IsValid
584 // purpose  : Return true if there are some entities
585 //=================================================================================
586 bool SMESH_VisualObjDef::IsValid() const
587 {
588   return ( GetNbEntities(SMDSAbs_0DElement) > 0 ||
589            GetNbEntities(SMDSAbs_Ball     ) > 0 ||
590            GetNbEntities(SMDSAbs_Edge     ) > 0 ||
591            GetNbEntities(SMDSAbs_Face     ) > 0 ||
592            GetNbEntities(SMDSAbs_Volume   ) > 0 ||
593            GetNbEntities(SMDSAbs_Node     ) > 0 );
594 }
595
596 //=================================================================================
597 // function : updateEntitiesFlags
598 // purpose  : Update entities flags
599 //=================================================================================
600 void SMESH_VisualObjDef::updateEntitiesFlags()
601 {
602   unsigned int tmp = myEntitiesState;
603   ClearEntitiesFlags();
604
605   map<SMDSAbs_ElementType,int> entities = SMESH::GetEntitiesFromObject(this);
606
607
608   if( myEntitiesCache[SMDSAbs_0DElement] != 0 ||
609       myEntitiesCache[SMDSAbs_0DElement] >= entities[SMDSAbs_0DElement] )
610     myEntitiesState &= ~SMESH_Actor::e0DElements;
611
612   if( myEntitiesCache[SMDSAbs_Ball] != 0 ||
613       myEntitiesCache[SMDSAbs_Ball] >= entities[SMDSAbs_Ball] )
614     myEntitiesState &= ~SMESH_Actor::eBallElem;
615
616   if( myEntitiesCache[SMDSAbs_Edge] != 0 ||
617       myEntitiesCache[SMDSAbs_Edge] >= entities[SMDSAbs_Edge] )
618     myEntitiesState &= ~SMESH_Actor::eEdges;
619
620   if( myEntitiesCache[SMDSAbs_Face] != 0 ||
621       myEntitiesCache[SMDSAbs_Face] >= entities[SMDSAbs_Face] )
622     myEntitiesState &= ~SMESH_Actor::eFaces;
623
624   if( myEntitiesCache[SMDSAbs_Volume] != 0 ||
625       myEntitiesCache[SMDSAbs_Volume] >= entities[SMDSAbs_Volume] )
626     myEntitiesState &= ~SMESH_Actor::eVolumes;
627
628   if( tmp != myEntitiesState ) {
629     myEntitiesFlag = true;
630   }
631
632   myEntitiesCache = entities;
633 }
634
635 //=================================================================================
636 // function : ClearEntitiesFlags
637 // purpose  : Clear the entities flags
638 //=================================================================================
639 void SMESH_VisualObjDef::ClearEntitiesFlags()
640 {
641   myEntitiesState = SMESH_Actor::eAllEntity;
642   myEntitiesFlag = false;
643 }
644
645 //=================================================================================
646 // function : GetEntitiesFlag
647 // purpose  : Return the entities flag
648 //=================================================================================
649 bool SMESH_VisualObjDef::GetEntitiesFlag()
650 {
651   return myEntitiesFlag;
652 }
653
654 //=================================================================================
655 // function : GetEntitiesState
656 // purpose  : Return the entities state
657 //=================================================================================
658 unsigned int SMESH_VisualObjDef::GetEntitiesState()
659 {
660   return myEntitiesState;
661 }
662
663 /*
664   Class       : SMESH_MeshObj
665   Description : Class for visualisation of mesh
666 */
667
668 //=================================================================================
669 // function : SMESH_MeshObj
670 // purpose  : Constructor
671 //=================================================================================
672 SMESH_MeshObj::SMESH_MeshObj(SMESH::SMESH_Mesh_ptr theMesh):
673   myClient(SalomeApp_Application::orb(),theMesh)
674 {
675         myEmptyGrid = 0;
676   if ( MYDEBUG ) 
677     MESSAGE("SMESH_MeshObj - this = "<<this<<"; theMesh->_is_nil() = "<<theMesh->_is_nil());
678 }
679
680 //=================================================================================
681 // function : ~SMESH_MeshObj
682 // purpose  : Destructor
683 //=================================================================================
684 SMESH_MeshObj::~SMESH_MeshObj()
685 {
686   if ( MYDEBUG ) 
687     MESSAGE("SMESH_MeshObj - this = "<<this<<"\n");
688   if ( myEmptyGrid )
689     myEmptyGrid->Delete();
690 }
691
692 //=================================================================================
693 // function : Update
694 // purpose  : Update mesh and fill grid with new values if necessary 
695 //=================================================================================
696 bool SMESH_MeshObj::Update( int theIsClear )
697 {
698   // Update SMDS_Mesh on client part
699   if ( MYDEBUG ) MESSAGE("SMESH_MeshObj::Update " << this);
700   if ( myClient.Update(theIsClear) || GetUnstructuredGrid()->GetNumberOfPoints()==0) {
701     if ( MYDEBUG ) MESSAGE("buildPrs");
702     buildPrs();  // Fill unstructured grid
703     return true;
704   }
705   return false;
706 }
707
708 bool SMESH_MeshObj::NulData()
709 {
710   if ( MYDEBUG ) MESSAGE ("SMESH_MeshObj::NulData() =============================================");
711   if (!myEmptyGrid)
712   {
713     myEmptyGrid = SMDS_UnstructuredGrid::New();
714     myEmptyGrid->Initialize();
715     myEmptyGrid->Allocate();
716     vtkPoints* points = vtkPoints::New();
717     points->SetNumberOfPoints(0);
718     myEmptyGrid->SetPoints( points );
719     points->Delete();
720     myEmptyGrid->BuildLinks();
721   }
722   myGrid->ShallowCopy(myEmptyGrid);
723   return true;
724 }
725 //=================================================================================
726 // function : GetElemDimension
727 // purpose  : Get dimension of element
728 //=================================================================================
729 int SMESH_MeshObj::GetElemDimension( const int theObjId )
730 {
731   const SMDS_MeshElement* anElem = myClient->FindElement( theObjId );
732   if ( anElem == 0 )
733     return 0;
734
735   int aType = anElem->GetType();
736   switch ( aType )
737   {
738     case SMDSAbs_0DElement : return 0;
739     case SMDSAbs_Ball : return 0;
740     case SMDSAbs_Edge  : return 1;
741     case SMDSAbs_Face  : return 2;
742     case SMDSAbs_Volume: return 3;
743     default            : return 0;
744   }
745 }
746
747 //=================================================================================
748 // function : GetEntities
749 // purpose  : Get entities of specified type. Return number of entities
750 //=================================================================================
751 int SMESH_MeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
752 {
753   switch ( theType )
754   {
755     case SMDSAbs_Node:
756     {
757       return myClient->NbNodes();
758     }
759     break;
760     case SMDSAbs_0DElement:
761     {
762       return myClient->Nb0DElements();
763     }
764     case SMDSAbs_Ball:
765     {
766       return myClient->NbBalls();
767     }
768     break;
769     case SMDSAbs_Edge:
770     {
771       return myClient->NbEdges();
772     }
773     break;
774     case SMDSAbs_Face:
775     {
776       return myClient->NbFaces();
777     }
778     break;
779     case SMDSAbs_Volume:
780     {
781       return myClient->NbVolumes();
782     }
783     break;
784     default:
785       return 0;
786     break;
787   }
788 }
789
790 int SMESH_MeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theObjs ) const
791 {
792   theObjs.clear();
793
794   switch ( theType )
795   {
796     case SMDSAbs_Node:
797     {
798       SMDS_NodeIteratorPtr anIter = myClient->nodesIterator();
799       while ( anIter->more() ) theObjs.push_back( anIter->next() );
800     }
801     break;
802     case SMDSAbs_0DElement:
803     {
804       SMDS_ElemIteratorPtr anIter = myClient->elementsIterator(SMDSAbs_0DElement);
805       while ( anIter->more() ) theObjs.push_back( anIter->next() );
806     }
807     break;
808     case SMDSAbs_Ball:
809     {
810       SMDS_ElemIteratorPtr anIter = myClient->elementGeomIterator(SMDSGeom_BALL);
811       while ( anIter->more() ) theObjs.push_back( anIter->next() );
812     }
813     break;
814     case SMDSAbs_Edge:
815     {
816       SMDS_EdgeIteratorPtr anIter = myClient->edgesIterator();
817       while ( anIter->more() ) theObjs.push_back( anIter->next() );
818     }
819     break;
820     case SMDSAbs_Face:
821     {
822       SMDS_FaceIteratorPtr anIter = myClient->facesIterator();
823       while ( anIter->more() ) theObjs.push_back( anIter->next() );
824     }
825     break;
826     case SMDSAbs_Volume:
827     {
828       SMDS_VolumeIteratorPtr anIter = myClient->volumesIterator();
829       while ( anIter->more() ) theObjs.push_back( anIter->next() );
830     }
831     break;
832     default:
833     break;
834   }
835
836   return theObjs.size();
837 }
838
839 //=================================================================================
840 // function : UpdateFunctor
841 // purpose  : Update functor in accordance with current mesh
842 //=================================================================================
843 void SMESH_MeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
844 {
845   theFunctor->SetMesh( GetMesh() );
846 }
847
848 //=================================================================================
849 // function : IsNodePrs
850 // purpose  : Return true if node presentation is used
851 //=================================================================================
852 bool SMESH_MeshObj::IsNodePrs() const
853 {
854   return myClient->Nb0DElements() + myClient->NbEdges() + myClient->NbFaces() + myClient->NbVolumes() + myClient->NbBalls() == 0 ;
855 }
856
857
858 /*
859   Class       : SMESH_SubMeshObj
860   Description : Base class for visualisation of submeshes and groups
861 */
862
863 //=================================================================================
864 // function : SMESH_SubMeshObj
865 // purpose  : Constructor
866 //=================================================================================
867 SMESH_SubMeshObj::SMESH_SubMeshObj( SMESH_MeshObj* theMeshObj )
868 {
869   if ( MYDEBUG ) MESSAGE( "SMESH_SubMeshObj - theMeshObj = " << theMeshObj );
870   
871   myMeshObj = theMeshObj;
872 }
873
874 SMESH_SubMeshObj::~SMESH_SubMeshObj()
875 {
876 }
877
878 //=================================================================================
879 // function : GetElemDimension
880 // purpose  : Get dimension of element
881 //=================================================================================
882 int SMESH_SubMeshObj::GetElemDimension( const int theObjId )
883 {
884   return myMeshObj == 0 ? 0 : myMeshObj->GetElemDimension( theObjId );
885 }
886
887 //=================================================================================
888 // function : UpdateFunctor
889 // purpose  : Update functor in accordance with current mesh
890 //=================================================================================
891
892 void SMESH_SubMeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
893 {
894   theFunctor->SetMesh( myMeshObj->GetMesh() );
895 }
896
897 //=================================================================================
898 // function : Update
899 // purpose  : Update mesh object and fill grid with new values 
900 //=================================================================================
901 bool SMESH_SubMeshObj::Update( int theIsClear )
902 {
903   if ( MYDEBUG ) MESSAGE("SMESH_SubMeshObj::Update " << this)
904   bool changed = myMeshObj->Update( theIsClear );
905   buildPrs(true);
906   return changed;
907 }
908
909
910 /*
911   Class       : SMESH_GroupObj
912   Description : Class for visualisation of groups
913 */
914
915 //=================================================================================
916 // function : SMESH_GroupObj
917 // purpose  : Constructor
918 //=================================================================================
919 SMESH_GroupObj::SMESH_GroupObj( SMESH::SMESH_GroupBase_ptr theGroup, 
920                                 SMESH_MeshObj*             theMeshObj )
921 : SMESH_SubMeshObj( theMeshObj ),
922   myGroupServer( SMESH::SMESH_GroupBase::_duplicate(theGroup) )
923 {
924   if ( MYDEBUG ) MESSAGE("SMESH_GroupObj - theGroup->_is_nil() = "<<theGroup->_is_nil());
925   myGroupServer->Register();
926 }
927
928 SMESH_GroupObj::~SMESH_GroupObj()
929 {
930   if ( MYDEBUG ) MESSAGE("~SMESH_GroupObj");
931   myGroupServer->UnRegister();
932 }
933
934 //=================================================================================
935 // function : IsNodePrs
936 // purpose  : Return true if node presentation is used
937 //=================================================================================
938 bool SMESH_GroupObj::IsNodePrs() const
939 {
940   return myGroupServer->GetType() == SMESH::NODE;
941 }
942
943 //=================================================================================
944 // function : GetElementType
945 // purpose  : Return type of elements of group
946 //=================================================================================
947 SMDSAbs_ElementType SMESH_GroupObj::GetElementType() const
948 {
949   return SMDSAbs_ElementType(myGroupServer->GetType());
950 }
951
952 //=================================================================================
953 // function : getNodesFromElems
954 // purpose  : Retrieve nodes from elements
955 //=================================================================================
956 static int getNodesFromElems( SMESH::long_array_var&              theElemIds,
957                               const SMDS_Mesh*                    theMesh,
958                               std::list<const SMDS_MeshElement*>& theResList )
959 {
960   set<const SMDS_MeshElement*> aNodeSet;
961
962   for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
963   {
964     const SMDS_MeshElement* anElem = theMesh->FindElement( theElemIds[ i ] );
965     if ( anElem != 0 )
966     {
967       SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
968       while ( anIter->more() )
969       {
970         const SMDS_MeshElement* aNode = anIter->next();
971         if ( aNode != 0 )
972           aNodeSet.insert( aNode );
973       }
974     }
975   }
976
977   set<const SMDS_MeshElement*>::const_iterator anIter;
978   for ( anIter = aNodeSet.begin(); anIter != aNodeSet.end(); ++anIter )
979     theResList.push_back( *anIter );
980
981   return theResList.size();    
982 }
983
984 //=================================================================================
985 // function : getPointers
986 // purpose  : Get std::list<const SMDS_MeshElement*> from list of IDs
987 //=================================================================================
988 static int getPointers( const SMDSAbs_ElementType           theRequestType,
989                         SMESH::long_array_var&              theElemIds,
990                         const SMDS_Mesh*                    theMesh,
991                         std::list<const SMDS_MeshElement*>& theResList )
992 {
993   for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
994   {
995     const SMDS_MeshElement* anElem = theRequestType == SMDSAbs_Node
996       ? theMesh->FindNode( theElemIds[ i ] ) : theMesh->FindElement( theElemIds[ i ] );
997
998     if ( anElem != 0 )
999       theResList.push_back( anElem );
1000   }
1001
1002   return theResList.size();
1003 }
1004
1005
1006 //=================================================================================
1007 // function : GetEntities
1008 // purpose  : Get entities of specified type. Return number of entities
1009 //=================================================================================
1010 int SMESH_GroupObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1011 {
1012   if(SMDSAbs_ElementType(myGroupServer->GetType()) == theType) {
1013     return myGroupServer->Size();
1014   }
1015   if ( theType == SMDSAbs_Node ) {
1016     return myGroupServer->GetNumberOfNodes();
1017   }
1018   return 0;
1019 }
1020
1021 int SMESH_GroupObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1022 {
1023   theResList.clear();
1024   SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1025   
1026   if ( aMesh == 0 )
1027     return 0;
1028
1029   SMDSAbs_ElementType aGrpType = SMDSAbs_ElementType(myGroupServer->GetType());
1030   if ( aGrpType != theType && theType != SMDSAbs_Node )
1031     return 0;
1032
1033   SMESH::long_array_var anIds = myGroupServer->GetListOfID();
1034   if ( anIds->length() == 0 )
1035     return 0;
1036
1037   if ( aGrpType == theType )
1038     return getPointers( theType, anIds, aMesh, theResList );
1039   else if ( theType == SMDSAbs_Node )
1040     return getNodesFromElems( anIds, aMesh, theResList );
1041   else
1042     return 0;
1043 }
1044
1045
1046
1047 /*
1048   Class       : SMESH_subMeshObj
1049   Description : Class for visualisation of submeshes
1050 */
1051
1052 //=================================================================================
1053 // function : SMESH_subMeshObj
1054 // purpose  : Constructor
1055 //=================================================================================
1056 SMESH_subMeshObj::SMESH_subMeshObj( SMESH::SMESH_subMesh_ptr theSubMesh,
1057                                     SMESH_MeshObj*           theMeshObj )
1058 : SMESH_SubMeshObj( theMeshObj ),
1059   mySubMeshServer( SMESH::SMESH_subMesh::_duplicate( theSubMesh ) )
1060 {
1061   if ( MYDEBUG ) MESSAGE( "SMESH_subMeshObj - theSubMesh->_is_nil() = " << theSubMesh->_is_nil() );
1062   
1063   mySubMeshServer->Register();
1064 }
1065
1066 SMESH_subMeshObj::~SMESH_subMeshObj()
1067 {
1068   if ( MYDEBUG ) MESSAGE( "~SMESH_subMeshObj" );
1069   mySubMeshServer->UnRegister();
1070 }
1071
1072 //=================================================================================
1073 // function : GetEntities
1074 // purpose  : Get entities of specified type. Return number of entities
1075 //=================================================================================
1076 int SMESH_subMeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1077 {
1078   switch ( theType )
1079   {
1080     case SMDSAbs_Node:
1081     {
1082       return mySubMeshServer->GetNumberOfNodes( /*all=*/true );
1083     }
1084     break;
1085     case SMDSAbs_Ball:
1086     case SMDSAbs_0DElement:
1087     case SMDSAbs_Edge:
1088     case SMDSAbs_Face:
1089     case SMDSAbs_Volume:
1090     {
1091       SMESH::long_array_var anIds = 
1092         mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1093       return anIds->length();
1094     }
1095     default:
1096       return 0;
1097     break;
1098   }
1099 }
1100
1101 int SMESH_subMeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1102 {
1103   theResList.clear();
1104
1105   SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1106   if ( aMesh == 0 )
1107     return 0;
1108
1109   bool isNodal = IsNodePrs();
1110
1111   if ( isNodal )
1112   {
1113     if ( theType == SMDSAbs_Node )
1114     {
1115       SMESH::long_array_var anIds = mySubMeshServer->GetNodesId();
1116       return getPointers( SMDSAbs_Node, anIds, aMesh, theResList );
1117     }
1118   }
1119   else
1120   {
1121     if ( theType == SMDSAbs_Node )
1122     {
1123       SMESH::long_array_var anIds = mySubMeshServer->GetElementsId();
1124       return getNodesFromElems( anIds, aMesh, theResList );
1125     }
1126     else
1127     {
1128       SMESH::long_array_var anIds = 
1129         mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1130       return getPointers( theType, anIds, aMesh, theResList );
1131     }
1132   }
1133
1134   return 0;
1135 }
1136
1137 //=================================================================================
1138 // function : IsNodePrs
1139 // purpose  : Return true if node presentation is used
1140 //=================================================================================
1141 bool SMESH_subMeshObj::IsNodePrs() const
1142 {
1143   return mySubMeshServer->GetNumberOfElements() == 0;
1144 }