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