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