1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : StdMeshers_Quadrangle_2D.cxx
24 // Author : Paul RASCLE, EDF
27 #include "StdMeshers_Quadrangle_2D.hxx"
29 #include "SMDS_EdgePosition.hxx"
30 #include "SMDS_FacePosition.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMESH_Block.hxx"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_Gen.hxx"
36 #include "SMESH_HypoFilter.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MeshAlgos.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMESH_subMesh.hxx"
41 #include "StdMeshers_FaceSide.hxx"
42 #include "StdMeshers_QuadrangleParams.hxx"
43 #include "StdMeshers_ViscousLayers2D.hxx"
45 #include <BRepClass_FaceClassifier.hxx>
46 #include <BRep_Tool.hxx>
47 #include <GeomAPI_ProjectPointOnSurf.hxx>
48 #include <Geom_Surface.hxx>
49 #include <NCollection_DefineArray2.hxx>
50 #include <Precision.hxx>
51 #include <Quantity_Parameter.hxx>
52 #include <TColStd_SequenceOfInteger.hxx>
53 #include <TColStd_SequenceOfReal.hxx>
54 #include <TColgp_SequenceOfXY.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <TopTools_DataMapOfShapeReal.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <TopTools_MapOfShape.hxx>
62 #include "utilities.h"
63 #include "Utils_ExceptHandlers.hxx"
65 #ifndef StdMeshers_Array2OfNode_HeaderFile
66 #define StdMeshers_Array2OfNode_HeaderFile
67 typedef const SMDS_MeshNode* SMDS_MeshNodePtr;
68 DEFINE_BASECOLLECTION (StdMeshers_BaseCollectionNodePtr, SMDS_MeshNodePtr)
69 DEFINE_ARRAY2(StdMeshers_Array2OfNode,
70 StdMeshers_BaseCollectionNodePtr, SMDS_MeshNodePtr)
76 typedef SMESH_Comment TComm;
78 //=============================================================================
82 //=============================================================================
84 StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D (int hypId, int studyId,
86 : SMESH_2D_Algo(hypId, studyId, gen),
87 myQuadranglePreference(false),
88 myTrianglePreference(false),
92 myQuadType(QUAD_STANDARD),
95 MESSAGE("StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D");
96 _name = "Quadrangle_2D";
97 _shapeType = (1 << TopAbs_FACE);
98 _compatibleHypothesis.push_back("QuadrangleParams");
99 _compatibleHypothesis.push_back("QuadranglePreference");
100 _compatibleHypothesis.push_back("TrianglePreference");
101 _compatibleHypothesis.push_back("ViscousLayers2D");
104 //=============================================================================
108 //=============================================================================
110 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
112 MESSAGE("StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D");
115 //=============================================================================
119 //=============================================================================
121 bool StdMeshers_Quadrangle_2D::CheckHypothesis
123 const TopoDS_Shape& aShape,
124 SMESH_Hypothesis::Hypothesis_Status& aStatus)
127 myQuadType = QUAD_STANDARD;
128 myQuadranglePreference = false;
129 myTrianglePreference = false;
130 myHelper = (SMESH_MesherHelper*)NULL;
135 aStatus = SMESH_Hypothesis::HYP_OK;
137 const list <const SMESHDS_Hypothesis * >& hyps =
138 GetUsedHypothesis(aMesh, aShape, false);
139 const SMESHDS_Hypothesis * aHyp = 0;
141 bool isFirstParams = true;
143 // First assigned hypothesis (if any) is processed now
144 if (hyps.size() > 0) {
146 if (strcmp("QuadrangleParams", aHyp->GetName()) == 0)
148 myParams = (const StdMeshers_QuadrangleParams*)aHyp;
149 myTriaVertexID = myParams->GetTriaVertex();
150 myQuadType = myParams->GetQuadType();
151 if (myQuadType == QUAD_QUADRANGLE_PREF ||
152 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
153 myQuadranglePreference = true;
154 else if (myQuadType == QUAD_TRIANGLE_PREF)
155 myTrianglePreference = true;
157 else if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
158 isFirstParams = false;
159 myQuadranglePreference = true;
161 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
162 isFirstParams = false;
163 myTrianglePreference = true;
166 isFirstParams = false;
170 // Second(last) assigned hypothesis (if any) is processed now
171 if (hyps.size() > 1) {
174 if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
175 myQuadranglePreference = true;
176 myTrianglePreference = false;
177 myQuadType = QUAD_STANDARD;
179 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
180 myQuadranglePreference = false;
181 myTrianglePreference = true;
182 myQuadType = QUAD_STANDARD;
186 const StdMeshers_QuadrangleParams* aHyp2 =
187 (const StdMeshers_QuadrangleParams*)aHyp;
188 myTriaVertexID = aHyp2->GetTriaVertex();
190 if (!myQuadranglePreference && !myTrianglePreference) { // priority of hypos
191 myQuadType = aHyp2->GetQuadType();
192 if (myQuadType == QUAD_QUADRANGLE_PREF ||
193 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
194 myQuadranglePreference = true;
195 else if (myQuadType == QUAD_TRIANGLE_PREF)
196 myTrianglePreference = true;
204 //=============================================================================
208 //=============================================================================
210 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
211 const TopoDS_Shape& aShape)
213 const TopoDS_Face& F = TopoDS::Face(aShape);
214 aMesh.GetSubMesh( F );
216 // do not initialize my fields before this as StdMeshers_ViscousLayers2D
217 // can call Compute() recursively
218 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
222 myProxyMesh = proxyMesh;
224 SMESH_MesherHelper helper (aMesh);
227 _quadraticMesh = myHelper->IsQuadraticSubMesh(aShape);
228 myNeedSmooth = false;
230 FaceQuadStruct::Ptr quad = CheckNbEdges( aMesh, F, /*considerMesh=*/true );
234 myQuadList.push_back( quad );
236 if ( !getEnforcedUV() )
239 updateDegenUV( quad );
241 int n1 = quad->side[0].NbPoints();
242 int n2 = quad->side[1].NbPoints();
243 int n3 = quad->side[2].NbPoints();
244 int n4 = quad->side[3].NbPoints();
246 enum { NOT_COMPUTED = -1, COMPUTE_FAILED = 0, COMPUTE_OK = 1 };
247 int res = NOT_COMPUTED;
248 if (myQuadranglePreference)
250 int nfull = n1+n2+n3+n4;
251 if ((nfull % 2) == 0 && ((n1 != n3) || (n2 != n4)))
253 // special path genarating only quandrangle faces
254 res = computeQuadPref( aMesh, F, quad );
257 else if (myQuadType == QUAD_REDUCED)
261 int n13tmp = n13/2; n13tmp = n13tmp*2;
262 int n24tmp = n24/2; n24tmp = n24tmp*2;
263 if ((n1 == n3 && n2 != n4 && n24tmp == n24) ||
264 (n2 == n4 && n1 != n3 && n13tmp == n13))
266 res = computeReduced( aMesh, F, quad );
270 if ( n1 != n3 && n2 != n4 )
271 error( COMPERR_WARNING,
272 "To use 'Reduced' transition, "
273 "two opposite sides should have same number of segments, "
274 "but actual number of segments is different on all sides. "
275 "'Standard' transion has been used.");
277 error( COMPERR_WARNING,
278 "To use 'Reduced' transition, "
279 "two opposite sides should have an even difference in number of segments. "
280 "'Standard' transion has been used.");
284 if ( res == NOT_COMPUTED )
286 if ( n1 != n3 || n2 != n4 )
287 res = computeTriangles( aMesh, F, quad );
289 res = computeQuadDominant( aMesh, F );
292 if ( res == COMPUTE_OK && myNeedSmooth )
295 return ( res == COMPUTE_OK );
298 //================================================================================
300 * \brief Compute quadrangles and triangles on the quad
302 //================================================================================
304 bool StdMeshers_Quadrangle_2D::computeTriangles(SMESH_Mesh& aMesh,
305 const TopoDS_Face& aFace,
306 FaceQuadStruct::Ptr quad)
308 int nb = quad->side[0].grid->NbPoints();
309 int nr = quad->side[1].grid->NbPoints();
310 int nt = quad->side[2].grid->NbPoints();
311 int nl = quad->side[3].grid->NbPoints();
313 // rotate the quad to have nbNodeOut sides on TOP [and LEFT]
315 quad->shift( nl > nr ? 3 : 2, true );
317 quad->shift( 1, true );
319 quad->shift( nt > nb ? 0 : 3, true );
321 if ( !setNormalizedGrid( quad ))
324 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
326 splitQuad( quad, 0, quad->jSize-2 );
328 if ( quad->nbNodeOut( QUAD_BOTTOM_SIDE )) // this should not happen
330 splitQuad( quad, 0, 1 );
332 FaceQuadStruct::Ptr newQuad = myQuadList.back();
333 if ( quad != newQuad ) // split done
336 FaceQuadStruct::Ptr botQuad = // a bottom part
337 ( quad->side[ QUAD_LEFT_SIDE ].from == 0 ) ? quad : newQuad;
338 if ( botQuad->nbNodeOut( QUAD_LEFT_SIDE ) > 0 )
339 botQuad->side[ QUAD_LEFT_SIDE ].to += botQuad->nbNodeOut( QUAD_LEFT_SIDE );
340 else if ( botQuad->nbNodeOut( QUAD_RIGHT_SIDE ) > 0 )
341 botQuad->side[ QUAD_RIGHT_SIDE ].to += botQuad->nbNodeOut( QUAD_RIGHT_SIDE );
343 // make quad be a greatest one
344 if ( quad->side[ QUAD_LEFT_SIDE ].NbPoints() == 2 ||
345 quad->side[ QUAD_RIGHT_SIDE ].NbPoints() == 2 )
347 if ( !setNormalizedGrid( quad ))
351 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
353 splitQuad( quad, quad->iSize-2, 0 );
355 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
357 splitQuad( quad, 1, 0 );
360 return computeQuadDominant( aMesh, aFace );
363 //================================================================================
365 * \brief Compute quadrangles and possibly triangles on all quads of myQuadList
367 //================================================================================
369 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
370 const TopoDS_Face& aFace)
372 if ( !addEnforcedNodes() )
375 std::list< FaceQuadStruct::Ptr >::iterator quad = myQuadList.begin();
376 for ( ; quad != myQuadList.end(); ++quad )
377 if ( !computeQuadDominant( aMesh, aFace, *quad ))
383 //================================================================================
385 * \brief Compute quadrangles and possibly triangles
387 //================================================================================
389 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
390 const TopoDS_Face& aFace,
391 FaceQuadStruct::Ptr quad)
393 // --- set normalized grid on unit square in parametric domain
395 if ( !setNormalizedGrid( quad ))
398 // --- create nodes on points, and create quadrangles
400 int nbhoriz = quad->iSize;
401 int nbvertic = quad->jSize;
403 // internal mesh nodes
404 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
405 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
406 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
407 for (i = 1; i < nbhoriz - 1; i++)
408 for (j = 1; j < nbvertic - 1; j++)
410 UVPtStruct& uvPnt = quad->UVPt( i, j );
411 gp_Pnt P = S->Value( uvPnt.u, uvPnt.v );
412 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
413 meshDS->SetNodeOnFace( uvPnt.node, geomFaceID, uvPnt.u, uvPnt.v );
419 // --.--.--.--.--.-- nbvertic
425 // ---.----.----.--- 0
426 // 0 > > > > > > > > nbhoriz
431 int iup = nbhoriz - 1;
432 if (quad->nbNodeOut(3)) { ilow++; } else { if (quad->nbNodeOut(1)) iup--; }
435 int jup = nbvertic - 1;
436 if (quad->nbNodeOut(0)) { jlow++; } else { if (quad->nbNodeOut(2)) jup--; }
438 // regular quadrangles
439 for (i = ilow; i < iup; i++) {
440 for (j = jlow; j < jup; j++) {
441 const SMDS_MeshNode *a, *b, *c, *d;
442 a = quad->uv_grid[ j * nbhoriz + i ].node;
443 b = quad->uv_grid[ j * nbhoriz + i + 1].node;
444 c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
445 d = quad->uv_grid[(j + 1) * nbhoriz + i ].node;
446 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
448 meshDS->SetMeshElementOnShape(face, geomFaceID);
453 // Boundary elements (must always be on an outer boundary of the FACE)
455 const vector<UVPtStruct>& uv_e0 = quad->side[0].grid->GetUVPtStruct();
456 const vector<UVPtStruct>& uv_e1 = quad->side[1].grid->GetUVPtStruct();
457 const vector<UVPtStruct>& uv_e2 = quad->side[2].grid->GetUVPtStruct();
458 const vector<UVPtStruct>& uv_e3 = quad->side[3].grid->GetUVPtStruct();
460 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
461 return error(COMPERR_BAD_INPUT_MESH);
463 double eps = Precision::Confusion();
465 int nbdown = (int) uv_e0.size();
466 int nbup = (int) uv_e2.size();
467 int nbright = (int) uv_e1.size();
468 int nbleft = (int) uv_e3.size();
470 if (quad->nbNodeOut(0) && nbvertic == 2) // this should not occure
474 // |___|___|___|___|___|___|
476 // |___|___|___|___|___|___|
478 // |___|___|___|___|___|___| __ first row of the regular grid
479 // . . . . . . . . . __ down edge nodes
481 // >->->->->->->->->->->->-> -- direction of processing
483 int g = 0; // number of last processed node in the regular grid
485 // number of last node of the down edge to be processed
486 int stop = nbdown - 1;
487 // if right edge is out, we will stop at a node, previous to the last one
488 //if (quad->nbNodeOut(1)) stop--;
489 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
490 quad->UVPt( nbhoriz-1, 1 ).node = uv_e1[1].node;
491 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
492 quad->UVPt( 0, 1 ).node = uv_e3[1].node;
494 // for each node of the down edge find nearest node
495 // in the first row of the regular grid and link them
496 for (i = 0; i < stop; i++) {
497 const SMDS_MeshNode *a, *b, *c, *d;
499 b = uv_e0[i + 1].node;
500 gp_Pnt pb (b->X(), b->Y(), b->Z());
502 // find node c in the regular grid, which will be linked with node b
505 // right bound reached, link with the rightmost node
507 c = quad->uv_grid[nbhoriz + iup].node;
510 // find in the grid node c, nearest to the b
511 double mind = RealLast();
512 for (int k = g; k <= iup; k++) {
514 const SMDS_MeshNode *nk;
515 if (k < ilow) // this can be, if left edge is out
516 nk = uv_e3[1].node; // get node from the left edge
518 nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
520 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
521 double dist = pb.Distance(pnk);
522 if (dist < mind - eps) {
532 if (near == g) { // make triangle
533 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
534 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
536 else { // make quadrangle
540 d = quad->uv_grid[nbhoriz + near - 1].node;
541 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
543 if (!myTrianglePreference){
544 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
545 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
548 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
551 // if node d is not at position g - make additional triangles
553 for (int k = near - 1; k > g; k--) {
554 c = quad->uv_grid[nbhoriz + k].node;
558 d = quad->uv_grid[nbhoriz + k - 1].node;
559 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
560 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
567 if (quad->nbNodeOut(2) && nbvertic == 2)
571 // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
573 // . . . . . . . . . __ up edge nodes
574 // ___ ___ ___ ___ ___ ___ __ first row of the regular grid
576 // |___|___|___|___|___|___|
578 // |___|___|___|___|___|___|
581 int g = nbhoriz - 1; // last processed node in the regular grid
587 // if left edge is out, we will stop at a second node
588 //if (quad->nbNodeOut(3)) stop++;
589 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
590 quad->UVPt( nbhoriz-1, 0 ).node = uv_e1[ nbright-2 ].node;
591 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
592 quad->UVPt( 0, 0 ).node = uv_e3[ nbleft-2 ].node;
594 // for each node of the up edge find nearest node
595 // in the first row of the regular grid and link them
596 for (i = nbup - 1; i > stop; i--) {
597 const SMDS_MeshNode *a, *b, *c, *d;
599 b = uv_e2[i - 1].node;
600 gp_Pnt pb (b->X(), b->Y(), b->Z());
602 // find node c in the grid, which will be linked with node b
604 if (i == stop + 1) { // left bound reached, link with the leftmost node
605 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
608 // find node c in the grid, nearest to the b
609 double mind = RealLast();
610 for (int k = g; k >= ilow; k--) {
611 const SMDS_MeshNode *nk;
613 nk = uv_e1[nbright - 2].node;
615 nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
616 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
617 double dist = pb.Distance(pnk);
618 if (dist < mind - eps) {
628 if (near == g) { // make triangle
629 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
630 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
632 else { // make quadrangle
634 d = uv_e1[nbright - 2].node;
636 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
637 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
638 if (!myTrianglePreference){
639 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
640 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
643 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
646 if (near + 1 < g) { // if d is not at g - make additional triangles
647 for (int k = near + 1; k < g; k++) {
648 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
650 d = uv_e1[nbright - 2].node;
652 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
653 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
654 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
663 // right or left boundary quadrangles
664 if (quad->nbNodeOut( QUAD_RIGHT_SIDE ) && nbhoriz == 2) // this should not occure
666 int g = 0; // last processed node in the grid
667 int stop = nbright - 1;
669 if (quad->side[ QUAD_RIGHT_SIDE ].from != i ) i++;
670 if (quad->side[ QUAD_RIGHT_SIDE ].to != stop ) stop--;
671 for ( ; i < stop; i++) {
672 const SMDS_MeshNode *a, *b, *c, *d;
674 b = uv_e1[i + 1].node;
675 gp_Pnt pb (b->X(), b->Y(), b->Z());
677 // find node c in the grid, nearest to the b
679 if (i == stop - 1) { // up bondary reached
680 c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
683 double mind = RealLast();
684 for (int k = g; k <= jup; k++) {
685 const SMDS_MeshNode *nk;
687 nk = uv_e0[nbdown - 2].node;
689 nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
690 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
691 double dist = pb.Distance(pnk);
692 if (dist < mind - eps) {
702 if (near == g) { // make triangle
703 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
704 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
706 else { // make quadrangle
708 d = uv_e0[nbdown - 2].node;
710 d = quad->uv_grid[nbhoriz*near - 2].node;
711 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
713 if (!myTrianglePreference){
714 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
715 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
718 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
721 if (near - 1 > g) { // if d not is at g - make additional triangles
722 for (int k = near - 1; k > g; k--) {
723 c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
725 d = uv_e0[nbdown - 2].node;
727 d = quad->uv_grid[nbhoriz*k - 2].node;
728 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
729 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
736 if (quad->nbNodeOut(3) && nbhoriz == 2) {
737 // MESSAGE("left edge is out");
738 int g = nbvertic - 1; // last processed node in the grid
740 i = quad->side[ QUAD_LEFT_SIDE ].to-1; // nbleft - 1;
741 for (; i > stop; i--) {
742 const SMDS_MeshNode *a, *b, *c, *d;
744 b = uv_e3[i - 1].node;
745 gp_Pnt pb (b->X(), b->Y(), b->Z());
747 // find node c in the grid, nearest to the b
749 if (i == stop + 1) { // down bondary reached
750 c = quad->uv_grid[nbhoriz*jlow + 1].node;
753 double mind = RealLast();
754 for (int k = g; k >= jlow; k--) {
755 const SMDS_MeshNode *nk;
759 nk = quad->uv_grid[nbhoriz*k + 1].node;
760 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
761 double dist = pb.Distance(pnk);
762 if (dist < mind - eps) {
772 if (near == g) { // make triangle
773 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
774 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
776 else { // make quadrangle
780 d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
781 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
782 if (!myTrianglePreference){
783 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
784 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
787 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
790 if (near + 1 < g) { // if d not is at g - make additional triangles
791 for (int k = near + 1; k < g; k++) {
792 c = quad->uv_grid[nbhoriz*k + 1].node;
796 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
797 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
798 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
812 //=============================================================================
816 //=============================================================================
818 bool StdMeshers_Quadrangle_2D::Evaluate(SMESH_Mesh& aMesh,
819 const TopoDS_Shape& aFace,
820 MapShapeNbElems& aResMap)
823 aMesh.GetSubMesh(aFace);
825 std::vector<int> aNbNodes(4);
826 bool IsQuadratic = false;
827 if (!checkNbEdgesForEvaluate(aMesh, aFace, aResMap, aNbNodes, IsQuadratic)) {
828 std::vector<int> aResVec(SMDSEntity_Last);
829 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
830 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
831 aResMap.insert(std::make_pair(sm,aResVec));
832 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
833 smError.reset(new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
837 if (myQuadranglePreference) {
838 int n1 = aNbNodes[0];
839 int n2 = aNbNodes[1];
840 int n3 = aNbNodes[2];
841 int n4 = aNbNodes[3];
842 int nfull = n1+n2+n3+n4;
845 if (nfull==ntmp && ((n1!=n3) || (n2!=n4))) {
846 // special path for using only quandrangle faces
847 return evaluateQuadPref(aMesh, aFace, aNbNodes, aResMap, IsQuadratic);
852 int nbdown = aNbNodes[0];
853 int nbup = aNbNodes[2];
855 int nbright = aNbNodes[1];
856 int nbleft = aNbNodes[3];
858 int nbhoriz = Min(nbdown, nbup);
859 int nbvertic = Min(nbright, nbleft);
861 int dh = Max(nbdown, nbup) - nbhoriz;
862 int dv = Max(nbright, nbleft) - nbvertic;
869 int nbNodes = (nbhoriz-2)*(nbvertic-2);
870 //int nbFaces3 = dh + dv + kdh*(nbvertic-1)*2 + kdv*(nbhoriz-1)*2;
871 int nbFaces3 = dh + dv;
872 //if (kdh==1 && kdv==1) nbFaces3 -= 2;
873 //if (dh>0 && dv>0) nbFaces3 -= 2;
874 //int nbFaces4 = (nbhoriz-1-kdh)*(nbvertic-1-kdv);
875 int nbFaces4 = (nbhoriz-1)*(nbvertic-1);
877 std::vector<int> aVec(SMDSEntity_Last);
878 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
880 aVec[SMDSEntity_Quad_Triangle] = nbFaces3;
881 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4;
882 int nbbndedges = nbdown + nbup + nbright + nbleft -4;
883 int nbintedges = (nbFaces4*4 + nbFaces3*3 - nbbndedges) / 2;
884 aVec[SMDSEntity_Node] = nbNodes + nbintedges;
885 if (aNbNodes.size()==5) {
886 aVec[SMDSEntity_Quad_Triangle] = nbFaces3 + aNbNodes[3] -1;
887 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4 - aNbNodes[3] +1;
891 aVec[SMDSEntity_Node] = nbNodes;
892 aVec[SMDSEntity_Triangle] = nbFaces3;
893 aVec[SMDSEntity_Quadrangle] = nbFaces4;
894 if (aNbNodes.size()==5) {
895 aVec[SMDSEntity_Triangle] = nbFaces3 + aNbNodes[3] - 1;
896 aVec[SMDSEntity_Quadrangle] = nbFaces4 - aNbNodes[3] + 1;
899 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
900 aResMap.insert(std::make_pair(sm,aVec));
906 //================================================================================
908 * \brief Return true if only two given edges meat at their common vertex
910 //================================================================================
912 static bool twoEdgesMeatAtVertex(const TopoDS_Edge& e1,
913 const TopoDS_Edge& e2,
917 if (!TopExp::CommonVertex(e1, e2, v))
919 TopTools_ListIteratorOfListOfShape ancestIt(mesh.GetAncestors(v));
920 for (; ancestIt.More() ; ancestIt.Next())
921 if (ancestIt.Value().ShapeType() == TopAbs_EDGE)
922 if (!e1.IsSame(ancestIt.Value()) && !e2.IsSame(ancestIt.Value()))
927 //=============================================================================
931 //=============================================================================
933 FaceQuadStruct::Ptr StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh & aMesh,
934 const TopoDS_Shape & aShape,
935 const bool considerMesh)
937 if ( !myQuadList.empty() && myQuadList.front()->face.IsSame( aShape ))
938 return myQuadList.front();
940 TopoDS_Face F = TopoDS::Face(aShape);
941 if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
942 const bool ignoreMediumNodes = _quadraticMesh;
944 // verify 1 wire only, with 4 edges
945 list< TopoDS_Edge > edges;
946 list< int > nbEdgesInWire;
947 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
949 error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
950 return FaceQuadStruct::Ptr();
953 // find corner vertices of the quad
954 vector<TopoDS_Vertex> corners;
955 int nbDegenEdges, nbSides = getCorners( F, aMesh, edges, corners, nbDegenEdges, considerMesh );
958 return FaceQuadStruct::Ptr();
960 FaceQuadStruct::Ptr quad( new FaceQuadStruct );
961 quad->side.reserve(nbEdgesInWire.front());
964 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
965 if ( nbSides == 3 ) // 3 sides and corners[0] is a vertex with myTriaVertexID
967 for ( int iSide = 0; iSide < 3; ++iSide )
969 list< TopoDS_Edge > sideEdges;
970 TopoDS_Vertex nextSideV = corners[( iSide + 1 ) % 3 ];
971 while ( edgeIt != edges.end() &&
972 !nextSideV.IsSame( SMESH_MesherHelper::IthVertex( 0, *edgeIt )))
973 if ( SMESH_Algo::isDegenerated( *edgeIt ))
976 sideEdges.push_back( *edgeIt++ );
977 if ( !sideEdges.empty() )
978 quad->side.push_back( StdMeshers_FaceSide::New(F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
979 ignoreMediumNodes, myProxyMesh));
983 const vector<UVPtStruct>& UVPSleft = quad->side[0].GetUVPtStruct(true,0);
984 /* vector<UVPtStruct>& UVPStop = */quad->side[1].GetUVPtStruct(false,1);
985 /* vector<UVPtStruct>& UVPSright = */quad->side[2].GetUVPtStruct(true,1);
986 const SMDS_MeshNode* aNode = UVPSleft[0].node;
987 gp_Pnt2d aPnt2d = UVPSleft[0].UV();
988 quad->side.push_back( StdMeshers_FaceSide::New( quad->side[1].grid.get(), aNode, &aPnt2d ));
989 myNeedSmooth = ( nbDegenEdges > 0 );
994 myNeedSmooth = ( corners.size() == 4 && nbDegenEdges > 0 );
995 int iSide = 0, nbUsedDegen = 0, nbLoops = 0;
996 for ( ; edgeIt != edges.end(); ++nbLoops )
998 list< TopoDS_Edge > sideEdges;
999 TopoDS_Vertex nextSideV = corners[( iSide + 1 - nbUsedDegen ) % corners.size() ];
1000 while ( edgeIt != edges.end() &&
1001 !nextSideV.IsSame( myHelper->IthVertex( 0, *edgeIt )))
1003 if ( SMESH_Algo::isDegenerated( *edgeIt ) )
1007 ++edgeIt; // no side on the degenerated EDGE
1011 if ( sideEdges.empty() )
1014 sideEdges.push_back( *edgeIt++ ); // a degenerated side
1019 break; // do not append a degenerated EDGE to a regular side
1025 sideEdges.push_back( *edgeIt++ );
1028 if ( !sideEdges.empty() )
1030 quad->side.push_back( StdMeshers_FaceSide::New( F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1031 ignoreMediumNodes, myProxyMesh ));
1034 else if ( !SMESH_Algo::isDegenerated( *edgeIt ) && // closed EDGE
1035 myHelper->IthVertex( 0, *edgeIt ).IsSame( myHelper->IthVertex( 1, *edgeIt )))
1037 quad->side.push_back( StdMeshers_FaceSide::New( F, *edgeIt++, &aMesh, iSide < QUAD_TOP_SIDE,
1038 ignoreMediumNodes, myProxyMesh));
1041 if ( quad->side.size() == 4 )
1045 error(TComm("Bug: infinite loop in StdMeshers_Quadrangle_2D::CheckNbEdges()"));
1050 if ( quad && quad->side.size() != 4 )
1052 error(TComm("Bug: ") << quad->side.size() << " sides found instead of 4");
1061 //=============================================================================
1065 //=============================================================================
1067 bool StdMeshers_Quadrangle_2D::checkNbEdgesForEvaluate(SMESH_Mesh& aMesh,
1068 const TopoDS_Shape & aShape,
1069 MapShapeNbElems& aResMap,
1070 std::vector<int>& aNbNodes,
1074 const TopoDS_Face & F = TopoDS::Face(aShape);
1076 // verify 1 wire only, with 4 edges
1077 list< TopoDS_Edge > edges;
1078 list< int > nbEdgesInWire;
1079 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1087 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1088 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1089 MapShapeNbElemsItr anIt = aResMap.find(sm);
1090 if (anIt==aResMap.end()) {
1093 std::vector<int> aVec = (*anIt).second;
1094 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
1095 if (nbEdgesInWire.front() == 3) { // exactly 3 edges
1096 if (myTriaVertexID>0) {
1097 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
1098 TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
1100 TopoDS_Edge E1,E2,E3;
1101 for (; edgeIt != edges.end(); ++edgeIt) {
1102 TopoDS_Edge E = TopoDS::Edge(*edgeIt);
1103 TopoDS_Vertex VF, VL;
1104 TopExp::Vertices(E, VF, VL, true);
1107 else if (VL.IsSame(V))
1112 SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
1113 MapShapeNbElemsItr anIt = aResMap.find(sm);
1114 if (anIt==aResMap.end()) return false;
1115 std::vector<int> aVec = (*anIt).second;
1117 aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1119 aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
1120 sm = aMesh.GetSubMesh(E2);
1121 anIt = aResMap.find(sm);
1122 if (anIt==aResMap.end()) return false;
1123 aVec = (*anIt).second;
1125 aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1127 aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1128 sm = aMesh.GetSubMesh(E3);
1129 anIt = aResMap.find(sm);
1130 if (anIt==aResMap.end()) return false;
1131 aVec = (*anIt).second;
1133 aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1135 aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1136 aNbNodes[3] = aNbNodes[1];
1142 if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1143 for (; edgeIt != edges.end(); edgeIt++) {
1144 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1145 MapShapeNbElemsItr anIt = aResMap.find(sm);
1146 if (anIt==aResMap.end()) {
1149 std::vector<int> aVec = (*anIt).second;
1151 aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1153 aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1157 else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1158 list< TopoDS_Edge > sideEdges;
1159 while (!edges.empty()) {
1161 sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1162 bool sameSide = true;
1163 while (!edges.empty() && sameSide) {
1164 sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1166 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1168 if (nbSides == 0) { // go backward from the first edge
1170 while (!edges.empty() && sameSide) {
1171 sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1173 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1176 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1177 aNbNodes[nbSides] = 1;
1178 for (; ite!=sideEdges.end(); ite++) {
1179 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1180 MapShapeNbElemsItr anIt = aResMap.find(sm);
1181 if (anIt==aResMap.end()) {
1184 std::vector<int> aVec = (*anIt).second;
1186 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1188 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1192 // issue 20222. Try to unite only edges shared by two same faces
1195 SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1196 while (!edges.empty()) {
1198 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1199 bool sameSide = true;
1200 while (!edges.empty() && sameSide) {
1202 SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1203 twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1205 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1207 if (nbSides == 0) { // go backward from the first edge
1209 while (!edges.empty() && sameSide) {
1211 SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1212 twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1214 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1217 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1218 aNbNodes[nbSides] = 1;
1219 for (; ite!=sideEdges.end(); ite++) {
1220 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1221 MapShapeNbElemsItr anIt = aResMap.find(sm);
1222 if (anIt==aResMap.end()) {
1225 std::vector<int> aVec = (*anIt).second;
1227 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1229 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1237 nbSides = nbEdgesInWire.front();
1238 error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1246 //=============================================================================
1250 //=============================================================================
1253 StdMeshers_Quadrangle_2D::CheckAnd2Dcompute (SMESH_Mesh & aMesh,
1254 const TopoDS_Shape & aShape,
1255 const bool CreateQuadratic)
1257 _quadraticMesh = CreateQuadratic;
1259 FaceQuadStruct::Ptr quad = CheckNbEdges(aMesh, aShape);
1262 // set normalized grid on unit square in parametric domain
1263 if ( ! setNormalizedGrid( quad ))
1271 inline const vector<UVPtStruct>& getUVPtStructIn(FaceQuadStruct::Ptr& quad, int i, int nbSeg)
1273 bool isXConst = (i == QUAD_BOTTOM_SIDE || i == QUAD_TOP_SIDE);
1274 double constValue = (i == QUAD_BOTTOM_SIDE || i == QUAD_LEFT_SIDE) ? 0 : 1;
1276 quad->nbNodeOut(i) ?
1277 quad->side[i].grid->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1278 quad->side[i].grid->GetUVPtStruct (isXConst,constValue);
1280 inline gp_UV calcUV(double x, double y,
1281 const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1282 const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1285 ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1286 ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1290 //=============================================================================
1294 //=============================================================================
1296 bool StdMeshers_Quadrangle_2D::setNormalizedGrid (FaceQuadStruct::Ptr quad)
1298 if ( !quad->uv_grid.empty() )
1301 // Algorithme décrit dans "Génération automatique de maillages"
1302 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
1303 // traitement dans le domaine paramétrique 2d u,v
1304 // transport - projection sur le carré unité
1307 // |<----north-2-------^ a3 -------------> a2
1309 // west-3 east-1 =right | |
1313 // v----south-0--------> a0 -------------> a1
1317 const FaceQuadStruct::Side & bSide = quad->side[0];
1318 const FaceQuadStruct::Side & rSide = quad->side[1];
1319 const FaceQuadStruct::Side & tSide = quad->side[2];
1320 const FaceQuadStruct::Side & lSide = quad->side[3];
1322 int nbhoriz = Min( bSide.NbPoints(), tSide.NbPoints() );
1323 int nbvertic = Min( rSide.NbPoints(), lSide.NbPoints() );
1325 if ( myQuadList.size() == 1 )
1327 // all sub-quads must have NO sides with nbNodeOut > 0
1328 quad->nbNodeOut(0) = Max( 0, bSide.grid->NbPoints() - tSide.grid->NbPoints() );
1329 quad->nbNodeOut(1) = Max( 0, rSide.grid->NbPoints() - lSide.grid->NbPoints() );
1330 quad->nbNodeOut(2) = Max( 0, tSide.grid->NbPoints() - bSide.grid->NbPoints() );
1331 quad->nbNodeOut(3) = Max( 0, lSide.grid->NbPoints() - rSide.grid->NbPoints() );
1333 const vector<UVPtStruct>& uv_e0 = bSide.GetUVPtStruct();
1334 const vector<UVPtStruct>& uv_e1 = rSide.GetUVPtStruct();
1335 const vector<UVPtStruct>& uv_e2 = tSide.GetUVPtStruct();
1336 const vector<UVPtStruct>& uv_e3 = lSide.GetUVPtStruct();
1337 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1338 //return error("Can't find nodes on sides");
1339 return error(COMPERR_BAD_INPUT_MESH);
1341 quad->uv_grid.resize( nbvertic * nbhoriz );
1342 quad->iSize = nbhoriz;
1343 quad->jSize = nbvertic;
1344 UVPtStruct *uv_grid = & quad->uv_grid[0];
1346 quad->uv_box.Clear();
1348 // copy data of face boundary
1350 FaceQuadStruct::SideIterator sideIter;
1354 const double x0 = bSide.First().normParam;
1355 const double dx = bSide.Last().normParam - bSide.First().normParam;
1356 for ( sideIter.Init( bSide ); sideIter.More(); sideIter.Next() ) {
1357 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1358 sideIter.UVPt().y = 0.;
1359 uv_grid[ j * nbhoriz + sideIter.Count() ] = sideIter.UVPt();
1360 quad->uv_box.Add( sideIter.UVPt().UV() );
1364 const int i = nbhoriz - 1;
1365 const double y0 = rSide.First().normParam;
1366 const double dy = rSide.Last().normParam - rSide.First().normParam;
1367 sideIter.Init( rSide );
1368 if ( quad->UVPt( i, sideIter.Count() ).node )
1369 sideIter.Next(); // avoid copying from a split emulated side
1370 for ( ; sideIter.More(); sideIter.Next() ) {
1371 sideIter.UVPt().x = 1.;
1372 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1373 uv_grid[ sideIter.Count() * nbhoriz + i ] = sideIter.UVPt();
1374 quad->uv_box.Add( sideIter.UVPt().UV() );
1378 const int j = nbvertic - 1;
1379 const double x0 = tSide.First().normParam;
1380 const double dx = tSide.Last().normParam - tSide.First().normParam;
1381 int i = 0, nb = nbhoriz;
1382 sideIter.Init( tSide );
1383 if ( quad->UVPt( nb-1, j ).node ) --nb; // avoid copying from a split emulated side
1384 for ( ; i < nb; i++, sideIter.Next()) {
1385 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1386 sideIter.UVPt().y = 1.;
1387 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1388 quad->uv_box.Add( sideIter.UVPt().UV() );
1393 const double y0 = lSide.First().normParam;
1394 const double dy = lSide.Last().normParam - lSide.First().normParam;
1395 int j = 0, nb = nbvertic;
1396 sideIter.Init( lSide );
1397 if ( quad->UVPt( i, j ).node )
1398 ++j, sideIter.Next(); // avoid copying from a split emulated side
1399 if ( quad->UVPt( i, nb-1 ).node )
1401 for ( ; j < nb; j++, sideIter.Next()) {
1402 sideIter.UVPt().x = 0.;
1403 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1404 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1405 quad->uv_box.Add( sideIter.UVPt().UV() );
1409 // normalized 2d parameters on grid
1411 for (int i = 1; i < nbhoriz-1; i++)
1413 const double x0 = quad->UVPt( i, 0 ).x;
1414 const double x1 = quad->UVPt( i, nbvertic-1 ).x;
1415 for (int j = 1; j < nbvertic-1; j++)
1417 const double y0 = quad->UVPt( 0, j ).y;
1418 const double y1 = quad->UVPt( nbhoriz-1, j ).y;
1419 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1420 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1421 double y = y0 + x * (y1 - y0);
1422 int ij = j * nbhoriz + i;
1425 uv_grid[ij].node = NULL;
1429 // projection on 2d domain (u,v)
1431 gp_UV a0 = quad->UVPt( 0, 0 ).UV();
1432 gp_UV a1 = quad->UVPt( nbhoriz-1, 0 ).UV();
1433 gp_UV a2 = quad->UVPt( nbhoriz-1, nbvertic-1 ).UV();
1434 gp_UV a3 = quad->UVPt( 0, nbvertic-1 ).UV();
1436 for (int i = 1; i < nbhoriz-1; i++)
1438 gp_UV p0 = quad->UVPt( i, 0 ).UV();
1439 gp_UV p2 = quad->UVPt( i, nbvertic-1 ).UV();
1440 for (int j = 1; j < nbvertic-1; j++)
1442 gp_UV p1 = quad->UVPt( nbhoriz-1, j ).UV();
1443 gp_UV p3 = quad->UVPt( 0, j ).UV();
1445 int ij = j * nbhoriz + i;
1446 double x = uv_grid[ij].x;
1447 double y = uv_grid[ij].y;
1449 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1451 uv_grid[ij].u = uv.X();
1452 uv_grid[ij].v = uv.Y();
1458 //=======================================================================
1459 //function : ShiftQuad
1460 //purpose : auxilary function for computeQuadPref
1461 //=======================================================================
1463 void StdMeshers_Quadrangle_2D::shiftQuad(FaceQuadStruct::Ptr& quad, const int num )
1465 quad->shift( num, /*ori=*/true, /*keepGrid=*/myQuadList.size() > 1 );
1468 //================================================================================
1470 * \brief Rotate sides of a quad by given nb of quartes
1471 * \param nb - number of rotation quartes
1472 * \param ori - to keep orientation of sides as in an unit quad or not
1473 * \param keepGrid - if \c true Side::grid is not changed, Side::from and Side::to
1474 * are altered instead
1476 //================================================================================
1478 void FaceQuadStruct::shift( size_t nb, bool ori, bool keepGrid )
1480 if ( nb == 0 ) return;
1482 vector< Side > newSides( side.size() );
1483 vector< Side* > sidePtrs( side.size() );
1484 for (int i = QUAD_BOTTOM_SIDE; i < NB_QUAD_SIDES; ++i)
1486 int id = (i + nb) % NB_QUAD_SIDES;
1489 bool wasForward = (i < QUAD_TOP_SIDE);
1490 bool newForward = (id < QUAD_TOP_SIDE);
1491 if ( wasForward != newForward )
1492 side[ i ].Reverse( keepGrid );
1494 newSides[ id ] = side[ i ];
1495 sidePtrs[ i ] = & side[ i ];
1497 // make newSides refer newSides via Side::Contact's
1498 for ( size_t i = 0; i < newSides.size(); ++i )
1500 FaceQuadStruct::Side& ns = newSides[ i ];
1501 for ( size_t iC = 0; iC < ns.contacts.size(); ++iC )
1503 FaceQuadStruct::Side* oSide = ns.contacts[iC].other_side;
1504 vector< Side* >::iterator sIt = std::find( sidePtrs.begin(), sidePtrs.end(), oSide );
1505 if ( sIt != sidePtrs.end() )
1506 ns.contacts[iC].other_side = & newSides[ *sIt - sidePtrs[0] ];
1509 newSides.swap( side );
1514 //=======================================================================
1516 //purpose : auxilary function for computeQuadPref
1517 //=======================================================================
1519 static gp_UV calcUV(double x0, double x1, double y0, double y1,
1520 FaceQuadStruct::Ptr& quad,
1521 const gp_UV& a0, const gp_UV& a1,
1522 const gp_UV& a2, const gp_UV& a3)
1524 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1525 double y = y0 + x * (y1 - y0);
1527 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1528 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1529 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1530 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1532 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1537 //=======================================================================
1538 //function : calcUV2
1539 //purpose : auxilary function for computeQuadPref
1540 //=======================================================================
1542 static gp_UV calcUV2(double x, double y,
1543 FaceQuadStruct::Ptr& quad,
1544 const gp_UV& a0, const gp_UV& a1,
1545 const gp_UV& a2, const gp_UV& a3)
1547 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1548 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1549 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1550 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1552 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1558 //=======================================================================
1560 * Create only quandrangle faces
1562 //=======================================================================
1564 bool StdMeshers_Quadrangle_2D::computeQuadPref (SMESH_Mesh & aMesh,
1565 const TopoDS_Face& aFace,
1566 FaceQuadStruct::Ptr quad)
1568 const bool OldVersion = (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED);
1569 const bool WisF = true;
1571 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1572 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
1573 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
1575 int nb = quad->side[0].NbPoints();
1576 int nr = quad->side[1].NbPoints();
1577 int nt = quad->side[2].NbPoints();
1578 int nl = quad->side[3].NbPoints();
1579 int dh = abs(nb-nt);
1580 int dv = abs(nr-nl);
1582 if ( myForcedPnts.empty() )
1584 // rotate sides to be as in the picture below and to have
1585 // dh >= dv and nt > nb
1587 shiftQuad( quad, ( nt > nb ) ? 0 : 2 );
1589 shiftQuad( quad, ( nr > nl ) ? 1 : 3 );
1593 // rotate the quad to have nt > nb [and nr > nl]
1595 shiftQuad ( quad, nr > nl ? 1 : 2 );
1597 shiftQuad( quad, nb == nt ? 1 : 0 );
1599 shiftQuad( quad, 3 );
1602 nb = quad->side[0].NbPoints();
1603 nr = quad->side[1].NbPoints();
1604 nt = quad->side[2].NbPoints();
1605 nl = quad->side[3].NbPoints();
1608 int nbh = Max(nb,nt);
1609 int nbv = Max(nr,nl);
1613 // Orientation of face and 3 main domain for future faces
1614 // ----------- Old version ---------------
1620 // left | |__| | rigth
1627 // ----------- New version ---------------
1633 // left |/________\| rigth
1641 const int bfrom = quad->side[0].from;
1642 const int rfrom = quad->side[1].from;
1643 const int tfrom = quad->side[2].from;
1644 const int lfrom = quad->side[3].from;
1646 const vector<UVPtStruct>& uv_eb_vec = quad->side[0].GetUVPtStruct(true,0);
1647 const vector<UVPtStruct>& uv_er_vec = quad->side[1].GetUVPtStruct(false,1);
1648 const vector<UVPtStruct>& uv_et_vec = quad->side[2].GetUVPtStruct(true,1);
1649 const vector<UVPtStruct>& uv_el_vec = quad->side[3].GetUVPtStruct(false,0);
1650 if (uv_eb_vec.empty() ||
1651 uv_er_vec.empty() ||
1652 uv_et_vec.empty() ||
1654 return error(COMPERR_BAD_INPUT_MESH);
1656 FaceQuadStruct::SideIterator uv_eb, uv_er, uv_et, uv_el;
1657 uv_eb.Init( quad->side[0] );
1658 uv_er.Init( quad->side[1] );
1659 uv_et.Init( quad->side[2] );
1660 uv_el.Init( quad->side[3] );
1662 gp_UV a0,a1,a2,a3, p0,p1,p2,p3, uv;
1665 a0 = uv_eb[ 0 ].UV();
1666 a1 = uv_er[ 0 ].UV();
1667 a2 = uv_er[ nr-1 ].UV();
1668 a3 = uv_et[ 0 ].UV();
1670 if ( !myForcedPnts.empty() )
1672 if ( dv != 0 && dh != 0 ) // here myQuadList.size() == 1
1674 const int dmin = Min( dv, dh );
1676 // Make a side separating domains L and Cb
1677 StdMeshers_FaceSidePtr sideLCb;
1678 UVPtStruct p3dom; // a point where 3 domains meat
1680 vector<UVPtStruct> pointsLCb( dmin+1 ); // 1--------1
1681 pointsLCb[0] = uv_eb[0]; // | | |
1682 for ( int i = 1; i <= dmin; ++i ) // | |Ct|
1684 x = uv_et[ i ].normParam; // | |__|
1685 y = uv_er[ i ].normParam; // | / |
1686 p0 = quad->side[0].grid->Value2d( x ).XY(); // | / Cb |dmin
1687 p1 = uv_er[ i ].UV(); // |/ |
1688 p2 = uv_et[ i ].UV(); // 0--------0
1689 p3 = quad->side[3].grid->Value2d( y ).XY();
1690 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1691 pointsLCb[ i ].u = uv.X();
1692 pointsLCb[ i ].v = uv.Y();
1694 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1695 p3dom = pointsLCb.back();
1697 // Make a side separating domains L and Ct
1698 StdMeshers_FaceSidePtr sideLCt;
1700 vector<UVPtStruct> pointsLCt( nl );
1701 pointsLCt[0] = p3dom;
1702 pointsLCt.back() = uv_et[ dmin ];
1703 x = uv_et[ dmin ].normParam;
1704 p0 = quad->side[0].grid->Value2d( x ).XY();
1705 p2 = uv_et[ dmin ].UV();
1706 double y0 = uv_er[ dmin ].normParam;
1707 for ( int i = 1; i < nl-1; ++i )
1709 y = y0 + i / ( nl-1. ) * ( 1. - y0 );
1710 p1 = quad->side[1].grid->Value2d( y ).XY();
1711 p3 = quad->side[3].grid->Value2d( y ).XY();
1712 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1713 pointsLCt[ i ].u = uv.X();
1714 pointsLCt[ i ].v = uv.Y();
1716 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
1718 // Make a side separating domains Cb and Ct
1719 StdMeshers_FaceSidePtr sideCbCt;
1721 vector<UVPtStruct> pointsCbCt( nb );
1722 pointsCbCt[0] = p3dom;
1723 pointsCbCt.back() = uv_er[ dmin ];
1724 y = uv_er[ dmin ].normParam;
1725 p1 = uv_er[ dmin ].UV();
1726 p3 = quad->side[3].grid->Value2d( y ).XY();
1727 double x0 = uv_et[ dmin ].normParam;
1728 for ( int i = 1; i < nb-1; ++i )
1730 x = x0 + i / ( nb-1. ) * ( 1. - x0 );
1731 p2 = quad->side[2].grid->Value2d( x ).XY();
1732 p0 = quad->side[0].grid->Value2d( x ).XY();
1733 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1734 pointsCbCt[ i ].u = uv.X();
1735 pointsCbCt[ i ].v = uv.Y();
1737 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
1740 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
1741 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
1742 qCb->side.resize(4);
1743 qCb->side[0] = quad->side[0];
1744 qCb->side[1] = quad->side[1];
1745 qCb->side[2] = sideCbCt;
1746 qCb->side[3] = sideLCb;
1747 qCb->side[1].to = dmin+1;
1749 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
1750 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
1752 qL->side[0] = sideLCb;
1753 qL->side[1] = sideLCt;
1754 qL->side[2] = quad->side[2];
1755 qL->side[3] = quad->side[3];
1756 qL->side[2].to = dmin+1;
1757 // Make Ct from the main quad
1758 FaceQuadStruct::Ptr qCt = quad;
1759 qCt->side[0] = sideCbCt;
1760 qCt->side[3] = sideLCt;
1761 qCt->side[1].from = dmin;
1762 qCt->side[2].from = dmin;
1763 qCt->uv_grid.clear();
1767 qCb->side[3].AddContact( dmin, & qCb->side[2], 0 );
1768 qCb->side[3].AddContact( dmin, & qCt->side[3], 0 );
1769 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
1770 qCt->side[0].AddContact( 0, & qL ->side[0], dmin );
1771 qL ->side[0].AddContact( dmin, & qL ->side[1], 0 );
1772 qL ->side[0].AddContact( dmin, & qCb->side[2], 0 );
1775 return computeQuadDominant( aMesh, aFace );
1777 return computeQuadPref( aMesh, aFace, qCt );
1779 } // if ( dv != 0 && dh != 0 )
1781 const int db = quad->side[0].IsReversed() ? -1 : +1;
1782 const int dr = quad->side[1].IsReversed() ? -1 : +1;
1783 const int dt = quad->side[2].IsReversed() ? -1 : +1;
1784 const int dl = quad->side[3].IsReversed() ? -1 : +1;
1786 // Case dv == 0, here possibly myQuadList.size() > 1
1798 const int lw = dh/2; // lateral width
1802 double lL = quad->side[3].Length();
1803 double lLwL = quad->side[2].Length( tfrom,
1804 tfrom + ( lw ) * dt );
1805 yCbL = lLwL / ( lLwL + lL );
1807 double lR = quad->side[1].Length();
1808 double lLwR = quad->side[2].Length( tfrom + ( lw + nb-1 ) * dt,
1809 tfrom + ( lw + nb-1 + lw ) * dt);
1810 yCbR = lLwR / ( lLwR + lR );
1812 // Make sides separating domains Cb and L and R
1813 StdMeshers_FaceSidePtr sideLCb, sideRCb;
1814 UVPtStruct pTBL, pTBR; // points where 3 domains meat
1816 vector<UVPtStruct> pointsLCb( lw+1 ), pointsRCb( lw+1 );
1817 pointsLCb[0] = uv_eb[ 0 ];
1818 pointsRCb[0] = uv_eb[ nb-1 ];
1819 for ( int i = 1, i2 = nt-2; i <= lw; ++i, --i2 )
1821 x = quad->side[2].Param( i );
1823 p0 = quad->side[0].Value2d( x );
1824 p1 = quad->side[1].Value2d( y );
1825 p2 = uv_et[ i ].UV();
1826 p3 = quad->side[3].Value2d( y );
1827 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1828 pointsLCb[ i ].u = uv.X();
1829 pointsLCb[ i ].v = uv.Y();
1830 pointsLCb[ i ].x = x;
1832 x = quad->side[2].Param( i2 );
1834 p1 = quad->side[1].Value2d( y );
1835 p0 = quad->side[0].Value2d( x );
1836 p2 = uv_et[ i2 ].UV();
1837 p3 = quad->side[3].Value2d( y );
1838 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1839 pointsRCb[ i ].u = uv.X();
1840 pointsRCb[ i ].v = uv.Y();
1841 pointsRCb[ i ].x = x;
1843 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1844 sideRCb = StdMeshers_FaceSide::New( pointsRCb, aFace );
1845 pTBL = pointsLCb.back();
1846 pTBR = pointsRCb.back();
1848 // Make sides separating domains Ct and L and R
1849 StdMeshers_FaceSidePtr sideLCt, sideRCt;
1851 vector<UVPtStruct> pointsLCt( nl ), pointsRCt( nl );
1852 pointsLCt[0] = pTBL;
1853 pointsLCt.back() = uv_et[ lw ];
1854 pointsRCt[0] = pTBR;
1855 pointsRCt.back() = uv_et[ lw + nb - 1 ];
1857 p0 = quad->side[0].Value2d( x );
1858 p2 = uv_et[ lw ].UV();
1859 int iR = lw + nb - 1;
1861 gp_UV p0R = quad->side[0].Value2d( xR );
1862 gp_UV p2R = uv_et[ iR ].UV();
1863 for ( int i = 1; i < nl-1; ++i )
1865 y = yCbL + ( 1. - yCbL ) * i / (nl-1.);
1866 p1 = quad->side[1].Value2d( y );
1867 p3 = quad->side[3].Value2d( y );
1868 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1869 pointsLCt[ i ].u = uv.X();
1870 pointsLCt[ i ].v = uv.Y();
1872 y = yCbR + ( 1. - yCbR ) * i / (nl-1.);
1873 p1 = quad->side[1].Value2d( y );
1874 p3 = quad->side[3].Value2d( y );
1875 uv = calcUV( xR,y, a0,a1,a2,a3, p0R,p1,p2R,p3 );
1876 pointsRCt[ i ].u = uv.X();
1877 pointsRCt[ i ].v = uv.Y();
1879 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
1880 sideRCt = StdMeshers_FaceSide::New( pointsRCt, aFace );
1882 // Make a side separating domains Cb and Ct
1883 StdMeshers_FaceSidePtr sideCbCt;
1885 vector<UVPtStruct> pointsCbCt( nb );
1886 pointsCbCt[0] = pTBL;
1887 pointsCbCt.back() = pTBR;
1888 p1 = quad->side[1].Value2d( yCbR );
1889 p3 = quad->side[3].Value2d( yCbL );
1890 for ( int i = 1; i < nb-1; ++i )
1892 x = quad->side[2].Param( i + lw );
1893 y = yCbL + ( yCbR - yCbL ) * i / (nb-1.);
1894 p2 = uv_et[ i + lw ].UV();
1895 p0 = quad->side[0].Value2d( x );
1896 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1897 pointsCbCt[ i ].u = uv.X();
1898 pointsCbCt[ i ].v = uv.Y();
1900 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
1903 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
1904 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
1905 qCb->side.resize(4);
1906 qCb->side[0] = quad->side[0];
1907 qCb->side[1] = sideRCb;
1908 qCb->side[2] = sideCbCt;
1909 qCb->side[3] = sideLCb;
1911 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
1912 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
1914 qL->side[0] = sideLCb;
1915 qL->side[1] = sideLCt;
1916 qL->side[2] = quad->side[2];
1917 qL->side[3] = quad->side[3];
1918 qL->side[2].to = ( lw + 1 ) * dt + tfrom;
1920 FaceQuadStruct* qR = new FaceQuadStruct( quad->face, "R" );
1921 myQuadList.push_back( FaceQuadStruct::Ptr( qR ));
1923 qR->side[0] = sideRCb;
1924 qR->side[0].from = lw;
1925 qR->side[0].to = -1;
1926 qR->side[0].di = -1;
1927 qR->side[1] = quad->side[1];
1928 qR->side[2] = quad->side[2];
1929 qR->side[2].from = ( lw + nb-1 ) * dt + tfrom;
1930 qR->side[3] = sideRCt;
1931 // Make Ct from the main quad
1932 FaceQuadStruct::Ptr qCt = quad;
1933 qCt->side[0] = sideCbCt;
1934 qCt->side[1] = sideRCt;
1935 qCt->side[2].from = ( lw ) * dt + tfrom;
1936 qCt->side[2].to = ( lw + nb ) * dt + tfrom;
1937 qCt->side[3] = sideLCt;
1938 qCt->uv_grid.clear();
1942 qCb->side[3].AddContact( lw, & qCb->side[2], 0 );
1943 qCb->side[3].AddContact( lw, & qCt->side[3], 0 );
1944 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
1945 qCt->side[0].AddContact( 0, & qL ->side[0], lw );
1946 qL ->side[0].AddContact( lw, & qL ->side[1], 0 );
1947 qL ->side[0].AddContact( lw, & qCb->side[2], 0 );
1949 qCb->side[1].AddContact( lw, & qCb->side[2], nb-1 );
1950 qCb->side[1].AddContact( lw, & qCt->side[1], 0 );
1951 qCt->side[0].AddContact( nb-1, & qCt->side[1], 0 );
1952 qCt->side[0].AddContact( nb-1, & qR ->side[0], lw );
1953 qR ->side[3].AddContact( 0, & qR ->side[0], lw );
1954 qR ->side[3].AddContact( 0, & qCb->side[2], nb-1 );
1956 return computeQuadDominant( aMesh, aFace );
1958 } // if ( !myForcedPnts.empty() )
1969 // arrays for normalized params
1970 TColStd_SequenceOfReal npb, npr, npt, npl;
1971 for (i=0; i<nb; i++) {
1972 npb.Append(uv_eb[i].normParam);
1974 for (i=0; i<nr; i++) {
1975 npr.Append(uv_er[i].normParam);
1977 for (i=0; i<nt; i++) {
1978 npt.Append(uv_et[i].normParam);
1980 for (i=0; i<nl; i++) {
1981 npl.Append(uv_el[i].normParam);
1986 // add some params to right and left after the first param
1989 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
1990 for (i=1; i<=dr; i++) {
1991 npr.InsertAfter(1,npr.Value(2)-dpr);
1995 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
1996 for (i=1; i<=dl; i++) {
1997 npl.InsertAfter(1,npl.Value(2)-dpr);
2001 int nnn = Min(nr,nl);
2002 // auxilary sequence of XY for creation nodes
2003 // in the bottom part of central domain
2004 // Length of UVL and UVR must be == nbv-nnn
2005 TColgp_SequenceOfXY UVL, UVR, UVT;
2008 // step1: create faces for left domain
2009 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2011 for (j=1; j<=nl; j++)
2012 NodesL.SetValue(1,j,uv_el[j-1].node);
2015 for (i=1; i<=dl; i++)
2016 NodesL.SetValue(i+1,nl,uv_et[i].node);
2017 // create and add needed nodes
2018 TColgp_SequenceOfXY UVtmp;
2019 for (i=1; i<=dl; i++) {
2020 double x0 = npt.Value(i+1);
2023 double y0 = npl.Value(i+1);
2024 double y1 = npr.Value(i+1);
2025 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2026 gp_Pnt P = S->Value(UV.X(),UV.Y());
2027 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2028 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2029 NodesL.SetValue(i+1,1,N);
2030 if (UVL.Length()<nbv-nnn) UVL.Append(UV);
2032 for (j=2; j<nl; j++) {
2033 double y0 = npl.Value(dl+j);
2034 double y1 = npr.Value(dl+j);
2035 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2036 gp_Pnt P = S->Value(UV.X(),UV.Y());
2037 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2038 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2039 NodesL.SetValue(i+1,j,N);
2040 if (i==dl) UVtmp.Append(UV);
2043 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn; i++) {
2044 UVL.Append(UVtmp.Value(i));
2047 for (i=1; i<=dl; i++) {
2048 for (j=1; j<nl; j++) {
2051 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2052 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2053 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2059 // fill UVL using c2d
2060 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
2061 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2065 // step2: create faces for right domain
2066 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2068 for (j=1; j<=nr; j++)
2069 NodesR.SetValue(1,j,uv_er[nr-j].node);
2072 for (i=1; i<=dr; i++)
2073 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2074 // create and add needed nodes
2075 TColgp_SequenceOfXY UVtmp;
2076 for (i=1; i<=dr; i++) {
2077 double x0 = npt.Value(nt-i);
2080 double y0 = npl.Value(i+1);
2081 double y1 = npr.Value(i+1);
2082 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2083 gp_Pnt P = S->Value(UV.X(),UV.Y());
2084 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2085 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2086 NodesR.SetValue(i+1,nr,N);
2087 if (UVR.Length()<nbv-nnn) UVR.Append(UV);
2089 for (j=2; j<nr; j++) {
2090 double y0 = npl.Value(nbv-j+1);
2091 double y1 = npr.Value(nbv-j+1);
2092 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2093 gp_Pnt P = S->Value(UV.X(),UV.Y());
2094 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2095 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2096 NodesR.SetValue(i+1,j,N);
2097 if (i==dr) UVtmp.Prepend(UV);
2100 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
2101 UVR.Append(UVtmp.Value(i));
2104 for (i=1; i<=dr; i++) {
2105 for (j=1; j<nr; j++) {
2108 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2109 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2110 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2116 // fill UVR using c2d
2117 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
2118 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2122 // step3: create faces for central domain
2123 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2124 // add first line using NodesL
2125 for (i=1; i<=dl+1; i++)
2126 NodesC.SetValue(1,i,NodesL(i,1));
2127 for (i=2; i<=nl; i++)
2128 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2129 // add last line using NodesR
2130 for (i=1; i<=dr+1; i++)
2131 NodesC.SetValue(nb,i,NodesR(i,nr));
2132 for (i=1; i<nr; i++)
2133 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2134 // add top nodes (last columns)
2135 for (i=dl+2; i<nbh-dr; i++)
2136 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2137 // add bottom nodes (first columns)
2138 for (i=2; i<nb; i++)
2139 NodesC.SetValue(i,1,uv_eb[i-1].node);
2141 // create and add needed nodes
2142 // add linear layers
2143 for (i=2; i<nb; i++) {
2144 double x0 = npt.Value(dl+i);
2146 for (j=1; j<nnn; j++) {
2147 double y0 = npl.Value(nbv-nnn+j);
2148 double y1 = npr.Value(nbv-nnn+j);
2149 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2150 gp_Pnt P = S->Value(UV.X(),UV.Y());
2151 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2152 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2153 NodesC.SetValue(i,nbv-nnn+j,N);
2158 // add diagonal layers
2159 gp_UV A2 = UVR.Value(nbv-nnn);
2160 gp_UV A3 = UVL.Value(nbv-nnn);
2161 for (i=1; i<nbv-nnn; i++) {
2162 gp_UV p1 = UVR.Value(i);
2163 gp_UV p3 = UVL.Value(i);
2164 double y = i / double(nbv-nnn);
2165 for (j=2; j<nb; j++) {
2166 double x = npb.Value(j);
2167 gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
2168 gp_UV p2 = UVT.Value( j-1 );
2169 gp_UV UV = calcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
2170 gp_Pnt P = S->Value(UV.X(),UV.Y());
2171 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2172 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2173 NodesC.SetValue(j,i+1,N);
2177 for (i=1; i<nb; i++) {
2178 for (j=1; j<nbv; j++) {
2181 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2182 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2183 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2189 else { // New version (!OldVersion)
2190 // step1: create faces for bottom rectangle domain
2191 StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
2192 // fill UVL and UVR using c2d
2193 for (j=0; j<nb; j++) {
2194 NodesBRD.SetValue(j+1,1,uv_eb[j].node);
2196 for (i=1; i<nnn-1; i++) {
2197 NodesBRD.SetValue(1,i+1,uv_el[i].node);
2198 NodesBRD.SetValue(nb,i+1,uv_er[i].node);
2199 for (j=2; j<nb; j++) {
2200 double x = npb.Value(j);
2201 double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
2202 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2203 gp_Pnt P = S->Value(UV.X(),UV.Y());
2204 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2205 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2206 NodesBRD.SetValue(j,i+1,N);
2209 for (j=1; j<nnn-1; j++) {
2210 for (i=1; i<nb; i++) {
2213 myHelper->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
2214 NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
2215 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2219 int drl = abs(nr-nl);
2220 // create faces for region C
2221 StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
2222 // add nodes from previous region
2223 for (j=1; j<=nb; j++) {
2224 NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
2226 if ((drl+addv) > 0) {
2231 TColgp_SequenceOfXY UVtmp;
2232 double drparam = npr.Value(nr) - npr.Value(nnn-1);
2233 double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
2235 for (i=1; i<=drl; i++) {
2236 // add existed nodes from right edge
2237 NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
2238 //double dtparam = npt.Value(i+1);
2239 y1 = npr.Value(nnn+i-1); // param on right edge
2240 double dpar = (y1 - npr.Value(nnn-1))/drparam;
2241 y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
2242 double dy = y1 - y0;
2243 for (j=1; j<nb; j++) {
2244 double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
2245 double y = y0 + dy*x;
2246 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2247 gp_Pnt P = S->Value(UV.X(),UV.Y());
2248 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2249 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2250 NodesC.SetValue(j,i+1,N);
2253 double dy0 = (1-y0)/(addv+1);
2254 double dy1 = (1-y1)/(addv+1);
2255 for (i=1; i<=addv; i++) {
2256 double yy0 = y0 + dy0*i;
2257 double yy1 = y1 + dy1*i;
2258 double dyy = yy1 - yy0;
2259 for (j=1; j<=nb; j++) {
2260 double x = npt.Value(i+1+drl) +
2261 npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
2262 double y = yy0 + dyy*x;
2263 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2264 gp_Pnt P = S->Value(UV.X(),UV.Y());
2265 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2266 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2267 NodesC.SetValue(j,i+drl+1,N);
2274 TColgp_SequenceOfXY UVtmp;
2275 double dlparam = npl.Value(nl) - npl.Value(nnn-1);
2276 double drparam = npr.Value(nnn) - npr.Value(nnn-1);
2277 double y0 = npl.Value(nnn-1);
2278 double y1 = npr.Value(nnn-1);
2279 for (i=1; i<=drl; i++) {
2280 // add existed nodes from right edge
2281 NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
2282 y0 = npl.Value(nnn+i-1); // param on left edge
2283 double dpar = (y0 - npl.Value(nnn-1))/dlparam;
2284 y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
2285 double dy = y1 - y0;
2286 for (j=2; j<=nb; j++) {
2287 double x = npb.Value(j)*npt.Value(nt-i);
2288 double y = y0 + dy*x;
2289 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2290 gp_Pnt P = S->Value(UV.X(),UV.Y());
2291 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2292 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2293 NodesC.SetValue(j,i+1,N);
2296 double dy0 = (1-y0)/(addv+1);
2297 double dy1 = (1-y1)/(addv+1);
2298 for (i=1; i<=addv; i++) {
2299 double yy0 = y0 + dy0*i;
2300 double yy1 = y1 + dy1*i;
2301 double dyy = yy1 - yy0;
2302 for (j=1; j<=nb; j++) {
2303 double x = npt.Value(i+1) +
2304 npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
2305 double y = yy0 + dyy*x;
2306 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2307 gp_Pnt P = S->Value(UV.X(),UV.Y());
2308 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2309 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2310 NodesC.SetValue(j,i+drl+1,N);
2315 for (j=1; j<=drl+addv; j++) {
2316 for (i=1; i<nb; i++) {
2319 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2320 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2321 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2326 StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
2327 for (i=1; i<=nt; i++) {
2328 NodesLast.SetValue(i,2,uv_et[i-1].node);
2331 for (i=n1; i<drl+addv+1; i++) {
2333 NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
2335 for (i=1; i<=nb; i++) {
2337 NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
2339 for (i=drl+addv; i>=n2; i--) {
2341 NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
2343 for (i=1; i<nt; i++) {
2346 myHelper->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
2347 NodesLast.Value(i+1,2), NodesLast.Value(i,2));
2348 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2351 } // if ((drl+addv) > 0)
2353 } // end new version implementation
2360 //=======================================================================
2362 * Evaluate only quandrangle faces
2364 //=======================================================================
2366 bool StdMeshers_Quadrangle_2D::evaluateQuadPref(SMESH_Mesh & aMesh,
2367 const TopoDS_Shape& aShape,
2368 std::vector<int>& aNbNodes,
2369 MapShapeNbElems& aResMap,
2372 // Auxilary key in order to keep old variant
2373 // of meshing after implementation new variant
2374 // for bug 0016220 from Mantis.
2375 bool OldVersion = false;
2376 if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
2379 const TopoDS_Face& F = TopoDS::Face(aShape);
2380 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2382 int nb = aNbNodes[0];
2383 int nr = aNbNodes[1];
2384 int nt = aNbNodes[2];
2385 int nl = aNbNodes[3];
2386 int dh = abs(nb-nt);
2387 int dv = abs(nr-nl);
2391 // it is a base case => not shift
2394 // we have to shift on 2
2403 // we have to shift quad on 1
2410 // we have to shift quad on 3
2420 int nbh = Max(nb,nt);
2421 int nbv = Max(nr,nl);
2436 // add some params to right and left after the first param
2443 int nnn = Min(nr,nl);
2448 // step1: create faces for left domain
2450 nbNodes += dl*(nl-1);
2451 nbFaces += dl*(nl-1);
2453 // step2: create faces for right domain
2455 nbNodes += dr*(nr-1);
2456 nbFaces += dr*(nr-1);
2458 // step3: create faces for central domain
2459 nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2460 nbFaces += (nb-1)*(nbv-1);
2462 else { // New version (!OldVersion)
2463 nbNodes += (nnn-2)*(nb-2);
2464 nbFaces += (nnn-2)*(nb-1);
2465 int drl = abs(nr-nl);
2466 nbNodes += drl*(nb-1) + addv*nb;
2467 nbFaces += (drl+addv)*(nb-1) + (nt-1);
2468 } // end new version implementation
2470 std::vector<int> aVec(SMDSEntity_Last);
2471 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2473 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2474 aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2475 if (aNbNodes.size()==5) {
2476 aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2477 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2481 aVec[SMDSEntity_Node] = nbNodes;
2482 aVec[SMDSEntity_Quadrangle] = nbFaces;
2483 if (aNbNodes.size()==5) {
2484 aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2485 aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2488 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2489 aResMap.insert(std::make_pair(sm,aVec));
2494 //=============================================================================
2495 /*! Split quadrangle in to 2 triangles by smallest diagonal
2498 //=============================================================================
2500 void StdMeshers_Quadrangle_2D::splitQuadFace(SMESHDS_Mesh * theMeshDS,
2502 const SMDS_MeshNode* theNode1,
2503 const SMDS_MeshNode* theNode2,
2504 const SMDS_MeshNode* theNode3,
2505 const SMDS_MeshNode* theNode4)
2507 SMDS_MeshFace* face;
2508 if ( SMESH_TNodeXYZ( theNode1 ).SquareDistance( theNode3 ) >
2509 SMESH_TNodeXYZ( theNode2 ).SquareDistance( theNode4 ) )
2511 face = myHelper->AddFace(theNode2, theNode4 , theNode1);
2512 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2513 face = myHelper->AddFace(theNode2, theNode3, theNode4);
2514 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2518 face = myHelper->AddFace(theNode1, theNode2 ,theNode3);
2519 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2520 face = myHelper->AddFace(theNode1, theNode3, theNode4);
2521 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2527 enum uvPos { UV_A0, UV_A1, UV_A2, UV_A3, UV_B, UV_R, UV_T, UV_L, UV_SIZE };
2529 inline SMDS_MeshNode* makeNode( UVPtStruct & uvPt,
2531 FaceQuadStruct::Ptr& quad,
2533 SMESH_MesherHelper* helper,
2534 Handle(Geom_Surface) S)
2536 const vector<UVPtStruct>& uv_eb = quad->side[QUAD_BOTTOM_SIDE].GetUVPtStruct();
2537 const vector<UVPtStruct>& uv_et = quad->side[QUAD_TOP_SIDE ].GetUVPtStruct();
2538 double rBot = ( uv_eb.size() - 1 ) * uvPt.normParam;
2539 double rTop = ( uv_et.size() - 1 ) * uvPt.normParam;
2540 int iBot = int( rBot );
2541 int iTop = int( rTop );
2542 double xBot = uv_eb[ iBot ].normParam + ( rBot - iBot ) * ( uv_eb[ iBot+1 ].normParam - uv_eb[ iBot ].normParam );
2543 double xTop = uv_et[ iTop ].normParam + ( rTop - iTop ) * ( uv_et[ iTop+1 ].normParam - uv_et[ iTop ].normParam );
2544 double x = xBot + y * ( xTop - xBot );
2546 gp_UV uv = calcUV(/*x,y=*/x, y,
2547 /*a0,...=*/UVs[UV_A0], UVs[UV_A1], UVs[UV_A2], UVs[UV_A3],
2548 /*p0=*/quad->side[QUAD_BOTTOM_SIDE].grid->Value2d( x ).XY(),
2550 /*p2=*/quad->side[QUAD_TOP_SIDE ].grid->Value2d( x ).XY(),
2551 /*p3=*/UVs[ UV_L ]);
2552 gp_Pnt P = S->Value( uv.X(), uv.Y() );
2555 return helper->AddNode(P.X(), P.Y(), P.Z(), 0, uv.X(), uv.Y() );
2558 void reduce42( const vector<UVPtStruct>& curr_base,
2559 vector<UVPtStruct>& next_base,
2561 int & next_base_len,
2562 FaceQuadStruct::Ptr& quad,
2565 SMESH_MesherHelper* helper,
2566 Handle(Geom_Surface)& S)
2568 // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2570 // .-----a-----b i + 1
2581 const SMDS_MeshNode*& Na = next_base[ ++next_base_len ].node;
2583 Na = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2586 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2588 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2591 double u = (curr_base[j + 2].u + next_base[next_base_len - 2].u) / 2.0;
2592 double v = (curr_base[j + 2].v + next_base[next_base_len - 2].v) / 2.0;
2593 gp_Pnt P = S->Value(u,v);
2594 SMDS_MeshNode* Nc = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2597 u = (curr_base[j + 2].u + next_base[next_base_len - 1].u) / 2.0;
2598 v = (curr_base[j + 2].v + next_base[next_base_len - 1].v) / 2.0;
2600 SMDS_MeshNode* Nd = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2603 u = (curr_base[j + 2].u + next_base[next_base_len].u) / 2.0;
2604 v = (curr_base[j + 2].v + next_base[next_base_len].v) / 2.0;
2606 SMDS_MeshNode* Ne = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2609 helper->AddFace(curr_base[j + 0].node,
2610 curr_base[j + 1].node, Nc,
2611 next_base[next_base_len - 2].node);
2613 helper->AddFace(curr_base[j + 1].node,
2614 curr_base[j + 2].node, Nd, Nc);
2616 helper->AddFace(curr_base[j + 2].node,
2617 curr_base[j + 3].node, Ne, Nd);
2619 helper->AddFace(curr_base[j + 3].node,
2620 curr_base[j + 4].node, Nb, Ne);
2622 helper->AddFace(Nc, Nd, Na, next_base[next_base_len - 2].node);
2624 helper->AddFace(Nd, Ne, Nb, Na);
2627 void reduce31( const vector<UVPtStruct>& curr_base,
2628 vector<UVPtStruct>& next_base,
2630 int & next_base_len,
2631 FaceQuadStruct::Ptr& quad,
2634 SMESH_MesherHelper* helper,
2635 Handle(Geom_Surface)& S)
2637 // add one "H": nodes b,c,e and faces 1,2,4,5
2639 // .---------b i + 1
2650 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2652 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2655 double u1 = (curr_base[ j ].u + next_base[ next_base_len-1 ].u ) / 2.0;
2656 double u2 = (curr_base[ j+3 ].u + next_base[ next_base_len ].u ) / 2.0;
2657 double u3 = (u2 - u1) / 3.0;
2659 double v1 = (curr_base[ j ].v + next_base[ next_base_len-1 ].v ) / 2.0;
2660 double v2 = (curr_base[ j+3 ].v + next_base[ next_base_len ].v ) / 2.0;
2661 double v3 = (v2 - v1) / 3.0;
2665 gp_Pnt P = S->Value(u,v);
2666 SMDS_MeshNode* Nc = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2671 SMDS_MeshNode* Ne = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2675 helper->AddFace( curr_base[ j + 0 ].node,
2676 curr_base[ j + 1 ].node,
2678 next_base[ next_base_len - 1 ].node);
2680 helper->AddFace( curr_base[ j + 1 ].node,
2681 curr_base[ j + 2 ].node, Ne, Nc);
2683 helper->AddFace( curr_base[ j + 2 ].node,
2684 curr_base[ j + 3 ].node, Nb, Ne);
2686 helper->AddFace(Nc, Ne, Nb,
2687 next_base[ next_base_len - 1 ].node);
2690 typedef void (* PReduceFunction) ( const vector<UVPtStruct>& curr_base,
2691 vector<UVPtStruct>& next_base,
2693 int & next_base_len,
2694 FaceQuadStruct::Ptr & quad,
2697 SMESH_MesherHelper* helper,
2698 Handle(Geom_Surface)& S);
2702 //=======================================================================
2704 * Implementation of Reduced algorithm (meshing with quadrangles only)
2706 //=======================================================================
2708 bool StdMeshers_Quadrangle_2D::computeReduced (SMESH_Mesh & aMesh,
2709 const TopoDS_Face& aFace,
2710 FaceQuadStruct::Ptr quad)
2712 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2713 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
2714 int i,j,geomFaceID = meshDS->ShapeToIndex(aFace);
2716 int nb = quad->side[0].NbPoints(); // bottom
2717 int nr = quad->side[1].NbPoints(); // right
2718 int nt = quad->side[2].NbPoints(); // top
2719 int nl = quad->side[3].NbPoints(); // left
2721 // Simple Reduce 10->8->6->4 (3 steps) Multiple Reduce 10->4 (1 step)
2723 // .-----.-----.-----.-----. .-----.-----.-----.-----.
2724 // | / \ | / \ | | / \ | / \ |
2725 // | / .--.--. \ | | / \ | / \ |
2726 // | / / | \ \ | | / .----.----. \ |
2727 // .---.---.---.---.---.---. | / / \ | / \ \ |
2728 // | / / \ | / \ \ | | / / \ | / \ \ |
2729 // | / / .-.-. \ \ | | / / .---.---. \ \ |
2730 // | / / / | \ \ \ | | / / / \ | / \ \ \ |
2731 // .--.--.--.--.--.--.--.--. | / / / \ | / \ \ \ |
2732 // | / / / \ | / \ \ \ | | / / / .-.-. \ \ \ |
2733 // | / / / .-.-. \ \ \ | | / / / / | \ \ \ \ |
2734 // | / / / / | \ \ \ \ | | / / / / | \ \ \ \ |
2735 // .-.-.-.--.--.--.--.-.-.-. .-.-.-.--.--.--.--.-.-.-.
2737 bool MultipleReduce = false;
2749 else if (nb == nt) {
2750 nr1 = nb; // and == nt
2764 // number of rows and columns
2765 int nrows = nr1 - 1;
2766 int ncol_top = nt1 - 1;
2767 int ncol_bot = nb1 - 1;
2768 // number of rows needed to reduce ncol_bot to ncol_top using simple 3->1 "tree" (see below)
2770 int( ceil( log( double(ncol_bot) / ncol_top) / log( 3.))); // = log x base 3
2771 if ( nrows < nrows_tree31 )
2773 MultipleReduce = true;
2774 error( COMPERR_WARNING,
2775 SMESH_Comment("To use 'Reduced' transition, "
2776 "number of face rows should be at least ")
2777 << nrows_tree31 << ". Actual number of face rows is " << nrows << ". "
2778 "'Quadrangle preference (reversed)' transion has been used.");
2782 if (MultipleReduce) { // == computeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2783 //==================================================
2784 int dh = abs(nb-nt);
2785 int dv = abs(nr-nl);
2789 // it is a base case => not shift quad but may be replacement is need
2793 // we have to shift quad on 2
2799 // we have to shift quad on 1
2803 // we have to shift quad on 3
2808 nb = quad->side[0].NbPoints();
2809 nr = quad->side[1].NbPoints();
2810 nt = quad->side[2].NbPoints();
2811 nl = quad->side[3].NbPoints();
2814 int nbh = Max(nb,nt);
2815 int nbv = Max(nr,nl);
2828 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
2829 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
2830 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
2831 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
2833 if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2834 return error(COMPERR_BAD_INPUT_MESH);
2836 // arrays for normalized params
2837 TColStd_SequenceOfReal npb, npr, npt, npl;
2838 for (j = 0; j < nb; j++) {
2839 npb.Append(uv_eb[j].normParam);
2841 for (i = 0; i < nr; i++) {
2842 npr.Append(uv_er[i].normParam);
2844 for (j = 0; j < nt; j++) {
2845 npt.Append(uv_et[j].normParam);
2847 for (i = 0; i < nl; i++) {
2848 npl.Append(uv_el[i].normParam);
2852 // orientation of face and 3 main domain for future faces
2858 // left | | | | rigth
2865 // add some params to right and left after the first param
2868 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
2869 for (i=1; i<=dr; i++) {
2870 npr.InsertAfter(1,npr.Value(2)-dpr);
2874 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
2875 for (i=1; i<=dl; i++) {
2876 npl.InsertAfter(1,npl.Value(2)-dpr);
2879 gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
2880 gp_XY a1 (uv_eb.back().u, uv_eb.back().v);
2881 gp_XY a2 (uv_et.back().u, uv_et.back().v);
2882 gp_XY a3 (uv_et.front().u, uv_et.front().v);
2884 int nnn = Min(nr,nl);
2885 // auxilary sequence of XY for creation of nodes
2886 // in the bottom part of central domain
2887 // it's length must be == nbv-nnn-1
2888 TColgp_SequenceOfXY UVL;
2889 TColgp_SequenceOfXY UVR;
2890 //==================================================
2892 // step1: create faces for left domain
2893 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2895 for (j=1; j<=nl; j++)
2896 NodesL.SetValue(1,j,uv_el[j-1].node);
2899 for (i=1; i<=dl; i++)
2900 NodesL.SetValue(i+1,nl,uv_et[i].node);
2901 // create and add needed nodes
2902 TColgp_SequenceOfXY UVtmp;
2903 for (i=1; i<=dl; i++) {
2904 double x0 = npt.Value(i+1);
2907 double y0 = npl.Value(i+1);
2908 double y1 = npr.Value(i+1);
2909 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2910 gp_Pnt P = S->Value(UV.X(),UV.Y());
2911 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2912 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2913 NodesL.SetValue(i+1,1,N);
2914 if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
2916 for (j=2; j<nl; j++) {
2917 double y0 = npl.Value(dl+j);
2918 double y1 = npr.Value(dl+j);
2919 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2920 gp_Pnt P = S->Value(UV.X(),UV.Y());
2921 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2922 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2923 NodesL.SetValue(i+1,j,N);
2924 if (i==dl) UVtmp.Append(UV);
2927 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
2928 UVL.Append(UVtmp.Value(i));
2931 for (i=1; i<=dl; i++) {
2932 for (j=1; j<nl; j++) {
2934 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2935 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2936 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2941 // fill UVL using c2d
2942 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
2943 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2947 // step2: create faces for right domain
2948 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2950 for (j=1; j<=nr; j++)
2951 NodesR.SetValue(1,j,uv_er[nr-j].node);
2954 for (i=1; i<=dr; i++)
2955 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2956 // create and add needed nodes
2957 TColgp_SequenceOfXY UVtmp;
2958 for (i=1; i<=dr; i++) {
2959 double x0 = npt.Value(nt-i);
2962 double y0 = npl.Value(i+1);
2963 double y1 = npr.Value(i+1);
2964 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2965 gp_Pnt P = S->Value(UV.X(),UV.Y());
2966 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2967 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2968 NodesR.SetValue(i+1,nr,N);
2969 if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
2971 for (j=2; j<nr; j++) {
2972 double y0 = npl.Value(nbv-j+1);
2973 double y1 = npr.Value(nbv-j+1);
2974 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2975 gp_Pnt P = S->Value(UV.X(),UV.Y());
2976 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2977 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2978 NodesR.SetValue(i+1,j,N);
2979 if (i==dr) UVtmp.Prepend(UV);
2982 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
2983 UVR.Append(UVtmp.Value(i));
2986 for (i=1; i<=dr; i++) {
2987 for (j=1; j<nr; j++) {
2989 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2990 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2991 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2996 // fill UVR using c2d
2997 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
2998 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
3002 // step3: create faces for central domain
3003 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
3004 // add first line using NodesL
3005 for (i=1; i<=dl+1; i++)
3006 NodesC.SetValue(1,i,NodesL(i,1));
3007 for (i=2; i<=nl; i++)
3008 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
3009 // add last line using NodesR
3010 for (i=1; i<=dr+1; i++)
3011 NodesC.SetValue(nb,i,NodesR(i,nr));
3012 for (i=1; i<nr; i++)
3013 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
3014 // add top nodes (last columns)
3015 for (i=dl+2; i<nbh-dr; i++)
3016 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
3017 // add bottom nodes (first columns)
3018 for (i=2; i<nb; i++)
3019 NodesC.SetValue(i,1,uv_eb[i-1].node);
3021 // create and add needed nodes
3022 // add linear layers
3023 for (i=2; i<nb; i++) {
3024 double x0 = npt.Value(dl+i);
3026 for (j=1; j<nnn; j++) {
3027 double y0 = npl.Value(nbv-nnn+j);
3028 double y1 = npr.Value(nbv-nnn+j);
3029 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3030 gp_Pnt P = S->Value(UV.X(),UV.Y());
3031 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3032 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3033 NodesC.SetValue(i,nbv-nnn+j,N);
3036 // add diagonal layers
3037 for (i=1; i<nbv-nnn; i++) {
3038 double du = UVR.Value(i).X() - UVL.Value(i).X();
3039 double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
3040 for (j=2; j<nb; j++) {
3041 double u = UVL.Value(i).X() + du*npb.Value(j);
3042 double v = UVL.Value(i).Y() + dv*npb.Value(j);
3043 gp_Pnt P = S->Value(u,v);
3044 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3045 meshDS->SetNodeOnFace(N, geomFaceID, u, v);
3046 NodesC.SetValue(j,i+1,N);
3050 for (i=1; i<nb; i++) {
3051 for (j=1; j<nbv; j++) {
3053 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
3054 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
3055 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
3058 } // end Multiple Reduce implementation
3059 else { // Simple Reduce (!MultipleReduce)
3060 //=========================================================
3063 // it is a base case => not shift quad
3064 //shiftQuad(quad,0,true);
3067 // we have to shift quad on 2
3073 // we have to shift quad on 1
3077 // we have to shift quad on 3
3082 nb = quad->side[0].NbPoints();
3083 nr = quad->side[1].NbPoints();
3084 nt = quad->side[2].NbPoints();
3085 nl = quad->side[3].NbPoints();
3087 // number of rows and columns
3088 int nrows = nr - 1; // and also == nl - 1
3089 int ncol_top = nt - 1;
3090 int ncol_bot = nb - 1;
3091 int npair_top = ncol_top / 2;
3092 // maximum number of bottom elements for "linear" simple reduce 4->2
3093 int max_lin42 = ncol_top + npair_top * 2 * nrows;
3094 // maximum number of bottom elements for "linear" simple reduce 3->1
3095 int max_lin31 = ncol_top + ncol_top * 2 * nrows;
3096 // maximum number of bottom elements for "tree" simple reduce 4->2
3098 // number of rows needed to reduce ncol_bot to ncol_top using simple 4->2 "tree"
3099 int nrows_tree42 = int( log( (double)(ncol_bot / ncol_top) )/log((double)2) ); // needed to avoid overflow at pow(2) while computing max_tree42
3100 if (nrows_tree42 < nrows) {
3101 max_tree42 = npair_top * pow(2.0, nrows + 1);
3102 if ( ncol_top > npair_top * 2 ) {
3103 int delta = ncol_bot - max_tree42;
3104 for (int irow = 1; irow < nrows; irow++) {
3105 int nfour = delta / 4;
3108 if (delta <= (ncol_top - npair_top * 2))
3109 max_tree42 = ncol_bot;
3112 // maximum number of bottom elements for "tree" simple reduce 3->1
3113 //int max_tree31 = ncol_top * pow(3.0, nrows);
3114 bool is_lin_31 = false;
3115 bool is_lin_42 = false;
3116 bool is_tree_31 = false;
3117 bool is_tree_42 = false;
3118 int max_lin = max_lin42;
3119 if (ncol_bot > max_lin42) {
3120 if (ncol_bot <= max_lin31) {
3122 max_lin = max_lin31;
3126 // if ncol_bot is a 3*n or not 2*n
3127 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3129 max_lin = max_lin31;
3135 if (ncol_bot > max_lin) { // not "linear"
3136 is_tree_31 = (ncol_bot > max_tree42);
3137 if (ncol_bot <= max_tree42) {
3138 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3147 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
3148 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
3149 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
3150 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
3152 if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
3153 return error(COMPERR_BAD_INPUT_MESH);
3155 myHelper->SetElementsOnShape( true );
3157 gp_UV uv[ UV_SIZE ];
3158 uv[ UV_A0 ].SetCoord( uv_eb.front().u, uv_eb.front().v);
3159 uv[ UV_A1 ].SetCoord( uv_eb.back().u, uv_eb.back().v );
3160 uv[ UV_A2 ].SetCoord( uv_et.back().u, uv_et.back().v );
3161 uv[ UV_A3 ].SetCoord( uv_et.front().u, uv_et.front().v);
3163 vector<UVPtStruct> curr_base = uv_eb, next_base;
3165 UVPtStruct nullUVPtStruct; nullUVPtStruct.node = 0;
3167 int curr_base_len = nb;
3168 int next_base_len = 0;
3171 { // ------------------------------------------------------------------
3172 // New algorithm implemented by request of IPAL22856
3173 // "2D quadrangle mesher of reduced type works wrong"
3174 // http://bugtracker.opencascade.com/show_bug.cgi?id=22856
3176 // the algorithm is following: all reduces are centred in horizontal
3177 // direction and are distributed among all rows
3179 if (ncol_bot > max_tree42) {
3183 if ((ncol_top/3)*3 == ncol_top ) {
3191 const int col_top_size = is_lin_42 ? 2 : 1;
3192 const int col_base_size = is_lin_42 ? 4 : 3;
3194 // Compute nb of "columns" (like in "linear" simple reducing) in all rows
3196 vector<int> nb_col_by_row;
3198 int delta_all = nb - nt;
3199 int delta_one_col = nrows * 2;
3200 int nb_col = delta_all / delta_one_col;
3201 int remainder = delta_all - nb_col * delta_one_col;
3202 if (remainder > 0) {
3205 if ( nb_col * col_top_size >= nt ) // == "tree" reducing situation
3207 // top row is full (all elements reduced), add "columns" one by one
3208 // in rows below until all bottom elements are reduced
3209 nb_col = ( nt - 1 ) / col_top_size;
3210 nb_col_by_row.resize( nrows, nb_col );
3211 int nbrows_not_full = nrows - 1;
3212 int cur_top_size = nt - 1;
3213 remainder = delta_all - nb_col * delta_one_col;
3214 while ( remainder > 0 )
3216 delta_one_col = nbrows_not_full * 2;
3217 int nb_col_add = remainder / delta_one_col;
3218 cur_top_size += 2 * nb_col_by_row[ nbrows_not_full ];
3219 int nb_col_free = cur_top_size / col_top_size - nb_col_by_row[ nbrows_not_full-1 ];
3220 if ( nb_col_add > nb_col_free )
3221 nb_col_add = nb_col_free;
3222 for ( int irow = 0; irow < nbrows_not_full; ++irow )
3223 nb_col_by_row[ irow ] += nb_col_add;
3225 remainder -= nb_col_add * delta_one_col;
3228 else // == "linear" reducing situation
3230 nb_col_by_row.resize( nrows, nb_col );
3232 for ( int irow = remainder / 2; irow < nrows; ++irow )
3233 nb_col_by_row[ irow ]--;
3238 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3240 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3242 for (i = 1; i < nr; i++) // layer by layer
3244 nb_col = nb_col_by_row[ i-1 ];
3245 int nb_next = curr_base_len - nb_col * 2;
3246 if (nb_next < nt) nb_next = nt;
3248 const double y = uv_el[ i ].normParam;
3250 if ( i + 1 == nr ) // top
3257 next_base.resize( nb_next, nullUVPtStruct );
3258 next_base.front() = uv_el[i];
3259 next_base.back() = uv_er[i];
3261 // compute normalized param u
3262 double du = 1. / ( nb_next - 1 );
3263 next_base[0].normParam = 0.;
3264 for ( j = 1; j < nb_next; ++j )
3265 next_base[j].normParam = next_base[j-1].normParam + du;
3267 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3268 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3270 int free_left = ( curr_base_len - 1 - nb_col * col_base_size ) / 2;
3271 int free_middle = curr_base_len - 1 - nb_col * col_base_size - 2 * free_left;
3273 // not reduced left elements
3274 for (j = 0; j < free_left; j++)
3277 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3279 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3281 myHelper->AddFace(curr_base[ j ].node,
3282 curr_base[ j+1 ].node,
3284 next_base[ next_base_len-1 ].node);
3287 for (int icol = 1; icol <= nb_col; icol++)
3290 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3292 j += reduce_grp_size;
3294 // elements in the middle of "columns" added for symmetry
3295 if ( free_middle > 0 && ( nb_col % 2 == 0 ) && icol == nb_col / 2 )
3297 for (int imiddle = 1; imiddle <= free_middle; imiddle++) {
3298 // f (i + 1, j + imiddle)
3299 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3301 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3303 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3304 curr_base[ j +imiddle ].node,
3306 next_base[ next_base_len-1 ].node);
3312 // not reduced right elements
3313 for (; j < curr_base_len-1; j++) {
3315 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3317 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3319 myHelper->AddFace(curr_base[ j ].node,
3320 curr_base[ j+1 ].node,
3322 next_base[ next_base_len-1 ].node);
3325 curr_base_len = next_base_len + 1;
3327 curr_base.swap( next_base );
3331 else if ( is_tree_42 || is_tree_31 )
3333 // "tree" simple reduce "42": 2->4->8->16->32->...
3335 // .-------------------------------.-------------------------------. nr
3337 // | \ .---------------.---------------. / |
3339 // .---------------.---------------.---------------.---------------.
3340 // | \ | / | \ | / |
3341 // | \ .-------.-------. / | \ .-------.-------. / |
3342 // | | | | | | | | |
3343 // .-------.-------.-------.-------.-------.-------.-------.-------. i
3344 // |\ | /|\ | /|\ | /|\ | /|
3345 // | \.---.---./ | \.---.---./ | \.---.---./ | \.---.---./ |
3346 // | | | | | | | | | | | | | | | | |
3347 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
3348 // |\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|
3349 // | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
3350 // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3351 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3354 // "tree" simple reduce "31": 1->3->9->27->...
3356 // .-----------------------------------------------------. nr
3358 // | .-----------------. |
3360 // .-----------------.-----------------.-----------------.
3361 // | \ / | \ / | \ / |
3362 // | .-----. | .-----. | .-----. | i
3363 // | | | | | | | | | |
3364 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.
3365 // |\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|
3366 // | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. |
3367 // | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3368 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3371 PReduceFunction reduceFunction = & ( is_tree_42 ? reduce42 : reduce31 );
3373 const int reduce_grp_size = is_tree_42 ? 4 : 3;
3375 for (i = 1; i < nr; i++) // layer by layer
3377 // to stop reducing, if number of nodes reaches nt
3378 int delta = curr_base_len - nt;
3380 // to calculate normalized parameter, we must know number of points in next layer
3381 int nb_reduce_groups = (curr_base_len - 1) / reduce_grp_size;
3382 int nb_next = nb_reduce_groups * (reduce_grp_size-2) + (curr_base_len - nb_reduce_groups*reduce_grp_size);
3383 if (nb_next < nt) nb_next = nt;
3385 const double y = uv_el[ i ].normParam;
3387 if ( i + 1 == nr ) // top
3394 next_base.resize( nb_next, nullUVPtStruct );
3395 next_base.front() = uv_el[i];
3396 next_base.back() = uv_er[i];
3398 // compute normalized param u
3399 double du = 1. / ( nb_next - 1 );
3400 next_base[0].normParam = 0.;
3401 for ( j = 1; j < nb_next; ++j )
3402 next_base[j].normParam = next_base[j-1].normParam + du;
3404 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3405 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3407 for (j = 0; j+reduce_grp_size < curr_base_len && delta > 0; j+=reduce_grp_size, delta-=2)
3409 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3412 // not reduced side elements (if any)
3413 for (; j < curr_base_len-1; j++)
3416 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3418 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3420 myHelper->AddFace(curr_base[ j ].node,
3421 curr_base[ j+1 ].node,
3423 next_base[ next_base_len-1 ].node);
3425 curr_base_len = next_base_len + 1;
3427 curr_base.swap( next_base );
3429 } // end "tree" simple reduce
3431 else if ( is_lin_42 || is_lin_31 ) {
3432 // "linear" simple reduce "31": 2->6->10->14
3434 // .-----------------------------.-----------------------------. nr
3436 // | .---------. | .---------. |
3438 // .---------.---------.---------.---------.---------.---------.
3439 // | / \ / \ | / \ / \ |
3440 // | / .-----. \ | / .-----. \ | i
3441 // | / | | \ | / | | \ |
3442 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.-----.
3443 // | / / \ / \ \ | / / \ / \ \ |
3444 // | / / .-. \ \ | / / .-. \ \ |
3445 // | / / / \ \ \ | / / / \ \ \ |
3446 // .--.----.---.-----.---.-----.-.--.----.---.-----.---.-----.-. 1
3449 // "linear" simple reduce "42": 4->8->12->16
3451 // .---------------.---------------.---------------.---------------. nr
3452 // | \ | / | \ | / |
3453 // | \ .-------.-------. / | \ .-------.-------. / |
3454 // | | | | | | | | |
3455 // .-------.-------.-------.-------.-------.-------.-------.-------.
3456 // | / \ | / \ | / \ | / \ |
3457 // | / \.----.----./ \ | / \.----.----./ \ | i
3458 // | / | | | \ | / | | | \ |
3459 // .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
3460 // | / / \ | / \ \ | / / \ | / \ \ |
3461 // | / / .-.-. \ \ | / / .-.-. \ \ |
3462 // | / / / | \ \ \ | / / / | \ \ \ |
3463 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
3466 // nt = 5, nb = 7, nr = 4
3467 //int delta_all = 2;
3468 //int delta_one_col = 6;
3470 //int remainder = 2;
3471 //if (remainder > 0) nb_col++;
3473 //int free_left = 1;
3475 //int free_middle = 4;
3477 int delta_all = nb - nt;
3478 int delta_one_col = (nr - 1) * 2;
3479 int nb_col = delta_all / delta_one_col;
3480 int remainder = delta_all - nb_col * delta_one_col;
3481 if (remainder > 0) {
3484 const int col_top_size = is_lin_42 ? 2 : 1;
3485 int free_left = ((nt - 1) - nb_col * col_top_size) / 2;
3486 free_left += nr - 2;
3487 int free_middle = (nr - 2) * 2;
3488 if (remainder > 0 && nb_col == 1) {
3489 int nb_rows_short_col = remainder / 2;
3490 int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
3491 free_left -= nb_rows_thrown;
3494 // nt = 5, nb = 17, nr = 4
3495 //int delta_all = 12;
3496 //int delta_one_col = 6;
3498 //int remainder = 0;
3499 //int free_left = 2;
3500 //int free_middle = 4;
3502 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3504 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3506 for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) // layer by layer
3508 // to calculate normalized parameter, we must know number of points in next layer
3509 int nb_next = curr_base_len - nb_col * 2;
3510 if (remainder > 0 && i > remainder / 2)
3511 // take into account short "column"
3513 if (nb_next < nt) nb_next = nt;
3515 const double y = uv_el[ i ].normParam;
3517 if ( i + 1 == nr ) // top
3524 next_base.resize( nb_next, nullUVPtStruct );
3525 next_base.front() = uv_el[i];
3526 next_base.back() = uv_er[i];
3528 // compute normalized param u
3529 double du = 1. / ( nb_next - 1 );
3530 next_base[0].normParam = 0.;
3531 for ( j = 1; j < nb_next; ++j )
3532 next_base[j].normParam = next_base[j-1].normParam + du;
3534 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3535 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3537 // not reduced left elements
3538 for (j = 0; j < free_left; j++)
3541 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3543 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3545 myHelper->AddFace(curr_base[ j ].node,
3546 curr_base[ j+1 ].node,
3548 next_base[ next_base_len-1 ].node);
3551 for (int icol = 1; icol <= nb_col; icol++) {
3553 if (remainder > 0 && icol == nb_col && i > remainder / 2)
3554 // stop short "column"
3558 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3560 j += reduce_grp_size;
3562 // not reduced middle elements
3563 if (icol < nb_col) {
3564 if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3565 // pass middle elements before stopped short "column"
3568 int free_add = free_middle;
3569 if (remainder > 0 && icol == nb_col - 1)
3570 // next "column" is short
3571 free_add -= (nr - 1) - (remainder / 2);
3573 for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3574 // f (i + 1, j + imiddle)
3575 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3577 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3579 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3580 curr_base[ j +imiddle ].node,
3582 next_base[ next_base_len-1 ].node);
3588 // not reduced right elements
3589 for (; j < curr_base_len-1; j++) {
3591 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3593 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3595 myHelper->AddFace(curr_base[ j ].node,
3596 curr_base[ j+1 ].node,
3598 next_base[ next_base_len-1 ].node);
3601 curr_base_len = next_base_len + 1;
3603 curr_base.swap( next_base );
3606 } // end "linear" simple reduce
3611 } // end Simple Reduce implementation
3617 //================================================================================
3618 namespace // data for smoothing
3621 // --------------------------------------------------------------------------------
3623 * \brief Structure used to check validity of node position after smoothing.
3624 * It holds two nodes connected to a smoothed node and belonging to
3631 TTriangle( TSmoothNode* n1=0, TSmoothNode* n2=0 ): _n1(n1), _n2(n2) {}
3633 inline bool IsForward( gp_UV uv ) const;
3635 // --------------------------------------------------------------------------------
3637 * \brief Data of a smoothed node
3643 vector< TTriangle > _triangles; // if empty, then node is not movable
3645 // --------------------------------------------------------------------------------
3646 inline bool TTriangle::IsForward( gp_UV uv ) const
3648 gp_Vec2d v1( uv, _n1->_uv ), v2( uv, _n2->_uv );
3654 //================================================================================
3656 * \brief Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3658 * WARNING: this method must be called AFTER retrieving UVPtStruct's from quad
3660 //================================================================================
3662 void StdMeshers_Quadrangle_2D::updateDegenUV(FaceQuadStruct::Ptr quad)
3666 // Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3667 // --------------------------------------------------------------------------
3668 for ( unsigned i = 0; i < quad->side.size(); ++i )
3670 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3672 // find which end of the side is on degenerated shape
3674 if ( myHelper->IsDegenShape( uvVec[0].node->getshapeId() ))
3676 else if ( myHelper->IsDegenShape( uvVec.back().node->getshapeId() ))
3677 degenInd = uvVec.size() - 1;
3681 // find another side sharing the degenerated shape
3682 bool isPrev = ( degenInd == 0 );
3683 if ( i >= QUAD_TOP_SIDE )
3685 int i2 = ( isPrev ? ( i + 3 ) : ( i + 1 )) % 4;
3686 const vector<UVPtStruct>& uvVec2 = quad->side[ i2 ].GetUVPtStruct();
3688 if ( uvVec[ degenInd ].node == uvVec2.front().node )
3690 else if ( uvVec[ degenInd ].node == uvVec2.back().node )
3691 degenInd2 = uvVec2.size() - 1;
3693 throw SALOME_Exception( LOCALIZED( "Logical error" ));
3695 // move UV in the middle
3696 uvPtStruct& uv1 = const_cast<uvPtStruct&>( uvVec [ degenInd ]);
3697 uvPtStruct& uv2 = const_cast<uvPtStruct&>( uvVec2[ degenInd2 ]);
3698 uv1.u = uv2.u = 0.5 * ( uv1.u + uv2.u );
3699 uv1.v = uv2.v = 0.5 * ( uv1.v + uv2.v );
3702 else if ( quad->side.size() == 4 && myQuadType == QUAD_STANDARD)
3704 // Set number of nodes on a degenerated side to be same as on an opposite side
3705 // ----------------------------------------------------------------------------
3706 for ( unsigned i = 0; i < quad->side.size(); ++i )
3708 StdMeshers_FaceSidePtr degSide = quad->side[i];
3709 if ( !myHelper->IsDegenShape( degSide->EdgeID(0) ))
3711 StdMeshers_FaceSidePtr oppSide = quad->side[( i+2 ) % quad->side.size() ];
3712 if ( degSide->NbSegments() == oppSide->NbSegments() )
3715 // make new side data
3716 const vector<UVPtStruct>& uvVecDegOld = degSide->GetUVPtStruct();
3717 const SMDS_MeshNode* n = uvVecDegOld[0].node;
3718 Handle(Geom2d_Curve) c2d = degSide->Curve2d(0);
3719 double f = degSide->FirstU(0), l = degSide->LastU(0);
3720 gp_Pnt2d p1 = uvVecDegOld.front().UV();
3721 gp_Pnt2d p2 = uvVecDegOld.back().UV();
3723 quad->side[i] = StdMeshers_FaceSide::New( oppSide.get(), n, &p1, &p2, c2d, f, l );
3727 //================================================================================
3729 * \brief Perform smoothing of 2D elements on a FACE with ignored degenerated EDGE
3731 //================================================================================
3733 void StdMeshers_Quadrangle_2D::smooth (FaceQuadStruct::Ptr quad)
3735 if ( !myNeedSmooth ) return;
3737 // Get nodes to smooth
3739 typedef map< const SMDS_MeshNode*, TSmoothNode, TIDCompare > TNo2SmooNoMap;
3740 TNo2SmooNoMap smooNoMap;
3742 const TopoDS_Face& geomFace = TopoDS::Face( myHelper->GetSubShape() );
3743 Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
3744 double U1, U2, V1, V2;
3745 surface->Bounds(U1, U2, V1, V2);
3746 GeomAPI_ProjectPointOnSurf proj;
3747 proj.Init( surface, U1, U2, V1, V2, BRep_Tool::Tolerance( geomFace ) );
3749 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
3750 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( geomFace );
3751 SMDS_NodeIteratorPtr nIt = fSubMesh->GetNodes();
3752 while ( nIt->more() ) // loop on nodes bound to a FACE
3754 const SMDS_MeshNode* node = nIt->next();
3755 TSmoothNode & sNode = smooNoMap[ node ];
3756 sNode._uv = myHelper->GetNodeUV( geomFace, node );
3757 sNode._xyz = SMESH_TNodeXYZ( node );
3759 // set sNode._triangles
3760 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator( SMDSAbs_Face );
3761 while ( fIt->more() )
3763 const SMDS_MeshElement* face = fIt->next();
3764 const int nbN = face->NbCornerNodes();
3765 const int nInd = face->GetNodeIndex( node );
3766 const int prevInd = myHelper->WrapIndex( nInd - 1, nbN );
3767 const int nextInd = myHelper->WrapIndex( nInd + 1, nbN );
3768 const SMDS_MeshNode* prevNode = face->GetNode( prevInd );
3769 const SMDS_MeshNode* nextNode = face->GetNode( nextInd );
3770 sNode._triangles.push_back( TTriangle( & smooNoMap[ prevNode ],
3771 & smooNoMap[ nextNode ]));
3774 // set _uv of smooth nodes on FACE boundary
3775 for ( unsigned i = 0; i < quad->side.size(); ++i )
3777 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3778 for ( unsigned j = 0; j < uvVec.size(); ++j )
3780 TSmoothNode & sNode = smooNoMap[ uvVec[j].node ];
3781 sNode._uv = uvVec[j].UV();
3782 sNode._xyz = SMESH_TNodeXYZ( uvVec[j].node );
3786 // define refernce orientation in 2D
3787 TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
3788 for ( ; n2sn != smooNoMap.end(); ++n2sn )
3789 if ( !n2sn->second._triangles.empty() )
3791 if ( n2sn == smooNoMap.end() ) return;
3792 const TSmoothNode & sampleNode = n2sn->second;
3793 const bool refForward = ( sampleNode._triangles[0].IsForward( sampleNode._uv ));
3797 for ( int iLoop = 0; iLoop < 5; ++iLoop )
3799 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
3801 TSmoothNode& sNode = n2sn->second;
3802 if ( sNode._triangles.empty() )
3803 continue; // not movable node
3806 bool isValid = false;
3807 bool use3D = ( iLoop > 2 ); // 3 loops in 2D and 2, in 3D
3811 // compute a new XYZ
3812 gp_XYZ newXYZ (0,0,0);
3813 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
3814 newXYZ += sNode._triangles[i]._n1->_xyz;
3815 newXYZ /= sNode._triangles.size();
3817 // compute a new UV by projection
3818 proj.Perform( newXYZ );
3819 isValid = ( proj.IsDone() && proj.NbPoints() > 0 );
3822 // check validity of the newUV
3823 Quantity_Parameter u,v;
3824 proj.LowerDistanceParameters( u, v );
3825 newUV.SetCoord( u, v );
3826 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
3827 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
3832 // compute a new UV by averaging
3833 newUV.SetCoord(0.,0.);
3834 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
3835 newUV += sNode._triangles[i]._n1->_uv;
3836 newUV /= sNode._triangles.size();
3838 // check validity of the newUV
3840 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
3841 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
3846 sNode._xyz = surface->Value( newUV.X(), newUV.Y() ).XYZ();
3851 // Set new XYZ to the smoothed nodes
3853 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
3855 TSmoothNode& sNode = n2sn->second;
3856 if ( sNode._triangles.empty() )
3857 continue; // not movable node
3859 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( n2sn->first );
3860 gp_Pnt xyz = surface->Value( sNode._uv.X(), sNode._uv.Y() );
3861 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
3864 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( sNode._uv.X(), sNode._uv.Y() )));
3867 // Move medium nodes in quadratic mesh
3868 if ( _quadraticMesh )
3870 const TLinkNodeMap& links = myHelper->GetTLinkNodeMap();
3871 TLinkNodeMap::const_iterator linkIt = links.begin();
3872 for ( ; linkIt != links.end(); ++linkIt )
3874 const SMESH_TLink& link = linkIt->first;
3875 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( linkIt->second );
3877 if ( node->getshapeId() != myHelper->GetSubShapeID() )
3878 continue; // medium node is on EDGE or VERTEX
3880 gp_XY uv1 = myHelper->GetNodeUV( geomFace, link.node1(), node );
3881 gp_XY uv2 = myHelper->GetNodeUV( geomFace, link.node2(), node );
3883 gp_XY uv = myHelper->GetMiddleUV( surface, uv1, uv2 );
3884 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( uv.X(), uv.Y() )));
3886 gp_Pnt xyz = surface->Value( uv.X(), uv.Y() );
3887 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
3892 /*//================================================================================
3894 * \brief Finds vertices at the most sharp face corners
3895 * \param [in] theFace - the FACE
3896 * \param [in,out] theWire - the ordered edges of the face. It can be modified to
3897 * have the first VERTEX of the first EDGE in \a vertices
3898 * \param [out] theVertices - the found corner vertices in the order corresponding to
3899 * the order of EDGEs in \a theWire
3900 * \param [out] theNbDegenEdges - nb of degenerated EDGEs in theFace
3901 * \param [in] theConsiderMesh - if \c true, only meshed VERTEXes are considered
3902 * as possible corners
3903 * \return int - number of quad sides found: 0, 3 or 4
3905 //================================================================================
3907 int StdMeshers_Quadrangle_2D::getCorners(const TopoDS_Face& theFace,
3908 SMESH_Mesh & theMesh,
3909 std::list<TopoDS_Edge>& theWire,
3910 std::vector<TopoDS_Vertex>& theVertices,
3911 int & theNbDegenEdges,
3912 const bool theConsiderMesh)
3914 theNbDegenEdges = 0;
3916 SMESH_MesherHelper helper( theMesh );
3918 // sort theVertices by angle
3919 multimap<double, TopoDS_Vertex> vertexByAngle;
3920 TopTools_DataMapOfShapeReal angleByVertex;
3921 TopoDS_Edge prevE = theWire.back();
3922 if ( SMESH_Algo::isDegenerated( prevE ))
3924 list<TopoDS_Edge>::reverse_iterator edge = ++theWire.rbegin();
3925 while ( SMESH_Algo::isDegenerated( *edge ))
3927 if ( edge == theWire.rend() )
3931 list<TopoDS_Edge>::iterator edge = theWire.begin();
3932 for ( ; edge != theWire.end(); ++edge )
3934 if ( SMESH_Algo::isDegenerated( *edge ))
3939 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
3940 if ( !theConsiderMesh || SMESH_Algo::VertexNode( v, helper.GetMeshDS() ))
3942 double angle = SMESH_MesherHelper::GetAngle( prevE, *edge, theFace );
3943 vertexByAngle.insert( make_pair( angle, v ));
3944 angleByVertex.Bind( v, angle );
3949 // find out required nb of corners (3 or 4)
3951 TopoDS_Shape triaVertex = helper.GetMeshDS()->IndexToShape( myTriaVertexID );
3952 if ( !triaVertex.IsNull() &&
3953 triaVertex.ShapeType() == TopAbs_VERTEX &&
3954 helper.IsSubShape( triaVertex, theFace ) &&
3955 ( vertexByAngle.size() != 4 || vertexByAngle.begin()->first < 5 * M_PI/180. ))
3958 triaVertex.Nullify();
3960 // check nb of available corners
3961 if ( nbCorners == 3 )
3963 if ( vertexByAngle.size() < 3 )
3964 return error(COMPERR_BAD_SHAPE,
3965 TComm("Face must have 3 sides but not ") << vertexByAngle.size() );
3969 if ( vertexByAngle.size() == 3 && theNbDegenEdges == 0 )
3971 if ( myTriaVertexID < 1 )
3972 return error(COMPERR_BAD_PARMETERS,
3973 "No Base vertex provided for a trilateral geometrical face");
3975 TComm comment("Invalid Base vertex: ");
3976 comment << myTriaVertexID << " its ID is not among [ ";
3977 multimap<double, TopoDS_Vertex>::iterator a2v = vertexByAngle.begin();
3978 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
3979 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
3980 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << " ]";
3981 return error(COMPERR_BAD_PARMETERS, comment );
3983 if ( vertexByAngle.size() + ( theNbDegenEdges > 0 ) < 4 &&
3984 vertexByAngle.size() + theNbDegenEdges != 4 )
3985 return error(COMPERR_BAD_SHAPE,
3986 TComm("Face must have 4 sides but not ") << vertexByAngle.size() );
3989 // put all corner vertices in a map
3990 TopTools_MapOfShape vMap;
3991 if ( nbCorners == 3 )
3992 vMap.Add( triaVertex );
3993 multimap<double, TopoDS_Vertex>::reverse_iterator a2v = vertexByAngle.rbegin();
3994 for ( ; a2v != vertexByAngle.rend() && vMap.Extent() < nbCorners; ++a2v )
3995 vMap.Add( (*a2v).second );
3997 // check if there are possible variations in choosing corners
3998 bool isThereVariants = false;
3999 if ( vertexByAngle.size() > nbCorners )
4001 double lostAngle = a2v->first;
4002 double lastAngle = ( --a2v, a2v->first );
4003 isThereVariants = ( lostAngle * 1.1 >= lastAngle );
4006 // make theWire begin from a corner vertex or triaVertex
4007 if ( nbCorners == 3 )
4008 while ( !triaVertex.IsSame( ( helper.IthVertex( 0, theWire.front() ))) ||
4009 SMESH_Algo::isDegenerated( theWire.front() ))
4010 theWire.splice( theWire.end(), theWire, theWire.begin() );
4012 while ( !vMap.Contains( helper.IthVertex( 0, theWire.front() )) ||
4013 SMESH_Algo::isDegenerated( theWire.front() ))
4014 theWire.splice( theWire.end(), theWire, theWire.begin() );
4016 // fill the result vector and prepare for its refinement
4017 theVertices.clear();
4018 vector< double > angles;
4019 vector< TopoDS_Edge > edgeVec;
4020 vector< int > cornerInd, nbSeg;
4021 angles.reserve( vertexByAngle.size() );
4022 edgeVec.reserve( vertexByAngle.size() );
4023 nbSeg.reserve( vertexByAngle.size() );
4024 cornerInd.reserve( nbCorners );
4025 for ( edge = theWire.begin(); edge != theWire.end(); ++edge )
4027 if ( SMESH_Algo::isDegenerated( *edge ))
4029 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4030 bool isCorner = vMap.Contains( v );
4033 theVertices.push_back( v );
4034 cornerInd.push_back( angles.size() );
4036 angles.push_back( angleByVertex.IsBound( v ) ? angleByVertex( v ) : -M_PI );
4037 edgeVec.push_back( *edge );
4038 if ( theConsiderMesh && isThereVariants )
4040 if ( SMESHDS_SubMesh* sm = helper.GetMeshDS()->MeshElements( *edge ))
4041 nbSeg.push_back( sm->NbNodes() + 1 );
4043 nbSeg.push_back( 0 );
4047 // refine the result vector - make sides elual by length if
4048 // there are several equal angles
4049 if ( isThereVariants )
4051 if ( nbCorners == 3 )
4052 angles[0] = 2 * M_PI; // not to move the base triangle VERTEX
4054 set< int > refinedCorners;
4055 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4057 int iV = cornerInd[iC];
4058 if ( !refinedCorners.insert( iV ).second )
4060 list< int > equalVertices;
4061 equalVertices.push_back( iV );
4062 int nbC[2] = { 0, 0 };
4063 // find equal angles backward and forward from the iV-th corner vertex
4064 for ( int isFwd = 0; isFwd < 2; ++isFwd )
4066 int dV = isFwd ? +1 : -1;
4067 int iCNext = helper.WrapIndex( iC + dV, cornerInd.size() );
4068 int iVNext = helper.WrapIndex( iV + dV, angles.size() );
4069 while ( iVNext != iV )
4071 bool equal = Abs( angles[iV] - angles[iVNext] ) < 0.1 * angles[iV];
4073 equalVertices.insert( isFwd ? equalVertices.end() : equalVertices.begin(), iVNext );
4074 if ( iVNext == cornerInd[ iCNext ])
4079 refinedCorners.insert( cornerInd[ iCNext ] );
4080 iCNext = helper.WrapIndex( iCNext + dV, cornerInd.size() );
4082 iVNext = helper.WrapIndex( iVNext + dV, angles.size() );
4085 // move corners to make sides equal by length
4086 int nbEqualV = equalVertices.size();
4087 int nbExcessV = nbEqualV - ( 1 + nbC[0] + nbC[1] );
4088 if ( nbExcessV > 0 )
4090 // calculate normalized length of each side enclosed between neighbor equalVertices
4091 vector< double > curLengths;
4092 double totalLen = 0;
4093 vector< int > evVec( equalVertices.begin(), equalVertices.end() );
4095 int iE = cornerInd[ helper.WrapIndex( iC - nbC[0] - 1, cornerInd.size() )];
4096 int iEEnd = cornerInd[ helper.WrapIndex( iC + nbC[1] + 1, cornerInd.size() )];
4097 while ( curLengths.size() < nbEqualV + 1 )
4099 curLengths.push_back( totalLen );
4101 curLengths.back() += SMESH_Algo::EdgeLength( edgeVec[ iE ]);
4102 iE = helper.WrapIndex( iE + 1, edgeVec.size());
4103 if ( iEV < evVec.size() && iE == evVec[ iEV++ ] )
4106 while( iE != iEEnd );
4107 totalLen = curLengths.back();
4109 curLengths.resize( equalVertices.size() );
4110 for ( size_t iS = 0; iS < curLengths.size(); ++iS )
4111 curLengths[ iS ] /= totalLen;
4113 // find equalVertices most close to the ideal sub-division of all sides
4115 int iCorner = helper.WrapIndex( iC - nbC[0], cornerInd.size() );
4116 int nbSides = 2 + nbC[0] + nbC[1];
4117 for ( int iS = 1; iS < nbSides; ++iS, ++iBestEV )
4119 double idealLen = iS / double( nbSides );
4120 double d, bestDist = 1.;
4121 for ( iEV = iBestEV; iEV < curLengths.size(); ++iEV )
4122 if (( d = Abs( idealLen - curLengths[ iEV ])) < bestDist )
4127 if ( iBestEV > iS-1 + nbExcessV )
4128 iBestEV = iS-1 + nbExcessV;
4129 theVertices[ iCorner ] = helper.IthVertex( 0, edgeVec[ evVec[ iBestEV ]]);
4130 iCorner = helper.WrapIndex( iCorner + 1, cornerInd.size() );
4139 //================================================================================
4141 * \brief Constructor of a side of quad
4143 //================================================================================
4145 FaceQuadStruct::Side::Side(StdMeshers_FaceSidePtr theGrid)
4146 : grid(theGrid), nbNodeOut(0), from(0), to(theGrid ? theGrid->NbPoints() : 0 ), di(1)
4150 //=============================================================================
4152 * \brief Constructor of a quad
4154 //=============================================================================
4156 FaceQuadStruct::FaceQuadStruct(const TopoDS_Face& F, const std::string& theName)
4157 : face( F ), name( theName )
4162 //================================================================================
4164 * \brief Fills myForcedPnts
4166 //================================================================================
4168 bool StdMeshers_Quadrangle_2D::getEnforcedUV()
4170 myForcedPnts.clear();
4171 if ( !myParams ) return true; // missing hypothesis
4173 std::vector< TopoDS_Shape > shapes;
4174 std::vector< gp_Pnt > points;
4175 myParams->GetEnforcedNodes( shapes, points );
4177 TopTools_IndexedMapOfShape vMap;
4178 for ( size_t i = 0; i < shapes.size(); ++i )
4179 if ( !shapes[i].IsNull() )
4180 TopExp::MapShapes( shapes[i], TopAbs_VERTEX, vMap );
4182 size_t nbPoints = points.size();
4183 for ( int i = 1; i <= vMap.Extent(); ++i )
4184 points.push_back( BRep_Tool::Pnt( TopoDS::Vertex( vMap( i ))));
4186 // find out if all points must be in the FACE, which is so if
4187 // myParams is a local hypothesis on the FACE being meshed
4188 bool isStrictCheck = false;
4190 SMESH_HypoFilter paramFilter( SMESH_HypoFilter::Is( myParams ));
4191 TopoDS_Shape assignedTo;
4192 if ( myHelper->GetMesh()->GetHypothesis( myHelper->GetSubShape(),
4196 isStrictCheck = ( assignedTo.IsSame( myHelper->GetSubShape() ));
4199 multimap< double, ForcedPoint > sortedFP; // sort points by distance from EDGEs
4201 Standard_Real u1,u2,v1,v2;
4202 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4203 const double tol = BRep_Tool::Tolerance( face );
4204 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4205 surf->Bounds( u1,u2,v1,v2 );
4206 GeomAPI_ProjectPointOnSurf project;
4207 project.Init(surf, u1,u2, v1,v2, tol );
4209 for ( size_t iP = 0; iP < points.size(); ++iP )
4211 project.Perform( points[ iP ]);
4212 if ( !project.IsDone() )
4214 if ( isStrictCheck && iP < nbPoints )
4216 (TComm("Projection of an enforced point to the face failed - (")
4217 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4220 if ( project.LowerDistance() > tol*1000 )
4222 if ( isStrictCheck && iP < nbPoints )
4224 (COMPERR_BAD_PARMETERS, TComm("An enforced point is too far from the face, dist = ")
4225 << project.LowerDistance() << " - ("
4226 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4229 Quantity_Parameter u, v;
4230 project.LowerDistanceParameters(u, v);
4231 gp_Pnt2d uv( u, v );
4232 BRepClass_FaceClassifier clsf ( face, uv, tol );
4233 switch ( clsf.State() ) {
4236 double edgeDist = ( Min( Abs( u - u1 ), Abs( u - u2 )) +
4237 Min( Abs( v - v1 ), Abs( v - v2 )));
4240 fp.xyz = points[ iP ].XYZ();
4241 if ( iP >= nbPoints )
4242 fp.vertex = TopoDS::Vertex( vMap( iP - nbPoints + 1 ));
4244 sortedFP.insert( make_pair( edgeDist, fp ));
4249 if ( isStrictCheck && iP < nbPoints )
4251 (COMPERR_BAD_PARMETERS, TComm("An enforced point is out of the face boundary - ")
4252 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4257 if ( isStrictCheck && iP < nbPoints )
4259 (COMPERR_BAD_PARMETERS, TComm("An enforced point is on the face boundary - ")
4260 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4265 if ( isStrictCheck && iP < nbPoints )
4267 (TComm("Classification of an enforced point ralative to the face boundary failed - ")
4268 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4273 multimap< double, ForcedPoint >::iterator d2uv = sortedFP.begin();
4274 for ( ; d2uv != sortedFP.end(); ++d2uv )
4275 myForcedPnts.push_back( (*d2uv).second );
4280 //================================================================================
4282 * \brief Splits quads by adding points of enforced nodes and create nodes on
4283 * the sides shared by quads
4285 //================================================================================
4287 bool StdMeshers_Quadrangle_2D::addEnforcedNodes()
4289 // if ( myForcedPnts.empty() )
4292 // make a map of quads sharing a side
4293 map< StdMeshers_FaceSidePtr, vector< FaceQuadStruct::Ptr > > quadsBySide;
4294 list< FaceQuadStruct::Ptr >::iterator quadIt = myQuadList.begin();
4295 for ( ; quadIt != myQuadList.end(); ++quadIt )
4296 for ( size_t iSide = 0; iSide < (*quadIt)->side.size(); ++iSide )
4298 if ( !setNormalizedGrid( *quadIt ))
4300 quadsBySide[ (*quadIt)->side[iSide] ].push_back( *quadIt );
4303 SMESH_Mesh* mesh = myHelper->GetMesh();
4304 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
4305 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4306 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4308 for ( size_t iFP = 0; iFP < myForcedPnts.size(); ++iFP )
4310 bool isNodeEnforced = false;
4312 // look for a quad enclosing a enforced point
4313 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4315 FaceQuadStruct::Ptr quad = *quadIt;
4316 if ( !setNormalizedGrid( *quadIt ))
4319 if ( !quad->findCell( myForcedPnts[ iFP ], i, j ))
4322 // a grid cell is found, select a node of the cell to move
4323 // to the enforced point to and to split the quad at
4324 multimap< double, pair< int, int > > ijByDist;
4325 for ( int di = 0; di < 2; ++di )
4326 for ( int dj = 0; dj < 2; ++dj )
4328 double dist2 = ( myForcedPnts[ iFP ].uv - quad->UVPt( i+di,j+dj ).UV() ).SquareModulus();
4329 ijByDist.insert( make_pair( dist2, make_pair( di,dj )));
4331 // try all nodes starting from the closest one
4332 set< FaceQuadStruct::Ptr > changedQuads;
4333 multimap< double, pair< int, int > >::iterator d2ij = ijByDist.begin();
4334 for ( ; !isNodeEnforced && d2ij != ijByDist.end(); ++d2ij )
4336 int di = d2ij->second.first;
4337 int dj = d2ij->second.second;
4339 // check if a node is at a side
4341 if ( dj== 0 && j == 0 )
4342 iSide = QUAD_BOTTOM_SIDE;
4343 else if ( dj == 1 && j+2 == quad->jSize )
4344 iSide = QUAD_TOP_SIDE;
4345 else if ( di == 0 && i == 0 )
4346 iSide = QUAD_LEFT_SIDE;
4347 else if ( di == 1 && i+2 == quad->iSize )
4348 iSide = QUAD_RIGHT_SIDE;
4350 if ( iSide > -1 ) // ----- node is at a side
4352 FaceQuadStruct::Side& side = quad->side[ iSide ];
4353 // check if this node can be moved
4354 if ( quadsBySide[ side ].size() < 2 )
4355 continue; // its a face boundary -> can't move the node
4357 int quadNodeIndex = ( iSide % 2 ) ? j : i;
4358 int sideNodeIndex = side.ToSideIndex( quadNodeIndex );
4359 if ( side.IsForced( sideNodeIndex ))
4361 // the node is already moved to another enforced point
4362 isNodeEnforced = quad->isEqual( myForcedPnts[ iFP ], i, j );
4365 // make a node of a side forced
4366 vector<UVPtStruct>& points = (vector<UVPtStruct>&) side.GetUVPtStruct();
4367 points[ sideNodeIndex ].u = myForcedPnts[ iFP ].U();
4368 points[ sideNodeIndex ].v = myForcedPnts[ iFP ].V();
4370 updateSideUV( side, sideNodeIndex, quadsBySide );
4372 // update adjacent sides
4373 set< StdMeshers_FaceSidePtr > updatedSides;
4374 updatedSides.insert( side );
4375 for ( size_t i = 0; i < side.contacts.size(); ++i )
4376 if ( side.contacts[i].point == sideNodeIndex )
4378 const vector< FaceQuadStruct::Ptr >& adjQuads =
4379 quadsBySide[ *side.contacts[i].other_side ];
4380 if ( adjQuads.size() > 1 &&
4381 updatedSides.insert( * side.contacts[i].other_side ).second )
4383 updateSideUV( *side.contacts[i].other_side,
4384 side.contacts[i].other_point,
4387 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4389 const vector< FaceQuadStruct::Ptr >& adjQuads = quadsBySide[ side ];
4390 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4392 isNodeEnforced = true;
4394 else // ------------------ node is inside the quad
4398 // make a new side passing through IJ node and split the quad
4399 int indForced, iNewSide;
4400 if ( quad->iSize < quad->jSize ) // split vertically
4402 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/true );
4404 iNewSide = splitQuad( quad, i, 0 );
4408 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/false );
4410 iNewSide = splitQuad( quad, 0, j );
4412 FaceQuadStruct::Ptr newQuad = myQuadList.back();
4413 FaceQuadStruct::Side& newSide = newQuad->side[ iNewSide ];
4415 newSide.forced_nodes.insert( indForced );
4416 quad->side[( iNewSide+2 ) % 4 ].forced_nodes.insert( indForced );
4418 quadsBySide[ newSide ].push_back( quad );
4419 quadsBySide[ newQuad->side[0] ].push_back( newQuad );
4420 quadsBySide[ newQuad->side[1] ].push_back( newQuad );
4421 quadsBySide[ newQuad->side[2] ].push_back( newQuad );
4422 quadsBySide[ newQuad->side[3] ].push_back( newQuad );
4424 isNodeEnforced = true;
4426 } // end of "node is inside the quad"
4428 } // loop on nodes of the cell
4430 // remove out-of-date uv grid of changedQuads
4431 set< FaceQuadStruct::Ptr >::iterator qIt = changedQuads.begin();
4432 for ( ; qIt != changedQuads.end(); ++qIt )
4433 (*qIt)->uv_grid.clear();
4435 if ( isNodeEnforced )
4440 if ( !isNodeEnforced )
4442 if ( !myForcedPnts[ iFP ].vertex.IsNull() )
4443 return error(TComm("Unable to move any node to vertex #")
4444 <<myHelper->GetMeshDS()->ShapeToIndex( myForcedPnts[ iFP ].vertex ));
4446 return error(TComm("Unable to move any node to point ( ")
4447 << myForcedPnts[iFP].xyz.X() << ", "
4448 << myForcedPnts[iFP].xyz.Y() << ", "
4449 << myForcedPnts[iFP].xyz.Z() << " )");
4452 } // loop on enforced points
4454 // Compute nodes on all sides, where not yet present
4456 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4458 FaceQuadStruct::Ptr quad = *quadIt;
4459 for ( int iSide = 0; iSide < 4; ++iSide )
4461 FaceQuadStruct::Side & side = quad->side[ iSide ];
4462 if ( side.nbNodeOut > 0 )
4463 continue; // emulated side
4464 vector< FaceQuadStruct::Ptr >& quadVec = quadsBySide[ side ];
4465 if ( quadVec.size() <= 1 )
4466 continue; // outer side
4468 bool missedNodesOnSide = false;
4469 const vector<UVPtStruct>& points = side.grid->GetUVPtStruct();
4470 for ( size_t iC = 0; iC < side.contacts.size(); ++iC )
4472 const vector<UVPtStruct>& oGrid = side.contacts[iC].other_side->grid->GetUVPtStruct();
4473 const UVPtStruct& uvPt = points[ side.contacts[iC].point ];
4474 if ( side.contacts[iC].other_point >= oGrid.size() ||
4475 side.contacts[iC].point >= points.size() )
4476 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::addEnforcedNodes(): wrong contact" );
4477 if ( oGrid[ side.contacts[iC].other_point ].node )
4478 (( UVPtStruct& ) uvPt).node = oGrid[ side.contacts[iC].other_point ].node;
4480 for ( size_t iP = 0; iP < points.size(); ++iP )
4481 if ( !points[ iP ].node )
4483 UVPtStruct& uvPnt = ( UVPtStruct& ) points[ iP ];
4484 gp_Pnt P = surf->Value( uvPnt.u, uvPnt.v );
4485 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
4486 meshDS->SetNodeOnFace( uvPnt.node, myHelper->GetSubShapeID(), uvPnt.u, uvPnt.v );
4487 missedNodesOnSide = true;
4489 if ( missedNodesOnSide )
4491 // clear uv_grid where nodes are missing
4492 for ( size_t iQ = 0; iQ < quadVec.size(); ++iQ )
4493 quadVec[ iQ ]->uv_grid.clear();
4501 //================================================================================
4503 * \brief Splits a quad at I or J. Returns an index of a new side in the new quad
4505 //================================================================================
4507 int StdMeshers_Quadrangle_2D::splitQuad(FaceQuadStruct::Ptr quad, int I, int J)
4509 FaceQuadStruct* newQuad = new FaceQuadStruct( quad->face );
4510 myQuadList.push_back( FaceQuadStruct::Ptr( newQuad ));
4512 vector<UVPtStruct> points;
4515 points.reserve( quad->jSize );
4516 for ( int jP = 0; jP < quad->jSize; ++jP )
4517 points.push_back( quad->UVPt( I, jP ));
4519 newQuad->side.resize( 4 );
4520 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
4521 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
4522 newQuad->side[ QUAD_TOP_SIDE ] = quad->side[ QUAD_TOP_SIDE ];
4523 newQuad->side[ QUAD_LEFT_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
4525 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_LEFT_SIDE ];
4526 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_RIGHT_SIDE ];
4528 quad->side[ QUAD_RIGHT_SIDE ] = newSide;
4530 int iBot = quad->side[ QUAD_BOTTOM_SIDE ].ToSideIndex( I );
4531 int iTop = quad->side[ QUAD_TOP_SIDE ].ToSideIndex( I );
4533 newSide.AddContact ( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
4534 newSide2.AddContact( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
4535 newSide.AddContact ( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
4536 newSide2.AddContact( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
4537 // cout << "Contact: L " << &newSide << " "<< newSide.NbPoints()
4538 // << " R " << &newSide2 << " "<< newSide2.NbPoints()
4539 // << " B " << &quad->side[ QUAD_BOTTOM_SIDE ] << " "<< quad->side[ QUAD_BOTTOM_SIDE].NbPoints()
4540 // << " T " << &quad->side[ QUAD_TOP_SIDE ] << " "<< quad->side[ QUAD_TOP_SIDE].NbPoints()<< endl;
4542 newQuad->side[ QUAD_BOTTOM_SIDE ].from = iBot;
4543 newQuad->side[ QUAD_TOP_SIDE ].from = iTop;
4544 newQuad->name = ( TComm("Right of I=") << I );
4546 quad->side[ QUAD_BOTTOM_SIDE ].to = iBot + 1;
4547 quad->side[ QUAD_TOP_SIDE ].to = iTop + 1;
4548 quad->uv_grid.clear();
4550 return QUAD_LEFT_SIDE;
4552 else if ( J > 0 ) //// split horizontally, a new quad is below an old one
4554 points.reserve( quad->iSize );
4555 for ( int iP = 0; iP < quad->iSize; ++iP )
4556 points.push_back( quad->UVPt( iP, J ));
4558 newQuad->side.resize( 4 );
4559 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
4560 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
4561 newQuad->side[ QUAD_TOP_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
4562 newQuad->side[ QUAD_LEFT_SIDE ] = quad->side[ QUAD_LEFT_SIDE ];
4564 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_TOP_SIDE ];
4565 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_BOTTOM_SIDE ];
4567 quad->side[ QUAD_BOTTOM_SIDE ] = newSide;
4569 int iLft = quad->side[ QUAD_LEFT_SIDE ].ToSideIndex( J );
4570 int iRgt = quad->side[ QUAD_RIGHT_SIDE ].ToSideIndex( J );
4572 newSide.AddContact ( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
4573 newSide2.AddContact( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
4574 newSide.AddContact ( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
4575 newSide2.AddContact( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
4576 // cout << "Contact: T " << &newSide << " "<< newSide.NbPoints()
4577 // << " B " << &newSide2 << " "<< newSide2.NbPoints()
4578 // << " L " << &quad->side[ QUAD_LEFT_SIDE ] << " "<< quad->side[ QUAD_LEFT_SIDE].NbPoints()
4579 // << " R " << &quad->side[ QUAD_RIGHT_SIDE ] << " "<< quad->side[ QUAD_RIGHT_SIDE].NbPoints()<< endl;
4581 newQuad->side[ QUAD_RIGHT_SIDE ].to = iRgt+1;
4582 newQuad->side[ QUAD_LEFT_SIDE ].to = iLft+1;
4583 newQuad->name = ( TComm("Below J=") << J );
4585 quad->side[ QUAD_RIGHT_SIDE ].from = iRgt;
4586 quad->side[ QUAD_LEFT_SIDE ].from = iLft;
4587 quad->uv_grid.clear();
4589 return QUAD_TOP_SIDE;
4593 //================================================================================
4595 * \brief Updates UV of a side after moving its node
4597 //================================================================================
4599 void StdMeshers_Quadrangle_2D::updateSideUV( FaceQuadStruct::Side& side,
4601 const TQuadsBySide& quadsBySide,
4606 side.forced_nodes.insert( iForced );
4608 // update parts of the side before and after iForced
4610 set<int>::iterator iIt = side.forced_nodes.upper_bound( iForced );
4611 int iEnd = Min( side.NbPoints()-1, ( iIt == side.forced_nodes.end() ) ? int(1e7) : *iIt );
4612 if ( iForced + 1 < iEnd )
4613 updateSideUV( side, iForced, quadsBySide, &iEnd );
4615 iIt = side.forced_nodes.lower_bound( iForced );
4616 int iBeg = Max( 0, ( iIt == side.forced_nodes.begin() ) ? 0 : *--iIt );
4617 if ( iForced - 1 > iBeg )
4618 updateSideUV( side, iForced, quadsBySide, &iBeg );
4623 const int iFrom = Min ( iForced, *iNext );
4624 const int iTo = Max ( iForced, *iNext ) + 1;
4625 const int sideSize = iTo - iFrom;
4627 vector<UVPtStruct> points[4]; // side points of a temporary quad
4629 // from the quads get grid points adjacent to the side
4630 // to make two sides of a temporary quad
4631 vector< FaceQuadStruct::Ptr > quads = quadsBySide.find( side )->second; // copy!
4632 for ( int is2nd = 0; is2nd < 2; ++is2nd )
4634 points[ is2nd ].reserve( sideSize );
4636 while ( points[is2nd].size() < sideSize )
4638 int iCur = iFrom + points[is2nd].size() - int( !points[is2nd].empty() );
4640 // look for a quad adjacent to iCur-th point of the side
4641 for ( size_t iQ = 0; iQ < quads.size(); ++iQ )
4643 FaceQuadStruct::Ptr q = quads[ iQ ];
4647 for ( iS = 0; iS < q->side.size(); ++iS )
4648 if ( side.grid == q->side[ iS ].grid )
4651 if ( !q->side[ iS ].IsReversed() )
4652 isOut = ( q->side[ iS ].from > iCur || q->side[ iS ].to-1 <= iCur );
4654 isOut = ( q->side[ iS ].to >= iCur || q->side[ iS ].from <= iCur );
4657 if ( !setNormalizedGrid( q ))
4660 // found - copy points
4662 if ( iS % 2 ) // right or left
4664 i = ( iS == QUAD_LEFT_SIDE ) ? 1 : q->iSize-2;
4665 j = q->side[ iS ].ToQuadIndex( iCur );
4667 dj = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
4668 nb = ( q->side[ iS ].IsReversed() ) ? j+1 : q->jSize-j;
4670 else // bottom or top
4672 i = q->side[ iS ].ToQuadIndex( iCur );
4673 j = ( iS == QUAD_BOTTOM_SIDE ) ? 1 : q->jSize-2;
4674 di = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
4676 nb = ( q->side[ iS ].IsReversed() ) ? i+1 : q->iSize-i;
4678 if ( !points[is2nd].empty() )
4680 gp_UV lastUV = points[is2nd].back().UV();
4681 gp_UV quadUV = q->UVPt( i, j ).UV();
4682 if ( ( lastUV - quadUV ).SquareModulus() > 1e-10 )
4683 continue; // quad is on the other side of the side
4684 i += di; j += dj; --nb;
4686 for ( ; nb > 0 ; --nb )
4688 points[ is2nd ].push_back( q->UVPt( i, j ));
4689 if ( points[is2nd].size() >= sideSize )
4693 quads[ iQ ].reset(); // not to use this quad anymore
4695 if ( points[is2nd].size() >= sideSize )
4699 if ( nbLoops++ > quads.size() )
4700 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::updateSideUV() bug: infinite loop" );
4702 } // while ( points[is2nd].size() < sideSize )
4703 } // two loops to fill points[0] and points[1]
4705 // points for other pair of opposite sides of the temporary quad
4707 enum { L,R,B,T }; // side index of points[]
4709 points[B].push_back( points[L].front() );
4710 points[B].push_back( side.GetUVPtStruct()[ iFrom ]);
4711 points[B].push_back( points[R].front() );
4713 points[T].push_back( points[L].back() );
4714 points[T].push_back( side.GetUVPtStruct()[ iTo-1 ]);
4715 points[T].push_back( points[R].back() );
4717 // make the temporary quad
4718 FaceQuadStruct::Ptr tmpQuad
4719 ( new FaceQuadStruct( TopoDS::Face( myHelper->GetSubShape() ), "tmpQuad"));
4720 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[B] )); // bottom
4721 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[R] )); // right
4722 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[T] ));
4723 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[L] ));
4725 // compute new UV of the side
4726 setNormalizedGrid( tmpQuad );
4727 gp_UV uv = tmpQuad->UVPt(1,0).UV();
4728 tmpQuad->updateUV( uv, 1,0, /*isVertical=*/true );
4730 // update UV of the side
4731 vector<UVPtStruct>& sidePoints = (vector<UVPtStruct>&) side.GetUVPtStruct();
4732 for ( int i = iFrom; i < iTo; ++i )
4734 const uvPtStruct& uvPt = tmpQuad->UVPt( 1, i-iFrom );
4735 sidePoints[ i ].u = uvPt.u;
4736 sidePoints[ i ].v = uvPt.v;
4740 //================================================================================
4742 * \brief Finds indices of a grid quad enclosing the given enforced UV
4744 //================================================================================
4746 bool FaceQuadStruct::findCell( const gp_XY& UV, int & I, int & J )
4748 // setNormalizedGrid() must be called before!
4749 if ( uv_box.IsOut( UV ))
4752 // find an approximate position
4753 double x = 0.5, y = 0.5;
4754 gp_XY t0 = UVPt( iSize - 1, 0 ).UV();
4755 gp_XY t1 = UVPt( 0, jSize - 1 ).UV();
4756 gp_XY t2 = UVPt( 0, 0 ).UV();
4757 SMESH_MeshAlgos::GetBarycentricCoords( UV, t0, t1, t2, x, y );
4758 x = Min( 1., Max( 0., x ));
4759 y = Min( 1., Max( 0., y ));
4761 // precise the position
4762 normPa2IJ( x,y, I,J );
4763 if ( !isNear( UV, I,J ))
4765 // look for the most close IJ by traversing uv_grid in the middle
4766 double dist2, minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
4767 for ( int isU = 0; isU < 2; ++isU )
4769 int ind1 = isU ? 0 : iSize / 2;
4770 int ind2 = isU ? jSize / 2 : 0;
4771 int di1 = isU ? Max( 2, iSize / 20 ) : 0;
4772 int di2 = isU ? 0 : Max( 2, jSize / 20 );
4773 int i,nb = isU ? iSize / di1 : jSize / di2;
4774 for ( i = 0; i < nb; ++i, ind1 += di1, ind2 += di2 )
4775 if (( dist2 = ( UV - UVPt( ind1,ind2 ).UV() ).SquareModulus() ) < minDist2 )
4779 if ( isNear( UV, I,J ))
4781 minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
4784 if ( !isNear( UV, I,J, Max( iSize, jSize ) /2 ))
4790 //================================================================================
4792 * \brief Find indices (i,j) of a point in uv_grid by normalized parameters (x,y)
4794 //================================================================================
4796 void FaceQuadStruct::normPa2IJ(double X, double Y, int & I, int & J )
4799 I = Min( int ( iSize * X ), iSize - 2 );
4800 J = Min( int ( jSize * Y ), jSize - 2 );
4806 while ( X <= UVPt( I,J ).x && I != 0 )
4808 while ( X > UVPt( I+1,J ).x && I+2 < iSize )
4810 while ( Y <= UVPt( I,J ).y && J != 0 )
4812 while ( Y > UVPt( I,J+1 ).y && J+2 < jSize )
4814 } while ( oldI != I || oldJ != J );
4817 //================================================================================
4819 * \brief Looks for UV in quads around a given (I,J) and precise (I,J)
4821 //================================================================================
4823 bool FaceQuadStruct::isNear( const gp_XY& UV, int & I, int & J, int nbLoops )
4825 if ( I+1 >= iSize ) I = iSize - 2;
4826 if ( J+1 >= jSize ) J = jSize - 2;
4829 gp_XY uvI, uvJ, uv0, uv1;
4830 for ( int iLoop = 0; iLoop < nbLoops; ++iLoop )
4832 int oldI = I, oldJ = J;
4834 uvI = UVPt( I+1, J ).UV();
4835 uvJ = UVPt( I, J+1 ).UV();
4836 uv0 = UVPt( I, J ).UV();
4837 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
4838 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
4841 if ( I > 0 && bcI < 0. ) --I;
4842 if ( I+2 < iSize && bcI > 1. ) ++I;
4843 if ( J > 0 && bcJ < 0. ) --J;
4844 if ( J+2 < jSize && bcJ > 1. ) ++J;
4846 uv1 = UVPt( I+1,J+1).UV();
4847 if ( I != oldI || J != oldJ )
4849 uvI = UVPt( I+1, J ).UV();
4850 uvJ = UVPt( I, J+1 ).UV();
4852 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
4853 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
4856 if ( I > 0 && bcI > 1. ) --I;
4857 if ( I+2 < iSize && bcI < 0. ) ++I;
4858 if ( J > 0 && bcJ > 1. ) --J;
4859 if ( J+2 < jSize && bcJ < 0. ) ++J;
4861 if ( I == oldI && J == oldJ )
4864 if ( iLoop+1 == nbLoops )
4866 uvI = UVPt( I+1, J ).UV();
4867 uvJ = UVPt( I, J+1 ).UV();
4868 uv0 = UVPt( I, J ).UV();
4869 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
4870 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
4873 uv1 = UVPt( I+1,J+1).UV();
4874 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
4875 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
4882 //================================================================================
4884 * \brief Checks if a given UV is equal to a given grid point
4886 //================================================================================
4888 bool FaceQuadStruct::isEqual( const gp_XY& UV, int I, int J )
4890 TopLoc_Location loc;
4891 Handle(Geom_Surface) surf = BRep_Tool::Surface( face, loc );
4892 gp_Pnt p1 = surf->Value( UV.X(), UV.Y() );
4893 gp_Pnt p2 = surf->Value( UVPt( I,J ).u, UVPt( I,J ).v );
4895 double dist2 = 1e100;
4896 for ( int di = -1; di < 2; di += 2 )
4899 if ( i < 0 || i+1 >= iSize ) continue;
4900 for ( int dj = -1; dj < 2; dj += 2 )
4903 if ( j < 0 || j+1 >= jSize ) continue;
4906 p2.SquareDistance( surf->Value( UVPt( i,j ).u, UVPt( i,j ).v )));
4909 double tol2 = dist2 / 1000.;
4910 return p1.SquareDistance( p2 ) < tol2;
4913 //================================================================================
4915 * \brief Recompute UV of grid points around a moved point in one direction
4917 //================================================================================
4919 void FaceQuadStruct::updateUV( const gp_XY& UV, int I, int J, bool isVertical )
4921 UVPt( I, J ).u = UV.X();
4922 UVPt( I, J ).v = UV.Y();
4927 if ( J+1 < jSize-1 )
4929 gp_UV a0 = UVPt( 0, J ).UV();
4930 gp_UV a1 = UVPt( iSize-1, J ).UV();
4931 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
4932 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
4934 gp_UV p0 = UVPt( I, J ).UV();
4935 gp_UV p2 = UVPt( I, jSize-1 ).UV();
4936 const double y0 = UVPt( I, J ).y, dy = 1. - y0;
4937 for (int j = J+1; j < jSize-1; j++)
4939 gp_UV p1 = UVPt( iSize-1, j ).UV();
4940 gp_UV p3 = UVPt( 0, j ).UV();
4942 UVPtStruct& uvPt = UVPt( I, j );
4943 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
4951 gp_UV a0 = UVPt( 0, 0 ).UV();
4952 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
4953 gp_UV a2 = UVPt( iSize-1, J ).UV();
4954 gp_UV a3 = UVPt( 0, J ).UV();
4956 gp_UV p0 = UVPt( I, 0 ).UV();
4957 gp_UV p2 = UVPt( I, J ).UV();
4958 const double y0 = 0., dy = UVPt( I, J ).y - y0;
4959 for (int j = 1; j < J; j++)
4961 gp_UV p1 = UVPt( iSize-1, j ).UV();
4962 gp_UV p3 = UVPt( 0, j ).UV();
4964 UVPtStruct& uvPt = UVPt( I, j );
4965 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
4971 else // horizontally
4976 gp_UV a0 = UVPt( 0, 0 ).UV();
4977 gp_UV a1 = UVPt( I, 0 ).UV();
4978 gp_UV a2 = UVPt( I, jSize-1 ).UV();
4979 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
4981 gp_UV p1 = UVPt( I, J ).UV();
4982 gp_UV p3 = UVPt( 0, J ).UV();
4983 const double x0 = 0., dx = UVPt( I, J ).x - x0;
4984 for (int i = 1; i < I; i++)
4986 gp_UV p0 = UVPt( i, 0 ).UV();
4987 gp_UV p2 = UVPt( i, jSize-1 ).UV();
4989 UVPtStruct& uvPt = UVPt( i, J );
4990 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
4996 if ( I+1 < iSize-1 )
4998 gp_UV a0 = UVPt( I, 0 ).UV();
4999 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5000 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5001 gp_UV a3 = UVPt( I, jSize-1 ).UV();
5003 gp_UV p1 = UVPt( iSize-1, J ).UV();
5004 gp_UV p3 = UVPt( I, J ).UV();
5005 const double x0 = UVPt( I, J ).x, dx = 1. - x0;
5006 for (int i = I+1; i < iSize-1; i++)
5008 gp_UV p0 = UVPt( i, 0 ).UV();
5009 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5011 UVPtStruct& uvPt = UVPt( i, J );
5012 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5020 //================================================================================
5022 * \brief Side copying
5024 //================================================================================
5026 FaceQuadStruct::Side& FaceQuadStruct::Side::operator=(const Side& otherSide)
5028 grid = otherSide.grid;
5029 from = otherSide.from;
5032 forced_nodes = otherSide.forced_nodes;
5033 contacts = otherSide.contacts;
5034 nbNodeOut = otherSide.nbNodeOut;
5036 for ( size_t iC = 0; iC < contacts.size(); ++iC )
5038 FaceQuadStruct::Side* oSide = contacts[iC].other_side;
5039 for ( size_t iOC = 0; iOC < oSide->contacts.size(); ++iOC )
5040 if ( oSide->contacts[iOC].other_side == & otherSide )
5042 // cout << "SHIFT old " << &otherSide << " " << otherSide.NbPoints()
5043 // << " -> new " << this << " " << this->NbPoints() << endl;
5044 oSide->contacts[iOC].other_side = this;
5049 //================================================================================
5051 * \brief Converts node index of a quad to node index of this side
5053 //================================================================================
5055 int FaceQuadStruct::Side::ToSideIndex( int quadNodeIndex ) const
5057 return from + di * quadNodeIndex;
5060 //================================================================================
5062 * \brief Converts node index of this side to node index of a quad
5064 //================================================================================
5066 int FaceQuadStruct::Side::ToQuadIndex( int sideNodeIndex ) const
5068 return ( sideNodeIndex - from ) * di;
5071 //================================================================================
5073 * \brief Reverse the side
5075 //================================================================================
5077 bool FaceQuadStruct::Side::Reverse(bool keepGrid)
5085 std::swap( from, to );
5095 //================================================================================
5097 * \brief Checks if a node is enforced
5098 * \param [in] nodeIndex - an index of a node in a size
5099 * \return bool - \c true if the node is forced
5101 //================================================================================
5103 bool FaceQuadStruct::Side::IsForced( int nodeIndex ) const
5105 if ( nodeIndex < 0 || nodeIndex >= grid->NbPoints() )
5106 throw SALOME_Exception( " FaceQuadStruct::Side::IsForced(): wrong index" );
5108 if ( forced_nodes.count( nodeIndex ) )
5111 for ( size_t i = 0; i < this->contacts.size(); ++i )
5112 if ( contacts[ i ].point == nodeIndex &&
5113 contacts[ i ].other_side->forced_nodes.count( contacts[ i ].other_point ))
5119 //================================================================================
5121 * \brief Sets up a contact between this and another side
5123 //================================================================================
5125 void FaceQuadStruct::Side::AddContact( int ip, Side* side, int iop )
5127 if ( ip >= GetUVPtStruct().size() ||
5128 iop >= side->GetUVPtStruct().size() )
5129 throw SALOME_Exception( "FaceQuadStruct::Side::AddContact(): wrong point" );
5131 contacts.resize( contacts.size() + 1 );
5132 Contact& c = contacts.back();
5134 c.other_side = side;
5135 c.other_point = iop;
5138 side->contacts.resize( side->contacts.size() + 1 );
5139 Contact& c = side->contacts.back();
5141 c.other_side = this;
5146 //================================================================================
5148 * \brief Returns a normalized parameter of a point indexed within a quadrangle
5150 //================================================================================
5152 double FaceQuadStruct::Side::Param( int i ) const
5154 const vector<UVPtStruct>& points = GetUVPtStruct();
5155 return (( points[ from + i * di ].normParam - points[ from ].normParam ) /
5156 ( points[ to - 1 * di ].normParam - points[ from ].normParam ));
5159 //================================================================================
5161 * \brief Returns UV by a parameter normalized within a quadrangle
5163 //================================================================================
5165 gp_XY FaceQuadStruct::Side::Value2d( double x ) const
5167 const vector<UVPtStruct>& points = GetUVPtStruct();
5168 double u = ( points[ from ].normParam +
5169 x * ( points[ to-di ].normParam - points[ from ].normParam ));
5170 return grid->Value2d( u ).XY();
5173 //================================================================================
5175 * \brief Returns side length
5177 //================================================================================
5179 double FaceQuadStruct::Side::Length(int theFrom, int theTo) const
5181 if ( IsReversed() != ( theTo < theFrom ))
5182 std::swap( theTo, theFrom );
5184 const vector<UVPtStruct>& points = GetUVPtStruct();
5186 if ( theFrom == theTo && theTo == -1 )
5187 r = Abs( First().normParam -
5188 Last ().normParam );
5189 else if ( IsReversed() )
5190 r = Abs( points[ Max( to, theTo+1 ) ].normParam -
5191 points[ Min( from, theFrom ) ].normParam );
5193 r = Abs( points[ Min( to, theTo-1 ) ].normParam -
5194 points[ Max( from, theFrom ) ].normParam );
5195 return r * grid->Length();