Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / StdMeshers / StdMeshers_Hexa_3D.cxx
1 // Copyright (C) 2007-2011  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 SMESH : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Hexa_3D.cxx
25 //           Moved here from SMESH_Hexa_3D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //
29 #include "StdMeshers_Hexa_3D.hxx"
30
31 #include "StdMeshers_CompositeHexa_3D.hxx"
32 #include "StdMeshers_FaceSide.hxx"
33 #include "StdMeshers_HexaFromSkin_3D.hxx"
34 #include "StdMeshers_Penta_3D.hxx"
35 #include "StdMeshers_Prism_3D.hxx"
36 #include "StdMeshers_Quadrangle_2D.hxx"
37 #include "StdMeshers_ViscousLayers.hxx"
38
39 #include "SMESH_Comment.hxx"
40 #include "SMESH_Gen.hxx"
41 #include "SMESH_Mesh.hxx"
42 #include "SMESH_MesherHelper.hxx"
43 #include "SMESH_subMesh.hxx"
44
45 #include "SMDS_MeshElement.hxx"
46 #include "SMDS_MeshNode.hxx"
47 #include "SMDS_FacePosition.hxx"
48 #include "SMDS_VolumeTool.hxx"
49 #include "SMDS_VolumeOfNodes.hxx"
50
51 #include <TopExp.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopTools_ListOfShape.hxx>
56 #include <TopTools_SequenceOfShape.hxx>
57 #include <TopTools_MapOfShape.hxx>
58 #include <TopoDS.hxx>
59 #include <gp_Pnt2d.hxx>
60
61 #include "utilities.h"
62 #include "Utils_ExceptHandlers.hxx"
63
64 typedef SMESH_Comment TComm;
65
66 using namespace std;
67
68 static SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &,
69                                                     const TopoDS_Shape &,
70                                                     SMESH_ProxyMesh* proxyMesh=0);
71
72 static bool EvaluatePentahedralMesh(SMESH_Mesh &, const TopoDS_Shape &,
73                                     MapShapeNbElems &);
74
75 //=============================================================================
76 /*!
77  * Constructor
78  */
79 //=============================================================================
80
81 StdMeshers_Hexa_3D::StdMeshers_Hexa_3D(int hypId, int studyId, SMESH_Gen * gen)
82   :SMESH_3D_Algo(hypId, studyId, gen)
83 {
84   MESSAGE("StdMeshers_Hexa_3D::StdMeshers_Hexa_3D");
85   _name = "Hexa_3D";
86   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit /shape type
87   _requireShape = false;
88   _compatibleHypothesis.push_back("ViscousLayers");
89 }
90
91 //=============================================================================
92 /*!
93  * Destructor
94  */
95 //=============================================================================
96
97 StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D()
98 {
99   MESSAGE("StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D");
100 }
101
102 //=============================================================================
103 /*!
104  * Retrieves defined hypotheses
105  */
106 //=============================================================================
107
108 bool StdMeshers_Hexa_3D::CheckHypothesis
109                          (SMESH_Mesh&                          aMesh,
110                           const TopoDS_Shape&                  aShape,
111                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
112 {
113   // check nb of faces in the shape
114 /*  PAL16229
115   aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
116   int nbFaces = 0;
117   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next())
118     if ( ++nbFaces > 6 )
119       break;
120   if ( nbFaces != 6 )
121     return false;
122 */
123
124   _viscousLayersHyp = NULL;
125
126   const list<const SMESHDS_Hypothesis*>& hyps =
127     GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliary=*/false);
128   list <const SMESHDS_Hypothesis* >::const_iterator h = hyps.begin();
129   if ( h == hyps.end())
130   {
131     aStatus = SMESH_Hypothesis::HYP_OK;
132     return true;
133   }
134
135   aStatus = HYP_OK;
136   for ( ; h != hyps.end(); ++h )
137   {
138     string hypName = (*h)->GetName();
139     if ( find( _compatibleHypothesis.begin(),_compatibleHypothesis.end(),hypName )
140          != _compatibleHypothesis.end() )
141     {
142       _viscousLayersHyp = dynamic_cast< const StdMeshers_ViscousLayers*> ( *h );
143     }
144     else
145     {
146       aStatus = HYP_INCOMPATIBLE;
147     }
148   }
149
150   if ( !_viscousLayersHyp )
151     aStatus = HYP_INCOMPATIBLE;
152
153   return aStatus == HYP_OK;
154 }
155
156 namespace
157 {
158   //=============================================================================
159
160   typedef boost::shared_ptr< FaceQuadStruct > FaceQuadStructPtr;
161
162   // symbolic names of box sides
163   enum EBoxSides{ B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, B_NB_SIDES };
164
165   // symbolic names of sides of quadrangle
166   enum EQuadSides{ Q_BOTTOM=0, Q_RIGHT, Q_TOP, Q_LEFT, Q_NB_SIDES };
167
168   //=============================================================================
169   /*!
170    * \brief Container of nodes of structured mesh on a qudrangular geom FACE
171    */
172   struct _FaceGrid
173   {
174     // face sides
175     FaceQuadStructPtr _quad;
176
177     // map of (node parameter on EDGE) to (column (vector) of nodes)
178     TParam2ColumnMap _u2nodesMap;
179
180     // node column's taken form _u2nodesMap taking into account sub-shape orientation
181     vector<TNodeColumn> _columns;
182
183     // geometry of a cube side
184     TopoDS_Face _sideF;
185
186     const SMDS_MeshNode* GetNode(int iCol, int iRow) const
187     {
188       return _columns[iCol][iRow];
189     }
190     gp_XYZ GetXYZ(int iCol, int iRow) const
191     {
192       return SMESH_TNodeXYZ( GetNode( iCol, iRow ));
193     }
194   };
195
196   //================================================================================
197   /*!
198    * \brief Convertor of a pair of integers to a sole index
199    */
200   struct _Indexer
201   {
202     int _xSize, _ySize;
203     _Indexer( int xSize, int ySize ): _xSize(xSize), _ySize(ySize) {}
204     int size() const { return _xSize * _ySize; }
205     int operator()(const int x, const int y) const { return y * _xSize + x; }
206   };
207
208   //================================================================================
209   /*!
210    * \brief Appends a range of node columns from a map to another map
211    */
212   template< class TMapIterator >
213   void append( TParam2ColumnMap& toMap, TMapIterator from, TMapIterator to )
214   {
215     const SMDS_MeshNode* lastNode = toMap.rbegin()->second[0];
216     const SMDS_MeshNode* firstNode = from->second[0];
217     if ( lastNode == firstNode )
218       from++;
219     double u = toMap.rbegin()->first;
220     for (; from != to; ++from )
221     {
222       u += 1;
223       TParam2ColumnMap::iterator u2nn = toMap.insert( toMap.end(), make_pair ( u, TNodeColumn()));
224       u2nn->second.swap( from->second );
225     }
226   }
227
228   //================================================================================
229   /*!
230    * \brief Finds FaceQuadStruct having a side equal to a given one and rearranges
231    *  the found FaceQuadStruct::side to have the given side at a Q_BOTTOM place
232    */
233   FaceQuadStructPtr getQuadWithBottom( StdMeshers_FaceSide* side,
234                                        FaceQuadStructPtr    quad[ 6 ])
235   {
236     FaceQuadStructPtr foundQuad;
237     for ( int i = 1; i < 6; ++i )
238     {
239       if ( !quad[i] ) continue;
240       for ( unsigned iS = 0; iS < quad[i]->side.size(); ++iS )
241       {
242         const StdMeshers_FaceSide* side2 = quad[i]->side[iS];
243         if (( side->FirstVertex().IsSame( side2->FirstVertex() ) ||
244               side->FirstVertex().IsSame( side2->LastVertex() ))
245             &&
246             ( side->LastVertex().IsSame( side2->FirstVertex() ) ||
247               side->LastVertex().IsSame( side2->LastVertex() ))
248             )
249         {
250           if ( iS != Q_BOTTOM )
251           {
252             vector< StdMeshers_FaceSide*> newSides;
253             for ( unsigned j = iS; j < quad[i]->side.size(); ++j )
254               newSides.push_back( quad[i]->side[j] );
255             for ( unsigned j = 0; j < iS; ++j )
256               newSides.push_back( quad[i]->side[j] );
257             quad[i]->side.swap( newSides );
258           }
259           foundQuad.swap(quad[i]);
260           return foundQuad;
261         }
262       }
263     }
264     return foundQuad;
265   }
266 }
267
268 //=============================================================================
269 /*!
270  * Generates hexahedron mesh on hexaedron like form using algorithm from
271  * "Application de l'interpolation transfinie Ã  la création de maillages
272  *  C0 ou G1 continus sur des triangles, quadrangles, tetraedres, pentaedres
273  *  et hexaedres déformés."
274  * Alain PERONNET - 8 janvier 1999
275  */
276 //=============================================================================
277
278 bool StdMeshers_Hexa_3D::Compute(SMESH_Mesh &         aMesh,
279                                  const TopoDS_Shape & aShape)// throw(SALOME_Exception)
280 {
281   // PAL14921. Enable catching std::bad_alloc and Standard_OutOfMemory outside
282   //Unexpect aCatch(SalomeException);
283   MESSAGE("StdMeshers_Hexa_3D::Compute");
284   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
285
286   // Shape verification
287   // ----------------------
288
289   // shape must be a solid (or a shell) with 6 faces
290   TopExp_Explorer exp(aShape,TopAbs_SHELL);
291   if ( !exp.More() )
292     return error(COMPERR_BAD_SHAPE, "No SHELL in the geometry");
293   if ( exp.Next(), exp.More() )
294     return error(COMPERR_BAD_SHAPE, "More than one SHELL in the geometry");
295
296   TopTools_IndexedMapOfShape FF;
297   TopExp::MapShapes( aShape, TopAbs_FACE, FF);
298   if ( FF.Extent() != 6)
299   {
300     static StdMeshers_CompositeHexa_3D compositeHexa(_gen->GetANewId(), 0, _gen);
301     if ( !compositeHexa.Compute( aMesh, aShape ))
302       return error( compositeHexa.GetComputeError() );
303     return true;
304   }
305
306   // Find sides of a cube
307   // ---------------------
308   
309   FaceQuadStructPtr quad[ 6 ];
310   StdMeshers_Quadrangle_2D quadAlgo( _gen->GetANewId(), GetStudyId(), _gen);
311   for ( int i = 0; i < 6; ++i )
312   {
313     if ( !( quad[i] = FaceQuadStructPtr( quadAlgo.CheckNbEdges( aMesh, FF( i+1 )))))
314       return error( quadAlgo.GetComputeError() );
315     if ( quad[i]->side.size() != 4 )
316       return error( COMPERR_BAD_SHAPE, "Not a quadrangular box side" );
317   }
318
319   _FaceGrid aCubeSide[ 6 ];
320
321   swap( aCubeSide[B_BOTTOM]._quad, quad[0] );
322   swap( aCubeSide[B_BOTTOM]._quad->side[ Q_RIGHT],// direct the normal of bottom quad inside cube
323         aCubeSide[B_BOTTOM]._quad->side[ Q_LEFT ] );
324
325   aCubeSide[B_FRONT]._quad = getQuadWithBottom( aCubeSide[B_BOTTOM]._quad->side[Q_BOTTOM], quad );
326   aCubeSide[B_RIGHT]._quad = getQuadWithBottom( aCubeSide[B_BOTTOM]._quad->side[Q_RIGHT ], quad );
327   aCubeSide[B_BACK ]._quad = getQuadWithBottom( aCubeSide[B_BOTTOM]._quad->side[Q_TOP   ], quad );
328   aCubeSide[B_LEFT ]._quad = getQuadWithBottom( aCubeSide[B_BOTTOM]._quad->side[Q_LEFT  ], quad );
329   if ( aCubeSide[B_FRONT ]._quad )
330     aCubeSide[B_TOP  ]._quad = getQuadWithBottom( aCubeSide[B_FRONT ]._quad->side[Q_TOP ], quad );
331
332   for ( int i = 1; i < 6; ++i )
333     if ( !aCubeSide[i]._quad )
334       return error( COMPERR_BAD_SHAPE );
335
336   // Make viscous layers
337   // --------------------
338
339   SMESH_ProxyMesh::Ptr proxymesh;
340   if ( _viscousLayersHyp )
341   {
342     proxymesh = _viscousLayersHyp->Compute( aMesh, aShape, /*makeN2NMap=*/ true );
343     if ( !proxymesh )
344       return false;
345   }
346
347   // Check if there are triangles on cube sides
348   // -------------------------------------------
349
350   if ( aMesh.NbTriangles() > 0 )
351   {
352     for ( int i = 0; i < 6; ++i )
353     {
354       const TopoDS_Face& sideF = aCubeSide[i]._quad->face;
355       if ( SMESHDS_SubMesh* smDS = meshDS->MeshElements( sideF ))
356       {
357         bool isAllQuad = true;
358         SMDS_ElemIteratorPtr fIt = smDS->GetElements();
359         while ( fIt->more() && isAllQuad )
360         {
361           const SMDS_MeshElement* f = fIt->next();
362           isAllQuad = ( f->NbCornerNodes() == 4 );
363         }
364         if ( !isAllQuad )
365         {
366           SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape, proxymesh.get());
367           return error( err );
368         }
369       }
370     }
371   }
372
373   // Check presence of regular grid mesh on FACEs of the cube
374   // ------------------------------------------------------------
375
376   // tool creating quadratic elements if needed
377   SMESH_MesherHelper helper (aMesh);
378   _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
379
380   for ( int i = 0; i < 6; ++i )
381   {
382     const TopoDS_Face& F = aCubeSide[i]._quad->face;
383     StdMeshers_FaceSide* baseQuadSide = aCubeSide[i]._quad->side[ Q_BOTTOM ];
384     vector< TopAbs_Orientation > eOri( baseQuadSide->NbEdges() );
385
386     for ( int iE = 0; iE < baseQuadSide->NbEdges(); ++iE )
387     {
388       const TopoDS_Edge& baseE = baseQuadSide->Edge( iE );
389       eOri[ iE ] = baseE.Orientation();
390
391       // assure correctness of node positions on baseE:
392       // helper.GetNodeU() will fix positions if they are wrong
393       if ( SMESHDS_SubMesh* smDS = meshDS->MeshElements( baseE ))
394       {
395         bool ok;
396         helper.SetSubShape( baseE );
397         SMDS_ElemIteratorPtr eIt = smDS->GetElements();
398         while ( eIt->more() )
399         {
400           const SMDS_MeshElement* e = eIt->next();
401           helper.GetNodeU( baseE, e->GetNode(0), e->GetNode(1), &ok);
402           helper.GetNodeU( baseE, e->GetNode(1), e->GetNode(0), &ok);
403         }
404       }
405
406       // load grid
407       TParam2ColumnMap u2nodesMap;
408       if ( !helper.LoadNodeColumns( u2nodesMap, F, baseE, meshDS, proxymesh.get() ))
409       {
410         SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape, proxymesh.get());
411         return error( err );
412       }
413       // store u2nodesMap
414       if ( iE == 0 )
415       {
416         aCubeSide[i]._u2nodesMap.swap( u2nodesMap );
417       }
418       else // unite 2 maps
419       {
420         if ( eOri[0] == eOri[iE] )
421           append( aCubeSide[i]._u2nodesMap, u2nodesMap.begin(), u2nodesMap.end());
422         else
423           append( aCubeSide[i]._u2nodesMap, u2nodesMap.rbegin(), u2nodesMap.rend());
424       }
425     }
426     // check if the loaded grid corresponds to nb of quadrangles
427     const SMESHDS_SubMesh* faceSubMesh =
428       proxymesh ? proxymesh->GetSubMesh( F ) : meshDS->MeshElements( F );
429     const int nbQuads = faceSubMesh->NbElements();
430     const int nbHor = aCubeSide[i]._u2nodesMap.size() - 1;
431     const int nbVer = aCubeSide[i]._u2nodesMap.begin()->second.size() - 1;
432     if ( nbQuads != nbHor * nbVer )
433     {
434       SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape, proxymesh.get());
435       return error( err );
436     }
437   }
438
439   // Orient loaded grids of cube sides along axis of the unitary cube coord system
440   for ( int i = 0; i < 6; ++i )
441   {
442     bool reverse = false;
443     if ( helper.GetSubShapeOri( aShape.Oriented( TopAbs_FORWARD ),
444                                 aCubeSide[i]._quad->face ) == TopAbs_REVERSED )
445       reverse = !reverse;
446
447     if ( helper.GetSubShapeOri( aCubeSide[i]._quad->face.Oriented( TopAbs_FORWARD ),
448                                 aCubeSide[i]._quad->side[0]->Edge(0) ) == TopAbs_REVERSED )
449       reverse = !reverse;
450
451     if ( i == B_BOTTOM ||
452          i == B_LEFT   ||
453          i == B_BACK )
454       reverse = !reverse;
455
456     aCubeSide[i]._columns.resize( aCubeSide[i]._u2nodesMap.size() );
457
458     int iFwd = 0, iRev = aCubeSide[i]._columns.size()-1;
459     int* pi = reverse ? &iRev : &iFwd;
460     TParam2ColumnMap::iterator u2nn = aCubeSide[i]._u2nodesMap.begin();
461     for ( ; iFwd < aCubeSide[i]._columns.size(); --iRev, ++iFwd, ++u2nn )
462       aCubeSide[i]._columns[ *pi ].swap( u2nn->second );
463
464     aCubeSide[i]._u2nodesMap.clear();
465   }
466   
467   if ( proxymesh )
468     for ( int i = 0; i < 6; ++i )
469       for ( unsigned j = 0; j < aCubeSide[i]._columns.size(); ++j)
470         for ( unsigned k = 0; k < aCubeSide[i]._columns[j].size(); ++k)
471         {
472           const SMDS_MeshNode* & n = aCubeSide[i]._columns[j][k];
473           n = proxymesh->GetProxyNode( n );
474         }
475
476   // 4) Create internal nodes of the cube
477   // -------------------------------------
478
479   helper.SetSubShape( aShape );
480   helper.SetElementsOnShape(true);
481
482   // shortcuts to sides
483   _FaceGrid* fBottom = & aCubeSide[ B_BOTTOM ];
484   _FaceGrid* fRight  = & aCubeSide[ B_RIGHT  ];
485   _FaceGrid* fTop    = & aCubeSide[ B_TOP    ];
486   _FaceGrid* fLeft   = & aCubeSide[ B_LEFT   ];
487   _FaceGrid* fFront  = & aCubeSide[ B_FRONT  ];
488   _FaceGrid* fBack   = & aCubeSide[ B_BACK   ];
489
490   // cube size measured in nb of nodes
491   int x, xSize = fBottom->_columns.size() , X = xSize - 1;
492   int y, ySize = fLeft->_columns.size()   , Y = ySize - 1;
493   int z, zSize = fLeft->_columns[0].size(), Z = zSize - 1;
494
495   // columns of internal nodes "rising" from nodes of fBottom
496   _Indexer colIndex( xSize, ySize );
497   vector< vector< const SMDS_MeshNode* > > columns( colIndex.size() );
498
499   // fill node columns by front and back box sides
500   for ( x = 0; x < xSize; ++x ) {
501     vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
502     vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
503     column0.resize( zSize );
504     column1.resize( zSize );
505     for ( z = 0; z < zSize; ++z ) {
506       column0[ z ] = fFront->GetNode( x, z );
507       column1[ z ] = fBack ->GetNode( x, z );
508     }
509   }
510   // fill node columns by left and right box sides
511   for ( y = 1; y < ySize-1; ++y ) {
512     vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
513     vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
514     column0.resize( zSize );
515     column1.resize( zSize );
516     for ( z = 0; z < zSize; ++z ) {
517       column0[ z ] = fLeft ->GetNode( y, z );
518       column1[ z ] = fRight->GetNode( y, z );
519     }
520   }
521   // get nodes from top and bottom box sides
522   for ( x = 1; x < xSize-1; ++x ) {
523     for ( y = 1; y < ySize-1; ++y ) {
524       vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
525       column.resize( zSize );
526       column.front() = fBottom->GetNode( x, y );
527       column.back()  = fTop   ->GetNode( x, y );
528     }
529   }
530
531   // projection points of the internal node on cube sub-shapes by which
532   // coordinates of the internal node are computed
533   vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
534
535   // projections on vertices are constant
536   pointsOnShapes[ SMESH_Block::ID_V000 ] = fBottom->GetXYZ( 0, 0 );
537   pointsOnShapes[ SMESH_Block::ID_V100 ] = fBottom->GetXYZ( X, 0 );
538   pointsOnShapes[ SMESH_Block::ID_V010 ] = fBottom->GetXYZ( 0, Y );
539   pointsOnShapes[ SMESH_Block::ID_V110 ] = fBottom->GetXYZ( X, Y );
540   pointsOnShapes[ SMESH_Block::ID_V001 ] = fTop->GetXYZ( 0, 0 );
541   pointsOnShapes[ SMESH_Block::ID_V101 ] = fTop->GetXYZ( X, 0 );
542   pointsOnShapes[ SMESH_Block::ID_V011 ] = fTop->GetXYZ( 0, Y );
543   pointsOnShapes[ SMESH_Block::ID_V111 ] = fTop->GetXYZ( X, Y );
544
545   for ( x = 1; x < xSize-1; ++x )
546   {
547     gp_XYZ params; // normalized parameters of internal node within a unit box
548     params.SetCoord( 1, x / double(X) );
549     for ( y = 1; y < ySize-1; ++y )
550     {
551       params.SetCoord( 2, y / double(Y) );
552       // a column to fill in during z loop
553       vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
554       // projection points on horizontal edges
555       pointsOnShapes[ SMESH_Block::ID_Ex00 ] = fBottom->GetXYZ( x, 0 );
556       pointsOnShapes[ SMESH_Block::ID_Ex10 ] = fBottom->GetXYZ( x, Y );
557       pointsOnShapes[ SMESH_Block::ID_E0y0 ] = fBottom->GetXYZ( 0, y );
558       pointsOnShapes[ SMESH_Block::ID_E1y0 ] = fBottom->GetXYZ( X, y );
559       pointsOnShapes[ SMESH_Block::ID_Ex01 ] = fTop->GetXYZ( x, 0 );
560       pointsOnShapes[ SMESH_Block::ID_Ex11 ] = fTop->GetXYZ( x, Y );
561       pointsOnShapes[ SMESH_Block::ID_E0y1 ] = fTop->GetXYZ( 0, y );
562       pointsOnShapes[ SMESH_Block::ID_E1y1 ] = fTop->GetXYZ( X, y );
563       // projection points on horizontal faces
564       pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = fBottom->GetXYZ( x, y );
565       pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = fTop   ->GetXYZ( x, y );
566       for ( z = 1; z < zSize-1; ++z ) // z loop
567       {
568         params.SetCoord( 3, z / double(Z) );
569         // projection points on vertical edges
570         pointsOnShapes[ SMESH_Block::ID_E00z ] = fFront->GetXYZ( 0, z );    
571         pointsOnShapes[ SMESH_Block::ID_E10z ] = fFront->GetXYZ( X, z );    
572         pointsOnShapes[ SMESH_Block::ID_E01z ] = fBack->GetXYZ( 0, z );    
573         pointsOnShapes[ SMESH_Block::ID_E11z ] = fBack->GetXYZ( X, z );
574         // projection points on vertical faces
575         pointsOnShapes[ SMESH_Block::ID_Fx0z ] = fFront->GetXYZ( x, z );    
576         pointsOnShapes[ SMESH_Block::ID_Fx1z ] = fBack ->GetXYZ( x, z );    
577         pointsOnShapes[ SMESH_Block::ID_F0yz ] = fLeft ->GetXYZ( y, z );    
578         pointsOnShapes[ SMESH_Block::ID_F1yz ] = fRight->GetXYZ( y, z );
579
580         // compute internal node coordinates
581         gp_XYZ coords;
582         SMESH_Block::ShellPoint( params, pointsOnShapes, coords );
583         column[ z ] = helper.AddNode( coords.X(), coords.Y(), coords.Z() );
584
585       }
586     }
587   }
588
589   // side data no more needed, free memory
590   for ( int i = 0; i < 6; ++i )
591     aCubeSide[i]._columns.clear();
592
593   // 5) Create hexahedrons
594   // ---------------------
595
596   for ( x = 0; x < xSize-1; ++x ) {
597     for ( y = 0; y < ySize-1; ++y ) {
598       vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
599       vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
600       vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
601       vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
602       for ( z = 0; z < zSize-1; ++z )
603       {
604         // bottom face normal of a hexa mush point outside the volume
605         helper.AddVolume(col00[z],   col01[z],   col11[z],   col10[z],
606                          col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
607       }
608     }
609   }
610   return true;
611 }
612
613 //=============================================================================
614 /*!
615  *  Evaluate
616  */
617 //=============================================================================
618
619 bool StdMeshers_Hexa_3D::Evaluate(SMESH_Mesh & aMesh,
620                                   const TopoDS_Shape & aShape,
621                                   MapShapeNbElems& aResMap)
622 {
623   vector < SMESH_subMesh * >meshFaces;
624   TopTools_SequenceOfShape aFaces;
625   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
626     aFaces.Append(exp.Current());
627     SMESH_subMesh *aSubMesh = aMesh.GetSubMeshContaining(exp.Current());
628     ASSERT(aSubMesh);
629     meshFaces.push_back(aSubMesh);
630   }
631   if (meshFaces.size() != 6) {
632     //return error(COMPERR_BAD_SHAPE, TComm(meshFaces.size())<<" instead of 6 faces in a block");
633     static StdMeshers_CompositeHexa_3D compositeHexa(-10, 0, aMesh.GetGen());
634     return compositeHexa.Evaluate(aMesh, aShape, aResMap);
635   }
636   
637   int i = 0;
638   for(; i<6; i++) {
639     //TopoDS_Shape aFace = meshFaces[i]->GetSubShape();
640     TopoDS_Shape aFace = aFaces.Value(i+1);
641     SMESH_Algo *algo = _gen->GetAlgo(aMesh, aFace);
642     if( !algo ) {
643       std::vector<int> aResVec(SMDSEntity_Last);
644       for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
645       SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
646       aResMap.insert(std::make_pair(sm,aResVec));
647       SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
648       smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
649       return false;
650     }
651     string algoName = algo->GetName();
652     bool isAllQuad = false;
653     if (algoName == "Quadrangle_2D") {
654       MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i]);
655       if( anIt == aResMap.end() ) continue;
656       std::vector<int> aVec = (*anIt).second;
657       int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
658       if( nbtri == 0 )
659         isAllQuad = true;
660     }
661     if ( ! isAllQuad ) {
662       return EvaluatePentahedralMesh(aMesh, aShape, aResMap);
663     }
664   }
665   
666   // find number of 1d elems for 1 face
667   int nb1d = 0;
668   TopTools_MapOfShape Edges1;
669   bool IsQuadratic = false;
670   bool IsFirst = true;
671   for (TopExp_Explorer exp(aFaces.Value(1), TopAbs_EDGE); exp.More(); exp.Next()) {
672     Edges1.Add(exp.Current());
673     SMESH_subMesh *sm = aMesh.GetSubMesh(exp.Current());
674     if( sm ) {
675       MapShapeNbElemsItr anIt = aResMap.find(sm);
676       if( anIt == aResMap.end() ) continue;
677       std::vector<int> aVec = (*anIt).second;
678       nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
679       if(IsFirst) {
680         IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
681         IsFirst = false;
682       }
683     }
684   }
685   // find face opposite to 1 face
686   int OppNum = 0;
687   for(i=2; i<=6; i++) {
688     bool IsOpposite = true;
689     for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
690       if( Edges1.Contains(exp.Current()) ) {
691         IsOpposite = false;
692         break;
693       }
694     }
695     if(IsOpposite) {
696       OppNum = i;
697       break;
698     }
699   }
700   // find number of 2d elems on side faces
701   int nb2d = 0;
702   for(i=2; i<=6; i++) {
703     if( i == OppNum ) continue;
704     MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
705     if( anIt == aResMap.end() ) continue;
706     std::vector<int> aVec = (*anIt).second;
707     nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
708   }
709   
710   MapShapeNbElemsItr anIt = aResMap.find( meshFaces[0] );
711   std::vector<int> aVec = (*anIt).second;
712   int nb2d_face0 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
713   int nb0d_face0 = aVec[SMDSEntity_Node];
714
715   std::vector<int> aResVec(SMDSEntity_Last);
716   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
717   if(IsQuadratic) {
718     aResVec[SMDSEntity_Quad_Hexa] = nb2d_face0 * ( nb2d/nb1d );
719     int nb1d_face0_int = ( nb2d_face0*4 - nb1d ) / 2;
720     aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 ) - nb1d_face0_int * nb2d/nb1d;
721   }
722   else {
723     aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
724     aResVec[SMDSEntity_Hexa] = nb2d_face0 * ( nb2d/nb1d );
725   }
726   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
727   aResMap.insert(std::make_pair(sm,aResVec));
728
729   return true;
730 }
731
732 //================================================================================
733 /*!
734  * \brief Computes hexahedral mesh from 2D mesh of block
735  */
736 //================================================================================
737
738 bool StdMeshers_Hexa_3D::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
739 {
740   static StdMeshers_HexaFromSkin_3D * algo = 0;
741   if ( !algo ) {
742     SMESH_Gen* gen = aMesh.GetGen();
743     algo = new StdMeshers_HexaFromSkin_3D( gen->GetANewId(), 0, gen );
744   }
745   algo->InitComputeError();
746   algo->Compute( aMesh, aHelper );
747   return error( algo->GetComputeError());
748 }
749
750 //=======================================================================
751 //function : ComputePentahedralMesh
752 //purpose  : 
753 //=======================================================================
754
755 SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &          aMesh,
756                                              const TopoDS_Shape &  aShape,
757                                              SMESH_ProxyMesh*      proxyMesh)
758 {
759   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
760   if ( proxyMesh )
761   {
762     err->myName = COMPERR_BAD_INPUT_MESH;
763     err->myComment = "Can't build pentahedral mesh on viscous layers";
764     return err;
765   }
766   bool bOK;
767   StdMeshers_Penta_3D anAlgo;
768   //
769   bOK=anAlgo.Compute(aMesh, aShape);
770   //
771   err = anAlgo.GetComputeError();
772   //
773   if ( !bOK && anAlgo.ErrorStatus() == 5 )
774   {
775     static StdMeshers_Prism_3D * aPrism3D = 0;
776     if ( !aPrism3D ) {
777       SMESH_Gen* gen = aMesh.GetGen();
778       aPrism3D = new StdMeshers_Prism_3D( gen->GetANewId(), 0, gen );
779     }
780     SMESH_Hypothesis::Hypothesis_Status aStatus;
781     if ( aPrism3D->CheckHypothesis( aMesh, aShape, aStatus ) ) {
782       aPrism3D->InitComputeError();
783       bOK = aPrism3D->Compute( aMesh, aShape );
784       err = aPrism3D->GetComputeError();
785     }
786   }
787   return err;
788 }
789
790
791 //=======================================================================
792 //function : EvaluatePentahedralMesh
793 //purpose  : 
794 //=======================================================================
795
796 bool EvaluatePentahedralMesh(SMESH_Mesh & aMesh,
797                              const TopoDS_Shape & aShape,
798                              MapShapeNbElems& aResMap)
799 {
800   StdMeshers_Penta_3D anAlgo;
801   bool bOK = anAlgo.Evaluate(aMesh, aShape, aResMap);
802
803   //err = anAlgo.GetComputeError();
804   //if ( !bOK && anAlgo.ErrorStatus() == 5 )
805   if( !bOK ) {
806     static StdMeshers_Prism_3D * aPrism3D = 0;
807     if ( !aPrism3D ) {
808       SMESH_Gen* gen = aMesh.GetGen();
809       aPrism3D = new StdMeshers_Prism_3D( gen->GetANewId(), 0, gen );
810     }
811     SMESH_Hypothesis::Hypothesis_Status aStatus;
812     if ( aPrism3D->CheckHypothesis( aMesh, aShape, aStatus ) ) {
813       return aPrism3D->Evaluate(aMesh, aShape, aResMap);
814     }
815   }
816
817   return bOK;
818 }