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