Salome HOME
merge from branch BR_SMDS_MEMIMP 29 nov 2010
[modules/smesh.git] / src / StdMeshers / StdMeshers_Quadrangle_2D.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 //  File   : StdMeshers_Quadrangle_2D.cxx
23 //  Author : Paul RASCLE, EDF
24 //  Module : SMESH
25
26 #include "StdMeshers_Quadrangle_2D.hxx"
27
28 #include "StdMeshers_FaceSide.hxx"
29
30 #include "StdMeshers_QuadrangleParams.hxx"
31
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_subMesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_Block.hxx"
37 #include "SMESH_Comment.hxx"
38
39 #include "SMDS_MeshElement.hxx"
40 #include "SMDS_MeshNode.hxx"
41 #include "SMDS_EdgePosition.hxx"
42 #include "SMDS_FacePosition.hxx"
43
44 #include <BRep_Tool.hxx>
45 #include <Geom_Surface.hxx>
46 #include <NCollection_DefineArray2.hxx>
47 #include <Precision.hxx>
48 #include <TColStd_SequenceOfReal.hxx>
49 #include <TColStd_SequenceOfInteger.hxx>
50 #include <TColgp_SequenceOfXY.hxx>
51 #include <TopExp.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TopTools_MapOfShape.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,
80                                                     SMESH_Gen* gen)
81      : SMESH_2D_Algo(hypId, studyId, gen)
82 {
83   MESSAGE("StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D");
84   _name = "Quadrangle_2D";
85   _shapeType = (1 << TopAbs_FACE);
86   _compatibleHypothesis.push_back("QuadrangleParams");
87   _compatibleHypothesis.push_back("QuadranglePreference");
88   _compatibleHypothesis.push_back("TrianglePreference");
89   myTool = 0;
90 }
91
92 //=============================================================================
93 /*!
94  *  
95  */
96 //=============================================================================
97
98 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
99 {
100   MESSAGE("StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D");
101 }
102
103 //=============================================================================
104 /*!
105  *  
106  */
107 //=============================================================================
108
109 bool StdMeshers_Quadrangle_2D::CheckHypothesis
110                          (SMESH_Mesh&                          aMesh,
111                           const TopoDS_Shape&                  aShape,
112                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
113 {
114   bool isOk = true;
115   aStatus = SMESH_Hypothesis::HYP_OK;
116
117   const list <const SMESHDS_Hypothesis * >& hyps =
118     GetUsedHypothesis(aMesh, aShape, false);
119   const SMESHDS_Hypothesis * aHyp = 0;
120
121   myTriaVertexID = -1;
122   myQuadType = QUAD_STANDARD;
123   myQuadranglePreference = false;
124   myTrianglePreference = false;
125
126   bool isFirstParams = true;
127
128   // First assigned hypothesis (if any) is processed now
129   if (hyps.size() > 0) {
130     aHyp = hyps.front();
131     if (strcmp("QuadrangleParams", aHyp->GetName()) == 0) {
132       const StdMeshers_QuadrangleParams* aHyp1 = 
133         (const StdMeshers_QuadrangleParams*)aHyp;
134       myTriaVertexID = aHyp1->GetTriaVertex();
135       myQuadType = aHyp1->GetQuadType();
136       if (myQuadType == QUAD_QUADRANGLE_PREF ||
137           myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
138         myQuadranglePreference = true;
139       else if (myQuadType == QUAD_TRIANGLE_PREF)
140         myTrianglePreference = true;
141     }
142     else if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
143       isFirstParams = false;
144       myQuadranglePreference = true;
145     }
146     else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
147       isFirstParams = false;
148       myTrianglePreference = true; 
149     }
150     else {
151       isFirstParams = false;
152     }
153   }
154
155   // Second(last) assigned hypothesis (if any) is processed now
156   if (hyps.size() > 1) {
157     aHyp = hyps.back();
158     if (isFirstParams) {
159       if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
160         myQuadranglePreference = true;
161         myTrianglePreference = false; 
162         myQuadType = QUAD_STANDARD;
163       }
164       else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
165         myQuadranglePreference = false;
166         myTrianglePreference = true; 
167         myQuadType = QUAD_STANDARD;
168       }
169     }
170     else {
171       const StdMeshers_QuadrangleParams* aHyp2 = 
172         (const StdMeshers_QuadrangleParams*)aHyp;
173       myTriaVertexID = aHyp2->GetTriaVertex();
174
175       if (!myQuadranglePreference && !myTrianglePreference) { // priority of hypos
176         myQuadType = aHyp2->GetQuadType();
177         if (myQuadType == QUAD_QUADRANGLE_PREF ||
178             myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
179           myQuadranglePreference = true;
180         else if (myQuadType == QUAD_TRIANGLE_PREF)
181           myTrianglePreference = true;
182       }
183     }
184   }
185
186   return isOk;
187 }
188
189 //=============================================================================
190 /*!
191  *  
192  */
193 //=============================================================================
194
195 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
196                                         const TopoDS_Shape& aShape)// throw (SALOME_Exception)
197 {
198   // PAL14921. Enable catching std::bad_alloc and Standard_OutOfMemory outside
199   //Unexpect aCatchSalomeException);
200
201   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
202   aMesh.GetSubMesh(aShape);
203
204   SMESH_MesherHelper helper (aMesh);
205   myTool = &helper;
206
207   _quadraticMesh = myTool->IsQuadraticSubMesh(aShape);
208
209   FaceQuadStruct *quad = CheckNbEdges(aMesh, aShape);
210   std::auto_ptr<FaceQuadStruct> quadDeleter (quad); // to delete quad at exit from Compute()
211   if (!quad)
212     return false;
213
214   if (myQuadranglePreference) {
215     int n1 = quad->side[0]->NbPoints();
216     int n2 = quad->side[1]->NbPoints();
217     int n3 = quad->side[2]->NbPoints();
218     int n4 = quad->side[3]->NbPoints();
219     int nfull = n1+n2+n3+n4;
220     int ntmp = nfull/2;
221     ntmp = ntmp*2;
222     if (nfull == ntmp && ((n1 != n3) || (n2 != n4))) {
223       // special path for using only quandrangle faces
224       bool ok = ComputeQuadPref(aMesh, aShape, quad);
225       return ok;
226     }
227   }
228   else if (myQuadType == QUAD_REDUCED) {
229     int n1 = quad->side[0]->NbPoints();
230     int n2 = quad->side[1]->NbPoints();
231     int n3 = quad->side[2]->NbPoints();
232     int n4 = quad->side[3]->NbPoints();
233     int n13 = n1 - n3;
234     int n24 = n2 - n4;
235     int n13tmp = n13/2; n13tmp = n13tmp*2;
236     int n24tmp = n24/2; n24tmp = n24tmp*2;
237     if ((n1 == n3 && n2 != n4 && n24tmp == n24) ||
238         (n2 == n4 && n1 != n3 && n13tmp == n13)) {
239       bool ok = ComputeReduced(aMesh, aShape, quad);
240       return ok;
241     }
242   }
243
244   // set normalized grid on unit square in parametric domain
245   
246   if (!SetNormalizedGrid(aMesh, aShape, quad))
247     return false;
248
249   // --- compute 3D values on points, store points & quadrangles
250
251   int nbdown  = quad->side[0]->NbPoints();
252   int nbup    = quad->side[2]->NbPoints();
253
254   int nbright = quad->side[1]->NbPoints();
255   int nbleft  = quad->side[3]->NbPoints();
256
257   int nbhoriz  = Min(nbdown, nbup);
258   int nbvertic = Min(nbright, nbleft);
259
260   const TopoDS_Face& F = TopoDS::Face(aShape);
261   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
262
263   // internal mesh nodes
264   int i, j, geomFaceID = meshDS->ShapeToIndex(F);
265   for (i = 1; i < nbhoriz - 1; i++) {
266     for (j = 1; j < nbvertic - 1; j++) {
267       int ij = j * nbhoriz + i;
268       double u = quad->uv_grid[ij].u;
269       double v = quad->uv_grid[ij].v;
270       gp_Pnt P = S->Value(u, v);
271       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
272       meshDS->SetNodeOnFace(node, geomFaceID, u, v);
273       quad->uv_grid[ij].node = node;
274     }
275   }
276   
277   // mesh faces
278
279   //             [2]
280   //      --.--.--.--.--.--  nbvertic
281   //     |                 | ^
282   //     |                 | ^
283   // [3] |                 | ^ j  [1]
284   //     |                 | ^
285   //     |                 | ^
286   //      ---.----.----.---  0
287   //     0 > > > > > > > > nbhoriz
288   //              i
289   //             [0]
290   
291   i = 0;
292   int ilow = 0;
293   int iup = nbhoriz - 1;
294   if (quad->isEdgeOut[3]) { ilow++; } else { if (quad->isEdgeOut[1]) iup--; }
295   
296   int jlow = 0;
297   int jup = nbvertic - 1;
298   if (quad->isEdgeOut[0]) { jlow++; } else { if (quad->isEdgeOut[2]) jup--; }
299   
300   // regular quadrangles
301   for (i = ilow; i < iup; i++) {
302     for (j = jlow; j < jup; j++) {
303       const SMDS_MeshNode *a, *b, *c, *d;
304       a = quad->uv_grid[j * nbhoriz + i].node;
305       b = quad->uv_grid[j * nbhoriz + i + 1].node;
306       c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
307       d = quad->uv_grid[(j + 1) * nbhoriz + i].node;
308       SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
309       if (face) {
310         meshDS->SetMeshElementOnShape(face, geomFaceID);
311       }
312     }
313   }
314
315   const vector<UVPtStruct>& uv_e0 = quad->side[0]->GetUVPtStruct(true,0);
316   const vector<UVPtStruct>& uv_e1 = quad->side[1]->GetUVPtStruct(false,1);
317   const vector<UVPtStruct>& uv_e2 = quad->side[2]->GetUVPtStruct(true,1);
318   const vector<UVPtStruct>& uv_e3 = quad->side[3]->GetUVPtStruct(false,0);
319
320   if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
321     return error(COMPERR_BAD_INPUT_MESH);
322
323   double eps = Precision::Confusion();
324
325   // Boundary quadrangles
326   
327   if (quad->isEdgeOut[0]) {
328     // Down edge is out
329     // 
330     // |___|___|___|___|___|___|
331     // |   |   |   |   |   |   |
332     // |___|___|___|___|___|___|
333     // |   |   |   |   |   |   |
334     // |___|___|___|___|___|___| __ first row of the regular grid
335     // .  .  .  .  .  .  .  .  . __ down edge nodes
336     // 
337     // >->->->->->->->->->->->-> -- direction of processing
338       
339     int g = 0; // number of last processed node in the regular grid
340     
341     // number of last node of the down edge to be processed
342     int stop = nbdown - 1;
343     // if right edge is out, we will stop at a node, previous to the last one
344     if (quad->isEdgeOut[1]) stop--;
345     
346     // for each node of the down edge find nearest node
347     // in the first row of the regular grid and link them
348     for (i = 0; i < stop; i++) {
349       const SMDS_MeshNode *a, *b, *c, *d;
350       a = uv_e0[i].node;
351       b = uv_e0[i + 1].node;
352       gp_Pnt pb (b->X(), b->Y(), b->Z());
353       
354       // find node c in the regular grid, which will be linked with node b
355       int near = g;
356       if (i == stop - 1) {
357         // right bound reached, link with the rightmost node
358         near = iup;
359         c = quad->uv_grid[nbhoriz + iup].node;
360       }
361       else {
362         // find in the grid node c, nearest to the b
363         double mind = RealLast();
364         for (int k = g; k <= iup; k++) {
365           
366           const SMDS_MeshNode *nk;
367           if (k < ilow) // this can be, if left edge is out
368             nk = uv_e3[1].node; // get node from the left edge
369           else
370             nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
371
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 = myTool->AddFace(a, b, c);
386         if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
387       }
388       else { // make quadrangle
389         if (near - 1 < ilow)
390           d = uv_e3[1].node;
391         else
392           d = quad->uv_grid[nbhoriz + near - 1].node;
393         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
394         
395         if (!myTrianglePreference){
396           SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
397           if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
398         }
399         else {
400           SplitQuad(meshDS, geomFaceID, a, b, c, d);
401         }
402
403         // if node d is not at position g - make additional triangles
404         if (near - 1 > g) {
405           for (int k = near - 1; k > g; k--) {
406             c = quad->uv_grid[nbhoriz + k].node;
407             if (k - 1 < ilow)
408               d = uv_e3[1].node;
409             else
410               d = quad->uv_grid[nbhoriz + k - 1].node;
411             SMDS_MeshFace* face = myTool->AddFace(a, c, d);
412             if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
413           }
414         }
415         g = near;
416       }
417     }
418   } else {
419     if (quad->isEdgeOut[2]) {
420       // Up edge is out
421       // 
422       // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
423       // 
424       // .  .  .  .  .  .  .  .  . __ up edge nodes
425       //  ___ ___ ___ ___ ___ ___  __ first row of the regular grid
426       // |   |   |   |   |   |   |
427       // |___|___|___|___|___|___|
428       // |   |   |   |   |   |   |
429       // |___|___|___|___|___|___|
430       // |   |   |   |   |   |   |
431
432       int g = nbhoriz - 1; // last processed node in the regular grid
433
434       int stop = 0;
435       // if left edge is out, we will stop at a second node
436       if (quad->isEdgeOut[3]) stop++;
437
438       // for each node of the up edge find nearest node
439       // in the first row of the regular grid and link them
440       for (i = nbup - 1; i > stop; i--) {
441         const SMDS_MeshNode *a, *b, *c, *d;
442         a = uv_e2[i].node;
443         b = uv_e2[i - 1].node;
444         gp_Pnt pb (b->X(), b->Y(), b->Z());
445
446         // find node c in the grid, which will be linked with node b
447         int near = g;
448         if (i == stop + 1) { // left bound reached, link with the leftmost node
449           c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
450           near = ilow;
451         } else {
452           // find node c in the grid, nearest to the b
453           double mind = RealLast();
454           for (int k = g; k >= ilow; k--) {
455             const SMDS_MeshNode *nk;
456             if (k > iup)
457               nk = uv_e1[nbright - 2].node;
458             else
459               nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
460             gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
461             double dist = pb.Distance(pnk);
462             if (dist < mind - eps) {
463               c = nk;
464               near = k;
465               mind = dist;
466             } else {
467               break;
468             }
469           }
470         }
471
472         if (near == g) { // make triangle
473           SMDS_MeshFace* face = myTool->AddFace(a, b, c);
474           if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
475         }
476         else { // make quadrangle
477           if (near + 1 > iup)
478             d = uv_e1[nbright - 2].node;
479           else
480             d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
481           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
482           if (!myTrianglePreference){
483             SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
484             if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
485           }
486           else {
487             SplitQuad(meshDS, geomFaceID, a, b, c, d);
488           }
489
490           if (near + 1 < g) { // if d not is at g - make additional triangles
491             for (int k = near + 1; k < g; k++) {
492               c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
493               if (k + 1 > iup)
494                 d = uv_e1[nbright - 2].node;
495               else
496                 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
497               SMDS_MeshFace* face = myTool->AddFace(a, c, d);
498               if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
499             }
500           }
501           g = near;
502         }
503       }
504     }
505   }
506
507   // right or left boundary quadrangles
508   if (quad->isEdgeOut[1]) {
509 //    MESSAGE("right edge is out");
510     int g = 0; // last processed node in the grid
511     int stop = nbright - 1;
512     if (quad->isEdgeOut[2]) stop--;
513     for (i = 0; i < stop; i++) {
514       const SMDS_MeshNode *a, *b, *c, *d;
515       a = uv_e1[i].node;
516       b = uv_e1[i + 1].node;
517       gp_Pnt pb (b->X(), b->Y(), b->Z());
518
519       // find node c in the grid, nearest to the b
520       int near = g;
521       if (i == stop - 1) { // up bondary reached
522         c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
523         near = jup;
524       } else {
525         double mind = RealLast();
526         for (int k = g; k <= jup; k++) {
527           const SMDS_MeshNode *nk;
528           if (k < jlow)
529             nk = uv_e0[nbdown - 2].node;
530           else
531             nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
532           gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
533           double dist = pb.Distance(pnk);
534           if (dist < mind - eps) {
535             c = nk;
536             near = k;
537             mind = dist;
538           } else {
539             break;
540           }
541         }
542       }
543
544       if (near == g) { // make triangle
545         SMDS_MeshFace* face = myTool->AddFace(a, b, c);
546         if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
547       }
548       else { // make quadrangle
549         if (near - 1 < jlow)
550           d = uv_e0[nbdown - 2].node;
551         else
552           d = quad->uv_grid[nbhoriz*near - 2].node;
553         //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
554
555         if (!myTrianglePreference){
556           SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
557           if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
558         }
559         else {
560           SplitQuad(meshDS, geomFaceID, a, b, c, d);
561         }
562
563         if (near - 1 > g) { // if d not is at g - make additional triangles
564           for (int k = near - 1; k > g; k--) {
565             c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
566             if (k - 1 < jlow)
567               d = uv_e0[nbdown - 2].node;
568             else
569               d = quad->uv_grid[nbhoriz*k - 2].node;
570             SMDS_MeshFace* face = myTool->AddFace(a, c, d);
571             if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
572           }
573         }
574         g = near;
575       }
576     }
577   } else {
578     if (quad->isEdgeOut[3]) {
579 //      MESSAGE("left edge is out");
580       int g = nbvertic - 1; // last processed node in the grid
581       int stop = 0;
582       if (quad->isEdgeOut[0]) stop++;
583       for (i = nbleft - 1; i > stop; i--) {
584         const SMDS_MeshNode *a, *b, *c, *d;
585         a = uv_e3[i].node;
586         b = uv_e3[i - 1].node;
587         gp_Pnt pb (b->X(), b->Y(), b->Z());
588
589         // find node c in the grid, nearest to the b
590         int near = g;
591         if (i == stop + 1) { // down bondary reached
592           c = quad->uv_grid[nbhoriz*jlow + 1].node;
593           near = jlow;
594         } else {
595           double mind = RealLast();
596           for (int k = g; k >= jlow; k--) {
597             const SMDS_MeshNode *nk;
598             if (k > jup)
599               nk = uv_e2[1].node;
600             else
601               nk = quad->uv_grid[nbhoriz*k + 1].node;
602             gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
603             double dist = pb.Distance(pnk);
604             if (dist < mind - eps) {
605               c = nk;
606               near = k;
607               mind = dist;
608             } else {
609               break;
610             }
611           }
612         }
613
614         if (near == g) { // make triangle
615           SMDS_MeshFace* face = myTool->AddFace(a, b, c);
616           if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
617         }
618         else { // make quadrangle
619           if (near + 1 > jup)
620             d = uv_e2[1].node;
621           else
622             d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
623           //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
624           if (!myTrianglePreference){
625             SMDS_MeshFace* face = myTool->AddFace(a, b, c, d);
626             if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
627           }
628           else {
629             SplitQuad(meshDS, geomFaceID, a, b, c, d);
630           }
631
632           if (near + 1 < g) { // if d not is at g - make additional triangles
633             for (int k = near + 1; k < g; k++) {
634               c = quad->uv_grid[nbhoriz*k + 1].node;
635               if (k + 1 > jup)
636                 d = uv_e2[1].node;
637               else
638                 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
639               SMDS_MeshFace* face = myTool->AddFace(a, c, d);
640               if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
641             }
642           }
643           g = near;
644         }
645       }
646     }
647   }
648
649   bool isOk = true;
650   return isOk;
651 }
652
653
654 //=============================================================================
655 /*!
656  *  Evaluate
657  */
658 //=============================================================================
659
660 bool StdMeshers_Quadrangle_2D::Evaluate(SMESH_Mesh& aMesh,
661                                         const TopoDS_Shape& aShape,
662                                         MapShapeNbElems& aResMap)
663
664 {
665   aMesh.GetSubMesh(aShape);
666
667   std::vector<int> aNbNodes(4);
668   bool IsQuadratic = false;
669   if (!CheckNbEdgesForEvaluate(aMesh, aShape, aResMap, aNbNodes, IsQuadratic)) {
670     std::vector<int> aResVec(SMDSEntity_Last);
671     for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
672     SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
673     aResMap.insert(std::make_pair(sm,aResVec));
674     SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
675     smError.reset(new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
676     return false;
677   }
678
679   if (myQuadranglePreference) {
680     int n1 = aNbNodes[0];
681     int n2 = aNbNodes[1];
682     int n3 = aNbNodes[2];
683     int n4 = aNbNodes[3];
684     int nfull = n1+n2+n3+n4;
685     int ntmp = nfull/2;
686     ntmp = ntmp*2;
687     if (nfull==ntmp && ((n1!=n3) || (n2!=n4))) {
688       // special path for using only quandrangle faces
689       return EvaluateQuadPref(aMesh, aShape, aNbNodes, aResMap, IsQuadratic);
690       //return true;
691     }
692   }
693
694   int nbdown  = aNbNodes[0];
695   int nbup    = aNbNodes[2];
696
697   int nbright = aNbNodes[1];
698   int nbleft  = aNbNodes[3];
699
700   int nbhoriz  = Min(nbdown, nbup);
701   int nbvertic = Min(nbright, nbleft);
702
703   int dh = Max(nbdown, nbup) - nbhoriz;
704   int dv = Max(nbright, nbleft) - nbvertic;
705
706   //int kdh = 0;
707   //if (dh>0) kdh = 1;
708   //int kdv = 0;
709   //if (dv>0) kdv = 1;
710
711   int nbNodes = (nbhoriz-2)*(nbvertic-2);
712   //int nbFaces3 = dh + dv + kdh*(nbvertic-1)*2 + kdv*(nbhoriz-1)*2;
713   int nbFaces3 = dh + dv;
714   //if (kdh==1 && kdv==1) nbFaces3 -= 2;
715   //if (dh>0 && dv>0) nbFaces3 -= 2;
716   //int nbFaces4 = (nbhoriz-1-kdh)*(nbvertic-1-kdv);
717   int nbFaces4 = (nbhoriz-1)*(nbvertic-1);
718
719   std::vector<int> aVec(SMDSEntity_Last);
720   for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
721   if (IsQuadratic) {
722     aVec[SMDSEntity_Quad_Triangle] = nbFaces3;
723     aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4;
724     int nbbndedges = nbdown + nbup + nbright + nbleft -4;
725     int nbintedges = (nbFaces4*4 + nbFaces3*3 - nbbndedges) / 2;
726     aVec[SMDSEntity_Node] = nbNodes + nbintedges;
727     if (aNbNodes.size()==5) {
728       aVec[SMDSEntity_Quad_Triangle] = nbFaces3 + aNbNodes[3] -1;
729       aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4 - aNbNodes[3] +1;
730     }
731   }
732   else {
733     aVec[SMDSEntity_Node] = nbNodes;
734     aVec[SMDSEntity_Triangle] = nbFaces3;
735     aVec[SMDSEntity_Quadrangle] = nbFaces4;
736     if (aNbNodes.size()==5) {
737       aVec[SMDSEntity_Triangle] = nbFaces3 + aNbNodes[3] - 1;
738       aVec[SMDSEntity_Quadrangle] = nbFaces4 - aNbNodes[3] + 1;
739     }
740   }
741   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
742   aResMap.insert(std::make_pair(sm,aVec));
743
744   return true;
745 }
746
747
748 //================================================================================
749 /*!
750  * \brief Return true if only two given edges meat at their common vertex
751  */
752 //================================================================================
753
754 static bool twoEdgesMeatAtVertex(const TopoDS_Edge& e1,
755                                  const TopoDS_Edge& e2,
756                                  SMESH_Mesh &       mesh)
757 {
758   TopoDS_Vertex v;
759   if (!TopExp::CommonVertex(e1, e2, v))
760     return false;
761   TopTools_ListIteratorOfListOfShape ancestIt(mesh.GetAncestors(v));
762   for (; ancestIt.More() ; ancestIt.Next())
763     if (ancestIt.Value().ShapeType() == TopAbs_EDGE)
764       if (!e1.IsSame(ancestIt.Value()) && !e2.IsSame(ancestIt.Value()))
765         return false;
766   return true;
767 }
768
769 //=============================================================================
770 /*!
771  *  
772  */
773 //=============================================================================
774
775 FaceQuadStruct* StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh &         aMesh,
776                                                        const TopoDS_Shape & aShape)
777   //throw(SALOME_Exception)
778 {
779   TopoDS_Face F = TopoDS::Face(aShape);
780   if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
781   const bool ignoreMediumNodes = _quadraticMesh;
782
783   // verify 1 wire only, with 4 edges
784   TopoDS_Vertex V;
785   list< TopoDS_Edge > edges;
786   list< int > nbEdgesInWire;
787   int nbWire = SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
788   if (nbWire != 1) {
789     error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
790     return 0;
791   }
792   FaceQuadStruct* quad = new FaceQuadStruct;
793   quad->uv_grid = 0;
794   quad->side.reserve(nbEdgesInWire.front());
795
796   int nbSides = 0;
797   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
798   if (nbEdgesInWire.front() == 3) // exactly 3 edges
799   {
800     SMESH_Comment comment;
801     SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
802     if (myTriaVertexID == -1)
803     {
804       comment << "No Base vertex parameter provided for a trilateral geometrical face";
805     }
806     else
807     {
808       TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
809       if (!V.IsNull()) {
810         TopoDS_Edge E1,E2,E3;
811         for (; edgeIt != edges.end(); ++edgeIt) {
812           TopoDS_Edge E =  *edgeIt;
813           TopoDS_Vertex VF, VL;
814           TopExp::Vertices(E, VF, VL, true);
815           if (VF.IsSame(V))
816             E1 = E;
817           else if (VL.IsSame(V))
818             E3 = E;
819           else
820             E2 = E;
821         }
822         if (!E1.IsNull() && !E2.IsNull() && !E3.IsNull())
823         {
824           quad->side.push_back(new StdMeshers_FaceSide(F, E1, &aMesh, true, ignoreMediumNodes));
825           quad->side.push_back(new StdMeshers_FaceSide(F, E2, &aMesh, true, ignoreMediumNodes));
826           quad->side.push_back(new StdMeshers_FaceSide(F, E3, &aMesh, false,ignoreMediumNodes));
827           const vector<UVPtStruct>& UVPSleft  = quad->side[0]->GetUVPtStruct(true,0);
828           /*  vector<UVPtStruct>& UVPStop   = */quad->side[1]->GetUVPtStruct(false,1);
829           /*  vector<UVPtStruct>& UVPSright = */quad->side[2]->GetUVPtStruct(true,1);
830           const SMDS_MeshNode* aNode = UVPSleft[0].node;
831           gp_Pnt2d aPnt2d(UVPSleft[0].u, UVPSleft[0].v);
832           quad->side.push_back(new StdMeshers_FaceSide(aNode, aPnt2d, quad->side[1]));
833           return quad;
834         }
835       }
836       comment << "Invalid Base vertex parameter: " << myTriaVertexID << " is not among [";
837       TopTools_MapOfShape vMap;
838       for (TopExp_Explorer v(aShape, TopAbs_VERTEX); v.More(); v.Next())
839         if (vMap.Add(v.Current()))
840           comment << meshDS->ShapeToIndex(v.Current()) << (vMap.Extent()==3 ? "]" : ", ");
841     }
842     error(comment);
843     delete quad;
844     return quad = 0;
845   }
846   else if (nbEdgesInWire.front() == 4) { // exactly 4 edges
847     for (; edgeIt != edges.end(); ++edgeIt, nbSides++)
848       quad->side.push_back(new StdMeshers_FaceSide(F, *edgeIt, &aMesh,
849                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
850   }
851   else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
852     list< TopoDS_Edge > sideEdges;
853     while (!edges.empty()) {
854       sideEdges.clear();
855       sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
856       bool sameSide = true;
857       while (!edges.empty() && sameSide) {
858         sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
859         if (sameSide)
860           sideEdges.splice(sideEdges.end(), edges, edges.begin());
861       }
862       if (nbSides == 0) { // go backward from the first edge
863         sameSide = true;
864         while (!edges.empty() && sameSide) {
865           sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
866           if (sameSide)
867             sideEdges.splice(sideEdges.begin(), edges, --edges.end());
868         }
869       }
870       quad->side.push_back(new StdMeshers_FaceSide(F, sideEdges, &aMesh,
871                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
872       ++nbSides;
873     }
874     // issue 20222. Try to unite only edges shared by two same faces
875     if (nbSides < 4) {
876       // delete found sides
877       { FaceQuadStruct cleaner(*quad); }
878       quad->side.clear();
879       quad->side.reserve(nbEdgesInWire.front());
880       nbSides = 0;
881
882       SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
883       while (!edges.empty()) {
884         sideEdges.clear();
885         sideEdges.splice(sideEdges.end(), edges, edges.begin());
886         bool sameSide = true;
887         while (!edges.empty() && sameSide) {
888           sameSide =
889             SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
890             twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
891           if (sameSide)
892             sideEdges.splice(sideEdges.end(), edges, edges.begin());
893         }
894         if (nbSides == 0) { // go backward from the first edge
895           sameSide = true;
896           while (!edges.empty() && sameSide) {
897             sameSide =
898               SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
899               twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
900             if (sameSide)
901               sideEdges.splice(sideEdges.begin(), edges, --edges.end());
902           }
903         }
904         quad->side.push_back(new StdMeshers_FaceSide(F, sideEdges, &aMesh,
905                                                       nbSides<TOP_SIDE, ignoreMediumNodes));
906         ++nbSides;
907       }
908     }
909   }
910   if (nbSides != 4) {
911 #ifdef _DEBUG_
912     MESSAGE ("StdMeshers_Quadrangle_2D. Edge IDs of " << nbSides << " sides:\n");
913     for (int i = 0; i < nbSides; ++i) {
914       MESSAGE (" (");
915       for (int e = 0; e < quad->side[i]->NbEdges(); ++e)
916         MESSAGE (myTool->GetMeshDS()->ShapeToIndex(quad->side[i]->Edge(e)) << " ");
917       MESSAGE (")\n");
918     }
919     //cout << endl;
920 #endif
921     if (!nbSides)
922       nbSides = nbEdgesInWire.front();
923     error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
924     delete quad;
925     quad = 0;
926   }
927
928   return quad;
929 }
930
931
932 //=============================================================================
933 /*!
934  *  
935  */
936 //=============================================================================
937
938 bool StdMeshers_Quadrangle_2D::CheckNbEdgesForEvaluate(SMESH_Mesh& aMesh,
939                                                        const TopoDS_Shape & aShape,
940                                                        MapShapeNbElems& aResMap,
941                                                        std::vector<int>& aNbNodes,
942                                                        bool& IsQuadratic)
943
944 {
945   const TopoDS_Face & F = TopoDS::Face(aShape);
946
947   // verify 1 wire only, with 4 edges
948   TopoDS_Vertex V;
949   list< TopoDS_Edge > edges;
950   list< int > nbEdgesInWire;
951   int nbWire = SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
952   if (nbWire != 1) {
953     return false;
954   }
955
956   aNbNodes.resize(4);
957
958   int nbSides = 0;
959   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
960   SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
961   MapShapeNbElemsItr anIt = aResMap.find(sm);
962   if (anIt==aResMap.end()) {
963     return false;
964   }
965   std::vector<int> aVec = (*anIt).second;
966   IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
967   if (nbEdgesInWire.front() == 3) { // exactly 3 edges
968     if (myTriaVertexID>0) {
969       SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
970       TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
971       if (!V.IsNull()) {
972         TopoDS_Edge E1,E2,E3;
973         for (; edgeIt != edges.end(); ++edgeIt) {
974           TopoDS_Edge E =  TopoDS::Edge(*edgeIt);
975           TopoDS_Vertex VF, VL;
976           TopExp::Vertices(E, VF, VL, true);
977           if (VF.IsSame(V))
978             E1 = E;
979           else if (VL.IsSame(V))
980             E3 = E;
981           else
982             E2 = E;
983         }
984         SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
985         MapShapeNbElemsItr anIt = aResMap.find(sm);
986         if (anIt==aResMap.end()) return false;
987         std::vector<int> aVec = (*anIt).second;
988         if (IsQuadratic)
989           aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
990         else
991           aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
992         sm = aMesh.GetSubMesh(E2);
993         anIt = aResMap.find(sm);
994         if (anIt==aResMap.end()) return false;
995         aVec = (*anIt).second;
996         if (IsQuadratic)
997           aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
998         else
999           aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1000         sm = aMesh.GetSubMesh(E3);
1001         anIt = aResMap.find(sm);
1002         if (anIt==aResMap.end()) return false;
1003         aVec = (*anIt).second;
1004         if (IsQuadratic)
1005           aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1006         else
1007           aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1008         aNbNodes[3] = aNbNodes[1];
1009         aNbNodes.resize(5);
1010         nbSides = 4;
1011       }
1012     }
1013   }
1014   if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1015     for (; edgeIt != edges.end(); edgeIt++) {
1016       SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1017       MapShapeNbElemsItr anIt = aResMap.find(sm);
1018       if (anIt==aResMap.end()) {
1019         return false;
1020       }
1021       std::vector<int> aVec = (*anIt).second;
1022       if (IsQuadratic)
1023         aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1024       else
1025         aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1026       nbSides++;
1027     }
1028   }
1029   else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1030     list< TopoDS_Edge > sideEdges;
1031     while (!edges.empty()) {
1032       sideEdges.clear();
1033       sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1034       bool sameSide = true;
1035       while (!edges.empty() && sameSide) {
1036         sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1037         if (sameSide)
1038           sideEdges.splice(sideEdges.end(), edges, edges.begin());
1039       }
1040       if (nbSides == 0) { // go backward from the first edge
1041         sameSide = true;
1042         while (!edges.empty() && sameSide) {
1043           sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1044           if (sameSide)
1045             sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1046         }
1047       }
1048       list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1049       aNbNodes[nbSides] = 1;
1050       for (; ite!=sideEdges.end(); ite++) {
1051         SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1052         MapShapeNbElemsItr anIt = aResMap.find(sm);
1053         if (anIt==aResMap.end()) {
1054           return false;
1055         }
1056         std::vector<int> aVec = (*anIt).second;
1057         if (IsQuadratic)
1058           aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1059         else
1060           aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1061       }
1062       ++nbSides;
1063     }
1064     // issue 20222. Try to unite only edges shared by two same faces
1065     if (nbSides < 4) {
1066       nbSides = 0;
1067       SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
1068       while (!edges.empty()) {
1069         sideEdges.clear();
1070         sideEdges.splice(sideEdges.end(), edges, edges.begin());
1071         bool sameSide = true;
1072         while (!edges.empty() && sameSide) {
1073           sameSide =
1074             SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1075             twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1076           if (sameSide)
1077             sideEdges.splice(sideEdges.end(), edges, edges.begin());
1078         }
1079         if (nbSides == 0) { // go backward from the first edge
1080           sameSide = true;
1081           while (!edges.empty() && sameSide) {
1082             sameSide =
1083               SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1084               twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1085             if (sameSide)
1086               sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1087           }
1088         }
1089         list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1090         aNbNodes[nbSides] = 1;
1091         for (; ite!=sideEdges.end(); ite++) {
1092           SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1093           MapShapeNbElemsItr anIt = aResMap.find(sm);
1094           if (anIt==aResMap.end()) {
1095             return false;
1096           }
1097           std::vector<int> aVec = (*anIt).second;
1098           if (IsQuadratic)
1099             aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1100           else
1101             aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1102         }
1103         ++nbSides;
1104       }
1105     }
1106   }
1107   if (nbSides != 4) {
1108     if (!nbSides)
1109       nbSides = nbEdgesInWire.front();
1110     error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1111     return false;
1112   }
1113
1114   return true;
1115 }
1116
1117
1118 //=============================================================================
1119 /*!
1120  *  CheckAnd2Dcompute
1121  */
1122 //=============================================================================
1123
1124 FaceQuadStruct *StdMeshers_Quadrangle_2D::CheckAnd2Dcompute
1125                            (SMESH_Mesh &         aMesh,
1126                             const TopoDS_Shape & aShape,
1127                             const bool           CreateQuadratic) //throw(SALOME_Exception)
1128 {
1129   _quadraticMesh = CreateQuadratic;
1130
1131   FaceQuadStruct *quad = CheckNbEdges(aMesh, aShape);
1132
1133   if (!quad) return 0;
1134
1135   // set normalized grid on unit square in parametric domain
1136   bool stat = SetNormalizedGrid(aMesh, aShape, quad);
1137   if (!stat) {
1138     if (quad) delete quad;
1139     quad = 0;
1140   }
1141
1142   return quad;
1143 }
1144
1145 //=============================================================================
1146 /*!
1147  *  
1148  */
1149 //=============================================================================
1150
1151 faceQuadStruct::~faceQuadStruct()
1152 {
1153   for (int i = 0; i < side.size(); i++) {
1154     if (side[i])     delete side[i];
1155   }
1156   if (uv_grid)       delete [] uv_grid;
1157 }
1158
1159 namespace {
1160   inline const vector<UVPtStruct>& GetUVPtStructIn(FaceQuadStruct* quad, int i, int nbSeg)
1161   {
1162     bool   isXConst   = (i == BOTTOM_SIDE || i == TOP_SIDE);
1163     double constValue = (i == BOTTOM_SIDE || i == LEFT_SIDE) ? 0 : 1;
1164     return
1165       quad->isEdgeOut[i] ?
1166       quad->side[i]->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1167       quad->side[i]->GetUVPtStruct(isXConst,constValue);
1168   }
1169   inline gp_UV CalcUV(double x, double y,
1170                       const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1171                       const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1172   {
1173     return
1174       ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1175       ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1176   }
1177 }
1178
1179 //=============================================================================
1180 /*!
1181  *  
1182  */
1183 //=============================================================================
1184
1185 bool StdMeshers_Quadrangle_2D::SetNormalizedGrid (SMESH_Mesh & aMesh,
1186                                                   const TopoDS_Shape& aShape,
1187                                                   FaceQuadStruct* & quad) //throw (SALOME_Exception)
1188 {
1189   // Algorithme décrit dans "Génération automatique de maillages"
1190   // P.L. GEORGE, MASSON, Â§ 6.4.1 p. 84-85
1191   // traitement dans le domaine paramétrique 2d u,v
1192   // transport - projection sur le carré unité
1193
1194 //  MESSAGE("StdMeshers_Quadrangle_2D::SetNormalizedGrid");
1195 //  const TopoDS_Face& F = TopoDS::Face(aShape);
1196
1197   // 1 --- find orientation of the 4 edges, by test on extrema
1198
1199   //      max             min                    0     x1     1
1200   //     |<----north-2-------^                a3 -------------> a2
1201   //     |                   |                   ^1          1^
1202   //    west-3            east-1 =right          |            |
1203   //     |                   |         ==>       |            |
1204   //  y0 |                   | y1                |            |
1205   //     |                   |                   |0          0|
1206   //     v----south-0-------->                a0 -------------> a1
1207   //      min             max                    0     x0     1
1208   //             =down
1209   //
1210
1211   // 3 --- 2D normalized values on unit square [0..1][0..1]
1212
1213   int nbhoriz  = Min(quad->side[0]->NbPoints(), quad->side[2]->NbPoints());
1214   int nbvertic = Min(quad->side[1]->NbPoints(), quad->side[3]->NbPoints());
1215
1216   quad->isEdgeOut[0] = (quad->side[0]->NbPoints() > quad->side[2]->NbPoints());
1217   quad->isEdgeOut[1] = (quad->side[1]->NbPoints() > quad->side[3]->NbPoints());
1218   quad->isEdgeOut[2] = (quad->side[2]->NbPoints() > quad->side[0]->NbPoints());
1219   quad->isEdgeOut[3] = (quad->side[3]->NbPoints() > quad->side[1]->NbPoints());
1220
1221   UVPtStruct *uv_grid = quad->uv_grid = new UVPtStruct[nbvertic * nbhoriz];
1222
1223   const vector<UVPtStruct>& uv_e0 = GetUVPtStructIn(quad, 0, nbhoriz - 1);
1224   const vector<UVPtStruct>& uv_e1 = GetUVPtStructIn(quad, 1, nbvertic - 1);
1225   const vector<UVPtStruct>& uv_e2 = GetUVPtStructIn(quad, 2, nbhoriz - 1);
1226   const vector<UVPtStruct>& uv_e3 = GetUVPtStructIn(quad, 3, nbvertic - 1);
1227
1228   if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1229     //return error("Can't find nodes on sides");
1230     return error(COMPERR_BAD_INPUT_MESH);
1231
1232   // nodes Id on "in" edges
1233   if (! quad->isEdgeOut[0]) {
1234     int j = 0;
1235     for (int i = 0; i < nbhoriz; i++) { // down
1236       int ij = j * nbhoriz + i;
1237       uv_grid[ij].node = uv_e0[i].node;
1238     }
1239   }
1240   if (! quad->isEdgeOut[1]) {
1241     int i = nbhoriz - 1;
1242     for (int j = 0; j < nbvertic; j++) { // right
1243       int ij = j * nbhoriz + i;
1244       uv_grid[ij].node = uv_e1[j].node;
1245     }
1246   }
1247   if (! quad->isEdgeOut[2]) {
1248     int j = nbvertic - 1;
1249     for (int i = 0; i < nbhoriz; i++) { // up
1250       int ij = j * nbhoriz + i;
1251       uv_grid[ij].node = uv_e2[i].node;
1252     }
1253   }
1254   if (! quad->isEdgeOut[3]) {
1255     int i = 0;
1256     for (int j = 0; j < nbvertic; j++) { // left
1257       int ij = j * nbhoriz + i;
1258       uv_grid[ij].node = uv_e3[j].node;
1259     }
1260   }
1261
1262   // normalized 2d values on grid
1263   for (int i = 0; i < nbhoriz; i++) {
1264     for (int j = 0; j < nbvertic; j++) {
1265       int ij = j * nbhoriz + i;
1266       // --- droite i cste : x = x0 + y(x1-x0)
1267       double x0 = uv_e0[i].normParam;   // bas - sud
1268       double x1 = uv_e2[i].normParam;   // haut - nord
1269       // --- droite j cste : y = y0 + x(y1-y0)
1270       double y0 = uv_e3[j].normParam;   // gauche-ouest
1271       double y1 = uv_e1[j].normParam;   // droite - est
1272       // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1273       double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1274       double y = y0 + x * (y1 - y0);
1275       uv_grid[ij].x = x;
1276       uv_grid[ij].y = y;
1277       //MESSAGE("-xy-01 "<<x0<<" "<<x1<<" "<<y0<<" "<<y1);
1278       //MESSAGE("-xy-norm "<<i<<" "<<j<<" "<<x<<" "<<y);
1279     }
1280   }
1281
1282   // 4 --- projection on 2d domain (u,v)
1283   gp_UV a0(uv_e0.front().u, uv_e0.front().v);
1284   gp_UV a1(uv_e0.back().u,  uv_e0.back().v);
1285   gp_UV a2(uv_e2.back().u,  uv_e2.back().v);
1286   gp_UV a3(uv_e2.front().u, uv_e2.front().v);
1287
1288   for (int i = 0; i < nbhoriz; i++) {
1289     for (int j = 0; j < nbvertic; j++) {
1290       int ij = j * nbhoriz + i;
1291       double x = uv_grid[ij].x;
1292       double y = uv_grid[ij].y;
1293       double param_0 = uv_e0[0].normParam + x * (uv_e0.back().normParam - uv_e0[0].normParam); // sud
1294       double param_2 = uv_e2[0].normParam + x * (uv_e2.back().normParam - uv_e2[0].normParam); // nord
1295       double param_1 = uv_e1[0].normParam + y * (uv_e1.back().normParam - uv_e1[0].normParam); // est
1296       double param_3 = uv_e3[0].normParam + y * (uv_e3.back().normParam - uv_e3[0].normParam); // ouest
1297
1298       //MESSAGE("params "<<param_0<<" "<<param_1<<" "<<param_2<<" "<<param_3);
1299       gp_UV p0 = quad->side[0]->Value2d(param_0).XY();
1300       gp_UV p1 = quad->side[1]->Value2d(param_1).XY();
1301       gp_UV p2 = quad->side[2]->Value2d(param_2).XY();
1302       gp_UV p3 = quad->side[3]->Value2d(param_3).XY();
1303
1304       gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1305
1306       uv_grid[ij].u = uv.X();
1307       uv_grid[ij].v = uv.Y();
1308     }
1309   }
1310   return true;
1311 }
1312
1313 //=======================================================================
1314 //function : ShiftQuad
1315 //purpose  : auxilary function for ComputeQuadPref
1316 //=======================================================================
1317
1318 static void ShiftQuad(FaceQuadStruct* quad, const int num, bool)
1319 {
1320   StdMeshers_FaceSide* side[4] = { quad->side[0], quad->side[1], quad->side[2], quad->side[3] };
1321   for (int i = BOTTOM_SIDE; i < NB_SIDES; ++i) {
1322     int id = (i + num) % NB_SIDES;
1323     bool wasForward = (i < TOP_SIDE);
1324     bool newForward = (id < TOP_SIDE);
1325     if (wasForward != newForward)
1326       side[ i ]->Reverse();
1327     quad->side[ id ] = side[ i ];
1328   }
1329 }
1330
1331 //=======================================================================
1332 //function : CalcUV
1333 //purpose  : auxilary function for ComputeQuadPref
1334 //=======================================================================
1335
1336 static gp_UV CalcUV(double x0, double x1, double y0, double y1,
1337                     FaceQuadStruct* quad,
1338                     const gp_UV& a0, const gp_UV& a1,
1339                     const gp_UV& a2, const gp_UV& a3)
1340 {
1341   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
1342   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
1343   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
1344   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
1345
1346   double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1347   double y = y0 + x * (y1 - y0);
1348
1349   double param_b = uv_eb[0].normParam + x * (uv_eb.back().normParam - uv_eb[0].normParam);
1350   double param_t = uv_et[0].normParam + x * (uv_et.back().normParam - uv_et[0].normParam);
1351   double param_r = uv_er[0].normParam + y * (uv_er.back().normParam - uv_er[0].normParam);
1352   double param_l = uv_el[0].normParam + y * (uv_el.back().normParam - uv_el[0].normParam);
1353
1354   gp_UV p0 = quad->side[BOTTOM_SIDE]->Value2d(param_b).XY();
1355   gp_UV p1 = quad->side[RIGHT_SIDE ]->Value2d(param_r).XY();
1356   gp_UV p2 = quad->side[TOP_SIDE   ]->Value2d(param_t).XY();
1357   gp_UV p3 = quad->side[LEFT_SIDE  ]->Value2d(param_l).XY();
1358
1359   gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1360
1361   return uv;
1362 }
1363
1364 //=======================================================================
1365 //function : CalcUV2
1366 //purpose  : auxilary function for ComputeQuadPref
1367 //=======================================================================
1368
1369 static gp_UV CalcUV2(double x, double y,
1370                      FaceQuadStruct* quad,
1371                      const gp_UV& a0, const gp_UV& a1,
1372                      const gp_UV& a2, const gp_UV& a3)
1373 {
1374   gp_UV p0 = quad->side[BOTTOM_SIDE]->Value2d(x).XY();
1375   gp_UV p1 = quad->side[RIGHT_SIDE ]->Value2d(y).XY();
1376   gp_UV p2 = quad->side[TOP_SIDE   ]->Value2d(x).XY();
1377   gp_UV p3 = quad->side[LEFT_SIDE  ]->Value2d(y).XY();
1378
1379   gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1380
1381   return uv;
1382 }
1383
1384
1385 //=======================================================================
1386 /*!
1387  * Create only quandrangle faces
1388  */
1389 //=======================================================================
1390
1391 bool StdMeshers_Quadrangle_2D::ComputeQuadPref (SMESH_Mesh &        aMesh,
1392                                                 const TopoDS_Shape& aShape,
1393                                                 FaceQuadStruct*     quad)
1394 {
1395   // Auxilary key in order to keep old variant
1396   // of meshing after implementation new variant
1397   // for bug 0016220 from Mantis.
1398   bool OldVersion = false;
1399   if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
1400     OldVersion = true;
1401
1402   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1403   const TopoDS_Face& F = TopoDS::Face(aShape);
1404   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
1405   bool WisF = true;
1406   int i,j,geomFaceID = meshDS->ShapeToIndex(F);
1407
1408   int nb = quad->side[0]->NbPoints();
1409   int nr = quad->side[1]->NbPoints();
1410   int nt = quad->side[2]->NbPoints();
1411   int nl = quad->side[3]->NbPoints();
1412   int dh = abs(nb-nt);
1413   int dv = abs(nr-nl);
1414
1415   if (dh>=dv) {
1416     if (nt>nb) {
1417       // it is a base case => not shift quad but me be replacement is need
1418       ShiftQuad(quad,0,WisF);
1419     }
1420     else {
1421       // we have to shift quad on 2
1422       ShiftQuad(quad,2,WisF);
1423     }
1424   }
1425   else {
1426     if (nr>nl) {
1427       // we have to shift quad on 1
1428       ShiftQuad(quad,1,WisF);
1429     }
1430     else {
1431       // we have to shift quad on 3
1432       ShiftQuad(quad,3,WisF);
1433     }
1434   }
1435
1436   nb = quad->side[0]->NbPoints();
1437   nr = quad->side[1]->NbPoints();
1438   nt = quad->side[2]->NbPoints();
1439   nl = quad->side[3]->NbPoints();
1440   dh = abs(nb-nt);
1441   dv = abs(nr-nl);
1442   int nbh  = Max(nb,nt);
1443   int nbv = Max(nr,nl);
1444   int addh = 0;
1445   int addv = 0;
1446
1447   // ----------- Old version ---------------
1448   // orientation of face and 3 main domain for future faces
1449   //       0   top    1
1450   //      1------------1
1451   //       |   |  |   |
1452   //       |   |  |   |
1453   //       | L |  | R |
1454   //  left |   |  |   | rigth
1455   //       |  /    \  |
1456   //       | /  C   \ |
1457   //       |/        \|
1458   //      0------------0
1459   //       0  bottom  1
1460
1461   // ----------- New version ---------------
1462   // orientation of face and 3 main domain for future faces
1463   //       0   top    1
1464   //      1------------1
1465   //       |  |____|  |
1466   //       |  /    \  |
1467   //       | /  C   \ |
1468   //  left |/________\| rigth
1469   //       |          |
1470   //       |          |
1471   //       |          |
1472   //      0------------0
1473   //       0  bottom  1
1474
1475   if (dh>dv) {
1476     addv = (dh-dv)/2;
1477     nbv = nbv + addv;
1478   }
1479   else { // dv>=dh
1480     addh = (dv-dh)/2;
1481     nbh = nbh + addh;
1482   }
1483
1484   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
1485   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
1486   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
1487   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
1488
1489   if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
1490     return error(COMPERR_BAD_INPUT_MESH);
1491
1492   // arrays for normalized params
1493   //cout<<"Dump B:"<<endl;
1494   TColStd_SequenceOfReal npb, npr, npt, npl;
1495   for (i=0; i<nb; i++) {
1496     npb.Append(uv_eb[i].normParam);
1497     //cout<<"i="<<i<<" par="<<uv_eb[i].normParam<<" npar="<<uv_eb[i].normParam;
1498     //const SMDS_MeshNode* N = uv_eb[i].node;
1499     //cout<<" node("<<N->X()<<","<<N->Y()<<","<<N->Z()<<")"<<endl;
1500   }
1501   for (i=0; i<nr; i++) {
1502     npr.Append(uv_er[i].normParam);
1503   }
1504   for (i=0; i<nt; i++) {
1505     npt.Append(uv_et[i].normParam);
1506   }
1507   for (i=0; i<nl; i++) {
1508     npl.Append(uv_el[i].normParam);
1509   }
1510
1511   int dl,dr;
1512   if (OldVersion) {
1513     // add some params to right and left after the first param
1514     // insert to right
1515     dr = nbv - nr;
1516     double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
1517     for (i=1; i<=dr; i++) {
1518       npr.InsertAfter(1,npr.Value(2)-dpr);
1519     }
1520     // insert to left
1521     dl = nbv - nl;
1522     dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
1523     for (i=1; i<=dl; i++) {
1524       npl.InsertAfter(1,npl.Value(2)-dpr);
1525     }
1526   }
1527   //cout<<"npb:";
1528   //for (i=1; i<=npb.Length(); i++) {
1529   //  cout<<" "<<npb.Value(i);
1530   //}
1531   //cout<<endl;
1532   
1533   gp_XY a0(uv_eb.front().u, uv_eb.front().v);
1534   gp_XY a1(uv_eb.back().u,  uv_eb.back().v);
1535   gp_XY a2(uv_et.back().u,  uv_et.back().v);
1536   gp_XY a3(uv_et.front().u, uv_et.front().v);
1537   //cout<<" a0("<<a0.X()<<","<<a0.Y()<<")"<<" a1("<<a1.X()<<","<<a1.Y()<<")"
1538   //    <<" a2("<<a2.X()<<","<<a2.Y()<<")"<<" a3("<<a3.X()<<","<<a3.Y()<<")"<<endl;
1539
1540   int nnn = Min(nr,nl);
1541   // auxilary sequence of XY for creation nodes
1542   // in the bottom part of central domain
1543   // Length of UVL and UVR must be == nbv-nnn
1544   TColgp_SequenceOfXY UVL, UVR, UVT;
1545
1546   if (OldVersion) {
1547     // step1: create faces for left domain
1548     StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
1549     // add left nodes
1550     for (j=1; j<=nl; j++)
1551       NodesL.SetValue(1,j,uv_el[j-1].node);
1552     if (dl>0) {
1553       // add top nodes
1554       for (i=1; i<=dl; i++) 
1555         NodesL.SetValue(i+1,nl,uv_et[i].node);
1556       // create and add needed nodes
1557       TColgp_SequenceOfXY UVtmp;
1558       for (i=1; i<=dl; i++) {
1559         double x0 = npt.Value(i+1);
1560         double x1 = x0;
1561         // diagonal node
1562         double y0 = npl.Value(i+1);
1563         double y1 = npr.Value(i+1);
1564         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1565         gp_Pnt P = S->Value(UV.X(),UV.Y());
1566         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1567         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1568         NodesL.SetValue(i+1,1,N);
1569         if (UVL.Length()<nbv-nnn) UVL.Append(UV);
1570         // internal nodes
1571         for (j=2; j<nl; j++) {
1572           double y0 = npl.Value(dl+j);
1573           double y1 = npr.Value(dl+j);
1574           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1575           gp_Pnt P = S->Value(UV.X(),UV.Y());
1576           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1577           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1578           NodesL.SetValue(i+1,j,N);
1579           if (i==dl) UVtmp.Append(UV);
1580         }
1581       }
1582       for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn; i++) {
1583         UVL.Append(UVtmp.Value(i));
1584       }
1585       //cout<<"Dump NodesL:"<<endl;
1586       //for (i=1; i<=dl+1; i++) {
1587       //  cout<<"i="<<i;
1588       //  for (j=1; j<=nl; j++) {
1589       //    cout<<" ("<<NodesL.Value(i,j)->X()<<","<<NodesL.Value(i,j)->Y()<<","<<NodesL.Value(i,j)->Z()<<")";
1590       //  }
1591       //  cout<<endl;
1592       //}
1593       // create faces
1594       for (i=1; i<=dl; i++) {
1595         for (j=1; j<nl; j++) {
1596           if (WisF) {
1597             SMDS_MeshFace* F =
1598               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
1599                               NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
1600             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1601           }
1602           else {
1603             SMDS_MeshFace* F =
1604               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i,j+1),
1605                               NodesL.Value(i+1,j+1), NodesL.Value(i+1,j));
1606             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1607           }
1608         }
1609       }
1610     }
1611     else {
1612       // fill UVL using c2d
1613       for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
1614         UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
1615       }
1616     }
1617     
1618     // step2: create faces for right domain
1619     StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
1620     // add right nodes
1621     for (j=1; j<=nr; j++) 
1622       NodesR.SetValue(1,j,uv_er[nr-j].node);
1623     if (dr>0) {
1624       // add top nodes
1625       for (i=1; i<=dr; i++) 
1626         NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
1627       // create and add needed nodes
1628       TColgp_SequenceOfXY UVtmp;
1629       for (i=1; i<=dr; i++) {
1630         double x0 = npt.Value(nt-i);
1631         double x1 = x0;
1632         // diagonal node
1633         double y0 = npl.Value(i+1);
1634         double y1 = npr.Value(i+1);
1635         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1636         gp_Pnt P = S->Value(UV.X(),UV.Y());
1637         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1638         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1639         NodesR.SetValue(i+1,nr,N);
1640         if (UVR.Length()<nbv-nnn) UVR.Append(UV);
1641         // internal nodes
1642         for (j=2; j<nr; j++) {
1643           double y0 = npl.Value(nbv-j+1);
1644           double y1 = npr.Value(nbv-j+1);
1645           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1646           gp_Pnt P = S->Value(UV.X(),UV.Y());
1647           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1648           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1649           NodesR.SetValue(i+1,j,N);
1650           if (i==dr) UVtmp.Prepend(UV);
1651         }
1652       }
1653       for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
1654         UVR.Append(UVtmp.Value(i));
1655       }
1656       // create faces
1657       for (i=1; i<=dr; i++) {
1658         for (j=1; j<nr; j++) {
1659           if (WisF) {
1660             SMDS_MeshFace* F =
1661               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
1662                               NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
1663             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1664           }
1665           else {
1666             SMDS_MeshFace* F =
1667               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i,j+1),
1668                               NodesR.Value(i+1,j+1), NodesR.Value(i+1,j));
1669             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1670           }
1671         }
1672       }
1673     }
1674     else {
1675       // fill UVR using c2d
1676       for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
1677         UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
1678       }
1679     }
1680     
1681     // step3: create faces for central domain
1682     StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
1683     // add first line using NodesL
1684     for (i=1; i<=dl+1; i++)
1685       NodesC.SetValue(1,i,NodesL(i,1));
1686     for (i=2; i<=nl; i++)
1687       NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
1688     // add last line using NodesR
1689     for (i=1; i<=dr+1; i++)
1690       NodesC.SetValue(nb,i,NodesR(i,nr));
1691     for (i=1; i<nr; i++)
1692       NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
1693     // add top nodes (last columns)
1694     for (i=dl+2; i<nbh-dr; i++) 
1695       NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
1696     // add bottom nodes (first columns)
1697     for (i=2; i<nb; i++)
1698       NodesC.SetValue(i,1,uv_eb[i-1].node);
1699     
1700     // create and add needed nodes
1701     // add linear layers
1702     for (i=2; i<nb; i++) {
1703       double x0 = npt.Value(dl+i);
1704       double x1 = x0;
1705       for (j=1; j<nnn; j++) {
1706         double y0 = npl.Value(nbv-nnn+j);
1707         double y1 = npr.Value(nbv-nnn+j);
1708         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1709         gp_Pnt P = S->Value(UV.X(),UV.Y());
1710         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1711         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1712         NodesC.SetValue(i,nbv-nnn+j,N);
1713         if ( j==1 )
1714           UVT.Append( UV );
1715       }
1716     }
1717     // add diagonal layers
1718     //cout<<"UVL.Length()="<<UVL.Length()<<" UVR.Length()="<<UVR.Length()<<endl;
1719     //cout<<"Dump UVL:"<<endl;
1720     //for (i=1; i<=UVL.Length(); i++) {
1721     //  cout<<" ("<<UVL.Value(i).X()<<","<<UVL.Value(i).Y()<<")";
1722     //}
1723     //cout<<endl;
1724     gp_UV A2 = UVR.Value(nbv-nnn);
1725     gp_UV A3 = UVL.Value(nbv-nnn);
1726     for (i=1; i<nbv-nnn; i++) {
1727       gp_UV p1 = UVR.Value(i);
1728       gp_UV p3 = UVL.Value(i);
1729       double y = i / double(nbv-nnn);
1730       for (j=2; j<nb; j++) {
1731         double x = npb.Value(j);
1732         gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
1733         gp_UV p2 = UVT.Value( j-1 );
1734         gp_UV UV = CalcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
1735         gp_Pnt P = S->Value(UV.X(),UV.Y());
1736         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1737         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
1738         NodesC.SetValue(j,i+1,N);
1739       }
1740     }
1741     // create faces
1742     for (i=1; i<nb; i++) {
1743       for (j=1; j<nbv; j++) {
1744         if (WisF) {
1745           SMDS_MeshFace* F =
1746             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
1747                             NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
1748           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1749         }
1750         else {
1751           SMDS_MeshFace* F =
1752             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i,j+1),
1753                             NodesC.Value(i+1,j+1), NodesC.Value(i+1,j));
1754           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1755         }
1756       }
1757     }
1758   }
1759
1760   else { // New version (!OldVersion)
1761     // step1: create faces for bottom rectangle domain
1762     StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
1763     // fill UVL and UVR using c2d
1764     for (j=0; j<nb; j++) {
1765       NodesBRD.SetValue(j+1,1,uv_eb[j].node);
1766     }
1767     for (i=1; i<nnn-1; i++) {
1768       NodesBRD.SetValue(1,i+1,uv_el[i].node);
1769       NodesBRD.SetValue(nb,i+1,uv_er[i].node);
1770       for (j=2; j<nb; j++) {
1771         double x = npb.Value(j);
1772         double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
1773         gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1774         gp_Pnt P = S->Value(UV.X(),UV.Y());
1775         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1776         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
1777         NodesBRD.SetValue(j,i+1,N);
1778       }
1779     }
1780     for (j=1; j<nnn-1; j++) {
1781       for (i=1; i<nb; i++) {
1782         if (WisF) {
1783           SMDS_MeshFace* F =
1784             myTool->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
1785                             NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
1786           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1787         }
1788         else {
1789           SMDS_MeshFace* F =
1790             myTool->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i,j+1),
1791                             NodesBRD.Value(i+1,j+1), NodesBRD.Value(i+1,j));
1792           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1793         }
1794       }
1795     }
1796     int drl = abs(nr-nl);
1797     // create faces for region C
1798     StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
1799     // add nodes from previous region
1800     for (j=1; j<=nb; j++) {
1801       NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
1802     }
1803     if ((drl+addv) > 0) {
1804       int n1,n2;
1805       if (nr>nl) {
1806         n1 = 1;
1807         n2 = drl + 1;
1808         TColgp_SequenceOfXY UVtmp;
1809         double drparam = npr.Value(nr) - npr.Value(nnn-1);
1810         double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
1811         double y0,y1;
1812         for (i=1; i<=drl; i++) {
1813           // add existed nodes from right edge
1814           NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
1815           //double dtparam = npt.Value(i+1);
1816           y1 = npr.Value(nnn+i-1); // param on right edge
1817           double dpar = (y1 - npr.Value(nnn-1))/drparam;
1818           y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
1819           double dy = y1 - y0;
1820           for (j=1; j<nb; j++) {
1821             double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
1822             double y = y0 + dy*x;
1823             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1824             gp_Pnt P = S->Value(UV.X(),UV.Y());
1825             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1826             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1827             NodesC.SetValue(j,i+1,N);
1828           }
1829         }
1830         double dy0 = (1-y0)/(addv+1);
1831         double dy1 = (1-y1)/(addv+1);
1832         for (i=1; i<=addv; i++) {
1833           double yy0 = y0 + dy0*i;
1834           double yy1 = y1 + dy1*i;
1835           double dyy = yy1 - yy0;
1836           for (j=1; j<=nb; j++) {
1837             double x = npt.Value(i+1+drl) + 
1838               npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
1839             double y = yy0 + dyy*x;
1840             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1841             gp_Pnt P = S->Value(UV.X(),UV.Y());
1842             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1843             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1844             NodesC.SetValue(j,i+drl+1,N);
1845           }
1846         }
1847       }
1848       else { // nr<nl
1849         n2 = 1;
1850         n1 = drl + 1;
1851         TColgp_SequenceOfXY UVtmp;
1852         double dlparam = npl.Value(nl) - npl.Value(nnn-1);
1853         double drparam = npr.Value(nnn) - npr.Value(nnn-1);
1854         double y0 = npl.Value(nnn-1);
1855         double y1 = npr.Value(nnn-1);
1856         for (i=1; i<=drl; i++) {
1857           // add existed nodes from right edge
1858           NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
1859           y0 = npl.Value(nnn+i-1); // param on left edge
1860           double dpar = (y0 - npl.Value(nnn-1))/dlparam;
1861           y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
1862           double dy = y1 - y0;
1863           for (j=2; j<=nb; j++) {
1864             double x = npb.Value(j)*npt.Value(nt-i);
1865             double y = y0 + dy*x;
1866             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1867             gp_Pnt P = S->Value(UV.X(),UV.Y());
1868             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1869             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1870             NodesC.SetValue(j,i+1,N);
1871           }
1872         }
1873         double dy0 = (1-y0)/(addv+1);
1874         double dy1 = (1-y1)/(addv+1);
1875         for (i=1; i<=addv; i++) {
1876           double yy0 = y0 + dy0*i;
1877           double yy1 = y1 + dy1*i;
1878           double dyy = yy1 - yy0;
1879           for (j=1; j<=nb; j++) {
1880             double x = npt.Value(i+1) + 
1881               npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
1882             double y = yy0 + dyy*x;
1883             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1884             gp_Pnt P = S->Value(UV.X(),UV.Y());
1885             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1886             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1887             NodesC.SetValue(j,i+drl+1,N);
1888           }
1889         }
1890       }
1891       // create faces
1892       for (j=1; j<=drl+addv; j++) {
1893         for (i=1; i<nb; i++) {
1894           if (WisF) {
1895             SMDS_MeshFace* F =
1896               myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
1897                               NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
1898             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1899           }
1900           else {
1901             SMDS_MeshFace* F =
1902               myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i,j+1),
1903                               NodesC.Value(i+1,j+1), NodesC.Value(i+1,j));
1904             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1905           }
1906         }
1907       } // end nr<nl
1908
1909       StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
1910       for (i=1; i<=nt; i++) {
1911         NodesLast.SetValue(i,2,uv_et[i-1].node);
1912       }
1913       int nnn=0;
1914       for (i=n1; i<drl+addv+1; i++) {
1915         nnn++;
1916         NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
1917       }
1918       for (i=1; i<=nb; i++) {
1919         nnn++;
1920         NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
1921       }
1922       for (i=drl+addv; i>=n2; i--) {
1923         nnn++;
1924         NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
1925       }
1926       for (i=1; i<nt; i++) {
1927         if (WisF) {
1928           SMDS_MeshFace* F =
1929             myTool->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
1930                             NodesLast.Value(i+1,2), NodesLast.Value(i,2));
1931           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1932         }
1933         else {
1934           SMDS_MeshFace* F =
1935             myTool->AddFace(NodesLast.Value(i,1), NodesLast.Value(i,2),
1936                             NodesLast.Value(i+1,2), NodesLast.Value(i+1,2));
1937           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1938         }
1939       }
1940     } // if ((drl+addv) > 0)
1941
1942   } // end new version implementation
1943
1944   bool isOk = true;
1945   return isOk;
1946 }
1947
1948
1949 //=======================================================================
1950 /*!
1951  * Evaluate only quandrangle faces
1952  */
1953 //=======================================================================
1954
1955 bool StdMeshers_Quadrangle_2D::EvaluateQuadPref(SMESH_Mesh &        aMesh,
1956                                                 const TopoDS_Shape& aShape,
1957                                                 std::vector<int>& aNbNodes,
1958                                                 MapShapeNbElems& aResMap,
1959                                                 bool IsQuadratic)
1960 {
1961   // Auxilary key in order to keep old variant
1962   // of meshing after implementation new variant
1963   // for bug 0016220 from Mantis.
1964   bool OldVersion = false;
1965   if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
1966     OldVersion = true;
1967
1968   const TopoDS_Face& F = TopoDS::Face(aShape);
1969   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
1970
1971   int nb = aNbNodes[0];
1972   int nr = aNbNodes[1];
1973   int nt = aNbNodes[2];
1974   int nl = aNbNodes[3];
1975   int dh = abs(nb-nt);
1976   int dv = abs(nr-nl);
1977
1978   if (dh>=dv) {
1979     if (nt>nb) {
1980       // it is a base case => not shift 
1981     }
1982     else {
1983       // we have to shift on 2
1984       nb = aNbNodes[2];
1985       nr = aNbNodes[3];
1986       nt = aNbNodes[0];
1987       nl = aNbNodes[1];
1988     }
1989   }
1990   else {
1991     if (nr>nl) {
1992       // we have to shift quad on 1
1993       nb = aNbNodes[3];
1994       nr = aNbNodes[0];
1995       nt = aNbNodes[1];
1996       nl = aNbNodes[2];
1997     }
1998     else {
1999       // we have to shift quad on 3
2000       nb = aNbNodes[1];
2001       nr = aNbNodes[2];
2002       nt = aNbNodes[3];
2003       nl = aNbNodes[0];
2004     }
2005   }
2006
2007   dh = abs(nb-nt);
2008   dv = abs(nr-nl);
2009   int nbh  = Max(nb,nt);
2010   int nbv = Max(nr,nl);
2011   int addh = 0;
2012   int addv = 0;
2013
2014   if (dh>dv) {
2015     addv = (dh-dv)/2;
2016     nbv = nbv + addv;
2017   }
2018   else { // dv>=dh
2019     addh = (dv-dh)/2;
2020     nbh = nbh + addh;
2021   }
2022
2023   int dl,dr;
2024   if (OldVersion) {
2025     // add some params to right and left after the first param
2026     // insert to right
2027     dr = nbv - nr;
2028     // insert to left
2029     dl = nbv - nl;
2030   }
2031   
2032   int nnn = Min(nr,nl);
2033
2034   int nbNodes = 0;
2035   int nbFaces = 0;
2036   if (OldVersion) {
2037     // step1: create faces for left domain
2038     if (dl>0) {
2039       nbNodes += dl*(nl-1);
2040       nbFaces += dl*(nl-1);
2041     }
2042     // step2: create faces for right domain
2043     if (dr>0) {
2044       nbNodes += dr*(nr-1);
2045       nbFaces += dr*(nr-1);
2046     }
2047     // step3: create faces for central domain
2048     nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2049     nbFaces += (nb-1)*(nbv-1);
2050   }
2051   else { // New version (!OldVersion)
2052     nbNodes += (nnn-2)*(nb-2);
2053     nbFaces += (nnn-2)*(nb-1);
2054     int drl = abs(nr-nl);
2055     nbNodes += drl*(nb-1) + addv*nb;
2056     nbFaces += (drl+addv)*(nb-1) + (nt-1);
2057   } // end new version implementation
2058
2059   std::vector<int> aVec(SMDSEntity_Last);
2060   for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2061   if (IsQuadratic) {
2062     aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2063     aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2064     if (aNbNodes.size()==5) {
2065       aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2066       aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2067     }
2068   }
2069   else {
2070     aVec[SMDSEntity_Node] = nbNodes;
2071     aVec[SMDSEntity_Quadrangle] = nbFaces;
2072     if (aNbNodes.size()==5) {
2073       aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2074       aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2075     }
2076   }
2077   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2078   aResMap.insert(std::make_pair(sm,aVec));
2079
2080   return true;
2081 }
2082
2083
2084 //=============================================================================
2085 /*! Split quadrangle in to 2 triangles by smallest diagonal
2086  *   
2087  */
2088 //=============================================================================
2089 void StdMeshers_Quadrangle_2D::SplitQuad(SMESHDS_Mesh *theMeshDS,
2090                                     int theFaceID,
2091                                     const SMDS_MeshNode* theNode1,
2092                                     const SMDS_MeshNode* theNode2,
2093                                     const SMDS_MeshNode* theNode3,
2094                                     const SMDS_MeshNode* theNode4)
2095 {
2096   gp_Pnt a(theNode1->X(),theNode1->Y(),theNode1->Z());
2097   gp_Pnt b(theNode2->X(),theNode2->Y(),theNode2->Z());
2098   gp_Pnt c(theNode3->X(),theNode3->Y(),theNode3->Z());
2099   gp_Pnt d(theNode4->X(),theNode4->Y(),theNode4->Z());
2100   SMDS_MeshFace* face;
2101   if (a.Distance(c) > b.Distance(d)){
2102     face = myTool->AddFace(theNode2, theNode4 , theNode1);
2103     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2104     face = myTool->AddFace(theNode2, theNode3, theNode4);
2105     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2106
2107   }
2108   else{
2109     face = myTool->AddFace(theNode1, theNode2 ,theNode3);
2110     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2111     face = myTool->AddFace(theNode1, theNode3, theNode4);
2112     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2113   }
2114 }
2115
2116 //=======================================================================
2117 /*!
2118  *  Implementation of Reduced algorithm (meshing with quadrangles only)
2119  */
2120 //=======================================================================
2121 bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
2122                                                const TopoDS_Shape& aShape,
2123                                                FaceQuadStruct*     quad)
2124 {
2125   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2126   const TopoDS_Face& F = TopoDS::Face(aShape);
2127   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2128   int i,j,geomFaceID = meshDS->ShapeToIndex(F);
2129
2130   int nb = quad->side[0]->NbPoints();
2131   int nr = quad->side[1]->NbPoints();
2132   int nt = quad->side[2]->NbPoints();
2133   int nl = quad->side[3]->NbPoints();
2134
2135   //  Simple Reduce 8->6->4->2 (3 steps)      Multiple Reduce 8->2 (1 step)
2136   //
2137   //  .-----.-----.-----.-----.               .-----.-----.-----.-----.
2138   //  |    / \    |    / \    |               |    / \    |    / \    |
2139   //  |   /    .--.--.    \   |               |    / \    |    / \    |
2140   //  |   /   /   |   \   \   |               |   /  .----.----.  \   |
2141   //  .---.---.---.---.---.---.               |   / / \   |   / \ \   |
2142   //  |   /  / \  |  / \  \   |               |   / / \   |   / \ \   |
2143   //  |  /  /   .-.-.   \  \  |               |  / /  .---.---.  \ \  |
2144   //  |  /  /  /  |  \  \  \  |               |  / / / \  |  / \ \ \  |
2145   //  .--.--.--.--.--.--.--.--.               |  / / /  \ | /  \ \ \  |
2146   //  |  / /  / \ | / \  \ \  |               | / / /   .-.-.   \ \ \ |
2147   //  | / /  /  .-.-.  \  \ \ |               | / / /  /  |  \  \ \ \ |
2148   //  | / / /  /  |  \  \ \ \ |               | / / /  /  |  \  \ \ \ |
2149   //  .-.-.-.--.--.--.--.-.-.-.               .-.-.-.--.--.--.--.-.-.-.
2150
2151   bool MultipleReduce = false;
2152   {
2153     int nb1 = nb;
2154     int nr1 = nr;
2155     int nt1 = nt;
2156     int nl1 = nl;
2157
2158     if (nr == nl) {
2159       if (nb < nt) {
2160         nt1 = nb;
2161         nb1 = nt;
2162       }
2163     }
2164     else if (nb == nt) {
2165       nl1 = nb; // and == nt
2166       nr1 = nb; // and == nt
2167       if (nl < nr) {
2168         nt1 = nl;
2169         nb1 = nr;
2170       }
2171       else {
2172         nt1 = nr;
2173         nb1 = nl;
2174       }
2175     }
2176     else {
2177       return false;
2178     }
2179
2180     // number of rows and columns
2181     int nrows = nr1 - 1; // and also == nl1 - 1
2182     int ncol_top = nt1 - 1;
2183     int ncol_bot = nb1 - 1;
2184     int npair_top = ncol_top / 2;
2185     // maximum number of bottom elements for "linear" simple reduce
2186     //int max_lin = ncol_top + npair_top * 2 * nrows;
2187     // maximum number of bottom elements for "tree" simple reduce
2188     int max_tree = npair_top * pow(2.0, nrows + 1);
2189     if (ncol_top > npair_top * 2) {
2190       int delta = ncol_bot - max_tree;
2191       for (int irow = 1; irow < nrows; irow++) {
2192         int nfour = delta / 4;
2193         delta -= nfour*2;
2194       }
2195       if (delta <= (ncol_top - npair_top * 2))
2196         max_tree = ncol_bot;
2197     }
2198
2199     if (ncol_bot > max_tree)
2200       MultipleReduce = true;
2201   }
2202
2203   if (MultipleReduce) { // == ComputeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2204     //==================================================
2205     int dh = abs(nb-nt);
2206     int dv = abs(nr-nl);
2207
2208     if (dh >= dv) {
2209       if (nt > nb) {
2210         // it is a base case => not shift quad but may be replacement is need
2211         ShiftQuad(quad,0,true);
2212       }
2213       else {
2214         // we have to shift quad on 2
2215         ShiftQuad(quad,2,true);
2216       }
2217     }
2218     else {
2219       if (nr > nl) {
2220         // we have to shift quad on 1
2221         ShiftQuad(quad,1,true);
2222       }
2223       else {
2224         // we have to shift quad on 3
2225         ShiftQuad(quad,3,true);
2226       }
2227     }
2228
2229     nb = quad->side[0]->NbPoints();
2230     nr = quad->side[1]->NbPoints();
2231     nt = quad->side[2]->NbPoints();
2232     nl = quad->side[3]->NbPoints();
2233     dh = abs(nb-nt);
2234     dv = abs(nr-nl);
2235     int nbh  = Max(nb,nt);
2236     int nbv = Max(nr,nl);
2237     int addh = 0;
2238     int addv = 0;
2239
2240     if (dh>dv) {
2241       addv = (dh-dv)/2;
2242       nbv = nbv + addv;
2243     }
2244     else { // dv>=dh
2245       addh = (dv-dh)/2;
2246       nbh = nbh + addh;
2247     }
2248
2249     const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
2250     const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
2251     const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
2252     const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
2253
2254     if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2255       return error(COMPERR_BAD_INPUT_MESH);
2256
2257     // arrays for normalized params
2258     TColStd_SequenceOfReal npb, npr, npt, npl;
2259     for (j = 0; j < nb; j++) {
2260       npb.Append(uv_eb[j].normParam);
2261     }
2262     for (i = 0; i < nr; i++) {
2263       npr.Append(uv_er[i].normParam);
2264     }
2265     for (j = 0; j < nt; j++) {
2266       npt.Append(uv_et[j].normParam);
2267     }
2268     for (i = 0; i < nl; i++) {
2269       npl.Append(uv_el[i].normParam);
2270     }
2271
2272     int dl,dr;
2273     // orientation of face and 3 main domain for future faces
2274     //       0   top    1
2275     //      1------------1
2276     //       |   |  |   |
2277     //       |   |  |   |
2278     //       | L |  | R |
2279     //  left |   |  |   | rigth
2280     //       |  /    \  |
2281     //       | /  C   \ |
2282     //       |/        \|
2283     //      0------------0
2284     //       0  bottom  1
2285
2286     // add some params to right and left after the first param
2287     // insert to right
2288     dr = nbv - nr;
2289     double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
2290     for (i=1; i<=dr; i++) {
2291       npr.InsertAfter(1,npr.Value(2)-dpr);
2292     }
2293     // insert to left
2294     dl = nbv - nl;
2295     dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
2296     for (i=1; i<=dl; i++) {
2297       npl.InsertAfter(1,npl.Value(2)-dpr);
2298     }
2299   
2300     gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
2301     gp_XY a1 (uv_eb.back().u,  uv_eb.back().v);
2302     gp_XY a2 (uv_et.back().u,  uv_et.back().v);
2303     gp_XY a3 (uv_et.front().u, uv_et.front().v);
2304
2305     int nnn = Min(nr,nl);
2306     // auxilary sequence of XY for creation nodes
2307     // in the bottom part of central domain
2308     // it's length must be == nbv-nnn-1
2309     TColgp_SequenceOfXY UVL;
2310     TColgp_SequenceOfXY UVR;
2311     //==================================================
2312
2313     // step1: create faces for left domain
2314     StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2315     // add left nodes
2316     for (j=1; j<=nl; j++)
2317       NodesL.SetValue(1,j,uv_el[j-1].node);
2318     if (dl>0) {
2319       // add top nodes
2320       for (i=1; i<=dl; i++) 
2321         NodesL.SetValue(i+1,nl,uv_et[i].node);
2322       // create and add needed nodes
2323       TColgp_SequenceOfXY UVtmp;
2324       for (i=1; i<=dl; i++) {
2325         double x0 = npt.Value(i+1);
2326         double x1 = x0;
2327         // diagonal node
2328         double y0 = npl.Value(i+1);
2329         double y1 = npr.Value(i+1);
2330         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2331         gp_Pnt P = S->Value(UV.X(),UV.Y());
2332         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2333         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2334         NodesL.SetValue(i+1,1,N);
2335         if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
2336         // internal nodes
2337         for (j=2; j<nl; j++) {
2338           double y0 = npl.Value(dl+j);
2339           double y1 = npr.Value(dl+j);
2340           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2341           gp_Pnt P = S->Value(UV.X(),UV.Y());
2342           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2343           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2344           NodesL.SetValue(i+1,j,N);
2345           if (i==dl) UVtmp.Append(UV);
2346         }
2347       }
2348       for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
2349         UVL.Append(UVtmp.Value(i));
2350       }
2351       // create faces
2352       for (i=1; i<=dl; i++) {
2353         for (j=1; j<nl; j++) {
2354             SMDS_MeshFace* F =
2355               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2356                               NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2357             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2358         }
2359       }
2360     }
2361     else {
2362       // fill UVL using c2d
2363       for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
2364         UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2365       }
2366     }
2367     
2368     // step2: create faces for right domain
2369     StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2370     // add right nodes
2371     for (j=1; j<=nr; j++) 
2372       NodesR.SetValue(1,j,uv_er[nr-j].node);
2373     if (dr>0) {
2374       // add top nodes
2375       for (i=1; i<=dr; i++) 
2376         NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2377       // create and add needed nodes
2378       TColgp_SequenceOfXY UVtmp;
2379       for (i=1; i<=dr; i++) {
2380         double x0 = npt.Value(nt-i);
2381         double x1 = x0;
2382         // diagonal node
2383         double y0 = npl.Value(i+1);
2384         double y1 = npr.Value(i+1);
2385         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2386         gp_Pnt P = S->Value(UV.X(),UV.Y());
2387         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2388         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2389         NodesR.SetValue(i+1,nr,N);
2390         if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
2391         // internal nodes
2392         for (j=2; j<nr; j++) {
2393           double y0 = npl.Value(nbv-j+1);
2394           double y1 = npr.Value(nbv-j+1);
2395           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2396           gp_Pnt P = S->Value(UV.X(),UV.Y());
2397           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2398           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2399           NodesR.SetValue(i+1,j,N);
2400           if (i==dr) UVtmp.Prepend(UV);
2401         }
2402       }
2403       for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
2404         UVR.Append(UVtmp.Value(i));
2405       }
2406       // create faces
2407       for (i=1; i<=dr; i++) {
2408         for (j=1; j<nr; j++) {
2409             SMDS_MeshFace* F =
2410               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2411                               NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2412             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2413         }
2414       }
2415     }
2416     else {
2417       // fill UVR using c2d
2418       for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
2419         UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2420       }
2421     }
2422     
2423     // step3: create faces for central domain
2424     StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2425     // add first line using NodesL
2426     for (i=1; i<=dl+1; i++)
2427       NodesC.SetValue(1,i,NodesL(i,1));
2428     for (i=2; i<=nl; i++)
2429       NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2430     // add last line using NodesR
2431     for (i=1; i<=dr+1; i++)
2432       NodesC.SetValue(nb,i,NodesR(i,nr));
2433     for (i=1; i<nr; i++)
2434       NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2435     // add top nodes (last columns)
2436     for (i=dl+2; i<nbh-dr; i++) 
2437       NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2438     // add bottom nodes (first columns)
2439     for (i=2; i<nb; i++)
2440       NodesC.SetValue(i,1,uv_eb[i-1].node);
2441     
2442     // create and add needed nodes
2443     // add linear layers
2444     for (i=2; i<nb; i++) {
2445       double x0 = npt.Value(dl+i);
2446       double x1 = x0;
2447       for (j=1; j<nnn; j++) {
2448         double y0 = npl.Value(nbv-nnn+j);
2449         double y1 = npr.Value(nbv-nnn+j);
2450         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2451         gp_Pnt P = S->Value(UV.X(),UV.Y());
2452         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2453         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2454         NodesC.SetValue(i,nbv-nnn+j,N);
2455       }
2456     }
2457     // add diagonal layers
2458     for (i=1; i<nbv-nnn; i++) {
2459       double du = UVR.Value(i).X() - UVL.Value(i).X();
2460       double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
2461       for (j=2; j<nb; j++) {
2462         double u = UVL.Value(i).X() + du*npb.Value(j);
2463         double v = UVL.Value(i).Y() + dv*npb.Value(j);
2464         gp_Pnt P = S->Value(u,v);
2465         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2466         meshDS->SetNodeOnFace(N, geomFaceID, u, v);
2467         NodesC.SetValue(j,i+1,N);
2468       }
2469     }
2470     // create faces
2471     for (i=1; i<nb; i++) {
2472       for (j=1; j<nbv; j++) {
2473           SMDS_MeshFace* F =
2474             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2475                             NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2476           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2477       }
2478     }
2479     // TODO ???
2480   } // end Multiple Reduce implementation
2481   else { // Simple Reduce (!MultipleReduce)
2482     //=========================================================
2483     if (nr == nl) {
2484       if (nt < nb) {
2485         // it is a base case => not shift quad
2486         //ShiftQuad(quad,0,true);
2487       }
2488       else {
2489         // we have to shift quad on 2
2490         ShiftQuad(quad,2,true);
2491       }
2492     }
2493     else {
2494       if (nl > nr) {
2495         // we have to shift quad on 1
2496         ShiftQuad(quad,1,true);
2497       }
2498       else {
2499         // we have to shift quad on 3
2500         ShiftQuad(quad,3,true);
2501       }
2502     }
2503
2504     nb = quad->side[0]->NbPoints();
2505     nr = quad->side[1]->NbPoints();
2506     nt = quad->side[2]->NbPoints();
2507     nl = quad->side[3]->NbPoints();
2508  
2509     // number of rows and columns
2510     int nrows = nr - 1; // and also == nl - 1
2511     int ncol_top = nt - 1;
2512     int ncol_bot = nb - 1;
2513     int npair_top = ncol_top / 2;
2514     // maximum number of bottom elements for "linear" simple reduce
2515     int max_lin = ncol_top + npair_top * 2 * nrows;
2516     // maximum number of bottom elements for "tree" simple reduce
2517     //int max_tree = npair_top * pow(2, nrows + 1);
2518
2519     //if (ncol_bot > max_tree)
2520     //  MultipleReduce = true;
2521
2522     const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
2523     const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
2524     const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
2525     const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
2526
2527     if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2528       return error(COMPERR_BAD_INPUT_MESH);
2529
2530     // arrays for normalized params
2531     TColStd_SequenceOfReal npb, npr, npt, npl;
2532     for (j = 0; j < nb; j++) {
2533       npb.Append(uv_eb[j].normParam);
2534     }
2535     for (i = 0; i < nr; i++) {
2536       npr.Append(uv_er[i].normParam);
2537     }
2538     for (j = 0; j < nt; j++) {
2539       npt.Append(uv_et[j].normParam);
2540     }
2541     for (i = 0; i < nl; i++) {
2542       npl.Append(uv_el[i].normParam);
2543     }
2544
2545     // We will ajust new points to this grid
2546     if (!SetNormalizedGrid(aMesh, aShape, quad))
2547       return false;
2548
2549     // TODO ???
2550     gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
2551     gp_XY a1 (uv_eb.back().u,  uv_eb.back().v);
2552     gp_XY a2 (uv_et.back().u,  uv_et.back().v);
2553     gp_XY a3 (uv_et.front().u, uv_et.front().v);
2554     //=========================================================
2555
2556     TColStd_SequenceOfInteger curr_base, next_base;
2557     TColStd_SequenceOfReal curr_par_u, curr_par_v;
2558     TColStd_SequenceOfReal next_par_u, next_par_v;
2559     StdMeshers_Array2OfNode NodesBRD (1,nb, 1,nr);
2560     for (j = 1; j <= nb; j++) {
2561       NodesBRD.SetValue(j, 1, uv_eb[j - 1].node); // bottom
2562       curr_base.Append(j);
2563       next_base.Append(-1);
2564       curr_par_u.Append(uv_eb[j-1].u);
2565       curr_par_v.Append(uv_eb[j-1].v);
2566       next_par_u.Append(0.);
2567       next_par_v.Append(0.);
2568     }
2569     for (j = 1; j <= nt; j++) {
2570       NodesBRD.SetValue(j, nr, uv_et[j - 1].node); // top
2571     }
2572
2573     int curr_base_len = nb;
2574     int next_base_len = 0;
2575
2576     if (ncol_bot > max_lin) {
2577       // "tree" simple reduce 2->4->8->16->32->...
2578       //
2579       //  .---------------.---------------.---------------.---------------. nr
2580       //  | \             |             / | \             |             / |
2581       //  |     \ .-------.-------. /     |     \ .-------.-------. /     |
2582       //  |       |       |       |       |       |       |       |       |
2583       //  .-------.-------.-------.-------.-------.-------.-------.-------.
2584       //  |\      |      /|\      |      /|\      |      /|\      |      /|
2585       //  |  \.---.---./  |  \.---.---./  |  \.---.---./  |  \.---.---./  | i
2586       //  |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
2587       //  .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
2588       //  |\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|
2589       //  | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
2590       //  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
2591       //  .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
2592       //  1                               j                               nb
2593
2594       for (i = 1; i < nr; i++) { // layer by layer
2595         // left
2596         NodesBRD.SetValue(1, i+1, uv_el[i].node);
2597         next_base.SetValue(++next_base_len, 1);
2598         // right
2599         NodesBRD.SetValue(nb, i+1, uv_er[i].node);
2600
2601         next_par_u.SetValue(next_base_len, uv_el[i].u);
2602         next_par_v.SetValue(next_base_len, uv_el[i].v);
2603
2604         // to stop reducing, if number of nodes reaches nt
2605         int delta = curr_base_len - nt;
2606
2607         //double du = uv_er[i].u - uv_el[i].u;
2608         //double dv = uv_er[i].v - uv_el[i].v;
2609
2610         // to calculate normalized parameter, we must know number of points in next layer
2611         int nb_four = (curr_base_len - 1) / 4;
2612         int nb_next = nb_four*2 + (curr_base_len - nb_four*4);
2613         if (nb_next < nt) nb_next = nt;
2614
2615         for (j = 1; j + 4 <= curr_base_len && delta > 0; j += 4, delta -= 2) {
2616           // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2617           //
2618           //  .-----a-----b i + 1
2619           //  |\ 5  | 6  /|
2620           //  | \   |   / |
2621           //  |  c--d--e  |
2622           //  |1 |2 |3 |4 |
2623           //  |  |  |  |  |
2624           //  .--.--.--.--. i
2625           //
2626           //  j     j+2   j+4
2627
2628           double u,v;
2629
2630           // a (i + 1, j + 2)
2631           const SMDS_MeshNode* Na;
2632           next_base_len++;
2633           next_base.SetValue(next_base_len, curr_base.Value(j + 2));
2634           if (i + 1 == nr) { // top
2635             Na = uv_et[next_base_len - 1].node;
2636             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na);
2637             u = uv_et[next_base_len - 1].u;
2638             v = uv_et[next_base_len - 1].v;
2639           }
2640           else {
2641             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2642             //u = uv_el[i].u + du * norm_par;
2643             //v = uv_el[i].v + dv * norm_par;
2644             {
2645               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2646               int nearest_node_j = (int)rel;
2647               rel -= nearest_node_j;
2648               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2649               double u1 = quad->uv_grid[ij].u;
2650               double v1 = quad->uv_grid[ij].v;
2651               double u2 = quad->uv_grid[ij + 1].u;
2652               double v2 = quad->uv_grid[ij + 1].v;
2653               double duj = (u2 - u1) * rel;
2654               double dvj = (v2 - v1) * rel;
2655               u = u1 + duj;
2656               v = v1 + dvj;
2657             }
2658             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 2));
2659             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 2));
2660             gp_Pnt P = S->Value(u,v);
2661             SMDS_MeshNode* Na1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2662             meshDS->SetNodeOnFace(Na1, geomFaceID, u, v);
2663             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na1);
2664             Na = Na1;
2665           }
2666           next_par_u.SetValue(next_base_len, u);
2667           next_par_v.SetValue(next_base_len, v);
2668
2669           // b (i + 1, j + 4)
2670           const SMDS_MeshNode* Nb;
2671           next_base_len++;
2672           next_base.SetValue(next_base_len, curr_base.Value(j + 4));
2673           if (i + 1 == nr) { // top
2674             Nb = uv_et[next_base_len - 1].node;
2675             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb);
2676             u = uv_et[next_base_len - 1].u;
2677             v = uv_et[next_base_len - 1].v;
2678           }
2679           else if (j + 4 == curr_base_len) { // right
2680             Nb = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
2681             u = uv_er[i].u;
2682             v = uv_er[i].v;
2683           }
2684           else {
2685             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2686             //u = uv_el[i].u + du * norm_par;
2687             //v = uv_el[i].v + dv * norm_par;
2688             {
2689               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2690               int nearest_node_j = (int)rel;
2691               rel -= nearest_node_j;
2692               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2693               double u1 = quad->uv_grid[ij].u;
2694               double v1 = quad->uv_grid[ij].v;
2695               double u2 = quad->uv_grid[ij + 1].u;
2696               double v2 = quad->uv_grid[ij + 1].v;
2697               double duj = (u2 - u1) * rel;
2698               double dvj = (v2 - v1) * rel;
2699               u = u1 + duj;
2700               v = v1 + dvj;
2701             }
2702             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 4));
2703             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 4));
2704             gp_Pnt P = S->Value(u,v);
2705             SMDS_MeshNode* Nb1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2706             meshDS->SetNodeOnFace(Nb1, geomFaceID, u, v);
2707             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb1);
2708             Nb = Nb1;
2709           }
2710           next_par_u.SetValue(next_base_len, u);
2711           next_par_v.SetValue(next_base_len, v);
2712
2713           // c
2714           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 2)) / 2.0;
2715           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 2)) / 2.0;
2716           gp_Pnt P = S->Value(u,v);
2717           SMDS_MeshNode* Nc = meshDS->AddNode(P.X(), P.Y(), P.Z());
2718           meshDS->SetNodeOnFace(Nc, geomFaceID, u, v);
2719
2720           // d
2721           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 1)) / 2.0;
2722           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 1)) / 2.0;
2723           P = S->Value(u,v);
2724           SMDS_MeshNode* Nd = meshDS->AddNode(P.X(), P.Y(), P.Z());
2725           meshDS->SetNodeOnFace(Nd, geomFaceID, u, v);
2726
2727           // e
2728           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len)) / 2.0;
2729           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len)) / 2.0;
2730           P = S->Value(u,v);
2731           SMDS_MeshNode* Ne = meshDS->AddNode(P.X(), P.Y(), P.Z());
2732           meshDS->SetNodeOnFace(Ne, geomFaceID, u, v);
2733
2734           // Faces
2735           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 0), i),
2736                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2737                                               Nc,
2738                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
2739           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2740
2741           SMDS_MeshFace* F2 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 1), i),
2742                                               NodesBRD.Value(curr_base.Value(j + 2), i),
2743                                               Nd, Nc);
2744           if (F2) meshDS->SetMeshElementOnShape(F2, geomFaceID);
2745
2746           SMDS_MeshFace* F3 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 2), i),
2747                                               NodesBRD.Value(curr_base.Value(j + 3), i),
2748                                               Ne, Nd);
2749           if (F3) meshDS->SetMeshElementOnShape(F3, geomFaceID);
2750
2751           SMDS_MeshFace* F4 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 3), i),
2752                                               NodesBRD.Value(curr_base.Value(j + 4), i),
2753                                               Nb, Ne);
2754           if (F4) meshDS->SetMeshElementOnShape(F4, geomFaceID);
2755
2756           SMDS_MeshFace* F5 = myTool->AddFace(Nc, Nd, Na,
2757                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
2758           if (F5) meshDS->SetMeshElementOnShape(F5, geomFaceID);
2759
2760           SMDS_MeshFace* F6 = myTool->AddFace(Nd, Ne, Nb, Na);
2761           if (F6) meshDS->SetMeshElementOnShape(F6, geomFaceID);
2762         }
2763
2764         // not reduced side elements (if any)
2765         for (; j < curr_base_len; j++) {
2766           // f (i + 1, j + 1)
2767           const SMDS_MeshNode* Nf;
2768           double u,v;
2769           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
2770           if (i + 1 == nr) { // top
2771             Nf = uv_et[next_base_len - 1].node;
2772             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
2773             u = uv_et[next_base_len - 1].u;
2774             v = uv_et[next_base_len - 1].v;
2775           }
2776           else if (j + 1 == curr_base_len) { // right
2777             Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
2778             u = uv_er[i].u;
2779             v = uv_er[i].v;
2780           }
2781           else {
2782             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2783             //u = uv_el[i].u + du * norm_par;
2784             //v = uv_el[i].v + dv * norm_par;
2785             {
2786               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2787               int nearest_node_j = (int)rel;
2788               rel -= nearest_node_j;
2789               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2790               double u1 = quad->uv_grid[ij].u;
2791               double v1 = quad->uv_grid[ij].v;
2792               double u2 = quad->uv_grid[ij + 1].u;
2793               double v2 = quad->uv_grid[ij + 1].v;
2794               double duj = (u2 - u1) * rel;
2795               double dvj = (v2 - v1) * rel;
2796               u = u1 + duj;
2797               v = v1 + dvj;
2798             }
2799             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 1));
2800             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 1));
2801             gp_Pnt P = S->Value(u,v);
2802             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2803             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
2804             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
2805             Nf = Nf1;
2806           }
2807           next_par_u.SetValue(next_base_len, u);
2808           next_par_v.SetValue(next_base_len, v);
2809           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
2810                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2811                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
2812                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
2813           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2814         }
2815
2816         curr_base_len = next_base_len;
2817         curr_base = next_base;
2818         curr_par_u = next_par_u;
2819         curr_par_v = next_par_v;
2820         next_base_len = 0;
2821       }
2822     } // end "tree" simple reduce
2823     else {
2824       // "linear" simple reduce 4->8->12->16 (3 steps)
2825       //
2826       //  .---------------.---------------.---------------.---------------. nr
2827       //  | \             |             / | \             |             / |
2828       //  |     \ .-------.-------. /     |     \ .-------.-------. /     |
2829       //  |       |       |       |       |       |       |       |       |
2830       //  .-------.-------.-------.-------.-------.-------.-------.-------.
2831       //  |      / \      |      / \      |      / \      |      / \      |
2832       //  |     /   \.----.----./   \     |     /   \.----.----./   \     | i
2833       //  |     /    |    |    |    \     |     /    |    |    |    \     |
2834       //  .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
2835       //  |     /   / \   |  /  \   \     |     /   / \   |  /  \   \     |
2836       //  |    /   /    .-.-.    \   \    |    /   /    .-.-.    \   \    |
2837       //  |   /   /    /  |  \    \   \   |   /   /    /  |  \    \   \   |
2838       //  .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
2839       //  1                               j                               nb
2840
2841       // nt = 5, nb = 7, nr = 4
2842       //int delta_all = 2;
2843       //int delta_one_col = 6;
2844       //int nb_col = 0;
2845       //int remainder = 2;
2846       //if (remainder > 0) nb_col++;
2847       //nb_col = 1;
2848       //int free_left = 1;
2849       //free_left += 2;
2850       //int free_middle = 4;
2851
2852       int delta_all = nb - nt;
2853       int delta_one_col = (nr - 1) * 2;
2854       int nb_col = delta_all / delta_one_col;
2855       int remainder = delta_all - nb_col * delta_one_col;
2856       if (remainder > 0) {
2857         nb_col++;
2858       }
2859       int free_left = ((nt - 1) - nb_col * 2) / 2;
2860       free_left += nr - 2;
2861       int free_middle = (nr - 2) * 2;
2862       if (remainder > 0 && nb_col == 1) {
2863         int nb_rows_short_col = remainder / 2;
2864         int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
2865         free_left -= nb_rows_thrown;
2866       }
2867
2868       // nt = 5, nb = 17, nr = 4
2869       //int delta_all = 12;
2870       //int delta_one_col = 6;
2871       //int nb_col = 2;
2872       //int remainder = 0;
2873       //int free_left = 2;
2874       //int free_middle = 4;
2875
2876       for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) { // layer by layer
2877         // left
2878         NodesBRD.SetValue(1, i+1, uv_el[i].node);
2879         next_base.SetValue(++next_base_len, 1);
2880         // right
2881         NodesBRD.SetValue(nb, i+1, uv_er[i].node);
2882
2883         // left
2884         next_par_u.SetValue(next_base_len, uv_el[i].u);
2885         next_par_v.SetValue(next_base_len, uv_el[i].v);
2886
2887         // to calculate normalized parameter, we must know number of points in next layer
2888         int nb_next = curr_base_len - nb_col * 2;
2889         if (remainder > 0 && i > remainder / 2)
2890           // take into account short "column"
2891           nb_next += 2;
2892         if (nb_next < nt) nb_next = nt;
2893
2894         // not reduced left elements
2895         for (j = 1; j <= free_left; j++) {
2896           // f (i + 1, j + 1)
2897           const SMDS_MeshNode* Nf;
2898           double u,v;
2899           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
2900           if (i + 1 == nr) { // top
2901             Nf = uv_et[next_base_len - 1].node;
2902             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
2903             u = uv_et[next_base_len - 1].u;
2904             v = uv_et[next_base_len - 1].v;
2905           }
2906           else {
2907             {
2908               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2909               int nearest_node_j = (int)rel;
2910               rel -= nearest_node_j;
2911               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2912               double u1 = quad->uv_grid[ij].u;
2913               double v1 = quad->uv_grid[ij].v;
2914               double u2 = quad->uv_grid[ij + 1].u;
2915               double v2 = quad->uv_grid[ij + 1].v;
2916               double duj = (u2 - u1) * rel;
2917               double dvj = (v2 - v1) * rel;
2918               u = u1 + duj;
2919               v = v1 + dvj;
2920             }
2921             gp_Pnt P = S->Value(u,v);
2922             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2923             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
2924             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
2925             Nf = Nf1;
2926           }
2927           next_par_u.SetValue(next_base_len, u);
2928           next_par_v.SetValue(next_base_len, v);
2929           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
2930                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2931                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
2932                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
2933           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2934         }
2935
2936         for (int icol = 1; icol <= nb_col; icol++) {
2937
2938           if (remainder > 0 && icol == nb_col && i > remainder / 2)
2939             // stop short "column"
2940             break;
2941
2942           // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2943           //
2944           //  .-----a-----b i + 1
2945           //  |\ 5  | 6  /|
2946           //  | \   |   / |
2947           //  |  c--d--e  |
2948           //  |1 |2 |3 |4 |
2949           //  |  |  |  |  |
2950           //  .--.--.--.--. i
2951           //
2952           //  j     j+2   j+4
2953
2954           double u,v;
2955
2956           // a (i + 1, j + 2)
2957           const SMDS_MeshNode* Na;
2958           next_base_len++;
2959           next_base.SetValue(next_base_len, curr_base.Value(j + 2));
2960           if (i + 1 == nr) { // top
2961             Na = uv_et[next_base_len - 1].node;
2962             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na);
2963             u = uv_et[next_base_len - 1].u;
2964             v = uv_et[next_base_len - 1].v;
2965           }
2966           else {
2967             {
2968               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2969               int nearest_node_j = (int)rel;
2970               rel -= nearest_node_j;
2971               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2972               double u1 = quad->uv_grid[ij].u;
2973               double v1 = quad->uv_grid[ij].v;
2974               double u2 = quad->uv_grid[ij + 1].u;
2975               double v2 = quad->uv_grid[ij + 1].v;
2976               double duj = (u2 - u1) * rel;
2977               double dvj = (v2 - v1) * rel;
2978               u = u1 + duj;
2979               v = v1 + dvj;
2980             }
2981             gp_Pnt P = S->Value(u,v);
2982             SMDS_MeshNode* Na1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2983             meshDS->SetNodeOnFace(Na1, geomFaceID, u, v);
2984             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na1);
2985             Na = Na1;
2986           }
2987           next_par_u.SetValue(next_base_len, u);
2988           next_par_v.SetValue(next_base_len, v);
2989
2990           // b (i + 1, j + 4)
2991           const SMDS_MeshNode* Nb;
2992           next_base_len++;
2993           next_base.SetValue(next_base_len, curr_base.Value(j + 4));
2994           if (i + 1 == nr) { // top
2995             Nb = uv_et[next_base_len - 1].node;
2996             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb);
2997             u = uv_et[next_base_len - 1].u;
2998             v = uv_et[next_base_len - 1].v;
2999           }
3000           else if (j + 4 == curr_base_len) { // right
3001             Nb = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3002             u = uv_er[i].u;
3003             v = uv_er[i].v;
3004           }
3005           else {
3006             {
3007               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3008               int nearest_node_j = (int)rel;
3009               rel -= nearest_node_j;
3010               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3011               double u1 = quad->uv_grid[ij].u;
3012               double v1 = quad->uv_grid[ij].v;
3013               double u2 = quad->uv_grid[ij + 1].u;
3014               double v2 = quad->uv_grid[ij + 1].v;
3015               double duj = (u2 - u1) * rel;
3016               double dvj = (v2 - v1) * rel;
3017               u = u1 + duj;
3018               v = v1 + dvj;
3019             }
3020             gp_Pnt P = S->Value(u,v);
3021             SMDS_MeshNode* Nb1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3022             meshDS->SetNodeOnFace(Nb1, geomFaceID, u, v);
3023             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb1);
3024             Nb = Nb1;
3025           }
3026           next_par_u.SetValue(next_base_len, u);
3027           next_par_v.SetValue(next_base_len, v);
3028
3029           // c
3030           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 2)) / 2.0;
3031           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 2)) / 2.0;
3032           gp_Pnt P = S->Value(u,v);
3033           SMDS_MeshNode* Nc = meshDS->AddNode(P.X(), P.Y(), P.Z());
3034           meshDS->SetNodeOnFace(Nc, geomFaceID, u, v);
3035
3036           // d
3037           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 1)) / 2.0;
3038           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 1)) / 2.0;
3039           P = S->Value(u,v);
3040           SMDS_MeshNode* Nd = meshDS->AddNode(P.X(), P.Y(), P.Z());
3041           meshDS->SetNodeOnFace(Nd, geomFaceID, u, v);
3042
3043           // e
3044           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len)) / 2.0;
3045           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len)) / 2.0;
3046           P = S->Value(u,v);
3047           SMDS_MeshNode* Ne = meshDS->AddNode(P.X(), P.Y(), P.Z());
3048           meshDS->SetNodeOnFace(Ne, geomFaceID, u, v);
3049
3050           // Faces
3051           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 0), i),
3052                                               NodesBRD.Value(curr_base.Value(j + 1), i),
3053                                               Nc,
3054                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
3055           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3056
3057           SMDS_MeshFace* F2 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 1), i),
3058                                               NodesBRD.Value(curr_base.Value(j + 2), i),
3059                                               Nd, Nc);
3060           if (F2) meshDS->SetMeshElementOnShape(F2, geomFaceID);
3061
3062           SMDS_MeshFace* F3 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 2), i),
3063                                               NodesBRD.Value(curr_base.Value(j + 3), i),
3064                                               Ne, Nd);
3065           if (F3) meshDS->SetMeshElementOnShape(F3, geomFaceID);
3066
3067           SMDS_MeshFace* F4 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 3), i),
3068                                               NodesBRD.Value(curr_base.Value(j + 4), i),
3069                                               Nb, Ne);
3070           if (F4) meshDS->SetMeshElementOnShape(F4, geomFaceID);
3071
3072           SMDS_MeshFace* F5 = myTool->AddFace(Nc, Nd, Na,
3073                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
3074           if (F5) meshDS->SetMeshElementOnShape(F5, geomFaceID);
3075
3076           SMDS_MeshFace* F6 = myTool->AddFace(Nd, Ne, Nb, Na);
3077           if (F6) meshDS->SetMeshElementOnShape(F6, geomFaceID);
3078
3079           j += 4;
3080
3081           // not reduced middle elements
3082           if (icol < nb_col) {
3083             if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3084               // pass middle elements before stopped short "column"
3085               break;
3086
3087             int free_add = free_middle;
3088             if (remainder > 0 && icol == nb_col - 1)
3089               // next "column" is short
3090               free_add -= (nr - 1) - (remainder / 2);
3091
3092             for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3093               // f (i + 1, j + imiddle)
3094               const SMDS_MeshNode* Nf;
3095               double u,v;
3096               next_base.SetValue(++next_base_len, curr_base.Value(j + imiddle));
3097               if (i + 1 == nr) { // top
3098                 Nf = uv_et[next_base_len - 1].node;
3099                 NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
3100                 u = uv_et[next_base_len - 1].u;
3101                 v = uv_et[next_base_len - 1].v;
3102               }
3103               else if (j + imiddle == curr_base_len) { // right
3104                 Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3105                 u = uv_er[i].u;
3106                 v = uv_er[i].v;
3107               }
3108               else {
3109                 {
3110                   double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3111                   int nearest_node_j = (int)rel;
3112                   rel -= nearest_node_j;
3113                   int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3114                   double u1 = quad->uv_grid[ij].u;
3115                   double v1 = quad->uv_grid[ij].v;
3116                   double u2 = quad->uv_grid[ij + 1].u;
3117                   double v2 = quad->uv_grid[ij + 1].v;
3118                   double duj = (u2 - u1) * rel;
3119                   double dvj = (v2 - v1) * rel;
3120                   u = u1 + duj;
3121                   v = v1 + dvj;
3122                 }
3123                 gp_Pnt P = S->Value(u,v);
3124                 SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3125                 meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
3126                 NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
3127                 Nf = Nf1;
3128               }
3129               next_par_u.SetValue(next_base_len, u);
3130               next_par_v.SetValue(next_base_len, v);
3131               SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j - 1 + imiddle), i),
3132                                                   NodesBRD.Value(curr_base.Value(j + imiddle), i),
3133                                                   NodesBRD.Value(next_base.Value(next_base_len), i + 1),
3134                                                   NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
3135               if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3136             }
3137             j += free_add;
3138           }
3139         }
3140
3141         // not reduced right elements
3142         for (; j < curr_base_len; j++) {
3143           // f (i + 1, j + 1)
3144           const SMDS_MeshNode* Nf;
3145           double u,v;
3146           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
3147           if (i + 1 == nr) { // top
3148             Nf = uv_et[next_base_len - 1].node;
3149             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
3150             u = uv_et[next_base_len - 1].u;
3151             v = uv_et[next_base_len - 1].v;
3152           }
3153           else if (j + 1 == curr_base_len) { // right
3154             Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3155             u = uv_er[i].u;
3156             v = uv_er[i].v;
3157           }
3158           else {
3159             {
3160               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3161               int nearest_node_j = (int)rel;
3162               rel -= nearest_node_j;
3163               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3164               double u1 = quad->uv_grid[ij].u;
3165               double v1 = quad->uv_grid[ij].v;
3166               double u2 = quad->uv_grid[ij + 1].u;
3167               double v2 = quad->uv_grid[ij + 1].v;
3168               double duj = (u2 - u1) * rel;
3169               double dvj = (v2 - v1) * rel;
3170               u = u1 + duj;
3171               v = v1 + dvj;
3172             }
3173             gp_Pnt P = S->Value(u,v);
3174             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3175             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
3176             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
3177             Nf = Nf1;
3178           }
3179           next_par_u.SetValue(next_base_len, u);
3180           next_par_v.SetValue(next_base_len, v);
3181           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
3182                                               NodesBRD.Value(curr_base.Value(j + 1), i),
3183                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
3184                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
3185           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3186         }
3187
3188         curr_base_len = next_base_len;
3189         curr_base = next_base;
3190         curr_par_u = next_par_u;
3191         curr_par_v = next_par_v;
3192         next_base_len = 0;
3193       }
3194     } // end "linear" simple reduce
3195   } // end Simple Reduce implementation
3196
3197   bool isOk = true;
3198   return isOk;
3199 }