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