1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 <BRepBndLib.hxx>
46 #include <BRepClass_FaceClassifier.hxx>
47 #include <BRep_Tool.hxx>
48 #include <Bnd_Box.hxx>
49 #include <GeomAPI_ProjectPointOnSurf.hxx>
50 #include <Geom_Surface.hxx>
51 #include <NCollection_DefineArray2.hxx>
52 #include <Precision.hxx>
53 #include <Quantity_Parameter.hxx>
54 #include <TColStd_SequenceOfInteger.hxx>
55 #include <TColStd_SequenceOfReal.hxx>
56 #include <TColgp_SequenceOfXY.hxx>
58 #include <TopExp_Explorer.hxx>
59 #include <TopTools_DataMapOfShapeReal.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
61 #include <TopTools_MapOfShape.hxx>
64 #include "utilities.h"
65 #include "Utils_ExceptHandlers.hxx"
67 #ifndef StdMeshers_Array2OfNode_HeaderFile
68 #define StdMeshers_Array2OfNode_HeaderFile
69 typedef const SMDS_MeshNode* SMDS_MeshNodePtr;
70 typedef NCollection_Array2<SMDS_MeshNodePtr> StdMeshers_Array2OfNode;
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),
93 myQuadType(QUAD_STANDARD),
96 MESSAGE("StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D");
97 _name = "Quadrangle_2D";
98 _shapeType = (1 << TopAbs_FACE);
99 _compatibleHypothesis.push_back("QuadrangleParams");
100 _compatibleHypothesis.push_back("QuadranglePreference");
101 _compatibleHypothesis.push_back("TrianglePreference");
102 _compatibleHypothesis.push_back("ViscousLayers2D");
105 //=============================================================================
109 //=============================================================================
111 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
113 MESSAGE("StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D");
116 //=============================================================================
120 //=============================================================================
122 bool StdMeshers_Quadrangle_2D::CheckHypothesis
124 const TopoDS_Shape& aShape,
125 SMESH_Hypothesis::Hypothesis_Status& aStatus)
128 myQuadType = QUAD_STANDARD;
129 myQuadranglePreference = false;
130 myTrianglePreference = false;
131 myHelper = (SMESH_MesherHelper*)NULL;
136 aStatus = SMESH_Hypothesis::HYP_OK;
138 const list <const SMESHDS_Hypothesis * >& hyps =
139 GetUsedHypothesis(aMesh, aShape, false);
140 const SMESHDS_Hypothesis * aHyp = 0;
142 bool isFirstParams = true;
144 // First assigned hypothesis (if any) is processed now
145 if (hyps.size() > 0) {
147 if (strcmp("QuadrangleParams", aHyp->GetName()) == 0)
149 myParams = (const StdMeshers_QuadrangleParams*)aHyp;
150 myTriaVertexID = myParams->GetTriaVertex();
151 myQuadType = myParams->GetQuadType();
152 if (myQuadType == QUAD_QUADRANGLE_PREF ||
153 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
154 myQuadranglePreference = true;
155 else if (myQuadType == QUAD_TRIANGLE_PREF)
156 myTrianglePreference = true;
158 else if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
159 isFirstParams = false;
160 myQuadranglePreference = true;
162 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
163 isFirstParams = false;
164 myTrianglePreference = true;
167 isFirstParams = false;
171 // Second(last) assigned hypothesis (if any) is processed now
172 if (hyps.size() > 1) {
175 if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
176 myQuadranglePreference = true;
177 myTrianglePreference = false;
178 myQuadType = QUAD_STANDARD;
180 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
181 myQuadranglePreference = false;
182 myTrianglePreference = true;
183 myQuadType = QUAD_STANDARD;
187 const StdMeshers_QuadrangleParams* aHyp2 =
188 (const StdMeshers_QuadrangleParams*)aHyp;
189 myTriaVertexID = aHyp2->GetTriaVertex();
191 if (!myQuadranglePreference && !myTrianglePreference) { // priority of hypos
192 myQuadType = aHyp2->GetQuadType();
193 if (myQuadType == QUAD_QUADRANGLE_PREF ||
194 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
195 myQuadranglePreference = true;
196 else if (myQuadType == QUAD_TRIANGLE_PREF)
197 myTrianglePreference = true;
202 error( StdMeshers_ViscousLayers2D::CheckHypothesis( aMesh, aShape, aStatus ));
204 return aStatus == HYP_OK;
207 //=============================================================================
211 //=============================================================================
213 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
214 const TopoDS_Shape& aShape)
216 const TopoDS_Face& F = TopoDS::Face(aShape);
217 aMesh.GetSubMesh( F );
219 // do not initialize my fields before this as StdMeshers_ViscousLayers2D
220 // can call Compute() recursively
221 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
225 myProxyMesh = proxyMesh;
227 SMESH_MesherHelper helper (aMesh);
230 _quadraticMesh = myHelper->IsQuadraticSubMesh(aShape);
231 myNeedSmooth = false;
234 FaceQuadStruct::Ptr quad = CheckNbEdges( aMesh, F, /*considerMesh=*/true );
238 myQuadList.push_back( quad );
240 if ( !getEnforcedUV() )
243 updateDegenUV( quad );
245 int n1 = quad->side[0].NbPoints();
246 int n2 = quad->side[1].NbPoints();
247 int n3 = quad->side[2].NbPoints();
248 int n4 = quad->side[3].NbPoints();
250 enum { NOT_COMPUTED = -1, COMPUTE_FAILED = 0, COMPUTE_OK = 1 };
251 int res = NOT_COMPUTED;
252 if (myQuadranglePreference)
254 int nfull = n1+n2+n3+n4;
255 if ((nfull % 2) == 0 && ((n1 != n3) || (n2 != n4)))
257 // special path genarating only quandrangle faces
258 res = computeQuadPref( aMesh, F, quad );
261 else if (myQuadType == QUAD_REDUCED)
265 int n13tmp = n13/2; n13tmp = n13tmp*2;
266 int n24tmp = n24/2; n24tmp = n24tmp*2;
267 if ((n1 == n3 && n2 != n4 && n24tmp == n24) ||
268 (n2 == n4 && n1 != n3 && n13tmp == n13))
270 res = computeReduced( aMesh, F, quad );
274 if ( n1 != n3 && n2 != n4 )
275 error( COMPERR_WARNING,
276 "To use 'Reduced' transition, "
277 "two opposite sides should have same number of segments, "
278 "but actual number of segments is different on all sides. "
279 "'Standard' transion has been used.");
280 else if ( ! ( n1 == n3 && n2 == n4 ))
281 error( COMPERR_WARNING,
282 "To use 'Reduced' transition, "
283 "two opposite sides should have an even difference in number of segments. "
284 "'Standard' transion has been used.");
288 if ( res == NOT_COMPUTED )
290 if ( n1 != n3 || n2 != n4 )
291 res = computeTriangles( aMesh, F, quad );
293 res = computeQuadDominant( aMesh, F );
296 if ( res == COMPUTE_OK && myNeedSmooth )
299 if ( res == COMPUTE_OK )
302 return ( res == COMPUTE_OK );
305 //================================================================================
307 * \brief Compute quadrangles and triangles on the quad
309 //================================================================================
311 bool StdMeshers_Quadrangle_2D::computeTriangles(SMESH_Mesh& aMesh,
312 const TopoDS_Face& aFace,
313 FaceQuadStruct::Ptr quad)
315 int nb = quad->side[0].grid->NbPoints();
316 int nr = quad->side[1].grid->NbPoints();
317 int nt = quad->side[2].grid->NbPoints();
318 int nl = quad->side[3].grid->NbPoints();
320 // rotate the quad to have nbNodeOut sides on TOP [and LEFT]
322 quad->shift( nl > nr ? 3 : 2, true );
324 quad->shift( 1, true );
326 quad->shift( nt > nb ? 0 : 3, true );
328 if ( !setNormalizedGrid( quad ))
331 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
333 splitQuad( quad, 0, quad->jSize-2 );
335 if ( quad->nbNodeOut( QUAD_BOTTOM_SIDE )) // this should not happen
337 splitQuad( quad, 0, 1 );
339 FaceQuadStruct::Ptr newQuad = myQuadList.back();
340 if ( quad != newQuad ) // split done
342 { // update left side limit till where to make triangles
343 FaceQuadStruct::Ptr botQuad = // a bottom part
344 ( quad->side[ QUAD_LEFT_SIDE ].from == 0 ) ? quad : newQuad;
345 if ( botQuad->nbNodeOut( QUAD_LEFT_SIDE ) > 0 )
346 botQuad->side[ QUAD_LEFT_SIDE ].to += botQuad->nbNodeOut( QUAD_LEFT_SIDE );
347 else if ( botQuad->nbNodeOut( QUAD_RIGHT_SIDE ) > 0 )
348 botQuad->side[ QUAD_RIGHT_SIDE ].to += botQuad->nbNodeOut( QUAD_RIGHT_SIDE );
350 // make quad be a greatest one
351 if ( quad->side[ QUAD_LEFT_SIDE ].NbPoints() == 2 ||
352 quad->side[ QUAD_RIGHT_SIDE ].NbPoints() == 2 )
354 if ( !setNormalizedGrid( quad ))
358 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
360 splitQuad( quad, quad->iSize-2, 0 );
362 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
364 splitQuad( quad, 1, 0 );
366 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
368 newQuad = myQuadList.back();
369 if ( newQuad == quad ) // too narrow to split
371 // update left side limit till where to make triangles
372 quad->side[ QUAD_LEFT_SIDE ].to--;
376 FaceQuadStruct::Ptr leftQuad =
377 ( quad->side[ QUAD_BOTTOM_SIDE ].from == 0 ) ? quad : newQuad;
378 leftQuad->nbNodeOut( QUAD_TOP_SIDE ) = 0;
383 if ( ! computeQuadDominant( aMesh, aFace ))
386 // try to fix zero-area triangles near straight-angle corners
391 //================================================================================
393 * \brief Compute quadrangles and possibly triangles on all quads of myQuadList
395 //================================================================================
397 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
398 const TopoDS_Face& aFace)
400 if ( !addEnforcedNodes() )
403 std::list< FaceQuadStruct::Ptr >::iterator quad = myQuadList.begin();
404 for ( ; quad != myQuadList.end(); ++quad )
405 if ( !computeQuadDominant( aMesh, aFace, *quad ))
411 //================================================================================
413 * \brief Compute quadrangles and possibly triangles
415 //================================================================================
417 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
418 const TopoDS_Face& aFace,
419 FaceQuadStruct::Ptr quad)
421 // --- set normalized grid on unit square in parametric domain
423 if ( !setNormalizedGrid( quad ))
426 // --- create nodes on points, and create quadrangles
428 int nbhoriz = quad->iSize;
429 int nbvertic = quad->jSize;
431 // internal mesh nodes
432 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
433 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
434 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
435 for (i = 1; i < nbhoriz - 1; i++)
436 for (j = 1; j < nbvertic - 1; j++)
438 UVPtStruct& uvPnt = quad->UVPt( i, j );
439 gp_Pnt P = S->Value( uvPnt.u, uvPnt.v );
440 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
441 meshDS->SetNodeOnFace( uvPnt.node, geomFaceID, uvPnt.u, uvPnt.v );
447 // --.--.--.--.--.-- nbvertic
453 // ---.----.----.--- 0
454 // 0 > > > > > > > > nbhoriz
459 int iup = nbhoriz - 1;
460 if (quad->nbNodeOut(3)) { ilow++; } else { if (quad->nbNodeOut(1)) iup--; }
463 int jup = nbvertic - 1;
464 if (quad->nbNodeOut(0)) { jlow++; } else { if (quad->nbNodeOut(2)) jup--; }
466 // regular quadrangles
467 for (i = ilow; i < iup; i++) {
468 for (j = jlow; j < jup; j++) {
469 const SMDS_MeshNode *a, *b, *c, *d;
470 a = quad->uv_grid[ j * nbhoriz + i ].node;
471 b = quad->uv_grid[ j * nbhoriz + i + 1].node;
472 c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
473 d = quad->uv_grid[(j + 1) * nbhoriz + i ].node;
474 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
476 meshDS->SetMeshElementOnShape(face, geomFaceID);
481 // Boundary elements (must always be on an outer boundary of the FACE)
483 const vector<UVPtStruct>& uv_e0 = quad->side[0].grid->GetUVPtStruct();
484 const vector<UVPtStruct>& uv_e1 = quad->side[1].grid->GetUVPtStruct();
485 const vector<UVPtStruct>& uv_e2 = quad->side[2].grid->GetUVPtStruct();
486 const vector<UVPtStruct>& uv_e3 = quad->side[3].grid->GetUVPtStruct();
488 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
489 return error(COMPERR_BAD_INPUT_MESH);
491 double eps = Precision::Confusion();
493 int nbdown = (int) uv_e0.size();
494 int nbup = (int) uv_e2.size();
495 int nbright = (int) uv_e1.size();
496 int nbleft = (int) uv_e3.size();
498 if (quad->nbNodeOut(0) && nbvertic == 2) // this should not occure
502 // |___|___|___|___|___|___|
504 // |___|___|___|___|___|___|
506 // |___|___|___|___|___|___| __ first row of the regular grid
507 // . . . . . . . . . __ down edge nodes
509 // >->->->->->->->->->->->-> -- direction of processing
511 int g = 0; // number of last processed node in the regular grid
513 // number of last node of the down edge to be processed
514 int stop = nbdown - 1;
515 // if right edge is out, we will stop at a node, previous to the last one
516 //if (quad->nbNodeOut(1)) stop--;
517 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
518 quad->UVPt( nbhoriz-1, 1 ).node = uv_e1[1].node;
519 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
520 quad->UVPt( 0, 1 ).node = uv_e3[1].node;
522 // for each node of the down edge find nearest node
523 // in the first row of the regular grid and link them
524 for (i = 0; i < stop; i++) {
525 const SMDS_MeshNode *a, *b, *c, *d;
527 b = uv_e0[i + 1].node;
528 gp_Pnt pb (b->X(), b->Y(), b->Z());
530 // find node c in the regular grid, which will be linked with node b
533 // right bound reached, link with the rightmost node
535 c = quad->uv_grid[nbhoriz + iup].node;
538 // find in the grid node c, nearest to the b
539 double mind = RealLast();
540 for (int k = g; k <= iup; k++) {
542 const SMDS_MeshNode *nk;
543 if (k < ilow) // this can be, if left edge is out
544 nk = uv_e3[1].node; // get node from the left edge
546 nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
548 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
549 double dist = pb.Distance(pnk);
550 if (dist < mind - eps) {
560 if (near == g) { // make triangle
561 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
562 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
564 else { // make quadrangle
568 d = quad->uv_grid[nbhoriz + near - 1].node;
569 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
571 if (!myTrianglePreference){
572 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
573 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
576 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
579 // if node d is not at position g - make additional triangles
581 for (int k = near - 1; k > g; k--) {
582 c = quad->uv_grid[nbhoriz + k].node;
586 d = quad->uv_grid[nbhoriz + k - 1].node;
587 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
588 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
595 if (quad->nbNodeOut(2) && nbvertic == 2)
599 // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
601 // . . . . . . . . . __ up edge nodes
602 // ___ ___ ___ ___ ___ ___ __ first row of the regular grid
604 // |___|___|___|___|___|___|
606 // |___|___|___|___|___|___|
609 int g = nbhoriz - 1; // last processed node in the regular grid
615 if ( quad->side[3].grid->Edge(0).IsNull() ) // left side is simulated one
617 // quad divided at I but not at J, as nbvertic==nbright==2
618 stop++; // we stop at a second node
622 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
623 quad->UVPt( nbhoriz-1, 0 ).node = uv_e1[ nbright-2 ].node;
624 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
625 quad->UVPt( 0, 0 ).node = uv_e3[ nbleft-2 ].node;
627 if ( nbright > 2 ) // there was a split at J
628 quad->nbNodeOut( QUAD_LEFT_SIDE ) = 0;
630 const SMDS_MeshNode *a, *b, *c, *d;
632 // avoid creating zero-area triangles near a straight-angle corner
636 c = uv_e1[nbright-2].node;
637 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
638 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
639 if ( Abs( area ) < 1e-20 )
642 d = quad->UVPt( g, nbvertic-2 ).node;
643 if ( myTrianglePreference )
645 if ( SMDS_MeshFace* face = myHelper->AddFace(a, d, c))
646 meshDS->SetMeshElementOnShape(face, geomFaceID);
650 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
652 meshDS->SetMeshElementOnShape(face, geomFaceID);
653 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
654 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
656 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
657 "Bad quality quad created"));
658 err->myBadElements.push_back( face );
665 // for each node of the up edge find nearest node
666 // in the first row of the regular grid and link them
667 for ( ; i > stop; i--) {
669 b = uv_e2[i - 1].node;
670 gp_Pnt pb = SMESH_TNodeXYZ( b );
672 // find node c in the grid, which will be linked with node b
674 if (i == stop + 1) { // left bound reached, link with the leftmost node
675 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
678 // find node c in the grid, nearest to the b
679 double mind = RealLast();
680 for (int k = g; k >= ilow; k--) {
681 const SMDS_MeshNode *nk;
683 nk = uv_e1[nbright - 2].node;
685 nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
686 gp_Pnt pnk = SMESH_TNodeXYZ( nk );
687 double dist = pb.Distance(pnk);
688 if (dist < mind - eps) {
698 if (near == g) { // make triangle
699 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
700 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
702 else { // make quadrangle
704 d = uv_e1[nbright - 2].node;
706 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
707 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
708 if (!myTrianglePreference){
709 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
710 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
713 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
716 if (near + 1 < g) { // if d is not at g - make additional triangles
717 for (int k = near + 1; k < g; k++) {
718 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
720 d = uv_e1[nbright - 2].node;
722 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
723 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
724 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
733 // right or left boundary quadrangles
734 if (quad->nbNodeOut( QUAD_RIGHT_SIDE ) && nbhoriz == 2) // this should not occure
736 int g = 0; // last processed node in the grid
737 int stop = nbright - 1;
739 if (quad->side[ QUAD_RIGHT_SIDE ].from != i ) i++;
740 if (quad->side[ QUAD_RIGHT_SIDE ].to != stop ) stop--;
741 for ( ; i < stop; i++) {
742 const SMDS_MeshNode *a, *b, *c, *d;
744 b = uv_e1[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) { // up bondary reached
750 c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
753 double mind = RealLast();
754 for (int k = g; k <= jup; k++) {
755 const SMDS_MeshNode *nk;
757 nk = uv_e0[nbdown - 2].node;
759 nk = quad->uv_grid[nbhoriz*(k + 1) - 2].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
778 d = uv_e0[nbdown - 2].node;
780 d = quad->uv_grid[nbhoriz*near - 2].node;
781 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
783 if (!myTrianglePreference){
784 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
785 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
788 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
791 if (near - 1 > g) { // if d not is at g - make additional triangles
792 for (int k = near - 1; k > g; k--) {
793 c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
795 d = uv_e0[nbdown - 2].node;
797 d = quad->uv_grid[nbhoriz*k - 2].node;
798 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
799 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
806 if (quad->nbNodeOut(3) && nbhoriz == 2) {
807 // MESSAGE("left edge is out");
808 int g = nbvertic - 1; // last processed node in the grid
810 i = quad->side[ QUAD_LEFT_SIDE ].to-1; // nbleft - 1;
812 const SMDS_MeshNode *a, *b, *c, *d;
813 // avoid creating zero-area triangles near a straight-angle corner
817 c = quad->UVPt( 1, g ).node;
818 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
819 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
820 if ( Abs( area ) < 1e-20 )
823 d = quad->UVPt( 1, g ).node;
824 if ( myTrianglePreference )
826 if ( SMDS_MeshFace* face = myHelper->AddFace(a, d, c))
827 meshDS->SetMeshElementOnShape(face, geomFaceID);
831 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
833 meshDS->SetMeshElementOnShape(face, geomFaceID);
834 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
835 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
837 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
838 "Bad quality quad created"));
839 err->myBadElements.push_back( face );
846 for (; i > stop; i--) // loop on nodes on the left side
849 b = uv_e3[i - 1].node;
850 gp_Pnt pb (b->X(), b->Y(), b->Z());
852 // find node c in the grid, nearest to the b
854 if (i == stop + 1) { // down bondary reached
855 c = quad->uv_grid[nbhoriz*jlow + 1].node;
859 double mind = RealLast();
860 for (int k = g; k >= jlow; k--) {
861 const SMDS_MeshNode *nk;
863 nk = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
865 nk = quad->uv_grid[nbhoriz*k + 1].node;
866 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
867 double dist = pb.Distance(pnk);
868 if (dist < mind - eps) {
878 if (near == g) { // make triangle
879 SMDS_MeshFace* face = myHelper->AddFace(a, b, c);
880 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
882 else { // make quadrangle
884 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
886 d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
887 if (!myTrianglePreference) {
888 SMDS_MeshFace* face = myHelper->AddFace(a, b, c, d);
889 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
892 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
895 if (near + 1 < g) { // if d not is at g - make additional triangles
896 for (int k = near + 1; k < g; k++) {
897 c = quad->uv_grid[nbhoriz*k + 1].node;
899 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
901 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
902 SMDS_MeshFace* face = myHelper->AddFace(a, c, d);
903 if (face) meshDS->SetMeshElementOnShape(face, geomFaceID);
917 //=============================================================================
921 //=============================================================================
923 bool StdMeshers_Quadrangle_2D::Evaluate(SMESH_Mesh& aMesh,
924 const TopoDS_Shape& aFace,
925 MapShapeNbElems& aResMap)
928 aMesh.GetSubMesh(aFace);
930 std::vector<int> aNbNodes(4);
931 bool IsQuadratic = false;
932 if (!checkNbEdgesForEvaluate(aMesh, aFace, aResMap, aNbNodes, IsQuadratic)) {
933 std::vector<int> aResVec(SMDSEntity_Last);
934 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
935 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
936 aResMap.insert(std::make_pair(sm,aResVec));
937 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
938 smError.reset(new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
942 if (myQuadranglePreference) {
943 int n1 = aNbNodes[0];
944 int n2 = aNbNodes[1];
945 int n3 = aNbNodes[2];
946 int n4 = aNbNodes[3];
947 int nfull = n1+n2+n3+n4;
950 if (nfull==ntmp && ((n1!=n3) || (n2!=n4))) {
951 // special path for using only quandrangle faces
952 return evaluateQuadPref(aMesh, aFace, aNbNodes, aResMap, IsQuadratic);
957 int nbdown = aNbNodes[0];
958 int nbup = aNbNodes[2];
960 int nbright = aNbNodes[1];
961 int nbleft = aNbNodes[3];
963 int nbhoriz = Min(nbdown, nbup);
964 int nbvertic = Min(nbright, nbleft);
966 int dh = Max(nbdown, nbup) - nbhoriz;
967 int dv = Max(nbright, nbleft) - nbvertic;
974 int nbNodes = (nbhoriz-2)*(nbvertic-2);
975 //int nbFaces3 = dh + dv + kdh*(nbvertic-1)*2 + kdv*(nbhoriz-1)*2;
976 int nbFaces3 = dh + dv;
977 //if (kdh==1 && kdv==1) nbFaces3 -= 2;
978 //if (dh>0 && dv>0) nbFaces3 -= 2;
979 //int nbFaces4 = (nbhoriz-1-kdh)*(nbvertic-1-kdv);
980 int nbFaces4 = (nbhoriz-1)*(nbvertic-1);
982 std::vector<int> aVec(SMDSEntity_Last);
983 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
985 aVec[SMDSEntity_Quad_Triangle] = nbFaces3;
986 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4;
987 int nbbndedges = nbdown + nbup + nbright + nbleft -4;
988 int nbintedges = (nbFaces4*4 + nbFaces3*3 - nbbndedges) / 2;
989 aVec[SMDSEntity_Node] = nbNodes + nbintedges;
990 if (aNbNodes.size()==5) {
991 aVec[SMDSEntity_Quad_Triangle] = nbFaces3 + aNbNodes[3] -1;
992 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4 - aNbNodes[3] +1;
996 aVec[SMDSEntity_Node] = nbNodes;
997 aVec[SMDSEntity_Triangle] = nbFaces3;
998 aVec[SMDSEntity_Quadrangle] = nbFaces4;
999 if (aNbNodes.size()==5) {
1000 aVec[SMDSEntity_Triangle] = nbFaces3 + aNbNodes[3] - 1;
1001 aVec[SMDSEntity_Quadrangle] = nbFaces4 - aNbNodes[3] + 1;
1004 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
1005 aResMap.insert(std::make_pair(sm,aVec));
1010 //================================================================================
1012 * \brief Return true if the algorithm can mesh this shape
1013 * \param [in] aShape - shape to check
1014 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
1015 * else, returns OK if at least one shape is OK
1017 //================================================================================
1019 bool StdMeshers_Quadrangle_2D::IsApplicable( const TopoDS_Shape & aShape, bool toCheckAll )
1021 int nbFoundFaces = 0;
1022 for (TopExp_Explorer exp( aShape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFoundFaces )
1024 const TopoDS_Shape& aFace = exp.Current();
1025 int nbWire = SMESH_MesherHelper::Count( aFace, TopAbs_WIRE, false );
1026 if ( nbWire != 1 ) {
1027 if ( toCheckAll ) return false;
1031 int nbNoDegenEdges = 0;
1032 TopExp_Explorer eExp( aFace, TopAbs_EDGE );
1033 for ( ; eExp.More() && nbNoDegenEdges < 3; eExp.Next() ) {
1034 if ( !SMESH_Algo::isDegenerated( TopoDS::Edge( eExp.Current() )))
1037 if ( toCheckAll && nbNoDegenEdges < 3 ) return false;
1038 if ( !toCheckAll && nbNoDegenEdges >= 3 ) return true;
1040 return ( toCheckAll && nbFoundFaces != 0 );
1043 //================================================================================
1045 * \brief Return true if only two given edges meat at their common vertex
1047 //================================================================================
1049 static bool twoEdgesMeatAtVertex(const TopoDS_Edge& e1,
1050 const TopoDS_Edge& e2,
1054 if (!TopExp::CommonVertex(e1, e2, v))
1056 TopTools_ListIteratorOfListOfShape ancestIt(mesh.GetAncestors(v));
1057 for (; ancestIt.More() ; ancestIt.Next())
1058 if (ancestIt.Value().ShapeType() == TopAbs_EDGE)
1059 if (!e1.IsSame(ancestIt.Value()) && !e2.IsSame(ancestIt.Value()))
1064 //=============================================================================
1068 //=============================================================================
1070 FaceQuadStruct::Ptr StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh & aMesh,
1071 const TopoDS_Shape & aShape,
1072 const bool considerMesh)
1074 if ( !myQuadList.empty() && myQuadList.front()->face.IsSame( aShape ))
1075 return myQuadList.front();
1077 TopoDS_Face F = TopoDS::Face(aShape);
1078 if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
1079 const bool ignoreMediumNodes = _quadraticMesh;
1081 // verify 1 wire only
1082 list< TopoDS_Edge > edges;
1083 list< int > nbEdgesInWire;
1084 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1086 error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
1087 return FaceQuadStruct::Ptr();
1090 // find corner vertices of the quad
1091 vector<TopoDS_Vertex> corners;
1092 int nbDegenEdges, nbSides = getCorners( F, aMesh, edges, corners, nbDegenEdges, considerMesh );
1095 return FaceQuadStruct::Ptr();
1097 FaceQuadStruct::Ptr quad( new FaceQuadStruct );
1098 quad->side.reserve(nbEdgesInWire.front());
1101 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1102 if ( nbSides == 3 ) // 3 sides and corners[0] is a vertex with myTriaVertexID
1104 for ( int iSide = 0; iSide < 3; ++iSide )
1106 list< TopoDS_Edge > sideEdges;
1107 TopoDS_Vertex nextSideV = corners[( iSide + 1 ) % 3 ];
1108 while ( edgeIt != edges.end() &&
1109 !nextSideV.IsSame( SMESH_MesherHelper::IthVertex( 0, *edgeIt )))
1110 if ( SMESH_Algo::isDegenerated( *edgeIt ))
1113 sideEdges.push_back( *edgeIt++ );
1114 if ( !sideEdges.empty() )
1115 quad->side.push_back( StdMeshers_FaceSide::New(F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1116 ignoreMediumNodes, myProxyMesh));
1120 const vector<UVPtStruct>& UVPSleft = quad->side[0].GetUVPtStruct(true,0);
1121 /* vector<UVPtStruct>& UVPStop = */quad->side[1].GetUVPtStruct(false,1);
1122 /* vector<UVPtStruct>& UVPSright = */quad->side[2].GetUVPtStruct(true,1);
1123 const SMDS_MeshNode* aNode = UVPSleft[0].node;
1124 gp_Pnt2d aPnt2d = UVPSleft[0].UV();
1125 quad->side.push_back( StdMeshers_FaceSide::New( quad->side[1].grid.get(), aNode, &aPnt2d ));
1126 myNeedSmooth = ( nbDegenEdges > 0 );
1131 myNeedSmooth = ( corners.size() == 4 && nbDegenEdges > 0 );
1132 int iSide = 0, nbUsedDegen = 0, nbLoops = 0;
1133 for ( ; edgeIt != edges.end(); ++nbLoops )
1135 list< TopoDS_Edge > sideEdges;
1136 TopoDS_Vertex nextSideV = corners[( iSide + 1 - nbUsedDegen ) % corners.size() ];
1137 bool nextSideVReached = false;
1140 const TopoDS_Edge& edge = *edgeIt;
1141 nextSideVReached = nextSideV.IsSame( myHelper->IthVertex( 1, edge ));
1142 if ( SMESH_Algo::isDegenerated( edge ))
1144 if ( !myNeedSmooth ) // need to make a side on a degen edge
1146 if ( sideEdges.empty() )
1148 sideEdges.push_back( edge );
1150 nextSideVReached = true;
1160 sideEdges.push_back( edge );
1164 while ( edgeIt != edges.end() && !nextSideVReached );
1166 if ( !sideEdges.empty() )
1168 quad->side.push_back
1169 ( StdMeshers_FaceSide::New( F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1170 ignoreMediumNodes, myProxyMesh ));
1173 if ( quad->side.size() == 4 )
1177 error(TComm("Bug: infinite loop in StdMeshers_Quadrangle_2D::CheckNbEdges()"));
1182 if ( quad && quad->side.size() != 4 )
1184 error(TComm("Bug: ") << quad->side.size() << " sides found instead of 4");
1193 //=============================================================================
1197 //=============================================================================
1199 bool StdMeshers_Quadrangle_2D::checkNbEdgesForEvaluate(SMESH_Mesh& aMesh,
1200 const TopoDS_Shape & aShape,
1201 MapShapeNbElems& aResMap,
1202 std::vector<int>& aNbNodes,
1206 const TopoDS_Face & F = TopoDS::Face(aShape);
1208 // verify 1 wire only, with 4 edges
1209 list< TopoDS_Edge > edges;
1210 list< int > nbEdgesInWire;
1211 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1219 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1220 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1221 MapShapeNbElemsItr anIt = aResMap.find(sm);
1222 if (anIt==aResMap.end()) {
1225 std::vector<int> aVec = (*anIt).second;
1226 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
1227 if (nbEdgesInWire.front() == 3) { // exactly 3 edges
1228 if (myTriaVertexID>0) {
1229 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
1230 TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
1232 TopoDS_Edge E1,E2,E3;
1233 for (; edgeIt != edges.end(); ++edgeIt) {
1234 TopoDS_Edge E = TopoDS::Edge(*edgeIt);
1235 TopoDS_Vertex VF, VL;
1236 TopExp::Vertices(E, VF, VL, true);
1239 else if (VL.IsSame(V))
1244 SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
1245 MapShapeNbElemsItr anIt = aResMap.find(sm);
1246 if (anIt==aResMap.end()) return false;
1247 std::vector<int> aVec = (*anIt).second;
1249 aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1251 aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
1252 sm = aMesh.GetSubMesh(E2);
1253 anIt = aResMap.find(sm);
1254 if (anIt==aResMap.end()) return false;
1255 aVec = (*anIt).second;
1257 aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1259 aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1260 sm = aMesh.GetSubMesh(E3);
1261 anIt = aResMap.find(sm);
1262 if (anIt==aResMap.end()) return false;
1263 aVec = (*anIt).second;
1265 aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1267 aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1268 aNbNodes[3] = aNbNodes[1];
1274 if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1275 for (; edgeIt != edges.end(); edgeIt++) {
1276 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1277 MapShapeNbElemsItr anIt = aResMap.find(sm);
1278 if (anIt==aResMap.end()) {
1281 std::vector<int> aVec = (*anIt).second;
1283 aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1285 aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1289 else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1290 list< TopoDS_Edge > sideEdges;
1291 while (!edges.empty()) {
1293 sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1294 bool sameSide = true;
1295 while (!edges.empty() && sameSide) {
1296 sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1298 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1300 if (nbSides == 0) { // go backward from the first edge
1302 while (!edges.empty() && sameSide) {
1303 sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1305 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1308 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1309 aNbNodes[nbSides] = 1;
1310 for (; ite!=sideEdges.end(); ite++) {
1311 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1312 MapShapeNbElemsItr anIt = aResMap.find(sm);
1313 if (anIt==aResMap.end()) {
1316 std::vector<int> aVec = (*anIt).second;
1318 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1320 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1324 // issue 20222. Try to unite only edges shared by two same faces
1327 SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1328 while (!edges.empty()) {
1330 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1331 bool sameSide = true;
1332 while (!edges.empty() && sameSide) {
1334 SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1335 twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1337 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1339 if (nbSides == 0) { // go backward from the first edge
1341 while (!edges.empty() && sameSide) {
1343 SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1344 twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1346 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1349 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1350 aNbNodes[nbSides] = 1;
1351 for (; ite!=sideEdges.end(); ite++) {
1352 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1353 MapShapeNbElemsItr anIt = aResMap.find(sm);
1354 if (anIt==aResMap.end()) {
1357 std::vector<int> aVec = (*anIt).second;
1359 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1361 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1369 nbSides = nbEdgesInWire.front();
1370 error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1378 //=============================================================================
1382 //=============================================================================
1385 StdMeshers_Quadrangle_2D::CheckAnd2Dcompute (SMESH_Mesh & aMesh,
1386 const TopoDS_Shape & aShape,
1387 const bool CreateQuadratic)
1389 _quadraticMesh = CreateQuadratic;
1391 FaceQuadStruct::Ptr quad = CheckNbEdges(aMesh, aShape);
1394 // set normalized grid on unit square in parametric domain
1395 if ( ! setNormalizedGrid( quad ))
1403 inline const vector<UVPtStruct>& getUVPtStructIn(FaceQuadStruct::Ptr& quad, int i, int nbSeg)
1405 bool isXConst = (i == QUAD_BOTTOM_SIDE || i == QUAD_TOP_SIDE);
1406 double constValue = (i == QUAD_BOTTOM_SIDE || i == QUAD_LEFT_SIDE) ? 0 : 1;
1408 quad->nbNodeOut(i) ?
1409 quad->side[i].grid->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1410 quad->side[i].grid->GetUVPtStruct (isXConst,constValue);
1412 inline gp_UV calcUV(double x, double y,
1413 const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1414 const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1417 ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1418 ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1422 //=============================================================================
1426 //=============================================================================
1428 bool StdMeshers_Quadrangle_2D::setNormalizedGrid (FaceQuadStruct::Ptr quad)
1430 if ( !quad->uv_grid.empty() )
1433 // Algorithme décrit dans "Génération automatique de maillages"
1434 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
1435 // traitement dans le domaine paramétrique 2d u,v
1436 // transport - projection sur le carré unité
1439 // |<----north-2-------^ a3 -------------> a2
1441 // west-3 east-1 =right | |
1445 // v----south-0--------> a0 -------------> a1
1449 const FaceQuadStruct::Side & bSide = quad->side[0];
1450 const FaceQuadStruct::Side & rSide = quad->side[1];
1451 const FaceQuadStruct::Side & tSide = quad->side[2];
1452 const FaceQuadStruct::Side & lSide = quad->side[3];
1454 int nbhoriz = Min( bSide.NbPoints(), tSide.NbPoints() );
1455 int nbvertic = Min( rSide.NbPoints(), lSide.NbPoints() );
1457 if ( myQuadList.size() == 1 )
1459 // all sub-quads must have NO sides with nbNodeOut > 0
1460 quad->nbNodeOut(0) = Max( 0, bSide.grid->NbPoints() - tSide.grid->NbPoints() );
1461 quad->nbNodeOut(1) = Max( 0, rSide.grid->NbPoints() - lSide.grid->NbPoints() );
1462 quad->nbNodeOut(2) = Max( 0, tSide.grid->NbPoints() - bSide.grid->NbPoints() );
1463 quad->nbNodeOut(3) = Max( 0, lSide.grid->NbPoints() - rSide.grid->NbPoints() );
1465 const vector<UVPtStruct>& uv_e0 = bSide.GetUVPtStruct();
1466 const vector<UVPtStruct>& uv_e1 = rSide.GetUVPtStruct();
1467 const vector<UVPtStruct>& uv_e2 = tSide.GetUVPtStruct();
1468 const vector<UVPtStruct>& uv_e3 = lSide.GetUVPtStruct();
1469 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1470 //return error("Can't find nodes on sides");
1471 return error(COMPERR_BAD_INPUT_MESH);
1473 quad->uv_grid.resize( nbvertic * nbhoriz );
1474 quad->iSize = nbhoriz;
1475 quad->jSize = nbvertic;
1476 UVPtStruct *uv_grid = & quad->uv_grid[0];
1478 quad->uv_box.Clear();
1480 // copy data of face boundary
1482 FaceQuadStruct::SideIterator sideIter;
1486 const double x0 = bSide.First().normParam;
1487 const double dx = bSide.Last().normParam - bSide.First().normParam;
1488 for ( sideIter.Init( bSide ); sideIter.More(); sideIter.Next() ) {
1489 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1490 sideIter.UVPt().y = 0.;
1491 uv_grid[ j * nbhoriz + sideIter.Count() ] = sideIter.UVPt();
1492 quad->uv_box.Add( sideIter.UVPt().UV() );
1496 const int i = nbhoriz - 1;
1497 const double y0 = rSide.First().normParam;
1498 const double dy = rSide.Last().normParam - rSide.First().normParam;
1499 sideIter.Init( rSide );
1500 if ( quad->UVPt( i, sideIter.Count() ).node )
1501 sideIter.Next(); // avoid copying from a split emulated side
1502 for ( ; sideIter.More(); sideIter.Next() ) {
1503 sideIter.UVPt().x = 1.;
1504 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1505 uv_grid[ sideIter.Count() * nbhoriz + i ] = sideIter.UVPt();
1506 quad->uv_box.Add( sideIter.UVPt().UV() );
1510 const int j = nbvertic - 1;
1511 const double x0 = tSide.First().normParam;
1512 const double dx = tSide.Last().normParam - tSide.First().normParam;
1513 int i = 0, nb = nbhoriz;
1514 sideIter.Init( tSide );
1515 if ( quad->UVPt( nb-1, j ).node ) --nb; // avoid copying from a split emulated side
1516 for ( ; i < nb; i++, sideIter.Next()) {
1517 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1518 sideIter.UVPt().y = 1.;
1519 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1520 quad->uv_box.Add( sideIter.UVPt().UV() );
1525 const double y0 = lSide.First().normParam;
1526 const double dy = lSide.Last().normParam - lSide.First().normParam;
1527 int j = 0, nb = nbvertic;
1528 sideIter.Init( lSide );
1529 if ( quad->UVPt( i, j ).node )
1530 ++j, sideIter.Next(); // avoid copying from a split emulated side
1531 if ( quad->UVPt( i, nb-1 ).node )
1533 for ( ; j < nb; j++, sideIter.Next()) {
1534 sideIter.UVPt().x = 0.;
1535 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1536 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1537 quad->uv_box.Add( sideIter.UVPt().UV() );
1541 // normalized 2d parameters on grid
1543 for (int i = 1; i < nbhoriz-1; i++)
1545 const double x0 = quad->UVPt( i, 0 ).x;
1546 const double x1 = quad->UVPt( i, nbvertic-1 ).x;
1547 for (int j = 1; j < nbvertic-1; j++)
1549 const double y0 = quad->UVPt( 0, j ).y;
1550 const double y1 = quad->UVPt( nbhoriz-1, j ).y;
1551 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1552 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1553 double y = y0 + x * (y1 - y0);
1554 int ij = j * nbhoriz + i;
1557 uv_grid[ij].node = NULL;
1561 // projection on 2d domain (u,v)
1563 gp_UV a0 = quad->UVPt( 0, 0 ).UV();
1564 gp_UV a1 = quad->UVPt( nbhoriz-1, 0 ).UV();
1565 gp_UV a2 = quad->UVPt( nbhoriz-1, nbvertic-1 ).UV();
1566 gp_UV a3 = quad->UVPt( 0, nbvertic-1 ).UV();
1568 for (int i = 1; i < nbhoriz-1; i++)
1570 gp_UV p0 = quad->UVPt( i, 0 ).UV();
1571 gp_UV p2 = quad->UVPt( i, nbvertic-1 ).UV();
1572 for (int j = 1; j < nbvertic-1; j++)
1574 gp_UV p1 = quad->UVPt( nbhoriz-1, j ).UV();
1575 gp_UV p3 = quad->UVPt( 0, j ).UV();
1577 int ij = j * nbhoriz + i;
1578 double x = uv_grid[ij].x;
1579 double y = uv_grid[ij].y;
1581 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1583 uv_grid[ij].u = uv.X();
1584 uv_grid[ij].v = uv.Y();
1590 //=======================================================================
1591 //function : ShiftQuad
1592 //purpose : auxilary function for computeQuadPref
1593 //=======================================================================
1595 void StdMeshers_Quadrangle_2D::shiftQuad(FaceQuadStruct::Ptr& quad, const int num )
1597 quad->shift( num, /*ori=*/true, /*keepGrid=*/myQuadList.size() > 1 );
1600 //================================================================================
1602 * \brief Rotate sides of a quad CCW by given nb of quartes
1603 * \param nb - number of rotation quartes
1604 * \param ori - to keep orientation of sides as in an unit quad or not
1605 * \param keepGrid - if \c true Side::grid is not changed, Side::from and Side::to
1606 * are altered instead
1608 //================================================================================
1610 void FaceQuadStruct::shift( size_t nb, bool ori, bool keepGrid )
1612 if ( nb == 0 ) return;
1614 nb = nb % NB_QUAD_SIDES;
1616 vector< Side > newSides( side.size() );
1617 vector< Side* > sidePtrs( side.size() );
1618 for (int i = QUAD_BOTTOM_SIDE; i < NB_QUAD_SIDES; ++i)
1620 int id = (i + nb) % NB_QUAD_SIDES;
1623 bool wasForward = (i < QUAD_TOP_SIDE);
1624 bool newForward = (id < QUAD_TOP_SIDE);
1625 if ( wasForward != newForward )
1626 side[ i ].Reverse( keepGrid );
1628 newSides[ id ] = side[ i ];
1629 sidePtrs[ i ] = & side[ i ];
1631 // make newSides refer newSides via Side::Contact's
1632 for ( size_t i = 0; i < newSides.size(); ++i )
1634 FaceQuadStruct::Side& ns = newSides[ i ];
1635 for ( size_t iC = 0; iC < ns.contacts.size(); ++iC )
1637 FaceQuadStruct::Side* oSide = ns.contacts[iC].other_side;
1638 vector< Side* >::iterator sIt = std::find( sidePtrs.begin(), sidePtrs.end(), oSide );
1639 if ( sIt != sidePtrs.end() )
1640 ns.contacts[iC].other_side = & newSides[ *sIt - sidePtrs[0] ];
1643 newSides.swap( side );
1645 if ( keepGrid && !uv_grid.empty() )
1647 if ( nb == 2 ) // "PI"
1649 std::reverse( uv_grid.begin(), uv_grid.end() );
1653 FaceQuadStruct newQuad;
1654 newQuad.uv_grid.resize( uv_grid.size() );
1655 newQuad.iSize = jSize;
1656 newQuad.jSize = iSize;
1657 int i, j, iRev, jRev;
1658 int *iNew = ( nb == 1 ) ? &jRev : &j;
1659 int *jNew = ( nb == 1 ) ? &i : &iRev;
1660 for ( i = 0, iRev = iSize-1; i < iSize; ++i, --iRev )
1661 for ( j = 0, jRev = jSize-1; j < jSize; ++j, --jRev )
1662 newQuad.UVPt( *iNew, *jNew ) = UVPt( i, j );
1664 std::swap( iSize, jSize );
1665 std::swap( uv_grid, newQuad.uv_grid );
1674 //=======================================================================
1676 //purpose : auxilary function for computeQuadPref
1677 //=======================================================================
1679 static gp_UV calcUV(double x0, double x1, double y0, double y1,
1680 FaceQuadStruct::Ptr& quad,
1681 const gp_UV& a0, const gp_UV& a1,
1682 const gp_UV& a2, const gp_UV& a3)
1684 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1685 double y = y0 + x * (y1 - y0);
1687 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1688 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1689 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1690 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1692 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1697 //=======================================================================
1698 //function : calcUV2
1699 //purpose : auxilary function for computeQuadPref
1700 //=======================================================================
1702 static gp_UV calcUV2(double x, double y,
1703 FaceQuadStruct::Ptr& quad,
1704 const gp_UV& a0, const gp_UV& a1,
1705 const gp_UV& a2, const gp_UV& a3)
1707 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1708 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1709 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1710 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1712 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1718 //=======================================================================
1720 * Create only quandrangle faces
1722 //=======================================================================
1724 bool StdMeshers_Quadrangle_2D::computeQuadPref (SMESH_Mesh & aMesh,
1725 const TopoDS_Face& aFace,
1726 FaceQuadStruct::Ptr quad)
1728 const bool OldVersion = (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED);
1729 const bool WisF = true;
1731 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1732 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
1733 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
1735 int nb = quad->side[0].NbPoints();
1736 int nr = quad->side[1].NbPoints();
1737 int nt = quad->side[2].NbPoints();
1738 int nl = quad->side[3].NbPoints();
1739 int dh = abs(nb-nt);
1740 int dv = abs(nr-nl);
1742 if ( myForcedPnts.empty() )
1744 // rotate sides to be as in the picture below and to have
1745 // dh >= dv and nt > nb
1747 shiftQuad( quad, ( nt > nb ) ? 0 : 2 );
1749 shiftQuad( quad, ( nr > nl ) ? 1 : 3 );
1753 // rotate the quad to have nt > nb [and nr > nl]
1755 shiftQuad ( quad, nr > nl ? 1 : 2 );
1757 shiftQuad( quad, nb == nt ? 1 : 0 );
1759 shiftQuad( quad, 3 );
1762 nb = quad->side[0].NbPoints();
1763 nr = quad->side[1].NbPoints();
1764 nt = quad->side[2].NbPoints();
1765 nl = quad->side[3].NbPoints();
1768 int nbh = Max(nb,nt);
1769 int nbv = Max(nr,nl);
1773 // Orientation of face and 3 main domain for future faces
1774 // ----------- Old version ---------------
1780 // left | |__| | rigth
1787 // ----------- New version ---------------
1793 // left |/________\| rigth
1801 const int bfrom = quad->side[0].from;
1802 const int rfrom = quad->side[1].from;
1803 const int tfrom = quad->side[2].from;
1804 const int lfrom = quad->side[3].from;
1806 const vector<UVPtStruct>& uv_eb_vec = quad->side[0].GetUVPtStruct(true,0);
1807 const vector<UVPtStruct>& uv_er_vec = quad->side[1].GetUVPtStruct(false,1);
1808 const vector<UVPtStruct>& uv_et_vec = quad->side[2].GetUVPtStruct(true,1);
1809 const vector<UVPtStruct>& uv_el_vec = quad->side[3].GetUVPtStruct(false,0);
1810 if (uv_eb_vec.empty() ||
1811 uv_er_vec.empty() ||
1812 uv_et_vec.empty() ||
1814 return error(COMPERR_BAD_INPUT_MESH);
1816 FaceQuadStruct::SideIterator uv_eb, uv_er, uv_et, uv_el;
1817 uv_eb.Init( quad->side[0] );
1818 uv_er.Init( quad->side[1] );
1819 uv_et.Init( quad->side[2] );
1820 uv_el.Init( quad->side[3] );
1822 gp_UV a0,a1,a2,a3, p0,p1,p2,p3, uv;
1825 a0 = uv_eb[ 0 ].UV();
1826 a1 = uv_er[ 0 ].UV();
1827 a2 = uv_er[ nr-1 ].UV();
1828 a3 = uv_et[ 0 ].UV();
1830 if ( !myForcedPnts.empty() )
1832 if ( dv != 0 && dh != 0 ) // here myQuadList.size() == 1
1834 const int dmin = Min( dv, dh );
1836 // Make a side separating domains L and Cb
1837 StdMeshers_FaceSidePtr sideLCb;
1838 UVPtStruct p3dom; // a point where 3 domains meat
1840 vector<UVPtStruct> pointsLCb( dmin+1 ); // 1--------1
1841 pointsLCb[0] = uv_eb[0]; // | | |
1842 for ( int i = 1; i <= dmin; ++i ) // | |Ct|
1844 x = uv_et[ i ].normParam; // | |__|
1845 y = uv_er[ i ].normParam; // | / |
1846 p0 = quad->side[0].grid->Value2d( x ).XY(); // | / Cb |dmin
1847 p1 = uv_er[ i ].UV(); // |/ |
1848 p2 = uv_et[ i ].UV(); // 0--------0
1849 p3 = quad->side[3].grid->Value2d( y ).XY();
1850 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1851 pointsLCb[ i ].u = uv.X();
1852 pointsLCb[ i ].v = uv.Y();
1854 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1855 p3dom = pointsLCb.back();
1857 // Make a side separating domains L and Ct
1858 StdMeshers_FaceSidePtr sideLCt;
1860 vector<UVPtStruct> pointsLCt( nl );
1861 pointsLCt[0] = p3dom;
1862 pointsLCt.back() = uv_et[ dmin ];
1863 x = uv_et[ dmin ].normParam;
1864 p0 = quad->side[0].grid->Value2d( x ).XY();
1865 p2 = uv_et[ dmin ].UV();
1866 double y0 = uv_er[ dmin ].normParam;
1867 for ( int i = 1; i < nl-1; ++i )
1869 y = y0 + i / ( nl-1. ) * ( 1. - y0 );
1870 p1 = quad->side[1].grid->Value2d( y ).XY();
1871 p3 = quad->side[3].grid->Value2d( y ).XY();
1872 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1873 pointsLCt[ i ].u = uv.X();
1874 pointsLCt[ i ].v = uv.Y();
1876 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
1878 // Make a side separating domains Cb and Ct
1879 StdMeshers_FaceSidePtr sideCbCt;
1881 vector<UVPtStruct> pointsCbCt( nb );
1882 pointsCbCt[0] = p3dom;
1883 pointsCbCt.back() = uv_er[ dmin ];
1884 y = uv_er[ dmin ].normParam;
1885 p1 = uv_er[ dmin ].UV();
1886 p3 = quad->side[3].grid->Value2d( y ).XY();
1887 double x0 = uv_et[ dmin ].normParam;
1888 for ( int i = 1; i < nb-1; ++i )
1890 x = x0 + i / ( nb-1. ) * ( 1. - x0 );
1891 p2 = quad->side[2].grid->Value2d( x ).XY();
1892 p0 = quad->side[0].grid->Value2d( x ).XY();
1893 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1894 pointsCbCt[ i ].u = uv.X();
1895 pointsCbCt[ i ].v = uv.Y();
1897 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
1900 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
1901 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
1902 qCb->side.resize(4);
1903 qCb->side[0] = quad->side[0];
1904 qCb->side[1] = quad->side[1];
1905 qCb->side[2] = sideCbCt;
1906 qCb->side[3] = sideLCb;
1907 qCb->side[1].to = dmin+1;
1909 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
1910 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
1912 qL->side[0] = sideLCb;
1913 qL->side[1] = sideLCt;
1914 qL->side[2] = quad->side[2];
1915 qL->side[3] = quad->side[3];
1916 qL->side[2].to = dmin+1;
1917 // Make Ct from the main quad
1918 FaceQuadStruct::Ptr qCt = quad;
1919 qCt->side[0] = sideCbCt;
1920 qCt->side[3] = sideLCt;
1921 qCt->side[1].from = dmin;
1922 qCt->side[2].from = dmin;
1923 qCt->uv_grid.clear();
1927 qCb->side[3].AddContact( dmin, & qCb->side[2], 0 );
1928 qCb->side[3].AddContact( dmin, & qCt->side[3], 0 );
1929 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
1930 qCt->side[0].AddContact( 0, & qL ->side[0], dmin );
1931 qL ->side[0].AddContact( dmin, & qL ->side[1], 0 );
1932 qL ->side[0].AddContact( dmin, & qCb->side[2], 0 );
1935 return computeQuadDominant( aMesh, aFace );
1937 return computeQuadPref( aMesh, aFace, qCt );
1939 } // if ( dv != 0 && dh != 0 )
1941 const int db = quad->side[0].IsReversed() ? -1 : +1;
1942 const int dr = quad->side[1].IsReversed() ? -1 : +1;
1943 const int dt = quad->side[2].IsReversed() ? -1 : +1;
1944 const int dl = quad->side[3].IsReversed() ? -1 : +1;
1946 // Case dv == 0, here possibly myQuadList.size() > 1
1958 const int lw = dh/2; // lateral width
1962 double lL = quad->side[3].Length();
1963 double lLwL = quad->side[2].Length( tfrom,
1964 tfrom + ( lw ) * dt );
1965 yCbL = lLwL / ( lLwL + lL );
1967 double lR = quad->side[1].Length();
1968 double lLwR = quad->side[2].Length( tfrom + ( lw + nb-1 ) * dt,
1969 tfrom + ( lw + nb-1 + lw ) * dt);
1970 yCbR = lLwR / ( lLwR + lR );
1972 // Make sides separating domains Cb and L and R
1973 StdMeshers_FaceSidePtr sideLCb, sideRCb;
1974 UVPtStruct pTBL, pTBR; // points where 3 domains meat
1976 vector<UVPtStruct> pointsLCb( lw+1 ), pointsRCb( lw+1 );
1977 pointsLCb[0] = uv_eb[ 0 ];
1978 pointsRCb[0] = uv_eb[ nb-1 ];
1979 for ( int i = 1, i2 = nt-2; i <= lw; ++i, --i2 )
1981 x = quad->side[2].Param( i );
1983 p0 = quad->side[0].Value2d( x );
1984 p1 = quad->side[1].Value2d( y );
1985 p2 = uv_et[ i ].UV();
1986 p3 = quad->side[3].Value2d( y );
1987 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1988 pointsLCb[ i ].u = uv.X();
1989 pointsLCb[ i ].v = uv.Y();
1990 pointsLCb[ i ].x = x;
1992 x = quad->side[2].Param( i2 );
1994 p1 = quad->side[1].Value2d( y );
1995 p0 = quad->side[0].Value2d( x );
1996 p2 = uv_et[ i2 ].UV();
1997 p3 = quad->side[3].Value2d( y );
1998 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1999 pointsRCb[ i ].u = uv.X();
2000 pointsRCb[ i ].v = uv.Y();
2001 pointsRCb[ i ].x = x;
2003 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
2004 sideRCb = StdMeshers_FaceSide::New( pointsRCb, aFace );
2005 pTBL = pointsLCb.back();
2006 pTBR = pointsRCb.back();
2008 // Make sides separating domains Ct and L and R
2009 StdMeshers_FaceSidePtr sideLCt, sideRCt;
2011 vector<UVPtStruct> pointsLCt( nl ), pointsRCt( nl );
2012 pointsLCt[0] = pTBL;
2013 pointsLCt.back() = uv_et[ lw ];
2014 pointsRCt[0] = pTBR;
2015 pointsRCt.back() = uv_et[ lw + nb - 1 ];
2017 p0 = quad->side[0].Value2d( x );
2018 p2 = uv_et[ lw ].UV();
2019 int iR = lw + nb - 1;
2021 gp_UV p0R = quad->side[0].Value2d( xR );
2022 gp_UV p2R = uv_et[ iR ].UV();
2023 for ( int i = 1; i < nl-1; ++i )
2025 y = yCbL + ( 1. - yCbL ) * i / (nl-1.);
2026 p1 = quad->side[1].Value2d( y );
2027 p3 = quad->side[3].Value2d( y );
2028 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
2029 pointsLCt[ i ].u = uv.X();
2030 pointsLCt[ i ].v = uv.Y();
2032 y = yCbR + ( 1. - yCbR ) * i / (nl-1.);
2033 p1 = quad->side[1].Value2d( y );
2034 p3 = quad->side[3].Value2d( y );
2035 uv = calcUV( xR,y, a0,a1,a2,a3, p0R,p1,p2R,p3 );
2036 pointsRCt[ i ].u = uv.X();
2037 pointsRCt[ i ].v = uv.Y();
2039 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
2040 sideRCt = StdMeshers_FaceSide::New( pointsRCt, aFace );
2042 // Make a side separating domains Cb and Ct
2043 StdMeshers_FaceSidePtr sideCbCt;
2045 vector<UVPtStruct> pointsCbCt( nb );
2046 pointsCbCt[0] = pTBL;
2047 pointsCbCt.back() = pTBR;
2048 p1 = quad->side[1].Value2d( yCbR );
2049 p3 = quad->side[3].Value2d( yCbL );
2050 for ( int i = 1; i < nb-1; ++i )
2052 x = quad->side[2].Param( i + lw );
2053 y = yCbL + ( yCbR - yCbL ) * i / (nb-1.);
2054 p2 = uv_et[ i + lw ].UV();
2055 p0 = quad->side[0].Value2d( x );
2056 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
2057 pointsCbCt[ i ].u = uv.X();
2058 pointsCbCt[ i ].v = uv.Y();
2060 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
2063 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
2064 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
2065 qCb->side.resize(4);
2066 qCb->side[0] = quad->side[0];
2067 qCb->side[1] = sideRCb;
2068 qCb->side[2] = sideCbCt;
2069 qCb->side[3] = sideLCb;
2071 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
2072 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
2074 qL->side[0] = sideLCb;
2075 qL->side[1] = sideLCt;
2076 qL->side[2] = quad->side[2];
2077 qL->side[3] = quad->side[3];
2078 qL->side[2].to = ( lw + 1 ) * dt + tfrom;
2080 FaceQuadStruct* qR = new FaceQuadStruct( quad->face, "R" );
2081 myQuadList.push_back( FaceQuadStruct::Ptr( qR ));
2083 qR->side[0] = sideRCb;
2084 qR->side[0].from = lw;
2085 qR->side[0].to = -1;
2086 qR->side[0].di = -1;
2087 qR->side[1] = quad->side[1];
2088 qR->side[2] = quad->side[2];
2089 qR->side[2].from = ( lw + nb-1 ) * dt + tfrom;
2090 qR->side[3] = sideRCt;
2091 // Make Ct from the main quad
2092 FaceQuadStruct::Ptr qCt = quad;
2093 qCt->side[0] = sideCbCt;
2094 qCt->side[1] = sideRCt;
2095 qCt->side[2].from = ( lw ) * dt + tfrom;
2096 qCt->side[2].to = ( lw + nb ) * dt + tfrom;
2097 qCt->side[3] = sideLCt;
2098 qCt->uv_grid.clear();
2102 qCb->side[3].AddContact( lw, & qCb->side[2], 0 );
2103 qCb->side[3].AddContact( lw, & qCt->side[3], 0 );
2104 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
2105 qCt->side[0].AddContact( 0, & qL ->side[0], lw );
2106 qL ->side[0].AddContact( lw, & qL ->side[1], 0 );
2107 qL ->side[0].AddContact( lw, & qCb->side[2], 0 );
2109 qCb->side[1].AddContact( lw, & qCb->side[2], nb-1 );
2110 qCb->side[1].AddContact( lw, & qCt->side[1], 0 );
2111 qCt->side[0].AddContact( nb-1, & qCt->side[1], 0 );
2112 qCt->side[0].AddContact( nb-1, & qR ->side[0], lw );
2113 qR ->side[3].AddContact( 0, & qR ->side[0], lw );
2114 qR ->side[3].AddContact( 0, & qCb->side[2], nb-1 );
2116 return computeQuadDominant( aMesh, aFace );
2118 } // if ( !myForcedPnts.empty() )
2129 // arrays for normalized params
2130 TColStd_SequenceOfReal npb, npr, npt, npl;
2131 for (i=0; i<nb; i++) {
2132 npb.Append(uv_eb[i].normParam);
2134 for (i=0; i<nr; i++) {
2135 npr.Append(uv_er[i].normParam);
2137 for (i=0; i<nt; i++) {
2138 npt.Append(uv_et[i].normParam);
2140 for (i=0; i<nl; i++) {
2141 npl.Append(uv_el[i].normParam);
2146 // add some params to right and left after the first param
2149 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
2150 for (i=1; i<=dr; i++) {
2151 npr.InsertAfter(1,npr.Value(2)-dpr);
2155 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
2156 for (i=1; i<=dl; i++) {
2157 npl.InsertAfter(1,npl.Value(2)-dpr);
2161 int nnn = Min(nr,nl);
2162 // auxilary sequence of XY for creation nodes
2163 // in the bottom part of central domain
2164 // Length of UVL and UVR must be == nbv-nnn
2165 TColgp_SequenceOfXY UVL, UVR, UVT;
2168 // step1: create faces for left domain
2169 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2171 for (j=1; j<=nl; j++)
2172 NodesL.SetValue(1,j,uv_el[j-1].node);
2175 for (i=1; i<=dl; i++)
2176 NodesL.SetValue(i+1,nl,uv_et[i].node);
2177 // create and add needed nodes
2178 TColgp_SequenceOfXY UVtmp;
2179 for (i=1; i<=dl; i++) {
2180 double x0 = npt.Value(i+1);
2183 double y0 = npl.Value(i+1);
2184 double y1 = npr.Value(i+1);
2185 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2186 gp_Pnt P = S->Value(UV.X(),UV.Y());
2187 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2188 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2189 NodesL.SetValue(i+1,1,N);
2190 if (UVL.Length()<nbv-nnn) UVL.Append(UV);
2192 for (j=2; j<nl; j++) {
2193 double y0 = npl.Value(dl+j);
2194 double y1 = npr.Value(dl+j);
2195 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2196 gp_Pnt P = S->Value(UV.X(),UV.Y());
2197 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2198 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2199 NodesL.SetValue(i+1,j,N);
2200 if (i==dl) UVtmp.Append(UV);
2203 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn; i++) {
2204 UVL.Append(UVtmp.Value(i));
2207 for (i=1; i<=dl; i++) {
2208 for (j=1; j<nl; j++) {
2211 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2212 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2213 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2219 // fill UVL using c2d
2220 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
2221 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2225 // step2: create faces for right domain
2226 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2228 for (j=1; j<=nr; j++)
2229 NodesR.SetValue(1,j,uv_er[nr-j].node);
2232 for (i=1; i<=dr; i++)
2233 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2234 // create and add needed nodes
2235 TColgp_SequenceOfXY UVtmp;
2236 for (i=1; i<=dr; i++) {
2237 double x0 = npt.Value(nt-i);
2240 double y0 = npl.Value(i+1);
2241 double y1 = npr.Value(i+1);
2242 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2243 gp_Pnt P = S->Value(UV.X(),UV.Y());
2244 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2245 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2246 NodesR.SetValue(i+1,nr,N);
2247 if (UVR.Length()<nbv-nnn) UVR.Append(UV);
2249 for (j=2; j<nr; j++) {
2250 double y0 = npl.Value(nbv-j+1);
2251 double y1 = npr.Value(nbv-j+1);
2252 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2253 gp_Pnt P = S->Value(UV.X(),UV.Y());
2254 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2255 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2256 NodesR.SetValue(i+1,j,N);
2257 if (i==dr) UVtmp.Prepend(UV);
2260 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
2261 UVR.Append(UVtmp.Value(i));
2264 for (i=1; i<=dr; i++) {
2265 for (j=1; j<nr; j++) {
2268 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2269 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2270 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2276 // fill UVR using c2d
2277 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
2278 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2282 // step3: create faces for central domain
2283 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2284 // add first line using NodesL
2285 for (i=1; i<=dl+1; i++)
2286 NodesC.SetValue(1,i,NodesL(i,1));
2287 for (i=2; i<=nl; i++)
2288 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2289 // add last line using NodesR
2290 for (i=1; i<=dr+1; i++)
2291 NodesC.SetValue(nb,i,NodesR(i,nr));
2292 for (i=1; i<nr; i++)
2293 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2294 // add top nodes (last columns)
2295 for (i=dl+2; i<nbh-dr; i++)
2296 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2297 // add bottom nodes (first columns)
2298 for (i=2; i<nb; i++)
2299 NodesC.SetValue(i,1,uv_eb[i-1].node);
2301 // create and add needed nodes
2302 // add linear layers
2303 for (i=2; i<nb; i++) {
2304 double x0 = npt.Value(dl+i);
2306 for (j=1; j<nnn; j++) {
2307 double y0 = npl.Value(nbv-nnn+j);
2308 double y1 = npr.Value(nbv-nnn+j);
2309 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2310 gp_Pnt P = S->Value(UV.X(),UV.Y());
2311 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2312 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2313 NodesC.SetValue(i,nbv-nnn+j,N);
2318 // add diagonal layers
2319 gp_UV A2 = UVR.Value(nbv-nnn);
2320 gp_UV A3 = UVL.Value(nbv-nnn);
2321 for (i=1; i<nbv-nnn; i++) {
2322 gp_UV p1 = UVR.Value(i);
2323 gp_UV p3 = UVL.Value(i);
2324 double y = i / double(nbv-nnn);
2325 for (j=2; j<nb; j++) {
2326 double x = npb.Value(j);
2327 gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
2328 gp_UV p2 = UVT.Value( j-1 );
2329 gp_UV UV = calcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
2330 gp_Pnt P = S->Value(UV.X(),UV.Y());
2331 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2332 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2333 NodesC.SetValue(j,i+1,N);
2337 for (i=1; i<nb; i++) {
2338 for (j=1; j<nbv; j++) {
2341 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2342 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2343 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2349 else { // New version (!OldVersion)
2350 // step1: create faces for bottom rectangle domain
2351 StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
2352 // fill UVL and UVR using c2d
2353 for (j=0; j<nb; j++) {
2354 NodesBRD.SetValue(j+1,1,uv_eb[j].node);
2356 for (i=1; i<nnn-1; i++) {
2357 NodesBRD.SetValue(1,i+1,uv_el[i].node);
2358 NodesBRD.SetValue(nb,i+1,uv_er[i].node);
2359 for (j=2; j<nb; j++) {
2360 double x = npb.Value(j);
2361 double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
2362 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2363 gp_Pnt P = S->Value(UV.X(),UV.Y());
2364 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2365 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2366 NodesBRD.SetValue(j,i+1,N);
2369 for (j=1; j<nnn-1; j++) {
2370 for (i=1; i<nb; i++) {
2373 myHelper->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
2374 NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
2375 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2379 int drl = abs(nr-nl);
2380 // create faces for region C
2381 StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
2382 // add nodes from previous region
2383 for (j=1; j<=nb; j++) {
2384 NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
2386 if ((drl+addv) > 0) {
2391 TColgp_SequenceOfXY UVtmp;
2392 double drparam = npr.Value(nr) - npr.Value(nnn-1);
2393 double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
2395 for (i=1; i<=drl; i++) {
2396 // add existed nodes from right edge
2397 NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
2398 //double dtparam = npt.Value(i+1);
2399 y1 = npr.Value(nnn+i-1); // param on right edge
2400 double dpar = (y1 - npr.Value(nnn-1))/drparam;
2401 y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
2402 double dy = y1 - y0;
2403 for (j=1; j<nb; j++) {
2404 double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
2405 double y = y0 + dy*x;
2406 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2407 gp_Pnt P = S->Value(UV.X(),UV.Y());
2408 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2409 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2410 NodesC.SetValue(j,i+1,N);
2413 double dy0 = (1-y0)/(addv+1);
2414 double dy1 = (1-y1)/(addv+1);
2415 for (i=1; i<=addv; i++) {
2416 double yy0 = y0 + dy0*i;
2417 double yy1 = y1 + dy1*i;
2418 double dyy = yy1 - yy0;
2419 for (j=1; j<=nb; j++) {
2420 double x = npt.Value(i+1+drl) +
2421 npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
2422 double y = yy0 + dyy*x;
2423 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2424 gp_Pnt P = S->Value(UV.X(),UV.Y());
2425 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2426 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2427 NodesC.SetValue(j,i+drl+1,N);
2434 TColgp_SequenceOfXY UVtmp;
2435 double dlparam = npl.Value(nl) - npl.Value(nnn-1);
2436 double drparam = npr.Value(nnn) - npr.Value(nnn-1);
2437 double y0 = npl.Value(nnn-1);
2438 double y1 = npr.Value(nnn-1);
2439 for (i=1; i<=drl; i++) {
2440 // add existed nodes from right edge
2441 NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
2442 y0 = npl.Value(nnn+i-1); // param on left edge
2443 double dpar = (y0 - npl.Value(nnn-1))/dlparam;
2444 y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
2445 double dy = y1 - y0;
2446 for (j=2; j<=nb; j++) {
2447 double x = npb.Value(j)*npt.Value(nt-i);
2448 double y = y0 + dy*x;
2449 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2450 gp_Pnt P = S->Value(UV.X(),UV.Y());
2451 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2452 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2453 NodesC.SetValue(j,i+1,N);
2456 double dy0 = (1-y0)/(addv+1);
2457 double dy1 = (1-y1)/(addv+1);
2458 for (i=1; i<=addv; i++) {
2459 double yy0 = y0 + dy0*i;
2460 double yy1 = y1 + dy1*i;
2461 double dyy = yy1 - yy0;
2462 for (j=1; j<=nb; j++) {
2463 double x = npt.Value(i+1) +
2464 npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
2465 double y = yy0 + dyy*x;
2466 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2467 gp_Pnt P = S->Value(UV.X(),UV.Y());
2468 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2469 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2470 NodesC.SetValue(j,i+drl+1,N);
2475 for (j=1; j<=drl+addv; j++) {
2476 for (i=1; i<nb; i++) {
2479 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2480 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2481 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2486 StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
2487 for (i=1; i<=nt; i++) {
2488 NodesLast.SetValue(i,2,uv_et[i-1].node);
2491 for (i=n1; i<drl+addv+1; i++) {
2493 NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
2495 for (i=1; i<=nb; i++) {
2497 NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
2499 for (i=drl+addv; i>=n2; i--) {
2501 NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
2503 for (i=1; i<nt; i++) {
2506 myHelper->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
2507 NodesLast.Value(i+1,2), NodesLast.Value(i,2));
2508 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
2511 } // if ((drl+addv) > 0)
2513 } // end new version implementation
2520 //=======================================================================
2522 * Evaluate only quandrangle faces
2524 //=======================================================================
2526 bool StdMeshers_Quadrangle_2D::evaluateQuadPref(SMESH_Mesh & aMesh,
2527 const TopoDS_Shape& aShape,
2528 std::vector<int>& aNbNodes,
2529 MapShapeNbElems& aResMap,
2532 // Auxilary key in order to keep old variant
2533 // of meshing after implementation new variant
2534 // for bug 0016220 from Mantis.
2535 bool OldVersion = false;
2536 if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
2539 const TopoDS_Face& F = TopoDS::Face(aShape);
2540 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2542 int nb = aNbNodes[0];
2543 int nr = aNbNodes[1];
2544 int nt = aNbNodes[2];
2545 int nl = aNbNodes[3];
2546 int dh = abs(nb-nt);
2547 int dv = abs(nr-nl);
2551 // it is a base case => not shift
2554 // we have to shift on 2
2563 // we have to shift quad on 1
2570 // we have to shift quad on 3
2580 int nbh = Max(nb,nt);
2581 int nbv = Max(nr,nl);
2596 // add some params to right and left after the first param
2603 int nnn = Min(nr,nl);
2608 // step1: create faces for left domain
2610 nbNodes += dl*(nl-1);
2611 nbFaces += dl*(nl-1);
2613 // step2: create faces for right domain
2615 nbNodes += dr*(nr-1);
2616 nbFaces += dr*(nr-1);
2618 // step3: create faces for central domain
2619 nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2620 nbFaces += (nb-1)*(nbv-1);
2622 else { // New version (!OldVersion)
2623 nbNodes += (nnn-2)*(nb-2);
2624 nbFaces += (nnn-2)*(nb-1);
2625 int drl = abs(nr-nl);
2626 nbNodes += drl*(nb-1) + addv*nb;
2627 nbFaces += (drl+addv)*(nb-1) + (nt-1);
2628 } // end new version implementation
2630 std::vector<int> aVec(SMDSEntity_Last);
2631 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2633 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2634 aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2635 if (aNbNodes.size()==5) {
2636 aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2637 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2641 aVec[SMDSEntity_Node] = nbNodes;
2642 aVec[SMDSEntity_Quadrangle] = nbFaces;
2643 if (aNbNodes.size()==5) {
2644 aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2645 aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2648 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2649 aResMap.insert(std::make_pair(sm,aVec));
2654 //=============================================================================
2655 /*! Split quadrangle in to 2 triangles by smallest diagonal
2658 //=============================================================================
2660 void StdMeshers_Quadrangle_2D::splitQuadFace(SMESHDS_Mesh * theMeshDS,
2662 const SMDS_MeshNode* theNode1,
2663 const SMDS_MeshNode* theNode2,
2664 const SMDS_MeshNode* theNode3,
2665 const SMDS_MeshNode* theNode4)
2667 SMDS_MeshFace* face;
2668 if ( SMESH_TNodeXYZ( theNode1 ).SquareDistance( theNode3 ) >
2669 SMESH_TNodeXYZ( theNode2 ).SquareDistance( theNode4 ) )
2671 face = myHelper->AddFace(theNode2, theNode4 , theNode1);
2672 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2673 face = myHelper->AddFace(theNode2, theNode3, theNode4);
2674 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2678 face = myHelper->AddFace(theNode1, theNode2 ,theNode3);
2679 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2680 face = myHelper->AddFace(theNode1, theNode3, theNode4);
2681 if (face) theMeshDS->SetMeshElementOnShape(face, theFaceID);
2687 enum uvPos { UV_A0, UV_A1, UV_A2, UV_A3, UV_B, UV_R, UV_T, UV_L, UV_SIZE };
2689 inline SMDS_MeshNode* makeNode( UVPtStruct & uvPt,
2691 FaceQuadStruct::Ptr& quad,
2693 SMESH_MesherHelper* helper,
2694 Handle(Geom_Surface) S)
2696 const vector<UVPtStruct>& uv_eb = quad->side[QUAD_BOTTOM_SIDE].GetUVPtStruct();
2697 const vector<UVPtStruct>& uv_et = quad->side[QUAD_TOP_SIDE ].GetUVPtStruct();
2698 double rBot = ( uv_eb.size() - 1 ) * uvPt.normParam;
2699 double rTop = ( uv_et.size() - 1 ) * uvPt.normParam;
2700 int iBot = int( rBot );
2701 int iTop = int( rTop );
2702 double xBot = uv_eb[ iBot ].normParam + ( rBot - iBot ) * ( uv_eb[ iBot+1 ].normParam - uv_eb[ iBot ].normParam );
2703 double xTop = uv_et[ iTop ].normParam + ( rTop - iTop ) * ( uv_et[ iTop+1 ].normParam - uv_et[ iTop ].normParam );
2704 double x = xBot + y * ( xTop - xBot );
2706 gp_UV uv = calcUV(/*x,y=*/x, y,
2707 /*a0,...=*/UVs[UV_A0], UVs[UV_A1], UVs[UV_A2], UVs[UV_A3],
2708 /*p0=*/quad->side[QUAD_BOTTOM_SIDE].grid->Value2d( x ).XY(),
2710 /*p2=*/quad->side[QUAD_TOP_SIDE ].grid->Value2d( x ).XY(),
2711 /*p3=*/UVs[ UV_L ]);
2712 gp_Pnt P = S->Value( uv.X(), uv.Y() );
2715 return helper->AddNode(P.X(), P.Y(), P.Z(), 0, uv.X(), uv.Y() );
2718 void reduce42( const vector<UVPtStruct>& curr_base,
2719 vector<UVPtStruct>& next_base,
2721 int & next_base_len,
2722 FaceQuadStruct::Ptr& quad,
2725 SMESH_MesherHelper* helper,
2726 Handle(Geom_Surface)& S)
2728 // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2730 // .-----a-----b i + 1
2741 const SMDS_MeshNode*& Na = next_base[ ++next_base_len ].node;
2743 Na = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2746 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2748 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2751 double u = (curr_base[j + 2].u + next_base[next_base_len - 2].u) / 2.0;
2752 double v = (curr_base[j + 2].v + next_base[next_base_len - 2].v) / 2.0;
2753 gp_Pnt P = S->Value(u,v);
2754 SMDS_MeshNode* Nc = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2757 u = (curr_base[j + 2].u + next_base[next_base_len - 1].u) / 2.0;
2758 v = (curr_base[j + 2].v + next_base[next_base_len - 1].v) / 2.0;
2760 SMDS_MeshNode* Nd = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2763 u = (curr_base[j + 2].u + next_base[next_base_len].u) / 2.0;
2764 v = (curr_base[j + 2].v + next_base[next_base_len].v) / 2.0;
2766 SMDS_MeshNode* Ne = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2769 helper->AddFace(curr_base[j + 0].node,
2770 curr_base[j + 1].node, Nc,
2771 next_base[next_base_len - 2].node);
2773 helper->AddFace(curr_base[j + 1].node,
2774 curr_base[j + 2].node, Nd, Nc);
2776 helper->AddFace(curr_base[j + 2].node,
2777 curr_base[j + 3].node, Ne, Nd);
2779 helper->AddFace(curr_base[j + 3].node,
2780 curr_base[j + 4].node, Nb, Ne);
2782 helper->AddFace(Nc, Nd, Na, next_base[next_base_len - 2].node);
2784 helper->AddFace(Nd, Ne, Nb, Na);
2787 void reduce31( const vector<UVPtStruct>& curr_base,
2788 vector<UVPtStruct>& next_base,
2790 int & next_base_len,
2791 FaceQuadStruct::Ptr& quad,
2794 SMESH_MesherHelper* helper,
2795 Handle(Geom_Surface)& S)
2797 // add one "H": nodes b,c,e and faces 1,2,4,5
2799 // .---------b i + 1
2810 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2812 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2815 double u1 = (curr_base[ j ].u + next_base[ next_base_len-1 ].u ) / 2.0;
2816 double u2 = (curr_base[ j+3 ].u + next_base[ next_base_len ].u ) / 2.0;
2817 double u3 = (u2 - u1) / 3.0;
2819 double v1 = (curr_base[ j ].v + next_base[ next_base_len-1 ].v ) / 2.0;
2820 double v2 = (curr_base[ j+3 ].v + next_base[ next_base_len ].v ) / 2.0;
2821 double v3 = (v2 - v1) / 3.0;
2825 gp_Pnt P = S->Value(u,v);
2826 SMDS_MeshNode* Nc = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2831 SMDS_MeshNode* Ne = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2835 helper->AddFace( curr_base[ j + 0 ].node,
2836 curr_base[ j + 1 ].node,
2838 next_base[ next_base_len - 1 ].node);
2840 helper->AddFace( curr_base[ j + 1 ].node,
2841 curr_base[ j + 2 ].node, Ne, Nc);
2843 helper->AddFace( curr_base[ j + 2 ].node,
2844 curr_base[ j + 3 ].node, Nb, Ne);
2846 helper->AddFace(Nc, Ne, Nb,
2847 next_base[ next_base_len - 1 ].node);
2850 typedef void (* PReduceFunction) ( const vector<UVPtStruct>& curr_base,
2851 vector<UVPtStruct>& next_base,
2853 int & next_base_len,
2854 FaceQuadStruct::Ptr & quad,
2857 SMESH_MesherHelper* helper,
2858 Handle(Geom_Surface)& S);
2862 //=======================================================================
2864 * Implementation of Reduced algorithm (meshing with quadrangles only)
2866 //=======================================================================
2868 bool StdMeshers_Quadrangle_2D::computeReduced (SMESH_Mesh & aMesh,
2869 const TopoDS_Face& aFace,
2870 FaceQuadStruct::Ptr quad)
2872 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2873 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
2874 int i,j,geomFaceID = meshDS->ShapeToIndex(aFace);
2876 int nb = quad->side[0].NbPoints(); // bottom
2877 int nr = quad->side[1].NbPoints(); // right
2878 int nt = quad->side[2].NbPoints(); // top
2879 int nl = quad->side[3].NbPoints(); // left
2881 // Simple Reduce 10->8->6->4 (3 steps) Multiple Reduce 10->4 (1 step)
2883 // .-----.-----.-----.-----. .-----.-----.-----.-----.
2884 // | / \ | / \ | | / \ | / \ |
2885 // | / .--.--. \ | | / \ | / \ |
2886 // | / / | \ \ | | / .----.----. \ |
2887 // .---.---.---.---.---.---. | / / \ | / \ \ |
2888 // | / / \ | / \ \ | | / / \ | / \ \ |
2889 // | / / .-.-. \ \ | | / / .---.---. \ \ |
2890 // | / / / | \ \ \ | | / / / \ | / \ \ \ |
2891 // .--.--.--.--.--.--.--.--. | / / / \ | / \ \ \ |
2892 // | / / / \ | / \ \ \ | | / / / .-.-. \ \ \ |
2893 // | / / / .-.-. \ \ \ | | / / / / | \ \ \ \ |
2894 // | / / / / | \ \ \ \ | | / / / / | \ \ \ \ |
2895 // .-.-.-.--.--.--.--.-.-.-. .-.-.-.--.--.--.--.-.-.-.
2897 bool MultipleReduce = false;
2909 else if (nb == nt) {
2910 nr1 = nb; // and == nt
2924 // number of rows and columns
2925 int nrows = nr1 - 1;
2926 int ncol_top = nt1 - 1;
2927 int ncol_bot = nb1 - 1;
2928 // number of rows needed to reduce ncol_bot to ncol_top using simple 3->1 "tree" (see below)
2930 int( ceil( log( double(ncol_bot) / ncol_top) / log( 3.))); // = log x base 3
2931 if ( nrows < nrows_tree31 )
2933 MultipleReduce = true;
2934 error( COMPERR_WARNING,
2935 SMESH_Comment("To use 'Reduced' transition, "
2936 "number of face rows should be at least ")
2937 << nrows_tree31 << ". Actual number of face rows is " << nrows << ". "
2938 "'Quadrangle preference (reversed)' transion has been used.");
2942 if (MultipleReduce) { // == computeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2943 //==================================================
2944 int dh = abs(nb-nt);
2945 int dv = abs(nr-nl);
2949 // it is a base case => not shift quad but may be replacement is need
2953 // we have to shift quad on 2
2959 // we have to shift quad on 1
2963 // we have to shift quad on 3
2968 nb = quad->side[0].NbPoints();
2969 nr = quad->side[1].NbPoints();
2970 nt = quad->side[2].NbPoints();
2971 nl = quad->side[3].NbPoints();
2974 int nbh = Max(nb,nt);
2975 int nbv = Max(nr,nl);
2988 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
2989 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
2990 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
2991 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
2993 if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
2994 return error(COMPERR_BAD_INPUT_MESH);
2996 // arrays for normalized params
2997 TColStd_SequenceOfReal npb, npr, npt, npl;
2998 for (j = 0; j < nb; j++) {
2999 npb.Append(uv_eb[j].normParam);
3001 for (i = 0; i < nr; i++) {
3002 npr.Append(uv_er[i].normParam);
3004 for (j = 0; j < nt; j++) {
3005 npt.Append(uv_et[j].normParam);
3007 for (i = 0; i < nl; i++) {
3008 npl.Append(uv_el[i].normParam);
3012 // orientation of face and 3 main domain for future faces
3018 // left | | | | rigth
3025 // add some params to right and left after the first param
3028 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
3029 for (i=1; i<=dr; i++) {
3030 npr.InsertAfter(1,npr.Value(2)-dpr);
3034 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
3035 for (i=1; i<=dl; i++) {
3036 npl.InsertAfter(1,npl.Value(2)-dpr);
3039 gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
3040 gp_XY a1 (uv_eb.back().u, uv_eb.back().v);
3041 gp_XY a2 (uv_et.back().u, uv_et.back().v);
3042 gp_XY a3 (uv_et.front().u, uv_et.front().v);
3044 int nnn = Min(nr,nl);
3045 // auxilary sequence of XY for creation of nodes
3046 // in the bottom part of central domain
3047 // it's length must be == nbv-nnn-1
3048 TColgp_SequenceOfXY UVL;
3049 TColgp_SequenceOfXY UVR;
3050 //==================================================
3052 // step1: create faces for left domain
3053 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
3055 for (j=1; j<=nl; j++)
3056 NodesL.SetValue(1,j,uv_el[j-1].node);
3059 for (i=1; i<=dl; i++)
3060 NodesL.SetValue(i+1,nl,uv_et[i].node);
3061 // create and add needed nodes
3062 TColgp_SequenceOfXY UVtmp;
3063 for (i=1; i<=dl; i++) {
3064 double x0 = npt.Value(i+1);
3067 double y0 = npl.Value(i+1);
3068 double y1 = npr.Value(i+1);
3069 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3070 gp_Pnt P = S->Value(UV.X(),UV.Y());
3071 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3072 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3073 NodesL.SetValue(i+1,1,N);
3074 if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
3076 for (j=2; j<nl; j++) {
3077 double y0 = npl.Value(dl+j);
3078 double y1 = npr.Value(dl+j);
3079 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3080 gp_Pnt P = S->Value(UV.X(),UV.Y());
3081 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3082 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3083 NodesL.SetValue(i+1,j,N);
3084 if (i==dl) UVtmp.Append(UV);
3087 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
3088 UVL.Append(UVtmp.Value(i));
3091 for (i=1; i<=dl; i++) {
3092 for (j=1; j<nl; j++) {
3094 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
3095 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
3096 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
3101 // fill UVL using c2d
3102 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
3103 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
3107 // step2: create faces for right domain
3108 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
3110 for (j=1; j<=nr; j++)
3111 NodesR.SetValue(1,j,uv_er[nr-j].node);
3114 for (i=1; i<=dr; i++)
3115 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
3116 // create and add needed nodes
3117 TColgp_SequenceOfXY UVtmp;
3118 for (i=1; i<=dr; i++) {
3119 double x0 = npt.Value(nt-i);
3122 double y0 = npl.Value(i+1);
3123 double y1 = npr.Value(i+1);
3124 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3125 gp_Pnt P = S->Value(UV.X(),UV.Y());
3126 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3127 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3128 NodesR.SetValue(i+1,nr,N);
3129 if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
3131 for (j=2; j<nr; j++) {
3132 double y0 = npl.Value(nbv-j+1);
3133 double y1 = npr.Value(nbv-j+1);
3134 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3135 gp_Pnt P = S->Value(UV.X(),UV.Y());
3136 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3137 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3138 NodesR.SetValue(i+1,j,N);
3139 if (i==dr) UVtmp.Prepend(UV);
3142 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
3143 UVR.Append(UVtmp.Value(i));
3146 for (i=1; i<=dr; i++) {
3147 for (j=1; j<nr; j++) {
3149 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
3150 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
3151 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
3156 // fill UVR using c2d
3157 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
3158 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
3162 // step3: create faces for central domain
3163 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
3164 // add first line using NodesL
3165 for (i=1; i<=dl+1; i++)
3166 NodesC.SetValue(1,i,NodesL(i,1));
3167 for (i=2; i<=nl; i++)
3168 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
3169 // add last line using NodesR
3170 for (i=1; i<=dr+1; i++)
3171 NodesC.SetValue(nb,i,NodesR(i,nr));
3172 for (i=1; i<nr; i++)
3173 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
3174 // add top nodes (last columns)
3175 for (i=dl+2; i<nbh-dr; i++)
3176 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
3177 // add bottom nodes (first columns)
3178 for (i=2; i<nb; i++)
3179 NodesC.SetValue(i,1,uv_eb[i-1].node);
3181 // create and add needed nodes
3182 // add linear layers
3183 for (i=2; i<nb; i++) {
3184 double x0 = npt.Value(dl+i);
3186 for (j=1; j<nnn; j++) {
3187 double y0 = npl.Value(nbv-nnn+j);
3188 double y1 = npr.Value(nbv-nnn+j);
3189 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3190 gp_Pnt P = S->Value(UV.X(),UV.Y());
3191 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3192 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3193 NodesC.SetValue(i,nbv-nnn+j,N);
3196 // add diagonal layers
3197 for (i=1; i<nbv-nnn; i++) {
3198 double du = UVR.Value(i).X() - UVL.Value(i).X();
3199 double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
3200 for (j=2; j<nb; j++) {
3201 double u = UVL.Value(i).X() + du*npb.Value(j);
3202 double v = UVL.Value(i).Y() + dv*npb.Value(j);
3203 gp_Pnt P = S->Value(u,v);
3204 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3205 meshDS->SetNodeOnFace(N, geomFaceID, u, v);
3206 NodesC.SetValue(j,i+1,N);
3210 for (i=1; i<nb; i++) {
3211 for (j=1; j<nbv; j++) {
3213 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
3214 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
3215 if (F) meshDS->SetMeshElementOnShape(F, geomFaceID);
3218 } // end Multiple Reduce implementation
3219 else { // Simple Reduce (!MultipleReduce)
3220 //=========================================================
3223 // it is a base case => not shift quad
3224 //shiftQuad(quad,0,true);
3227 // we have to shift quad on 2
3233 // we have to shift quad on 1
3237 // we have to shift quad on 3
3242 nb = quad->side[0].NbPoints();
3243 nr = quad->side[1].NbPoints();
3244 nt = quad->side[2].NbPoints();
3245 nl = quad->side[3].NbPoints();
3247 // number of rows and columns
3248 int nrows = nr - 1; // and also == nl - 1
3249 int ncol_top = nt - 1;
3250 int ncol_bot = nb - 1;
3251 int npair_top = ncol_top / 2;
3252 // maximum number of bottom elements for "linear" simple reduce 4->2
3253 int max_lin42 = ncol_top + npair_top * 2 * nrows;
3254 // maximum number of bottom elements for "linear" simple reduce 3->1
3255 int max_lin31 = ncol_top + ncol_top * 2 * nrows;
3256 // maximum number of bottom elements for "tree" simple reduce 4->2
3258 // number of rows needed to reduce ncol_bot to ncol_top using simple 4->2 "tree"
3259 int nrows_tree42 = int( log( (double)(ncol_bot / ncol_top) )/log((double)2) ); // needed to avoid overflow at pow(2) while computing max_tree42
3260 if (nrows_tree42 < nrows) {
3261 max_tree42 = npair_top * pow(2.0, nrows + 1);
3262 if ( ncol_top > npair_top * 2 ) {
3263 int delta = ncol_bot - max_tree42;
3264 for (int irow = 1; irow < nrows; irow++) {
3265 int nfour = delta / 4;
3268 if (delta <= (ncol_top - npair_top * 2))
3269 max_tree42 = ncol_bot;
3272 // maximum number of bottom elements for "tree" simple reduce 3->1
3273 //int max_tree31 = ncol_top * pow(3.0, nrows);
3274 bool is_lin_31 = false;
3275 bool is_lin_42 = false;
3276 bool is_tree_31 = false;
3277 bool is_tree_42 = false;
3278 int max_lin = max_lin42;
3279 if (ncol_bot > max_lin42) {
3280 if (ncol_bot <= max_lin31) {
3282 max_lin = max_lin31;
3286 // if ncol_bot is a 3*n or not 2*n
3287 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3289 max_lin = max_lin31;
3295 if (ncol_bot > max_lin) { // not "linear"
3296 is_tree_31 = (ncol_bot > max_tree42);
3297 if (ncol_bot <= max_tree42) {
3298 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3307 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
3308 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
3309 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
3310 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
3312 if (uv_eb.size() != nb || uv_er.size() != nr || uv_et.size() != nt || uv_el.size() != nl)
3313 return error(COMPERR_BAD_INPUT_MESH);
3315 myHelper->SetElementsOnShape( true );
3317 gp_UV uv[ UV_SIZE ];
3318 uv[ UV_A0 ].SetCoord( uv_eb.front().u, uv_eb.front().v);
3319 uv[ UV_A1 ].SetCoord( uv_eb.back().u, uv_eb.back().v );
3320 uv[ UV_A2 ].SetCoord( uv_et.back().u, uv_et.back().v );
3321 uv[ UV_A3 ].SetCoord( uv_et.front().u, uv_et.front().v);
3323 vector<UVPtStruct> curr_base = uv_eb, next_base;
3325 UVPtStruct nullUVPtStruct; nullUVPtStruct.node = 0;
3327 int curr_base_len = nb;
3328 int next_base_len = 0;
3331 { // ------------------------------------------------------------------
3332 // New algorithm implemented by request of IPAL22856
3333 // "2D quadrangle mesher of reduced type works wrong"
3334 // http://bugtracker.opencascade.com/show_bug.cgi?id=22856
3336 // the algorithm is following: all reduces are centred in horizontal
3337 // direction and are distributed among all rows
3339 if (ncol_bot > max_tree42) {
3343 if ((ncol_top/3)*3 == ncol_top ) {
3351 const int col_top_size = is_lin_42 ? 2 : 1;
3352 const int col_base_size = is_lin_42 ? 4 : 3;
3354 // Compute nb of "columns" (like in "linear" simple reducing) in all rows
3356 vector<int> nb_col_by_row;
3358 int delta_all = nb - nt;
3359 int delta_one_col = nrows * 2;
3360 int nb_col = delta_all / delta_one_col;
3361 int remainder = delta_all - nb_col * delta_one_col;
3362 if (remainder > 0) {
3365 if ( nb_col * col_top_size >= nt ) // == "tree" reducing situation
3367 // top row is full (all elements reduced), add "columns" one by one
3368 // in rows below until all bottom elements are reduced
3369 nb_col = ( nt - 1 ) / col_top_size;
3370 nb_col_by_row.resize( nrows, nb_col );
3371 int nbrows_not_full = nrows - 1;
3372 int cur_top_size = nt - 1;
3373 remainder = delta_all - nb_col * delta_one_col;
3374 while ( remainder > 0 )
3376 delta_one_col = nbrows_not_full * 2;
3377 int nb_col_add = remainder / delta_one_col;
3378 cur_top_size += 2 * nb_col_by_row[ nbrows_not_full ];
3379 int nb_col_free = cur_top_size / col_top_size - nb_col_by_row[ nbrows_not_full-1 ];
3380 if ( nb_col_add > nb_col_free )
3381 nb_col_add = nb_col_free;
3382 for ( int irow = 0; irow < nbrows_not_full; ++irow )
3383 nb_col_by_row[ irow ] += nb_col_add;
3385 remainder -= nb_col_add * delta_one_col;
3388 else // == "linear" reducing situation
3390 nb_col_by_row.resize( nrows, nb_col );
3392 for ( int irow = remainder / 2; irow < nrows; ++irow )
3393 nb_col_by_row[ irow ]--;
3398 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3400 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3402 for (i = 1; i < nr; i++) // layer by layer
3404 nb_col = nb_col_by_row[ i-1 ];
3405 int nb_next = curr_base_len - nb_col * 2;
3406 if (nb_next < nt) nb_next = nt;
3408 const double y = uv_el[ i ].normParam;
3410 if ( i + 1 == nr ) // top
3417 next_base.resize( nb_next, nullUVPtStruct );
3418 next_base.front() = uv_el[i];
3419 next_base.back() = uv_er[i];
3421 // compute normalized param u
3422 double du = 1. / ( nb_next - 1 );
3423 next_base[0].normParam = 0.;
3424 for ( j = 1; j < nb_next; ++j )
3425 next_base[j].normParam = next_base[j-1].normParam + du;
3427 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3428 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3430 int free_left = ( curr_base_len - 1 - nb_col * col_base_size ) / 2;
3431 int free_middle = curr_base_len - 1 - nb_col * col_base_size - 2 * free_left;
3433 // not reduced left elements
3434 for (j = 0; j < free_left; j++)
3437 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3439 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3441 myHelper->AddFace(curr_base[ j ].node,
3442 curr_base[ j+1 ].node,
3444 next_base[ next_base_len-1 ].node);
3447 for (int icol = 1; icol <= nb_col; icol++)
3450 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3452 j += reduce_grp_size;
3454 // elements in the middle of "columns" added for symmetry
3455 if ( free_middle > 0 && ( nb_col % 2 == 0 ) && icol == nb_col / 2 )
3457 for (int imiddle = 1; imiddle <= free_middle; imiddle++) {
3458 // f (i + 1, j + imiddle)
3459 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3461 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3463 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3464 curr_base[ j +imiddle ].node,
3466 next_base[ next_base_len-1 ].node);
3472 // not reduced right elements
3473 for (; j < curr_base_len-1; j++) {
3475 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3477 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3479 myHelper->AddFace(curr_base[ j ].node,
3480 curr_base[ j+1 ].node,
3482 next_base[ next_base_len-1 ].node);
3485 curr_base_len = next_base_len + 1;
3487 curr_base.swap( next_base );
3491 else if ( is_tree_42 || is_tree_31 )
3493 // "tree" simple reduce "42": 2->4->8->16->32->...
3495 // .-------------------------------.-------------------------------. nr
3497 // | \ .---------------.---------------. / |
3499 // .---------------.---------------.---------------.---------------.
3500 // | \ | / | \ | / |
3501 // | \ .-------.-------. / | \ .-------.-------. / |
3502 // | | | | | | | | |
3503 // .-------.-------.-------.-------.-------.-------.-------.-------. i
3504 // |\ | /|\ | /|\ | /|\ | /|
3505 // | \.---.---./ | \.---.---./ | \.---.---./ | \.---.---./ |
3506 // | | | | | | | | | | | | | | | | |
3507 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
3508 // |\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|
3509 // | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
3510 // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3511 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3514 // "tree" simple reduce "31": 1->3->9->27->...
3516 // .-----------------------------------------------------. nr
3518 // | .-----------------. |
3520 // .-----------------.-----------------.-----------------.
3521 // | \ / | \ / | \ / |
3522 // | .-----. | .-----. | .-----. | i
3523 // | | | | | | | | | |
3524 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.
3525 // |\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|
3526 // | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. |
3527 // | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3528 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3531 PReduceFunction reduceFunction = & ( is_tree_42 ? reduce42 : reduce31 );
3533 const int reduce_grp_size = is_tree_42 ? 4 : 3;
3535 for (i = 1; i < nr; i++) // layer by layer
3537 // to stop reducing, if number of nodes reaches nt
3538 int delta = curr_base_len - nt;
3540 // to calculate normalized parameter, we must know number of points in next layer
3541 int nb_reduce_groups = (curr_base_len - 1) / reduce_grp_size;
3542 int nb_next = nb_reduce_groups * (reduce_grp_size-2) + (curr_base_len - nb_reduce_groups*reduce_grp_size);
3543 if (nb_next < nt) nb_next = nt;
3545 const double y = uv_el[ i ].normParam;
3547 if ( i + 1 == nr ) // top
3554 next_base.resize( nb_next, nullUVPtStruct );
3555 next_base.front() = uv_el[i];
3556 next_base.back() = uv_er[i];
3558 // compute normalized param u
3559 double du = 1. / ( nb_next - 1 );
3560 next_base[0].normParam = 0.;
3561 for ( j = 1; j < nb_next; ++j )
3562 next_base[j].normParam = next_base[j-1].normParam + du;
3564 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3565 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3567 for (j = 0; j+reduce_grp_size < curr_base_len && delta > 0; j+=reduce_grp_size, delta-=2)
3569 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3572 // not reduced side elements (if any)
3573 for (; j < curr_base_len-1; j++)
3576 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3578 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3580 myHelper->AddFace(curr_base[ j ].node,
3581 curr_base[ j+1 ].node,
3583 next_base[ next_base_len-1 ].node);
3585 curr_base_len = next_base_len + 1;
3587 curr_base.swap( next_base );
3589 } // end "tree" simple reduce
3591 else if ( is_lin_42 || is_lin_31 ) {
3592 // "linear" simple reduce "31": 2->6->10->14
3594 // .-----------------------------.-----------------------------. nr
3596 // | .---------. | .---------. |
3598 // .---------.---------.---------.---------.---------.---------.
3599 // | / \ / \ | / \ / \ |
3600 // | / .-----. \ | / .-----. \ | i
3601 // | / | | \ | / | | \ |
3602 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.-----.
3603 // | / / \ / \ \ | / / \ / \ \ |
3604 // | / / .-. \ \ | / / .-. \ \ |
3605 // | / / / \ \ \ | / / / \ \ \ |
3606 // .--.----.---.-----.---.-----.-.--.----.---.-----.---.-----.-. 1
3609 // "linear" simple reduce "42": 4->8->12->16
3611 // .---------------.---------------.---------------.---------------. nr
3612 // | \ | / | \ | / |
3613 // | \ .-------.-------. / | \ .-------.-------. / |
3614 // | | | | | | | | |
3615 // .-------.-------.-------.-------.-------.-------.-------.-------.
3616 // | / \ | / \ | / \ | / \ |
3617 // | / \.----.----./ \ | / \.----.----./ \ | i
3618 // | / | | | \ | / | | | \ |
3619 // .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
3620 // | / / \ | / \ \ | / / \ | / \ \ |
3621 // | / / .-.-. \ \ | / / .-.-. \ \ |
3622 // | / / / | \ \ \ | / / / | \ \ \ |
3623 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
3626 // nt = 5, nb = 7, nr = 4
3627 //int delta_all = 2;
3628 //int delta_one_col = 6;
3630 //int remainder = 2;
3631 //if (remainder > 0) nb_col++;
3633 //int free_left = 1;
3635 //int free_middle = 4;
3637 int delta_all = nb - nt;
3638 int delta_one_col = (nr - 1) * 2;
3639 int nb_col = delta_all / delta_one_col;
3640 int remainder = delta_all - nb_col * delta_one_col;
3641 if (remainder > 0) {
3644 const int col_top_size = is_lin_42 ? 2 : 1;
3645 int free_left = ((nt - 1) - nb_col * col_top_size) / 2;
3646 free_left += nr - 2;
3647 int free_middle = (nr - 2) * 2;
3648 if (remainder > 0 && nb_col == 1) {
3649 int nb_rows_short_col = remainder / 2;
3650 int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
3651 free_left -= nb_rows_thrown;
3654 // nt = 5, nb = 17, nr = 4
3655 //int delta_all = 12;
3656 //int delta_one_col = 6;
3658 //int remainder = 0;
3659 //int free_left = 2;
3660 //int free_middle = 4;
3662 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3664 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3666 for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) // layer by layer
3668 // to calculate normalized parameter, we must know number of points in next layer
3669 int nb_next = curr_base_len - nb_col * 2;
3670 if (remainder > 0 && i > remainder / 2)
3671 // take into account short "column"
3673 if (nb_next < nt) nb_next = nt;
3675 const double y = uv_el[ i ].normParam;
3677 if ( i + 1 == nr ) // top
3684 next_base.resize( nb_next, nullUVPtStruct );
3685 next_base.front() = uv_el[i];
3686 next_base.back() = uv_er[i];
3688 // compute normalized param u
3689 double du = 1. / ( nb_next - 1 );
3690 next_base[0].normParam = 0.;
3691 for ( j = 1; j < nb_next; ++j )
3692 next_base[j].normParam = next_base[j-1].normParam + du;
3694 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3695 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3697 // not reduced left elements
3698 for (j = 0; j < free_left; j++)
3701 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3703 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3705 myHelper->AddFace(curr_base[ j ].node,
3706 curr_base[ j+1 ].node,
3708 next_base[ next_base_len-1 ].node);
3711 for (int icol = 1; icol <= nb_col; icol++) {
3713 if (remainder > 0 && icol == nb_col && i > remainder / 2)
3714 // stop short "column"
3718 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3720 j += reduce_grp_size;
3722 // not reduced middle elements
3723 if (icol < nb_col) {
3724 if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3725 // pass middle elements before stopped short "column"
3728 int free_add = free_middle;
3729 if (remainder > 0 && icol == nb_col - 1)
3730 // next "column" is short
3731 free_add -= (nr - 1) - (remainder / 2);
3733 for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3734 // f (i + 1, j + imiddle)
3735 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3737 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3739 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3740 curr_base[ j +imiddle ].node,
3742 next_base[ next_base_len-1 ].node);
3748 // not reduced right elements
3749 for (; j < curr_base_len-1; j++) {
3751 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3753 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3755 myHelper->AddFace(curr_base[ j ].node,
3756 curr_base[ j+1 ].node,
3758 next_base[ next_base_len-1 ].node);
3761 curr_base_len = next_base_len + 1;
3763 curr_base.swap( next_base );
3766 } // end "linear" simple reduce
3771 } // end Simple Reduce implementation
3777 //================================================================================
3778 namespace // data for smoothing
3781 // --------------------------------------------------------------------------------
3783 * \brief Structure used to check validity of node position after smoothing.
3784 * It holds two nodes connected to a smoothed node and belonging to
3791 TTriangle( TSmoothNode* n1=0, TSmoothNode* n2=0 ): _n1(n1), _n2(n2) {}
3793 inline bool IsForward( gp_UV uv ) const;
3795 // --------------------------------------------------------------------------------
3797 * \brief Data of a smoothed node
3803 vector< TTriangle > _triangles; // if empty, then node is not movable
3805 // --------------------------------------------------------------------------------
3806 inline bool TTriangle::IsForward( gp_UV uv ) const
3808 gp_Vec2d v1( uv, _n1->_uv ), v2( uv, _n2->_uv );
3812 //================================================================================
3814 * \brief Returns area of a triangle
3816 //================================================================================
3818 double getArea( const gp_UV uv1, const gp_UV uv2, const gp_UV uv3 )
3820 gp_XY v1 = uv1 - uv2, v2 = uv3 - uv2;
3826 //================================================================================
3828 * \brief Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3830 * WARNING: this method must be called AFTER retrieving UVPtStruct's from quad
3832 //================================================================================
3834 void StdMeshers_Quadrangle_2D::updateDegenUV(FaceQuadStruct::Ptr quad)
3838 // Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3839 // --------------------------------------------------------------------------
3840 for ( unsigned i = 0; i < quad->side.size(); ++i )
3842 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3844 // find which end of the side is on degenerated shape
3846 if ( myHelper->IsDegenShape( uvVec[0].node->getshapeId() ))
3848 else if ( myHelper->IsDegenShape( uvVec.back().node->getshapeId() ))
3849 degenInd = uvVec.size() - 1;
3853 // find another side sharing the degenerated shape
3854 bool isPrev = ( degenInd == 0 );
3855 if ( i >= QUAD_TOP_SIDE )
3857 int i2 = ( isPrev ? ( i + 3 ) : ( i + 1 )) % 4;
3858 const vector<UVPtStruct>& uvVec2 = quad->side[ i2 ].GetUVPtStruct();
3860 if ( uvVec[ degenInd ].node == uvVec2.front().node )
3862 else if ( uvVec[ degenInd ].node == uvVec2.back().node )
3863 degenInd2 = uvVec2.size() - 1;
3865 throw SALOME_Exception( LOCALIZED( "Logical error" ));
3867 // move UV in the middle
3868 uvPtStruct& uv1 = const_cast<uvPtStruct&>( uvVec [ degenInd ]);
3869 uvPtStruct& uv2 = const_cast<uvPtStruct&>( uvVec2[ degenInd2 ]);
3870 uv1.u = uv2.u = 0.5 * ( uv1.u + uv2.u );
3871 uv1.v = uv2.v = 0.5 * ( uv1.v + uv2.v );
3874 else if ( quad->side.size() == 4 /*&& myQuadType == QUAD_STANDARD*/)
3876 // Set number of nodes on a degenerated side to be same as on an opposite side
3877 // ----------------------------------------------------------------------------
3878 for ( unsigned i = 0; i < quad->side.size(); ++i )
3880 StdMeshers_FaceSidePtr degSide = quad->side[i];
3881 if ( !myHelper->IsDegenShape( degSide->EdgeID(0) ))
3883 StdMeshers_FaceSidePtr oppSide = quad->side[( i+2 ) % quad->side.size() ];
3884 if ( degSide->NbSegments() == oppSide->NbSegments() )
3887 // make new side data
3888 const vector<UVPtStruct>& uvVecDegOld = degSide->GetUVPtStruct();
3889 const SMDS_MeshNode* n = uvVecDegOld[0].node;
3890 Handle(Geom2d_Curve) c2d = degSide->Curve2d(0);
3891 double f = degSide->FirstU(0), l = degSide->LastU(0);
3892 gp_Pnt2d p1 = uvVecDegOld.front().UV();
3893 gp_Pnt2d p2 = uvVecDegOld.back().UV();
3895 quad->side[i] = StdMeshers_FaceSide::New( oppSide.get(), n, &p1, &p2, c2d, f, l );
3899 //================================================================================
3901 * \brief Perform smoothing of 2D elements on a FACE with ignored degenerated EDGE
3903 //================================================================================
3905 void StdMeshers_Quadrangle_2D::smooth (FaceQuadStruct::Ptr quad)
3907 if ( !myNeedSmooth ) return;
3909 // Get nodes to smooth
3911 // TODO: do not smooth fixed nodes
3913 typedef map< const SMDS_MeshNode*, TSmoothNode, TIDCompare > TNo2SmooNoMap;
3914 TNo2SmooNoMap smooNoMap;
3916 const TopoDS_Face& geomFace = TopoDS::Face( myHelper->GetSubShape() );
3917 Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
3918 double U1, U2, V1, V2;
3919 surface->Bounds(U1, U2, V1, V2);
3920 GeomAPI_ProjectPointOnSurf proj;
3921 proj.Init( surface, U1, U2, V1, V2, BRep_Tool::Tolerance( geomFace ) );
3923 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
3924 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( geomFace );
3925 SMDS_NodeIteratorPtr nIt = fSubMesh->GetNodes();
3926 while ( nIt->more() ) // loop on nodes bound to a FACE
3928 const SMDS_MeshNode* node = nIt->next();
3929 TSmoothNode & sNode = smooNoMap[ node ];
3930 sNode._uv = myHelper->GetNodeUV( geomFace, node );
3931 sNode._xyz = SMESH_TNodeXYZ( node );
3933 // set sNode._triangles
3934 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator( SMDSAbs_Face );
3935 while ( fIt->more() )
3937 const SMDS_MeshElement* face = fIt->next();
3938 const int nbN = face->NbCornerNodes();
3939 const int nInd = face->GetNodeIndex( node );
3940 const int prevInd = myHelper->WrapIndex( nInd - 1, nbN );
3941 const int nextInd = myHelper->WrapIndex( nInd + 1, nbN );
3942 const SMDS_MeshNode* prevNode = face->GetNode( prevInd );
3943 const SMDS_MeshNode* nextNode = face->GetNode( nextInd );
3944 sNode._triangles.push_back( TTriangle( & smooNoMap[ prevNode ],
3945 & smooNoMap[ nextNode ]));
3948 // set _uv of smooth nodes on FACE boundary
3949 for ( unsigned i = 0; i < quad->side.size(); ++i )
3951 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3952 for ( unsigned j = 0; j < uvVec.size(); ++j )
3954 TSmoothNode & sNode = smooNoMap[ uvVec[j].node ];
3955 sNode._uv = uvVec[j].UV();
3956 sNode._xyz = SMESH_TNodeXYZ( uvVec[j].node );
3960 // define refernce orientation in 2D
3961 TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
3962 for ( ; n2sn != smooNoMap.end(); ++n2sn )
3963 if ( !n2sn->second._triangles.empty() )
3965 if ( n2sn == smooNoMap.end() ) return;
3966 const TSmoothNode & sampleNode = n2sn->second;
3967 const bool refForward = ( sampleNode._triangles[0].IsForward( sampleNode._uv ));
3971 for ( int iLoop = 0; iLoop < 5; ++iLoop )
3973 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
3975 TSmoothNode& sNode = n2sn->second;
3976 if ( sNode._triangles.empty() )
3977 continue; // not movable node
3980 bool isValid = false;
3981 bool use3D = ( iLoop > 2 ); // 3 loops in 2D and 2, in 3D
3985 // compute a new XYZ
3986 gp_XYZ newXYZ (0,0,0);
3987 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
3988 newXYZ += sNode._triangles[i]._n1->_xyz;
3989 newXYZ /= sNode._triangles.size();
3991 // compute a new UV by projection
3992 proj.Perform( newXYZ );
3993 isValid = ( proj.IsDone() && proj.NbPoints() > 0 );
3996 // check validity of the newUV
3997 Quantity_Parameter u,v;
3998 proj.LowerDistanceParameters( u, v );
3999 newUV.SetCoord( u, v );
4000 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
4001 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4006 // compute a new UV by averaging
4007 newUV.SetCoord(0.,0.);
4008 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
4009 newUV += sNode._triangles[i]._n1->_uv;
4010 newUV /= sNode._triangles.size();
4012 // check validity of the newUV
4014 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
4015 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4020 sNode._xyz = surface->Value( newUV.X(), newUV.Y() ).XYZ();
4025 // Set new XYZ to the smoothed nodes
4027 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
4029 TSmoothNode& sNode = n2sn->second;
4030 if ( sNode._triangles.empty() )
4031 continue; // not movable node
4033 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( n2sn->first );
4034 gp_Pnt xyz = surface->Value( sNode._uv.X(), sNode._uv.Y() );
4035 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4038 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( sNode._uv.X(), sNode._uv.Y() )));
4041 // Move medium nodes in quadratic mesh
4042 if ( _quadraticMesh )
4044 const TLinkNodeMap& links = myHelper->GetTLinkNodeMap();
4045 TLinkNodeMap::const_iterator linkIt = links.begin();
4046 for ( ; linkIt != links.end(); ++linkIt )
4048 const SMESH_TLink& link = linkIt->first;
4049 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( linkIt->second );
4051 if ( node->getshapeId() != myHelper->GetSubShapeID() )
4052 continue; // medium node is on EDGE or VERTEX
4054 gp_XY uv1 = myHelper->GetNodeUV( geomFace, link.node1(), node );
4055 gp_XY uv2 = myHelper->GetNodeUV( geomFace, link.node2(), node );
4057 gp_XY uv = myHelper->GetMiddleUV( surface, uv1, uv2 );
4058 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( uv.X(), uv.Y() )));
4060 gp_Pnt xyz = surface->Value( uv.X(), uv.Y() );
4061 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4066 //================================================================================
4068 * \brief Checks validity of generated faces
4070 //================================================================================
4072 bool StdMeshers_Quadrangle_2D::check()
4074 const bool isOK = true;
4075 if ( !myCheckOri || myQuadList.empty() || !myQuadList.front() || !myHelper )
4078 TopoDS_Face geomFace = TopoDS::Face( myHelper->GetSubShape() );
4079 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
4080 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( geomFace );
4082 if ( geomFace.Orientation() >= TopAbs_INTERNAL ) geomFace.Orientation( TopAbs_FORWARD );
4084 // Get a reference orientation sign
4089 TSideVector wireVec =
4090 StdMeshers_FaceSide::GetFaceWires( geomFace, *myHelper->GetMesh(), true, err );
4091 StdMeshers_FaceSidePtr wire = wireVec[0];
4093 // find a right angle VERTEX
4095 double maxAngle = -1e100;
4096 for ( int i = 0; i < wire->NbEdges(); ++i )
4098 int iPrev = myHelper->WrapIndex( i-1, wire->NbEdges() );
4099 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4100 const TopoDS_Edge& e2 = wire->Edge( i );
4101 double angle = myHelper->GetAngle( e1, e2, geomFace, wire->FirstVertex( i ));
4102 if (( maxAngle < angle ) &&
4103 ( 5.* M_PI/180 < angle && angle < 175.* M_PI/180 ))
4109 if ( maxAngle < -2*M_PI ) return isOK;
4111 // get a sign of 2D area of a corner face
4113 int iPrev = myHelper->WrapIndex( iVertex-1, wire->NbEdges() );
4114 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4115 const TopoDS_Edge& e2 = wire->Edge( iVertex );
4117 gp_Vec2d v1, v2; gp_Pnt2d p;
4120 bool rev = ( e1.Orientation() == TopAbs_REVERSED );
4121 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e1, geomFace, u[0], u[1] );
4122 c->D1( u[ !rev ], p, v1 );
4127 bool rev = ( e2.Orientation() == TopAbs_REVERSED );
4128 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e2, geomFace, u[0], u[1] );
4129 c->D1( u[ rev ], p, v2 );
4140 // Look for incorrectly oriented faces
4142 std::list<const SMDS_MeshElement*> badFaces;
4144 const SMDS_MeshNode* nn [ 8 ]; // 8 is just for safety
4146 SMDS_ElemIteratorPtr fIt = fSubMesh->GetElements();
4147 while ( fIt->more() ) // loop on faces bound to a FACE
4149 const SMDS_MeshElement* f = fIt->next();
4151 const int nbN = f->NbCornerNodes();
4152 for ( int i = 0; i < nbN; ++i )
4153 nn[ i ] = f->GetNode( i );
4155 const SMDS_MeshNode* nInFace = 0;
4156 if ( myHelper->HasSeam() )
4157 for ( int i = 0; i < nbN && !nInFace; ++i )
4158 if ( !myHelper->IsSeamShape( nn[i]->getshapeId() ))
4161 for ( int i = 0; i < nbN; ++i )
4162 uv[ i ] = myHelper->GetNodeUV( geomFace, nn[i], nInFace, &toCheckUV );
4167 double sign1 = getArea( uv[0], uv[1], uv[2] );
4168 double sign2 = getArea( uv[0], uv[2], uv[3] );
4169 if ( sign1 * sign2 < 0 )
4171 sign2 = getArea( uv[1], uv[2], uv[3] );
4172 sign1 = getArea( uv[1], uv[3], uv[0] );
4173 if ( sign1 * sign2 < 0 )
4174 continue; // this should not happen
4176 if ( sign1 * okSign < 0 )
4177 badFaces.push_back ( f );
4182 double sign = getArea( uv[0], uv[1], uv[2] );
4183 if ( sign * okSign < 0 )
4184 badFaces.push_back ( f );
4191 if ( !badFaces.empty() )
4193 SMESH_subMesh* fSM = myHelper->GetMesh()->GetSubMesh( geomFace );
4194 SMESH_ComputeErrorPtr& err = fSM->GetComputeError();
4195 err.reset ( new SMESH_ComputeError( COMPERR_ALGO_FAILED,
4196 "Inverted elements generated"));
4197 err->myBadElements.swap( badFaces );
4205 /*//================================================================================
4207 * \brief Finds vertices at the most sharp face corners
4208 * \param [in] theFace - the FACE
4209 * \param [in,out] theWire - the ordered edges of the face. It can be modified to
4210 * have the first VERTEX of the first EDGE in \a vertices
4211 * \param [out] theVertices - the found corner vertices in the order corresponding to
4212 * the order of EDGEs in \a theWire
4213 * \param [out] theNbDegenEdges - nb of degenerated EDGEs in theFace
4214 * \param [in] theConsiderMesh - if \c true, only meshed VERTEXes are considered
4215 * as possible corners
4216 * \return int - number of quad sides found: 0, 3 or 4
4218 //================================================================================
4220 int StdMeshers_Quadrangle_2D::getCorners(const TopoDS_Face& theFace,
4221 SMESH_Mesh & theMesh,
4222 std::list<TopoDS_Edge>& theWire,
4223 std::vector<TopoDS_Vertex>& theVertices,
4224 int & theNbDegenEdges,
4225 const bool theConsiderMesh)
4227 theNbDegenEdges = 0;
4229 SMESH_MesherHelper helper( theMesh );
4230 StdMeshers_FaceSide faceSide( theFace, theWire, &theMesh, /*isFwd=*/true, /*skipMedium=*/true);
4232 // sort theVertices by angle
4233 multimap<double, TopoDS_Vertex> vertexByAngle;
4234 TopTools_DataMapOfShapeReal angleByVertex;
4235 TopoDS_Edge prevE = theWire.back();
4236 if ( SMESH_Algo::isDegenerated( prevE ))
4238 list<TopoDS_Edge>::reverse_iterator edge = ++theWire.rbegin();
4239 while ( SMESH_Algo::isDegenerated( *edge ))
4241 if ( edge == theWire.rend() )
4245 list<TopoDS_Edge>::iterator edge = theWire.begin();
4246 for ( int iE = 0; edge != theWire.end(); ++edge, ++iE )
4248 if ( SMESH_Algo::isDegenerated( *edge ))
4253 if ( !theConsiderMesh || faceSide.VertexNode( iE ))
4255 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4256 double angle = SMESH_MesherHelper::GetAngle( prevE, *edge, theFace, v );
4257 vertexByAngle.insert( make_pair( angle, v ));
4258 angleByVertex.Bind( v, angle );
4263 // find out required nb of corners (3 or 4)
4265 TopoDS_Shape triaVertex = helper.GetMeshDS()->IndexToShape( myTriaVertexID );
4266 if ( !triaVertex.IsNull() &&
4267 triaVertex.ShapeType() == TopAbs_VERTEX &&
4268 helper.IsSubShape( triaVertex, theFace ) &&
4269 ( vertexByAngle.size() != 4 || vertexByAngle.begin()->first < 5 * M_PI/180. ))
4272 triaVertex.Nullify();
4274 // check nb of available corners
4275 if ( faceSide.NbEdges() < nbCorners )
4276 return error(COMPERR_BAD_SHAPE,
4277 TComm("Face must have 4 sides but not ") << faceSide.NbEdges() );
4279 const int nbSegments = Max( faceSide.NbPoints()-1, faceSide.NbSegments() );
4280 if ( nbSegments < nbCorners )
4281 return error(COMPERR_BAD_INPUT_MESH, TComm("Too few boundary nodes: ") << nbSegments);
4283 if ( nbCorners == 3 )
4285 if ( vertexByAngle.size() < 3 )
4286 return error(COMPERR_BAD_SHAPE,
4287 TComm("Face must have 3 sides but not ") << vertexByAngle.size() );
4291 if ( vertexByAngle.size() == 3 && theNbDegenEdges == 0 )
4293 if ( myTriaVertexID < 1 )
4294 return error(COMPERR_BAD_PARMETERS,
4295 "No Base vertex provided for a trilateral geometrical face");
4297 TComm comment("Invalid Base vertex: ");
4298 comment << myTriaVertexID << " its ID is not among [ ";
4299 multimap<double, TopoDS_Vertex>::iterator a2v = vertexByAngle.begin();
4300 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4301 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4302 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << " ]";
4303 return error(COMPERR_BAD_PARMETERS, comment );
4305 if ( vertexByAngle.size() + ( theNbDegenEdges > 0 ) < 4 &&
4306 vertexByAngle.size() + theNbDegenEdges != 4 )
4307 return error(COMPERR_BAD_SHAPE,
4308 TComm("Face must have 4 sides but not ") << vertexByAngle.size() );
4311 // put all corner vertices in a map
4312 TopTools_MapOfShape vMap;
4313 if ( nbCorners == 3 )
4314 vMap.Add( triaVertex );
4315 multimap<double, TopoDS_Vertex>::reverse_iterator a2v = vertexByAngle.rbegin();
4316 for ( int iC = 0; a2v != vertexByAngle.rend() && iC < nbCorners; ++a2v, ++iC )
4317 vMap.Add( (*a2v).second );
4319 // check if there are possible variations in choosing corners
4320 bool haveVariants = false;
4321 if ( vertexByAngle.size() > nbCorners )
4323 double lostAngle = a2v->first;
4324 double lastAngle = ( --a2v, a2v->first );
4325 haveVariants = ( lostAngle * 1.1 >= lastAngle );
4328 const double angleTol = 5.* M_PI/180;
4329 myCheckOri = ( vertexByAngle.size() > nbCorners ||
4330 vertexByAngle.begin()->first < angleTol );
4332 // make theWire begin from a corner vertex or triaVertex
4333 if ( nbCorners == 3 )
4334 while ( !triaVertex.IsSame( ( helper.IthVertex( 0, theWire.front() ))) ||
4335 SMESH_Algo::isDegenerated( theWire.front() ))
4336 theWire.splice( theWire.end(), theWire, theWire.begin() );
4338 while ( !vMap.Contains( helper.IthVertex( 0, theWire.front() )) ||
4339 SMESH_Algo::isDegenerated( theWire.front() ))
4340 theWire.splice( theWire.end(), theWire, theWire.begin() );
4342 // fill the result vector and prepare for its refinement
4343 theVertices.clear();
4344 vector< double > angles;
4345 vector< TopoDS_Edge > edgeVec;
4346 vector< int > cornerInd, nbSeg;
4348 angles .reserve( vertexByAngle.size() );
4349 edgeVec.reserve( vertexByAngle.size() );
4350 nbSeg .reserve( vertexByAngle.size() );
4351 cornerInd.reserve( nbCorners );
4352 for ( edge = theWire.begin(); edge != theWire.end(); ++edge )
4354 if ( SMESH_Algo::isDegenerated( *edge ))
4356 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4357 bool isCorner = vMap.Contains( v );
4360 theVertices.push_back( v );
4361 cornerInd.push_back( angles.size() );
4363 angles .push_back( angleByVertex.IsBound( v ) ? angleByVertex( v ) : -M_PI );
4364 edgeVec.push_back( *edge );
4365 if ( theConsiderMesh && haveVariants )
4367 if ( SMESHDS_SubMesh* sm = helper.GetMeshDS()->MeshElements( *edge ))
4368 nbSeg.push_back( sm->NbNodes() + 1 );
4370 nbSeg.push_back( 0 );
4371 nbSegTot += nbSeg.back();
4375 // refine the result vector - make sides equal by length if
4376 // there are several equal angles
4379 if ( nbCorners == 3 )
4380 angles[0] = 2 * M_PI; // not to move the base triangle VERTEX
4382 // here we refer to VERTEX'es and EDGEs by indices in angles and edgeVec vectors
4383 typedef int TGeoIndex;
4385 // for each vertex find a vertex till which there are nbSegHalf segments
4386 const int nbSegHalf = ( nbSegTot % 2 || nbCorners == 3 ) ? 0 : nbSegTot / 2;
4387 vector< TGeoIndex > halfDivider( angles.size(), -1 );
4388 int nbHalfDividers = 0;
4391 // get min angle of corners
4392 double minAngle = 10.;
4393 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4394 minAngle = Min( minAngle, angles[ cornerInd[ iC ]]);
4396 // find halfDivider's
4397 for ( TGeoIndex iV1 = 0; iV1 < TGeoIndex( angles.size() ); ++iV1 )
4400 TGeoIndex iV2 = iV1;
4402 nbSegs += nbSeg[ iV2 ];
4403 iV2 = helper.WrapIndex( iV2 + 1, nbSeg.size() );
4404 } while ( nbSegs < nbSegHalf );
4406 if ( nbSegs == nbSegHalf &&
4407 angles[ iV1 ] + angleTol >= minAngle &&
4408 angles[ iV2 ] + angleTol >= minAngle )
4410 halfDivider[ iV1 ] = iV2;
4416 set< TGeoIndex > refinedCorners, treatedCorners;
4417 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4419 TGeoIndex iV = cornerInd[iC];
4420 if ( !treatedCorners.insert( iV ).second )
4422 list< TGeoIndex > equVerts; // inds of vertices that can become corners
4423 equVerts.push_back( iV );
4424 int nbC[2] = { 0, 0 };
4425 // find equal angles backward and forward from the iV-th corner vertex
4426 for ( int isFwd = 0; isFwd < 2; ++isFwd )
4428 int dV = isFwd ? +1 : -1;
4429 int iCNext = helper.WrapIndex( iC + dV, cornerInd.size() );
4430 TGeoIndex iVNext = helper.WrapIndex( iV + dV, angles.size() );
4431 while ( iVNext != iV )
4433 bool equal = Abs( angles[iV] - angles[iVNext] ) < angleTol;
4435 equVerts.insert( isFwd ? equVerts.end() : equVerts.begin(), iVNext );
4436 if ( iVNext == cornerInd[ iCNext ])
4440 if ( angles[iV] < angles[iVNext] )
4441 refinedCorners.insert( iVNext );
4445 treatedCorners.insert( cornerInd[ iCNext ] );
4446 iCNext = helper.WrapIndex( iCNext + dV, cornerInd.size() );
4448 iVNext = helper.WrapIndex( iVNext + dV, angles.size() );
4451 break; // all angles equal
4454 const bool allCornersSame = ( nbC[0] == 3 );
4455 if ( allCornersSame && nbHalfDividers > 0 )
4457 // select two halfDivider's as corners
4458 TGeoIndex hd1, hd2 = -1;
4460 for ( iC2 = 0; iC2 < cornerInd.size() && hd2 < 0; ++iC2 )
4462 hd1 = cornerInd[ iC2 ];
4463 hd2 = halfDivider[ hd1 ];
4464 if ( std::find( equVerts.begin(), equVerts.end(), hd2 ) == equVerts.end() )
4465 hd2 = -1; // hd2-th vertex can't become a corner
4471 angles[ hd1 ] = 2 * M_PI; // make hd1-th vertex no more "equal"
4472 angles[ hd2 ] = 2 * M_PI;
4473 refinedCorners.insert( hd1 );
4474 refinedCorners.insert( hd2 );
4475 treatedCorners = refinedCorners;
4477 equVerts.push_front( equVerts.back() );
4478 equVerts.push_back( equVerts.front() );
4479 list< TGeoIndex >::iterator hdPos =
4480 std::find( equVerts.begin(), equVerts.end(), hd2 );
4481 if ( hdPos == equVerts.end() ) break;
4482 cornerInd[ helper.WrapIndex( iC2 + 0, cornerInd.size()) ] = hd1;
4483 cornerInd[ helper.WrapIndex( iC2 + 1, cornerInd.size()) ] = *( --hdPos );
4484 cornerInd[ helper.WrapIndex( iC2 + 2, cornerInd.size()) ] = hd2;
4485 cornerInd[ helper.WrapIndex( iC2 + 3, cornerInd.size()) ] = *( ++hdPos, ++hdPos );
4487 theVertices[ 0 ] = helper.IthVertex( 0, edgeVec[ cornerInd[0] ]);
4488 theVertices[ 1 ] = helper.IthVertex( 0, edgeVec[ cornerInd[1] ]);
4489 theVertices[ 2 ] = helper.IthVertex( 0, edgeVec[ cornerInd[2] ]);
4490 theVertices[ 3 ] = helper.IthVertex( 0, edgeVec[ cornerInd[3] ]);
4496 // move corners to make sides equal by length
4497 int nbEqualV = equVerts.size();
4498 int nbExcessV = nbEqualV - ( 1 + nbC[0] + nbC[1] );
4499 if ( nbExcessV > 0 ) // there is nbExcessV vertices that can become corners
4501 // calculate normalized length of each "side" enclosed between neighbor equVerts
4502 vector< double > accuLength;
4503 double totalLen = 0;
4504 vector< TGeoIndex > evVec( equVerts.begin(), equVerts.end() );
4506 TGeoIndex iE = cornerInd[ helper.WrapIndex( iC - nbC[0] - 1, cornerInd.size() )];
4507 TGeoIndex iEEnd = cornerInd[ helper.WrapIndex( iC + nbC[1] + 1, cornerInd.size() )];
4508 while ( accuLength.size() < nbEqualV + int( !allCornersSame ) )
4510 // accumulate length of edges before iEV-th equal vertex
4511 accuLength.push_back( totalLen );
4513 accuLength.back() += SMESH_Algo::EdgeLength( edgeVec[ iE ]);
4514 iE = helper.WrapIndex( iE + 1, edgeVec.size());
4515 if ( iEV < evVec.size() && iE == evVec[ iEV ] ) {
4517 break; // equal vertex reached
4520 while( iE != iEEnd );
4521 totalLen = accuLength.back();
4523 accuLength.resize( equVerts.size() );
4524 for ( size_t iS = 0; iS < accuLength.size(); ++iS )
4525 accuLength[ iS ] /= totalLen;
4527 // find equVerts most close to the ideal sub-division of all sides
4529 int iCorner = helper.WrapIndex( iC - nbC[0], cornerInd.size() );
4530 int nbSides = Min( nbCorners, 2 + nbC[0] + nbC[1] );
4531 for ( int iS = 1; iS < nbSides; ++iS, ++iBestEV )
4533 double idealLen = iS / double( nbSides );
4534 double d, bestDist = 2.;
4535 for ( iEV = iBestEV; iEV < accuLength.size(); ++iEV )
4537 d = Abs( idealLen - accuLength[ iEV ]);
4539 // take into account presence of a coresponding halfDivider
4540 const double cornerWgt = 0.5 / nbSides;
4541 const double vertexWgt = 0.25 / nbSides;
4542 TGeoIndex hd = halfDivider[ evVec[ iEV ]];
4545 else if( refinedCorners.count( hd ))
4550 // choose vertex with the best d
4557 if ( iBestEV > iS-1 + nbExcessV )
4558 iBestEV = iS-1 + nbExcessV;
4559 theVertices[ iCorner ] = helper.IthVertex( 0, edgeVec[ evVec[ iBestEV ]]);
4560 refinedCorners.insert( evVec[ iBestEV ]);
4561 iCorner = helper.WrapIndex( iCorner + 1, cornerInd.size() );
4564 } // if ( nbExcessV > 0 )
4567 refinedCorners.insert( cornerInd[ iC ]);
4569 } // loop on cornerInd
4571 // make theWire begin from the cornerInd[0]-th EDGE
4572 while ( !theWire.front().IsSame( edgeVec[ cornerInd[0] ]))
4573 theWire.splice( theWire.begin(), theWire, --theWire.end() );
4575 } // if ( haveVariants )
4580 //================================================================================
4582 * \brief Constructor of a side of quad
4584 //================================================================================
4586 FaceQuadStruct::Side::Side(StdMeshers_FaceSidePtr theGrid)
4587 : grid(theGrid), nbNodeOut(0), from(0), to(theGrid ? theGrid->NbPoints() : 0 ), di(1)
4591 //=============================================================================
4593 * \brief Constructor of a quad
4595 //=============================================================================
4597 FaceQuadStruct::FaceQuadStruct(const TopoDS_Face& F, const std::string& theName)
4598 : face( F ), name( theName )
4603 //================================================================================
4605 * \brief Fills myForcedPnts
4607 //================================================================================
4609 bool StdMeshers_Quadrangle_2D::getEnforcedUV()
4611 myForcedPnts.clear();
4612 if ( !myParams ) return true; // missing hypothesis
4614 std::vector< TopoDS_Shape > shapes;
4615 std::vector< gp_Pnt > points;
4616 myParams->GetEnforcedNodes( shapes, points );
4618 TopTools_IndexedMapOfShape vMap;
4619 for ( size_t i = 0; i < shapes.size(); ++i )
4620 if ( !shapes[i].IsNull() )
4621 TopExp::MapShapes( shapes[i], TopAbs_VERTEX, vMap );
4623 size_t nbPoints = points.size();
4624 for ( int i = 1; i <= vMap.Extent(); ++i )
4625 points.push_back( BRep_Tool::Pnt( TopoDS::Vertex( vMap( i ))));
4627 // find out if all points must be in the FACE, which is so if
4628 // myParams is a local hypothesis on the FACE being meshed
4629 bool isStrictCheck = false;
4631 SMESH_HypoFilter paramFilter( SMESH_HypoFilter::Is( myParams ));
4632 TopoDS_Shape assignedTo;
4633 if ( myHelper->GetMesh()->GetHypothesis( myHelper->GetSubShape(),
4637 isStrictCheck = ( assignedTo.IsSame( myHelper->GetSubShape() ));
4640 multimap< double, ForcedPoint > sortedFP; // sort points by distance from EDGEs
4642 Standard_Real u1,u2,v1,v2;
4643 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4644 const double tol = BRep_Tool::Tolerance( face );
4645 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4646 surf->Bounds( u1,u2,v1,v2 );
4647 GeomAPI_ProjectPointOnSurf project;
4648 project.Init(surf, u1,u2, v1,v2, tol );
4650 BRepBndLib::Add( face, bbox );
4651 double farTol = 0.01 * sqrt( bbox.SquareExtent() );
4653 for ( size_t iP = 0; iP < points.size(); ++iP )
4655 project.Perform( points[ iP ]);
4656 if ( !project.IsDone() )
4658 if ( isStrictCheck && iP < nbPoints )
4660 (TComm("Projection of an enforced point to the face failed - (")
4661 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4664 if ( project.LowerDistance() > farTol )
4666 if ( isStrictCheck && iP < nbPoints )
4668 (COMPERR_BAD_PARMETERS, TComm("An enforced point is too far from the face, dist = ")
4669 << project.LowerDistance() << " - ("
4670 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4673 Quantity_Parameter u, v;
4674 project.LowerDistanceParameters(u, v);
4675 gp_Pnt2d uv( u, v );
4676 BRepClass_FaceClassifier clsf ( face, uv, tol );
4677 switch ( clsf.State() ) {
4680 double edgeDist = ( Min( Abs( u - u1 ), Abs( u - u2 )) +
4681 Min( Abs( v - v1 ), Abs( v - v2 )));
4684 fp.xyz = points[ iP ].XYZ();
4685 if ( iP >= nbPoints )
4686 fp.vertex = TopoDS::Vertex( vMap( iP - nbPoints + 1 ));
4688 sortedFP.insert( make_pair( edgeDist, fp ));
4693 if ( isStrictCheck && iP < nbPoints )
4695 (COMPERR_BAD_PARMETERS, TComm("An enforced point is out of the face boundary - ")
4696 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4701 if ( isStrictCheck && iP < nbPoints )
4703 (COMPERR_BAD_PARMETERS, TComm("An enforced point is on the face boundary - ")
4704 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4709 if ( isStrictCheck && iP < nbPoints )
4711 (TComm("Classification of an enforced point ralative to the face boundary failed - ")
4712 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4717 multimap< double, ForcedPoint >::iterator d2uv = sortedFP.begin();
4718 for ( ; d2uv != sortedFP.end(); ++d2uv )
4719 myForcedPnts.push_back( (*d2uv).second );
4724 //================================================================================
4726 * \brief Splits quads by adding points of enforced nodes and create nodes on
4727 * the sides shared by quads
4729 //================================================================================
4731 bool StdMeshers_Quadrangle_2D::addEnforcedNodes()
4733 // if ( myForcedPnts.empty() )
4736 // make a map of quads sharing a side
4737 map< StdMeshers_FaceSidePtr, vector< FaceQuadStruct::Ptr > > quadsBySide;
4738 list< FaceQuadStruct::Ptr >::iterator quadIt = myQuadList.begin();
4739 for ( ; quadIt != myQuadList.end(); ++quadIt )
4740 for ( size_t iSide = 0; iSide < (*quadIt)->side.size(); ++iSide )
4742 if ( !setNormalizedGrid( *quadIt ))
4744 quadsBySide[ (*quadIt)->side[iSide] ].push_back( *quadIt );
4747 SMESH_Mesh* mesh = myHelper->GetMesh();
4748 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
4749 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4750 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4752 for ( size_t iFP = 0; iFP < myForcedPnts.size(); ++iFP )
4754 bool isNodeEnforced = false;
4756 // look for a quad enclosing a enforced point
4757 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4759 FaceQuadStruct::Ptr quad = *quadIt;
4760 if ( !setNormalizedGrid( *quadIt ))
4763 if ( !quad->findCell( myForcedPnts[ iFP ], i, j ))
4766 // a grid cell is found, select a node of the cell to move
4767 // to the enforced point to and to split the quad at
4768 multimap< double, pair< int, int > > ijByDist;
4769 for ( int di = 0; di < 2; ++di )
4770 for ( int dj = 0; dj < 2; ++dj )
4772 double dist2 = ( myForcedPnts[ iFP ].uv - quad->UVPt( i+di,j+dj ).UV() ).SquareModulus();
4773 ijByDist.insert( make_pair( dist2, make_pair( di,dj )));
4775 // try all nodes starting from the closest one
4776 set< FaceQuadStruct::Ptr > changedQuads;
4777 multimap< double, pair< int, int > >::iterator d2ij = ijByDist.begin();
4778 for ( ; !isNodeEnforced && d2ij != ijByDist.end(); ++d2ij )
4780 int di = d2ij->second.first;
4781 int dj = d2ij->second.second;
4783 // check if a node is at a side
4785 if ( dj== 0 && j == 0 )
4786 iSide = QUAD_BOTTOM_SIDE;
4787 else if ( dj == 1 && j+2 == quad->jSize )
4788 iSide = QUAD_TOP_SIDE;
4789 else if ( di == 0 && i == 0 )
4790 iSide = QUAD_LEFT_SIDE;
4791 else if ( di == 1 && i+2 == quad->iSize )
4792 iSide = QUAD_RIGHT_SIDE;
4794 if ( iSide > -1 ) // ----- node is at a side
4796 FaceQuadStruct::Side& side = quad->side[ iSide ];
4797 // check if this node can be moved
4798 if ( quadsBySide[ side ].size() < 2 )
4799 continue; // its a face boundary -> can't move the node
4801 int quadNodeIndex = ( iSide % 2 ) ? j : i;
4802 int sideNodeIndex = side.ToSideIndex( quadNodeIndex );
4803 if ( side.IsForced( sideNodeIndex ))
4805 // the node is already moved to another enforced point
4806 isNodeEnforced = quad->isEqual( myForcedPnts[ iFP ], i, j );
4809 // make a node of a side forced
4810 vector<UVPtStruct>& points = (vector<UVPtStruct>&) side.GetUVPtStruct();
4811 points[ sideNodeIndex ].u = myForcedPnts[ iFP ].U();
4812 points[ sideNodeIndex ].v = myForcedPnts[ iFP ].V();
4814 updateSideUV( side, sideNodeIndex, quadsBySide );
4816 // update adjacent sides
4817 set< StdMeshers_FaceSidePtr > updatedSides;
4818 updatedSides.insert( side );
4819 for ( size_t i = 0; i < side.contacts.size(); ++i )
4820 if ( side.contacts[i].point == sideNodeIndex )
4822 const vector< FaceQuadStruct::Ptr >& adjQuads =
4823 quadsBySide[ *side.contacts[i].other_side ];
4824 if ( adjQuads.size() > 1 &&
4825 updatedSides.insert( * side.contacts[i].other_side ).second )
4827 updateSideUV( *side.contacts[i].other_side,
4828 side.contacts[i].other_point,
4831 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4833 const vector< FaceQuadStruct::Ptr >& adjQuads = quadsBySide[ side ];
4834 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4836 isNodeEnforced = true;
4838 else // ------------------ node is inside the quad
4842 // make a new side passing through IJ node and split the quad
4843 int indForced, iNewSide;
4844 if ( quad->iSize < quad->jSize ) // split vertically
4846 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/true );
4848 iNewSide = splitQuad( quad, i, 0 );
4852 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/false );
4854 iNewSide = splitQuad( quad, 0, j );
4856 FaceQuadStruct::Ptr newQuad = myQuadList.back();
4857 FaceQuadStruct::Side& newSide = newQuad->side[ iNewSide ];
4859 newSide.forced_nodes.insert( indForced );
4860 quad->side[( iNewSide+2 ) % 4 ].forced_nodes.insert( indForced );
4862 quadsBySide[ newSide ].push_back( quad );
4863 quadsBySide[ newQuad->side[0] ].push_back( newQuad );
4864 quadsBySide[ newQuad->side[1] ].push_back( newQuad );
4865 quadsBySide[ newQuad->side[2] ].push_back( newQuad );
4866 quadsBySide[ newQuad->side[3] ].push_back( newQuad );
4868 isNodeEnforced = true;
4870 } // end of "node is inside the quad"
4872 } // loop on nodes of the cell
4874 // remove out-of-date uv grid of changedQuads
4875 set< FaceQuadStruct::Ptr >::iterator qIt = changedQuads.begin();
4876 for ( ; qIt != changedQuads.end(); ++qIt )
4877 (*qIt)->uv_grid.clear();
4879 if ( isNodeEnforced )
4884 if ( !isNodeEnforced )
4886 if ( !myForcedPnts[ iFP ].vertex.IsNull() )
4887 return error(TComm("Unable to move any node to vertex #")
4888 <<myHelper->GetMeshDS()->ShapeToIndex( myForcedPnts[ iFP ].vertex ));
4890 return error(TComm("Unable to move any node to point ( ")
4891 << myForcedPnts[iFP].xyz.X() << ", "
4892 << myForcedPnts[iFP].xyz.Y() << ", "
4893 << myForcedPnts[iFP].xyz.Z() << " )");
4896 } // loop on enforced points
4898 // Compute nodes on all sides, where not yet present
4900 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4902 FaceQuadStruct::Ptr quad = *quadIt;
4903 for ( int iSide = 0; iSide < 4; ++iSide )
4905 FaceQuadStruct::Side & side = quad->side[ iSide ];
4906 if ( side.nbNodeOut > 0 )
4907 continue; // emulated side
4908 vector< FaceQuadStruct::Ptr >& quadVec = quadsBySide[ side ];
4909 if ( quadVec.size() <= 1 )
4910 continue; // outer side
4912 bool missedNodesOnSide = false;
4913 const vector<UVPtStruct>& points = side.grid->GetUVPtStruct();
4914 for ( size_t iC = 0; iC < side.contacts.size(); ++iC )
4916 const vector<UVPtStruct>& oGrid = side.contacts[iC].other_side->grid->GetUVPtStruct();
4917 const UVPtStruct& uvPt = points[ side.contacts[iC].point ];
4918 if ( side.contacts[iC].other_point >= oGrid.size() ||
4919 side.contacts[iC].point >= points.size() )
4920 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::addEnforcedNodes(): wrong contact" );
4921 if ( oGrid[ side.contacts[iC].other_point ].node )
4922 (( UVPtStruct& ) uvPt).node = oGrid[ side.contacts[iC].other_point ].node;
4924 for ( size_t iP = 0; iP < points.size(); ++iP )
4925 if ( !points[ iP ].node )
4927 UVPtStruct& uvPnt = ( UVPtStruct& ) points[ iP ];
4928 gp_Pnt P = surf->Value( uvPnt.u, uvPnt.v );
4929 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
4930 meshDS->SetNodeOnFace( uvPnt.node, myHelper->GetSubShapeID(), uvPnt.u, uvPnt.v );
4931 missedNodesOnSide = true;
4933 if ( missedNodesOnSide )
4935 // clear uv_grid where nodes are missing
4936 for ( size_t iQ = 0; iQ < quadVec.size(); ++iQ )
4937 quadVec[ iQ ]->uv_grid.clear();
4945 //================================================================================
4947 * \brief Splits a quad at I or J. Returns an index of a new side in the new quad
4949 //================================================================================
4951 int StdMeshers_Quadrangle_2D::splitQuad(FaceQuadStruct::Ptr quad, int I, int J)
4953 FaceQuadStruct* newQuad = new FaceQuadStruct( quad->face );
4954 myQuadList.push_back( FaceQuadStruct::Ptr( newQuad ));
4956 vector<UVPtStruct> points;
4957 if ( I > 0 && I <= quad->iSize-2 )
4959 points.reserve( quad->jSize );
4960 for ( int jP = 0; jP < quad->jSize; ++jP )
4961 points.push_back( quad->UVPt( I, jP ));
4963 newQuad->side.resize( 4 );
4964 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
4965 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
4966 newQuad->side[ QUAD_TOP_SIDE ] = quad->side[ QUAD_TOP_SIDE ];
4967 newQuad->side[ QUAD_LEFT_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
4969 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_LEFT_SIDE ];
4970 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_RIGHT_SIDE ];
4972 quad->side[ QUAD_RIGHT_SIDE ] = newSide;
4974 int iBot = quad->side[ QUAD_BOTTOM_SIDE ].ToSideIndex( I );
4975 int iTop = quad->side[ QUAD_TOP_SIDE ].ToSideIndex( I );
4977 newSide.AddContact ( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
4978 newSide2.AddContact( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
4979 newSide.AddContact ( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
4980 newSide2.AddContact( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
4981 // cout << "Contact: L " << &newSide << " "<< newSide.NbPoints()
4982 // << " R " << &newSide2 << " "<< newSide2.NbPoints()
4983 // << " B " << &quad->side[ QUAD_BOTTOM_SIDE ] << " "<< quad->side[ QUAD_BOTTOM_SIDE].NbPoints()
4984 // << " T " << &quad->side[ QUAD_TOP_SIDE ] << " "<< quad->side[ QUAD_TOP_SIDE].NbPoints()<< endl;
4986 newQuad->side[ QUAD_BOTTOM_SIDE ].from = iBot;
4987 newQuad->side[ QUAD_TOP_SIDE ].from = iTop;
4988 newQuad->name = ( TComm("Right of I=") << I );
4990 quad->side[ QUAD_BOTTOM_SIDE ].to = iBot + 1;
4991 quad->side[ QUAD_TOP_SIDE ].to = iTop + 1;
4992 quad->uv_grid.clear();
4994 return QUAD_LEFT_SIDE;
4996 else if ( J > 0 && J <= quad->jSize-2 ) //// split horizontally, a new quad is below an old one
4998 points.reserve( quad->iSize );
4999 for ( int iP = 0; iP < quad->iSize; ++iP )
5000 points.push_back( quad->UVPt( iP, J ));
5002 newQuad->side.resize( 4 );
5003 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
5004 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
5005 newQuad->side[ QUAD_TOP_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
5006 newQuad->side[ QUAD_LEFT_SIDE ] = quad->side[ QUAD_LEFT_SIDE ];
5008 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_TOP_SIDE ];
5009 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_BOTTOM_SIDE ];
5011 quad->side[ QUAD_BOTTOM_SIDE ] = newSide;
5013 int iLft = quad->side[ QUAD_LEFT_SIDE ].ToSideIndex( J );
5014 int iRgt = quad->side[ QUAD_RIGHT_SIDE ].ToSideIndex( J );
5016 newSide.AddContact ( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5017 newSide2.AddContact( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5018 newSide.AddContact ( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5019 newSide2.AddContact( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5020 // cout << "Contact: T " << &newSide << " "<< newSide.NbPoints()
5021 // << " B " << &newSide2 << " "<< newSide2.NbPoints()
5022 // << " L " << &quad->side[ QUAD_LEFT_SIDE ] << " "<< quad->side[ QUAD_LEFT_SIDE].NbPoints()
5023 // << " R " << &quad->side[ QUAD_RIGHT_SIDE ] << " "<< quad->side[ QUAD_RIGHT_SIDE].NbPoints()<< endl;
5025 newQuad->side[ QUAD_RIGHT_SIDE ].to = iRgt+1;
5026 newQuad->side[ QUAD_LEFT_SIDE ].to = iLft+1;
5027 newQuad->name = ( TComm("Below J=") << J );
5029 quad->side[ QUAD_RIGHT_SIDE ].from = iRgt;
5030 quad->side[ QUAD_LEFT_SIDE ].from = iLft;
5031 quad->uv_grid.clear();
5033 return QUAD_TOP_SIDE;
5036 myQuadList.pop_back();
5040 //================================================================================
5042 * \brief Updates UV of a side after moving its node
5044 //================================================================================
5046 void StdMeshers_Quadrangle_2D::updateSideUV( FaceQuadStruct::Side& side,
5048 const TQuadsBySide& quadsBySide,
5053 side.forced_nodes.insert( iForced );
5055 // update parts of the side before and after iForced
5057 set<int>::iterator iIt = side.forced_nodes.upper_bound( iForced );
5058 int iEnd = Min( side.NbPoints()-1, ( iIt == side.forced_nodes.end() ) ? int(1e7) : *iIt );
5059 if ( iForced + 1 < iEnd )
5060 updateSideUV( side, iForced, quadsBySide, &iEnd );
5062 iIt = side.forced_nodes.lower_bound( iForced );
5063 int iBeg = Max( 0, ( iIt == side.forced_nodes.begin() ) ? 0 : *--iIt );
5064 if ( iForced - 1 > iBeg )
5065 updateSideUV( side, iForced, quadsBySide, &iBeg );
5070 const int iFrom = Min ( iForced, *iNext );
5071 const int iTo = Max ( iForced, *iNext ) + 1;
5072 const int sideSize = iTo - iFrom;
5074 vector<UVPtStruct> points[4]; // side points of a temporary quad
5076 // from the quads get grid points adjacent to the side
5077 // to make two sides of a temporary quad
5078 vector< FaceQuadStruct::Ptr > quads = quadsBySide.find( side )->second; // copy!
5079 for ( int is2nd = 0; is2nd < 2; ++is2nd )
5081 points[ is2nd ].reserve( sideSize );
5083 while ( points[is2nd].size() < sideSize )
5085 int iCur = iFrom + points[is2nd].size() - int( !points[is2nd].empty() );
5087 // look for a quad adjacent to iCur-th point of the side
5088 for ( size_t iQ = 0; iQ < quads.size(); ++iQ )
5090 FaceQuadStruct::Ptr q = quads[ iQ ];
5094 for ( iS = 0; iS < q->side.size(); ++iS )
5095 if ( side.grid == q->side[ iS ].grid )
5098 if ( !q->side[ iS ].IsReversed() )
5099 isOut = ( q->side[ iS ].from > iCur || q->side[ iS ].to-1 <= iCur );
5101 isOut = ( q->side[ iS ].to >= iCur || q->side[ iS ].from <= iCur );
5104 if ( !setNormalizedGrid( q ))
5107 // found - copy points
5109 if ( iS % 2 ) // right or left
5111 i = ( iS == QUAD_LEFT_SIDE ) ? 1 : q->iSize-2;
5112 j = q->side[ iS ].ToQuadIndex( iCur );
5114 dj = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5115 nb = ( q->side[ iS ].IsReversed() ) ? j+1 : q->jSize-j;
5117 else // bottom or top
5119 i = q->side[ iS ].ToQuadIndex( iCur );
5120 j = ( iS == QUAD_BOTTOM_SIDE ) ? 1 : q->jSize-2;
5121 di = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5123 nb = ( q->side[ iS ].IsReversed() ) ? i+1 : q->iSize-i;
5125 if ( !points[is2nd].empty() )
5127 gp_UV lastUV = points[is2nd].back().UV();
5128 gp_UV quadUV = q->UVPt( i, j ).UV();
5129 if ( ( lastUV - quadUV ).SquareModulus() > 1e-10 )
5130 continue; // quad is on the other side of the side
5131 i += di; j += dj; --nb;
5133 for ( ; nb > 0 ; --nb )
5135 points[ is2nd ].push_back( q->UVPt( i, j ));
5136 if ( points[is2nd].size() >= sideSize )
5140 quads[ iQ ].reset(); // not to use this quad anymore
5142 if ( points[is2nd].size() >= sideSize )
5146 if ( nbLoops++ > quads.size() )
5147 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::updateSideUV() bug: infinite loop" );
5149 } // while ( points[is2nd].size() < sideSize )
5150 } // two loops to fill points[0] and points[1]
5152 // points for other pair of opposite sides of the temporary quad
5154 enum { L,R,B,T }; // side index of points[]
5156 points[B].push_back( points[L].front() );
5157 points[B].push_back( side.GetUVPtStruct()[ iFrom ]);
5158 points[B].push_back( points[R].front() );
5160 points[T].push_back( points[L].back() );
5161 points[T].push_back( side.GetUVPtStruct()[ iTo-1 ]);
5162 points[T].push_back( points[R].back() );
5164 // make the temporary quad
5165 FaceQuadStruct::Ptr tmpQuad
5166 ( new FaceQuadStruct( TopoDS::Face( myHelper->GetSubShape() ), "tmpQuad"));
5167 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[B] )); // bottom
5168 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[R] )); // right
5169 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[T] ));
5170 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[L] ));
5172 // compute new UV of the side
5173 setNormalizedGrid( tmpQuad );
5174 gp_UV uv = tmpQuad->UVPt(1,0).UV();
5175 tmpQuad->updateUV( uv, 1,0, /*isVertical=*/true );
5177 // update UV of the side
5178 vector<UVPtStruct>& sidePoints = (vector<UVPtStruct>&) side.GetUVPtStruct();
5179 for ( int i = iFrom; i < iTo; ++i )
5181 const uvPtStruct& uvPt = tmpQuad->UVPt( 1, i-iFrom );
5182 sidePoints[ i ].u = uvPt.u;
5183 sidePoints[ i ].v = uvPt.v;
5187 //================================================================================
5189 * \brief Finds indices of a grid quad enclosing the given enforced UV
5191 //================================================================================
5193 bool FaceQuadStruct::findCell( const gp_XY& UV, int & I, int & J )
5195 // setNormalizedGrid() must be called before!
5196 if ( uv_box.IsOut( UV ))
5199 // find an approximate position
5200 double x = 0.5, y = 0.5;
5201 gp_XY t0 = UVPt( iSize - 1, 0 ).UV();
5202 gp_XY t1 = UVPt( 0, jSize - 1 ).UV();
5203 gp_XY t2 = UVPt( 0, 0 ).UV();
5204 SMESH_MeshAlgos::GetBarycentricCoords( UV, t0, t1, t2, x, y );
5205 x = Min( 1., Max( 0., x ));
5206 y = Min( 1., Max( 0., y ));
5208 // precise the position
5209 normPa2IJ( x,y, I,J );
5210 if ( !isNear( UV, I,J ))
5212 // look for the most close IJ by traversing uv_grid in the middle
5213 double dist2, minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5214 for ( int isU = 0; isU < 2; ++isU )
5216 int ind1 = isU ? 0 : iSize / 2;
5217 int ind2 = isU ? jSize / 2 : 0;
5218 int di1 = isU ? Max( 2, iSize / 20 ) : 0;
5219 int di2 = isU ? 0 : Max( 2, jSize / 20 );
5220 int i,nb = isU ? iSize / di1 : jSize / di2;
5221 for ( i = 0; i < nb; ++i, ind1 += di1, ind2 += di2 )
5222 if (( dist2 = ( UV - UVPt( ind1,ind2 ).UV() ).SquareModulus() ) < minDist2 )
5226 if ( isNear( UV, I,J ))
5228 minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5231 if ( !isNear( UV, I,J, Max( iSize, jSize ) /2 ))
5237 //================================================================================
5239 * \brief Find indices (i,j) of a point in uv_grid by normalized parameters (x,y)
5241 //================================================================================
5243 void FaceQuadStruct::normPa2IJ(double X, double Y, int & I, int & J )
5246 I = Min( int ( iSize * X ), iSize - 2 );
5247 J = Min( int ( jSize * Y ), jSize - 2 );
5253 while ( X <= UVPt( I,J ).x && I != 0 )
5255 while ( X > UVPt( I+1,J ).x && I+2 < iSize )
5257 while ( Y <= UVPt( I,J ).y && J != 0 )
5259 while ( Y > UVPt( I,J+1 ).y && J+2 < jSize )
5261 } while ( oldI != I || oldJ != J );
5264 //================================================================================
5266 * \brief Looks for UV in quads around a given (I,J) and precise (I,J)
5268 //================================================================================
5270 bool FaceQuadStruct::isNear( const gp_XY& UV, int & I, int & J, int nbLoops )
5272 if ( I+1 >= iSize ) I = iSize - 2;
5273 if ( J+1 >= jSize ) J = jSize - 2;
5276 gp_XY uvI, uvJ, uv0, uv1;
5277 for ( int iLoop = 0; iLoop < nbLoops; ++iLoop )
5279 int oldI = I, oldJ = J;
5281 uvI = UVPt( I+1, J ).UV();
5282 uvJ = UVPt( I, J+1 ).UV();
5283 uv0 = UVPt( I, J ).UV();
5284 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5285 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5288 if ( I > 0 && bcI < 0. ) --I;
5289 if ( I+2 < iSize && bcI > 1. ) ++I;
5290 if ( J > 0 && bcJ < 0. ) --J;
5291 if ( J+2 < jSize && bcJ > 1. ) ++J;
5293 uv1 = UVPt( I+1,J+1).UV();
5294 if ( I != oldI || J != oldJ )
5296 uvI = UVPt( I+1, J ).UV();
5297 uvJ = UVPt( I, J+1 ).UV();
5299 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5300 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5303 if ( I > 0 && bcI > 1. ) --I;
5304 if ( I+2 < iSize && bcI < 0. ) ++I;
5305 if ( J > 0 && bcJ > 1. ) --J;
5306 if ( J+2 < jSize && bcJ < 0. ) ++J;
5308 if ( I == oldI && J == oldJ )
5311 if ( iLoop+1 == nbLoops )
5313 uvI = UVPt( I+1, J ).UV();
5314 uvJ = UVPt( I, J+1 ).UV();
5315 uv0 = UVPt( I, J ).UV();
5316 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5317 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5320 uv1 = UVPt( I+1,J+1).UV();
5321 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5322 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5329 //================================================================================
5331 * \brief Checks if a given UV is equal to a given grid point
5333 //================================================================================
5335 bool FaceQuadStruct::isEqual( const gp_XY& UV, int I, int J )
5337 TopLoc_Location loc;
5338 Handle(Geom_Surface) surf = BRep_Tool::Surface( face, loc );
5339 gp_Pnt p1 = surf->Value( UV.X(), UV.Y() );
5340 gp_Pnt p2 = surf->Value( UVPt( I,J ).u, UVPt( I,J ).v );
5342 double dist2 = 1e100;
5343 for ( int di = -1; di < 2; di += 2 )
5346 if ( i < 0 || i+1 >= iSize ) continue;
5347 for ( int dj = -1; dj < 2; dj += 2 )
5350 if ( j < 0 || j+1 >= jSize ) continue;
5353 p2.SquareDistance( surf->Value( UVPt( i,j ).u, UVPt( i,j ).v )));
5356 double tol2 = dist2 / 1000.;
5357 return p1.SquareDistance( p2 ) < tol2;
5360 //================================================================================
5362 * \brief Recompute UV of grid points around a moved point in one direction
5364 //================================================================================
5366 void FaceQuadStruct::updateUV( const gp_XY& UV, int I, int J, bool isVertical )
5368 UVPt( I, J ).u = UV.X();
5369 UVPt( I, J ).v = UV.Y();
5374 if ( J+1 < jSize-1 )
5376 gp_UV a0 = UVPt( 0, J ).UV();
5377 gp_UV a1 = UVPt( iSize-1, J ).UV();
5378 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5379 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5381 gp_UV p0 = UVPt( I, J ).UV();
5382 gp_UV p2 = UVPt( I, jSize-1 ).UV();
5383 const double y0 = UVPt( I, J ).y, dy = 1. - y0;
5384 for (int j = J+1; j < jSize-1; j++)
5386 gp_UV p1 = UVPt( iSize-1, j ).UV();
5387 gp_UV p3 = UVPt( 0, j ).UV();
5389 UVPtStruct& uvPt = UVPt( I, j );
5390 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5398 gp_UV a0 = UVPt( 0, 0 ).UV();
5399 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5400 gp_UV a2 = UVPt( iSize-1, J ).UV();
5401 gp_UV a3 = UVPt( 0, J ).UV();
5403 gp_UV p0 = UVPt( I, 0 ).UV();
5404 gp_UV p2 = UVPt( I, J ).UV();
5405 const double y0 = 0., dy = UVPt( I, J ).y - y0;
5406 for (int j = 1; j < J; j++)
5408 gp_UV p1 = UVPt( iSize-1, j ).UV();
5409 gp_UV p3 = UVPt( 0, j ).UV();
5411 UVPtStruct& uvPt = UVPt( I, j );
5412 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5418 else // horizontally
5423 gp_UV a0 = UVPt( 0, 0 ).UV();
5424 gp_UV a1 = UVPt( I, 0 ).UV();
5425 gp_UV a2 = UVPt( I, jSize-1 ).UV();
5426 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5428 gp_UV p1 = UVPt( I, J ).UV();
5429 gp_UV p3 = UVPt( 0, J ).UV();
5430 const double x0 = 0., dx = UVPt( I, J ).x - x0;
5431 for (int i = 1; i < I; i++)
5433 gp_UV p0 = UVPt( i, 0 ).UV();
5434 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5436 UVPtStruct& uvPt = UVPt( i, J );
5437 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5443 if ( I+1 < iSize-1 )
5445 gp_UV a0 = UVPt( I, 0 ).UV();
5446 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5447 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5448 gp_UV a3 = UVPt( I, jSize-1 ).UV();
5450 gp_UV p1 = UVPt( iSize-1, J ).UV();
5451 gp_UV p3 = UVPt( I, J ).UV();
5452 const double x0 = UVPt( I, J ).x, dx = 1. - x0;
5453 for (int i = I+1; i < iSize-1; i++)
5455 gp_UV p0 = UVPt( i, 0 ).UV();
5456 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5458 UVPtStruct& uvPt = UVPt( i, J );
5459 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5467 //================================================================================
5469 * \brief Side copying
5471 //================================================================================
5473 FaceQuadStruct::Side& FaceQuadStruct::Side::operator=(const Side& otherSide)
5475 grid = otherSide.grid;
5476 from = otherSide.from;
5479 forced_nodes = otherSide.forced_nodes;
5480 contacts = otherSide.contacts;
5481 nbNodeOut = otherSide.nbNodeOut;
5483 for ( size_t iC = 0; iC < contacts.size(); ++iC )
5485 FaceQuadStruct::Side* oSide = contacts[iC].other_side;
5486 for ( size_t iOC = 0; iOC < oSide->contacts.size(); ++iOC )
5487 if ( oSide->contacts[iOC].other_side == & otherSide )
5489 // cout << "SHIFT old " << &otherSide << " " << otherSide.NbPoints()
5490 // << " -> new " << this << " " << this->NbPoints() << endl;
5491 oSide->contacts[iOC].other_side = this;
5497 //================================================================================
5499 * \brief Converts node index of a quad to node index of this side
5501 //================================================================================
5503 int FaceQuadStruct::Side::ToSideIndex( int quadNodeIndex ) const
5505 return from + di * quadNodeIndex;
5508 //================================================================================
5510 * \brief Converts node index of this side to node index of a quad
5512 //================================================================================
5514 int FaceQuadStruct::Side::ToQuadIndex( int sideNodeIndex ) const
5516 return ( sideNodeIndex - from ) * di;
5519 //================================================================================
5521 * \brief Reverse the side
5523 //================================================================================
5525 bool FaceQuadStruct::Side::Reverse(bool keepGrid)
5533 std::swap( from, to );
5544 //================================================================================
5546 * \brief Checks if a node is enforced
5547 * \param [in] nodeIndex - an index of a node in a size
5548 * \return bool - \c true if the node is forced
5550 //================================================================================
5552 bool FaceQuadStruct::Side::IsForced( int nodeIndex ) const
5554 if ( nodeIndex < 0 || nodeIndex >= grid->NbPoints() )
5555 throw SALOME_Exception( " FaceQuadStruct::Side::IsForced(): wrong index" );
5557 if ( forced_nodes.count( nodeIndex ) )
5560 for ( size_t i = 0; i < this->contacts.size(); ++i )
5561 if ( contacts[ i ].point == nodeIndex &&
5562 contacts[ i ].other_side->forced_nodes.count( contacts[ i ].other_point ))
5568 //================================================================================
5570 * \brief Sets up a contact between this and another side
5572 //================================================================================
5574 void FaceQuadStruct::Side::AddContact( int ip, Side* side, int iop )
5576 if ( ip >= GetUVPtStruct().size() ||
5577 iop >= side->GetUVPtStruct().size() )
5578 throw SALOME_Exception( "FaceQuadStruct::Side::AddContact(): wrong point" );
5580 contacts.resize( contacts.size() + 1 );
5581 Contact& c = contacts.back();
5583 c.other_side = side;
5584 c.other_point = iop;
5587 side->contacts.resize( side->contacts.size() + 1 );
5588 Contact& c = side->contacts.back();
5590 c.other_side = this;
5595 //================================================================================
5597 * \brief Returns a normalized parameter of a point indexed within a quadrangle
5599 //================================================================================
5601 double FaceQuadStruct::Side::Param( int i ) const
5603 const vector<UVPtStruct>& points = GetUVPtStruct();
5604 return (( points[ from + i * di ].normParam - points[ from ].normParam ) /
5605 ( points[ to - 1 * di ].normParam - points[ from ].normParam ));
5608 //================================================================================
5610 * \brief Returns UV by a parameter normalized within a quadrangle
5612 //================================================================================
5614 gp_XY FaceQuadStruct::Side::Value2d( double x ) const
5616 const vector<UVPtStruct>& points = GetUVPtStruct();
5617 double u = ( points[ from ].normParam +
5618 x * ( points[ to-di ].normParam - points[ from ].normParam ));
5619 return grid->Value2d( u ).XY();
5622 //================================================================================
5624 * \brief Returns side length
5626 //================================================================================
5628 double FaceQuadStruct::Side::Length(int theFrom, int theTo) const
5630 if ( IsReversed() != ( theTo < theFrom ))
5631 std::swap( theTo, theFrom );
5633 const vector<UVPtStruct>& points = GetUVPtStruct();
5635 if ( theFrom == theTo && theTo == -1 )
5636 r = Abs( First().normParam -
5637 Last ().normParam );
5638 else if ( IsReversed() )
5639 r = Abs( points[ Max( to, theTo+1 ) ].normParam -
5640 points[ Min( from, theFrom ) ].normParam );
5642 r = Abs( points[ Min( to, theTo-1 ) ].normParam -
5643 points[ Max( from, theFrom ) ].normParam );
5644 return r * grid->Length();