Salome HOME
Corrected for bug IPAL12571.
[modules/smesh.git] / src / StdMeshers / StdMeshers_Quadrangle_2D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : StdMeshers_Quadrangle_2D.cxx
25 //           Moved here from SMESH_Quadrangle_2D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 #include "StdMeshers_Quadrangle_2D.hxx"
31
32 #include "StdMeshers_FaceSide.hxx"
33
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_subMesh.hxx"
37 #include "SMESH_MesherHelper.hxx"
38 #include "SMESH_Block.hxx"
39 #include "SMESH_Comment.hxx"
40
41 #include "SMDS_MeshElement.hxx"
42 #include "SMDS_MeshNode.hxx"
43 #include "SMDS_EdgePosition.hxx"
44 #include "SMDS_FacePosition.hxx"
45
46 #include <BRepAdaptor_Curve.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRepLProp.hxx>
49 #include <BRepTools.hxx>
50 #include <BRepTools_WireExplorer.hxx>
51 #include <Geom_Surface.hxx>
52 #include <Geom_Curve.hxx>
53 #include <Geom2d_Curve.hxx>
54 #include <GeomAdaptor_Curve.hxx>
55 #include <GCPnts_UniformAbscissa.hxx>
56 #include <TopExp.hxx>
57 #include <Precision.hxx>
58 #include <gp_Pnt2d.hxx>
59 #include <TColStd_ListIteratorOfListOfInteger.hxx>
60 #include <TColStd_SequenceOfReal.hxx>
61 #include <TColgp_SequenceOfXY.hxx>
62 #include <NCollection_DefineArray2.hxx>
63
64 #include "utilities.h"
65 #include "Utils_ExceptHandlers.hxx"
66
67 #ifndef StdMeshers_Array2OfNode_HeaderFile
68 #define StdMeshers_Array2OfNode_HeaderFile
69 typedef const SMDS_MeshNode* SMDS_MeshNodePtr;
70 DEFINE_BASECOLLECTION (StdMeshers_BaseCollectionNodePtr, SMDS_MeshNodePtr)
71 DEFINE_ARRAY2(StdMeshers_Array2OfNode,
72               StdMeshers_BaseCollectionNodePtr, SMDS_MeshNodePtr)
73 #endif
74
75 using namespace std;
76
77 typedef gp_XY gp_UV;
78 typedef SMESH_Comment TComm;
79
80 //=============================================================================
81 /*!
82  *  
83  */
84 //=============================================================================
85
86 StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D (int hypId, int studyId, SMESH_Gen* gen)
87      : SMESH_2D_Algo(hypId, studyId, gen)
88 {
89   MESSAGE("StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D");
90   _name = "Quadrangle_2D";
91   _shapeType = (1 << TopAbs_FACE);
92   _compatibleHypothesis.push_back("QuadranglePreference");
93   myTool = 0;
94 }
95
96 //=============================================================================
97 /*!
98  *  
99  */
100 //=============================================================================
101
102 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
103 {
104   MESSAGE("StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D");
105 }
106
107 //=============================================================================
108 /*!
109  *  
110  */
111 //=============================================================================
112
113 bool StdMeshers_Quadrangle_2D::CheckHypothesis
114                          (SMESH_Mesh&                          aMesh,
115                           const TopoDS_Shape&                  aShape,
116                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
117 {
118   bool isOk = true;
119   aStatus = SMESH_Hypothesis::HYP_OK;
120
121   // there is only one compatible Hypothesis so far
122   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape, false);
123   myQuadranglePreference = hyps.size() > 0;
124
125   return isOk;
126 }
127
128 //=============================================================================
129 /*!
130  *  
131  */
132 //=============================================================================
133
134 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
135                                         const TopoDS_Shape& aShape)// throw (SALOME_Exception)
136 {
137   // PAL14921. Enable catching std::bad_alloc and Standard_OutOfMemory outside
138   //Unexpect aCatchSalomeException);
139
140   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
141   aMesh.GetSubMesh(aShape);
142
143   SMESH_MesherHelper helper(aMesh);
144   myTool = &helper;
145
146   _quadraticMesh = myTool->IsQuadraticSubMesh(aShape);
147
148   FaceQuadStruct *quad = CheckNbEdges( aMesh, aShape );
149   std::auto_ptr<FaceQuadStruct> quadDeleter( quad ); // to delete quad at exit from Compute()
150   if (!quad)
151     return false;
152
153   if(myQuadranglePreference) {
154     int n1 = quad->side[0]->NbPoints();
155     int n2 = quad->side[1]->NbPoints();
156     int n3 = quad->side[2]->NbPoints();
157     int n4 = quad->side[3]->NbPoints();
158     int nfull = n1+n2+n3+n4;
159     int ntmp = nfull/2;
160     ntmp = ntmp*2;
161     if( nfull==ntmp && ( (n1!=n3) || (n2!=n4) ) ) {
162       // special path for using only quandrangle faces
163       bool ok = ComputeQuadPref(aMesh, aShape, quad);
164       return ok;
165     }
166   }
167
168   // set normalized grid on unit square in parametric domain
169   
170   if (!SetNormalizedGrid(aMesh, aShape, quad))
171     return false;
172
173   // --- compute 3D values on points, store points & quadrangles
174
175   int nbdown  = quad->side[0]->NbPoints();
176   int nbup    = quad->side[2]->NbPoints();
177
178   int nbright = quad->side[1]->NbPoints();
179   int nbleft  = quad->side[3]->NbPoints();
180
181   int nbhoriz  = Min(nbdown, nbup);
182   int nbvertic = Min(nbright, nbleft);
183
184   const TopoDS_Face& F = TopoDS::Face(aShape);
185   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
186
187   // internal mesh nodes
188   int i, j, geomFaceID = meshDS->ShapeToIndex( F );
189   for (i = 1; i < nbhoriz - 1; i++) {
190     for (j = 1; j < nbvertic - 1; j++) {
191       int ij = j * nbhoriz + i;
192       double u = quad->uv_grid[ij].u;
193       double v = quad->uv_grid[ij].v;
194       gp_Pnt P = S->Value(u, v);
195       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
196       meshDS->SetNodeOnFace(node, geomFaceID, u, v);
197       quad->uv_grid[ij].node = node;
198     }
199   }
200   
201   // mesh faces
202
203   //             [2]
204   //      --.--.--.--.--.--  nbvertic
205   //     |                 | ^
206   //     |                 | ^
207   // [3] |                 | ^ j  [1]
208   //     |                 | ^
209   //     |                 | ^
210   //      ---.----.----.---  0
211   //     0 > > > > > > > > nbhoriz
212   //              i
213   //             [0]
214   
215   i = 0;
216   int ilow = 0;
217   int iup = nbhoriz - 1;
218   if (quad->isEdgeOut[3]) { ilow++; } else { if (quad->isEdgeOut[1]) iup--; }
219   
220   int jlow = 0;
221   int jup = nbvertic - 1;
222   if (quad->isEdgeOut[0]) { jlow++; } else { if (quad->isEdgeOut[2]) jup--; }
223   
224   // regular quadrangles
225   for (i = ilow; i < iup; i++) {
226     for (j = jlow; j < jup; j++) {
227       const SMDS_MeshNode *a, *b, *c, *d;
228       a = quad->uv_grid[j * nbhoriz + i].node;
229       b = quad->uv_grid[j * nbhoriz + i + 1].node;
230       c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
231       d = quad->uv_grid[(j + 1) * nbhoriz + i].node;
232       SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
233       meshDS->SetMeshElementOnShape(face, geomFaceID);
234     }
235   }
236
237   const vector<UVPtStruct>& uv_e0 = quad->side[0]->GetUVPtStruct(true,0 );
238   const vector<UVPtStruct>& uv_e1 = quad->side[1]->GetUVPtStruct(false,1);
239   const vector<UVPtStruct>& uv_e2 = quad->side[2]->GetUVPtStruct(true,1 );
240   const vector<UVPtStruct>& uv_e3 = quad->side[3]->GetUVPtStruct(false,0);
241
242   double eps = Precision::Confusion();
243
244   // Boundary quadrangles
245   
246   if (quad->isEdgeOut[0]) {
247     // Down edge is out
248     // 
249     // |___|___|___|___|___|___|
250     // |   |   |   |   |   |   |
251     // |___|___|___|___|___|___|
252     // |   |   |   |   |   |   |
253     // |___|___|___|___|___|___| __ first row of the regular grid
254     // .  .  .  .  .  .  .  .  . __ down edge nodes
255     // 
256     // >->->->->->->->->->->->-> -- direction of processing
257       
258     int g = 0; // number of last processed node in the regular grid
259     
260     // number of last node of the down edge to be processed
261     int stop = nbdown - 1;
262     // if right edge is out, we will stop at a node, previous to the last one
263     if (quad->isEdgeOut[1]) stop--;
264     
265     // for each node of the down edge find nearest node
266     // in the first row of the regular grid and link them
267     for (i = 0; i < stop; i++) {
268       const SMDS_MeshNode *a, *b, *c, *d;
269       a = uv_e0[i].node;
270       b = uv_e0[i + 1].node;
271       gp_Pnt pb (b->X(), b->Y(), b->Z());
272       
273       // find node c in the regular grid, which will be linked with node b
274       int near = g;
275       if (i == stop - 1) {
276         // right bound reached, link with the rightmost node
277         near = iup;
278         c = quad->uv_grid[nbhoriz + iup].node;
279       }
280       else {
281         // find in the grid node c, nearest to the b
282         double mind = RealLast();
283         for (int k = g; k <= iup; k++) {
284           
285           const SMDS_MeshNode *nk;
286           if (k < ilow) // this can be, if left edge is out
287             nk = uv_e3[1].node; // get node from the left edge
288           else
289             nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
290
291           gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
292           double dist = pb.Distance(pnk);
293           if (dist < mind - eps) {
294             c = nk;
295             near = k;
296             mind = dist;
297           } else {
298             break;
299           }
300         }
301       }
302
303       if (near == g) { // make triangle
304         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
305         SMDS_MeshFace* face = myTool->AddFace(a, b, c);
306         meshDS->SetMeshElementOnShape(face, geomFaceID);
307       }
308       else { // make quadrangle
309         if (near - 1 < ilow)
310           d = uv_e3[1].node;
311         else
312           d = quad->uv_grid[nbhoriz + near - 1].node;
313         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
314         SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
315         meshDS->SetMeshElementOnShape(face, geomFaceID);
316
317         // if node d is not at position g - make additional triangles
318         if (near - 1 > g) {
319           for (int k = near - 1; k > g; k--) {
320             c = quad->uv_grid[nbhoriz + k].node;
321             if (k - 1 < ilow)
322               d = uv_e3[1].node;
323             else
324               d = quad->uv_grid[nbhoriz + k - 1].node;
325             //SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
326             SMDS_MeshFace* face = myTool->AddFace(a, c, d);
327             meshDS->SetMeshElementOnShape(face, geomFaceID);
328           }
329         }
330         g = near;
331       }
332     }
333   } else {
334     if (quad->isEdgeOut[2]) {
335       // Up edge is out
336       // 
337       // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
338       // 
339       // .  .  .  .  .  .  .  .  . __ up edge nodes
340       //  ___ ___ ___ ___ ___ ___  __ first row of the regular grid
341       // |   |   |   |   |   |   |
342       // |___|___|___|___|___|___|
343       // |   |   |   |   |   |   |
344       // |___|___|___|___|___|___|
345       // |   |   |   |   |   |   |
346
347       int g = nbhoriz - 1; // last processed node in the regular grid
348
349       int stop = 0;
350       // if left edge is out, we will stop at a second node
351       if (quad->isEdgeOut[3]) stop++;
352
353       // for each node of the up edge find nearest node
354       // in the first row of the regular grid and link them
355       for (i = nbup - 1; i > stop; i--) {
356         const SMDS_MeshNode *a, *b, *c, *d;
357         a = uv_e2[i].node;
358         b = uv_e2[i - 1].node;
359         gp_Pnt pb (b->X(), b->Y(), b->Z());
360
361         // find node c in the grid, which will be linked with node b
362         int near = g;
363         if (i == stop + 1) { // left bound reached, link with the leftmost node
364           c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
365           near = ilow;
366         } else {
367           // find node c in the grid, nearest to the b
368           double mind = RealLast();
369           for (int k = g; k >= ilow; k--) {
370             const SMDS_MeshNode *nk;
371             if (k > iup)
372               nk = uv_e1[nbright - 2].node;
373             else
374               nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
375             gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
376             double dist = pb.Distance(pnk);
377             if (dist < mind - eps) {
378               c = nk;
379               near = k;
380               mind = dist;
381             } else {
382               break;
383             }
384           }
385         }
386
387         if (near == g) { // make triangle
388           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
389           SMDS_MeshFace* face = myTool->AddFace(a, b, c);
390           meshDS->SetMeshElementOnShape(face, geomFaceID);
391         }
392         else { // make quadrangle
393           if (near + 1 > iup)
394             d = uv_e1[nbright - 2].node;
395           else
396             d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
397           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
398           SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
399           meshDS->SetMeshElementOnShape(face, geomFaceID);
400
401           if (near + 1 < g) { // if d not is at g - make additional triangles
402             for (int k = near + 1; k < g; k++) {
403               c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
404               if (k + 1 > iup)
405                 d = uv_e1[nbright - 2].node;
406               else
407                 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
408               //SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
409               SMDS_MeshFace* face = myTool->AddFace(a, c, d);
410               meshDS->SetMeshElementOnShape(face, geomFaceID);
411             }
412           }
413           g = near;
414         }
415       }
416     }
417   }
418
419   // right or left boundary quadrangles
420   if (quad->isEdgeOut[1]) {
421 //    MESSAGE("right edge is out");
422     int g = 0; // last processed node in the grid
423     int stop = nbright - 1;
424     if (quad->isEdgeOut[2]) stop--;
425     for (i = 0; i < stop; i++) {
426       const SMDS_MeshNode *a, *b, *c, *d;
427       a = uv_e1[i].node;
428       b = uv_e1[i + 1].node;
429       gp_Pnt pb (b->X(), b->Y(), b->Z());
430
431       // find node c in the grid, nearest to the b
432       int near = g;
433       if (i == stop - 1) { // up bondary reached
434         c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
435         near = jup;
436       } else {
437         double mind = RealLast();
438         for (int k = g; k <= jup; k++) {
439           const SMDS_MeshNode *nk;
440           if (k < jlow)
441             nk = uv_e0[nbdown - 2].node;
442           else
443             nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
444           gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
445           double dist = pb.Distance(pnk);
446           if (dist < mind - eps) {
447             c = nk;
448             near = k;
449             mind = dist;
450           } else {
451             break;
452           }
453         }
454       }
455
456       if (near == g) { // make triangle
457         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
458         SMDS_MeshFace* face = myTool->AddFace(a, b, c);
459         meshDS->SetMeshElementOnShape(face, geomFaceID);
460       }
461       else { // make quadrangle
462         if (near - 1 < jlow)
463           d = uv_e0[nbdown - 2].node;
464         else
465           d = quad->uv_grid[nbhoriz*near - 2].node;
466         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
467         SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
468         meshDS->SetMeshElementOnShape(face, geomFaceID);
469
470         if (near - 1 > g) { // if d not is at g - make additional triangles
471           for (int k = near - 1; k > g; k--) {
472             c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
473             if (k - 1 < jlow)
474               d = uv_e0[nbdown - 2].node;
475             else
476               d = quad->uv_grid[nbhoriz*k - 2].node;
477             //SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
478             SMDS_MeshFace* face = myTool->AddFace(a, c, d);
479             meshDS->SetMeshElementOnShape(face, geomFaceID);
480           }
481         }
482         g = near;
483       }
484     }
485   } else {
486     if (quad->isEdgeOut[3]) {
487 //      MESSAGE("left edge is out");
488       int g = nbvertic - 1; // last processed node in the grid
489       int stop = 0;
490       if (quad->isEdgeOut[0]) stop++;
491       for (i = nbleft - 1; i > stop; i--) {
492         const SMDS_MeshNode *a, *b, *c, *d;
493         a = uv_e3[i].node;
494         b = uv_e3[i - 1].node;
495         gp_Pnt pb (b->X(), b->Y(), b->Z());
496
497         // find node c in the grid, nearest to the b
498         int near = g;
499         if (i == stop + 1) { // down bondary reached
500           c = quad->uv_grid[nbhoriz*jlow + 1].node;
501           near = jlow;
502         } else {
503           double mind = RealLast();
504           for (int k = g; k >= jlow; k--) {
505             const SMDS_MeshNode *nk;
506             if (k > jup)
507               nk = uv_e2[1].node;
508             else
509               nk = quad->uv_grid[nbhoriz*k + 1].node;
510             gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
511             double dist = pb.Distance(pnk);
512             if (dist < mind - eps) {
513               c = nk;
514               near = k;
515               mind = dist;
516             } else {
517               break;
518             }
519           }
520         }
521
522         if (near == g) { // make triangle
523           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
524           SMDS_MeshFace* face = myTool->AddFace(a, b, c);
525           meshDS->SetMeshElementOnShape(face, geomFaceID);
526         }
527         else { // make quadrangle
528           if (near + 1 > jup)
529             d = uv_e2[1].node;
530           else
531             d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
532           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
533           SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
534           meshDS->SetMeshElementOnShape(face, geomFaceID);
535
536           if (near + 1 < g) { // if d not is at g - make additional triangles
537             for (int k = near + 1; k < g; k++) {
538               c = quad->uv_grid[nbhoriz*k + 1].node;
539               if (k + 1 > jup)
540                 d = uv_e2[1].node;
541               else
542                 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
543               //SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
544               SMDS_MeshFace* face = myTool->AddFace(a, c, d);
545               meshDS->SetMeshElementOnShape(face, geomFaceID);
546             }
547           }
548           g = near;
549         }
550       }
551     }
552   }
553
554   bool isOk = true;
555   return isOk;
556 }
557
558 //=============================================================================
559 /*!
560  *  
561  */
562 //=============================================================================
563
564 FaceQuadStruct* StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh &         aMesh,
565                                                        const TopoDS_Shape & aShape)
566   //throw(SALOME_Exception)
567 {
568   const TopoDS_Face & F = TopoDS::Face(aShape);
569   const bool ignoreMediumNodes = _quadraticMesh;
570
571   // verify 1 wire only, with 4 edges
572   TopoDS_Vertex V;
573   list< TopoDS_Edge > edges;
574   list< int > nbEdgesInWire;
575   int nbWire = SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
576   if (nbWire != 1) {
577     error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
578     return 0;
579   }
580   FaceQuadStruct* quad = new FaceQuadStruct;
581   quad->uv_grid = 0;
582   quad->side.reserve(nbEdgesInWire.front());
583
584   int nbSides = 0;
585   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
586   if ( nbEdgesInWire.front() == 4 ) { // exactly 4 edges
587     for ( ; edgeIt != edges.end(); ++edgeIt, nbSides++ )
588       quad->side.push_back( new StdMeshers_FaceSide(F, *edgeIt, &aMesh,
589                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
590   }
591   else if ( nbEdgesInWire.front() > 4 ) { // more than 4 edges - try to unite some
592     list< TopoDS_Edge > sideEdges;
593     while ( !edges.empty()) {
594       sideEdges.clear();
595       sideEdges.splice( sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
596       bool sameSide = true;
597       while ( !edges.empty() && sameSide ) {
598         sameSide = SMESH_Algo::IsContinuous( sideEdges.back(), edges.front() );
599         if ( sameSide )
600           sideEdges.splice( sideEdges.end(), edges, edges.begin());
601       }
602       if ( nbSides == 0 ) { // go backward from the first edge
603         sameSide = true;
604         while ( !edges.empty() && sameSide ) {
605           sameSide = SMESH_Algo::IsContinuous( sideEdges.front(), edges.back() );
606           if ( sameSide )
607             sideEdges.splice( sideEdges.begin(), edges, --edges.end());
608         }
609       }
610       quad->side.push_back( new StdMeshers_FaceSide(F, sideEdges, &aMesh,
611                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
612       ++nbSides;
613     }
614   }
615   if (nbSides != 4) {
616 #ifdef _DEBUG_
617     cout << endl << "StdMeshers_Quadrangle_2D. Edge IDs of " << nbSides << " sides:";
618     for ( int i = 0; i < nbSides; ++i ) {
619       cout << " ( ";
620       for ( int e = 0; e < quad->side[i]->NbEdges(); ++e )
621         cout << myTool->GetMeshDS()->ShapeToIndex( quad->side[i]->Edge( e )) << " ";
622       cout << ")";
623     }
624     cout << endl;
625 #endif
626     if ( !nbSides )
627       nbSides = nbEdgesInWire.front();
628     error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
629     delete quad;
630     quad = 0;
631   }
632
633   return quad;
634 }
635
636 //=============================================================================
637 /*!
638  *  CheckAnd2Dcompute
639  */
640 //=============================================================================
641
642 FaceQuadStruct *StdMeshers_Quadrangle_2D::CheckAnd2Dcompute
643                            (SMESH_Mesh &         aMesh,
644                             const TopoDS_Shape & aShape,
645                             const bool           CreateQuadratic) //throw(SALOME_Exception)
646 {
647   _quadraticMesh = CreateQuadratic;
648
649   FaceQuadStruct *quad = CheckNbEdges(aMesh, aShape);
650
651   if(!quad) return 0;
652
653   // set normalized grid on unit square in parametric domain
654   bool stat = SetNormalizedGrid(aMesh, aShape, quad);
655   if(!stat) {
656     if(!quad)
657       delete quad;
658     quad = 0;
659   }
660
661   return quad;
662 }
663
664 //=============================================================================
665 /*!
666  *  
667  */
668 //=============================================================================
669
670 faceQuadStruct::~faceQuadStruct()
671 {
672   for (int i = 0; i < side.size(); i++) {
673     if (side[i])     delete side[i];
674   }
675   if (uv_grid)       delete [] uv_grid;
676 }
677
678 namespace {
679   inline const vector<UVPtStruct>& GetUVPtStructIn(FaceQuadStruct* quad, int i, int nbSeg)
680   {
681     bool   isXConst   = ( i == BOTTOM_SIDE || i == TOP_SIDE );
682     double constValue = ( i == BOTTOM_SIDE || i == LEFT_SIDE ) ? 0 : 1;
683     return
684       quad->isEdgeOut[i] ?
685       quad->side[i]->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
686       quad->side[i]->GetUVPtStruct(isXConst,constValue);
687   }
688 }
689
690 //=============================================================================
691 /*!
692  *  
693  */
694 //=============================================================================
695
696 bool StdMeshers_Quadrangle_2D::SetNormalizedGrid (SMESH_Mesh & aMesh,
697                                                   const TopoDS_Shape& aShape,
698                                                   FaceQuadStruct* & quad) //throw (SALOME_Exception)
699 {
700   // Algorithme décrit dans "Génération automatique de maillages"
701   // P.L. GEORGE, MASSON, Â§ 6.4.1 p. 84-85
702   // traitement dans le domaine paramétrique 2d u,v
703   // transport - projection sur le carré unité
704
705 //  MESSAGE("StdMeshers_Quadrangle_2D::SetNormalizedGrid");
706 //  const TopoDS_Face& F = TopoDS::Face(aShape);
707
708   // 1 --- find orientation of the 4 edges, by test on extrema
709
710   //      max             min                    0     x1     1
711   //     |<----north-2-------^                a3 -------------> a2
712   //     |                   |                   ^1          1^
713   //    west-3            east-1 =right          |            |
714   //     |                   |         ==>       |            |
715   //  y0 |                   | y1                |            |
716   //     |                   |                   |0          0|
717   //     v----south-0-------->                a0 -------------> a1
718   //      min             max                    0     x0     1
719   //             =down
720   //
721
722   // 3 --- 2D normalized values on unit square [0..1][0..1]
723
724   int nbhoriz  = Min(quad->side[0]->NbPoints(), quad->side[2]->NbPoints());
725   int nbvertic = Min(quad->side[1]->NbPoints(), quad->side[3]->NbPoints());
726
727   quad->isEdgeOut[0] = (quad->side[0]->NbPoints() > quad->side[2]->NbPoints());
728   quad->isEdgeOut[1] = (quad->side[1]->NbPoints() > quad->side[3]->NbPoints());
729   quad->isEdgeOut[2] = (quad->side[2]->NbPoints() > quad->side[0]->NbPoints());
730   quad->isEdgeOut[3] = (quad->side[3]->NbPoints() > quad->side[1]->NbPoints());
731
732   UVPtStruct *uv_grid = quad->uv_grid = new UVPtStruct[nbvertic * nbhoriz];
733
734   const vector<UVPtStruct>& uv_e0 = GetUVPtStructIn( quad, 0, nbhoriz - 1 );
735   const vector<UVPtStruct>& uv_e1 = GetUVPtStructIn( quad, 1, nbvertic - 1 );
736   const vector<UVPtStruct>& uv_e2 = GetUVPtStructIn( quad, 2, nbhoriz - 1 );
737   const vector<UVPtStruct>& uv_e3 = GetUVPtStructIn( quad, 3, nbvertic - 1 );
738
739   if ( uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty() )
740     //return error( "Can't find nodes on sides");
741     return error( COMPERR_BAD_INPUT_MESH );
742
743   // nodes Id on "in" edges
744   if (! quad->isEdgeOut[0]) {
745     int j = 0;
746     for (int i = 0; i < nbhoriz; i++) { // down
747       int ij = j * nbhoriz + i;
748       uv_grid[ij].node = uv_e0[i].node;
749     }
750   }
751   if (! quad->isEdgeOut[1]) {
752     int i = nbhoriz - 1;
753     for (int j = 0; j < nbvertic; j++) { // right
754       int ij = j * nbhoriz + i;
755       uv_grid[ij].node = uv_e1[j].node;
756     }
757   }
758   if (! quad->isEdgeOut[2]) {
759     int j = nbvertic - 1;
760     for (int i = 0; i < nbhoriz; i++) { // up
761       int ij = j * nbhoriz + i;
762       uv_grid[ij].node = uv_e2[i].node;
763     }
764   }
765   if (! quad->isEdgeOut[3]) {
766     int i = 0;
767     for (int j = 0; j < nbvertic; j++) { // left
768       int ij = j * nbhoriz + i;
769       uv_grid[ij].node = uv_e3[j].node;
770     }
771   }
772
773   // normalized 2d values on grid
774   for (int i = 0; i < nbhoriz; i++)
775   {
776     for (int j = 0; j < nbvertic; j++)
777     {
778       int ij = j * nbhoriz + i;
779       // --- droite i cste : x = x0 + y(x1-x0)
780       double x0 = uv_e0[i].normParam;   // bas - sud
781       double x1 = uv_e2[i].normParam;   // haut - nord
782       // --- droite j cste : y = y0 + x(y1-y0)
783       double y0 = uv_e3[j].normParam;   // gauche-ouest
784       double y1 = uv_e1[j].normParam;   // droite - est
785       // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
786       double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
787       double y = y0 + x * (y1 - y0);
788       uv_grid[ij].x = x;
789       uv_grid[ij].y = y;
790       //MESSAGE("-xy-01 "<<x0<<" "<<x1<<" "<<y0<<" "<<y1);
791       //MESSAGE("-xy-norm "<<i<<" "<<j<<" "<<x<<" "<<y);
792     }
793   }
794
795   // 4 --- projection on 2d domain (u,v)
796   gp_UV a0( uv_e0.front().u, uv_e0.front().v );
797   gp_UV a1( uv_e0.back().u,  uv_e0.back().v );
798   gp_UV a2( uv_e2.back().u,  uv_e2.back().v );
799   gp_UV a3( uv_e2.front().u, uv_e2.front().v );
800
801   for (int i = 0; i < nbhoriz; i++)
802   {
803     for (int j = 0; j < nbvertic; j++)
804     {
805       int ij = j * nbhoriz + i;
806       double x = uv_grid[ij].x;
807       double y = uv_grid[ij].y;
808       double param_0 = uv_e0[0].normParam + x * (uv_e0.back().normParam - uv_e0[0].normParam); // sud
809       double param_2 = uv_e2[0].normParam + x * (uv_e2.back().normParam - uv_e2[0].normParam); // nord
810       double param_1 = uv_e1[0].normParam + y * (uv_e1.back().normParam - uv_e1[0].normParam); // est
811       double param_3 = uv_e3[0].normParam + y * (uv_e3.back().normParam - uv_e3[0].normParam); // ouest
812
813       //MESSAGE("params "<<param_0<<" "<<param_1<<" "<<param_2<<" "<<param_3);
814       gp_UV p0 = quad->side[0]->Value2d(param_0).XY();
815       gp_UV p1 = quad->side[1]->Value2d(param_1).XY();
816       gp_UV p2 = quad->side[2]->Value2d(param_2).XY();
817       gp_UV p3 = quad->side[3]->Value2d(param_3).XY();
818
819       gp_UV uv = (1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3;
820       uv -= (1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3;
821
822       uv_grid[ij].u = uv.X();
823       uv_grid[ij].v = uv.Y();
824     }
825   }
826   return true;
827 }
828
829 //=======================================================================
830 //function : ShiftQuad
831 //purpose  : auxilary function for ComputeQuadPref
832 //=======================================================================
833
834 static void ShiftQuad(FaceQuadStruct* quad, const int num, bool)
835 {
836   StdMeshers_FaceSide* side[4] = { quad->side[0], quad->side[1], quad->side[2], quad->side[3] };
837   for (int i = BOTTOM_SIDE; i < NB_SIDES; ++i ) {
838     int id = ( i + num ) % NB_SIDES;
839     bool wasForward = ( i < TOP_SIDE );
840     bool newForward = ( id < TOP_SIDE );
841     if ( wasForward != newForward )
842       side[ i ]->Reverse();
843     quad->side[ id ] = side[ i ];
844   }
845 }
846
847 //=======================================================================
848 //function : CalcUV
849 //purpose  : auxilary function for ComputeQuadPref
850 //=======================================================================
851
852 static gp_UV CalcUV(double x0, double x1, double y0, double y1,
853                     FaceQuadStruct* quad,
854                     const gp_UV& a0, const gp_UV& a1,
855                     const gp_UV& a2, const gp_UV& a3)
856 {
857   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0 );
858   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
859   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1 );
860   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
861
862   double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
863   double y = y0 + x * (y1 - y0);
864
865   double param_b = uv_eb[0].normParam + x * (uv_eb.back().normParam - uv_eb[0].normParam);
866   double param_t = uv_et[0].normParam + x * (uv_et.back().normParam - uv_et[0].normParam);
867   double param_r = uv_er[0].normParam + y * (uv_er.back().normParam - uv_er[0].normParam);
868   double param_l = uv_el[0].normParam + y * (uv_el.back().normParam - uv_el[0].normParam);
869
870   gp_UV p0 = quad->side[BOTTOM_SIDE]->Value2d(param_b).XY();
871   gp_UV p1 = quad->side[RIGHT_SIDE ]->Value2d(param_r).XY();
872   gp_UV p2 = quad->side[TOP_SIDE   ]->Value2d(param_t).XY();
873   gp_UV p3 = quad->side[LEFT_SIDE  ]->Value2d(param_l).XY();
874
875   gp_UV uv = p0 * (1 - y) + p1 * x + p2 * y + p3 * (1 - x);
876
877   uv -= (1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3;
878
879   return uv;
880 }
881
882 //=======================================================================
883 /*!
884  * Create only quandrangle faces
885  */
886 //=======================================================================
887
888 bool StdMeshers_Quadrangle_2D::ComputeQuadPref (SMESH_Mesh &        aMesh,
889                                                 const TopoDS_Shape& aShape,
890                                                 FaceQuadStruct*     quad)
891 {
892   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
893   const TopoDS_Face& F = TopoDS::Face(aShape);
894   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
895 //  const TopoDS_Wire& W = BRepTools::OuterWire(F);
896   bool WisF = true;
897 //   if(W.Orientation()==TopAbs_FORWARD) 
898 //     WisF = true;
899   //if(WisF) cout<<"W is FORWARD"<<endl;
900   //else cout<<"W is REVERSED"<<endl;
901 //   bool FisF = (F.Orientation()==TopAbs_FORWARD);
902 //   if(!FisF) WisF = !WisF;
903 //  WisF = FisF;
904   int i,j,geomFaceID = meshDS->ShapeToIndex( F );
905
906   int nb = quad->side[0]->NbPoints();
907   int nr = quad->side[1]->NbPoints();
908   int nt = quad->side[2]->NbPoints();
909   int nl = quad->side[3]->NbPoints();
910   int dh = abs(nb-nt);
911   int dv = abs(nr-nl);
912
913   if( dh>=dv ) {
914     if( nt>nb ) {
915       // it is a base case => not shift quad but me be replacement is need
916       ShiftQuad(quad,0,WisF);
917     }
918     else {
919       // we have to shift quad on 2
920       ShiftQuad(quad,2,WisF);
921     }
922   }
923   else {
924     if( nr>nl ) {
925       // we have to shift quad on 1
926       ShiftQuad(quad,1,WisF);
927     }
928     else {
929       // we have to shift quad on 3
930       ShiftQuad(quad,3,WisF);
931     }
932   }
933
934   nb = quad->side[0]->NbPoints();
935   nr = quad->side[1]->NbPoints();
936   nt = quad->side[2]->NbPoints();
937   nl = quad->side[3]->NbPoints();
938   dh = abs(nb-nt);
939   dv = abs(nr-nl);
940   int nbh  = Max(nb,nt);
941   int nbv = Max(nr,nl);
942   int addh = 0;
943   int addv = 0;
944
945   // orientation of face and 3 main domain for future faces
946   //       0   top    1
947   //      1------------1
948   //       |   |  |   |
949   //       |   |  |   |
950   //       | L |  | R |
951   //  left |   |  |   | rigth
952   //       |  /    \  |
953   //       | /  C   \ |
954   //       |/        \|
955   //      0------------0
956   //       0  bottom  1
957
958   if(dh>dv) {
959     addv = (dh-dv)/2;
960     nbv = nbv + addv;
961   }
962   else { // dv>=dh
963     addh = (dv-dh)/2;
964     nbh = nbh + addh;
965   }
966
967   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0 );
968   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
969   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1 );
970   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
971
972   // arrays for normalized params
973   //cout<<"Dump B:"<<endl;
974   TColStd_SequenceOfReal npb, npr, npt, npl;
975   for(i=0; i<nb; i++) {
976     npb.Append(uv_eb[i].normParam);
977     //cout<<"i="<<i<<" par="<<uv_eb[i].normParam<<" npar="<<uv_eb[i].normParam;
978     //const SMDS_MeshNode* N = uv_eb[i].node;
979     //cout<<" node("<<N->X()<<","<<N->Y()<<","<<N->Z()<<")"<<endl;
980   }
981   for(i=0; i<nr; i++) {
982     npr.Append(uv_er[i].normParam);
983   }
984   for(i=0; i<nt; i++) {
985     npt.Append(uv_et[i].normParam);
986   }
987   for(i=0; i<nl; i++) {
988     npl.Append(uv_el[i].normParam);
989   }
990
991   // add some params to right and left after the first param
992   // insert to right
993   int dr = nbv - nr;
994   double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
995   for(i=1; i<=dr; i++) {
996     npr.InsertAfter(1,npr.Value(2)-dpr);
997   }
998   // insert to left
999   int dl = nbv - nl;
1000   dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
1001   for(i=1; i<=dl; i++) {
1002     npl.InsertAfter(1,npl.Value(2)-dpr);
1003   }
1004   //cout<<"npb:";
1005   //for(i=1; i<=npb.Length(); i++) {
1006   //  cout<<" "<<npb.Value(i);
1007   //}
1008   //cout<<endl;
1009   
1010   gp_XY a0( uv_eb.front().u, uv_eb.front().v );
1011   gp_XY a1( uv_eb.back().u,  uv_eb.back().v );
1012   gp_XY a2( uv_et.back().u,  uv_et.back().v );
1013   gp_XY a3( uv_et.front().u, uv_et.front().v );
1014   //cout<<" a0("<<a0.X()<<","<<a0.Y()<<")"<<" a1("<<a1.X()<<","<<a1.Y()<<")"
1015   //    <<" a2("<<a2.X()<<","<<a2.Y()<<")"<<" a3("<<a3.X()<<","<<a3.Y()<<")"<<endl;
1016
1017   int nnn = Min(nr,nl);
1018   // auxilary sequence of XY for creation nodes
1019   // in the bottom part of central domain
1020   // it's length must be == nbv-nnn-1
1021   TColgp_SequenceOfXY UVL;
1022   TColgp_SequenceOfXY UVR;
1023
1024   // step1: create faces for left domain
1025   StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
1026   // add left nodes
1027   for(j=1; j<=nl; j++)
1028     NodesL.SetValue(1,j,uv_el[j-1].node);
1029   if(dl>0) {
1030     // add top nodes
1031     for(i=1; i<=dl; i++) 
1032       NodesL.SetValue(i+1,nl,uv_et[i].node);
1033     // create and add needed nodes
1034     TColgp_SequenceOfXY UVtmp;
1035     for(i=1; i<=dl; i++) {
1036       double x0 = npt.Value(i+1);
1037       double x1 = x0;
1038       // diagonal node
1039       double y0 = npl.Value(i+1);
1040       double y1 = npr.Value(i+1);
1041       gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1042       gp_Pnt P = S->Value(UV.X(),UV.Y());
1043       SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1044       meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1045       NodesL.SetValue(i+1,1,N);
1046       if(UVL.Length()<nbv-nnn-1) UVL.Append(UV);
1047       // internal nodes
1048       for(j=2; j<nl; j++) {
1049         double y0 = npl.Value(dl+j);
1050         double y1 = npr.Value(dl+j);
1051         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1052         gp_Pnt P = S->Value(UV.X(),UV.Y());
1053         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1054         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1055         NodesL.SetValue(i+1,j,N);
1056         if( i==dl ) UVtmp.Append(UV);
1057       }
1058     }
1059     for(i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
1060       UVL.Append(UVtmp.Value(i));
1061     }
1062     //cout<<"Dump NodesL:"<<endl;
1063     //for(i=1; i<=dl+1; i++) {
1064     //  cout<<"i="<<i;
1065     //  for(j=1; j<=nl; j++) {
1066     //    cout<<" ("<<NodesL.Value(i,j)->X()<<","<<NodesL.Value(i,j)->Y()<<","<<NodesL.Value(i,j)->Z()<<")";
1067     //  }
1068     //  cout<<endl;
1069     //}
1070     // create faces
1071     for(i=1; i<=dl; i++) {
1072       for(j=1; j<nl; j++) {
1073         if(WisF) {
1074           SMDS_MeshFace* F =
1075             myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
1076                             NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
1077           meshDS->SetMeshElementOnShape(F, geomFaceID);
1078         }
1079         else {
1080           SMDS_MeshFace* F =
1081             myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i,j+1),
1082                             NodesL.Value(i+1,j+1), NodesL.Value(i+1,j));
1083           meshDS->SetMeshElementOnShape(F, geomFaceID);
1084         }
1085       }
1086     }
1087   }
1088   else {
1089     // fill UVL using c2d
1090     for(i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
1091       UVL.Append( gp_UV ( uv_el[i].u, uv_el[i].v ));
1092     }
1093   }
1094
1095   // step2: create faces for right domain
1096   StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
1097   // add right nodes
1098   for(j=1; j<=nr; j++) 
1099     NodesR.SetValue(1,j,uv_er[nr-j].node);
1100   if(dr>0) {
1101     // add top nodes
1102     for(i=1; i<=dr; i++) 
1103       NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
1104     // create and add needed nodes
1105     TColgp_SequenceOfXY UVtmp;
1106     for(i=1; i<=dr; i++) {
1107       double x0 = npt.Value(nt-i);
1108       double x1 = x0;
1109       // diagonal node
1110       double y0 = npl.Value(i+1);
1111       double y1 = npr.Value(i+1);
1112       gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1113       gp_Pnt P = S->Value(UV.X(),UV.Y());
1114       SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1115       meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1116       NodesR.SetValue(i+1,nr,N);
1117       if(UVR.Length()<nbv-nnn-1) UVR.Append(UV);
1118       // internal nodes
1119       for(j=2; j<nr; j++) {
1120         double y0 = npl.Value(nbv-j+1);
1121         double y1 = npr.Value(nbv-j+1);
1122         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1123         gp_Pnt P = S->Value(UV.X(),UV.Y());
1124         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1125         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1126         NodesR.SetValue(i+1,j,N);
1127         if( i==dr ) UVtmp.Prepend(UV);
1128       }
1129     }
1130     for(i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
1131       UVR.Append(UVtmp.Value(i));
1132     }
1133     // create faces
1134     for(i=1; i<=dr; i++) {
1135       for(j=1; j<nr; j++) {
1136         if(WisF) {
1137           SMDS_MeshFace* F =
1138             myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
1139                             NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
1140           meshDS->SetMeshElementOnShape(F, geomFaceID);
1141         }
1142         else {
1143           SMDS_MeshFace* F =
1144             myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i,j+1),
1145                             NodesR.Value(i+1,j+1), NodesR.Value(i+1,j));
1146           meshDS->SetMeshElementOnShape(F, geomFaceID);
1147         }
1148       }
1149     }
1150   }
1151   else {
1152     // fill UVR using c2d
1153     for(i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
1154       UVR.Append( gp_UV( uv_er[i].u, uv_er[i].v ));
1155     }
1156   }
1157
1158   // step3: create faces for central domain
1159   StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
1160   // add first string using NodesL
1161   for(i=1; i<=dl+1; i++)
1162     NodesC.SetValue(1,i,NodesL(i,1));
1163   for(i=2; i<=nl; i++)
1164     NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
1165   // add last string using NodesR
1166   for(i=1; i<=dr+1; i++)
1167     NodesC.SetValue(nb,i,NodesR(i,nr));
1168   for(i=1; i<nr; i++)
1169     NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
1170   // add top nodes (last columns)
1171   for(i=dl+2; i<nbh-dr; i++) 
1172     NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
1173   // add bottom nodes (first columns)
1174   for(i=2; i<nb; i++)
1175     NodesC.SetValue(i,1,uv_eb[i-1].node);
1176
1177   // create and add needed nodes
1178   // add linear layers
1179   for(i=2; i<nb; i++) {
1180     double x0 = npt.Value(dl+i);
1181     double x1 = x0;
1182     for(j=1; j<nnn; j++) {
1183       double y0 = npl.Value(nbv-nnn+j);
1184       double y1 = npr.Value(nbv-nnn+j);
1185       gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1186       gp_Pnt P = S->Value(UV.X(),UV.Y());
1187       SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1188       meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1189       NodesC.SetValue(i,nbv-nnn+j,N);
1190     }
1191   }
1192   // add diagonal layers
1193   //cout<<"UVL.Length()="<<UVL.Length()<<" UVR.Length()="<<UVR.Length()<<endl;
1194   //cout<<"Dump UVL:"<<endl;
1195   //for(i=1; i<=UVL.Length(); i++) {
1196   //  cout<<" ("<<UVL.Value(i).X()<<","<<UVL.Value(i).Y()<<")";
1197   //}
1198   //cout<<endl;
1199   for(i=1; i<nbv-nnn; i++) {
1200     double du = UVR.Value(i).X() - UVL.Value(i).X();
1201     double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
1202     for(j=2; j<nb; j++) {
1203       double u = UVL.Value(i).X() + du*npb.Value(j);
1204       double v = UVL.Value(i).Y() + dv*npb.Value(j);
1205       gp_Pnt P = S->Value(u,v);
1206       SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1207       meshDS->SetNodeOnFace(N, geomFaceID, u, v);
1208       NodesC.SetValue(j,i+1,N);
1209     }
1210   }
1211   // create faces
1212   for(i=1; i<nb; i++) {
1213     for(j=1; j<nbv; j++) {
1214       if(WisF) {
1215         SMDS_MeshFace* F =
1216           myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
1217                           NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
1218         meshDS->SetMeshElementOnShape(F, geomFaceID);
1219       }
1220       else {
1221         SMDS_MeshFace* F =
1222           myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i,j+1),
1223                           NodesC.Value(i+1,j+1), NodesC.Value(i+1,j));
1224         meshDS->SetMeshElementOnShape(F, geomFaceID);
1225       }
1226     }
1227   }
1228
1229   bool isOk = true;
1230   return isOk;
1231 }
1232