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