Salome HOME
For redesigned StdMeshers_Hexa_3D (to work with composed cube edges)
[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   quad->face = F;
796
797   int nbSides = 0;
798   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
799   if (nbEdgesInWire.front() == 3) // exactly 3 edges
800   {
801     SMESH_Comment comment;
802     SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
803     if (myTriaVertexID == -1)
804     {
805       comment << "No Base vertex parameter provided for a trilateral geometrical face";
806     }
807     else
808     {
809       TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
810       if (!V.IsNull()) {
811         TopoDS_Edge E1,E2,E3;
812         for (; edgeIt != edges.end(); ++edgeIt) {
813           TopoDS_Edge E =  *edgeIt;
814           TopoDS_Vertex VF, VL;
815           TopExp::Vertices(E, VF, VL, true);
816           if (VF.IsSame(V))
817             E1 = E;
818           else if (VL.IsSame(V))
819             E3 = E;
820           else
821             E2 = E;
822         }
823         if (!E1.IsNull() && !E2.IsNull() && !E3.IsNull())
824         {
825           quad->side.push_back(new StdMeshers_FaceSide(F, E1, &aMesh, true, ignoreMediumNodes));
826           quad->side.push_back(new StdMeshers_FaceSide(F, E2, &aMesh, true, ignoreMediumNodes));
827           quad->side.push_back(new StdMeshers_FaceSide(F, E3, &aMesh, false,ignoreMediumNodes));
828           const vector<UVPtStruct>& UVPSleft  = quad->side[0]->GetUVPtStruct(true,0);
829           /*  vector<UVPtStruct>& UVPStop   = */quad->side[1]->GetUVPtStruct(false,1);
830           /*  vector<UVPtStruct>& UVPSright = */quad->side[2]->GetUVPtStruct(true,1);
831           const SMDS_MeshNode* aNode = UVPSleft[0].node;
832           gp_Pnt2d aPnt2d(UVPSleft[0].u, UVPSleft[0].v);
833           quad->side.push_back(new StdMeshers_FaceSide(aNode, aPnt2d, quad->side[1]));
834           return quad;
835         }
836       }
837       comment << "Invalid Base vertex parameter: " << myTriaVertexID << " is not among [";
838       TopTools_MapOfShape vMap;
839       for (TopExp_Explorer v(aShape, TopAbs_VERTEX); v.More(); v.Next())
840         if (vMap.Add(v.Current()))
841           comment << meshDS->ShapeToIndex(v.Current()) << (vMap.Extent()==3 ? "]" : ", ");
842     }
843     error(comment);
844     delete quad;
845     return quad = 0;
846   }
847   else if (nbEdgesInWire.front() == 4) { // exactly 4 edges
848     for (; edgeIt != edges.end(); ++edgeIt, nbSides++)
849       quad->side.push_back(new StdMeshers_FaceSide(F, *edgeIt, &aMesh,
850                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
851   }
852   else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
853     list< TopoDS_Edge > sideEdges;
854     while (!edges.empty()) {
855       sideEdges.clear();
856       sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
857       bool sameSide = true;
858       while (!edges.empty() && sameSide) {
859         sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
860         if (sameSide)
861           sideEdges.splice(sideEdges.end(), edges, edges.begin());
862       }
863       if (nbSides == 0) { // go backward from the first edge
864         sameSide = true;
865         while (!edges.empty() && sameSide) {
866           sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
867           if (sameSide)
868             sideEdges.splice(sideEdges.begin(), edges, --edges.end());
869         }
870       }
871       quad->side.push_back(new StdMeshers_FaceSide(F, sideEdges, &aMesh,
872                                                     nbSides<TOP_SIDE, ignoreMediumNodes));
873       ++nbSides;
874     }
875     // issue 20222. Try to unite only edges shared by two same faces
876     if (nbSides < 4) {
877       // delete found sides
878       { FaceQuadStruct cleaner(*quad); }
879       quad->side.clear();
880       quad->side.reserve(nbEdgesInWire.front());
881       nbSides = 0;
882
883       SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
884       while (!edges.empty()) {
885         sideEdges.clear();
886         sideEdges.splice(sideEdges.end(), edges, edges.begin());
887         bool sameSide = true;
888         while (!edges.empty() && sameSide) {
889           sameSide =
890             SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
891             twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
892           if (sameSide)
893             sideEdges.splice(sideEdges.end(), edges, edges.begin());
894         }
895         if (nbSides == 0) { // go backward from the first edge
896           sameSide = true;
897           while (!edges.empty() && sameSide) {
898             sameSide =
899               SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
900               twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
901             if (sameSide)
902               sideEdges.splice(sideEdges.begin(), edges, --edges.end());
903           }
904         }
905         quad->side.push_back(new StdMeshers_FaceSide(F, sideEdges, &aMesh,
906                                                       nbSides<TOP_SIDE, ignoreMediumNodes));
907         ++nbSides;
908       }
909     }
910   }
911   if (nbSides != 4) {
912 #ifdef _DEBUG_
913     MESSAGE ("StdMeshers_Quadrangle_2D. Edge IDs of " << nbSides << " sides:\n");
914     for (int i = 0; i < nbSides; ++i) {
915       MESSAGE (" (");
916       for (int e = 0; e < quad->side[i]->NbEdges(); ++e)
917         MESSAGE (myTool->GetMeshDS()->ShapeToIndex(quad->side[i]->Edge(e)) << " ");
918       MESSAGE (")\n");
919     }
920     //cout << endl;
921 #endif
922     if (!nbSides)
923       nbSides = nbEdgesInWire.front();
924     error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
925     delete quad;
926     quad = 0;
927   }
928
929   return quad;
930 }
931
932
933 //=============================================================================
934 /*!
935  *  
936  */
937 //=============================================================================
938
939 bool StdMeshers_Quadrangle_2D::CheckNbEdgesForEvaluate(SMESH_Mesh& aMesh,
940                                                        const TopoDS_Shape & aShape,
941                                                        MapShapeNbElems& aResMap,
942                                                        std::vector<int>& aNbNodes,
943                                                        bool& IsQuadratic)
944
945 {
946   const TopoDS_Face & F = TopoDS::Face(aShape);
947
948   // verify 1 wire only, with 4 edges
949   TopoDS_Vertex V;
950   list< TopoDS_Edge > edges;
951   list< int > nbEdgesInWire;
952   int nbWire = SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
953   if (nbWire != 1) {
954     return false;
955   }
956
957   aNbNodes.resize(4);
958
959   int nbSides = 0;
960   list< TopoDS_Edge >::iterator edgeIt = edges.begin();
961   SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
962   MapShapeNbElemsItr anIt = aResMap.find(sm);
963   if (anIt==aResMap.end()) {
964     return false;
965   }
966   std::vector<int> aVec = (*anIt).second;
967   IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
968   if (nbEdgesInWire.front() == 3) { // exactly 3 edges
969     if (myTriaVertexID>0) {
970       SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
971       TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
972       if (!V.IsNull()) {
973         TopoDS_Edge E1,E2,E3;
974         for (; edgeIt != edges.end(); ++edgeIt) {
975           TopoDS_Edge E =  TopoDS::Edge(*edgeIt);
976           TopoDS_Vertex VF, VL;
977           TopExp::Vertices(E, VF, VL, true);
978           if (VF.IsSame(V))
979             E1 = E;
980           else if (VL.IsSame(V))
981             E3 = E;
982           else
983             E2 = E;
984         }
985         SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
986         MapShapeNbElemsItr anIt = aResMap.find(sm);
987         if (anIt==aResMap.end()) return false;
988         std::vector<int> aVec = (*anIt).second;
989         if (IsQuadratic)
990           aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
991         else
992           aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
993         sm = aMesh.GetSubMesh(E2);
994         anIt = aResMap.find(sm);
995         if (anIt==aResMap.end()) return false;
996         aVec = (*anIt).second;
997         if (IsQuadratic)
998           aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
999         else
1000           aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1001         sm = aMesh.GetSubMesh(E3);
1002         anIt = aResMap.find(sm);
1003         if (anIt==aResMap.end()) return false;
1004         aVec = (*anIt).second;
1005         if (IsQuadratic)
1006           aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1007         else
1008           aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1009         aNbNodes[3] = aNbNodes[1];
1010         aNbNodes.resize(5);
1011         nbSides = 4;
1012       }
1013     }
1014   }
1015   if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1016     for (; edgeIt != edges.end(); edgeIt++) {
1017       SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1018       MapShapeNbElemsItr anIt = aResMap.find(sm);
1019       if (anIt==aResMap.end()) {
1020         return false;
1021       }
1022       std::vector<int> aVec = (*anIt).second;
1023       if (IsQuadratic)
1024         aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1025       else
1026         aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1027       nbSides++;
1028     }
1029   }
1030   else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1031     list< TopoDS_Edge > sideEdges;
1032     while (!edges.empty()) {
1033       sideEdges.clear();
1034       sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1035       bool sameSide = true;
1036       while (!edges.empty() && sameSide) {
1037         sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1038         if (sameSide)
1039           sideEdges.splice(sideEdges.end(), edges, edges.begin());
1040       }
1041       if (nbSides == 0) { // go backward from the first edge
1042         sameSide = true;
1043         while (!edges.empty() && sameSide) {
1044           sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1045           if (sameSide)
1046             sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1047         }
1048       }
1049       list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1050       aNbNodes[nbSides] = 1;
1051       for (; ite!=sideEdges.end(); ite++) {
1052         SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1053         MapShapeNbElemsItr anIt = aResMap.find(sm);
1054         if (anIt==aResMap.end()) {
1055           return false;
1056         }
1057         std::vector<int> aVec = (*anIt).second;
1058         if (IsQuadratic)
1059           aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1060         else
1061           aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1062       }
1063       ++nbSides;
1064     }
1065     // issue 20222. Try to unite only edges shared by two same faces
1066     if (nbSides < 4) {
1067       nbSides = 0;
1068       SMESH_Block::GetOrderedEdges (F, V, edges, nbEdgesInWire);
1069       while (!edges.empty()) {
1070         sideEdges.clear();
1071         sideEdges.splice(sideEdges.end(), edges, edges.begin());
1072         bool sameSide = true;
1073         while (!edges.empty() && sameSide) {
1074           sameSide =
1075             SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1076             twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1077           if (sameSide)
1078             sideEdges.splice(sideEdges.end(), edges, edges.begin());
1079         }
1080         if (nbSides == 0) { // go backward from the first edge
1081           sameSide = true;
1082           while (!edges.empty() && sameSide) {
1083             sameSide =
1084               SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1085               twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1086             if (sameSide)
1087               sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1088           }
1089         }
1090         list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1091         aNbNodes[nbSides] = 1;
1092         for (; ite!=sideEdges.end(); ite++) {
1093           SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1094           MapShapeNbElemsItr anIt = aResMap.find(sm);
1095           if (anIt==aResMap.end()) {
1096             return false;
1097           }
1098           std::vector<int> aVec = (*anIt).second;
1099           if (IsQuadratic)
1100             aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1101           else
1102             aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1103         }
1104         ++nbSides;
1105       }
1106     }
1107   }
1108   if (nbSides != 4) {
1109     if (!nbSides)
1110       nbSides = nbEdgesInWire.front();
1111     error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1112     return false;
1113   }
1114
1115   return true;
1116 }
1117
1118
1119 //=============================================================================
1120 /*!
1121  *  CheckAnd2Dcompute
1122  */
1123 //=============================================================================
1124
1125 FaceQuadStruct *StdMeshers_Quadrangle_2D::CheckAnd2Dcompute
1126                            (SMESH_Mesh &         aMesh,
1127                             const TopoDS_Shape & aShape,
1128                             const bool           CreateQuadratic) //throw(SALOME_Exception)
1129 {
1130   _quadraticMesh = CreateQuadratic;
1131
1132   FaceQuadStruct *quad = CheckNbEdges(aMesh, aShape);
1133
1134   if (!quad) return 0;
1135
1136   // set normalized grid on unit square in parametric domain
1137   bool stat = SetNormalizedGrid(aMesh, aShape, quad);
1138   if (!stat) {
1139     if (quad) delete quad;
1140     quad = 0;
1141   }
1142
1143   return quad;
1144 }
1145
1146 //=============================================================================
1147 /*!
1148  *  
1149  */
1150 //=============================================================================
1151
1152 faceQuadStruct::~faceQuadStruct()
1153 {
1154   for (int i = 0; i < side.size(); i++) {
1155     if (side[i])     delete side[i];
1156   }
1157   if (uv_grid)       delete [] uv_grid;
1158 }
1159
1160 namespace {
1161   inline const vector<UVPtStruct>& GetUVPtStructIn(FaceQuadStruct* quad, int i, int nbSeg)
1162   {
1163     bool   isXConst   = (i == BOTTOM_SIDE || i == TOP_SIDE);
1164     double constValue = (i == BOTTOM_SIDE || i == LEFT_SIDE) ? 0 : 1;
1165     return
1166       quad->isEdgeOut[i] ?
1167       quad->side[i]->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1168       quad->side[i]->GetUVPtStruct(isXConst,constValue);
1169   }
1170   inline gp_UV CalcUV(double x, double y,
1171                       const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1172                       const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1173   {
1174     return
1175       ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1176       ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1177   }
1178 }
1179
1180 //=============================================================================
1181 /*!
1182  *  
1183  */
1184 //=============================================================================
1185
1186 bool StdMeshers_Quadrangle_2D::SetNormalizedGrid (SMESH_Mesh & aMesh,
1187                                                   const TopoDS_Shape& aShape,
1188                                                   FaceQuadStruct* & quad) //throw (SALOME_Exception)
1189 {
1190   // Algorithme décrit dans "Génération automatique de maillages"
1191   // P.L. GEORGE, MASSON, Â§ 6.4.1 p. 84-85
1192   // traitement dans le domaine paramétrique 2d u,v
1193   // transport - projection sur le carré unité
1194
1195 //  MESSAGE("StdMeshers_Quadrangle_2D::SetNormalizedGrid");
1196 //  const TopoDS_Face& F = TopoDS::Face(aShape);
1197
1198   // 1 --- find orientation of the 4 edges, by test on extrema
1199
1200   //      max             min                    0     x1     1
1201   //     |<----north-2-------^                a3 -------------> a2
1202   //     |                   |                   ^1          1^
1203   //    west-3            east-1 =right          |            |
1204   //     |                   |         ==>       |            |
1205   //  y0 |                   | y1                |            |
1206   //     |                   |                   |0          0|
1207   //     v----south-0-------->                a0 -------------> a1
1208   //      min             max                    0     x0     1
1209   //             =down
1210   //
1211
1212   // 3 --- 2D normalized values on unit square [0..1][0..1]
1213
1214   int nbhoriz  = Min(quad->side[0]->NbPoints(), quad->side[2]->NbPoints());
1215   int nbvertic = Min(quad->side[1]->NbPoints(), quad->side[3]->NbPoints());
1216
1217   quad->isEdgeOut[0] = (quad->side[0]->NbPoints() > quad->side[2]->NbPoints());
1218   quad->isEdgeOut[1] = (quad->side[1]->NbPoints() > quad->side[3]->NbPoints());
1219   quad->isEdgeOut[2] = (quad->side[2]->NbPoints() > quad->side[0]->NbPoints());
1220   quad->isEdgeOut[3] = (quad->side[3]->NbPoints() > quad->side[1]->NbPoints());
1221
1222   UVPtStruct *uv_grid = quad->uv_grid = new UVPtStruct[nbvertic * nbhoriz];
1223
1224   const vector<UVPtStruct>& uv_e0 = GetUVPtStructIn(quad, 0, nbhoriz - 1);
1225   const vector<UVPtStruct>& uv_e1 = GetUVPtStructIn(quad, 1, nbvertic - 1);
1226   const vector<UVPtStruct>& uv_e2 = GetUVPtStructIn(quad, 2, nbhoriz - 1);
1227   const vector<UVPtStruct>& uv_e3 = GetUVPtStructIn(quad, 3, nbvertic - 1);
1228
1229   if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1230     //return error("Can't find nodes on sides");
1231     return error(COMPERR_BAD_INPUT_MESH);
1232
1233   // nodes Id on "in" edges
1234   if (! quad->isEdgeOut[0]) {
1235     int j = 0;
1236     for (int i = 0; i < nbhoriz; i++) { // down
1237       int ij = j * nbhoriz + i;
1238       uv_grid[ij].node = uv_e0[i].node;
1239     }
1240   }
1241   if (! quad->isEdgeOut[1]) {
1242     int i = nbhoriz - 1;
1243     for (int j = 0; j < nbvertic; j++) { // right
1244       int ij = j * nbhoriz + i;
1245       uv_grid[ij].node = uv_e1[j].node;
1246     }
1247   }
1248   if (! quad->isEdgeOut[2]) {
1249     int j = nbvertic - 1;
1250     for (int i = 0; i < nbhoriz; i++) { // up
1251       int ij = j * nbhoriz + i;
1252       uv_grid[ij].node = uv_e2[i].node;
1253     }
1254   }
1255   if (! quad->isEdgeOut[3]) {
1256     int i = 0;
1257     for (int j = 0; j < nbvertic; j++) { // left
1258       int ij = j * nbhoriz + i;
1259       uv_grid[ij].node = uv_e3[j].node;
1260     }
1261   }
1262
1263   // normalized 2d values on grid
1264   for (int i = 0; i < nbhoriz; i++) {
1265     for (int j = 0; j < nbvertic; j++) {
1266       int ij = j * nbhoriz + i;
1267       // --- droite i cste : x = x0 + y(x1-x0)
1268       double x0 = uv_e0[i].normParam;   // bas - sud
1269       double x1 = uv_e2[i].normParam;   // haut - nord
1270       // --- droite j cste : y = y0 + x(y1-y0)
1271       double y0 = uv_e3[j].normParam;   // gauche-ouest
1272       double y1 = uv_e1[j].normParam;   // droite - est
1273       // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1274       double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1275       double y = y0 + x * (y1 - y0);
1276       uv_grid[ij].x = x;
1277       uv_grid[ij].y = y;
1278       //MESSAGE("-xy-01 "<<x0<<" "<<x1<<" "<<y0<<" "<<y1);
1279       //MESSAGE("-xy-norm "<<i<<" "<<j<<" "<<x<<" "<<y);
1280     }
1281   }
1282
1283   // 4 --- projection on 2d domain (u,v)
1284   gp_UV a0(uv_e0.front().u, uv_e0.front().v);
1285   gp_UV a1(uv_e0.back().u,  uv_e0.back().v);
1286   gp_UV a2(uv_e2.back().u,  uv_e2.back().v);
1287   gp_UV a3(uv_e2.front().u, uv_e2.front().v);
1288
1289   for (int i = 0; i < nbhoriz; i++) {
1290     for (int j = 0; j < nbvertic; j++) {
1291       int ij = j * nbhoriz + i;
1292       double x = uv_grid[ij].x;
1293       double y = uv_grid[ij].y;
1294       double param_0 = uv_e0[0].normParam + x * (uv_e0.back().normParam - uv_e0[0].normParam); // sud
1295       double param_2 = uv_e2[0].normParam + x * (uv_e2.back().normParam - uv_e2[0].normParam); // nord
1296       double param_1 = uv_e1[0].normParam + y * (uv_e1.back().normParam - uv_e1[0].normParam); // est
1297       double param_3 = uv_e3[0].normParam + y * (uv_e3.back().normParam - uv_e3[0].normParam); // ouest
1298
1299       //MESSAGE("params "<<param_0<<" "<<param_1<<" "<<param_2<<" "<<param_3);
1300       gp_UV p0 = quad->side[0]->Value2d(param_0).XY();
1301       gp_UV p1 = quad->side[1]->Value2d(param_1).XY();
1302       gp_UV p2 = quad->side[2]->Value2d(param_2).XY();
1303       gp_UV p3 = quad->side[3]->Value2d(param_3).XY();
1304
1305       gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1306
1307       uv_grid[ij].u = uv.X();
1308       uv_grid[ij].v = uv.Y();
1309     }
1310   }
1311   return true;
1312 }
1313
1314 //=======================================================================
1315 //function : ShiftQuad
1316 //purpose  : auxilary function for ComputeQuadPref
1317 //=======================================================================
1318
1319 static void ShiftQuad(FaceQuadStruct* quad, const int num, bool)
1320 {
1321   StdMeshers_FaceSide* side[4] = { quad->side[0], quad->side[1], quad->side[2], quad->side[3] };
1322   for (int i = BOTTOM_SIDE; i < NB_SIDES; ++i) {
1323     int id = (i + num) % NB_SIDES;
1324     bool wasForward = (i < TOP_SIDE);
1325     bool newForward = (id < TOP_SIDE);
1326     if (wasForward != newForward)
1327       side[ i ]->Reverse();
1328     quad->side[ id ] = side[ i ];
1329   }
1330 }
1331
1332 //=======================================================================
1333 //function : CalcUV
1334 //purpose  : auxilary function for ComputeQuadPref
1335 //=======================================================================
1336
1337 static gp_UV CalcUV(double x0, double x1, double y0, double y1,
1338                     FaceQuadStruct* quad,
1339                     const gp_UV& a0, const gp_UV& a1,
1340                     const gp_UV& a2, const gp_UV& a3)
1341 {
1342   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
1343   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
1344   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
1345   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
1346
1347   double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1348   double y = y0 + x * (y1 - y0);
1349
1350   double param_b = uv_eb[0].normParam + x * (uv_eb.back().normParam - uv_eb[0].normParam);
1351   double param_t = uv_et[0].normParam + x * (uv_et.back().normParam - uv_et[0].normParam);
1352   double param_r = uv_er[0].normParam + y * (uv_er.back().normParam - uv_er[0].normParam);
1353   double param_l = uv_el[0].normParam + y * (uv_el.back().normParam - uv_el[0].normParam);
1354
1355   gp_UV p0 = quad->side[BOTTOM_SIDE]->Value2d(param_b).XY();
1356   gp_UV p1 = quad->side[RIGHT_SIDE ]->Value2d(param_r).XY();
1357   gp_UV p2 = quad->side[TOP_SIDE   ]->Value2d(param_t).XY();
1358   gp_UV p3 = quad->side[LEFT_SIDE  ]->Value2d(param_l).XY();
1359
1360   gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1361
1362   return uv;
1363 }
1364
1365 //=======================================================================
1366 //function : CalcUV2
1367 //purpose  : auxilary function for ComputeQuadPref
1368 //=======================================================================
1369
1370 static gp_UV CalcUV2(double x, double y,
1371                      FaceQuadStruct* quad,
1372                      const gp_UV& a0, const gp_UV& a1,
1373                      const gp_UV& a2, const gp_UV& a3)
1374 {
1375   gp_UV p0 = quad->side[BOTTOM_SIDE]->Value2d(x).XY();
1376   gp_UV p1 = quad->side[RIGHT_SIDE ]->Value2d(y).XY();
1377   gp_UV p2 = quad->side[TOP_SIDE   ]->Value2d(x).XY();
1378   gp_UV p3 = quad->side[LEFT_SIDE  ]->Value2d(y).XY();
1379
1380   gp_UV uv = CalcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1381
1382   return uv;
1383 }
1384
1385
1386 //=======================================================================
1387 /*!
1388  * Create only quandrangle faces
1389  */
1390 //=======================================================================
1391
1392 bool StdMeshers_Quadrangle_2D::ComputeQuadPref (SMESH_Mesh &        aMesh,
1393                                                 const TopoDS_Shape& aShape,
1394                                                 FaceQuadStruct*     quad)
1395 {
1396   // Auxilary key in order to keep old variant
1397   // of meshing after implementation new variant
1398   // for bug 0016220 from Mantis.
1399   bool OldVersion = false;
1400   if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
1401     OldVersion = true;
1402
1403   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1404   const TopoDS_Face& F = TopoDS::Face(aShape);
1405   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
1406   bool WisF = true;
1407   int i,j,geomFaceID = meshDS->ShapeToIndex(F);
1408
1409   int nb = quad->side[0]->NbPoints();
1410   int nr = quad->side[1]->NbPoints();
1411   int nt = quad->side[2]->NbPoints();
1412   int nl = quad->side[3]->NbPoints();
1413   int dh = abs(nb-nt);
1414   int dv = abs(nr-nl);
1415
1416   if (dh>=dv) {
1417     if (nt>nb) {
1418       // it is a base case => not shift quad but me be replacement is need
1419       ShiftQuad(quad,0,WisF);
1420     }
1421     else {
1422       // we have to shift quad on 2
1423       ShiftQuad(quad,2,WisF);
1424     }
1425   }
1426   else {
1427     if (nr>nl) {
1428       // we have to shift quad on 1
1429       ShiftQuad(quad,1,WisF);
1430     }
1431     else {
1432       // we have to shift quad on 3
1433       ShiftQuad(quad,3,WisF);
1434     }
1435   }
1436
1437   nb = quad->side[0]->NbPoints();
1438   nr = quad->side[1]->NbPoints();
1439   nt = quad->side[2]->NbPoints();
1440   nl = quad->side[3]->NbPoints();
1441   dh = abs(nb-nt);
1442   dv = abs(nr-nl);
1443   int nbh  = Max(nb,nt);
1444   int nbv = Max(nr,nl);
1445   int addh = 0;
1446   int addv = 0;
1447
1448   // ----------- Old version ---------------
1449   // orientation of face and 3 main domain for future faces
1450   //       0   top    1
1451   //      1------------1
1452   //       |   |  |   |
1453   //       |   |  |   |
1454   //       | L |  | R |
1455   //  left |   |  |   | rigth
1456   //       |  /    \  |
1457   //       | /  C   \ |
1458   //       |/        \|
1459   //      0------------0
1460   //       0  bottom  1
1461
1462   // ----------- New version ---------------
1463   // orientation of face and 3 main domain for future faces
1464   //       0   top    1
1465   //      1------------1
1466   //       |  |____|  |
1467   //       |  /    \  |
1468   //       | /  C   \ |
1469   //  left |/________\| rigth
1470   //       |          |
1471   //       |          |
1472   //       |          |
1473   //      0------------0
1474   //       0  bottom  1
1475
1476   if (dh>dv) {
1477     addv = (dh-dv)/2;
1478     nbv = nbv + addv;
1479   }
1480   else { // dv>=dh
1481     addh = (dv-dh)/2;
1482     nbh = nbh + addh;
1483   }
1484
1485   const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
1486   const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
1487   const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
1488   const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
1489
1490   if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
1491     return error(COMPERR_BAD_INPUT_MESH);
1492
1493   // arrays for normalized params
1494   //cout<<"Dump B:"<<endl;
1495   TColStd_SequenceOfReal npb, npr, npt, npl;
1496   for (i=0; i<nb; i++) {
1497     npb.Append(uv_eb[i].normParam);
1498     //cout<<"i="<<i<<" par="<<uv_eb[i].normParam<<" npar="<<uv_eb[i].normParam;
1499     //const SMDS_MeshNode* N = uv_eb[i].node;
1500     //cout<<" node("<<N->X()<<","<<N->Y()<<","<<N->Z()<<")"<<endl;
1501   }
1502   for (i=0; i<nr; i++) {
1503     npr.Append(uv_er[i].normParam);
1504   }
1505   for (i=0; i<nt; i++) {
1506     npt.Append(uv_et[i].normParam);
1507   }
1508   for (i=0; i<nl; i++) {
1509     npl.Append(uv_el[i].normParam);
1510   }
1511
1512   int dl,dr;
1513   if (OldVersion) {
1514     // add some params to right and left after the first param
1515     // insert to right
1516     dr = nbv - nr;
1517     double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
1518     for (i=1; i<=dr; i++) {
1519       npr.InsertAfter(1,npr.Value(2)-dpr);
1520     }
1521     // insert to left
1522     dl = nbv - nl;
1523     dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
1524     for (i=1; i<=dl; i++) {
1525       npl.InsertAfter(1,npl.Value(2)-dpr);
1526     }
1527   }
1528   //cout<<"npb:";
1529   //for (i=1; i<=npb.Length(); i++) {
1530   //  cout<<" "<<npb.Value(i);
1531   //}
1532   //cout<<endl;
1533   
1534   gp_XY a0(uv_eb.front().u, uv_eb.front().v);
1535   gp_XY a1(uv_eb.back().u,  uv_eb.back().v);
1536   gp_XY a2(uv_et.back().u,  uv_et.back().v);
1537   gp_XY a3(uv_et.front().u, uv_et.front().v);
1538   //cout<<" a0("<<a0.X()<<","<<a0.Y()<<")"<<" a1("<<a1.X()<<","<<a1.Y()<<")"
1539   //    <<" a2("<<a2.X()<<","<<a2.Y()<<")"<<" a3("<<a3.X()<<","<<a3.Y()<<")"<<endl;
1540
1541   int nnn = Min(nr,nl);
1542   // auxilary sequence of XY for creation nodes
1543   // in the bottom part of central domain
1544   // Length of UVL and UVR must be == nbv-nnn
1545   TColgp_SequenceOfXY UVL, UVR, UVT;
1546
1547   if (OldVersion) {
1548     // step1: create faces for left domain
1549     StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
1550     // add left nodes
1551     for (j=1; j<=nl; j++)
1552       NodesL.SetValue(1,j,uv_el[j-1].node);
1553     if (dl>0) {
1554       // add top nodes
1555       for (i=1; i<=dl; i++) 
1556         NodesL.SetValue(i+1,nl,uv_et[i].node);
1557       // create and add needed nodes
1558       TColgp_SequenceOfXY UVtmp;
1559       for (i=1; i<=dl; i++) {
1560         double x0 = npt.Value(i+1);
1561         double x1 = x0;
1562         // diagonal node
1563         double y0 = npl.Value(i+1);
1564         double y1 = npr.Value(i+1);
1565         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1566         gp_Pnt P = S->Value(UV.X(),UV.Y());
1567         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1568         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1569         NodesL.SetValue(i+1,1,N);
1570         if (UVL.Length()<nbv-nnn) UVL.Append(UV);
1571         // internal nodes
1572         for (j=2; j<nl; j++) {
1573           double y0 = npl.Value(dl+j);
1574           double y1 = npr.Value(dl+j);
1575           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1576           gp_Pnt P = S->Value(UV.X(),UV.Y());
1577           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1578           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1579           NodesL.SetValue(i+1,j,N);
1580           if (i==dl) UVtmp.Append(UV);
1581         }
1582       }
1583       for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn; i++) {
1584         UVL.Append(UVtmp.Value(i));
1585       }
1586       //cout<<"Dump NodesL:"<<endl;
1587       //for (i=1; i<=dl+1; i++) {
1588       //  cout<<"i="<<i;
1589       //  for (j=1; j<=nl; j++) {
1590       //    cout<<" ("<<NodesL.Value(i,j)->X()<<","<<NodesL.Value(i,j)->Y()<<","<<NodesL.Value(i,j)->Z()<<")";
1591       //  }
1592       //  cout<<endl;
1593       //}
1594       // create faces
1595       for (i=1; i<=dl; i++) {
1596         for (j=1; j<nl; j++) {
1597           if (WisF) {
1598             SMDS_MeshFace* F =
1599               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
1600                               NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
1601             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1602           }
1603           else {
1604             SMDS_MeshFace* F =
1605               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i,j+1),
1606                               NodesL.Value(i+1,j+1), NodesL.Value(i+1,j));
1607             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1608           }
1609         }
1610       }
1611     }
1612     else {
1613       // fill UVL using c2d
1614       for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
1615         UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
1616       }
1617     }
1618     
1619     // step2: create faces for right domain
1620     StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
1621     // add right nodes
1622     for (j=1; j<=nr; j++) 
1623       NodesR.SetValue(1,j,uv_er[nr-j].node);
1624     if (dr>0) {
1625       // add top nodes
1626       for (i=1; i<=dr; i++) 
1627         NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
1628       // create and add needed nodes
1629       TColgp_SequenceOfXY UVtmp;
1630       for (i=1; i<=dr; i++) {
1631         double x0 = npt.Value(nt-i);
1632         double x1 = x0;
1633         // diagonal node
1634         double y0 = npl.Value(i+1);
1635         double y1 = npr.Value(i+1);
1636         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1637         gp_Pnt P = S->Value(UV.X(),UV.Y());
1638         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1639         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1640         NodesR.SetValue(i+1,nr,N);
1641         if (UVR.Length()<nbv-nnn) UVR.Append(UV);
1642         // internal nodes
1643         for (j=2; j<nr; j++) {
1644           double y0 = npl.Value(nbv-j+1);
1645           double y1 = npr.Value(nbv-j+1);
1646           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1647           gp_Pnt P = S->Value(UV.X(),UV.Y());
1648           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1649           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1650           NodesR.SetValue(i+1,j,N);
1651           if (i==dr) UVtmp.Prepend(UV);
1652         }
1653       }
1654       for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
1655         UVR.Append(UVtmp.Value(i));
1656       }
1657       // create faces
1658       for (i=1; i<=dr; i++) {
1659         for (j=1; j<nr; j++) {
1660           if (WisF) {
1661             SMDS_MeshFace* F =
1662               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
1663                               NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
1664             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1665           }
1666           else {
1667             SMDS_MeshFace* F =
1668               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i,j+1),
1669                               NodesR.Value(i+1,j+1), NodesR.Value(i+1,j));
1670             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1671           }
1672         }
1673       }
1674     }
1675     else {
1676       // fill UVR using c2d
1677       for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
1678         UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
1679       }
1680     }
1681     
1682     // step3: create faces for central domain
1683     StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
1684     // add first line using NodesL
1685     for (i=1; i<=dl+1; i++)
1686       NodesC.SetValue(1,i,NodesL(i,1));
1687     for (i=2; i<=nl; i++)
1688       NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
1689     // add last line using NodesR
1690     for (i=1; i<=dr+1; i++)
1691       NodesC.SetValue(nb,i,NodesR(i,nr));
1692     for (i=1; i<nr; i++)
1693       NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
1694     // add top nodes (last columns)
1695     for (i=dl+2; i<nbh-dr; i++) 
1696       NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
1697     // add bottom nodes (first columns)
1698     for (i=2; i<nb; i++)
1699       NodesC.SetValue(i,1,uv_eb[i-1].node);
1700     
1701     // create and add needed nodes
1702     // add linear layers
1703     for (i=2; i<nb; i++) {
1704       double x0 = npt.Value(dl+i);
1705       double x1 = x0;
1706       for (j=1; j<nnn; j++) {
1707         double y0 = npl.Value(nbv-nnn+j);
1708         double y1 = npr.Value(nbv-nnn+j);
1709         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
1710         gp_Pnt P = S->Value(UV.X(),UV.Y());
1711         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1712         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1713         NodesC.SetValue(i,nbv-nnn+j,N);
1714         if ( j==1 )
1715           UVT.Append( UV );
1716       }
1717     }
1718     // add diagonal layers
1719     //cout<<"UVL.Length()="<<UVL.Length()<<" UVR.Length()="<<UVR.Length()<<endl;
1720     //cout<<"Dump UVL:"<<endl;
1721     //for (i=1; i<=UVL.Length(); i++) {
1722     //  cout<<" ("<<UVL.Value(i).X()<<","<<UVL.Value(i).Y()<<")";
1723     //}
1724     //cout<<endl;
1725     gp_UV A2 = UVR.Value(nbv-nnn);
1726     gp_UV A3 = UVL.Value(nbv-nnn);
1727     for (i=1; i<nbv-nnn; i++) {
1728       gp_UV p1 = UVR.Value(i);
1729       gp_UV p3 = UVL.Value(i);
1730       double y = i / double(nbv-nnn);
1731       for (j=2; j<nb; j++) {
1732         double x = npb.Value(j);
1733         gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
1734         gp_UV p2 = UVT.Value( j-1 );
1735         gp_UV UV = CalcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
1736         gp_Pnt P = S->Value(UV.X(),UV.Y());
1737         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1738         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
1739         NodesC.SetValue(j,i+1,N);
1740       }
1741     }
1742     // create faces
1743     for (i=1; i<nb; i++) {
1744       for (j=1; j<nbv; j++) {
1745         if (WisF) {
1746           SMDS_MeshFace* F =
1747             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
1748                             NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
1749           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1750         }
1751         else {
1752           SMDS_MeshFace* F =
1753             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i,j+1),
1754                             NodesC.Value(i+1,j+1), NodesC.Value(i+1,j));
1755           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1756         }
1757       }
1758     }
1759   }
1760
1761   else { // New version (!OldVersion)
1762     // step1: create faces for bottom rectangle domain
1763     StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
1764     // fill UVL and UVR using c2d
1765     for (j=0; j<nb; j++) {
1766       NodesBRD.SetValue(j+1,1,uv_eb[j].node);
1767     }
1768     for (i=1; i<nnn-1; i++) {
1769       NodesBRD.SetValue(1,i+1,uv_el[i].node);
1770       NodesBRD.SetValue(nb,i+1,uv_er[i].node);
1771       for (j=2; j<nb; j++) {
1772         double x = npb.Value(j);
1773         double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
1774         gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1775         gp_Pnt P = S->Value(UV.X(),UV.Y());
1776         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1777         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
1778         NodesBRD.SetValue(j,i+1,N);
1779       }
1780     }
1781     for (j=1; j<nnn-1; j++) {
1782       for (i=1; i<nb; i++) {
1783         if (WisF) {
1784           SMDS_MeshFace* F =
1785             myTool->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
1786                             NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
1787           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1788         }
1789         else {
1790           SMDS_MeshFace* F =
1791             myTool->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i,j+1),
1792                             NodesBRD.Value(i+1,j+1), NodesBRD.Value(i+1,j));
1793           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1794         }
1795       }
1796     }
1797     int drl = abs(nr-nl);
1798     // create faces for region C
1799     StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
1800     // add nodes from previous region
1801     for (j=1; j<=nb; j++) {
1802       NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
1803     }
1804     if ((drl+addv) > 0) {
1805       int n1,n2;
1806       if (nr>nl) {
1807         n1 = 1;
1808         n2 = drl + 1;
1809         TColgp_SequenceOfXY UVtmp;
1810         double drparam = npr.Value(nr) - npr.Value(nnn-1);
1811         double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
1812         double y0,y1;
1813         for (i=1; i<=drl; i++) {
1814           // add existed nodes from right edge
1815           NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
1816           //double dtparam = npt.Value(i+1);
1817           y1 = npr.Value(nnn+i-1); // param on right edge
1818           double dpar = (y1 - npr.Value(nnn-1))/drparam;
1819           y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
1820           double dy = y1 - y0;
1821           for (j=1; j<nb; j++) {
1822             double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
1823             double y = y0 + dy*x;
1824             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1825             gp_Pnt P = S->Value(UV.X(),UV.Y());
1826             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1827             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1828             NodesC.SetValue(j,i+1,N);
1829           }
1830         }
1831         double dy0 = (1-y0)/(addv+1);
1832         double dy1 = (1-y1)/(addv+1);
1833         for (i=1; i<=addv; i++) {
1834           double yy0 = y0 + dy0*i;
1835           double yy1 = y1 + dy1*i;
1836           double dyy = yy1 - yy0;
1837           for (j=1; j<=nb; j++) {
1838             double x = npt.Value(i+1+drl) + 
1839               npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
1840             double y = yy0 + dyy*x;
1841             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1842             gp_Pnt P = S->Value(UV.X(),UV.Y());
1843             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1844             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1845             NodesC.SetValue(j,i+drl+1,N);
1846           }
1847         }
1848       }
1849       else { // nr<nl
1850         n2 = 1;
1851         n1 = drl + 1;
1852         TColgp_SequenceOfXY UVtmp;
1853         double dlparam = npl.Value(nl) - npl.Value(nnn-1);
1854         double drparam = npr.Value(nnn) - npr.Value(nnn-1);
1855         double y0 = npl.Value(nnn-1);
1856         double y1 = npr.Value(nnn-1);
1857         for (i=1; i<=drl; i++) {
1858           // add existed nodes from right edge
1859           NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
1860           y0 = npl.Value(nnn+i-1); // param on left edge
1861           double dpar = (y0 - npl.Value(nnn-1))/dlparam;
1862           y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
1863           double dy = y1 - y0;
1864           for (j=2; j<=nb; j++) {
1865             double x = npb.Value(j)*npt.Value(nt-i);
1866             double y = y0 + dy*x;
1867             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1868             gp_Pnt P = S->Value(UV.X(),UV.Y());
1869             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1870             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1871             NodesC.SetValue(j,i+1,N);
1872           }
1873         }
1874         double dy0 = (1-y0)/(addv+1);
1875         double dy1 = (1-y1)/(addv+1);
1876         for (i=1; i<=addv; i++) {
1877           double yy0 = y0 + dy0*i;
1878           double yy1 = y1 + dy1*i;
1879           double dyy = yy1 - yy0;
1880           for (j=1; j<=nb; j++) {
1881             double x = npt.Value(i+1) + 
1882               npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
1883             double y = yy0 + dyy*x;
1884             gp_UV UV = CalcUV2(x, y, quad, a0, a1, a2, a3);
1885             gp_Pnt P = S->Value(UV.X(),UV.Y());
1886             SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
1887             meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
1888             NodesC.SetValue(j,i+drl+1,N);
1889           }
1890         }
1891       }
1892       // create faces
1893       for (j=1; j<=drl+addv; j++) {
1894         for (i=1; i<nb; i++) {
1895           if (WisF) {
1896             SMDS_MeshFace* F =
1897               myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
1898                               NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
1899             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1900           }
1901           else {
1902             SMDS_MeshFace* F =
1903               myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i,j+1),
1904                               NodesC.Value(i+1,j+1), NodesC.Value(i+1,j));
1905             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1906           }
1907         }
1908       } // end nr<nl
1909
1910       StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
1911       for (i=1; i<=nt; i++) {
1912         NodesLast.SetValue(i,2,uv_et[i-1].node);
1913       }
1914       int nnn=0;
1915       for (i=n1; i<drl+addv+1; i++) {
1916         nnn++;
1917         NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
1918       }
1919       for (i=1; i<=nb; i++) {
1920         nnn++;
1921         NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
1922       }
1923       for (i=drl+addv; i>=n2; i--) {
1924         nnn++;
1925         NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
1926       }
1927       for (i=1; i<nt; i++) {
1928         if (WisF) {
1929           SMDS_MeshFace* F =
1930             myTool->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
1931                             NodesLast.Value(i+1,2), NodesLast.Value(i,2));
1932           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1933         }
1934         else {
1935           SMDS_MeshFace* F =
1936             myTool->AddFace(NodesLast.Value(i,1), NodesLast.Value(i,2),
1937                             NodesLast.Value(i+1,2), NodesLast.Value(i+1,2));
1938           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
1939         }
1940       }
1941     } // if ((drl+addv) > 0)
1942
1943   } // end new version implementation
1944
1945   bool isOk = true;
1946   return isOk;
1947 }
1948
1949
1950 //=======================================================================
1951 /*!
1952  * Evaluate only quandrangle faces
1953  */
1954 //=======================================================================
1955
1956 bool StdMeshers_Quadrangle_2D::EvaluateQuadPref(SMESH_Mesh &        aMesh,
1957                                                 const TopoDS_Shape& aShape,
1958                                                 std::vector<int>& aNbNodes,
1959                                                 MapShapeNbElems& aResMap,
1960                                                 bool IsQuadratic)
1961 {
1962   // Auxilary key in order to keep old variant
1963   // of meshing after implementation new variant
1964   // for bug 0016220 from Mantis.
1965   bool OldVersion = false;
1966   if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
1967     OldVersion = true;
1968
1969   const TopoDS_Face& F = TopoDS::Face(aShape);
1970   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
1971
1972   int nb = aNbNodes[0];
1973   int nr = aNbNodes[1];
1974   int nt = aNbNodes[2];
1975   int nl = aNbNodes[3];
1976   int dh = abs(nb-nt);
1977   int dv = abs(nr-nl);
1978
1979   if (dh>=dv) {
1980     if (nt>nb) {
1981       // it is a base case => not shift 
1982     }
1983     else {
1984       // we have to shift on 2
1985       nb = aNbNodes[2];
1986       nr = aNbNodes[3];
1987       nt = aNbNodes[0];
1988       nl = aNbNodes[1];
1989     }
1990   }
1991   else {
1992     if (nr>nl) {
1993       // we have to shift quad on 1
1994       nb = aNbNodes[3];
1995       nr = aNbNodes[0];
1996       nt = aNbNodes[1];
1997       nl = aNbNodes[2];
1998     }
1999     else {
2000       // we have to shift quad on 3
2001       nb = aNbNodes[1];
2002       nr = aNbNodes[2];
2003       nt = aNbNodes[3];
2004       nl = aNbNodes[0];
2005     }
2006   }
2007
2008   dh = abs(nb-nt);
2009   dv = abs(nr-nl);
2010   int nbh  = Max(nb,nt);
2011   int nbv = Max(nr,nl);
2012   int addh = 0;
2013   int addv = 0;
2014
2015   if (dh>dv) {
2016     addv = (dh-dv)/2;
2017     nbv = nbv + addv;
2018   }
2019   else { // dv>=dh
2020     addh = (dv-dh)/2;
2021     nbh = nbh + addh;
2022   }
2023
2024   int dl,dr;
2025   if (OldVersion) {
2026     // add some params to right and left after the first param
2027     // insert to right
2028     dr = nbv - nr;
2029     // insert to left
2030     dl = nbv - nl;
2031   }
2032   
2033   int nnn = Min(nr,nl);
2034
2035   int nbNodes = 0;
2036   int nbFaces = 0;
2037   if (OldVersion) {
2038     // step1: create faces for left domain
2039     if (dl>0) {
2040       nbNodes += dl*(nl-1);
2041       nbFaces += dl*(nl-1);
2042     }
2043     // step2: create faces for right domain
2044     if (dr>0) {
2045       nbNodes += dr*(nr-1);
2046       nbFaces += dr*(nr-1);
2047     }
2048     // step3: create faces for central domain
2049     nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2050     nbFaces += (nb-1)*(nbv-1);
2051   }
2052   else { // New version (!OldVersion)
2053     nbNodes += (nnn-2)*(nb-2);
2054     nbFaces += (nnn-2)*(nb-1);
2055     int drl = abs(nr-nl);
2056     nbNodes += drl*(nb-1) + addv*nb;
2057     nbFaces += (drl+addv)*(nb-1) + (nt-1);
2058   } // end new version implementation
2059
2060   std::vector<int> aVec(SMDSEntity_Last);
2061   for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2062   if (IsQuadratic) {
2063     aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2064     aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2065     if (aNbNodes.size()==5) {
2066       aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2067       aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2068     }
2069   }
2070   else {
2071     aVec[SMDSEntity_Node] = nbNodes;
2072     aVec[SMDSEntity_Quadrangle] = nbFaces;
2073     if (aNbNodes.size()==5) {
2074       aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2075       aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2076     }
2077   }
2078   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2079   aResMap.insert(std::make_pair(sm,aVec));
2080
2081   return true;
2082 }
2083
2084
2085 //=============================================================================
2086 /*! Split quadrangle in to 2 triangles by smallest diagonal
2087  *   
2088  */
2089 //=============================================================================
2090 void StdMeshers_Quadrangle_2D::SplitQuad(SMESHDS_Mesh *theMeshDS,
2091                                     int theFaceID,
2092                                     const SMDS_MeshNode* theNode1,
2093                                     const SMDS_MeshNode* theNode2,
2094                                     const SMDS_MeshNode* theNode3,
2095                                     const SMDS_MeshNode* theNode4)
2096 {
2097   gp_Pnt a(theNode1->X(),theNode1->Y(),theNode1->Z());
2098   gp_Pnt b(theNode2->X(),theNode2->Y(),theNode2->Z());
2099   gp_Pnt c(theNode3->X(),theNode3->Y(),theNode3->Z());
2100   gp_Pnt d(theNode4->X(),theNode4->Y(),theNode4->Z());
2101   SMDS_MeshFace* face;
2102   if (a.Distance(c) > b.Distance(d)){
2103     face = myTool->AddFace(theNode2, theNode4 , theNode1);
2104     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2105     face = myTool->AddFace(theNode2, theNode3, theNode4);
2106     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2107
2108   }
2109   else{
2110     face = myTool->AddFace(theNode1, theNode2 ,theNode3);
2111     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2112     face = myTool->AddFace(theNode1, theNode3, theNode4);
2113     if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2114   }
2115 }
2116
2117 //=======================================================================
2118 /*!
2119  *  Implementation of Reduced algorithm (meshing with quadrangles only)
2120  */
2121 //=======================================================================
2122 bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
2123                                                const TopoDS_Shape& aShape,
2124                                                FaceQuadStruct*     quad)
2125 {
2126   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2127   const TopoDS_Face& F = TopoDS::Face(aShape);
2128   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2129   int i,j,geomFaceID = meshDS->ShapeToIndex(F);
2130
2131   int nb = quad->side[0]->NbPoints();
2132   int nr = quad->side[1]->NbPoints();
2133   int nt = quad->side[2]->NbPoints();
2134   int nl = quad->side[3]->NbPoints();
2135
2136   //  Simple Reduce 8->6->4->2 (3 steps)      Multiple Reduce 8->2 (1 step)
2137   //
2138   //  .-----.-----.-----.-----.               .-----.-----.-----.-----.
2139   //  |    / \    |    / \    |               |    / \    |    / \    |
2140   //  |   /    .--.--.    \   |               |    / \    |    / \    |
2141   //  |   /   /   |   \   \   |               |   /  .----.----.  \   |
2142   //  .---.---.---.---.---.---.               |   / / \   |   / \ \   |
2143   //  |   /  / \  |  / \  \   |               |   / / \   |   / \ \   |
2144   //  |  /  /   .-.-.   \  \  |               |  / /  .---.---.  \ \  |
2145   //  |  /  /  /  |  \  \  \  |               |  / / / \  |  / \ \ \  |
2146   //  .--.--.--.--.--.--.--.--.               |  / / /  \ | /  \ \ \  |
2147   //  |  / /  / \ | / \  \ \  |               | / / /   .-.-.   \ \ \ |
2148   //  | / /  /  .-.-.  \  \ \ |               | / / /  /  |  \  \ \ \ |
2149   //  | / / /  /  |  \  \ \ \ |               | / / /  /  |  \  \ \ \ |
2150   //  .-.-.-.--.--.--.--.-.-.-.               .-.-.-.--.--.--.--.-.-.-.
2151
2152   bool MultipleReduce = false;
2153   {
2154     int nb1 = nb;
2155     int nr1 = nr;
2156     int nt1 = nt;
2157     int nl1 = nl;
2158
2159     if (nr == nl) {
2160       if (nb < nt) {
2161         nt1 = nb;
2162         nb1 = nt;
2163       }
2164     }
2165     else if (nb == nt) {
2166       nl1 = nb; // and == nt
2167       nr1 = nb; // and == nt
2168       if (nl < nr) {
2169         nt1 = nl;
2170         nb1 = nr;
2171       }
2172       else {
2173         nt1 = nr;
2174         nb1 = nl;
2175       }
2176     }
2177     else {
2178       return false;
2179     }
2180
2181     // number of rows and columns
2182     int nrows = nr1 - 1; // and also == nl1 - 1
2183     int ncol_top = nt1 - 1;
2184     int ncol_bot = nb1 - 1;
2185     int npair_top = ncol_top / 2;
2186     // maximum number of bottom elements for "linear" simple reduce
2187     //int max_lin = ncol_top + npair_top * 2 * nrows;
2188     // maximum number of bottom elements for "tree" simple reduce
2189     int max_tree = npair_top * pow(2.0, nrows + 1);
2190     if (ncol_top > npair_top * 2) {
2191       int delta = ncol_bot - max_tree;
2192       for (int irow = 1; irow < nrows; irow++) {
2193         int nfour = delta / 4;
2194         delta -= nfour*2;
2195       }
2196       if (delta <= (ncol_top - npair_top * 2))
2197         max_tree = ncol_bot;
2198     }
2199
2200     if (ncol_bot > max_tree)
2201       MultipleReduce = true;
2202   }
2203
2204   if (MultipleReduce) { // == ComputeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2205     //==================================================
2206     int dh = abs(nb-nt);
2207     int dv = abs(nr-nl);
2208
2209     if (dh >= dv) {
2210       if (nt > nb) {
2211         // it is a base case => not shift quad but may be replacement is need
2212         ShiftQuad(quad,0,true);
2213       }
2214       else {
2215         // we have to shift quad on 2
2216         ShiftQuad(quad,2,true);
2217       }
2218     }
2219     else {
2220       if (nr > nl) {
2221         // we have to shift quad on 1
2222         ShiftQuad(quad,1,true);
2223       }
2224       else {
2225         // we have to shift quad on 3
2226         ShiftQuad(quad,3,true);
2227       }
2228     }
2229
2230     nb = quad->side[0]->NbPoints();
2231     nr = quad->side[1]->NbPoints();
2232     nt = quad->side[2]->NbPoints();
2233     nl = quad->side[3]->NbPoints();
2234     dh = abs(nb-nt);
2235     dv = abs(nr-nl);
2236     int nbh  = Max(nb,nt);
2237     int nbv = Max(nr,nl);
2238     int addh = 0;
2239     int addv = 0;
2240
2241     if (dh>dv) {
2242       addv = (dh-dv)/2;
2243       nbv = nbv + addv;
2244     }
2245     else { // dv>=dh
2246       addh = (dv-dh)/2;
2247       nbh = nbh + addh;
2248     }
2249
2250     const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
2251     const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
2252     const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
2253     const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
2254
2255     if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2256       return error(COMPERR_BAD_INPUT_MESH);
2257
2258     // arrays for normalized params
2259     TColStd_SequenceOfReal npb, npr, npt, npl;
2260     for (j = 0; j < nb; j++) {
2261       npb.Append(uv_eb[j].normParam);
2262     }
2263     for (i = 0; i < nr; i++) {
2264       npr.Append(uv_er[i].normParam);
2265     }
2266     for (j = 0; j < nt; j++) {
2267       npt.Append(uv_et[j].normParam);
2268     }
2269     for (i = 0; i < nl; i++) {
2270       npl.Append(uv_el[i].normParam);
2271     }
2272
2273     int dl,dr;
2274     // orientation of face and 3 main domain for future faces
2275     //       0   top    1
2276     //      1------------1
2277     //       |   |  |   |
2278     //       |   |  |   |
2279     //       | L |  | R |
2280     //  left |   |  |   | rigth
2281     //       |  /    \  |
2282     //       | /  C   \ |
2283     //       |/        \|
2284     //      0------------0
2285     //       0  bottom  1
2286
2287     // add some params to right and left after the first param
2288     // insert to right
2289     dr = nbv - nr;
2290     double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
2291     for (i=1; i<=dr; i++) {
2292       npr.InsertAfter(1,npr.Value(2)-dpr);
2293     }
2294     // insert to left
2295     dl = nbv - nl;
2296     dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
2297     for (i=1; i<=dl; i++) {
2298       npl.InsertAfter(1,npl.Value(2)-dpr);
2299     }
2300   
2301     gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
2302     gp_XY a1 (uv_eb.back().u,  uv_eb.back().v);
2303     gp_XY a2 (uv_et.back().u,  uv_et.back().v);
2304     gp_XY a3 (uv_et.front().u, uv_et.front().v);
2305
2306     int nnn = Min(nr,nl);
2307     // auxilary sequence of XY for creation nodes
2308     // in the bottom part of central domain
2309     // it's length must be == nbv-nnn-1
2310     TColgp_SequenceOfXY UVL;
2311     TColgp_SequenceOfXY UVR;
2312     //==================================================
2313
2314     // step1: create faces for left domain
2315     StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2316     // add left nodes
2317     for (j=1; j<=nl; j++)
2318       NodesL.SetValue(1,j,uv_el[j-1].node);
2319     if (dl>0) {
2320       // add top nodes
2321       for (i=1; i<=dl; i++) 
2322         NodesL.SetValue(i+1,nl,uv_et[i].node);
2323       // create and add needed nodes
2324       TColgp_SequenceOfXY UVtmp;
2325       for (i=1; i<=dl; i++) {
2326         double x0 = npt.Value(i+1);
2327         double x1 = x0;
2328         // diagonal node
2329         double y0 = npl.Value(i+1);
2330         double y1 = npr.Value(i+1);
2331         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2332         gp_Pnt P = S->Value(UV.X(),UV.Y());
2333         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2334         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2335         NodesL.SetValue(i+1,1,N);
2336         if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
2337         // internal nodes
2338         for (j=2; j<nl; j++) {
2339           double y0 = npl.Value(dl+j);
2340           double y1 = npr.Value(dl+j);
2341           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2342           gp_Pnt P = S->Value(UV.X(),UV.Y());
2343           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2344           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2345           NodesL.SetValue(i+1,j,N);
2346           if (i==dl) UVtmp.Append(UV);
2347         }
2348       }
2349       for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
2350         UVL.Append(UVtmp.Value(i));
2351       }
2352       // create faces
2353       for (i=1; i<=dl; i++) {
2354         for (j=1; j<nl; j++) {
2355             SMDS_MeshFace* F =
2356               myTool->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2357                               NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2358             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2359         }
2360       }
2361     }
2362     else {
2363       // fill UVL using c2d
2364       for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
2365         UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2366       }
2367     }
2368     
2369     // step2: create faces for right domain
2370     StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2371     // add right nodes
2372     for (j=1; j<=nr; j++) 
2373       NodesR.SetValue(1,j,uv_er[nr-j].node);
2374     if (dr>0) {
2375       // add top nodes
2376       for (i=1; i<=dr; i++) 
2377         NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2378       // create and add needed nodes
2379       TColgp_SequenceOfXY UVtmp;
2380       for (i=1; i<=dr; i++) {
2381         double x0 = npt.Value(nt-i);
2382         double x1 = x0;
2383         // diagonal node
2384         double y0 = npl.Value(i+1);
2385         double y1 = npr.Value(i+1);
2386         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2387         gp_Pnt P = S->Value(UV.X(),UV.Y());
2388         SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2389         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2390         NodesR.SetValue(i+1,nr,N);
2391         if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
2392         // internal nodes
2393         for (j=2; j<nr; j++) {
2394           double y0 = npl.Value(nbv-j+1);
2395           double y1 = npr.Value(nbv-j+1);
2396           gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2397           gp_Pnt P = S->Value(UV.X(),UV.Y());
2398           SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2399           meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2400           NodesR.SetValue(i+1,j,N);
2401           if (i==dr) UVtmp.Prepend(UV);
2402         }
2403       }
2404       for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
2405         UVR.Append(UVtmp.Value(i));
2406       }
2407       // create faces
2408       for (i=1; i<=dr; i++) {
2409         for (j=1; j<nr; j++) {
2410             SMDS_MeshFace* F =
2411               myTool->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2412                               NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2413             if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2414         }
2415       }
2416     }
2417     else {
2418       // fill UVR using c2d
2419       for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
2420         UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2421       }
2422     }
2423     
2424     // step3: create faces for central domain
2425     StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2426     // add first line using NodesL
2427     for (i=1; i<=dl+1; i++)
2428       NodesC.SetValue(1,i,NodesL(i,1));
2429     for (i=2; i<=nl; i++)
2430       NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2431     // add last line using NodesR
2432     for (i=1; i<=dr+1; i++)
2433       NodesC.SetValue(nb,i,NodesR(i,nr));
2434     for (i=1; i<nr; i++)
2435       NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2436     // add top nodes (last columns)
2437     for (i=dl+2; i<nbh-dr; i++) 
2438       NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2439     // add bottom nodes (first columns)
2440     for (i=2; i<nb; i++)
2441       NodesC.SetValue(i,1,uv_eb[i-1].node);
2442     
2443     // create and add needed nodes
2444     // add linear layers
2445     for (i=2; i<nb; i++) {
2446       double x0 = npt.Value(dl+i);
2447       double x1 = x0;
2448       for (j=1; j<nnn; j++) {
2449         double y0 = npl.Value(nbv-nnn+j);
2450         double y1 = npr.Value(nbv-nnn+j);
2451         gp_UV UV = CalcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2452         gp_Pnt P = S->Value(UV.X(),UV.Y());
2453         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2454         meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2455         NodesC.SetValue(i,nbv-nnn+j,N);
2456       }
2457     }
2458     // add diagonal layers
2459     for (i=1; i<nbv-nnn; i++) {
2460       double du = UVR.Value(i).X() - UVL.Value(i).X();
2461       double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
2462       for (j=2; j<nb; j++) {
2463         double u = UVL.Value(i).X() + du*npb.Value(j);
2464         double v = UVL.Value(i).Y() + dv*npb.Value(j);
2465         gp_Pnt P = S->Value(u,v);
2466         SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2467         meshDS->SetNodeOnFace(N, geomFaceID, u, v);
2468         NodesC.SetValue(j,i+1,N);
2469       }
2470     }
2471     // create faces
2472     for (i=1; i<nb; i++) {
2473       for (j=1; j<nbv; j++) {
2474           SMDS_MeshFace* F =
2475             myTool->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2476                             NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2477           if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2478       }
2479     }
2480     // TODO ???
2481   } // end Multiple Reduce implementation
2482   else { // Simple Reduce (!MultipleReduce)
2483     //=========================================================
2484     if (nr == nl) {
2485       if (nt < nb) {
2486         // it is a base case => not shift quad
2487         //ShiftQuad(quad,0,true);
2488       }
2489       else {
2490         // we have to shift quad on 2
2491         ShiftQuad(quad,2,true);
2492       }
2493     }
2494     else {
2495       if (nl > nr) {
2496         // we have to shift quad on 1
2497         ShiftQuad(quad,1,true);
2498       }
2499       else {
2500         // we have to shift quad on 3
2501         ShiftQuad(quad,3,true);
2502       }
2503     }
2504
2505     nb = quad->side[0]->NbPoints();
2506     nr = quad->side[1]->NbPoints();
2507     nt = quad->side[2]->NbPoints();
2508     nl = quad->side[3]->NbPoints();
2509  
2510     // number of rows and columns
2511     int nrows = nr - 1; // and also == nl - 1
2512     int ncol_top = nt - 1;
2513     int ncol_bot = nb - 1;
2514     int npair_top = ncol_top / 2;
2515     // maximum number of bottom elements for "linear" simple reduce
2516     int max_lin = ncol_top + npair_top * 2 * nrows;
2517     // maximum number of bottom elements for "tree" simple reduce
2518     //int max_tree = npair_top * pow(2, nrows + 1);
2519
2520     //if (ncol_bot > max_tree)
2521     //  MultipleReduce = true;
2522
2523     const vector<UVPtStruct>& uv_eb = quad->side[0]->GetUVPtStruct(true,0);
2524     const vector<UVPtStruct>& uv_er = quad->side[1]->GetUVPtStruct(false,1);
2525     const vector<UVPtStruct>& uv_et = quad->side[2]->GetUVPtStruct(true,1);
2526     const vector<UVPtStruct>& uv_el = quad->side[3]->GetUVPtStruct(false,0);
2527
2528     if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2529       return error(COMPERR_BAD_INPUT_MESH);
2530
2531     // arrays for normalized params
2532     TColStd_SequenceOfReal npb, npr, npt, npl;
2533     for (j = 0; j < nb; j++) {
2534       npb.Append(uv_eb[j].normParam);
2535     }
2536     for (i = 0; i < nr; i++) {
2537       npr.Append(uv_er[i].normParam);
2538     }
2539     for (j = 0; j < nt; j++) {
2540       npt.Append(uv_et[j].normParam);
2541     }
2542     for (i = 0; i < nl; i++) {
2543       npl.Append(uv_el[i].normParam);
2544     }
2545
2546     // We will ajust new points to this grid
2547     if (!SetNormalizedGrid(aMesh, aShape, quad))
2548       return false;
2549
2550     // TODO ???
2551     gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
2552     gp_XY a1 (uv_eb.back().u,  uv_eb.back().v);
2553     gp_XY a2 (uv_et.back().u,  uv_et.back().v);
2554     gp_XY a3 (uv_et.front().u, uv_et.front().v);
2555     //=========================================================
2556
2557     TColStd_SequenceOfInteger curr_base, next_base;
2558     TColStd_SequenceOfReal curr_par_u, curr_par_v;
2559     TColStd_SequenceOfReal next_par_u, next_par_v;
2560     StdMeshers_Array2OfNode NodesBRD (1,nb, 1,nr);
2561     for (j = 1; j <= nb; j++) {
2562       NodesBRD.SetValue(j, 1, uv_eb[j - 1].node); // bottom
2563       curr_base.Append(j);
2564       next_base.Append(-1);
2565       curr_par_u.Append(uv_eb[j-1].u);
2566       curr_par_v.Append(uv_eb[j-1].v);
2567       next_par_u.Append(0.);
2568       next_par_v.Append(0.);
2569     }
2570     for (j = 1; j <= nt; j++) {
2571       NodesBRD.SetValue(j, nr, uv_et[j - 1].node); // top
2572     }
2573
2574     int curr_base_len = nb;
2575     int next_base_len = 0;
2576
2577     if (ncol_bot > max_lin) {
2578       // "tree" simple reduce 2->4->8->16->32->...
2579       //
2580       //  .---------------.---------------.---------------.---------------. nr
2581       //  | \             |             / | \             |             / |
2582       //  |     \ .-------.-------. /     |     \ .-------.-------. /     |
2583       //  |       |       |       |       |       |       |       |       |
2584       //  .-------.-------.-------.-------.-------.-------.-------.-------.
2585       //  |\      |      /|\      |      /|\      |      /|\      |      /|
2586       //  |  \.---.---./  |  \.---.---./  |  \.---.---./  |  \.---.---./  | i
2587       //  |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
2588       //  .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
2589       //  |\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|
2590       //  | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
2591       //  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
2592       //  .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
2593       //  1                               j                               nb
2594
2595       for (i = 1; i < nr; i++) { // layer by layer
2596         // left
2597         NodesBRD.SetValue(1, i+1, uv_el[i].node);
2598         next_base.SetValue(++next_base_len, 1);
2599         // right
2600         NodesBRD.SetValue(nb, i+1, uv_er[i].node);
2601
2602         next_par_u.SetValue(next_base_len, uv_el[i].u);
2603         next_par_v.SetValue(next_base_len, uv_el[i].v);
2604
2605         // to stop reducing, if number of nodes reaches nt
2606         int delta = curr_base_len - nt;
2607
2608         //double du = uv_er[i].u - uv_el[i].u;
2609         //double dv = uv_er[i].v - uv_el[i].v;
2610
2611         // to calculate normalized parameter, we must know number of points in next layer
2612         int nb_four = (curr_base_len - 1) / 4;
2613         int nb_next = nb_four*2 + (curr_base_len - nb_four*4);
2614         if (nb_next < nt) nb_next = nt;
2615
2616         for (j = 1; j + 4 <= curr_base_len && delta > 0; j += 4, delta -= 2) {
2617           // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2618           //
2619           //  .-----a-----b i + 1
2620           //  |\ 5  | 6  /|
2621           //  | \   |   / |
2622           //  |  c--d--e  |
2623           //  |1 |2 |3 |4 |
2624           //  |  |  |  |  |
2625           //  .--.--.--.--. i
2626           //
2627           //  j     j+2   j+4
2628
2629           double u,v;
2630
2631           // a (i + 1, j + 2)
2632           const SMDS_MeshNode* Na;
2633           next_base_len++;
2634           next_base.SetValue(next_base_len, curr_base.Value(j + 2));
2635           if (i + 1 == nr) { // top
2636             Na = uv_et[next_base_len - 1].node;
2637             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na);
2638             u = uv_et[next_base_len - 1].u;
2639             v = uv_et[next_base_len - 1].v;
2640           }
2641           else {
2642             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2643             //u = uv_el[i].u + du * norm_par;
2644             //v = uv_el[i].v + dv * norm_par;
2645             {
2646               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2647               int nearest_node_j = (int)rel;
2648               rel -= nearest_node_j;
2649               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2650               double u1 = quad->uv_grid[ij].u;
2651               double v1 = quad->uv_grid[ij].v;
2652               double u2 = quad->uv_grid[ij + 1].u;
2653               double v2 = quad->uv_grid[ij + 1].v;
2654               double duj = (u2 - u1) * rel;
2655               double dvj = (v2 - v1) * rel;
2656               u = u1 + duj;
2657               v = v1 + dvj;
2658             }
2659             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 2));
2660             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 2));
2661             gp_Pnt P = S->Value(u,v);
2662             SMDS_MeshNode* Na1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2663             meshDS->SetNodeOnFace(Na1, geomFaceID, u, v);
2664             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na1);
2665             Na = Na1;
2666           }
2667           next_par_u.SetValue(next_base_len, u);
2668           next_par_v.SetValue(next_base_len, v);
2669
2670           // b (i + 1, j + 4)
2671           const SMDS_MeshNode* Nb;
2672           next_base_len++;
2673           next_base.SetValue(next_base_len, curr_base.Value(j + 4));
2674           if (i + 1 == nr) { // top
2675             Nb = uv_et[next_base_len - 1].node;
2676             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb);
2677             u = uv_et[next_base_len - 1].u;
2678             v = uv_et[next_base_len - 1].v;
2679           }
2680           else if (j + 4 == curr_base_len) { // right
2681             Nb = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
2682             u = uv_er[i].u;
2683             v = uv_er[i].v;
2684           }
2685           else {
2686             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2687             //u = uv_el[i].u + du * norm_par;
2688             //v = uv_el[i].v + dv * norm_par;
2689             {
2690               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2691               int nearest_node_j = (int)rel;
2692               rel -= nearest_node_j;
2693               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2694               double u1 = quad->uv_grid[ij].u;
2695               double v1 = quad->uv_grid[ij].v;
2696               double u2 = quad->uv_grid[ij + 1].u;
2697               double v2 = quad->uv_grid[ij + 1].v;
2698               double duj = (u2 - u1) * rel;
2699               double dvj = (v2 - v1) * rel;
2700               u = u1 + duj;
2701               v = v1 + dvj;
2702             }
2703             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 4));
2704             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 4));
2705             gp_Pnt P = S->Value(u,v);
2706             SMDS_MeshNode* Nb1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2707             meshDS->SetNodeOnFace(Nb1, geomFaceID, u, v);
2708             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb1);
2709             Nb = Nb1;
2710           }
2711           next_par_u.SetValue(next_base_len, u);
2712           next_par_v.SetValue(next_base_len, v);
2713
2714           // c
2715           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 2)) / 2.0;
2716           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 2)) / 2.0;
2717           gp_Pnt P = S->Value(u,v);
2718           SMDS_MeshNode* Nc = meshDS->AddNode(P.X(), P.Y(), P.Z());
2719           meshDS->SetNodeOnFace(Nc, geomFaceID, u, v);
2720
2721           // d
2722           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 1)) / 2.0;
2723           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 1)) / 2.0;
2724           P = S->Value(u,v);
2725           SMDS_MeshNode* Nd = meshDS->AddNode(P.X(), P.Y(), P.Z());
2726           meshDS->SetNodeOnFace(Nd, geomFaceID, u, v);
2727
2728           // e
2729           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len)) / 2.0;
2730           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len)) / 2.0;
2731           P = S->Value(u,v);
2732           SMDS_MeshNode* Ne = meshDS->AddNode(P.X(), P.Y(), P.Z());
2733           meshDS->SetNodeOnFace(Ne, geomFaceID, u, v);
2734
2735           // Faces
2736           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 0), i),
2737                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2738                                               Nc,
2739                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
2740           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2741
2742           SMDS_MeshFace* F2 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 1), i),
2743                                               NodesBRD.Value(curr_base.Value(j + 2), i),
2744                                               Nd, Nc);
2745           if (F2) meshDS->SetMeshElementOnShape(F2, geomFaceID);
2746
2747           SMDS_MeshFace* F3 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 2), i),
2748                                               NodesBRD.Value(curr_base.Value(j + 3), i),
2749                                               Ne, Nd);
2750           if (F3) meshDS->SetMeshElementOnShape(F3, geomFaceID);
2751
2752           SMDS_MeshFace* F4 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 3), i),
2753                                               NodesBRD.Value(curr_base.Value(j + 4), i),
2754                                               Nb, Ne);
2755           if (F4) meshDS->SetMeshElementOnShape(F4, geomFaceID);
2756
2757           SMDS_MeshFace* F5 = myTool->AddFace(Nc, Nd, Na,
2758                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
2759           if (F5) meshDS->SetMeshElementOnShape(F5, geomFaceID);
2760
2761           SMDS_MeshFace* F6 = myTool->AddFace(Nd, Ne, Nb, Na);
2762           if (F6) meshDS->SetMeshElementOnShape(F6, geomFaceID);
2763         }
2764
2765         // not reduced side elements (if any)
2766         for (; j < curr_base_len; j++) {
2767           // f (i + 1, j + 1)
2768           const SMDS_MeshNode* Nf;
2769           double u,v;
2770           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
2771           if (i + 1 == nr) { // top
2772             Nf = uv_et[next_base_len - 1].node;
2773             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
2774             u = uv_et[next_base_len - 1].u;
2775             v = uv_et[next_base_len - 1].v;
2776           }
2777           else if (j + 1 == curr_base_len) { // right
2778             Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
2779             u = uv_er[i].u;
2780             v = uv_er[i].v;
2781           }
2782           else {
2783             //double norm_par = double(next_base_len - 1)/double(nb_next - 1);
2784             //u = uv_el[i].u + du * norm_par;
2785             //v = uv_el[i].v + dv * norm_par;
2786             {
2787               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2788               int nearest_node_j = (int)rel;
2789               rel -= nearest_node_j;
2790               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2791               double u1 = quad->uv_grid[ij].u;
2792               double v1 = quad->uv_grid[ij].v;
2793               double u2 = quad->uv_grid[ij + 1].u;
2794               double v2 = quad->uv_grid[ij + 1].v;
2795               double duj = (u2 - u1) * rel;
2796               double dvj = (v2 - v1) * rel;
2797               u = u1 + duj;
2798               v = v1 + dvj;
2799             }
2800             //u = uv_el[i].u + du*npb.Value(curr_base.Value(j + 1));
2801             //v = uv_el[i].v + dv*npb.Value(curr_base.Value(j + 1));
2802             gp_Pnt P = S->Value(u,v);
2803             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2804             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
2805             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
2806             Nf = Nf1;
2807           }
2808           next_par_u.SetValue(next_base_len, u);
2809           next_par_v.SetValue(next_base_len, v);
2810           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
2811                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2812                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
2813                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
2814           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2815         }
2816
2817         curr_base_len = next_base_len;
2818         curr_base = next_base;
2819         curr_par_u = next_par_u;
2820         curr_par_v = next_par_v;
2821         next_base_len = 0;
2822       }
2823     } // end "tree" simple reduce
2824     else {
2825       // "linear" simple reduce 4->8->12->16 (3 steps)
2826       //
2827       //  .---------------.---------------.---------------.---------------. nr
2828       //  | \             |             / | \             |             / |
2829       //  |     \ .-------.-------. /     |     \ .-------.-------. /     |
2830       //  |       |       |       |       |       |       |       |       |
2831       //  .-------.-------.-------.-------.-------.-------.-------.-------.
2832       //  |      / \      |      / \      |      / \      |      / \      |
2833       //  |     /   \.----.----./   \     |     /   \.----.----./   \     | i
2834       //  |     /    |    |    |    \     |     /    |    |    |    \     |
2835       //  .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
2836       //  |     /   / \   |  /  \   \     |     /   / \   |  /  \   \     |
2837       //  |    /   /    .-.-.    \   \    |    /   /    .-.-.    \   \    |
2838       //  |   /   /    /  |  \    \   \   |   /   /    /  |  \    \   \   |
2839       //  .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
2840       //  1                               j                               nb
2841
2842       // nt = 5, nb = 7, nr = 4
2843       //int delta_all = 2;
2844       //int delta_one_col = 6;
2845       //int nb_col = 0;
2846       //int remainder = 2;
2847       //if (remainder > 0) nb_col++;
2848       //nb_col = 1;
2849       //int free_left = 1;
2850       //free_left += 2;
2851       //int free_middle = 4;
2852
2853       int delta_all = nb - nt;
2854       int delta_one_col = (nr - 1) * 2;
2855       int nb_col = delta_all / delta_one_col;
2856       int remainder = delta_all - nb_col * delta_one_col;
2857       if (remainder > 0) {
2858         nb_col++;
2859       }
2860       int free_left = ((nt - 1) - nb_col * 2) / 2;
2861       free_left += nr - 2;
2862       int free_middle = (nr - 2) * 2;
2863       if (remainder > 0 && nb_col == 1) {
2864         int nb_rows_short_col = remainder / 2;
2865         int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
2866         free_left -= nb_rows_thrown;
2867       }
2868
2869       // nt = 5, nb = 17, nr = 4
2870       //int delta_all = 12;
2871       //int delta_one_col = 6;
2872       //int nb_col = 2;
2873       //int remainder = 0;
2874       //int free_left = 2;
2875       //int free_middle = 4;
2876
2877       for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) { // layer by layer
2878         // left
2879         NodesBRD.SetValue(1, i+1, uv_el[i].node);
2880         next_base.SetValue(++next_base_len, 1);
2881         // right
2882         NodesBRD.SetValue(nb, i+1, uv_er[i].node);
2883
2884         // left
2885         next_par_u.SetValue(next_base_len, uv_el[i].u);
2886         next_par_v.SetValue(next_base_len, uv_el[i].v);
2887
2888         // to calculate normalized parameter, we must know number of points in next layer
2889         int nb_next = curr_base_len - nb_col * 2;
2890         if (remainder > 0 && i > remainder / 2)
2891           // take into account short "column"
2892           nb_next += 2;
2893         if (nb_next < nt) nb_next = nt;
2894
2895         // not reduced left elements
2896         for (j = 1; j <= free_left; j++) {
2897           // f (i + 1, j + 1)
2898           const SMDS_MeshNode* Nf;
2899           double u,v;
2900           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
2901           if (i + 1 == nr) { // top
2902             Nf = uv_et[next_base_len - 1].node;
2903             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
2904             u = uv_et[next_base_len - 1].u;
2905             v = uv_et[next_base_len - 1].v;
2906           }
2907           else {
2908             {
2909               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2910               int nearest_node_j = (int)rel;
2911               rel -= nearest_node_j;
2912               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2913               double u1 = quad->uv_grid[ij].u;
2914               double v1 = quad->uv_grid[ij].v;
2915               double u2 = quad->uv_grid[ij + 1].u;
2916               double v2 = quad->uv_grid[ij + 1].v;
2917               double duj = (u2 - u1) * rel;
2918               double dvj = (v2 - v1) * rel;
2919               u = u1 + duj;
2920               v = v1 + dvj;
2921             }
2922             gp_Pnt P = S->Value(u,v);
2923             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2924             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
2925             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
2926             Nf = Nf1;
2927           }
2928           next_par_u.SetValue(next_base_len, u);
2929           next_par_v.SetValue(next_base_len, v);
2930           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
2931                                               NodesBRD.Value(curr_base.Value(j + 1), i),
2932                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
2933                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
2934           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
2935         }
2936
2937         for (int icol = 1; icol <= nb_col; icol++) {
2938
2939           if (remainder > 0 && icol == nb_col && i > remainder / 2)
2940             // stop short "column"
2941             break;
2942
2943           // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2944           //
2945           //  .-----a-----b i + 1
2946           //  |\ 5  | 6  /|
2947           //  | \   |   / |
2948           //  |  c--d--e  |
2949           //  |1 |2 |3 |4 |
2950           //  |  |  |  |  |
2951           //  .--.--.--.--. i
2952           //
2953           //  j     j+2   j+4
2954
2955           double u,v;
2956
2957           // a (i + 1, j + 2)
2958           const SMDS_MeshNode* Na;
2959           next_base_len++;
2960           next_base.SetValue(next_base_len, curr_base.Value(j + 2));
2961           if (i + 1 == nr) { // top
2962             Na = uv_et[next_base_len - 1].node;
2963             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na);
2964             u = uv_et[next_base_len - 1].u;
2965             v = uv_et[next_base_len - 1].v;
2966           }
2967           else {
2968             {
2969               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
2970               int nearest_node_j = (int)rel;
2971               rel -= nearest_node_j;
2972               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
2973               double u1 = quad->uv_grid[ij].u;
2974               double v1 = quad->uv_grid[ij].v;
2975               double u2 = quad->uv_grid[ij + 1].u;
2976               double v2 = quad->uv_grid[ij + 1].v;
2977               double duj = (u2 - u1) * rel;
2978               double dvj = (v2 - v1) * rel;
2979               u = u1 + duj;
2980               v = v1 + dvj;
2981             }
2982             gp_Pnt P = S->Value(u,v);
2983             SMDS_MeshNode* Na1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
2984             meshDS->SetNodeOnFace(Na1, geomFaceID, u, v);
2985             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Na1);
2986             Na = Na1;
2987           }
2988           next_par_u.SetValue(next_base_len, u);
2989           next_par_v.SetValue(next_base_len, v);
2990
2991           // b (i + 1, j + 4)
2992           const SMDS_MeshNode* Nb;
2993           next_base_len++;
2994           next_base.SetValue(next_base_len, curr_base.Value(j + 4));
2995           if (i + 1 == nr) { // top
2996             Nb = uv_et[next_base_len - 1].node;
2997             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb);
2998             u = uv_et[next_base_len - 1].u;
2999             v = uv_et[next_base_len - 1].v;
3000           }
3001           else if (j + 4 == curr_base_len) { // right
3002             Nb = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3003             u = uv_er[i].u;
3004             v = uv_er[i].v;
3005           }
3006           else {
3007             {
3008               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3009               int nearest_node_j = (int)rel;
3010               rel -= nearest_node_j;
3011               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3012               double u1 = quad->uv_grid[ij].u;
3013               double v1 = quad->uv_grid[ij].v;
3014               double u2 = quad->uv_grid[ij + 1].u;
3015               double v2 = quad->uv_grid[ij + 1].v;
3016               double duj = (u2 - u1) * rel;
3017               double dvj = (v2 - v1) * rel;
3018               u = u1 + duj;
3019               v = v1 + dvj;
3020             }
3021             gp_Pnt P = S->Value(u,v);
3022             SMDS_MeshNode* Nb1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3023             meshDS->SetNodeOnFace(Nb1, geomFaceID, u, v);
3024             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nb1);
3025             Nb = Nb1;
3026           }
3027           next_par_u.SetValue(next_base_len, u);
3028           next_par_v.SetValue(next_base_len, v);
3029
3030           // c
3031           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 2)) / 2.0;
3032           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 2)) / 2.0;
3033           gp_Pnt P = S->Value(u,v);
3034           SMDS_MeshNode* Nc = meshDS->AddNode(P.X(), P.Y(), P.Z());
3035           meshDS->SetNodeOnFace(Nc, geomFaceID, u, v);
3036
3037           // d
3038           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len - 1)) / 2.0;
3039           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len - 1)) / 2.0;
3040           P = S->Value(u,v);
3041           SMDS_MeshNode* Nd = meshDS->AddNode(P.X(), P.Y(), P.Z());
3042           meshDS->SetNodeOnFace(Nd, geomFaceID, u, v);
3043
3044           // e
3045           u = (curr_par_u.Value(j + 2) + next_par_u.Value(next_base_len)) / 2.0;
3046           v = (curr_par_v.Value(j + 2) + next_par_v.Value(next_base_len)) / 2.0;
3047           P = S->Value(u,v);
3048           SMDS_MeshNode* Ne = meshDS->AddNode(P.X(), P.Y(), P.Z());
3049           meshDS->SetNodeOnFace(Ne, geomFaceID, u, v);
3050
3051           // Faces
3052           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 0), i),
3053                                               NodesBRD.Value(curr_base.Value(j + 1), i),
3054                                               Nc,
3055                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
3056           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3057
3058           SMDS_MeshFace* F2 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 1), i),
3059                                               NodesBRD.Value(curr_base.Value(j + 2), i),
3060                                               Nd, Nc);
3061           if (F2) meshDS->SetMeshElementOnShape(F2, geomFaceID);
3062
3063           SMDS_MeshFace* F3 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 2), i),
3064                                               NodesBRD.Value(curr_base.Value(j + 3), i),
3065                                               Ne, Nd);
3066           if (F3) meshDS->SetMeshElementOnShape(F3, geomFaceID);
3067
3068           SMDS_MeshFace* F4 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j + 3), i),
3069                                               NodesBRD.Value(curr_base.Value(j + 4), i),
3070                                               Nb, Ne);
3071           if (F4) meshDS->SetMeshElementOnShape(F4, geomFaceID);
3072
3073           SMDS_MeshFace* F5 = myTool->AddFace(Nc, Nd, Na,
3074                                               NodesBRD.Value(next_base.Value(next_base_len - 2), i + 1));
3075           if (F5) meshDS->SetMeshElementOnShape(F5, geomFaceID);
3076
3077           SMDS_MeshFace* F6 = myTool->AddFace(Nd, Ne, Nb, Na);
3078           if (F6) meshDS->SetMeshElementOnShape(F6, geomFaceID);
3079
3080           j += 4;
3081
3082           // not reduced middle elements
3083           if (icol < nb_col) {
3084             if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3085               // pass middle elements before stopped short "column"
3086               break;
3087
3088             int free_add = free_middle;
3089             if (remainder > 0 && icol == nb_col - 1)
3090               // next "column" is short
3091               free_add -= (nr - 1) - (remainder / 2);
3092
3093             for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3094               // f (i + 1, j + imiddle)
3095               const SMDS_MeshNode* Nf;
3096               double u,v;
3097               next_base.SetValue(++next_base_len, curr_base.Value(j + imiddle));
3098               if (i + 1 == nr) { // top
3099                 Nf = uv_et[next_base_len - 1].node;
3100                 NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
3101                 u = uv_et[next_base_len - 1].u;
3102                 v = uv_et[next_base_len - 1].v;
3103               }
3104               else if (j + imiddle == curr_base_len) { // right
3105                 Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3106                 u = uv_er[i].u;
3107                 v = uv_er[i].v;
3108               }
3109               else {
3110                 {
3111                   double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3112                   int nearest_node_j = (int)rel;
3113                   rel -= nearest_node_j;
3114                   int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3115                   double u1 = quad->uv_grid[ij].u;
3116                   double v1 = quad->uv_grid[ij].v;
3117                   double u2 = quad->uv_grid[ij + 1].u;
3118                   double v2 = quad->uv_grid[ij + 1].v;
3119                   double duj = (u2 - u1) * rel;
3120                   double dvj = (v2 - v1) * rel;
3121                   u = u1 + duj;
3122                   v = v1 + dvj;
3123                 }
3124                 gp_Pnt P = S->Value(u,v);
3125                 SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3126                 meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
3127                 NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
3128                 Nf = Nf1;
3129               }
3130               next_par_u.SetValue(next_base_len, u);
3131               next_par_v.SetValue(next_base_len, v);
3132               SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j - 1 + imiddle), i),
3133                                                   NodesBRD.Value(curr_base.Value(j + imiddle), i),
3134                                                   NodesBRD.Value(next_base.Value(next_base_len), i + 1),
3135                                                   NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
3136               if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3137             }
3138             j += free_add;
3139           }
3140         }
3141
3142         // not reduced right elements
3143         for (; j < curr_base_len; j++) {
3144           // f (i + 1, j + 1)
3145           const SMDS_MeshNode* Nf;
3146           double u,v;
3147           next_base.SetValue(++next_base_len, curr_base.Value(j + 1));
3148           if (i + 1 == nr) { // top
3149             Nf = uv_et[next_base_len - 1].node;
3150             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf);
3151             u = uv_et[next_base_len - 1].u;
3152             v = uv_et[next_base_len - 1].v;
3153           }
3154           else if (j + 1 == curr_base_len) { // right
3155             Nf = NodesBRD.Value(next_base.Value(next_base_len), i + 1);
3156             u = uv_er[i].u;
3157             v = uv_er[i].v;
3158           }
3159           else {
3160             {
3161               double rel = double(next_base_len - 1) * double(nt - 1) / double(nb_next - 1) + 1;
3162               int nearest_node_j = (int)rel;
3163               rel -= nearest_node_j;
3164               int ij = (i + 1 - 1) * nt + (nearest_node_j - 1);
3165               double u1 = quad->uv_grid[ij].u;
3166               double v1 = quad->uv_grid[ij].v;
3167               double u2 = quad->uv_grid[ij + 1].u;
3168               double v2 = quad->uv_grid[ij + 1].v;
3169               double duj = (u2 - u1) * rel;
3170               double dvj = (v2 - v1) * rel;
3171               u = u1 + duj;
3172               v = v1 + dvj;
3173             }
3174             gp_Pnt P = S->Value(u,v);
3175             SMDS_MeshNode* Nf1 = meshDS->AddNode(P.X(), P.Y(), P.Z());
3176             meshDS->SetNodeOnFace(Nf1, geomFaceID, u, v);
3177             NodesBRD.SetValue(next_base.Value(next_base_len), i + 1, Nf1);
3178             Nf = Nf1;
3179           }
3180           next_par_u.SetValue(next_base_len, u);
3181           next_par_v.SetValue(next_base_len, v);
3182           SMDS_MeshFace* F1 = myTool->AddFace(NodesBRD.Value(curr_base.Value(j), i),
3183                                               NodesBRD.Value(curr_base.Value(j + 1), i),
3184                                               NodesBRD.Value(next_base.Value(next_base_len), i + 1),
3185                                               NodesBRD.Value(next_base.Value(next_base_len - 1), i + 1));
3186           if (F1) meshDS->SetMeshElementOnShape(F1, geomFaceID);
3187         }
3188
3189         curr_base_len = next_base_len;
3190         curr_base = next_base;
3191         curr_par_u = next_par_u;
3192         curr_par_v = next_par_v;
3193         next_base_len = 0;
3194       }
3195     } // end "linear" simple reduce
3196   } // end Simple Reduce implementation
3197
3198   bool isOk = true;
3199   return isOk;
3200 }