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