Salome HOME
IPAL52924: Edit Group Standalone: Filters with Source: Mesh, Initial selection does...
[modules/smesh.git] / src / StdMeshers / StdMeshers_CompositeHexa_3D.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SMESH SMESH : implementaion of SMESH idl descriptions
21 // File      : StdMeshers_CompositeHexa_3D.cxx
22 // Module    : SMESH
23 // Created   : Tue Nov 25 11:04:59 2008
24 // Author    : Edward AGAPOV (eap)
25
26 #include "StdMeshers_CompositeHexa_3D.hxx"
27
28 #include "SMDS_Mesh.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESH_Block.hxx"
32 #include "SMESH_Comment.hxx"
33 #include "SMESH_ComputeError.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MeshAlgos.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_subMesh.hxx"
38
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRep_Tool.hxx>
41 #include <Standard_ErrorHandler.hxx>
42 #include <Standard_Failure.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_MapIteratorOfMapOfShape.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_SequenceOfShape.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Vertex.hxx>
52 #include <gp_Pnt.hxx>
53 #include <gp_Pnt2d.hxx>
54 #include <gp_Vec.hxx>
55 #include <gp_XYZ.hxx>
56
57 #include <list>
58 #include <set>
59 #include <vector>
60
61
62 #ifdef _DEBUG_
63 // #define DEB_FACES
64 // #define DEB_GRID
65 // #define DUMP_VERT(msg,V) { TopoDS_Vertex v = V; gp_Pnt p = BRep_Tool::Pnt(v); cout << msg << "( "<< p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl; }
66 #endif
67
68 #ifndef DUMP_VERT
69 #define DUMP_VERT(msg,v)
70 #endif
71
72 //================================================================================
73 // text for message about an internal error
74 #define ERR_LI(txt) SMESH_Comment(txt) << ":" << __LINE__
75
76 // order corresponds to right order of edges in CASCADE face
77 enum EQuadSides{ Q_BOTTOM=0, Q_RIGHT, Q_TOP, Q_LEFT,   Q_CHILD, Q_PARENT };
78
79 enum EBoxSides{ B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, B_UNDEFINED };
80
81 //================================================================================
82 /*!
83  * \brief Convertor of a pair of integers to a sole index
84  */
85 struct _Indexer
86 {
87   int _xSize, _ySize;
88   _Indexer( int xSize, int ySize ): _xSize(xSize), _ySize(ySize) {}
89   int size() const { return _xSize * _ySize; }
90   int operator()(const int x, const int y) const { return y * _xSize + x; }
91 };
92
93 //================================================================================
94 /*!
95  * \brief Wrapper of a composite or an ordinary edge.
96  */
97 class _FaceSide
98 {
99 public:
100   _FaceSide(const _FaceSide& other);
101   _FaceSide(const TopoDS_Edge& edge=TopoDS_Edge());
102   _FaceSide(const list<TopoDS_Edge>& edges);
103   _FaceSide* GetSide(const int i);
104   const _FaceSide* GetSide(const int i) const;
105   int size() const { return myChildren.size(); }
106   int NbVertices() const;
107   int NbCommonVertices( const TopTools_MapOfShape& VV ) const;
108   TopoDS_Vertex FirstVertex() const;
109   TopoDS_Vertex LastVertex() const;
110   TopoDS_Vertex Vertex(int i) const;
111   TopoDS_Edge   Edge(int i) const;
112   bool Contain( const _FaceSide& side, int* which=0 ) const;
113   bool Contain( const TopoDS_Vertex& vertex ) const;
114   void AppendSide( const _FaceSide& side );
115   void SetBottomSide( int i );
116   int GetNbSegments(SMESH_Mesh& mesh) const;
117   bool StoreNodes(SMESH_Mesh& mesh, vector<const SMDS_MeshNode*>& myGrid, bool reverse );
118   void SetID(EQuadSides id) { myID = id; }
119   static inline const TopoDS_TShape* ptr(const TopoDS_Shape& theShape)
120   { return theShape.TShape().operator->(); }
121   void Dump() const;
122
123 private:
124
125
126   TopoDS_Edge       myEdge;
127   list< _FaceSide > myChildren;
128   int               myNbChildren;
129
130   TopTools_MapOfShape myVertices;
131
132   EQuadSides        myID; // debug
133 };
134 //================================================================================
135 /*!
136  * \brief Class corresponding to a meshed composite face of a box.
137  *        Provides simplified access to it's sub-mesh data.
138  */
139 class _QuadFaceGrid
140 {
141   typedef list< _QuadFaceGrid > TChildren;
142 public:
143   _QuadFaceGrid();
144
145 public: //** Methods to find and orient faces of 6 sides of the box **//
146   
147   //!< initialization
148   bool Init(const TopoDS_Face& f, SMESH_Mesh& mesh );
149
150   //!< try to unite self with other face
151   bool AddContinuousFace( const _QuadFaceGrid& f, const TopTools_MapOfShape& internalEdges );
152
153   //!< Try to set the side as bottom hirizontal side
154   bool SetBottomSide(const _FaceSide& side, int* sideIndex=0);
155
156   //!< Return face adjacent to zero-based i-th side of this face
157   _QuadFaceGrid* FindAdjacentForSide(int i, list<_QuadFaceGrid>& faces, EBoxSides id) const;
158
159   //!< Reverse edges in order to have the bottom edge going along axes of the unit box
160   void ReverseEdges(/*int e1, int e2*/);
161
162   bool IsComplex() const { return !myChildren.empty(); }
163
164   int NbChildren() const { return myChildren.size(); }
165
166   typedef SMDS_SetIterator< const _QuadFaceGrid&,
167                             TChildren::const_iterator,
168                             SMDS::SimpleAccessor<const _QuadFaceGrid&,TChildren::const_iterator>,
169                             SMDS::PassAllValueFilter<_QuadFaceGrid> >
170     TChildIterator;
171
172   TChildIterator GetChildren() const
173   { return TChildIterator( myChildren.begin(), myChildren.end()); }
174
175 public: //** Loading and access to mesh **//
176
177   //!< Load nodes of a mesh
178   bool LoadGrid( SMESH_Mesh& mesh );
179
180   //!< Return number of segments on the hirizontal sides
181   int GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
182
183   //!< Return number of segments on the vertical sides
184   int GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
185
186   //!< Return edge on the hirizontal bottom sides
187   int GetHoriEdges(vector<TopoDS_Edge> & edges) const;
188
189   //!< Return a node by its position
190   const SMDS_MeshNode* GetNode(int iHori, int iVert) const;
191
192   //!< Return node coordinates by its position
193   gp_XYZ GetXYZ(int iHori, int iVert) const;
194
195 public: //** Access to member fields **//
196
197   //!< Return i-th face side (0<i<4)
198   const _FaceSide& GetSide(int i) const;
199
200   //!< Return it's face, NULL if it is composite
201   TopoDS_Face GetFace() const { return myFace; }
202
203   //!< Return normal to the face at vertex v
204   bool GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const;
205
206   SMESH_ComputeErrorPtr GetError() const { return myError; }
207
208   void SetID(EBoxSides id) { myID = id; }
209
210   void DumpGrid() const;
211
212   void DumpVertices() const;
213
214 private:
215
216   bool error(const std::string& text, int code = COMPERR_ALGO_FAILED)
217   { myError = SMESH_ComputeError::New( code, text ); return false; }
218
219   bool error(const SMESH_ComputeErrorPtr& err)
220   { myError = err; return ( !myError || myError->IsOK() ); }
221
222   bool loadCompositeGrid(SMESH_Mesh& mesh);
223
224   bool fillGrid(SMESH_Mesh&                    theMesh,
225                 vector<const SMDS_MeshNode*> & theGrid,
226                 const _Indexer&                theIndexer,
227                 int                            theX,
228                 int                            theY);
229
230   bool locateChildren();
231
232   void setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers );
233
234   TopoDS_Face myFace;
235   _FaceSide   mySides;
236   bool        myReverse;
237
238   TChildren   myChildren;
239
240   _QuadFaceGrid* myLeftBottomChild;
241   _QuadFaceGrid* myRightBrother;
242   _QuadFaceGrid* myUpBrother;
243
244   _Indexer    myIndexer;
245   vector<const SMDS_MeshNode*>  myGrid;
246
247   SMESH_ComputeErrorPtr         myError;
248
249   EBoxSides   myID; // debug
250 };
251
252 //================================================================================
253 /*!
254  * \brief Constructor
255  */
256 //================================================================================
257
258 StdMeshers_CompositeHexa_3D::StdMeshers_CompositeHexa_3D(int hypId, int studyId, SMESH_Gen* gen)
259   :SMESH_3D_Algo(hypId, studyId, gen)
260 {
261   _name = "CompositeHexa_3D";
262   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit /shape type
263 }
264
265 //================================================================================
266 /*!
267  * \brief always return true
268  */
269 //================================================================================
270
271 bool StdMeshers_CompositeHexa_3D::CheckHypothesis(SMESH_Mesh&         aMesh,
272                                                   const TopoDS_Shape& aShape,
273                                                   Hypothesis_Status&  aStatus)
274 {
275   aStatus = HYP_OK;
276   return true;
277 }
278
279 namespace
280 {
281
282   //================================================================================
283   /*!
284    * \brief Checks structure of a quadrangular mesh at the common VERTEX of two EDGEs.
285    *        Returns true if there are two quadrangles near the VERTEX.
286    */
287   //================================================================================
288
289   bool isContinuousMesh(TopoDS_Edge        E1,
290                         TopoDS_Edge        E2,
291                         const TopoDS_Face& F,
292                         const SMESH_Mesh&  mesh)
293   {
294     if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
295       E1.Orientation( TopAbs_FORWARD );
296     if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
297       E2.Orientation( TopAbs_FORWARD );
298
299     TopoDS_Vertex V;
300     if ( !TopExp::CommonVertex( E1, E2, V )) return false;
301
302     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
303     if ( !n ) return false;
304
305     SMESHDS_SubMesh* sm = mesh.GetSubMeshContaining( F )->GetSubMeshDS();
306     if ( !sm ) return false;
307
308     int nbQuads = 0;
309     SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
310     while ( fIt->more() )
311     {
312       const SMDS_MeshElement* f = fIt->next();
313       if ( !sm->Contains( f )) continue;
314
315       if ( f->NbCornerNodes() == 4 )
316         ++nbQuads;
317       else
318         return false;
319     }
320     return nbQuads == 2;
321   }
322
323   //================================================================================
324   /*!
325    * \brief Finds VERTEXes located at block corners
326    */
327   //================================================================================
328
329   void getBlockCorners( SMESH_Mesh&          mesh,
330                         const TopoDS_Shape&  shape,
331                         TopTools_MapOfShape& cornerVV)
332   {
333     set<int> faceIDs; // ids of FACEs in the shape
334     TopExp_Explorer exp;
335     for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
336       faceIDs.insert( mesh.GetMeshDS()->ShapeToIndex( exp.Current() ));
337
338     TopTools_MapOfShape checkedVV;
339     for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
340     {
341       TopoDS_Vertex V = TopoDS::Vertex( exp.Current() );
342       if ( !checkedVV.Add( V )) continue;
343
344       const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
345       if ( !n ) continue;
346
347       int nbQuads = 0;
348       SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
349       while ( fIt->more() )
350       {
351         const SMDS_MeshElement* f = fIt->next();
352         if ( !faceIDs.count( f->getshapeId() )) continue;
353
354         if ( f->NbCornerNodes() == 4 )
355           ++nbQuads;
356         else
357           nbQuads = 100;
358       }
359       if ( nbQuads == 3 )
360         cornerVV.Add( V );
361     }
362   }
363
364   //================================================================================
365   /*!
366    * \brief Return EDGEs dividing one box side
367    */
368   //================================================================================
369
370   bool getInternalEdges( SMESH_Mesh&                mesh,
371                          const TopoDS_Shape&        shape,
372                          const TopTools_MapOfShape& cornerVV,
373                          TopTools_MapOfShape&       internEE)
374   {
375     TopTools_IndexedMapOfShape subEE, subFF;
376     TopExp::MapShapes( shape, TopAbs_EDGE, subEE );
377     TopExp::MapShapes( shape, TopAbs_FACE, subFF );
378
379     TopoDS_Vertex VV[2];
380     TopTools_MapOfShape subChecked/*, ridgeEE*/;
381     TopTools_MapIteratorOfMapOfShape vIt( cornerVV );
382     for ( ; vIt.More(); vIt.Next() )
383     {
384       TopoDS_Shape V0 = vIt.Key();
385       // walk from one corner VERTEX to another along ridge EDGEs
386       PShapeIteratorPtr riIt = SMESH_MesherHelper::GetAncestors( V0, mesh, TopAbs_EDGE );
387       while ( const TopoDS_Shape* riE = riIt->next() )
388       {
389         if ( !subEE.Contains( *riE ) || !subChecked.Add( *riE ))
390           continue;
391         TopoDS_Edge ridgeE = TopoDS::Edge( *riE );
392         while ( !ridgeE.IsNull() )
393         {
394           TopExp::Vertices( ridgeE, VV[0], VV[1] );
395           TopoDS_Shape V1 = VV[ V0.IsSame( VV[0] )];
396           if ( cornerVV.Contains( V1 ) )
397             break; // ridgeE reached a corner VERTEX
398
399           // detect internal EDGEs among those sharing V1. There can be 2, 3 or 4 EDGEs and
400           // number of internal EDGEs is N-2
401           TopoDS_Shape nextRidgeE;
402           PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
403           while ( const TopoDS_Shape* E = eIt->next() )
404           {
405             if ( E->IsSame( ridgeE ) || !subEE.Contains( *E ) || !subChecked.Add( *E ))
406               continue;
407             // look for FACEs sharing both E and ridgeE
408             PShapeIteratorPtr fIt = SMESH_MesherHelper::GetAncestors( *E, mesh, TopAbs_FACE );
409             while ( const TopoDS_Shape* F = fIt->next() )
410             {
411               if ( !SMESH_MesherHelper::IsSubShape( ridgeE, *F ))
412                 continue;
413               if ( isContinuousMesh( ridgeE, TopoDS::Edge( *E ), TopoDS::Face( *F ), mesh ))
414               {
415                 nextRidgeE = *E;
416               }
417               else
418               {
419                 internEE.Add( *E );
420               }
421               break;
422             }
423           }
424           // look for the next ridge EDGE ending at V1
425           if ( nextRidgeE.IsNull() )
426           {
427             eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
428             while ( const TopoDS_Shape* E = eIt->next() )
429               if ( !ridgeE.IsSame( *E ) && !internEE.Contains( *E ) && subEE.Contains( *E ))
430               {
431                 nextRidgeE = *E;
432                 break;
433               }
434           }
435           ridgeE = TopoDS::Edge( nextRidgeE );
436           V0 = V1;
437
438           if ( ridgeE.IsNull() )
439             return false;
440         } // check EDGEs around the last VERTEX of ridgeE 
441       } // loop on ridge EDGEs around a corner VERTEX
442     } // loop on on corner VERTEXes
443
444     return true;
445   } // getInternalEdges()
446 } // namespace
447
448 //================================================================================
449 /*!
450  * \brief Tries to find 6 sides of a box
451  */
452 //================================================================================
453
454 bool StdMeshers_CompositeHexa_3D::findBoxFaces( const TopoDS_Shape&    shape,
455                                                 list< _QuadFaceGrid >& boxFaces,
456                                                 SMESH_Mesh&            mesh,
457                                                 _QuadFaceGrid * &      fBottom,
458                                                 _QuadFaceGrid * &      fTop,
459                                                 _QuadFaceGrid * &      fFront,
460                                                 _QuadFaceGrid * &      fBack,
461                                                 _QuadFaceGrid * &      fLeft,
462                                                 _QuadFaceGrid * &      fRight)
463 {
464   TopTools_MapOfShape cornerVertices;
465   getBlockCorners( mesh, shape, cornerVertices );
466   if ( cornerVertices.Extent() != 8 )
467     return error( COMPERR_BAD_INPUT_MESH, "Can't find 8 corners of a block by 2D mesh" );
468   TopTools_MapOfShape internalEdges;
469   if ( !getInternalEdges( mesh, shape, cornerVertices, internalEdges ))
470     return error( COMPERR_BAD_INPUT_MESH, "2D mesh is not suitable for i,j,k hexa meshing" );
471
472   list< _QuadFaceGrid >::iterator boxFace;
473   TopExp_Explorer exp;
474   int nbFaces = 0;
475   for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFaces )
476   {
477     _QuadFaceGrid f;
478     if ( !f.Init( TopoDS::Face( exp.Current() ), mesh ))
479       return error (COMPERR_BAD_SHAPE);
480
481     _QuadFaceGrid* prevContinuous = 0;
482     for ( boxFace = boxFaces.begin(); boxFace != boxFaces.end(); ++boxFace )
483     {
484       if ( prevContinuous )
485       {
486         if ( prevContinuous->AddContinuousFace( *boxFace, internalEdges ))
487           boxFace = --boxFaces.erase( boxFace );
488       }
489       else if ( boxFace->AddContinuousFace( f, internalEdges ))
490       {
491         prevContinuous = & (*boxFace);
492       }
493     }
494     if ( !prevContinuous )
495       boxFaces.push_back( f );
496   }
497   // Check what we have
498   if ( boxFaces.size() != 6 && nbFaces != 6)
499     return error
500       (COMPERR_BAD_SHAPE,
501        SMESH_Comment("Can't find 6 sides of a box. Number of found sides - ")<<boxFaces.size());
502
503   if ( boxFaces.size() != 6 && nbFaces == 6 ) { // strange ordinary box with continuous faces
504     boxFaces.resize( 6 );
505     boxFace = boxFaces.begin();
506     for ( exp.Init( shape, TopAbs_FACE); exp.More(); exp.Next(), ++boxFace )
507       boxFace->Init( TopoDS::Face( exp.Current() ), mesh );
508   }
509   // ----------------------------------------
510   // Find out position of faces within a box
511   // ----------------------------------------
512   // start from a bottom face
513   fBottom = &boxFaces.front();
514   fBottom->SetID( B_BOTTOM );
515   // find vertical faces
516   fFront = fBottom->FindAdjacentForSide( Q_BOTTOM, boxFaces, B_FRONT );
517   fLeft  = fBottom->FindAdjacentForSide( Q_RIGHT,  boxFaces, B_LEFT  );
518   fBack  = fBottom->FindAdjacentForSide( Q_TOP,    boxFaces, B_BACK  );
519   fRight = fBottom->FindAdjacentForSide( Q_LEFT,   boxFaces, B_RIGHT );
520   // check the found
521   if ( !fFront || !fBack || !fLeft || !fRight )
522     return error(COMPERR_BAD_SHAPE);
523   // find a top face
524   fTop = 0;
525   for ( boxFace = ++boxFaces.begin(); boxFace != boxFaces.end() && !fTop; ++boxFace )
526   {
527     fTop = & (*boxFace);
528     fTop->SetID( B_TOP );
529     if ( fTop==fFront || fTop==fLeft || fTop==fBack || fTop==fRight )
530       fTop = 0;
531   }
532   // set bottom of the top side
533   if ( !fTop->SetBottomSide( fFront->GetSide( Q_TOP ) )) {
534     if ( !fFront->IsComplex() )
535       return error( ERR_LI("Error in StdMeshers_CompositeHexa_3D::Compute()"));
536     else {
537       _QuadFaceGrid::TChildIterator chIt = fFront->GetChildren();
538       while ( chIt.more() ) {
539         const _QuadFaceGrid& frontChild = chIt.next();
540         if ( fTop->SetBottomSide( frontChild.GetSide( Q_TOP )))
541           break;
542       }
543     }
544   }
545   if ( !fTop )
546     return error(COMPERR_BAD_SHAPE);
547
548   // orient bottom egde of faces along axes of the unit box
549   fBottom->ReverseEdges();
550   fBack  ->ReverseEdges();
551   fLeft  ->ReverseEdges();
552
553   return true;
554 }
555
556 //================================================================================
557 /*!
558  * \brief Computes hexahedral mesh on a box with composite sides
559  *  \param aMesh - mesh to compute
560  *  \param aShape - shape to mesh
561  *  \retval bool - succes sign
562  */
563 //================================================================================
564
565 bool StdMeshers_CompositeHexa_3D::Compute(SMESH_Mesh&         theMesh,
566                                           const TopoDS_Shape& theShape)
567 {
568   SMESH_MesherHelper helper( theMesh );
569   _quadraticMesh = helper.IsQuadraticSubMesh( theShape );
570   helper.SetElementsOnShape( true );
571
572   // -------------------------
573   // Try to find 6 side faces
574   // -------------------------
575   list< _QuadFaceGrid > boxFaceContainer;
576   _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
577   if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
578                        fBottom, fTop, fFront, fBack, fLeft, fRight))
579     return false;
580
581   // ------------------------------------------
582   // Fill columns of nodes with existing nodes
583   // ------------------------------------------
584
585   // let faces load their grids
586   if ( !fBottom->LoadGrid( theMesh )) return error( fBottom->GetError() );
587   if ( !fBack  ->LoadGrid( theMesh )) return error( fBack  ->GetError() );
588   if ( !fLeft  ->LoadGrid( theMesh )) return error( fLeft  ->GetError() );
589   if ( !fFront ->LoadGrid( theMesh )) return error( fFront ->GetError() );
590   if ( !fRight ->LoadGrid( theMesh )) return error( fRight ->GetError() );
591   if ( !fTop   ->LoadGrid( theMesh )) return error( fTop   ->GetError() );
592
593   int x, xSize = fBottom->GetNbHoriSegments(theMesh) + 1, X = xSize - 1;
594   int y, ySize = fBottom->GetNbVertSegments(theMesh) + 1, Y = ySize - 1;
595   int z, zSize = fFront ->GetNbVertSegments(theMesh) + 1, Z = zSize - 1;
596   _Indexer colIndex( xSize, ySize );
597   vector< vector< const SMDS_MeshNode* > > columns( colIndex.size() );
598
599   // fill node columns by front and back box sides
600   for ( x = 0; x < xSize; ++x ) {
601     vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
602     vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
603     column0.resize( zSize );
604     column1.resize( zSize );
605     for ( z = 0; z < zSize; ++z ) {
606       column0[ z ] = fFront->GetNode( x, z );
607       column1[ z ] = fBack ->GetNode( x, z );
608     }
609   }
610   // fill node columns by left and right box sides
611   for ( y = 1; y < ySize-1; ++y ) {
612     vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
613     vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
614     column0.resize( zSize );
615     column1.resize( zSize );
616     for ( z = 0; z < zSize; ++z ) {
617       column0[ z ] = fLeft ->GetNode( y, z );
618       column1[ z ] = fRight->GetNode( y, z );
619     }
620   }
621   // get nodes from top and bottom box sides
622   for ( x = 1; x < xSize-1; ++x ) {
623     for ( y = 1; y < ySize-1; ++y ) {
624       vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
625       column.resize( zSize );
626       column.front() = fBottom->GetNode( x, y );
627       column.back()  = fTop   ->GetNode( x, y );
628     }
629   }
630
631   // ----------------------------
632   // Add internal nodes of a box
633   // ----------------------------
634   // projection points of internal nodes on box sub-shapes by which
635   // coordinates of internal nodes are computed
636   vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
637
638   // projections on vertices are constant
639   pointsOnShapes[ SMESH_Block::ID_V000 ] = fBottom->GetXYZ( 0, 0 );
640   pointsOnShapes[ SMESH_Block::ID_V100 ] = fBottom->GetXYZ( X, 0 );
641   pointsOnShapes[ SMESH_Block::ID_V010 ] = fBottom->GetXYZ( 0, Y );
642   pointsOnShapes[ SMESH_Block::ID_V110 ] = fBottom->GetXYZ( X, Y );
643   pointsOnShapes[ SMESH_Block::ID_V001 ] = fTop->GetXYZ( 0, 0 );
644   pointsOnShapes[ SMESH_Block::ID_V101 ] = fTop->GetXYZ( X, 0 );
645   pointsOnShapes[ SMESH_Block::ID_V011 ] = fTop->GetXYZ( 0, Y );
646   pointsOnShapes[ SMESH_Block::ID_V111 ] = fTop->GetXYZ( X, Y );
647
648   for ( x = 1; x < xSize-1; ++x )
649   {
650     gp_XYZ params; // normalized parameters of internal node within a unit box
651     params.SetCoord( 1, x / double(X) );
652     for ( y = 1; y < ySize-1; ++y )
653     {
654       params.SetCoord( 2, y / double(Y) );
655       // column to fill during z loop
656       vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
657       // points projections on horizontal edges
658       pointsOnShapes[ SMESH_Block::ID_Ex00 ] = fBottom->GetXYZ( x, 0 );
659       pointsOnShapes[ SMESH_Block::ID_Ex10 ] = fBottom->GetXYZ( x, Y );
660       pointsOnShapes[ SMESH_Block::ID_E0y0 ] = fBottom->GetXYZ( 0, y );
661       pointsOnShapes[ SMESH_Block::ID_E1y0 ] = fBottom->GetXYZ( X, y );
662       pointsOnShapes[ SMESH_Block::ID_Ex01 ] = fTop->GetXYZ( x, 0 );
663       pointsOnShapes[ SMESH_Block::ID_Ex11 ] = fTop->GetXYZ( x, Y );
664       pointsOnShapes[ SMESH_Block::ID_E0y1 ] = fTop->GetXYZ( 0, y );
665       pointsOnShapes[ SMESH_Block::ID_E1y1 ] = fTop->GetXYZ( X, y );
666       // points projections on horizontal faces
667       pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = fBottom->GetXYZ( x, y );
668       pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = fTop   ->GetXYZ( x, y );
669       for ( z = 1; z < zSize-1; ++z ) // z loop
670       {
671         params.SetCoord( 3, z / double(Z) );
672         // point projections on vertical edges
673         pointsOnShapes[ SMESH_Block::ID_E00z ] = fFront->GetXYZ( 0, z );    
674         pointsOnShapes[ SMESH_Block::ID_E10z ] = fFront->GetXYZ( X, z );    
675         pointsOnShapes[ SMESH_Block::ID_E01z ] = fBack->GetXYZ( 0, z );    
676         pointsOnShapes[ SMESH_Block::ID_E11z ] = fBack->GetXYZ( X, z );
677         // point projections on vertical faces
678         pointsOnShapes[ SMESH_Block::ID_Fx0z ] = fFront->GetXYZ( x, z );    
679         pointsOnShapes[ SMESH_Block::ID_Fx1z ] = fBack ->GetXYZ( x, z );    
680         pointsOnShapes[ SMESH_Block::ID_F0yz ] = fLeft ->GetXYZ( y, z );    
681         pointsOnShapes[ SMESH_Block::ID_F1yz ] = fRight->GetXYZ( y, z );
682
683         // compute internal node coordinates
684         gp_XYZ coords;
685         SMESH_Block::ShellPoint( params, pointsOnShapes, coords );
686         column[ z ] = helper.AddNode( coords.X(), coords.Y(), coords.Z() );
687
688 #ifdef DEB_GRID
689         // debug
690         //cout << "----------------------------------------------------------------------"<<endl;
691         //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
692         //{
693         //  gp_XYZ p = pointsOnShapes[ id ];
694         //  SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
695         //}
696         //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
697         //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
698 #endif
699       }
700     }
701   }
702   // faces no more needed, free memory
703   boxFaceContainer.clear();
704
705   // ----------------
706   // Add hexahedrons
707   // ----------------
708   for ( x = 0; x < xSize-1; ++x ) {
709     for ( y = 0; y < ySize-1; ++y ) {
710       vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
711       vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
712       vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
713       vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
714       for ( z = 0; z < zSize-1; ++z )
715       {
716         // bottom face normal of a hexa mush point outside the volume
717         helper.AddVolume(col00[z],   col01[z],   col11[z],   col10[z],
718                          col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
719       }
720     }
721   }
722   return true;
723 }
724
725 //================================================================================
726 /*!
727  *  Evaluate
728  */
729 //================================================================================
730
731 bool StdMeshers_CompositeHexa_3D::Evaluate(SMESH_Mesh&         theMesh,
732                                            const TopoDS_Shape& theShape,
733                                            MapShapeNbElems&    aResMap)
734 {
735   // -------------------------
736   // Try to find 6 side faces
737   // -------------------------
738   list< _QuadFaceGrid > boxFaceContainer;
739   _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
740   if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
741                        fBottom, fTop, fFront, fBack, fLeft, fRight))
742     return false;
743
744   // Find a less complex side
745   _QuadFaceGrid * lessComplexSide = & boxFaceContainer.front();
746   list< _QuadFaceGrid >::iterator face = boxFaceContainer.begin();
747   for ( ++face; face != boxFaceContainer.end() && lessComplexSide->IsComplex(); ++face )
748     if ( face->NbChildren() < lessComplexSide->NbChildren() )
749       lessComplexSide = & *face;
750
751   // Get an 1D size of lessComplexSide
752   int nbSeg1 = 0;
753   vector<TopoDS_Edge> edges;
754   if ( !lessComplexSide->GetHoriEdges(edges) )
755     return false;
756   for ( size_t i = 0; i < edges.size(); ++i )
757   {
758     const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
759     if ( !nbElems.empty() )
760       nbSeg1 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
761   }
762
763   // Get an 1D size of a box side ortogonal to lessComplexSide
764   int nbSeg2 = 0;
765   _QuadFaceGrid* ortoSide =
766     lessComplexSide->FindAdjacentForSide( Q_LEFT, boxFaceContainer, B_UNDEFINED );
767   edges.clear();
768   if ( !ortoSide || !ortoSide->GetHoriEdges(edges) ) return false;
769   for ( size_t i = 0; i < edges.size(); ++i )
770   {
771     const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
772     if ( !nbElems.empty() )
773       nbSeg2 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
774   }
775
776   // Get an 2D size of a box side ortogonal to lessComplexSide
777   int nbFaces = 0, nbQuadFace = 0;
778   list< TopoDS_Face > sideFaces;
779   if ( ortoSide->IsComplex() )
780     for ( _QuadFaceGrid::TChildIterator child = ortoSide->GetChildren(); child.more(); )
781       sideFaces.push_back( child.next().GetFace() );
782   else
783     sideFaces.push_back( ortoSide->GetFace() );
784   //
785   list< TopoDS_Face >::iterator f = sideFaces.begin();
786   for ( ; f != sideFaces.end(); ++f )
787   {
788     const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( *f )];
789     if ( !nbElems.empty() )
790     {
791       nbFaces    = nbElems[ SMDSEntity_Quadrangle ];
792       nbQuadFace = nbElems[ SMDSEntity_Quad_Quadrangle ];
793     }
794   }
795
796   // Fill nb of elements
797   vector<int> aResVec(SMDSEntity_Last,0);
798   int nbSeg3 = ( nbFaces + nbQuadFace ) / nbSeg2;
799   aResVec[SMDSEntity_Node]       = (nbSeg1-1) * (nbSeg2-1) * (nbSeg3-1);
800   aResVec[SMDSEntity_Hexa]       = nbSeg1 * nbFaces;
801   aResVec[SMDSEntity_Quad_Hexa]  = nbSeg1 * nbQuadFace;
802
803   aResMap.insert( make_pair( theMesh.GetSubMesh(theShape), aResVec ));
804
805   return true;
806 }
807
808
809 //================================================================================
810 /*!
811  * \brief constructor of non-initialized _QuadFaceGrid
812  */
813 //================================================================================
814
815 _QuadFaceGrid::_QuadFaceGrid():
816   myReverse(false), myRightBrother(0), myUpBrother(0), myIndexer(0,0), myID(B_UNDEFINED)
817 {
818 }
819
820 //================================================================================
821 /*!
822  * \brief Initialization
823  */
824 //================================================================================
825
826 bool _QuadFaceGrid::Init(const TopoDS_Face& f, SMESH_Mesh& mesh)
827 {
828   myFace         = f;
829   mySides        = _FaceSide();
830   myReverse      = false;
831   myLeftBottomChild = myRightBrother = myUpBrother = 0;
832   myChildren.clear();
833   myGrid.clear();
834   //if ( myFace.Orientation() != TopAbs_FORWARD )
835     //myFace.Reverse();
836
837   list< TopoDS_Edge > edges;
838   list< int > nbEdgesInWire;
839   int nbWire = SMESH_Block::GetOrderedEdges (myFace, edges, nbEdgesInWire);
840   if ( nbWire != 1 )
841     return false;
842
843   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
844   if ( nbEdgesInWire.front() == 4 ) // exactly 4 edges
845   {
846     for ( ; edgeIt != edges.end(); ++edgeIt )
847       mySides.AppendSide( _FaceSide( *edgeIt ));
848   }
849   else if ( nbEdgesInWire.front() > 4 ) { // more than 4 edges - try to unite some
850     list< TopoDS_Edge > sideEdges;
851     while ( !edges.empty()) {
852       sideEdges.clear();
853       sideEdges.splice( sideEdges.end(), edges, edges.begin());// edges.front()->sideEdges.back()
854       while ( !edges.empty() ) {
855         if ( SMESH_Algo::IsContinuous( sideEdges.back(), edges.front() )) {
856           sideEdges.splice( sideEdges.end(), edges, edges.begin());
857         }
858         else if ( SMESH_Algo::IsContinuous( sideEdges.front(), edges.back() )) {
859           sideEdges.splice( sideEdges.begin(), edges, --edges.end());
860         }
861         else if ( isContinuousMesh( sideEdges.back(), edges.front(), f, mesh )) {
862           sideEdges.splice( sideEdges.end(), edges, edges.begin());
863         }
864         else if ( isContinuousMesh( sideEdges.front(), edges.back(), f, mesh )) {
865           sideEdges.splice( sideEdges.begin(), edges, --edges.end());
866         }
867         else {
868           break;
869         }
870       }
871       mySides.AppendSide( _FaceSide( sideEdges ));
872     }
873   }
874   if (mySides.size() != 4)
875     return false;
876
877 #ifdef _DEBUG_
878   mySides.GetSide( Q_BOTTOM )->SetID( Q_BOTTOM );
879   mySides.GetSide( Q_RIGHT  )->SetID( Q_RIGHT );
880   mySides.GetSide( Q_TOP    )->SetID( Q_TOP );
881   mySides.GetSide( Q_LEFT   )->SetID( Q_LEFT );
882 #endif
883
884   return true;
885 }
886
887 //================================================================================
888 /*!
889  * \brief Try to unite self with other ordinary face
890  */
891 //================================================================================
892
893 bool _QuadFaceGrid::AddContinuousFace( const _QuadFaceGrid&       other,
894                                        const TopTools_MapOfShape& internalEdges)
895 {
896   for ( int i = 0; i < 4; ++i )
897   {
898     const _FaceSide& otherSide = other.GetSide( i );
899     int iMyCommon;
900     if ( mySides.Contain( otherSide, &iMyCommon ) )
901     {
902       if ( internalEdges.Contains( otherSide.Edge( 0 )))
903       {
904         DUMP_VERT("Cont 1", mySides.GetSide(iMyCommon)->FirstVertex());
905         DUMP_VERT("Cont 2", mySides.GetSide(iMyCommon)->LastVertex());
906         DUMP_VERT("Cont 3", otherSide.FirstVertex());
907         DUMP_VERT("Cont 4", otherSide.LastVertex());
908         if ( myChildren.empty() ) {
909           myChildren.push_back( *this );
910           myFace.Nullify();
911         }
912
913         // orient new children equally
914         int otherBottomIndex = ( 4 + i - iMyCommon + 2 ) % 4;
915         if ( other.IsComplex() )
916           for ( TChildIterator children = other.GetChildren(); children.more(); ) {
917             myChildren.push_back( children.next() );
918             myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
919           }
920         else {
921           myChildren.push_back( other );
922           myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
923         }
924
925         myLeftBottomChild = 0;
926
927         // collect vertices in mySides
928         if ( other.IsComplex() )
929           for ( TChildIterator children = other.GetChildren(); children.more(); )
930           {
931             const _QuadFaceGrid& child =  children.next();
932             for ( int i = 0; i < 4; ++i )
933               mySides.AppendSide( child.GetSide(i) );
934           }
935         else
936           for ( int i = 0; i < 4; ++i )
937             mySides.AppendSide( other.GetSide(i) );
938
939         return true;
940       }
941     }
942   }
943   return false;
944 }
945
946 //================================================================================
947 /*!
948  * \brief Try to set the side as bottom hirizontal side
949  */
950 //================================================================================
951
952 bool _QuadFaceGrid::SetBottomSide(const _FaceSide& bottom, int* sideIndex)
953 {
954   myLeftBottomChild = myRightBrother = myUpBrother = 0;
955
956   int myBottomIndex;
957   if ( myChildren.empty() )
958   {
959     if ( mySides.Contain( bottom, &myBottomIndex )) {
960       mySides.SetBottomSide( myBottomIndex );
961       if ( sideIndex )
962         *sideIndex = myBottomIndex;
963       return true;
964     }
965   }
966   else
967   {
968     TChildren::iterator childFace = myChildren.begin(), childEnd = myChildren.end();
969     for ( ; childFace != childEnd; ++childFace )
970     {
971       if ( childFace->SetBottomSide( bottom, &myBottomIndex ))
972       {
973         TChildren::iterator orientedCild = childFace;
974         for ( childFace = myChildren.begin(); childFace != childEnd; ++childFace ) {
975           if ( childFace != orientedCild )
976             childFace->SetBottomSide( childFace->GetSide( myBottomIndex ));
977         }
978         if ( sideIndex )
979           *sideIndex = myBottomIndex;
980         return true;
981       }
982     }
983   }
984   return false;
985 }
986
987 //================================================================================
988 /*!
989  * \brief Return face adjacent to i-th side of this face, (0<i<4)
990  */
991 //================================================================================
992
993 _QuadFaceGrid* _QuadFaceGrid::FindAdjacentForSide(int                  i,
994                                                   list<_QuadFaceGrid>& faces,
995                                                   EBoxSides            id) const
996 {
997   const _FaceSide & iSide = GetSide( i );
998   list< _QuadFaceGrid >::iterator boxFace = faces.begin();
999   for ( ; boxFace != faces.end(); ++boxFace )
1000   {
1001     _QuadFaceGrid* f  = & (*boxFace);
1002     if ( f != this && f->SetBottomSide( iSide ))
1003       return f->SetID( id ), f;
1004   }
1005   return (_QuadFaceGrid*) 0;
1006 }
1007
1008 //================================================================================
1009 /*!
1010  * \brief Return i-th side
1011  */
1012 //================================================================================
1013
1014 const _FaceSide& _QuadFaceGrid::GetSide(int i) const
1015 {
1016   if ( myChildren.empty() )
1017     return *mySides.GetSide(i);
1018
1019   _QuadFaceGrid* me = const_cast<_QuadFaceGrid*>(this);
1020   if ( !me->locateChildren() || !myLeftBottomChild )
1021     return *mySides.GetSide(i);
1022
1023   const _QuadFaceGrid* child = myLeftBottomChild;
1024   switch ( i ){
1025   case Q_BOTTOM:
1026   case Q_LEFT:
1027     break;
1028   case Q_RIGHT:
1029     while ( child->myRightBrother )
1030       child = child->myRightBrother;
1031     break;
1032   case Q_TOP:
1033     while ( child->myUpBrother )
1034       child = child->myUpBrother;
1035     break;
1036   default: ;
1037   }
1038   return child->GetSide( i );
1039 }
1040
1041 //================================================================================
1042 /*!
1043  * \brief Reverse edges in order to have them oriented along axes of the unit box
1044  */
1045 //================================================================================
1046
1047 void _QuadFaceGrid::ReverseEdges(/*int e1, int e2*/)
1048 {
1049   myReverse = !myReverse;
1050
1051 // #ifdef DEB_FACES
1052 //   if ( !myFace.IsNull() )
1053 //     TopAbs::Print(myFace.Orientation(), cout);
1054 // #endif
1055
1056   if ( myChildren.empty() )
1057   {
1058 //     mySides.GetSide( e1 )->Reverse();
1059 //     mySides.GetSide( e2 )->Reverse();
1060     DumpVertices();
1061   }
1062   else
1063   {
1064     DumpVertices();
1065     TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1066     for ( ; child != childEnd; ++child )
1067       child->ReverseEdges( /*e1, e2*/ );
1068   }
1069 }
1070
1071 //================================================================================
1072 /*!
1073  * \brief Load nodes of a mesh
1074  */
1075 //================================================================================
1076
1077 bool _QuadFaceGrid::LoadGrid( SMESH_Mesh& mesh )
1078 {
1079   if ( !myChildren.empty() )
1080   {
1081     // Let child faces load their grids
1082     TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1083     for ( ; child != childEnd; ++child ) {
1084       child->SetID( myID );
1085       if ( !child->LoadGrid( mesh ) )
1086         return error( child->GetError() );
1087     }
1088     // Fill myGrid with nodes of patches
1089     return loadCompositeGrid( mesh );
1090   }
1091
1092   // ---------------------------------------
1093   // Fill myGrid with nodes bound to myFace
1094   // ---------------------------------------
1095
1096   if ( !myGrid.empty() )
1097     return true;
1098
1099   SMESHDS_SubMesh* faceSubMesh = mesh.GetSubMesh( myFace )->GetSubMeshDS();
1100   // check that all faces are quadrangular
1101   SMDS_ElemIteratorPtr fIt = faceSubMesh->GetElements();
1102   while ( fIt->more() )
1103     if ( fIt->next()->NbNodes() % 4 > 0 )
1104       return error("Non-quadrangular mesh faces are not allowed on sides of a composite block");
1105   
1106   myIndexer._xSize = 1 + mySides.GetSide( Q_BOTTOM )->GetNbSegments( mesh );
1107   myIndexer._ySize = 1 + mySides.GetSide( Q_LEFT   )->GetNbSegments( mesh );
1108
1109   myGrid.resize( myIndexer.size() );
1110
1111   // strore nodes bound to the bottom edge
1112   mySides.GetSide( Q_BOTTOM )->StoreNodes( mesh, myGrid, myReverse );
1113
1114   // store the rest nodes row by row
1115
1116   TIDSortedElemSet emptySet, avoidSet;
1117   const SMDS_MeshElement* firstQuad = 0; // most left face above the last row of found nodes
1118
1119   int nbFoundNodes = myIndexer._xSize;
1120   while ( nbFoundNodes != (int) myGrid.size() )
1121   {
1122     // first and last nodes of the last filled row of nodes
1123     const SMDS_MeshNode* n1down = myGrid[ nbFoundNodes - myIndexer._xSize ];
1124     const SMDS_MeshNode* n2down = myGrid[ nbFoundNodes - myIndexer._xSize + 1];
1125     const SMDS_MeshNode* n1downLast = myGrid[ nbFoundNodes-1 ];
1126
1127     // find the first face above the row by the first two left nodes
1128     //
1129     // n1up     n2up
1130     //     o---o
1131     //     |   |
1132     //     o---o  o  o  o  o
1133     //n1down    n2down
1134     //
1135     firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1136     while ( firstQuad && !faceSubMesh->Contains( firstQuad )) {
1137       avoidSet.insert( firstQuad );
1138       firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1139     }
1140     if ( !firstQuad || !faceSubMesh->Contains( firstQuad ))
1141       return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1142
1143     // find the node of quad bound to the left geom edge
1144     int i2down = firstQuad->GetNodeIndex( n2down );
1145     const SMDS_MeshNode* n1up = firstQuad->GetNode(( i2down+2 ) % 4 );
1146     myGrid[ nbFoundNodes++ ] = n1up;
1147     // the 4-the node of the first quad
1148     int i1down = firstQuad->GetNodeIndex( n1down );
1149     const SMDS_MeshNode* n2up = firstQuad->GetNode(( i1down+2 ) % 4 );
1150     myGrid[ nbFoundNodes++ ] = n2up;
1151
1152     n1down = n2down;
1153     n1up   = n2up;
1154     const SMDS_MeshElement* quad = firstQuad;
1155
1156     // find the rest nodes by remaining faces above the row
1157     //
1158     //             n1up
1159     //     o---o--o
1160     //     |   |  | ->
1161     //     o---o--o  o  o  o
1162     //                      n1downLast
1163     //
1164     while ( n1down != n1downLast )
1165     {
1166       // next face
1167       avoidSet.clear(); avoidSet.insert( quad );
1168       quad = SMESH_MeshAlgos::FindFaceInSet( n1down, n1up, emptySet, avoidSet );
1169       if ( !quad || quad->NbNodes() % 4 > 0)
1170         return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1171
1172       // next node
1173       if ( quad->GetNode( i1down ) != n1down ) // check already found index
1174         i1down = quad->GetNodeIndex( n1down );
1175       n2up = quad->GetNode(( i1down+2 ) % 4 );
1176       myGrid[ nbFoundNodes++ ] = n2up;
1177
1178       n1down = myGrid[ nbFoundNodes - myIndexer._xSize - 1 ];
1179       n1up   = n2up;
1180     }
1181     avoidSet.clear(); avoidSet.insert( firstQuad );
1182   }
1183   DumpGrid(); // debug
1184
1185   return true;
1186 }
1187
1188 //================================================================================
1189 /*!
1190  * \brief Find out mutual location of children: find their right and up brothers
1191  */
1192 //================================================================================
1193
1194 bool _QuadFaceGrid::locateChildren()
1195 {
1196   if ( myLeftBottomChild )
1197     return true;
1198
1199   TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1200
1201   // find a child sharing it's first bottom vertex with no other brother
1202   myLeftBottomChild = 0;
1203   for ( ; !myLeftBottomChild && child != childEnd; ++child )
1204   {
1205     TopoDS_Vertex leftVertex = child->GetSide( Q_BOTTOM ).FirstVertex();
1206     bool sharedVertex = false;
1207     TChildren::iterator otherChild = myChildren.begin();
1208     for ( ; otherChild != childEnd && !sharedVertex; ++otherChild )
1209       if ( otherChild != child )
1210         sharedVertex = otherChild->mySides.Contain( leftVertex );
1211     if ( !sharedVertex ) {
1212       myLeftBottomChild = & (*child);
1213       DUMP_VERT("0 left bottom Vertex: ",leftVertex );
1214     }
1215   }
1216   if (!myLeftBottomChild)
1217     return error(ERR_LI("Error in locateChildren()"));
1218
1219   set< _QuadFaceGrid* > notLocatedChilren;
1220   for (child = myChildren.begin() ; child != childEnd; ++child )
1221     notLocatedChilren.insert( & (*child));
1222
1223   // connect myLeftBottomChild to it's right and upper brothers
1224   notLocatedChilren.erase( myLeftBottomChild );
1225   myLeftBottomChild->setBrothers( notLocatedChilren );
1226   if ( !notLocatedChilren.empty() )
1227     return error(ERR_LI("Error in locateChildren()"));
1228
1229   return true;
1230 }
1231
1232 //================================================================================
1233 /*!
1234  * \brief Fill myGrid with nodes of patches
1235  */
1236 //================================================================================
1237
1238 bool _QuadFaceGrid::loadCompositeGrid(SMESH_Mesh& mesh)
1239 {
1240   // Find out mutual location of children: find their right and up brothers
1241   if ( !locateChildren() )
1242     return false;
1243
1244   // Load nodes according to mutual location of children
1245
1246   // grid size
1247   myIndexer._xSize = 1 + myLeftBottomChild->GetNbHoriSegments(mesh, /*withBrothers=*/true);
1248   myIndexer._ySize = 1 + myLeftBottomChild->GetNbVertSegments(mesh, /*withBrothers=*/true);
1249
1250   myGrid.resize( myIndexer.size() );
1251
1252   int fromX = myReverse ? myIndexer._xSize : 0;
1253   if (!myLeftBottomChild->fillGrid( mesh, myGrid, myIndexer, fromX, 0 ))
1254     return error( myLeftBottomChild->GetError() );
1255
1256   DumpGrid();
1257
1258   return true;
1259 }
1260
1261 //================================================================================
1262 /*!
1263  * \brief Find right an upper brothers among notLocatedBrothers
1264  */
1265 //================================================================================
1266
1267 void _QuadFaceGrid::setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers )
1268 {
1269   if ( !notLocatedBrothers.empty() )
1270   {
1271     // find right brother
1272     TopoDS_Vertex rightVertex = GetSide( Q_BOTTOM ).LastVertex();
1273     DUMP_VERT("1 right bottom Vertex: ",rightVertex );
1274     set< _QuadFaceGrid* >::iterator brIt, brEnd = notLocatedBrothers.end();
1275     for ( brIt = notLocatedBrothers.begin(); brIt != brEnd; ++brIt )
1276     {
1277       _QuadFaceGrid* brother = *brIt;
1278       TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1279       DUMP_VERT( "brother left bottom: ", brotherLeftVertex );
1280       if ( rightVertex.IsSame( brotherLeftVertex )) {
1281         myRightBrother = brother;
1282         notLocatedBrothers.erase( brIt );
1283         break;
1284       }
1285     }
1286     // find upper brother
1287     TopoDS_Vertex upVertex = GetSide( Q_LEFT ).FirstVertex();
1288     DUMP_VERT("1 left up Vertex: ",upVertex);
1289     brIt = notLocatedBrothers.begin(), brEnd = notLocatedBrothers.end();
1290     for ( ; brIt != brEnd; ++brIt )
1291     {
1292       _QuadFaceGrid* brother = *brIt;
1293       TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1294       DUMP_VERT("brother left bottom: ", brotherLeftVertex);
1295       if ( upVertex.IsSame( brotherLeftVertex )) {
1296         myUpBrother = brother;
1297         notLocatedBrothers.erase( myUpBrother );
1298         break;
1299       }
1300     }
1301     // recursive call
1302     if ( myRightBrother )
1303       myRightBrother->setBrothers( notLocatedBrothers );
1304     if ( myUpBrother )
1305       myUpBrother->setBrothers( notLocatedBrothers );
1306   }
1307 }
1308
1309 //================================================================================
1310 /*!
1311  * \brief Store nodes of a simple face into grid starting from (x,y) position
1312  */
1313 //================================================================================
1314
1315 bool _QuadFaceGrid::fillGrid(SMESH_Mesh&                    theMesh,
1316                              vector<const SMDS_MeshNode*> & theGrid,
1317                              const _Indexer&                theIndexer,
1318                              int                            theX,
1319                              int                            theY)
1320 {
1321   if ( myGrid.empty() && !LoadGrid( theMesh ))
1322     return false;
1323
1324   // store my own grid in the global grid
1325
1326   int fromX = myReverse ? theX - myIndexer._xSize: theX;
1327
1328   for ( int i = 0, x = fromX; i < myIndexer._xSize; ++i, ++x )
1329     for ( int j = 0, y = theY; j < myIndexer._ySize; ++j, ++y )
1330       theGrid[ theIndexer( x, y )] = myGrid[ myIndexer( i, j )];
1331
1332   // store grids of my right and upper brothers
1333
1334   if ( myRightBrother )
1335   {
1336     if ( myReverse )
1337       fromX += 1;
1338     else
1339       fromX += myIndexer._xSize - 1;
1340     if ( !myRightBrother->fillGrid( theMesh, theGrid, theIndexer, fromX, theY ))
1341       return error( myRightBrother->GetError() );
1342   }
1343   if ( myUpBrother )
1344   {
1345     if ( !myUpBrother->fillGrid( theMesh, theGrid, theIndexer,
1346                                  theX, theY + myIndexer._ySize - 1))
1347       return error( myUpBrother->GetError() );
1348   }
1349   return true;
1350 }
1351
1352 //================================================================================
1353 /*!
1354  * \brief Return number of segments on the hirizontal sides
1355  */
1356 //================================================================================
1357
1358 int _QuadFaceGrid::GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers) const
1359 {
1360   int nbSegs = 0;
1361   if ( myLeftBottomChild )
1362   {
1363     nbSegs += myLeftBottomChild->GetNbHoriSegments( mesh, true );
1364   }
1365   else
1366   {
1367     nbSegs = mySides.GetSide( Q_BOTTOM )->GetNbSegments(mesh);
1368     if ( withBrothers && myRightBrother )
1369       nbSegs += myRightBrother->GetNbHoriSegments( mesh, withBrothers );
1370   }
1371   return nbSegs;
1372 }
1373
1374 //================================================================================
1375 /*!
1376  * \brief Return number of segments on the vertical sides
1377  */
1378 //================================================================================
1379
1380 int _QuadFaceGrid::GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers) const
1381 {
1382   int nbSegs = 0;
1383   if ( myLeftBottomChild )
1384   {
1385     nbSegs += myLeftBottomChild->GetNbVertSegments( mesh, true );
1386   }
1387   else
1388   {
1389     nbSegs = mySides.GetSide( Q_LEFT )->GetNbSegments(mesh);
1390     if ( withBrothers && myUpBrother )
1391       nbSegs += myUpBrother->GetNbVertSegments( mesh, withBrothers );
1392   }
1393   return nbSegs;
1394 }
1395
1396 //================================================================================
1397 /*!
1398  * \brief Return edge on the hirizontal bottom sides
1399  */
1400 //================================================================================
1401
1402 int _QuadFaceGrid::GetHoriEdges(vector<TopoDS_Edge> & edges) const
1403 {
1404   if ( myLeftBottomChild )
1405   {
1406     return myLeftBottomChild->GetHoriEdges( edges );
1407   }
1408   else
1409   {
1410     const _FaceSide* bottom  = mySides.GetSide( Q_BOTTOM );
1411     int i = 0;
1412     while ( true ) {
1413       TopoDS_Edge e = bottom->Edge( i++ );
1414       if ( e.IsNull() )
1415         break;
1416       else
1417         edges.push_back( e );
1418     }
1419     if ( myRightBrother )
1420       myRightBrother->GetHoriEdges( edges );
1421   }
1422   return edges.size();
1423 }
1424
1425 //================================================================================
1426 /*!
1427  * \brief Return a node by its position
1428  */
1429 //================================================================================
1430
1431 const SMDS_MeshNode* _QuadFaceGrid::GetNode(int iHori, int iVert) const
1432 {
1433   return myGrid[ myIndexer( iHori, iVert )];
1434 }
1435
1436 //================================================================================
1437 /*!
1438  * \brief Return node coordinates by its position
1439  */
1440 //================================================================================
1441
1442 gp_XYZ _QuadFaceGrid::GetXYZ(int iHori, int iVert) const
1443 {
1444   const SMDS_MeshNode* n = myGrid[ myIndexer( iHori, iVert )];
1445   return gp_XYZ( n->X(), n->Y(), n->Z() );
1446 }
1447
1448 //================================================================================
1449 /*!
1450  * \brief Return normal to the face at vertex v
1451  */
1452 //================================================================================
1453
1454 bool _QuadFaceGrid::GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const
1455 {
1456   if ( myChildren.empty() )
1457   {
1458     if ( mySides.Contain( v )) {
1459       try {
1460         gp_Pnt2d uv = BRep_Tool::Parameters( v, myFace );
1461         BRepAdaptor_Surface surface( myFace );
1462         gp_Pnt p; gp_Vec d1u, d1v;
1463         surface.D1( uv.X(), uv.Y(), p, d1u, d1v );
1464         n = d1u.Crossed( d1v );
1465         return true;
1466       }
1467       catch (Standard_Failure) {
1468         return false;
1469       }
1470     }
1471   }
1472   else
1473   {
1474     TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1475     for ( ; child != childEnd; ++child )
1476       if ( child->GetNormal( v, n ))
1477         return true;
1478   }
1479   return false;
1480 }
1481
1482 //================================================================================
1483 /*!
1484  * \brief Dumps coordinates of grid nodes
1485  */
1486 //================================================================================
1487
1488 void _QuadFaceGrid::DumpGrid() const
1489 {
1490 #ifdef DEB_GRID
1491   const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1492   cout << "****** Face " << names[ myID ] << endl;
1493
1494   if ( myChildren.empty() || !myGrid.empty() )
1495   {
1496     cout << "x size: " << myIndexer._xSize << "; y size: " << myIndexer._ySize << endl;
1497     for ( int y = 0; y < myIndexer._ySize; ++y ) {
1498       cout << "-- row " << y << endl;
1499       for ( int x = 0; x < myIndexer._xSize; ++x ) {
1500         const SMDS_MeshNode* n = myGrid[ myIndexer( x, y ) ];
1501         cout << x << " ( " << n->X() << ", " << n->Y() << ", " << n->Z() << " )" << endl;
1502       }
1503     }
1504   }
1505   else
1506   {
1507     cout << "Nb children: " << myChildren.size() << endl;
1508     TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1509     for ( int i=0; child != childEnd; ++child, ++i ) {
1510       cout << "   *** SUBFACE " << i+1 << endl;
1511       ((_QuadFaceGrid&)(*child)).SetID( myID );
1512       child->DumpGrid();
1513     }
1514   }
1515 #endif
1516 }
1517
1518 //================================================================================
1519 /*!
1520  * \brief Dump vertices
1521  */
1522 //================================================================================
1523
1524 void _QuadFaceGrid::DumpVertices() const
1525 {
1526 #ifdef DEB_FACES
1527   cout << "****** Face ";
1528   const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1529   if ( myID >= B_BOTTOM && myID < B_BACK )
1530     cout << names[ myID ] << endl;
1531   else
1532     cout << "UNDEFINED" << endl;
1533
1534   if ( myChildren.empty() )
1535   {
1536     for ( int i = 0; i < 4; ++i )
1537     {
1538       cout << "  Side "; mySides.GetSide( i )->Dump();
1539     }
1540   }
1541   else
1542   {
1543     cout << "-- Nb children: " << myChildren.size() << endl;
1544     TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1545     for ( int i=0; child != childEnd; ++child, ++i ) {
1546       cout << "   *** SUBFACE " << i+1 << endl;
1547       ((_QuadFaceGrid&)(*child)).SetID( myID );
1548       child->DumpVertices();
1549     }
1550   }
1551 #endif
1552 }
1553
1554 //=======================================================================
1555 //function : _FaceSide
1556 //purpose  : copy constructor
1557 //=======================================================================
1558
1559 _FaceSide::_FaceSide(const _FaceSide& other)
1560 {
1561   myEdge = other.myEdge;
1562   myChildren = other.myChildren;
1563   myNbChildren = other.myNbChildren;
1564   myVertices.Assign( other.myVertices );
1565   myID = other.myID;
1566 }
1567
1568 //================================================================================
1569 /*!
1570  * \brief Construct a face side of one edge
1571  */
1572 //================================================================================
1573
1574 _FaceSide::_FaceSide(const TopoDS_Edge& edge):
1575   myEdge( edge ), myNbChildren(0)
1576 {
1577   if ( !edge.IsNull() )
1578     for ( TopExp_Explorer exp( edge, TopAbs_VERTEX ); exp.More(); exp.Next() )
1579       //myVertices.insert( ptr ( exp.Current() ));
1580       myVertices.Add( exp.Current() );
1581 }
1582
1583 //================================================================================
1584 /*!
1585  * \brief Construct a face side of several edges
1586  */
1587 //================================================================================
1588
1589 _FaceSide::_FaceSide(const list<TopoDS_Edge>& edges):
1590   myNbChildren(0)
1591 {
1592   list<TopoDS_Edge>::const_iterator edge = edges.begin(), eEnd = edges.end();
1593   for ( ; edge != eEnd; ++edge ) {
1594     myChildren.push_back( _FaceSide( *edge ));
1595     myNbChildren++;
1596 //     myVertices.insert( myChildren.back().myVertices.begin(),
1597 //                        myChildren.back().myVertices.end() );
1598     myVertices.Add( myChildren.back().FirstVertex() );
1599     myVertices.Add( myChildren.back().LastVertex() );
1600     myChildren.back().SetID( Q_CHILD ); // not to splice them
1601   }
1602 }
1603
1604 //=======================================================================
1605 //function : GetSide
1606 //purpose  : 
1607 //=======================================================================
1608
1609 _FaceSide* _FaceSide::GetSide(const int i)
1610 {
1611   if ( i >= myNbChildren )
1612     return 0;
1613
1614   list< _FaceSide >::iterator side = myChildren.begin();
1615   if ( i )
1616     std::advance( side, i );
1617   return & (*side);
1618 }
1619
1620 //=======================================================================
1621 //function : GetSide
1622 //purpose  : 
1623 //=======================================================================
1624
1625 const _FaceSide* _FaceSide::GetSide(const int i) const
1626 {
1627   return const_cast< _FaceSide* >(this)->GetSide(i);
1628 }
1629
1630 //=======================================================================
1631 //function : NbVertices
1632 //purpose  : return nb of vertices in the side
1633 //=======================================================================
1634
1635 int _FaceSide::NbVertices() const
1636 {
1637   if ( myChildren.empty() )
1638     return myVertices.Extent();
1639
1640   return myNbChildren + 1;
1641 }
1642
1643 //=======================================================================
1644 //function : NbCommonVertices
1645 //purpose  : Returns number of my vertices common with the given ones
1646 //=======================================================================
1647
1648 int _FaceSide::NbCommonVertices( const TopTools_MapOfShape& VV ) const
1649 {
1650   int nbCommon = 0;
1651   TopTools_MapIteratorOfMapOfShape vIt ( myVertices );
1652   for ( ; vIt.More(); vIt.Next() )
1653     nbCommon += ( VV.Contains( vIt.Key() ));
1654
1655   return nbCommon;
1656 }
1657
1658 //=======================================================================
1659 //function : FirstVertex
1660 //purpose  :
1661 //=======================================================================
1662
1663 TopoDS_Vertex _FaceSide::FirstVertex() const
1664 {
1665   if ( myChildren.empty() )
1666     return TopExp::FirstVertex( myEdge, Standard_True );
1667
1668   return myChildren.front().FirstVertex();
1669 }
1670
1671 //=======================================================================
1672 //function : LastVertex
1673 //purpose  : 
1674 //=======================================================================
1675
1676 TopoDS_Vertex _FaceSide::LastVertex() const
1677 {
1678   if ( myChildren.empty() )
1679     return TopExp::LastVertex( myEdge, Standard_True );
1680
1681   return myChildren.back().LastVertex();
1682 }
1683
1684 //=======================================================================
1685 //function : Vertex
1686 //purpose  : 
1687 //=======================================================================
1688
1689 TopoDS_Vertex _FaceSide::Vertex(int i) const
1690 {
1691   if ( myChildren.empty() )
1692     return i ? LastVertex() : FirstVertex();
1693       
1694   if ( i >= myNbChildren )
1695     return myChildren.back().LastVertex();
1696   
1697   return GetSide(i)->FirstVertex();
1698 }
1699
1700 //================================================================================
1701 /*!
1702  * \brief Return i-the zero-based edge of the side
1703  */
1704 //================================================================================
1705
1706 TopoDS_Edge _FaceSide::Edge(int i) const
1707 {
1708   if ( i == 0 && !myEdge.IsNull() )
1709     return myEdge;
1710
1711   if ( const _FaceSide* iSide = GetSide( i ))
1712     return iSide->myEdge;
1713
1714   return TopoDS_Edge();
1715 }
1716
1717 //=======================================================================
1718 //function : Contain
1719 //purpose  : 
1720 //=======================================================================
1721
1722 bool _FaceSide::Contain( const _FaceSide& side, int* which ) const
1723 {
1724   if ( !which || myChildren.empty() )
1725   {
1726     if ( which )
1727       *which = 0;
1728     int nbCommon = 0;
1729     TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1730     for ( ; vIt.More(); vIt.Next() )
1731       nbCommon += ( myVertices.Contains( vIt.Key() ));
1732     return (nbCommon > 1);
1733   }
1734   list< _FaceSide >::const_iterator mySide = myChildren.begin(), sideEnd = myChildren.end();
1735   for ( int i = 0; mySide != sideEnd; ++mySide, ++i ) {
1736     if ( mySide->Contain( side )) {
1737       *which = i;
1738       return true;
1739     }
1740   }
1741   return false;
1742 }
1743
1744 //=======================================================================
1745 //function : Contain
1746 //purpose  : 
1747 //=======================================================================
1748
1749 bool _FaceSide::Contain( const TopoDS_Vertex& vertex ) const
1750 {
1751   return myVertices.Contains( vertex );
1752 }
1753
1754 //=======================================================================
1755 //function : AppendSide
1756 //purpose  : 
1757 //=======================================================================
1758
1759 void _FaceSide::AppendSide( const _FaceSide& side )
1760 {
1761   if ( !myEdge.IsNull() )
1762   {
1763     myChildren.push_back( *this );
1764     myNbChildren = 1;
1765     myEdge.Nullify();
1766   }
1767   myChildren.push_back( side );
1768   myNbChildren++;
1769   TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1770   for ( ; vIt.More(); vIt.Next() )
1771     myVertices.Add( vIt.Key() );
1772
1773   myID = Q_PARENT;
1774   myChildren.back().SetID( EQuadSides( myNbChildren-1 ));
1775 }
1776
1777 //=======================================================================
1778 //function : SetBottomSide
1779 //purpose  : 
1780 //=======================================================================
1781
1782 void _FaceSide::SetBottomSide( int i )
1783 {
1784   if ( i > 0 && myID == Q_PARENT ) {
1785     list< _FaceSide >::iterator sideEnd, side = myChildren.begin();
1786     std::advance( side, i );
1787     myChildren.splice( myChildren.begin(), myChildren, side, myChildren.end() );
1788
1789     side = myChildren.begin(), sideEnd = myChildren.end();
1790     for ( int i = 0; side != sideEnd; ++side, ++i ) {
1791       side->SetID( EQuadSides(i) );
1792       side->SetBottomSide(i);
1793     }
1794   }
1795 }
1796
1797 //=======================================================================
1798 //function : GetNbSegments
1799 //purpose  : 
1800 //=======================================================================
1801
1802 int _FaceSide::GetNbSegments(SMESH_Mesh& mesh) const
1803 {
1804   int nb = 0;
1805   if ( myChildren.empty() )
1806   {
1807     nb = mesh.GetSubMesh(myEdge)->GetSubMeshDS()->NbElements();
1808   }
1809   else
1810   {
1811     list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1812     for ( ; side != sideEnd; ++side )
1813       nb += side->GetNbSegments(mesh);
1814   }
1815   return nb;
1816 }
1817
1818 //=======================================================================
1819 //function : StoreNodes
1820 //purpose  : 
1821 //=======================================================================
1822
1823 bool _FaceSide::StoreNodes(SMESH_Mesh&                   mesh,
1824                            vector<const SMDS_MeshNode*>& myGrid,
1825                            bool                          reverse )
1826 {
1827   list< TopoDS_Edge > edges;
1828   if ( myChildren.empty() )
1829   {
1830     edges.push_back( myEdge );
1831   }
1832   else
1833   {
1834     list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1835     for ( ; side != sideEnd; ++side )
1836       if ( reverse )
1837         edges.push_front( side->myEdge );
1838       else
1839         edges.push_back ( side->myEdge );
1840   }
1841   int nbNodes = 0;
1842   list< TopoDS_Edge >::iterator edge = edges.begin(), eEnd = edges.end();
1843   for ( ; edge != eEnd; ++edge )
1844   {
1845     map< double, const SMDS_MeshNode* > nodes;
1846     bool ok = SMESH_Algo::GetSortedNodesOnEdge( mesh.GetMeshDS(),
1847                                                 *edge,
1848                                                 /*ignoreMediumNodes=*/true,
1849                                                 nodes);
1850     if ( !ok ) return false;
1851
1852     bool forward = ( edge->Orientation() == TopAbs_FORWARD );
1853     if ( reverse ) forward = !forward;
1854     if ( forward )
1855     {
1856       map< double, const SMDS_MeshNode* >::iterator u_node, nEnd = nodes.end();
1857       for ( u_node = nodes.begin(); u_node != nEnd; ++u_node )
1858         myGrid[ nbNodes++ ] = u_node->second;
1859     }
1860     else 
1861     {
1862       map< double, const SMDS_MeshNode* >::reverse_iterator u_node, nEnd = nodes.rend();
1863       for ( u_node = nodes.rbegin(); u_node != nEnd; ++u_node )
1864         myGrid[ nbNodes++ ] = u_node->second;
1865     }
1866     nbNodes--; // node on vertex present in two adjacent edges
1867   }
1868   return nbNodes > 0;
1869 }
1870
1871 //=======================================================================
1872 //function : Dump
1873 //purpose  : dump end vertices
1874 //=======================================================================
1875
1876 void _FaceSide::Dump() const
1877 {
1878   if ( myChildren.empty() )
1879   {
1880     const char* sideNames[] = { "Q_BOTTOM", "Q_RIGHT", "Q_TOP", "Q_LEFT", "Q_CHILD", "Q_PARENT" };
1881     if ( myID >= Q_BOTTOM && myID < Q_PARENT )
1882       cout << sideNames[ myID ] << endl;
1883     else
1884       cout << "<UNDEFINED ID>" << endl;
1885     TopoDS_Vertex f = FirstVertex();
1886     TopoDS_Vertex l = LastVertex();
1887     gp_Pnt pf = BRep_Tool::Pnt(f);
1888     gp_Pnt pl = BRep_Tool::Pnt(l);
1889     cout << "\t ( "<< ptr( f ) << " - " << ptr( l )<< " )"
1890          << "\t ( "<< pf.X()<<", "<<pf.Y()<<", "<<pf.Z()<<" ) - "
1891          << " ( "<< pl.X()<<", "<<pl.Y()<<", "<<pl.Z()<<" )"<<endl;
1892   }
1893   else
1894   {
1895     list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1896     for ( ; side != sideEnd; ++side ) {
1897       side->Dump();
1898       cout << "\t";
1899     }
1900   }
1901 }