1 // Copyright (C) 2007-2016 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 "SMESHDS_Mesh.hxx"
34 #include "SMESH_Block.hxx"
35 #include "SMESH_Comment.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_HypoFilter.hxx"
38 #include "SMESH_Mesh.hxx"
39 #include "SMESH_MeshAlgos.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
42 #include "StdMeshers_FaceSide.hxx"
43 #include "StdMeshers_QuadrangleParams.hxx"
44 #include "StdMeshers_ViscousLayers2D.hxx"
46 #include <BRepBndLib.hxx>
47 #include <BRepClass_FaceClassifier.hxx>
48 #include <BRep_Tool.hxx>
49 #include <Bnd_Box.hxx>
50 #include <GeomAPI_ProjectPointOnSurf.hxx>
51 #include <Geom_Surface.hxx>
52 #include <NCollection_DefineArray2.hxx>
53 #include <Precision.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 _name = "Quadrangle_2D";
97 _shapeType = (1 << TopAbs_FACE);
98 _compatibleHypothesis.push_back("QuadrangleParams");
99 _compatibleHypothesis.push_back("QuadranglePreference");
100 _compatibleHypothesis.push_back("TrianglePreference");
101 _compatibleHypothesis.push_back("ViscousLayers2D");
104 //=============================================================================
108 //=============================================================================
110 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
114 //=============================================================================
118 //=============================================================================
120 bool StdMeshers_Quadrangle_2D::CheckHypothesis
122 const TopoDS_Shape& aShape,
123 SMESH_Hypothesis::Hypothesis_Status& aStatus)
126 myQuadType = QUAD_STANDARD;
127 myQuadranglePreference = false;
128 myTrianglePreference = false;
129 myHelper = (SMESH_MesherHelper*)NULL;
133 aStatus = SMESH_Hypothesis::HYP_OK;
135 const list <const SMESHDS_Hypothesis * >& hyps =
136 GetUsedHypothesis(aMesh, aShape, false);
137 const SMESHDS_Hypothesis * aHyp = 0;
139 bool isFirstParams = true;
141 // First assigned hypothesis (if any) is processed now
142 if (hyps.size() > 0) {
144 if (strcmp("QuadrangleParams", aHyp->GetName()) == 0)
146 myParams = (const StdMeshers_QuadrangleParams*)aHyp;
147 myTriaVertexID = myParams->GetTriaVertex();
148 myQuadType = myParams->GetQuadType();
149 if (myQuadType == QUAD_QUADRANGLE_PREF ||
150 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
151 myQuadranglePreference = true;
152 else if (myQuadType == QUAD_TRIANGLE_PREF)
153 myTrianglePreference = true;
155 else if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
156 isFirstParams = false;
157 myQuadranglePreference = true;
159 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
160 isFirstParams = false;
161 myTrianglePreference = true;
164 isFirstParams = false;
168 // Second(last) assigned hypothesis (if any) is processed now
169 if (hyps.size() > 1) {
172 if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
173 myQuadranglePreference = true;
174 myTrianglePreference = false;
175 myQuadType = QUAD_STANDARD;
177 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
178 myQuadranglePreference = false;
179 myTrianglePreference = true;
180 myQuadType = QUAD_STANDARD;
183 else if (const StdMeshers_QuadrangleParams* aHyp2 =
184 dynamic_cast<const StdMeshers_QuadrangleParams*>( aHyp ))
186 myTriaVertexID = aHyp2->GetTriaVertex();
188 if (!myQuadranglePreference && !myTrianglePreference) { // priority of hypos
189 myQuadType = aHyp2->GetQuadType();
190 if (myQuadType == QUAD_QUADRANGLE_PREF ||
191 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
192 myQuadranglePreference = true;
193 else if (myQuadType == QUAD_TRIANGLE_PREF)
194 myTrianglePreference = true;
199 error( StdMeshers_ViscousLayers2D::CheckHypothesis( aMesh, aShape, aStatus ));
201 return aStatus == HYP_OK;
204 //=============================================================================
208 //=============================================================================
210 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
211 const TopoDS_Shape& aShape)
213 const TopoDS_Face& F = TopoDS::Face(aShape);
214 aMesh.GetSubMesh( F );
216 // do not initialize my fields before this as StdMeshers_ViscousLayers2D
217 // can call Compute() recursively
218 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
222 myProxyMesh = proxyMesh;
224 SMESH_MesherHelper helper (aMesh);
227 _quadraticMesh = myHelper->IsQuadraticSubMesh(aShape);
228 myHelper->SetElementsOnShape( true );
229 myNeedSmooth = false;
232 FaceQuadStruct::Ptr quad = CheckNbEdges( aMesh, F, /*considerMesh=*/true, myHelper );
236 myQuadList.push_back( quad );
238 if ( !getEnforcedUV() )
241 updateDegenUV( quad );
243 int n1 = quad->side[0].NbPoints();
244 int n2 = quad->side[1].NbPoints();
245 int n3 = quad->side[2].NbPoints();
246 int n4 = quad->side[3].NbPoints();
248 enum { NOT_COMPUTED = -1, COMPUTE_FAILED = 0, COMPUTE_OK = 1 };
249 int res = NOT_COMPUTED;
250 if ( myQuadranglePreference )
252 int nfull = n1+n2+n3+n4;
253 if ((nfull % 2) == 0 && ((n1 != n3) || (n2 != n4)))
255 // special path genarating only quandrangle faces
256 res = computeQuadPref( aMesh, F, quad );
259 else if ( myQuadType == QUAD_REDUCED )
263 int n13tmp = n13/2; n13tmp = n13tmp*2;
264 int n24tmp = n24/2; n24tmp = n24tmp*2;
265 if ((n1 == n3 && n2 != n4 && n24tmp == n24) ||
266 (n2 == n4 && n1 != n3 && n13tmp == n13))
268 res = computeReduced( aMesh, F, quad );
272 if ( n1 != n3 && n2 != n4 )
273 error( COMPERR_WARNING,
274 "To use 'Reduced' transition, "
275 "two opposite sides should have same number of segments, "
276 "but actual number of segments is different on all sides. "
277 "'Standard' transion has been used.");
278 else if ( ! ( n1 == n3 && n2 == n4 ))
279 error( COMPERR_WARNING,
280 "To use 'Reduced' transition, "
281 "two opposite sides should have an even difference in number of segments. "
282 "'Standard' transion has been used.");
286 if ( res == NOT_COMPUTED )
288 if ( n1 != n3 || n2 != n4 )
289 res = computeTriangles( aMesh, F, quad );
291 res = computeQuadDominant( aMesh, F );
294 if ( res == COMPUTE_OK && myNeedSmooth )
297 if ( res == COMPUTE_OK )
300 return ( res == COMPUTE_OK );
303 //================================================================================
305 * \brief Compute quadrangles and triangles on the quad
307 //================================================================================
309 bool StdMeshers_Quadrangle_2D::computeTriangles(SMESH_Mesh& aMesh,
310 const TopoDS_Face& aFace,
311 FaceQuadStruct::Ptr quad)
313 int nb = quad->side[0].grid->NbPoints();
314 int nr = quad->side[1].grid->NbPoints();
315 int nt = quad->side[2].grid->NbPoints();
316 int nl = quad->side[3].grid->NbPoints();
318 // rotate the quad to have nbNodeOut sides on TOP [and LEFT]
320 quad->shift( nl > nr ? 3 : 2, true );
322 quad->shift( 1, true );
324 quad->shift( nt > nb ? 0 : 3, true );
326 if ( !setNormalizedGrid( quad ))
329 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
331 splitQuad( quad, 0, quad->jSize-2 );
333 if ( quad->nbNodeOut( QUAD_BOTTOM_SIDE )) // this should not happen
335 splitQuad( quad, 0, 1 );
337 FaceQuadStruct::Ptr newQuad = myQuadList.back();
338 if ( quad != newQuad ) // split done
340 { // update left side limit till where to make triangles
341 FaceQuadStruct::Ptr botQuad = // a bottom part
342 ( quad->side[ QUAD_LEFT_SIDE ].from == 0 ) ? quad : newQuad;
343 if ( botQuad->nbNodeOut( QUAD_LEFT_SIDE ) > 0 )
344 botQuad->side[ QUAD_LEFT_SIDE ].to += botQuad->nbNodeOut( QUAD_LEFT_SIDE );
345 else if ( botQuad->nbNodeOut( QUAD_RIGHT_SIDE ) > 0 )
346 botQuad->side[ QUAD_RIGHT_SIDE ].to += botQuad->nbNodeOut( QUAD_RIGHT_SIDE );
348 // make quad be a greatest one
349 if ( quad->side[ QUAD_LEFT_SIDE ].NbPoints() == 2 ||
350 quad->side[ QUAD_RIGHT_SIDE ].NbPoints() == 2 )
352 if ( !setNormalizedGrid( quad ))
356 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
358 splitQuad( quad, quad->iSize-2, 0 );
360 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
362 splitQuad( quad, 1, 0 );
364 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
366 newQuad = myQuadList.back();
367 if ( newQuad == quad ) // too narrow to split
369 // update left side limit till where to make triangles
370 quad->side[ QUAD_LEFT_SIDE ].to--;
374 FaceQuadStruct::Ptr leftQuad =
375 ( quad->side[ QUAD_BOTTOM_SIDE ].from == 0 ) ? quad : newQuad;
376 leftQuad->nbNodeOut( QUAD_TOP_SIDE ) = 0;
381 if ( ! computeQuadDominant( aMesh, aFace ))
384 // try to fix zero-area triangles near straight-angle corners
389 //================================================================================
391 * \brief Compute quadrangles and possibly triangles on all quads of myQuadList
393 //================================================================================
395 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
396 const TopoDS_Face& aFace)
398 if ( !addEnforcedNodes() )
401 std::list< FaceQuadStruct::Ptr >::iterator quad = myQuadList.begin();
402 for ( ; quad != myQuadList.end(); ++quad )
403 if ( !computeQuadDominant( aMesh, aFace, *quad ))
409 //================================================================================
411 * \brief Compute quadrangles and possibly triangles
413 //================================================================================
415 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
416 const TopoDS_Face& aFace,
417 FaceQuadStruct::Ptr quad)
419 // --- set normalized grid on unit square in parametric domain
421 if ( !setNormalizedGrid( quad ))
424 // --- create nodes on points, and create quadrangles
426 int nbhoriz = quad->iSize;
427 int nbvertic = quad->jSize;
429 // internal mesh nodes
430 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
431 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
432 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
433 for (i = 1; i < nbhoriz - 1; i++)
434 for (j = 1; j < nbvertic - 1; j++)
436 UVPtStruct& uvPnt = quad->UVPt( i, j );
437 gp_Pnt P = S->Value( uvPnt.u, uvPnt.v );
438 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
439 meshDS->SetNodeOnFace( uvPnt.node, geomFaceID, uvPnt.u, uvPnt.v );
445 // --.--.--.--.--.-- nbvertic
451 // ---.----.----.--- 0
452 // 0 > > > > > > > > nbhoriz
457 int iup = nbhoriz - 1;
458 if (quad->nbNodeOut(3)) { ilow++; } else { if (quad->nbNodeOut(1)) iup--; }
461 int jup = nbvertic - 1;
462 if (quad->nbNodeOut(0)) { jlow++; } else { if (quad->nbNodeOut(2)) jup--; }
464 // regular quadrangles
465 for (i = ilow; i < iup; i++) {
466 for (j = jlow; j < jup; j++) {
467 const SMDS_MeshNode *a, *b, *c, *d;
468 a = quad->uv_grid[ j * nbhoriz + i ].node;
469 b = quad->uv_grid[ j * nbhoriz + i + 1].node;
470 c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
471 d = quad->uv_grid[(j + 1) * nbhoriz + i ].node;
472 myHelper->AddFace(a, b, c, d);
476 // Boundary elements (must always be on an outer boundary of the FACE)
478 const vector<UVPtStruct>& uv_e0 = quad->side[0].grid->GetUVPtStruct();
479 const vector<UVPtStruct>& uv_e1 = quad->side[1].grid->GetUVPtStruct();
480 const vector<UVPtStruct>& uv_e2 = quad->side[2].grid->GetUVPtStruct();
481 const vector<UVPtStruct>& uv_e3 = quad->side[3].grid->GetUVPtStruct();
483 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
484 return error(COMPERR_BAD_INPUT_MESH);
486 double eps = Precision::Confusion();
488 int nbdown = (int) uv_e0.size();
489 int nbup = (int) uv_e2.size();
490 int nbright = (int) uv_e1.size();
491 int nbleft = (int) uv_e3.size();
493 if (quad->nbNodeOut(0) && nbvertic == 2) // this should not occur
497 // |___|___|___|___|___|___|
499 // |___|___|___|___|___|___|
501 // |___|___|___|___|___|___| __ first row of the regular grid
502 // . . . . . . . . . __ down edge nodes
504 // >->->->->->->->->->->->-> -- direction of processing
506 int g = 0; // number of last processed node in the regular grid
508 // number of last node of the down edge to be processed
509 int stop = nbdown - 1;
510 // if right edge is out, we will stop at a node, previous to the last one
511 //if (quad->nbNodeOut(1)) stop--;
512 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
513 quad->UVPt( nbhoriz-1, 1 ).node = uv_e1[1].node;
514 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
515 quad->UVPt( 0, 1 ).node = uv_e3[1].node;
517 // for each node of the down edge find nearest node
518 // in the first row of the regular grid and link them
519 for (i = 0; i < stop; i++) {
520 const SMDS_MeshNode *a, *b, *c=0, *d;
522 b = uv_e0[i + 1].node;
523 gp_Pnt pb (b->X(), b->Y(), b->Z());
525 // find node c in the regular grid, which will be linked with node b
528 // right bound reached, link with the rightmost node
530 c = quad->uv_grid[nbhoriz + iup].node;
533 // find in the grid node c, nearest to the b
535 double mind = RealLast();
536 for (int k = g; k <= iup; k++) {
538 const SMDS_MeshNode *nk;
539 if (k < ilow) // this can be, if left edge is out
540 nk = uv_e3[1].node; // get node from the left edge
542 nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
544 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
545 double dist = pb.Distance(pnk);
546 if (dist < mind - eps) {
556 if (near == g) { // make triangle
557 myHelper->AddFace(a, b, c);
559 else { // make quadrangle
563 d = quad->uv_grid[nbhoriz + near - 1].node;
564 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
566 if (!myTrianglePreference){
567 myHelper->AddFace(a, b, c, d);
570 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
573 // if node d is not at position g - make additional triangles
575 for (int k = near - 1; k > g; k--) {
576 c = quad->uv_grid[nbhoriz + k].node;
580 d = quad->uv_grid[nbhoriz + k - 1].node;
581 myHelper->AddFace(a, c, d);
588 if (quad->nbNodeOut(2) && nbvertic == 2)
592 // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
594 // . . . . . . . . . __ up edge nodes
595 // ___ ___ ___ ___ ___ ___ __ first row of the regular grid
597 // |___|___|___|___|___|___|
599 // |___|___|___|___|___|___|
602 int g = nbhoriz - 1; // last processed node in the regular grid
608 if ( quad->side[3].grid->Edge(0).IsNull() ) // left side is simulated one
610 if ( nbright == 2 ) // quad divided at I but not at J (2D_mesh_QuadranglePreference_01/B1)
611 stop++; // we stop at a second node
615 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
616 quad->UVPt( nbhoriz-1, 0 ).node = uv_e1[ nbright-2 ].node;
617 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
618 quad->UVPt( 0, 0 ).node = uv_e3[ nbleft-2 ].node;
620 if ( nbright > 2 ) // there was a split at J
621 quad->nbNodeOut( QUAD_LEFT_SIDE ) = 0;
623 const SMDS_MeshNode *a, *b, *c, *d;
625 // avoid creating zero-area triangles near a straight-angle corner
629 c = uv_e1[nbright-2].node;
630 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
631 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
632 if ( Abs( area ) < 1e-20 )
635 d = quad->UVPt( g, nbvertic-2 ).node;
636 if ( myTrianglePreference )
638 myHelper->AddFace(a, d, c);
642 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
644 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
645 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
647 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
648 "Bad quality quad created"));
649 err->myBadElements.push_back( face );
656 // for each node of the up edge find nearest node
657 // in the first row of the regular grid and link them
658 for ( ; i > stop; i--)
661 b = uv_e2[i - 1].node;
662 gp_Pnt pb = SMESH_TNodeXYZ( b );
664 // find node c in the grid, which will be linked with node b
666 if (i == stop + 1) { // left bound reached, link with the leftmost node
667 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
670 // find node c in the grid, nearest to the b
671 double mind = RealLast();
672 for (int k = g; k >= ilow; k--) {
673 const SMDS_MeshNode *nk;
675 nk = uv_e1[nbright - 2].node;
677 nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
678 gp_Pnt pnk = SMESH_TNodeXYZ( nk );
679 double dist = pb.Distance(pnk);
680 if (dist < mind - eps) {
690 if (near == g) { // make triangle
691 myHelper->AddFace(a, b, c);
693 else { // make quadrangle
695 d = uv_e1[nbright - 2].node;
697 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
698 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
699 if (!myTrianglePreference){
700 myHelper->AddFace(a, b, c, d);
703 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
706 if (near + 1 < g) { // if d is not at g - make additional triangles
707 for (int k = near + 1; k < g; k++) {
708 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
710 d = uv_e1[nbright - 2].node;
712 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
713 myHelper->AddFace(a, c, d);
722 // right or left boundary quadrangles
723 if (quad->nbNodeOut( QUAD_RIGHT_SIDE ) && nbhoriz == 2) // this should not occur
725 int g = 0; // last processed node in the grid
726 int stop = nbright - 1;
728 if (quad->side[ QUAD_RIGHT_SIDE ].from != i ) i++;
729 if (quad->side[ QUAD_RIGHT_SIDE ].to != stop ) stop--;
730 for ( ; i < stop; i++) {
731 const SMDS_MeshNode *a, *b, *c, *d;
733 b = uv_e1[i + 1].node;
734 gp_Pnt pb (b->X(), b->Y(), b->Z());
736 // find node c in the grid, nearest to the b
739 if (i == stop - 1) { // up boundary reached
740 c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
743 double mind = RealLast();
744 for (int k = g; k <= jup; k++) {
745 const SMDS_MeshNode *nk;
747 nk = uv_e0[nbdown - 2].node;
749 nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
750 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
751 double dist = pb.Distance(pnk);
752 if (dist < mind - eps) {
762 if (near == g) { // make triangle
763 myHelper->AddFace(a, b, c);
765 else { // make quadrangle
767 d = uv_e0[nbdown - 2].node;
769 d = quad->uv_grid[nbhoriz*near - 2].node;
770 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
772 if (!myTrianglePreference){
773 myHelper->AddFace(a, b, c, d);
776 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
779 if (near - 1 > g) { // if d not is at g - make additional triangles
780 for (int k = near - 1; k > g; k--) {
781 c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
783 d = uv_e0[nbdown - 2].node;
785 d = quad->uv_grid[nbhoriz*k - 2].node;
786 myHelper->AddFace(a, c, d);
793 if (quad->nbNodeOut(3) && nbhoriz == 2)
795 int g = nbvertic - 1; // last processed node in the grid
797 i = quad->side[ QUAD_LEFT_SIDE ].to-1; // nbleft - 1;
799 const SMDS_MeshNode *a, *b, *c, *d;
800 // avoid creating zero-area triangles near a straight-angle corner
804 c = quad->UVPt( 1, g ).node;
805 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
806 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
807 if ( Abs( area ) < 1e-20 )
810 d = quad->UVPt( 1, g ).node;
811 if ( myTrianglePreference )
813 myHelper->AddFace(a, d, c);
817 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
819 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
820 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
822 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
823 "Bad quality quad created"));
824 err->myBadElements.push_back( face );
831 for (; i > stop; i--) // loop on nodes on the left side
834 b = uv_e3[i - 1].node;
835 gp_Pnt pb (b->X(), b->Y(), b->Z());
837 // find node c in the grid, nearest to the b
839 if (i == stop + 1) { // down boundary reached
840 c = quad->uv_grid[nbhoriz*jlow + 1].node;
844 double mind = RealLast();
845 for (int k = g; k >= jlow; k--) {
846 const SMDS_MeshNode *nk;
848 nk = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
850 nk = quad->uv_grid[nbhoriz*k + 1].node;
851 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
852 double dist = pb.Distance(pnk);
853 if (dist < mind - eps) {
863 if (near == g) { // make triangle
864 myHelper->AddFace(a, b, c);
866 else { // make quadrangle
868 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
870 d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
871 if (!myTrianglePreference) {
872 myHelper->AddFace(a, b, c, d);
875 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
878 if (near + 1 < g) { // if d not is at g - make additional triangles
879 for (int k = near + 1; k < g; k++) {
880 c = quad->uv_grid[nbhoriz*k + 1].node;
882 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
884 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
885 myHelper->AddFace(a, c, d);
899 //=============================================================================
903 //=============================================================================
905 bool StdMeshers_Quadrangle_2D::Evaluate(SMESH_Mesh& aMesh,
906 const TopoDS_Shape& aFace,
907 MapShapeNbElems& aResMap)
910 aMesh.GetSubMesh(aFace);
912 std::vector<int> aNbNodes(4);
913 bool IsQuadratic = false;
914 if (!checkNbEdgesForEvaluate(aMesh, aFace, aResMap, aNbNodes, IsQuadratic)) {
915 std::vector<int> aResVec(SMDSEntity_Last);
916 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
917 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
918 aResMap.insert(std::make_pair(sm,aResVec));
919 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
920 smError.reset(new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
924 if (myQuadranglePreference) {
925 int n1 = aNbNodes[0];
926 int n2 = aNbNodes[1];
927 int n3 = aNbNodes[2];
928 int n4 = aNbNodes[3];
929 int nfull = n1+n2+n3+n4;
932 if (nfull==ntmp && ((n1!=n3) || (n2!=n4))) {
933 // special path for using only quandrangle faces
934 return evaluateQuadPref(aMesh, aFace, aNbNodes, aResMap, IsQuadratic);
939 int nbdown = aNbNodes[0];
940 int nbup = aNbNodes[2];
942 int nbright = aNbNodes[1];
943 int nbleft = aNbNodes[3];
945 int nbhoriz = Min(nbdown, nbup);
946 int nbvertic = Min(nbright, nbleft);
948 int dh = Max(nbdown, nbup) - nbhoriz;
949 int dv = Max(nbright, nbleft) - nbvertic;
956 int nbNodes = (nbhoriz-2)*(nbvertic-2);
957 //int nbFaces3 = dh + dv + kdh*(nbvertic-1)*2 + kdv*(nbhoriz-1)*2;
958 int nbFaces3 = dh + dv;
959 //if (kdh==1 && kdv==1) nbFaces3 -= 2;
960 //if (dh>0 && dv>0) nbFaces3 -= 2;
961 //int nbFaces4 = (nbhoriz-1-kdh)*(nbvertic-1-kdv);
962 int nbFaces4 = (nbhoriz-1)*(nbvertic-1);
964 std::vector<int> aVec(SMDSEntity_Last);
965 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
967 aVec[SMDSEntity_Quad_Triangle] = nbFaces3;
968 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4;
969 int nbbndedges = nbdown + nbup + nbright + nbleft -4;
970 int nbintedges = (nbFaces4*4 + nbFaces3*3 - nbbndedges) / 2;
971 aVec[SMDSEntity_Node] = nbNodes + nbintedges;
972 if (aNbNodes.size()==5) {
973 aVec[SMDSEntity_Quad_Triangle] = nbFaces3 + aNbNodes[3] -1;
974 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4 - aNbNodes[3] +1;
978 aVec[SMDSEntity_Node] = nbNodes;
979 aVec[SMDSEntity_Triangle] = nbFaces3;
980 aVec[SMDSEntity_Quadrangle] = nbFaces4;
981 if (aNbNodes.size()==5) {
982 aVec[SMDSEntity_Triangle] = nbFaces3 + aNbNodes[3] - 1;
983 aVec[SMDSEntity_Quadrangle] = nbFaces4 - aNbNodes[3] + 1;
986 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
987 aResMap.insert(std::make_pair(sm,aVec));
992 //================================================================================
994 * \brief Return true if the algorithm can mesh this shape
995 * \param [in] aShape - shape to check
996 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
997 * else, returns OK if at least one shape is OK
999 //================================================================================
1001 bool StdMeshers_Quadrangle_2D::IsApplicable( const TopoDS_Shape & aShape, bool toCheckAll )
1003 int nbFoundFaces = 0;
1004 for (TopExp_Explorer exp( aShape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFoundFaces )
1006 const TopoDS_Shape& aFace = exp.Current();
1007 int nbWire = SMESH_MesherHelper::Count( aFace, TopAbs_WIRE, false );
1008 if ( nbWire != 1 ) {
1009 if ( toCheckAll ) return false;
1013 int nbNoDegenEdges = 0;
1014 TopExp_Explorer eExp( aFace, TopAbs_EDGE );
1015 for ( ; eExp.More() && nbNoDegenEdges < 3; eExp.Next() ) {
1016 if ( !SMESH_Algo::isDegenerated( TopoDS::Edge( eExp.Current() )))
1019 if ( toCheckAll && nbNoDegenEdges < 3 ) return false;
1020 if ( !toCheckAll && nbNoDegenEdges >= 3 ) return true;
1022 return ( toCheckAll && nbFoundFaces != 0 );
1025 //================================================================================
1027 * \brief Return true if only two given edges meat at their common vertex
1029 //================================================================================
1031 static bool twoEdgesMeatAtVertex(const TopoDS_Edge& e1,
1032 const TopoDS_Edge& e2,
1036 if (!TopExp::CommonVertex(e1, e2, v))
1038 TopTools_ListIteratorOfListOfShape ancestIt(mesh.GetAncestors(v));
1039 for (; ancestIt.More() ; ancestIt.Next())
1040 if (ancestIt.Value().ShapeType() == TopAbs_EDGE)
1041 if (!e1.IsSame(ancestIt.Value()) && !e2.IsSame(ancestIt.Value()))
1046 //=============================================================================
1050 //=============================================================================
1052 FaceQuadStruct::Ptr StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh & aMesh,
1053 const TopoDS_Shape & aShape,
1054 const bool considerMesh,
1055 SMESH_MesherHelper* aFaceHelper)
1057 if ( !myQuadList.empty() && myQuadList.front()->face.IsSame( aShape ))
1058 return myQuadList.front();
1060 TopoDS_Face F = TopoDS::Face(aShape);
1061 if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
1062 const bool ignoreMediumNodes = _quadraticMesh;
1064 // verify 1 wire only
1065 list< TopoDS_Edge > edges;
1066 list< int > nbEdgesInWire;
1067 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1069 error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
1070 return FaceQuadStruct::Ptr();
1073 // find corner vertices of the quad
1074 myHelper = ( aFaceHelper && aFaceHelper->GetSubShape() == aShape ) ? aFaceHelper : NULL;
1075 vector<TopoDS_Vertex> corners;
1076 int nbDegenEdges, nbSides = getCorners( F, aMesh, edges, corners, nbDegenEdges, considerMesh );
1079 return FaceQuadStruct::Ptr();
1081 FaceQuadStruct::Ptr quad( new FaceQuadStruct );
1082 quad->side.reserve(nbEdgesInWire.front());
1085 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1086 if ( nbSides == 3 ) // 3 sides and corners[0] is a vertex with myTriaVertexID
1088 for ( int iSide = 0; iSide < 3; ++iSide )
1090 list< TopoDS_Edge > sideEdges;
1091 TopoDS_Vertex nextSideV = corners[( iSide + 1 ) % 3 ];
1092 while ( edgeIt != edges.end() &&
1093 !nextSideV.IsSame( SMESH_MesherHelper::IthVertex( 0, *edgeIt )))
1094 if ( SMESH_Algo::isDegenerated( *edgeIt ))
1097 sideEdges.push_back( *edgeIt++ );
1098 if ( !sideEdges.empty() )
1099 quad->side.push_back( StdMeshers_FaceSide::New(F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1100 ignoreMediumNodes, myHelper, myProxyMesh));
1104 const vector<UVPtStruct>& UVPSleft = quad->side[0].GetUVPtStruct(true,0);
1105 /* vector<UVPtStruct>& UVPStop = */quad->side[1].GetUVPtStruct(false,1);
1106 /* vector<UVPtStruct>& UVPSright = */quad->side[2].GetUVPtStruct(true,1);
1107 const SMDS_MeshNode* aNode = UVPSleft[0].node;
1108 gp_Pnt2d aPnt2d = UVPSleft[0].UV();
1109 quad->side.push_back( StdMeshers_FaceSide::New( quad->side[1].grid.get(), aNode, &aPnt2d ));
1110 myNeedSmooth = ( nbDegenEdges > 0 );
1115 myNeedSmooth = ( corners.size() == 4 && nbDegenEdges > 0 );
1116 int iSide = 0, nbUsedDegen = 0, nbLoops = 0;
1117 for ( ; edgeIt != edges.end(); ++nbLoops )
1119 list< TopoDS_Edge > sideEdges;
1120 TopoDS_Vertex nextSideV = corners[( iSide + 1 - nbUsedDegen ) % corners.size() ];
1121 bool nextSideVReached = false;
1124 const TopoDS_Edge& edge = *edgeIt;
1125 nextSideVReached = nextSideV.IsSame( myHelper->IthVertex( 1, edge ));
1126 if ( SMESH_Algo::isDegenerated( edge ))
1128 if ( !myNeedSmooth ) // need to make a side on a degen edge
1130 if ( sideEdges.empty() )
1132 sideEdges.push_back( edge );
1134 nextSideVReached = true;
1144 sideEdges.push_back( edge );
1148 while ( edgeIt != edges.end() && !nextSideVReached );
1150 if ( !sideEdges.empty() )
1152 quad->side.push_back
1153 ( StdMeshers_FaceSide::New( F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1154 ignoreMediumNodes, myHelper, myProxyMesh ));
1157 if ( quad->side.size() == 4 )
1161 error(TComm("Bug: infinite loop in StdMeshers_Quadrangle_2D::CheckNbEdges()"));
1166 if ( quad && quad->side.size() != 4 )
1168 error(TComm("Bug: ") << quad->side.size() << " sides found instead of 4");
1177 //=============================================================================
1181 //=============================================================================
1183 bool StdMeshers_Quadrangle_2D::checkNbEdgesForEvaluate(SMESH_Mesh& aMesh,
1184 const TopoDS_Shape & aShape,
1185 MapShapeNbElems& aResMap,
1186 std::vector<int>& aNbNodes,
1190 const TopoDS_Face & F = TopoDS::Face(aShape);
1192 // verify 1 wire only, with 4 edges
1193 list< TopoDS_Edge > edges;
1194 list< int > nbEdgesInWire;
1195 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1203 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1204 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1205 MapShapeNbElemsItr anIt = aResMap.find(sm);
1206 if (anIt==aResMap.end()) {
1209 std::vector<int> aVec = (*anIt).second;
1210 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
1211 if (nbEdgesInWire.front() == 3) { // exactly 3 edges
1212 if (myTriaVertexID>0) {
1213 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
1214 TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
1216 TopoDS_Edge E1,E2,E3;
1217 for (; edgeIt != edges.end(); ++edgeIt) {
1218 TopoDS_Edge E = TopoDS::Edge(*edgeIt);
1219 TopoDS_Vertex VF, VL;
1220 TopExp::Vertices(E, VF, VL, true);
1223 else if (VL.IsSame(V))
1228 SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
1229 MapShapeNbElemsItr anIt = aResMap.find(sm);
1230 if (anIt==aResMap.end()) return false;
1231 std::vector<int> aVec = (*anIt).second;
1233 aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1235 aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
1236 sm = aMesh.GetSubMesh(E2);
1237 anIt = aResMap.find(sm);
1238 if (anIt==aResMap.end()) return false;
1239 aVec = (*anIt).second;
1241 aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1243 aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1244 sm = aMesh.GetSubMesh(E3);
1245 anIt = aResMap.find(sm);
1246 if (anIt==aResMap.end()) return false;
1247 aVec = (*anIt).second;
1249 aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1251 aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1252 aNbNodes[3] = aNbNodes[1];
1258 if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1259 for (; edgeIt != edges.end(); edgeIt++) {
1260 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1261 MapShapeNbElemsItr anIt = aResMap.find(sm);
1262 if (anIt==aResMap.end()) {
1265 std::vector<int> aVec = (*anIt).second;
1267 aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1269 aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1273 else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1274 list< TopoDS_Edge > sideEdges;
1275 while (!edges.empty()) {
1277 sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1278 bool sameSide = true;
1279 while (!edges.empty() && sameSide) {
1280 sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1282 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1284 if (nbSides == 0) { // go backward from the first edge
1286 while (!edges.empty() && sameSide) {
1287 sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1289 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1292 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1293 aNbNodes[nbSides] = 1;
1294 for (; ite!=sideEdges.end(); ite++) {
1295 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1296 MapShapeNbElemsItr anIt = aResMap.find(sm);
1297 if (anIt==aResMap.end()) {
1300 std::vector<int> aVec = (*anIt).second;
1302 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1304 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1308 // issue 20222. Try to unite only edges shared by two same faces
1311 SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1312 while (!edges.empty()) {
1314 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1315 bool sameSide = true;
1316 while (!edges.empty() && sameSide) {
1318 SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1319 twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1321 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1323 if (nbSides == 0) { // go backward from the first edge
1325 while (!edges.empty() && sameSide) {
1327 SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1328 twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1330 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1333 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1334 aNbNodes[nbSides] = 1;
1335 for (; ite!=sideEdges.end(); ite++) {
1336 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1337 MapShapeNbElemsItr anIt = aResMap.find(sm);
1338 if (anIt==aResMap.end()) {
1341 std::vector<int> aVec = (*anIt).second;
1343 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1345 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1353 nbSides = nbEdgesInWire.front();
1354 error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1362 //=============================================================================
1366 //=============================================================================
1369 StdMeshers_Quadrangle_2D::CheckAnd2Dcompute (SMESH_Mesh & aMesh,
1370 const TopoDS_Shape & aShape,
1371 const bool CreateQuadratic)
1373 _quadraticMesh = CreateQuadratic;
1375 FaceQuadStruct::Ptr quad = CheckNbEdges(aMesh, aShape);
1378 // set normalized grid on unit square in parametric domain
1379 if ( ! setNormalizedGrid( quad ))
1387 inline const vector<UVPtStruct>& getUVPtStructIn(FaceQuadStruct::Ptr& quad, int i, int nbSeg)
1389 bool isXConst = (i == QUAD_BOTTOM_SIDE || i == QUAD_TOP_SIDE);
1390 double constValue = (i == QUAD_BOTTOM_SIDE || i == QUAD_LEFT_SIDE) ? 0 : 1;
1392 quad->nbNodeOut(i) ?
1393 quad->side[i].grid->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1394 quad->side[i].grid->GetUVPtStruct (isXConst,constValue);
1396 inline gp_UV calcUV(double x, double y,
1397 const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1398 const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1401 ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1402 ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1406 //=============================================================================
1410 //=============================================================================
1412 bool StdMeshers_Quadrangle_2D::setNormalizedGrid (FaceQuadStruct::Ptr quad)
1414 if ( !quad->uv_grid.empty() )
1417 // Algorithme décrit dans "Génération automatique de maillages"
1418 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
1419 // traitement dans le domaine paramétrique 2d u,v
1420 // transport - projection sur le carré unité
1423 // |<----north-2-------^ a3 -------------> a2
1425 // west-3 east-1 =right | |
1429 // v----south-0--------> a0 -------------> a1
1433 const FaceQuadStruct::Side & bSide = quad->side[0];
1434 const FaceQuadStruct::Side & rSide = quad->side[1];
1435 const FaceQuadStruct::Side & tSide = quad->side[2];
1436 const FaceQuadStruct::Side & lSide = quad->side[3];
1438 int nbhoriz = Min( bSide.NbPoints(), tSide.NbPoints() );
1439 int nbvertic = Min( rSide.NbPoints(), lSide.NbPoints() );
1440 if ( nbhoriz < 1 || nbvertic < 1 )
1441 return error("Algo error: empty quad");
1443 if ( myQuadList.size() == 1 )
1445 // all sub-quads must have NO sides with nbNodeOut > 0
1446 quad->nbNodeOut(0) = Max( 0, bSide.grid->NbPoints() - tSide.grid->NbPoints() );
1447 quad->nbNodeOut(1) = Max( 0, rSide.grid->NbPoints() - lSide.grid->NbPoints() );
1448 quad->nbNodeOut(2) = Max( 0, tSide.grid->NbPoints() - bSide.grid->NbPoints() );
1449 quad->nbNodeOut(3) = Max( 0, lSide.grid->NbPoints() - rSide.grid->NbPoints() );
1451 const vector<UVPtStruct>& uv_e0 = bSide.GetUVPtStruct();
1452 const vector<UVPtStruct>& uv_e1 = rSide.GetUVPtStruct();
1453 const vector<UVPtStruct>& uv_e2 = tSide.GetUVPtStruct();
1454 const vector<UVPtStruct>& uv_e3 = lSide.GetUVPtStruct();
1455 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1456 //return error("Can't find nodes on sides");
1457 return error(COMPERR_BAD_INPUT_MESH);
1459 quad->uv_grid.resize( nbvertic * nbhoriz );
1460 quad->iSize = nbhoriz;
1461 quad->jSize = nbvertic;
1462 UVPtStruct *uv_grid = & quad->uv_grid[0];
1464 quad->uv_box.Clear();
1466 // copy data of face boundary
1468 FaceQuadStruct::SideIterator sideIter;
1472 const double x0 = bSide.First().normParam;
1473 const double dx = bSide.Last().normParam - bSide.First().normParam;
1474 for ( sideIter.Init( bSide ); sideIter.More(); sideIter.Next() ) {
1475 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1476 sideIter.UVPt().y = 0.;
1477 uv_grid[ j * nbhoriz + sideIter.Count() ] = sideIter.UVPt();
1478 quad->uv_box.Add( sideIter.UVPt().UV() );
1482 const int i = nbhoriz - 1;
1483 const double y0 = rSide.First().normParam;
1484 const double dy = rSide.Last().normParam - rSide.First().normParam;
1485 sideIter.Init( rSide );
1486 if ( quad->UVPt( i, sideIter.Count() ).node )
1487 sideIter.Next(); // avoid copying from a split emulated side
1488 for ( ; sideIter.More(); sideIter.Next() ) {
1489 sideIter.UVPt().x = 1.;
1490 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1491 uv_grid[ sideIter.Count() * nbhoriz + i ] = sideIter.UVPt();
1492 quad->uv_box.Add( sideIter.UVPt().UV() );
1496 const int j = nbvertic - 1;
1497 const double x0 = tSide.First().normParam;
1498 const double dx = tSide.Last().normParam - tSide.First().normParam;
1499 int i = 0, nb = nbhoriz;
1500 sideIter.Init( tSide );
1501 if ( quad->UVPt( nb-1, j ).node ) --nb; // avoid copying from a split emulated side
1502 for ( ; i < nb; i++, sideIter.Next()) {
1503 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1504 sideIter.UVPt().y = 1.;
1505 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1506 quad->uv_box.Add( sideIter.UVPt().UV() );
1511 const double y0 = lSide.First().normParam;
1512 const double dy = lSide.Last().normParam - lSide.First().normParam;
1513 int j = 0, nb = nbvertic;
1514 sideIter.Init( lSide );
1515 if ( quad->UVPt( i, j ).node )
1516 ++j, sideIter.Next(); // avoid copying from a split emulated side
1517 if ( quad->UVPt( i, nb-1 ).node )
1519 for ( ; j < nb; j++, sideIter.Next()) {
1520 sideIter.UVPt().x = 0.;
1521 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1522 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1523 quad->uv_box.Add( sideIter.UVPt().UV() );
1527 // normalized 2d parameters on grid
1529 for (int i = 1; i < nbhoriz-1; i++)
1531 const double x0 = quad->UVPt( i, 0 ).x;
1532 const double x1 = quad->UVPt( i, nbvertic-1 ).x;
1533 for (int j = 1; j < nbvertic-1; j++)
1535 const double y0 = quad->UVPt( 0, j ).y;
1536 const double y1 = quad->UVPt( nbhoriz-1, j ).y;
1537 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1538 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1539 double y = y0 + x * (y1 - y0);
1540 int ij = j * nbhoriz + i;
1543 uv_grid[ij].node = NULL;
1547 // projection on 2d domain (u,v)
1549 gp_UV a0 = quad->UVPt( 0, 0 ).UV();
1550 gp_UV a1 = quad->UVPt( nbhoriz-1, 0 ).UV();
1551 gp_UV a2 = quad->UVPt( nbhoriz-1, nbvertic-1 ).UV();
1552 gp_UV a3 = quad->UVPt( 0, nbvertic-1 ).UV();
1554 for (int i = 1; i < nbhoriz-1; i++)
1556 gp_UV p0 = quad->UVPt( i, 0 ).UV();
1557 gp_UV p2 = quad->UVPt( i, nbvertic-1 ).UV();
1558 for (int j = 1; j < nbvertic-1; j++)
1560 gp_UV p1 = quad->UVPt( nbhoriz-1, j ).UV();
1561 gp_UV p3 = quad->UVPt( 0, j ).UV();
1563 int ij = j * nbhoriz + i;
1564 double x = uv_grid[ij].x;
1565 double y = uv_grid[ij].y;
1567 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1569 uv_grid[ij].u = uv.X();
1570 uv_grid[ij].v = uv.Y();
1576 //=======================================================================
1577 //function : ShiftQuad
1578 //purpose : auxiliary function for computeQuadPref
1579 //=======================================================================
1581 void StdMeshers_Quadrangle_2D::shiftQuad(FaceQuadStruct::Ptr& quad, const int num )
1583 quad->shift( num, /*ori=*/true, /*keepGrid=*/myQuadList.size() > 1 );
1586 //================================================================================
1588 * \brief Rotate sides of a quad CCW by given nb of quartes
1589 * \param nb - number of rotation quartes
1590 * \param ori - to keep orientation of sides as in an unit quad or not
1591 * \param keepGrid - if \c true Side::grid is not changed, Side::from and Side::to
1592 * are altered instead
1594 //================================================================================
1596 void FaceQuadStruct::shift( size_t nb, bool ori, bool keepGrid )
1598 if ( nb == 0 ) return;
1600 nb = nb % NB_QUAD_SIDES;
1602 vector< Side > newSides( side.size() );
1603 vector< Side* > sidePtrs( side.size() );
1604 for (int i = QUAD_BOTTOM_SIDE; i < NB_QUAD_SIDES; ++i)
1606 int id = (i + nb) % NB_QUAD_SIDES;
1609 bool wasForward = (i < QUAD_TOP_SIDE);
1610 bool newForward = (id < QUAD_TOP_SIDE);
1611 if ( wasForward != newForward )
1612 side[ i ].Reverse( keepGrid );
1614 newSides[ id ] = side[ i ];
1615 sidePtrs[ i ] = & side[ i ];
1617 // make newSides refer newSides via Side::Contact's
1618 for ( size_t i = 0; i < newSides.size(); ++i )
1620 FaceQuadStruct::Side& ns = newSides[ i ];
1621 for ( size_t iC = 0; iC < ns.contacts.size(); ++iC )
1623 FaceQuadStruct::Side* oSide = ns.contacts[iC].other_side;
1624 vector< Side* >::iterator sIt = std::find( sidePtrs.begin(), sidePtrs.end(), oSide );
1625 if ( sIt != sidePtrs.end() )
1626 ns.contacts[iC].other_side = & newSides[ *sIt - sidePtrs[0] ];
1629 newSides.swap( side );
1631 if ( keepGrid && !uv_grid.empty() )
1633 if ( nb == 2 ) // "PI"
1635 std::reverse( uv_grid.begin(), uv_grid.end() );
1639 FaceQuadStruct newQuad;
1640 newQuad.uv_grid.resize( uv_grid.size() );
1641 newQuad.iSize = jSize;
1642 newQuad.jSize = iSize;
1643 int i, j, iRev, jRev;
1644 int *iNew = ( nb == 1 ) ? &jRev : &j;
1645 int *jNew = ( nb == 1 ) ? &i : &iRev;
1646 for ( i = 0, iRev = iSize-1; i < iSize; ++i, --iRev )
1647 for ( j = 0, jRev = jSize-1; j < jSize; ++j, --jRev )
1648 newQuad.UVPt( *iNew, *jNew ) = UVPt( i, j );
1650 std::swap( iSize, jSize );
1651 std::swap( uv_grid, newQuad.uv_grid );
1660 //=======================================================================
1662 //purpose : auxiliary function for computeQuadPref
1663 //=======================================================================
1665 static gp_UV calcUV(double x0, double x1, double y0, double y1,
1666 FaceQuadStruct::Ptr& quad,
1667 const gp_UV& a0, const gp_UV& a1,
1668 const gp_UV& a2, const gp_UV& a3)
1670 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1671 double y = y0 + x * (y1 - y0);
1673 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1674 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1675 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1676 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1678 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1683 //=======================================================================
1684 //function : calcUV2
1685 //purpose : auxiliary function for computeQuadPref
1686 //=======================================================================
1688 static gp_UV calcUV2(double x, double y,
1689 FaceQuadStruct::Ptr& quad,
1690 const gp_UV& a0, const gp_UV& a1,
1691 const gp_UV& a2, const gp_UV& a3)
1693 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1694 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1695 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1696 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1698 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1704 //=======================================================================
1706 * Create only quandrangle faces
1708 //=======================================================================
1710 bool StdMeshers_Quadrangle_2D::computeQuadPref (SMESH_Mesh & aMesh,
1711 const TopoDS_Face& aFace,
1712 FaceQuadStruct::Ptr quad)
1714 const bool OldVersion = (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED);
1715 const bool WisF = true;
1717 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1718 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
1719 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
1721 int nb = quad->side[0].NbPoints();
1722 int nr = quad->side[1].NbPoints();
1723 int nt = quad->side[2].NbPoints();
1724 int nl = quad->side[3].NbPoints();
1725 int dh = abs(nb-nt);
1726 int dv = abs(nr-nl);
1728 if ( myForcedPnts.empty() )
1730 // rotate sides to be as in the picture below and to have
1731 // dh >= dv and nt > nb
1733 shiftQuad( quad, ( nt > nb ) ? 0 : 2 );
1735 shiftQuad( quad, ( nr > nl ) ? 1 : 3 );
1739 // rotate the quad to have nt > nb [and nr > nl]
1741 shiftQuad ( quad, nr > nl ? 1 : 2 );
1743 shiftQuad( quad, nb == nt ? 1 : 0 );
1745 shiftQuad( quad, 3 );
1748 nb = quad->side[0].NbPoints();
1749 nr = quad->side[1].NbPoints();
1750 nt = quad->side[2].NbPoints();
1751 nl = quad->side[3].NbPoints();
1754 int nbh = Max(nb,nt);
1755 int nbv = Max(nr,nl);
1759 // Orientation of face and 3 main domain for future faces
1760 // ----------- Old version ---------------
1766 // left | |__| | rigth
1773 // ----------- New version ---------------
1779 // left |/________\| rigth
1787 //const int bfrom = quad->side[0].from;
1788 //const int rfrom = quad->side[1].from;
1789 const int tfrom = quad->side[2].from;
1790 //const int lfrom = quad->side[3].from;
1792 const vector<UVPtStruct>& uv_eb_vec = quad->side[0].GetUVPtStruct(true,0);
1793 const vector<UVPtStruct>& uv_er_vec = quad->side[1].GetUVPtStruct(false,1);
1794 const vector<UVPtStruct>& uv_et_vec = quad->side[2].GetUVPtStruct(true,1);
1795 const vector<UVPtStruct>& uv_el_vec = quad->side[3].GetUVPtStruct(false,0);
1796 if (uv_eb_vec.empty() ||
1797 uv_er_vec.empty() ||
1798 uv_et_vec.empty() ||
1800 return error(COMPERR_BAD_INPUT_MESH);
1802 FaceQuadStruct::SideIterator uv_eb, uv_er, uv_et, uv_el;
1803 uv_eb.Init( quad->side[0] );
1804 uv_er.Init( quad->side[1] );
1805 uv_et.Init( quad->side[2] );
1806 uv_el.Init( quad->side[3] );
1808 gp_UV a0,a1,a2,a3, p0,p1,p2,p3, uv;
1811 a0 = uv_eb[ 0 ].UV();
1812 a1 = uv_er[ 0 ].UV();
1813 a2 = uv_er[ nr-1 ].UV();
1814 a3 = uv_et[ 0 ].UV();
1816 if ( !myForcedPnts.empty() )
1818 if ( dv != 0 && dh != 0 ) // here myQuadList.size() == 1
1820 const int dmin = Min( dv, dh );
1822 // Make a side separating domains L and Cb
1823 StdMeshers_FaceSidePtr sideLCb;
1824 UVPtStruct p3dom; // a point where 3 domains meat
1826 vector<UVPtStruct> pointsLCb( dmin+1 ); // 1--------1
1827 pointsLCb[0] = uv_eb[0]; // | | |
1828 for ( int i = 1; i <= dmin; ++i ) // | |Ct|
1830 x = uv_et[ i ].normParam; // | |__|
1831 y = uv_er[ i ].normParam; // | / |
1832 p0 = quad->side[0].grid->Value2d( x ).XY(); // | / Cb |dmin
1833 p1 = uv_er[ i ].UV(); // |/ |
1834 p2 = uv_et[ i ].UV(); // 0--------0
1835 p3 = quad->side[3].grid->Value2d( y ).XY();
1836 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1837 pointsLCb[ i ].u = uv.X();
1838 pointsLCb[ i ].v = uv.Y();
1840 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1841 p3dom = pointsLCb.back();
1843 gp_Pnt xyz = S->Value( p3dom.u, p3dom.v );
1844 p3dom.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, p3dom.u, p3dom.v );
1845 pointsLCb.back() = p3dom;
1847 // Make a side separating domains L and Ct
1848 StdMeshers_FaceSidePtr sideLCt;
1850 vector<UVPtStruct> pointsLCt( nl );
1851 pointsLCt[0] = p3dom;
1852 pointsLCt.back() = uv_et[ dmin ];
1853 x = uv_et[ dmin ].normParam;
1854 p0 = quad->side[0].grid->Value2d( x ).XY();
1855 p2 = uv_et[ dmin ].UV();
1856 double y0 = uv_er[ dmin ].normParam;
1857 for ( int i = 1; i < nl-1; ++i )
1859 y = y0 + i / ( nl-1. ) * ( 1. - y0 );
1860 p1 = quad->side[1].grid->Value2d( y ).XY();
1861 p3 = quad->side[3].grid->Value2d( y ).XY();
1862 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1863 pointsLCt[ i ].u = uv.X();
1864 pointsLCt[ i ].v = uv.Y();
1866 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
1868 // Make a side separating domains Cb and Ct
1869 StdMeshers_FaceSidePtr sideCbCt;
1871 vector<UVPtStruct> pointsCbCt( nb );
1872 pointsCbCt[0] = p3dom;
1873 pointsCbCt.back() = uv_er[ dmin ];
1874 y = uv_er[ dmin ].normParam;
1875 p1 = uv_er[ dmin ].UV();
1876 p3 = quad->side[3].grid->Value2d( y ).XY();
1877 double x0 = uv_et[ dmin ].normParam;
1878 for ( int i = 1; i < nb-1; ++i )
1880 x = x0 + i / ( nb-1. ) * ( 1. - x0 );
1881 p2 = quad->side[2].grid->Value2d( x ).XY();
1882 p0 = quad->side[0].grid->Value2d( x ).XY();
1883 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1884 pointsCbCt[ i ].u = uv.X();
1885 pointsCbCt[ i ].v = uv.Y();
1887 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
1890 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
1891 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
1892 qCb->side.resize(4);
1893 qCb->side[0] = quad->side[0];
1894 qCb->side[1] = quad->side[1];
1895 qCb->side[2] = sideCbCt;
1896 qCb->side[3] = sideLCb;
1897 qCb->side[1].to = dmin+1;
1899 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
1900 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
1902 qL->side[0] = sideLCb;
1903 qL->side[1] = sideLCt;
1904 qL->side[2] = quad->side[2];
1905 qL->side[3] = quad->side[3];
1906 qL->side[2].to = dmin+1;
1907 // Make Ct from the main quad
1908 FaceQuadStruct::Ptr qCt = quad;
1909 qCt->side[0] = sideCbCt;
1910 qCt->side[3] = sideLCt;
1911 qCt->side[1].from = dmin;
1912 qCt->side[2].from = dmin;
1913 qCt->uv_grid.clear();
1917 qCb->side[3].AddContact( dmin, & qCb->side[2], 0 );
1918 qCb->side[3].AddContact( dmin, & qCt->side[3], 0 );
1919 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
1920 qCt->side[0].AddContact( 0, & qL ->side[0], dmin );
1921 qL ->side[0].AddContact( dmin, & qL ->side[1], 0 );
1922 qL ->side[0].AddContact( dmin, & qCb->side[2], 0 );
1925 return computeQuadDominant( aMesh, aFace );
1927 return computeQuadPref( aMesh, aFace, qCt );
1929 } // if ( dv != 0 && dh != 0 )
1931 //const int db = quad->side[0].IsReversed() ? -1 : +1;
1932 //const int dr = quad->side[1].IsReversed() ? -1 : +1;
1933 const int dt = quad->side[2].IsReversed() ? -1 : +1;
1934 //const int dl = quad->side[3].IsReversed() ? -1 : +1;
1936 // Case dv == 0, here possibly myQuadList.size() > 1
1948 const int lw = dh/2; // lateral width
1952 double lL = quad->side[3].Length();
1953 double lLwL = quad->side[2].Length( tfrom,
1954 tfrom + ( lw ) * dt );
1955 yCbL = lLwL / ( lLwL + lL );
1957 double lR = quad->side[1].Length();
1958 double lLwR = quad->side[2].Length( tfrom + ( lw + nb-1 ) * dt,
1959 tfrom + ( lw + nb-1 + lw ) * dt);
1960 yCbR = lLwR / ( lLwR + lR );
1962 // Make sides separating domains Cb and L and R
1963 StdMeshers_FaceSidePtr sideLCb, sideRCb;
1964 UVPtStruct pTBL, pTBR; // points where 3 domains meat
1966 vector<UVPtStruct> pointsLCb( lw+1 ), pointsRCb( lw+1 );
1967 pointsLCb[0] = uv_eb[ 0 ];
1968 pointsRCb[0] = uv_eb[ nb-1 ];
1969 for ( int i = 1, i2 = nt-2; i <= lw; ++i, --i2 )
1971 x = quad->side[2].Param( i );
1973 p0 = quad->side[0].Value2d( x );
1974 p1 = quad->side[1].Value2d( y );
1975 p2 = uv_et[ i ].UV();
1976 p3 = quad->side[3].Value2d( y );
1977 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1978 pointsLCb[ i ].u = uv.X();
1979 pointsLCb[ i ].v = uv.Y();
1980 pointsLCb[ i ].x = x;
1982 x = quad->side[2].Param( i2 );
1984 p1 = quad->side[1].Value2d( y );
1985 p0 = quad->side[0].Value2d( x );
1986 p2 = uv_et[ i2 ].UV();
1987 p3 = quad->side[3].Value2d( y );
1988 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1989 pointsRCb[ i ].u = uv.X();
1990 pointsRCb[ i ].v = uv.Y();
1991 pointsRCb[ i ].x = x;
1993 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1994 sideRCb = StdMeshers_FaceSide::New( pointsRCb, aFace );
1995 pTBL = pointsLCb.back();
1996 pTBR = pointsRCb.back();
1998 gp_Pnt xyz = S->Value( pTBL.u, pTBL.v );
1999 pTBL.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, pTBL.u, pTBL.v );
2000 pointsLCb.back() = pTBL;
2003 gp_Pnt xyz = S->Value( pTBR.u, pTBR.v );
2004 pTBR.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, pTBR.u, pTBR.v );
2005 pointsRCb.back() = pTBR;
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 // auxiliary 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++) {
2210 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2211 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2217 // fill UVL using c2d
2218 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
2219 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2223 // step2: create faces for right domain
2224 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2226 for (j=1; j<=nr; j++)
2227 NodesR.SetValue(1,j,uv_er[nr-j].node);
2230 for (i=1; i<=dr; i++)
2231 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2232 // create and add needed nodes
2233 TColgp_SequenceOfXY UVtmp;
2234 for (i=1; i<=dr; i++) {
2235 double x0 = npt.Value(nt-i);
2238 double y0 = npl.Value(i+1);
2239 double y1 = npr.Value(i+1);
2240 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2241 gp_Pnt P = S->Value(UV.X(),UV.Y());
2242 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2243 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2244 NodesR.SetValue(i+1,nr,N);
2245 if (UVR.Length()<nbv-nnn) UVR.Append(UV);
2247 for (j=2; j<nr; j++) {
2248 double y0 = npl.Value(nbv-j+1);
2249 double y1 = npr.Value(nbv-j+1);
2250 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2251 gp_Pnt P = S->Value(UV.X(),UV.Y());
2252 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2253 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2254 NodesR.SetValue(i+1,j,N);
2255 if (i==dr) UVtmp.Prepend(UV);
2258 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
2259 UVR.Append(UVtmp.Value(i));
2262 for (i=1; i<=dr; i++) {
2263 for (j=1; j<nr; j++) {
2265 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2266 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2272 // fill UVR using c2d
2273 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
2274 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2278 // step3: create faces for central domain
2279 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2280 // add first line using NodesL
2281 for (i=1; i<=dl+1; i++)
2282 NodesC.SetValue(1,i,NodesL(i,1));
2283 for (i=2; i<=nl; i++)
2284 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2285 // add last line using NodesR
2286 for (i=1; i<=dr+1; i++)
2287 NodesC.SetValue(nb,i,NodesR(i,nr));
2288 for (i=1; i<nr; i++)
2289 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2290 // add top nodes (last columns)
2291 for (i=dl+2; i<nbh-dr; i++)
2292 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2293 // add bottom nodes (first columns)
2294 for (i=2; i<nb; i++)
2295 NodesC.SetValue(i,1,uv_eb[i-1].node);
2297 // create and add needed nodes
2298 // add linear layers
2299 for (i=2; i<nb; i++) {
2300 double x0 = npt.Value(dl+i);
2302 for (j=1; j<nnn; j++) {
2303 double y0 = npl.Value(nbv-nnn+j);
2304 double y1 = npr.Value(nbv-nnn+j);
2305 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2306 gp_Pnt P = S->Value(UV.X(),UV.Y());
2307 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2308 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2309 NodesC.SetValue(i,nbv-nnn+j,N);
2314 // add diagonal layers
2315 gp_UV A2 = UVR.Value(nbv-nnn);
2316 gp_UV A3 = UVL.Value(nbv-nnn);
2317 for (i=1; i<nbv-nnn; i++) {
2318 gp_UV p1 = UVR.Value(i);
2319 gp_UV p3 = UVL.Value(i);
2320 double y = i / double(nbv-nnn);
2321 for (j=2; j<nb; j++) {
2322 double x = npb.Value(j);
2323 gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
2324 gp_UV p2 = UVT.Value( j-1 );
2325 gp_UV UV = calcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
2326 gp_Pnt P = S->Value(UV.X(),UV.Y());
2327 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2328 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2329 NodesC.SetValue(j,i+1,N);
2333 for (i=1; i<nb; i++) {
2334 for (j=1; j<nbv; j++) {
2336 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2337 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2343 else { // New version (!OldVersion)
2344 // step1: create faces for bottom rectangle domain
2345 StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
2346 // fill UVL and UVR using c2d
2347 for (j=0; j<nb; j++) {
2348 NodesBRD.SetValue(j+1,1,uv_eb[j].node);
2350 for (i=1; i<nnn-1; i++) {
2351 NodesBRD.SetValue(1,i+1,uv_el[i].node);
2352 NodesBRD.SetValue(nb,i+1,uv_er[i].node);
2353 for (j=2; j<nb; j++) {
2354 double x = npb.Value(j);
2355 double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
2356 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2357 gp_Pnt P = S->Value(UV.X(),UV.Y());
2358 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2359 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2360 NodesBRD.SetValue(j,i+1,N);
2363 for (j=1; j<nnn-1; j++) {
2364 for (i=1; i<nb; i++) {
2366 myHelper->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
2367 NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
2371 int drl = abs(nr-nl);
2372 // create faces for region C
2373 StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
2374 // add nodes from previous region
2375 for (j=1; j<=nb; j++) {
2376 NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
2378 if ((drl+addv) > 0) {
2383 TColgp_SequenceOfXY UVtmp;
2384 double drparam = npr.Value(nr) - npr.Value(nnn-1);
2385 double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
2386 double y0 = 0, y1 = 0;
2387 for (i=1; i<=drl; i++) {
2388 // add existed nodes from right edge
2389 NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
2390 //double dtparam = npt.Value(i+1);
2391 y1 = npr.Value(nnn+i-1); // param on right edge
2392 double dpar = (y1 - npr.Value(nnn-1))/drparam;
2393 y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
2394 double dy = y1 - y0;
2395 for (j=1; j<nb; j++) {
2396 double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
2397 double y = y0 + dy*x;
2398 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2399 gp_Pnt P = S->Value(UV.X(),UV.Y());
2400 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2401 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2402 NodesC.SetValue(j,i+1,N);
2405 double dy0 = (1-y0)/(addv+1);
2406 double dy1 = (1-y1)/(addv+1);
2407 for (i=1; i<=addv; i++) {
2408 double yy0 = y0 + dy0*i;
2409 double yy1 = y1 + dy1*i;
2410 double dyy = yy1 - yy0;
2411 for (j=1; j<=nb; j++) {
2412 double x = npt.Value(i+1+drl) +
2413 npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
2414 double y = yy0 + dyy*x;
2415 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2416 gp_Pnt P = S->Value(UV.X(),UV.Y());
2417 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2418 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2419 NodesC.SetValue(j,i+drl+1,N);
2426 TColgp_SequenceOfXY UVtmp;
2427 double dlparam = npl.Value(nl) - npl.Value(nnn-1);
2428 double drparam = npr.Value(nnn) - npr.Value(nnn-1);
2429 double y0 = npl.Value(nnn-1);
2430 double y1 = npr.Value(nnn-1);
2431 for (i=1; i<=drl; i++) {
2432 // add existed nodes from right edge
2433 NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
2434 y0 = npl.Value(nnn+i-1); // param on left edge
2435 double dpar = (y0 - npl.Value(nnn-1))/dlparam;
2436 y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
2437 double dy = y1 - y0;
2438 for (j=2; j<=nb; j++) {
2439 double x = npb.Value(j)*npt.Value(nt-i);
2440 double y = y0 + dy*x;
2441 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2442 gp_Pnt P = S->Value(UV.X(),UV.Y());
2443 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2444 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2445 NodesC.SetValue(j,i+1,N);
2448 double dy0 = (1-y0)/(addv+1);
2449 double dy1 = (1-y1)/(addv+1);
2450 for (i=1; i<=addv; i++) {
2451 double yy0 = y0 + dy0*i;
2452 double yy1 = y1 + dy1*i;
2453 double dyy = yy1 - yy0;
2454 for (j=1; j<=nb; j++) {
2455 double x = npt.Value(i+1) +
2456 npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
2457 double y = yy0 + dyy*x;
2458 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2459 gp_Pnt P = S->Value(UV.X(),UV.Y());
2460 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2461 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2462 NodesC.SetValue(j,i+drl+1,N);
2467 for (j=1; j<=drl+addv; j++) {
2468 for (i=1; i<nb; i++) {
2470 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2471 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2476 StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
2477 for (i=1; i<=nt; i++) {
2478 NodesLast.SetValue(i,2,uv_et[i-1].node);
2481 for (i=n1; i<drl+addv+1; i++) {
2483 NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
2485 for (i=1; i<=nb; i++) {
2487 NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
2489 for (i=drl+addv; i>=n2; i--) {
2491 NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
2493 for (i=1; i<nt; i++) {
2495 myHelper->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
2496 NodesLast.Value(i+1,2), NodesLast.Value(i,2));
2499 } // if ((drl+addv) > 0)
2501 } // end new version implementation
2508 //=======================================================================
2510 * Evaluate only quandrangle faces
2512 //=======================================================================
2514 bool StdMeshers_Quadrangle_2D::evaluateQuadPref(SMESH_Mesh & aMesh,
2515 const TopoDS_Shape& aShape,
2516 std::vector<int>& aNbNodes,
2517 MapShapeNbElems& aResMap,
2520 // Auxiliary key in order to keep old variant
2521 // of meshing after implementation new variant
2522 // for bug 0016220 from Mantis.
2523 bool OldVersion = false;
2524 if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
2527 const TopoDS_Face& F = TopoDS::Face(aShape);
2528 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2530 int nb = aNbNodes[0];
2531 int nr = aNbNodes[1];
2532 int nt = aNbNodes[2];
2533 int nl = aNbNodes[3];
2534 int dh = abs(nb-nt);
2535 int dv = abs(nr-nl);
2539 // it is a base case => not shift
2542 // we have to shift on 2
2551 // we have to shift quad on 1
2558 // we have to shift quad on 3
2568 int nbh = Max(nb,nt);
2569 int nbv = Max(nr,nl);
2584 // add some params to right and left after the first param
2591 int nnn = Min(nr,nl);
2596 // step1: create faces for left domain
2598 nbNodes += dl*(nl-1);
2599 nbFaces += dl*(nl-1);
2601 // step2: create faces for right domain
2603 nbNodes += dr*(nr-1);
2604 nbFaces += dr*(nr-1);
2606 // step3: create faces for central domain
2607 nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2608 nbFaces += (nb-1)*(nbv-1);
2610 else { // New version (!OldVersion)
2611 nbNodes += (nnn-2)*(nb-2);
2612 nbFaces += (nnn-2)*(nb-1);
2613 int drl = abs(nr-nl);
2614 nbNodes += drl*(nb-1) + addv*nb;
2615 nbFaces += (drl+addv)*(nb-1) + (nt-1);
2616 } // end new version implementation
2618 std::vector<int> aVec(SMDSEntity_Last);
2619 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2621 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2622 aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2623 if (aNbNodes.size()==5) {
2624 aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2625 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2629 aVec[SMDSEntity_Node] = nbNodes;
2630 aVec[SMDSEntity_Quadrangle] = nbFaces;
2631 if (aNbNodes.size()==5) {
2632 aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2633 aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2636 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2637 aResMap.insert(std::make_pair(sm,aVec));
2642 //=============================================================================
2643 /*! Split quadrangle in to 2 triangles by smallest diagonal
2646 //=============================================================================
2648 void StdMeshers_Quadrangle_2D::splitQuadFace(SMESHDS_Mesh * theMeshDS,
2650 const SMDS_MeshNode* theNode1,
2651 const SMDS_MeshNode* theNode2,
2652 const SMDS_MeshNode* theNode3,
2653 const SMDS_MeshNode* theNode4)
2655 if ( SMESH_TNodeXYZ( theNode1 ).SquareDistance( theNode3 ) >
2656 SMESH_TNodeXYZ( theNode2 ).SquareDistance( theNode4 ) )
2658 myHelper->AddFace(theNode2, theNode4 , theNode1);
2659 myHelper->AddFace(theNode2, theNode3, theNode4);
2663 myHelper->AddFace(theNode1, theNode2 ,theNode3);
2664 myHelper->AddFace(theNode1, theNode3, theNode4);
2670 enum uvPos { UV_A0, UV_A1, UV_A2, UV_A3, UV_B, UV_R, UV_T, UV_L, UV_SIZE };
2672 inline SMDS_MeshNode* makeNode( UVPtStruct & uvPt,
2674 FaceQuadStruct::Ptr& quad,
2676 SMESH_MesherHelper* helper,
2677 Handle(Geom_Surface) S)
2679 const vector<UVPtStruct>& uv_eb = quad->side[QUAD_BOTTOM_SIDE].GetUVPtStruct();
2680 const vector<UVPtStruct>& uv_et = quad->side[QUAD_TOP_SIDE ].GetUVPtStruct();
2681 double rBot = ( uv_eb.size() - 1 ) * uvPt.normParam;
2682 double rTop = ( uv_et.size() - 1 ) * uvPt.normParam;
2683 int iBot = int( rBot );
2684 int iTop = int( rTop );
2685 double xBot = uv_eb[ iBot ].normParam + ( rBot - iBot ) * ( uv_eb[ iBot+1 ].normParam - uv_eb[ iBot ].normParam );
2686 double xTop = uv_et[ iTop ].normParam + ( rTop - iTop ) * ( uv_et[ iTop+1 ].normParam - uv_et[ iTop ].normParam );
2687 double x = xBot + y * ( xTop - xBot );
2689 gp_UV uv = calcUV(/*x,y=*/x, y,
2690 /*a0,...=*/UVs[UV_A0], UVs[UV_A1], UVs[UV_A2], UVs[UV_A3],
2691 /*p0=*/quad->side[QUAD_BOTTOM_SIDE].grid->Value2d( x ).XY(),
2693 /*p2=*/quad->side[QUAD_TOP_SIDE ].grid->Value2d( x ).XY(),
2694 /*p3=*/UVs[ UV_L ]);
2695 gp_Pnt P = S->Value( uv.X(), uv.Y() );
2698 return helper->AddNode(P.X(), P.Y(), P.Z(), 0, uv.X(), uv.Y() );
2701 void reduce42( const vector<UVPtStruct>& curr_base,
2702 vector<UVPtStruct>& next_base,
2704 int & next_base_len,
2705 FaceQuadStruct::Ptr& quad,
2708 SMESH_MesherHelper* helper,
2709 Handle(Geom_Surface)& S)
2711 // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2713 // .-----a-----b i + 1
2724 const SMDS_MeshNode*& Na = next_base[ ++next_base_len ].node;
2726 Na = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2729 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2731 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2734 double u = (curr_base[j + 2].u + next_base[next_base_len - 2].u) / 2.0;
2735 double v = (curr_base[j + 2].v + next_base[next_base_len - 2].v) / 2.0;
2736 gp_Pnt P = S->Value(u,v);
2737 SMDS_MeshNode* Nc = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2740 u = (curr_base[j + 2].u + next_base[next_base_len - 1].u) / 2.0;
2741 v = (curr_base[j + 2].v + next_base[next_base_len - 1].v) / 2.0;
2743 SMDS_MeshNode* Nd = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2746 u = (curr_base[j + 2].u + next_base[next_base_len].u) / 2.0;
2747 v = (curr_base[j + 2].v + next_base[next_base_len].v) / 2.0;
2749 SMDS_MeshNode* Ne = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2752 helper->AddFace(curr_base[j + 0].node,
2753 curr_base[j + 1].node, Nc,
2754 next_base[next_base_len - 2].node);
2756 helper->AddFace(curr_base[j + 1].node,
2757 curr_base[j + 2].node, Nd, Nc);
2759 helper->AddFace(curr_base[j + 2].node,
2760 curr_base[j + 3].node, Ne, Nd);
2762 helper->AddFace(curr_base[j + 3].node,
2763 curr_base[j + 4].node, Nb, Ne);
2765 helper->AddFace(Nc, Nd, Na, next_base[next_base_len - 2].node);
2767 helper->AddFace(Nd, Ne, Nb, Na);
2770 void reduce31( const vector<UVPtStruct>& curr_base,
2771 vector<UVPtStruct>& next_base,
2773 int & next_base_len,
2774 FaceQuadStruct::Ptr& quad,
2777 SMESH_MesherHelper* helper,
2778 Handle(Geom_Surface)& S)
2780 // add one "H": nodes b,c,e and faces 1,2,4,5
2782 // .---------b i + 1
2793 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2795 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2798 double u1 = (curr_base[ j ].u + next_base[ next_base_len-1 ].u ) / 2.0;
2799 double u2 = (curr_base[ j+3 ].u + next_base[ next_base_len ].u ) / 2.0;
2800 double u3 = (u2 - u1) / 3.0;
2802 double v1 = (curr_base[ j ].v + next_base[ next_base_len-1 ].v ) / 2.0;
2803 double v2 = (curr_base[ j+3 ].v + next_base[ next_base_len ].v ) / 2.0;
2804 double v3 = (v2 - v1) / 3.0;
2808 gp_Pnt P = S->Value(u,v);
2809 SMDS_MeshNode* Nc = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2814 SMDS_MeshNode* Ne = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2818 helper->AddFace( curr_base[ j + 0 ].node,
2819 curr_base[ j + 1 ].node,
2821 next_base[ next_base_len - 1 ].node);
2823 helper->AddFace( curr_base[ j + 1 ].node,
2824 curr_base[ j + 2 ].node, Ne, Nc);
2826 helper->AddFace( curr_base[ j + 2 ].node,
2827 curr_base[ j + 3 ].node, Nb, Ne);
2829 helper->AddFace(Nc, Ne, Nb,
2830 next_base[ next_base_len - 1 ].node);
2833 typedef void (* PReduceFunction) ( const vector<UVPtStruct>& curr_base,
2834 vector<UVPtStruct>& next_base,
2836 int & next_base_len,
2837 FaceQuadStruct::Ptr & quad,
2840 SMESH_MesherHelper* helper,
2841 Handle(Geom_Surface)& S);
2845 //=======================================================================
2847 * Implementation of Reduced algorithm (meshing with quadrangles only)
2849 //=======================================================================
2851 bool StdMeshers_Quadrangle_2D::computeReduced (SMESH_Mesh & aMesh,
2852 const TopoDS_Face& aFace,
2853 FaceQuadStruct::Ptr quad)
2855 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2856 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
2857 int i,j,geomFaceID = meshDS->ShapeToIndex(aFace);
2859 int nb = quad->side[0].NbPoints(); // bottom
2860 int nr = quad->side[1].NbPoints(); // right
2861 int nt = quad->side[2].NbPoints(); // top
2862 int nl = quad->side[3].NbPoints(); // left
2864 // Simple Reduce 10->8->6->4 (3 steps) Multiple Reduce 10->4 (1 step)
2866 // .-----.-----.-----.-----. .-----.-----.-----.-----.
2867 // | / \ | / \ | | / \ | / \ |
2868 // | / .--.--. \ | | / \ | / \ |
2869 // | / / | \ \ | | / .----.----. \ |
2870 // .---.---.---.---.---.---. | / / \ | / \ \ |
2871 // | / / \ | / \ \ | | / / \ | / \ \ |
2872 // | / / .-.-. \ \ | | / / .---.---. \ \ |
2873 // | / / / | \ \ \ | | / / / \ | / \ \ \ |
2874 // .--.--.--.--.--.--.--.--. | / / / \ | / \ \ \ |
2875 // | / / / \ | / \ \ \ | | / / / .-.-. \ \ \ |
2876 // | / / / .-.-. \ \ \ | | / / / / | \ \ \ \ |
2877 // | / / / / | \ \ \ \ | | / / / / | \ \ \ \ |
2878 // .-.-.-.--.--.--.--.-.-.-. .-.-.-.--.--.--.--.-.-.-.
2880 bool MultipleReduce = false;
2892 else if (nb == nt) {
2893 nr1 = nb; // and == nt
2907 // number of rows and columns
2908 int nrows = nr1 - 1;
2909 int ncol_top = nt1 - 1;
2910 int ncol_bot = nb1 - 1;
2911 // number of rows needed to reduce ncol_bot to ncol_top using simple 3->1 "tree" (see below)
2913 int( ceil( log( double(ncol_bot) / ncol_top) / log( 3.))); // = log x base 3
2914 if ( nrows < nrows_tree31 )
2916 MultipleReduce = true;
2917 error( COMPERR_WARNING,
2918 SMESH_Comment("To use 'Reduced' transition, "
2919 "number of face rows should be at least ")
2920 << nrows_tree31 << ". Actual number of face rows is " << nrows << ". "
2921 "'Quadrangle preference (reversed)' transion has been used.");
2925 if (MultipleReduce) { // == computeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2926 //==================================================
2927 int dh = abs(nb-nt);
2928 int dv = abs(nr-nl);
2932 // it is a base case => not shift quad but may be replacement is need
2936 // we have to shift quad on 2
2942 // we have to shift quad on 1
2946 // we have to shift quad on 3
2951 nb = quad->side[0].NbPoints();
2952 nr = quad->side[1].NbPoints();
2953 nt = quad->side[2].NbPoints();
2954 nl = quad->side[3].NbPoints();
2957 int nbh = Max(nb,nt);
2958 int nbv = Max(nr,nl);
2971 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
2972 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
2973 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
2974 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
2976 if ((int) uv_eb.size() != nb || (int) uv_er.size() != nr ||
2977 (int) uv_et.size() != nt || (int) uv_el.size() != nl)
2978 return error(COMPERR_BAD_INPUT_MESH);
2980 // arrays for normalized params
2981 TColStd_SequenceOfReal npb, npr, npt, npl;
2982 for (j = 0; j < nb; j++) {
2983 npb.Append(uv_eb[j].normParam);
2985 for (i = 0; i < nr; i++) {
2986 npr.Append(uv_er[i].normParam);
2988 for (j = 0; j < nt; j++) {
2989 npt.Append(uv_et[j].normParam);
2991 for (i = 0; i < nl; i++) {
2992 npl.Append(uv_el[i].normParam);
2996 // orientation of face and 3 main domain for future faces
3002 // left | | | | rigth
3009 // add some params to right and left after the first param
3012 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
3013 for (i=1; i<=dr; i++) {
3014 npr.InsertAfter(1,npr.Value(2)-dpr);
3018 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
3019 for (i=1; i<=dl; i++) {
3020 npl.InsertAfter(1,npl.Value(2)-dpr);
3023 gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
3024 gp_XY a1 (uv_eb.back().u, uv_eb.back().v);
3025 gp_XY a2 (uv_et.back().u, uv_et.back().v);
3026 gp_XY a3 (uv_et.front().u, uv_et.front().v);
3028 int nnn = Min(nr,nl);
3029 // auxiliary sequence of XY for creation of nodes
3030 // in the bottom part of central domain
3031 // it's length must be == nbv-nnn-1
3032 TColgp_SequenceOfXY UVL;
3033 TColgp_SequenceOfXY UVR;
3034 //==================================================
3036 // step1: create faces for left domain
3037 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
3039 for (j=1; j<=nl; j++)
3040 NodesL.SetValue(1,j,uv_el[j-1].node);
3043 for (i=1; i<=dl; i++)
3044 NodesL.SetValue(i+1,nl,uv_et[i].node);
3045 // create and add needed nodes
3046 TColgp_SequenceOfXY UVtmp;
3047 for (i=1; i<=dl; i++) {
3048 double x0 = npt.Value(i+1);
3051 double y0 = npl.Value(i+1);
3052 double y1 = npr.Value(i+1);
3053 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3054 gp_Pnt P = S->Value(UV.X(),UV.Y());
3055 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3056 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3057 NodesL.SetValue(i+1,1,N);
3058 if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
3060 for (j=2; j<nl; j++) {
3061 double y0 = npl.Value(dl+j);
3062 double y1 = npr.Value(dl+j);
3063 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3064 gp_Pnt P = S->Value(UV.X(),UV.Y());
3065 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3066 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3067 NodesL.SetValue(i+1,j,N);
3068 if (i==dl) UVtmp.Append(UV);
3071 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
3072 UVL.Append(UVtmp.Value(i));
3075 for (i=1; i<=dl; i++) {
3076 for (j=1; j<nl; j++) {
3077 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
3078 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
3083 // fill UVL using c2d
3084 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
3085 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
3089 // step2: create faces for right domain
3090 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
3092 for (j=1; j<=nr; j++)
3093 NodesR.SetValue(1,j,uv_er[nr-j].node);
3096 for (i=1; i<=dr; i++)
3097 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
3098 // create and add needed nodes
3099 TColgp_SequenceOfXY UVtmp;
3100 for (i=1; i<=dr; i++) {
3101 double x0 = npt.Value(nt-i);
3104 double y0 = npl.Value(i+1);
3105 double y1 = npr.Value(i+1);
3106 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3107 gp_Pnt P = S->Value(UV.X(),UV.Y());
3108 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3109 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3110 NodesR.SetValue(i+1,nr,N);
3111 if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
3113 for (j=2; j<nr; j++) {
3114 double y0 = npl.Value(nbv-j+1);
3115 double y1 = npr.Value(nbv-j+1);
3116 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3117 gp_Pnt P = S->Value(UV.X(),UV.Y());
3118 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3119 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3120 NodesR.SetValue(i+1,j,N);
3121 if (i==dr) UVtmp.Prepend(UV);
3124 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
3125 UVR.Append(UVtmp.Value(i));
3128 for (i=1; i<=dr; i++) {
3129 for (j=1; j<nr; j++) {
3130 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
3131 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
3136 // fill UVR using c2d
3137 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
3138 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
3142 // step3: create faces for central domain
3143 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
3144 // add first line using NodesL
3145 for (i=1; i<=dl+1; i++)
3146 NodesC.SetValue(1,i,NodesL(i,1));
3147 for (i=2; i<=nl; i++)
3148 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
3149 // add last line using NodesR
3150 for (i=1; i<=dr+1; i++)
3151 NodesC.SetValue(nb,i,NodesR(i,nr));
3152 for (i=1; i<nr; i++)
3153 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
3154 // add top nodes (last columns)
3155 for (i=dl+2; i<nbh-dr; i++)
3156 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
3157 // add bottom nodes (first columns)
3158 for (i=2; i<nb; i++)
3159 NodesC.SetValue(i,1,uv_eb[i-1].node);
3161 // create and add needed nodes
3162 // add linear layers
3163 for (i=2; i<nb; i++) {
3164 double x0 = npt.Value(dl+i);
3166 for (j=1; j<nnn; j++) {
3167 double y0 = npl.Value(nbv-nnn+j);
3168 double y1 = npr.Value(nbv-nnn+j);
3169 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3170 gp_Pnt P = S->Value(UV.X(),UV.Y());
3171 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3172 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3173 NodesC.SetValue(i,nbv-nnn+j,N);
3176 // add diagonal layers
3177 for (i=1; i<nbv-nnn; i++) {
3178 double du = UVR.Value(i).X() - UVL.Value(i).X();
3179 double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
3180 for (j=2; j<nb; j++) {
3181 double u = UVL.Value(i).X() + du*npb.Value(j);
3182 double v = UVL.Value(i).Y() + dv*npb.Value(j);
3183 gp_Pnt P = S->Value(u,v);
3184 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3185 meshDS->SetNodeOnFace(N, geomFaceID, u, v);
3186 NodesC.SetValue(j,i+1,N);
3190 for (i=1; i<nb; i++) {
3191 for (j=1; j<nbv; j++) {
3192 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
3193 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
3196 } // end Multiple Reduce implementation
3197 else { // Simple Reduce (!MultipleReduce)
3198 //=========================================================
3201 // it is a base case => not shift quad
3202 //shiftQuad(quad,0,true);
3205 // we have to shift quad on 2
3211 // we have to shift quad on 1
3215 // we have to shift quad on 3
3220 nb = quad->side[0].NbPoints();
3221 nr = quad->side[1].NbPoints();
3222 nt = quad->side[2].NbPoints();
3223 nl = quad->side[3].NbPoints();
3225 // number of rows and columns
3226 int nrows = nr - 1; // and also == nl - 1
3227 int ncol_top = nt - 1;
3228 int ncol_bot = nb - 1;
3229 int npair_top = ncol_top / 2;
3230 // maximum number of bottom elements for "linear" simple reduce 4->2
3231 int max_lin42 = ncol_top + npair_top * 2 * nrows;
3232 // maximum number of bottom elements for "linear" simple reduce 3->1
3233 int max_lin31 = ncol_top + ncol_top * 2 * nrows;
3234 // maximum number of bottom elements for "tree" simple reduce 4->2
3236 // number of rows needed to reduce ncol_bot to ncol_top using simple 4->2 "tree"
3237 int nrows_tree42 = int( log( (double)(ncol_bot / ncol_top) )/log((double)2) ); // needed to avoid overflow at pow(2) while computing max_tree42
3238 if (nrows_tree42 < nrows) {
3239 max_tree42 = npair_top * pow(2.0, nrows + 1);
3240 if ( ncol_top > npair_top * 2 ) {
3241 int delta = ncol_bot - max_tree42;
3242 for (int irow = 1; irow < nrows; irow++) {
3243 int nfour = delta / 4;
3246 if (delta <= (ncol_top - npair_top * 2))
3247 max_tree42 = ncol_bot;
3250 // maximum number of bottom elements for "tree" simple reduce 3->1
3251 //int max_tree31 = ncol_top * pow(3.0, nrows);
3252 bool is_lin_31 = false;
3253 bool is_lin_42 = false;
3254 bool is_tree_31 = false;
3255 bool is_tree_42 = false;
3256 int max_lin = max_lin42;
3257 if (ncol_bot > max_lin42) {
3258 if (ncol_bot <= max_lin31) {
3260 max_lin = max_lin31;
3264 // if ncol_bot is a 3*n or not 2*n
3265 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3267 max_lin = max_lin31;
3273 if (ncol_bot > max_lin) { // not "linear"
3274 is_tree_31 = (ncol_bot > max_tree42);
3275 if (ncol_bot <= max_tree42) {
3276 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3285 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
3286 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
3287 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
3288 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
3290 if ((int) uv_eb.size() != nb || (int) uv_er.size() != nr ||
3291 (int) uv_et.size() != nt || (int) uv_el.size() != nl)
3292 return error(COMPERR_BAD_INPUT_MESH);
3294 gp_UV uv[ UV_SIZE ];
3295 uv[ UV_A0 ].SetCoord( uv_eb.front().u, uv_eb.front().v);
3296 uv[ UV_A1 ].SetCoord( uv_eb.back().u, uv_eb.back().v );
3297 uv[ UV_A2 ].SetCoord( uv_et.back().u, uv_et.back().v );
3298 uv[ UV_A3 ].SetCoord( uv_et.front().u, uv_et.front().v);
3300 vector<UVPtStruct> curr_base = uv_eb, next_base;
3302 UVPtStruct nullUVPtStruct;
3303 nullUVPtStruct.node = 0;
3304 nullUVPtStruct.x = nullUVPtStruct.y = nullUVPtStruct.u = nullUVPtStruct.v = 0;
3305 nullUVPtStruct.param = 0;
3308 int curr_base_len = nb;
3309 int next_base_len = 0;
3312 { // ------------------------------------------------------------------
3313 // New algorithm implemented by request of IPAL22856
3314 // "2D quadrangle mesher of reduced type works wrong"
3315 // http://bugtracker.opencascade.com/show_bug.cgi?id=22856
3317 // the algorithm is following: all reduces are centred in horizontal
3318 // direction and are distributed among all rows
3320 if (ncol_bot > max_tree42) {
3324 if ((ncol_top/3)*3 == ncol_top ) {
3332 const int col_top_size = is_lin_42 ? 2 : 1;
3333 const int col_base_size = is_lin_42 ? 4 : 3;
3335 // Compute nb of "columns" (like in "linear" simple reducing) in all rows
3337 vector<int> nb_col_by_row;
3339 int delta_all = nb - nt;
3340 int delta_one_col = nrows * 2;
3341 int nb_col = delta_all / delta_one_col;
3342 int remainder = delta_all - nb_col * delta_one_col;
3343 if (remainder > 0) {
3346 if ( nb_col * col_top_size >= nt ) // == "tree" reducing situation
3348 // top row is full (all elements reduced), add "columns" one by one
3349 // in rows below until all bottom elements are reduced
3350 nb_col = ( nt - 1 ) / col_top_size;
3351 nb_col_by_row.resize( nrows, nb_col );
3352 int nbrows_not_full = nrows - 1;
3353 int cur_top_size = nt - 1;
3354 remainder = delta_all - nb_col * delta_one_col;
3355 while ( remainder > 0 )
3357 delta_one_col = nbrows_not_full * 2;
3358 int nb_col_add = remainder / delta_one_col;
3359 cur_top_size += 2 * nb_col_by_row[ nbrows_not_full ];
3360 int nb_col_free = cur_top_size / col_top_size - nb_col_by_row[ nbrows_not_full-1 ];
3361 if ( nb_col_add > nb_col_free )
3362 nb_col_add = nb_col_free;
3363 for ( int irow = 0; irow < nbrows_not_full; ++irow )
3364 nb_col_by_row[ irow ] += nb_col_add;
3366 remainder -= nb_col_add * delta_one_col;
3369 else // == "linear" reducing situation
3371 nb_col_by_row.resize( nrows, nb_col );
3373 for ( int irow = remainder / 2; irow < nrows; ++irow )
3374 nb_col_by_row[ irow ]--;
3379 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3381 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3383 for (i = 1; i < nr; i++) // layer by layer
3385 nb_col = nb_col_by_row[ i-1 ];
3386 int nb_next = curr_base_len - nb_col * 2;
3387 if (nb_next < nt) nb_next = nt;
3389 const double y = uv_el[ i ].normParam;
3391 if ( i + 1 == nr ) // top
3398 next_base.resize( nb_next, nullUVPtStruct );
3399 next_base.front() = uv_el[i];
3400 next_base.back() = uv_er[i];
3402 // compute normalized param u
3403 double du = 1. / ( nb_next - 1 );
3404 next_base[0].normParam = 0.;
3405 for ( j = 1; j < nb_next; ++j )
3406 next_base[j].normParam = next_base[j-1].normParam + du;
3408 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3409 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3411 int free_left = ( curr_base_len - 1 - nb_col * col_base_size ) / 2;
3412 int free_middle = curr_base_len - 1 - nb_col * col_base_size - 2 * free_left;
3414 // not reduced left elements
3415 for (j = 0; j < free_left; j++)
3418 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3420 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3422 myHelper->AddFace(curr_base[ j ].node,
3423 curr_base[ j+1 ].node,
3425 next_base[ next_base_len-1 ].node);
3428 for (int icol = 1; icol <= nb_col; icol++)
3431 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3433 j += reduce_grp_size;
3435 // elements in the middle of "columns" added for symmetry
3436 if ( free_middle > 0 && ( nb_col % 2 == 0 ) && icol == nb_col / 2 )
3438 for (int imiddle = 1; imiddle <= free_middle; imiddle++) {
3439 // f (i + 1, j + imiddle)
3440 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3442 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3444 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3445 curr_base[ j +imiddle ].node,
3447 next_base[ next_base_len-1 ].node);
3453 // not reduced right elements
3454 for (; j < curr_base_len-1; j++) {
3456 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3458 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3460 myHelper->AddFace(curr_base[ j ].node,
3461 curr_base[ j+1 ].node,
3463 next_base[ next_base_len-1 ].node);
3466 curr_base_len = next_base_len + 1;
3468 curr_base.swap( next_base );
3472 else if ( is_tree_42 || is_tree_31 )
3474 // "tree" simple reduce "42": 2->4->8->16->32->...
3476 // .-------------------------------.-------------------------------. nr
3478 // | \ .---------------.---------------. / |
3480 // .---------------.---------------.---------------.---------------.
3481 // | \ | / | \ | / |
3482 // | \ .-------.-------. / | \ .-------.-------. / |
3483 // | | | | | | | | |
3484 // .-------.-------.-------.-------.-------.-------.-------.-------. i
3485 // |\ | /|\ | /|\ | /|\ | /|
3486 // | \.---.---./ | \.---.---./ | \.---.---./ | \.---.---./ |
3487 // | | | | | | | | | | | | | | | | |
3488 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
3489 // |\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|
3490 // | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
3491 // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3492 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3495 // "tree" simple reduce "31": 1->3->9->27->...
3497 // .-----------------------------------------------------. nr
3499 // | .-----------------. |
3501 // .-----------------.-----------------.-----------------.
3502 // | \ / | \ / | \ / |
3503 // | .-----. | .-----. | .-----. | i
3504 // | | | | | | | | | |
3505 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.
3506 // |\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|
3507 // | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. |
3508 // | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3509 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3512 PReduceFunction reduceFunction = & ( is_tree_42 ? reduce42 : reduce31 );
3514 const int reduce_grp_size = is_tree_42 ? 4 : 3;
3516 for (i = 1; i < nr; i++) // layer by layer
3518 // to stop reducing, if number of nodes reaches nt
3519 int delta = curr_base_len - nt;
3521 // to calculate normalized parameter, we must know number of points in next layer
3522 int nb_reduce_groups = (curr_base_len - 1) / reduce_grp_size;
3523 int nb_next = nb_reduce_groups * (reduce_grp_size-2) + (curr_base_len - nb_reduce_groups*reduce_grp_size);
3524 if (nb_next < nt) nb_next = nt;
3526 const double y = uv_el[ i ].normParam;
3528 if ( i + 1 == nr ) // top
3535 next_base.resize( nb_next, nullUVPtStruct );
3536 next_base.front() = uv_el[i];
3537 next_base.back() = uv_er[i];
3539 // compute normalized param u
3540 double du = 1. / ( nb_next - 1 );
3541 next_base[0].normParam = 0.;
3542 for ( j = 1; j < nb_next; ++j )
3543 next_base[j].normParam = next_base[j-1].normParam + du;
3545 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3546 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3548 for (j = 0; j+reduce_grp_size < curr_base_len && delta > 0; j+=reduce_grp_size, delta-=2)
3550 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3553 // not reduced side elements (if any)
3554 for (; j < curr_base_len-1; j++)
3557 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3559 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3561 myHelper->AddFace(curr_base[ j ].node,
3562 curr_base[ j+1 ].node,
3564 next_base[ next_base_len-1 ].node);
3566 curr_base_len = next_base_len + 1;
3568 curr_base.swap( next_base );
3570 } // end "tree" simple reduce
3572 else if ( is_lin_42 || is_lin_31 ) {
3573 // "linear" simple reduce "31": 2->6->10->14
3575 // .-----------------------------.-----------------------------. nr
3577 // | .---------. | .---------. |
3579 // .---------.---------.---------.---------.---------.---------.
3580 // | / \ / \ | / \ / \ |
3581 // | / .-----. \ | / .-----. \ | i
3582 // | / | | \ | / | | \ |
3583 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.-----.
3584 // | / / \ / \ \ | / / \ / \ \ |
3585 // | / / .-. \ \ | / / .-. \ \ |
3586 // | / / / \ \ \ | / / / \ \ \ |
3587 // .--.----.---.-----.---.-----.-.--.----.---.-----.---.-----.-. 1
3590 // "linear" simple reduce "42": 4->8->12->16
3592 // .---------------.---------------.---------------.---------------. nr
3593 // | \ | / | \ | / |
3594 // | \ .-------.-------. / | \ .-------.-------. / |
3595 // | | | | | | | | |
3596 // .-------.-------.-------.-------.-------.-------.-------.-------.
3597 // | / \ | / \ | / \ | / \ |
3598 // | / \.----.----./ \ | / \.----.----./ \ | i
3599 // | / | | | \ | / | | | \ |
3600 // .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
3601 // | / / \ | / \ \ | / / \ | / \ \ |
3602 // | / / .-.-. \ \ | / / .-.-. \ \ |
3603 // | / / / | \ \ \ | / / / | \ \ \ |
3604 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
3607 // nt = 5, nb = 7, nr = 4
3608 //int delta_all = 2;
3609 //int delta_one_col = 6;
3611 //int remainder = 2;
3612 //if (remainder > 0) nb_col++;
3614 //int free_left = 1;
3616 //int free_middle = 4;
3618 int delta_all = nb - nt;
3619 int delta_one_col = (nr - 1) * 2;
3620 int nb_col = delta_all / delta_one_col;
3621 int remainder = delta_all - nb_col * delta_one_col;
3622 if (remainder > 0) {
3625 const int col_top_size = is_lin_42 ? 2 : 1;
3626 int free_left = ((nt - 1) - nb_col * col_top_size) / 2;
3627 free_left += nr - 2;
3628 int free_middle = (nr - 2) * 2;
3629 if (remainder > 0 && nb_col == 1) {
3630 int nb_rows_short_col = remainder / 2;
3631 int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
3632 free_left -= nb_rows_thrown;
3635 // nt = 5, nb = 17, nr = 4
3636 //int delta_all = 12;
3637 //int delta_one_col = 6;
3639 //int remainder = 0;
3640 //int free_left = 2;
3641 //int free_middle = 4;
3643 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3645 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3647 for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) // layer by layer
3649 // to calculate normalized parameter, we must know number of points in next layer
3650 int nb_next = curr_base_len - nb_col * 2;
3651 if (remainder > 0 && i > remainder / 2)
3652 // take into account short "column"
3654 if (nb_next < nt) nb_next = nt;
3656 const double y = uv_el[ i ].normParam;
3658 if ( i + 1 == nr ) // top
3665 next_base.resize( nb_next, nullUVPtStruct );
3666 next_base.front() = uv_el[i];
3667 next_base.back() = uv_er[i];
3669 // compute normalized param u
3670 double du = 1. / ( nb_next - 1 );
3671 next_base[0].normParam = 0.;
3672 for ( j = 1; j < nb_next; ++j )
3673 next_base[j].normParam = next_base[j-1].normParam + du;
3675 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3676 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3678 // not reduced left elements
3679 for (j = 0; j < free_left; j++)
3682 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3684 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3686 myHelper->AddFace(curr_base[ j ].node,
3687 curr_base[ j+1 ].node,
3689 next_base[ next_base_len-1 ].node);
3692 for (int icol = 1; icol <= nb_col; icol++) {
3694 if (remainder > 0 && icol == nb_col && i > remainder / 2)
3695 // stop short "column"
3699 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3701 j += reduce_grp_size;
3703 // not reduced middle elements
3704 if (icol < nb_col) {
3705 if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3706 // pass middle elements before stopped short "column"
3709 int free_add = free_middle;
3710 if (remainder > 0 && icol == nb_col - 1)
3711 // next "column" is short
3712 free_add -= (nr - 1) - (remainder / 2);
3714 for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3715 // f (i + 1, j + imiddle)
3716 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3718 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3720 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3721 curr_base[ j +imiddle ].node,
3723 next_base[ next_base_len-1 ].node);
3729 // not reduced right elements
3730 for (; j < curr_base_len-1; j++) {
3732 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3734 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3736 myHelper->AddFace(curr_base[ j ].node,
3737 curr_base[ j+1 ].node,
3739 next_base[ next_base_len-1 ].node);
3742 curr_base_len = next_base_len + 1;
3744 curr_base.swap( next_base );
3747 } // end "linear" simple reduce
3752 } // end Simple Reduce implementation
3758 //================================================================================
3759 namespace // data for smoothing
3762 // --------------------------------------------------------------------------------
3764 * \brief Structure used to check validity of node position after smoothing.
3765 * It holds two nodes connected to a smoothed node and belonging to
3772 TTriangle( TSmoothNode* n1=0, TSmoothNode* n2=0 ): _n1(n1), _n2(n2) {}
3774 inline bool IsForward( gp_UV uv ) const;
3776 // --------------------------------------------------------------------------------
3778 * \brief Data of a smoothed node
3784 vector< TTriangle > _triangles; // if empty, then node is not movable
3786 // --------------------------------------------------------------------------------
3787 inline bool TTriangle::IsForward( gp_UV uv ) const
3789 gp_Vec2d v1( uv, _n1->_uv ), v2( uv, _n2->_uv );
3793 //================================================================================
3795 * \brief Returns area of a triangle
3797 //================================================================================
3799 double getArea( const gp_UV uv1, const gp_UV uv2, const gp_UV uv3 )
3801 gp_XY v1 = uv1 - uv2, v2 = uv3 - uv2;
3807 //================================================================================
3809 * \brief Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3811 * WARNING: this method must be called AFTER retrieving UVPtStruct's from quad
3813 //================================================================================
3815 void StdMeshers_Quadrangle_2D::updateDegenUV(FaceQuadStruct::Ptr quad)
3819 // Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3820 // --------------------------------------------------------------------------
3821 for ( unsigned i = 0; i < quad->side.size(); ++i )
3823 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3825 // find which end of the side is on degenerated shape
3827 if ( myHelper->IsDegenShape( uvVec[0].node->getshapeId() ))
3829 else if ( myHelper->IsDegenShape( uvVec.back().node->getshapeId() ))
3830 degenInd = uvVec.size() - 1;
3834 // find another side sharing the degenerated shape
3835 bool isPrev = ( degenInd == 0 );
3836 if ( i >= QUAD_TOP_SIDE )
3838 int i2 = ( isPrev ? ( i + 3 ) : ( i + 1 )) % 4;
3839 const vector<UVPtStruct>& uvVec2 = quad->side[ i2 ].GetUVPtStruct();
3841 if ( uvVec[ degenInd ].node == uvVec2.front().node )
3843 else if ( uvVec[ degenInd ].node == uvVec2.back().node )
3844 degenInd2 = uvVec2.size() - 1;
3846 throw SALOME_Exception( LOCALIZED( "Logical error" ));
3848 // move UV in the middle
3849 uvPtStruct& uv1 = const_cast<uvPtStruct&>( uvVec [ degenInd ]);
3850 uvPtStruct& uv2 = const_cast<uvPtStruct&>( uvVec2[ degenInd2 ]);
3851 uv1.u = uv2.u = 0.5 * ( uv1.u + uv2.u );
3852 uv1.v = uv2.v = 0.5 * ( uv1.v + uv2.v );
3855 else if ( quad->side.size() == 4 /*&& myQuadType == QUAD_STANDARD*/)
3857 // Set number of nodes on a degenerated side to be same as on an opposite side
3858 // ----------------------------------------------------------------------------
3859 for ( size_t i = 0; i < quad->side.size(); ++i )
3861 StdMeshers_FaceSidePtr degSide = quad->side[i];
3862 if ( !myHelper->IsDegenShape( degSide->EdgeID(0) ))
3864 StdMeshers_FaceSidePtr oppSide = quad->side[( i+2 ) % quad->side.size() ];
3865 if ( degSide->NbSegments() == oppSide->NbSegments() )
3868 // make new side data
3869 const vector<UVPtStruct>& uvVecDegOld = degSide->GetUVPtStruct();
3870 const SMDS_MeshNode* n = uvVecDegOld[0].node;
3871 Handle(Geom2d_Curve) c2d = degSide->Curve2d(0);
3872 double f = degSide->FirstU(0), l = degSide->LastU(0);
3873 gp_Pnt2d p1 = uvVecDegOld.front().UV();
3874 gp_Pnt2d p2 = uvVecDegOld.back().UV();
3876 quad->side[i] = StdMeshers_FaceSide::New( oppSide.get(), n, &p1, &p2, c2d, f, l );
3880 //================================================================================
3882 * \brief Perform smoothing of 2D elements on a FACE with ignored degenerated EDGE
3884 //================================================================================
3886 void StdMeshers_Quadrangle_2D::smooth (FaceQuadStruct::Ptr quad)
3888 if ( !myNeedSmooth ) return;
3890 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
3891 const double tol = BRep_Tool::Tolerance( quad->face );
3892 Handle(ShapeAnalysis_Surface) surface = myHelper->GetSurface( quad->face );
3894 if ( myHelper->HasDegeneratedEdges() && myForcedPnts.empty() )
3896 // "smooth" by computing node positions using 3D TFI and further projection
3898 int nbhoriz = quad->iSize;
3899 int nbvertic = quad->jSize;
3901 SMESH_TNodeXYZ a0( quad->UVPt( 0, 0 ).node );
3902 SMESH_TNodeXYZ a1( quad->UVPt( nbhoriz-1, 0 ).node );
3903 SMESH_TNodeXYZ a2( quad->UVPt( nbhoriz-1, nbvertic-1 ).node );
3904 SMESH_TNodeXYZ a3( quad->UVPt( 0, nbvertic-1 ).node );
3906 for (int i = 1; i < nbhoriz-1; i++)
3908 SMESH_TNodeXYZ p0( quad->UVPt( i, 0 ).node );
3909 SMESH_TNodeXYZ p2( quad->UVPt( i, nbvertic-1 ).node );
3910 for (int j = 1; j < nbvertic-1; j++)
3912 SMESH_TNodeXYZ p1( quad->UVPt( nbhoriz-1, j ).node );
3913 SMESH_TNodeXYZ p3( quad->UVPt( 0, j ).node );
3915 UVPtStruct& uvp = quad->UVPt( i, j );
3917 gp_Pnt p = myHelper->calcTFI(uvp.x,uvp.y, a0,a1,a2,a3, p0,p1,p2,p3);
3918 gp_Pnt2d uv = surface->NextValueOfUV( uvp.UV(), p, 10*tol );
3919 gp_Pnt pnew = surface->Value( uv );
3921 meshDS->MoveNode( uvp.node, pnew.X(), pnew.Y(), pnew.Z() );
3929 // Get nodes to smooth
3931 typedef map< const SMDS_MeshNode*, TSmoothNode, TIDCompare > TNo2SmooNoMap;
3932 TNo2SmooNoMap smooNoMap;
3935 set< const SMDS_MeshNode* > fixedNodes;
3936 for ( size_t i = 0; i < myForcedPnts.size(); ++i )
3938 fixedNodes.insert( myForcedPnts[i].node );
3939 if ( myForcedPnts[i].node->getshapeId() != myHelper->GetSubShapeID() )
3941 TSmoothNode & sNode = smooNoMap[ myForcedPnts[i].node ];
3942 sNode._uv = myForcedPnts[i].uv;
3943 sNode._xyz = SMESH_TNodeXYZ( myForcedPnts[i].node );
3946 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( quad->face );
3947 SMDS_NodeIteratorPtr nIt = fSubMesh->GetNodes();
3948 while ( nIt->more() ) // loop on nodes bound to a FACE
3950 const SMDS_MeshNode* node = nIt->next();
3951 TSmoothNode & sNode = smooNoMap[ node ];
3952 sNode._uv = myHelper->GetNodeUV( quad->face, node );
3953 sNode._xyz = SMESH_TNodeXYZ( node );
3954 if ( fixedNodes.count( node ))
3955 continue; // fixed - no triangles
3957 // set sNode._triangles
3958 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator( SMDSAbs_Face );
3959 while ( fIt->more() )
3961 const SMDS_MeshElement* face = fIt->next();
3962 const int nbN = face->NbCornerNodes();
3963 const int nInd = face->GetNodeIndex( node );
3964 const int prevInd = myHelper->WrapIndex( nInd - 1, nbN );
3965 const int nextInd = myHelper->WrapIndex( nInd + 1, nbN );
3966 const SMDS_MeshNode* prevNode = face->GetNode( prevInd );
3967 const SMDS_MeshNode* nextNode = face->GetNode( nextInd );
3968 sNode._triangles.push_back( TTriangle( & smooNoMap[ prevNode ],
3969 & smooNoMap[ nextNode ]));
3972 // set _uv of smooth nodes on FACE boundary
3973 set< StdMeshers_FaceSide* > sidesOnEdge;
3974 list< FaceQuadStruct::Ptr >::iterator q = myQuadList.begin();
3975 for ( ; q != myQuadList.end() ; ++q )
3976 for ( size_t i = 0; i < (*q)->side.size(); ++i )
3977 if ( ! (*q)->side[i].grid->Edge(0).IsNull() &&
3978 //(*q)->nbNodeOut( i ) == 0 &&
3979 sidesOnEdge.insert( (*q)->side[i].grid.get() ).second )
3981 const vector<UVPtStruct>& uvVec = (*q)->side[i].grid->GetUVPtStruct();
3982 for ( unsigned j = 0; j < uvVec.size(); ++j )
3984 TSmoothNode & sNode = smooNoMap[ uvVec[j].node ];
3985 sNode._uv = uvVec[j].UV();
3986 sNode._xyz = SMESH_TNodeXYZ( uvVec[j].node );
3990 // define reference orientation in 2D
3991 TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
3992 for ( ; n2sn != smooNoMap.end(); ++n2sn )
3993 if ( !n2sn->second._triangles.empty() )
3995 if ( n2sn == smooNoMap.end() ) return;
3996 const TSmoothNode & sampleNode = n2sn->second;
3997 const bool refForward = ( sampleNode._triangles[0].IsForward( sampleNode._uv ));
4001 for ( int iLoop = 0; iLoop < 5; ++iLoop )
4003 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
4005 TSmoothNode& sNode = n2sn->second;
4006 if ( sNode._triangles.empty() )
4007 continue; // not movable node
4010 bool isValid = false;
4011 bool use3D = ( iLoop > 2 ); // 3 loops in 2D and 2, in 3D
4015 // compute a new XYZ
4016 gp_XYZ newXYZ (0,0,0);
4017 for ( size_t i = 0; i < sNode._triangles.size(); ++i )
4018 newXYZ += sNode._triangles[i]._n1->_xyz;
4019 newXYZ /= sNode._triangles.size();
4021 // compute a new UV by projection
4022 newUV = surface->NextValueOfUV( sNode._uv, newXYZ, 10*tol ).XY();
4024 // check validity of the newUV
4025 for ( size_t i = 0; i < sNode._triangles.size() && isValid; ++i )
4026 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4030 // compute a new UV by averaging
4031 newUV.SetCoord(0.,0.);
4032 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
4033 newUV += sNode._triangles[i]._n1->_uv;
4034 newUV /= sNode._triangles.size();
4036 // check validity of the newUV
4038 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
4039 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4044 sNode._xyz = surface->Value( newUV ).XYZ();
4049 // Set new XYZ to the smoothed nodes
4051 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
4053 TSmoothNode& sNode = n2sn->second;
4054 if ( sNode._triangles.empty() )
4055 continue; // not movable node
4057 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( n2sn->first );
4058 gp_Pnt xyz = surface->Value( sNode._uv );
4059 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4062 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( sNode._uv.X(), sNode._uv.Y() )));
4065 // Move medium nodes in quadratic mesh
4066 if ( _quadraticMesh )
4068 const TLinkNodeMap& links = myHelper->GetTLinkNodeMap();
4069 TLinkNodeMap::const_iterator linkIt = links.begin();
4070 for ( ; linkIt != links.end(); ++linkIt )
4072 const SMESH_TLink& link = linkIt->first;
4073 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( linkIt->second );
4075 if ( node->getshapeId() != myHelper->GetSubShapeID() )
4076 continue; // medium node is on EDGE or VERTEX
4078 gp_XYZ pm = 0.5 * ( SMESH_TNodeXYZ( link.node1() ) + SMESH_TNodeXYZ( link.node2() ));
4079 gp_XY uvm = myHelper->GetNodeUV( quad->face, node );
4081 gp_Pnt2d uv = surface->NextValueOfUV( uvm, pm, 10*tol );
4082 gp_Pnt xyz = surface->Value( uv );
4084 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( uv.X(), uv.Y() )));
4085 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4090 //================================================================================
4092 * \brief Checks validity of generated faces
4094 //================================================================================
4096 bool StdMeshers_Quadrangle_2D::check()
4098 const bool isOK = true;
4099 if ( !myCheckOri || myQuadList.empty() || !myQuadList.front() || !myHelper )
4102 TopoDS_Face geomFace = TopoDS::Face( myHelper->GetSubShape() );
4103 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
4104 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( geomFace );
4106 if ( geomFace.Orientation() >= TopAbs_INTERNAL ) geomFace.Orientation( TopAbs_FORWARD );
4108 // Get a reference orientation sign
4113 TSideVector wireVec =
4114 StdMeshers_FaceSide::GetFaceWires( geomFace, *myHelper->GetMesh(), true, err, myHelper );
4115 StdMeshers_FaceSidePtr wire = wireVec[0];
4117 // find a right angle VERTEX
4119 double maxAngle = -1e100;
4120 for ( int i = 0; i < wire->NbEdges(); ++i )
4122 int iPrev = myHelper->WrapIndex( i-1, wire->NbEdges() );
4123 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4124 const TopoDS_Edge& e2 = wire->Edge( i );
4125 double angle = myHelper->GetAngle( e1, e2, geomFace, wire->FirstVertex( i ));
4126 if (( maxAngle < angle ) &&
4127 ( 5.* M_PI/180 < angle && angle < 175.* M_PI/180 ))
4133 if ( maxAngle < -2*M_PI ) return isOK;
4135 // get a sign of 2D area of a corner face
4137 int iPrev = myHelper->WrapIndex( iVertex-1, wire->NbEdges() );
4138 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4139 const TopoDS_Edge& e2 = wire->Edge( iVertex );
4141 gp_Vec2d v1, v2; gp_Pnt2d p;
4144 bool rev = ( e1.Orientation() == TopAbs_REVERSED );
4145 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e1, geomFace, u[0], u[1] );
4146 c->D1( u[ !rev ], p, v1 );
4151 bool rev = ( e2.Orientation() == TopAbs_REVERSED );
4152 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e2, geomFace, u[0], u[1] );
4153 c->D1( u[ rev ], p, v2 );
4164 // Look for incorrectly oriented faces
4166 std::list<const SMDS_MeshElement*> badFaces;
4168 const SMDS_MeshNode* nn [ 8 ]; // 8 is just for safety
4170 SMDS_ElemIteratorPtr fIt = fSubMesh->GetElements();
4171 while ( fIt->more() ) // loop on faces bound to a FACE
4173 const SMDS_MeshElement* f = fIt->next();
4175 const int nbN = f->NbCornerNodes();
4176 for ( int i = 0; i < nbN; ++i )
4177 nn[ i ] = f->GetNode( i );
4179 const SMDS_MeshNode* nInFace = 0;
4180 if ( myHelper->HasSeam() )
4181 for ( int i = 0; i < nbN && !nInFace; ++i )
4182 if ( !myHelper->IsSeamShape( nn[i]->getshapeId() ))
4186 for ( int i = 0; i < nbN; ++i )
4187 uv[ i ] = myHelper->GetNodeUV( geomFace, nn[i], nInFace, &toCheckUV );
4192 double sign1 = getArea( uv[0], uv[1], uv[2] );
4193 double sign2 = getArea( uv[0], uv[2], uv[3] );
4194 if ( sign1 * sign2 < 0 )
4196 sign2 = getArea( uv[1], uv[2], uv[3] );
4197 sign1 = getArea( uv[1], uv[3], uv[0] );
4198 if ( sign1 * sign2 < 0 )
4199 continue; // this should not happen
4201 if ( sign1 * okSign < 0 )
4202 badFaces.push_back ( f );
4207 double sign = getArea( uv[0], uv[1], uv[2] );
4208 if ( sign * okSign < 0 )
4209 badFaces.push_back ( f );
4216 if ( !badFaces.empty() )
4218 SMESH_subMesh* fSM = myHelper->GetMesh()->GetSubMesh( geomFace );
4219 SMESH_ComputeErrorPtr& err = fSM->GetComputeError();
4220 err.reset ( new SMESH_ComputeError( COMPERR_ALGO_FAILED,
4221 "Inverted elements generated"));
4222 err->myBadElements.swap( badFaces );
4230 //================================================================================
4232 * \brief Finds vertices at the most sharp face corners
4233 * \param [in] theFace - the FACE
4234 * \param [in,out] theWire - the ordered edges of the face. It can be modified to
4235 * have the first VERTEX of the first EDGE in \a vertices
4236 * \param [out] theVertices - the found corner vertices in the order corresponding to
4237 * the order of EDGEs in \a theWire
4238 * \param [out] theNbDegenEdges - nb of degenerated EDGEs in theFace
4239 * \param [in] theConsiderMesh - if \c true, only meshed VERTEXes are considered
4240 * as possible corners
4241 * \return int - number of quad sides found: 0, 3 or 4
4243 //================================================================================
4245 int StdMeshers_Quadrangle_2D::getCorners(const TopoDS_Face& theFace,
4246 SMESH_Mesh & theMesh,
4247 std::list<TopoDS_Edge>& theWire,
4248 std::vector<TopoDS_Vertex>& theVertices,
4249 int & theNbDegenEdges,
4250 const bool theConsiderMesh)
4252 theNbDegenEdges = 0;
4254 SMESH_MesherHelper helper( theMesh );
4256 helper.CopySubShapeInfo( *myHelper );
4257 StdMeshers_FaceSide faceSide( theFace, theWire, &theMesh,
4258 /*isFwd=*/true, /*skipMedium=*/true, &helper );
4260 // sort theVertices by angle
4261 multimap<double, TopoDS_Vertex> vertexByAngle;
4262 TopTools_DataMapOfShapeReal angleByVertex;
4263 TopoDS_Edge prevE = theWire.back();
4264 if ( SMESH_Algo::isDegenerated( prevE ))
4266 list<TopoDS_Edge>::reverse_iterator edge = ++theWire.rbegin();
4267 while ( SMESH_Algo::isDegenerated( *edge ))
4269 if ( edge == theWire.rend() )
4273 list<TopoDS_Edge>::iterator edge = theWire.begin();
4274 for ( int iE = 0; edge != theWire.end(); ++edge, ++iE )
4276 if ( SMESH_Algo::isDegenerated( *edge ))
4281 if ( !theConsiderMesh || faceSide.VertexNode( iE ))
4283 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4284 double angle = helper.GetAngle( prevE, *edge, theFace, v );
4285 vertexByAngle.insert( make_pair( angle, v ));
4286 angleByVertex.Bind( v, angle );
4291 // find out required nb of corners (3 or 4)
4293 TopoDS_Shape triaVertex = helper.GetMeshDS()->IndexToShape( myTriaVertexID );
4294 if ( !triaVertex.IsNull() &&
4295 triaVertex.ShapeType() == TopAbs_VERTEX &&
4296 helper.IsSubShape( triaVertex, theFace ) &&
4297 ( vertexByAngle.size() != 4 || vertexByAngle.begin()->first < 5 * M_PI/180. ))
4300 triaVertex.Nullify();
4302 // check nb of available corners
4303 if ( faceSide.NbEdges() < nbCorners )
4304 return error(COMPERR_BAD_SHAPE,
4305 TComm("Face must have 4 sides but not ") << faceSide.NbEdges() );
4307 if ( theConsiderMesh )
4309 const int nbSegments = Max( faceSide.NbPoints()-1, faceSide.NbSegments() );
4310 if ( nbSegments < nbCorners )
4311 return error(COMPERR_BAD_INPUT_MESH, TComm("Too few boundary nodes: ") << nbSegments);
4314 if ( nbCorners == 3 )
4316 if ( vertexByAngle.size() < 3 )
4317 return error(COMPERR_BAD_SHAPE,
4318 TComm("Face must have 3 sides but not ") << vertexByAngle.size() );
4322 if ( vertexByAngle.size() == 3 && theNbDegenEdges == 0 )
4324 if ( myTriaVertexID < 1 )
4325 return error(COMPERR_BAD_PARMETERS,
4326 "No Base vertex provided for a trilateral geometrical face");
4328 TComm comment("Invalid Base vertex: ");
4329 comment << myTriaVertexID << " its ID is not among [ ";
4330 multimap<double, TopoDS_Vertex>::iterator a2v = vertexByAngle.begin();
4331 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4332 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4333 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << " ]";
4334 return error(COMPERR_BAD_PARMETERS, comment );
4336 if ( vertexByAngle.size() + ( theNbDegenEdges > 0 ) < 4 &&
4337 vertexByAngle.size() + theNbDegenEdges != 4 )
4338 return error(COMPERR_BAD_SHAPE,
4339 TComm("Face must have 4 sides but not ") << vertexByAngle.size() );
4342 // put all corner vertices in a map
4343 TopTools_MapOfShape vMap;
4344 if ( nbCorners == 3 )
4345 vMap.Add( triaVertex );
4346 multimap<double, TopoDS_Vertex>::reverse_iterator a2v = vertexByAngle.rbegin();
4347 for ( int iC = 0; a2v != vertexByAngle.rend() && iC < nbCorners; ++a2v, ++iC )
4348 vMap.Add( (*a2v).second );
4350 // check if there are possible variations in choosing corners
4351 bool haveVariants = false;
4352 if ((int) vertexByAngle.size() > nbCorners )
4354 double lostAngle = a2v->first;
4355 double lastAngle = ( --a2v, a2v->first );
4356 haveVariants = ( lostAngle * 1.1 >= lastAngle );
4359 const double angleTol = 5.* M_PI/180;
4360 myCheckOri = ( (int)vertexByAngle.size() > nbCorners ||
4361 vertexByAngle.begin()->first < angleTol );
4363 // make theWire begin from a corner vertex or triaVertex
4364 if ( nbCorners == 3 )
4365 while ( !triaVertex.IsSame( ( helper.IthVertex( 0, theWire.front() ))) ||
4366 SMESH_Algo::isDegenerated( theWire.front() ))
4367 theWire.splice( theWire.end(), theWire, theWire.begin() );
4369 while ( !vMap.Contains( helper.IthVertex( 0, theWire.front() )) ||
4370 SMESH_Algo::isDegenerated( theWire.front() ))
4371 theWire.splice( theWire.end(), theWire, theWire.begin() );
4373 // fill the result vector and prepare for its refinement
4374 theVertices.clear();
4375 vector< double > angles;
4376 vector< TopoDS_Edge > edgeVec;
4377 vector< int > cornerInd, nbSeg;
4379 angles .reserve( vertexByAngle.size() );
4380 edgeVec.reserve( vertexByAngle.size() );
4381 nbSeg .reserve( vertexByAngle.size() );
4382 cornerInd.reserve( nbCorners );
4383 for ( edge = theWire.begin(); edge != theWire.end(); ++edge )
4385 if ( SMESH_Algo::isDegenerated( *edge ))
4387 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4388 bool isCorner = vMap.Contains( v );
4391 theVertices.push_back( v );
4392 cornerInd.push_back( angles.size() );
4394 angles .push_back( angleByVertex.IsBound( v ) ? angleByVertex( v ) : -M_PI );
4395 edgeVec.push_back( *edge );
4396 if ( theConsiderMesh && haveVariants )
4398 if ( SMESHDS_SubMesh* sm = helper.GetMeshDS()->MeshElements( *edge ))
4399 nbSeg.push_back( sm->NbNodes() + 1 );
4401 nbSeg.push_back( 0 );
4402 nbSegTot += nbSeg.back();
4406 // refine the result vector - make sides equal by length if
4407 // there are several equal angles
4410 if ( nbCorners == 3 )
4411 angles[0] = 2 * M_PI; // not to move the base triangle VERTEX
4413 // here we refer to VERTEX'es and EDGEs by indices in angles and edgeVec vectors
4414 typedef int TGeoIndex;
4416 // for each vertex find a vertex till which there are nbSegHalf segments
4417 const int nbSegHalf = ( nbSegTot % 2 || nbCorners == 3 ) ? 0 : nbSegTot / 2;
4418 vector< TGeoIndex > halfDivider( angles.size(), -1 );
4419 int nbHalfDividers = 0;
4422 // get min angle of corners
4423 double minAngle = 10.;
4424 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4425 minAngle = Min( minAngle, angles[ cornerInd[ iC ]]);
4427 // find halfDivider's
4428 for ( TGeoIndex iV1 = 0; iV1 < TGeoIndex( angles.size() ); ++iV1 )
4431 TGeoIndex iV2 = iV1;
4433 nbSegs += nbSeg[ iV2 ];
4434 iV2 = helper.WrapIndex( iV2 + 1, nbSeg.size() );
4435 } while ( nbSegs < nbSegHalf );
4437 if ( nbSegs == nbSegHalf &&
4438 angles[ iV1 ] + angleTol >= minAngle &&
4439 angles[ iV2 ] + angleTol >= minAngle )
4441 halfDivider[ iV1 ] = iV2;
4447 set< TGeoIndex > refinedCorners, treatedCorners;
4448 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4450 TGeoIndex iV = cornerInd[iC];
4451 if ( !treatedCorners.insert( iV ).second )
4453 list< TGeoIndex > equVerts; // inds of vertices that can become corners
4454 equVerts.push_back( iV );
4455 int nbC[2] = { 0, 0 };
4456 // find equal angles backward and forward from the iV-th corner vertex
4457 for ( int isFwd = 0; isFwd < 2; ++isFwd )
4459 int dV = isFwd ? +1 : -1;
4460 int iCNext = helper.WrapIndex( iC + dV, cornerInd.size() );
4461 TGeoIndex iVNext = helper.WrapIndex( iV + dV, angles.size() );
4462 while ( iVNext != iV )
4464 bool equal = Abs( angles[iV] - angles[iVNext] ) < angleTol;
4466 equVerts.insert( isFwd ? equVerts.end() : equVerts.begin(), iVNext );
4467 if ( iVNext == cornerInd[ iCNext ])
4471 if ( angles[iV] < angles[iVNext] )
4472 refinedCorners.insert( iVNext );
4476 treatedCorners.insert( cornerInd[ iCNext ] );
4477 iCNext = helper.WrapIndex( iCNext + dV, cornerInd.size() );
4479 iVNext = helper.WrapIndex( iVNext + dV, angles.size() );
4482 break; // all angles equal
4485 const bool allCornersSame = ( nbC[0] == 3 );
4486 if ( allCornersSame && nbHalfDividers > 0 )
4488 // select two halfDivider's as corners
4489 TGeoIndex hd1, hd2 = -1;
4491 for ( iC2 = 0; iC2 < cornerInd.size() && hd2 < 0; ++iC2 )
4493 hd1 = cornerInd[ iC2 ];
4494 hd2 = halfDivider[ hd1 ];
4495 if ( std::find( equVerts.begin(), equVerts.end(), hd2 ) == equVerts.end() )
4496 hd2 = -1; // hd2-th vertex can't become a corner
4502 angles[ hd1 ] = 2 * M_PI; // make hd1-th vertex no more "equal"
4503 angles[ hd2 ] = 2 * M_PI;
4504 refinedCorners.insert( hd1 );
4505 refinedCorners.insert( hd2 );
4506 treatedCorners = refinedCorners;
4508 equVerts.push_front( equVerts.back() );
4509 equVerts.push_back( equVerts.front() );
4510 list< TGeoIndex >::iterator hdPos =
4511 std::find( equVerts.begin(), equVerts.end(), hd2 );
4512 if ( hdPos == equVerts.end() ) break;
4513 cornerInd[ helper.WrapIndex( iC2 + 0, cornerInd.size()) ] = hd1;
4514 cornerInd[ helper.WrapIndex( iC2 + 1, cornerInd.size()) ] = *( --hdPos );
4515 cornerInd[ helper.WrapIndex( iC2 + 2, cornerInd.size()) ] = hd2;
4516 cornerInd[ helper.WrapIndex( iC2 + 3, cornerInd.size()) ] = *( ++hdPos, ++hdPos );
4518 theVertices[ 0 ] = helper.IthVertex( 0, edgeVec[ cornerInd[0] ]);
4519 theVertices[ 1 ] = helper.IthVertex( 0, edgeVec[ cornerInd[1] ]);
4520 theVertices[ 2 ] = helper.IthVertex( 0, edgeVec[ cornerInd[2] ]);
4521 theVertices[ 3 ] = helper.IthVertex( 0, edgeVec[ cornerInd[3] ]);
4527 // move corners to make sides equal by length
4528 int nbEqualV = equVerts.size();
4529 int nbExcessV = nbEqualV - ( 1 + nbC[0] + nbC[1] );
4530 if ( nbExcessV > 0 ) // there are nbExcessV vertices that can become corners
4532 // calculate normalized length of each "side" enclosed between neighbor equVerts
4533 vector< double > accuLength;
4534 double totalLen = 0;
4535 vector< TGeoIndex > evVec( equVerts.begin(), equVerts.end() );
4537 TGeoIndex iE = cornerInd[ helper.WrapIndex( iC - nbC[0] - 1, cornerInd.size() )];
4538 TGeoIndex iEEnd = cornerInd[ helper.WrapIndex( iC + nbC[1] + 1, cornerInd.size() )];
4539 while ((int) accuLength.size() < nbEqualV + int( !allCornersSame ) )
4541 // accumulate length of edges before iEV-th equal vertex
4542 accuLength.push_back( totalLen );
4544 accuLength.back() += SMESH_Algo::EdgeLength( edgeVec[ iE ]);
4545 iE = helper.WrapIndex( iE + 1, edgeVec.size());
4546 if ( iEV < evVec.size() && iE == evVec[ iEV ] ) {
4548 break; // equal vertex reached
4551 while( iE != iEEnd );
4552 totalLen = accuLength.back();
4554 accuLength.resize( equVerts.size() );
4555 for ( size_t iS = 0; iS < accuLength.size(); ++iS )
4556 accuLength[ iS ] /= totalLen;
4558 // find equVerts most close to the ideal sub-division of all sides
4560 int iCorner = helper.WrapIndex( iC - nbC[0], cornerInd.size() );
4561 int nbSides = Min( nbCorners, 2 + nbC[0] + nbC[1] );
4562 for ( int iS = 1; iS < nbSides; ++iS, ++iBestEV )
4564 double idealLen = iS / double( nbSides );
4565 double d, bestDist = 2.;
4566 for ( iEV = iBestEV; iEV < accuLength.size(); ++iEV )
4568 d = Abs( idealLen - accuLength[ iEV ]);
4570 // take into account presence of a coresponding halfDivider
4571 const double cornerWgt = 0.5 / nbSides;
4572 const double vertexWgt = 0.25 / nbSides;
4573 TGeoIndex hd = halfDivider[ evVec[ iEV ]];
4576 else if( refinedCorners.count( hd ))
4581 // choose vertex with the best d
4588 if ( iBestEV > iS-1 + nbExcessV )
4589 iBestEV = iS-1 + nbExcessV;
4590 theVertices[ iCorner ] = helper.IthVertex( 0, edgeVec[ evVec[ iBestEV ]]);
4591 cornerInd [ iCorner ] = evVec[ iBestEV ];
4592 refinedCorners.insert( evVec[ iBestEV ]);
4593 iCorner = helper.WrapIndex( iCorner + 1, cornerInd.size() );
4596 } // if ( nbExcessV > 0 )
4599 refinedCorners.insert( cornerInd[ iC ]);
4601 } // loop on cornerInd
4603 // make theWire begin from the cornerInd[0]-th EDGE
4604 while ( !theWire.front().IsSame( edgeVec[ cornerInd[0] ]))
4605 theWire.splice( theWire.begin(), theWire, --theWire.end() );
4607 } // if ( haveVariants )
4612 //================================================================================
4614 * \brief Constructor of a side of quad
4616 //================================================================================
4618 FaceQuadStruct::Side::Side(StdMeshers_FaceSidePtr theGrid)
4619 : grid(theGrid), from(0), to(theGrid ? theGrid->NbPoints() : 0 ), di(1), nbNodeOut(0)
4623 //=============================================================================
4625 * \brief Constructor of a quad
4627 //=============================================================================
4629 FaceQuadStruct::FaceQuadStruct(const TopoDS_Face& F, const std::string& theName)
4630 : face( F ), name( theName )
4635 //================================================================================
4637 * \brief Fills myForcedPnts
4639 //================================================================================
4641 bool StdMeshers_Quadrangle_2D::getEnforcedUV()
4643 myForcedPnts.clear();
4644 if ( !myParams ) return true; // missing hypothesis
4646 std::vector< TopoDS_Shape > shapes;
4647 std::vector< gp_Pnt > points;
4648 myParams->GetEnforcedNodes( shapes, points );
4650 TopTools_IndexedMapOfShape vMap;
4651 for ( size_t i = 0; i < shapes.size(); ++i )
4652 if ( !shapes[i].IsNull() )
4653 TopExp::MapShapes( shapes[i], TopAbs_VERTEX, vMap );
4655 size_t nbPoints = points.size();
4656 for ( int i = 1; i <= vMap.Extent(); ++i )
4657 points.push_back( BRep_Tool::Pnt( TopoDS::Vertex( vMap( i ))));
4659 // find out if all points must be in the FACE, which is so if
4660 // myParams is a local hypothesis on the FACE being meshed
4661 bool isStrictCheck = false;
4663 SMESH_HypoFilter paramFilter( SMESH_HypoFilter::Is( myParams ));
4664 TopoDS_Shape assignedTo;
4665 if ( myHelper->GetMesh()->GetHypothesis( myHelper->GetSubShape(),
4669 isStrictCheck = ( assignedTo.IsSame( myHelper->GetSubShape() ));
4672 multimap< double, ForcedPoint > sortedFP; // sort points by distance from EDGEs
4674 Standard_Real u1,u2,v1,v2;
4675 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4676 const double tol = BRep_Tool::Tolerance( face );
4677 Handle(ShapeAnalysis_Surface) project = myHelper->GetSurface( face );
4678 project->Bounds( u1,u2,v1,v2 );
4680 BRepBndLib::Add( face, bbox );
4681 double farTol = 0.01 * sqrt( bbox.SquareExtent() );
4683 // get internal VERTEXes of the FACE to use them instead of equal points
4684 typedef map< pair< double, double >, TopoDS_Vertex > TUV2VMap;
4686 for ( TopExp_Explorer vExp( face, TopAbs_VERTEX, TopAbs_EDGE ); vExp.More(); vExp.Next() )
4688 TopoDS_Vertex v = TopoDS::Vertex( vExp.Current() );
4689 gp_Pnt2d uv = project->ValueOfUV( BRep_Tool::Pnt( v ), tol );
4690 uv2intV.insert( make_pair( make_pair( uv.X(), uv.Y() ), v ));
4693 for ( size_t iP = 0; iP < points.size(); ++iP )
4695 gp_Pnt2d uv = project->ValueOfUV( points[ iP ], tol );
4696 if ( project->Gap() > farTol )
4698 if ( isStrictCheck && iP < nbPoints )
4700 (COMPERR_BAD_PARMETERS, TComm("An enforced point is too far from the face, dist = ")
4701 << points[ iP ].Distance( project->Value( uv )) << " - ("
4702 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4705 BRepClass_FaceClassifier clsf ( face, uv, tol );
4706 switch ( clsf.State() ) {
4709 double edgeDist = ( Min( Abs( uv.X() - u1 ), Abs( uv.X() - u2 )) +
4710 Min( Abs( uv.Y() - v1 ), Abs( uv.Y() - v2 )));
4713 fp.xyz = points[ iP ].XYZ();
4714 if ( iP >= nbPoints )
4715 fp.vertex = TopoDS::Vertex( vMap( iP - nbPoints + 1 ));
4717 TUV2VMap::iterator uv2v = uv2intV.lower_bound( make_pair( uv.X()-tol, uv.Y()-tol ));
4718 for ( ; uv2v != uv2intV.end() && uv2v->first.first <= uv.X()+tol; ++uv2v )
4719 if ( uv.SquareDistance( gp_Pnt2d( uv2v->first.first, uv2v->first.second )) < tol*tol )
4721 fp.vertex = uv2v->second;
4726 if ( myHelper->IsSubShape( fp.vertex, myHelper->GetMesh() ))
4728 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( fp.vertex );
4729 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
4730 fp.node = SMESH_Algo::VertexNode( fp.vertex, myHelper->GetMeshDS() );
4734 fp.node = myHelper->AddNode( fp.xyz.X(), fp.xyz.Y(), fp.xyz.Z(),
4735 0, fp.uv.X(), fp.uv.Y() );
4737 sortedFP.insert( make_pair( edgeDist, fp ));
4742 if ( isStrictCheck && iP < nbPoints )
4744 (COMPERR_BAD_PARMETERS, TComm("An enforced point is out of the face boundary - ")
4745 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4750 if ( isStrictCheck && iP < nbPoints )
4752 (COMPERR_BAD_PARMETERS, TComm("An enforced point is on the face boundary - ")
4753 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4758 if ( isStrictCheck && iP < nbPoints )
4760 (TComm("Classification of an enforced point ralative to the face boundary failed - ")
4761 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4766 multimap< double, ForcedPoint >::iterator d2uv = sortedFP.begin();
4767 for ( ; d2uv != sortedFP.end(); ++d2uv )
4768 myForcedPnts.push_back( (*d2uv).second );
4773 //================================================================================
4775 * \brief Splits quads by adding points of enforced nodes and create nodes on
4776 * the sides shared by quads
4778 //================================================================================
4780 bool StdMeshers_Quadrangle_2D::addEnforcedNodes()
4782 // if ( myForcedPnts.empty() )
4785 // make a map of quads sharing a side
4786 map< StdMeshers_FaceSidePtr, vector< FaceQuadStruct::Ptr > > quadsBySide;
4787 list< FaceQuadStruct::Ptr >::iterator quadIt = myQuadList.begin();
4788 for ( ; quadIt != myQuadList.end(); ++quadIt )
4789 for ( size_t iSide = 0; iSide < (*quadIt)->side.size(); ++iSide )
4791 if ( !setNormalizedGrid( *quadIt ))
4793 quadsBySide[ (*quadIt)->side[iSide] ].push_back( *quadIt );
4796 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4797 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4799 for ( size_t iFP = 0; iFP < myForcedPnts.size(); ++iFP )
4801 bool isNodeEnforced = false;
4803 // look for a quad enclosing an enforced point
4804 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4806 FaceQuadStruct::Ptr quad = *quadIt;
4807 if ( !setNormalizedGrid( *quadIt ))
4810 if ( !quad->findCell( myForcedPnts[ iFP ], i, j ))
4813 // a grid cell is found, select a node of the cell to move
4814 // to the enforced point to and to split the quad at
4815 multimap< double, pair< int, int > > ijByDist;
4816 for ( int di = 0; di < 2; ++di )
4817 for ( int dj = 0; dj < 2; ++dj )
4819 double dist2 = ( myForcedPnts[ iFP ].uv - quad->UVPt( i+di,j+dj ).UV() ).SquareModulus();
4820 ijByDist.insert( make_pair( dist2, make_pair( di,dj )));
4822 // try all nodes starting from the closest one
4823 set< FaceQuadStruct::Ptr > changedQuads;
4824 multimap< double, pair< int, int > >::iterator d2ij = ijByDist.begin();
4825 for ( ; !isNodeEnforced && d2ij != ijByDist.end(); ++d2ij )
4827 int di = d2ij->second.first;
4828 int dj = d2ij->second.second;
4830 // check if a node is at a side
4832 if ( dj== 0 && j == 0 )
4833 iSide = QUAD_BOTTOM_SIDE;
4834 else if ( dj == 1 && j+2 == quad->jSize )
4835 iSide = QUAD_TOP_SIDE;
4836 else if ( di == 0 && i == 0 )
4837 iSide = QUAD_LEFT_SIDE;
4838 else if ( di == 1 && i+2 == quad->iSize )
4839 iSide = QUAD_RIGHT_SIDE;
4841 if ( iSide > -1 ) // ----- node is at a side
4843 FaceQuadStruct::Side& side = quad->side[ iSide ];
4844 // check if this node can be moved
4845 if ( quadsBySide[ side ].size() < 2 )
4846 continue; // its a face boundary -> can't move the node
4848 int quadNodeIndex = ( iSide % 2 ) ? j : i;
4849 int sideNodeIndex = side.ToSideIndex( quadNodeIndex );
4850 if ( side.IsForced( sideNodeIndex ))
4852 // the node is already moved to another enforced point
4853 isNodeEnforced = quad->isEqual( myForcedPnts[ iFP ], i, j );
4856 // make a node of a side forced
4857 vector<UVPtStruct>& points = (vector<UVPtStruct>&) side.GetUVPtStruct();
4858 points[ sideNodeIndex ].u = myForcedPnts[ iFP ].U();
4859 points[ sideNodeIndex ].v = myForcedPnts[ iFP ].V();
4860 points[ sideNodeIndex ].node = myForcedPnts[ iFP ].node;
4862 updateSideUV( side, sideNodeIndex, quadsBySide );
4864 // update adjacent sides
4865 set< StdMeshers_FaceSidePtr > updatedSides;
4866 updatedSides.insert( side );
4867 for ( size_t i = 0; i < side.contacts.size(); ++i )
4868 if ( side.contacts[i].point == sideNodeIndex )
4870 const vector< FaceQuadStruct::Ptr >& adjQuads =
4871 quadsBySide[ *side.contacts[i].other_side ];
4872 if ( adjQuads.size() > 1 &&
4873 updatedSides.insert( * side.contacts[i].other_side ).second )
4875 updateSideUV( *side.contacts[i].other_side,
4876 side.contacts[i].other_point,
4879 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4881 const vector< FaceQuadStruct::Ptr >& adjQuads = quadsBySide[ side ];
4882 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4884 isNodeEnforced = true;
4886 else // ------------------ node is inside the quad
4890 // make a new side passing through IJ node and split the quad
4891 int indForced, iNewSide;
4892 if ( quad->iSize < quad->jSize ) // split vertically
4894 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/true );
4896 iNewSide = splitQuad( quad, i, 0 );
4900 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/false );
4902 iNewSide = splitQuad( quad, 0, j );
4904 FaceQuadStruct::Ptr newQuad = myQuadList.back();
4905 FaceQuadStruct::Side& newSide = newQuad->side[ iNewSide ];
4907 vector<UVPtStruct>& points = (vector<UVPtStruct>&) newSide.GetUVPtStruct();
4908 points[ indForced ].node = myForcedPnts[ iFP ].node;
4910 newSide.forced_nodes.insert( indForced );
4911 quad->side[( iNewSide+2 ) % 4 ].forced_nodes.insert( indForced );
4913 quadsBySide[ newSide ].push_back( quad );
4914 quadsBySide[ newQuad->side[0] ].push_back( newQuad );
4915 quadsBySide[ newQuad->side[1] ].push_back( newQuad );
4916 quadsBySide[ newQuad->side[2] ].push_back( newQuad );
4917 quadsBySide[ newQuad->side[3] ].push_back( newQuad );
4919 isNodeEnforced = true;
4921 } // end of "node is inside the quad"
4923 } // loop on nodes of the cell
4925 // remove out-of-date uv grid of changedQuads
4926 set< FaceQuadStruct::Ptr >::iterator qIt = changedQuads.begin();
4927 for ( ; qIt != changedQuads.end(); ++qIt )
4928 (*qIt)->uv_grid.clear();
4930 if ( isNodeEnforced )
4935 if ( !isNodeEnforced )
4937 if ( !myForcedPnts[ iFP ].vertex.IsNull() )
4938 return error(TComm("Unable to move any node to vertex #")
4939 <<myHelper->GetMeshDS()->ShapeToIndex( myForcedPnts[ iFP ].vertex ));
4941 return error(TComm("Unable to move any node to point ( ")
4942 << myForcedPnts[iFP].xyz.X() << ", "
4943 << myForcedPnts[iFP].xyz.Y() << ", "
4944 << myForcedPnts[iFP].xyz.Z() << " )");
4946 myNeedSmooth = true;
4948 } // loop on enforced points
4950 // Compute nodes on all sides, where not yet present
4952 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4954 FaceQuadStruct::Ptr quad = *quadIt;
4955 for ( int iSide = 0; iSide < 4; ++iSide )
4957 FaceQuadStruct::Side & side = quad->side[ iSide ];
4958 if ( side.nbNodeOut > 0 )
4959 continue; // emulated side
4960 vector< FaceQuadStruct::Ptr >& quadVec = quadsBySide[ side ];
4961 if ( quadVec.size() <= 1 )
4962 continue; // outer side
4964 const vector<UVPtStruct>& points = side.grid->GetUVPtStruct();
4965 for ( size_t iC = 0; iC < side.contacts.size(); ++iC )
4967 if ( side.contacts[iC].point < side.from ||
4968 side.contacts[iC].point >= side.to )
4970 if ( side.contacts[iC].other_point < side.contacts[iC].other_side->from ||
4971 side.contacts[iC].other_point >= side.contacts[iC].other_side->to )
4973 const vector<UVPtStruct>& oGrid = side.contacts[iC].other_side->grid->GetUVPtStruct();
4974 const UVPtStruct& uvPt = points[ side.contacts[iC].point ];
4975 if ( side.contacts[iC].other_point >= (int) oGrid .size() ||
4976 side.contacts[iC].point >= (int) points.size() )
4977 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::addEnforcedNodes(): wrong contact" );
4978 if ( oGrid[ side.contacts[iC].other_point ].node )
4979 (( UVPtStruct& ) uvPt).node = oGrid[ side.contacts[iC].other_point ].node;
4982 bool missedNodesOnSide = false;
4983 for ( size_t iP = 0; iP < points.size(); ++iP )
4984 if ( !points[ iP ].node )
4986 UVPtStruct& uvPnt = ( UVPtStruct& ) points[ iP ];
4987 gp_Pnt P = surf->Value( uvPnt.u, uvPnt.v );
4988 uvPnt.node = myHelper->AddNode(P.X(), P.Y(), P.Z(), 0, uvPnt.u, uvPnt.v );
4989 missedNodesOnSide = true;
4991 if ( missedNodesOnSide )
4993 // clear uv_grid where nodes are missing
4994 for ( size_t iQ = 0; iQ < quadVec.size(); ++iQ )
4995 quadVec[ iQ ]->uv_grid.clear();
5003 //================================================================================
5005 * \brief Splits a quad at I or J. Returns an index of a new side in the new quad
5007 //================================================================================
5009 int StdMeshers_Quadrangle_2D::splitQuad(FaceQuadStruct::Ptr quad, int I, int J)
5011 FaceQuadStruct* newQuad = new FaceQuadStruct( quad->face );
5012 myQuadList.push_back( FaceQuadStruct::Ptr( newQuad ));
5014 vector<UVPtStruct> points;
5015 if ( I > 0 && I <= quad->iSize-2 )
5017 points.reserve( quad->jSize );
5018 for ( int jP = 0; jP < quad->jSize; ++jP )
5019 points.push_back( quad->UVPt( I, jP ));
5021 newQuad->side.resize( 4 );
5022 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
5023 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
5024 newQuad->side[ QUAD_TOP_SIDE ] = quad->side[ QUAD_TOP_SIDE ];
5025 newQuad->side[ QUAD_LEFT_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
5027 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_LEFT_SIDE ];
5028 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_RIGHT_SIDE ];
5030 quad->side[ QUAD_RIGHT_SIDE ] = newSide;
5032 int iBot = quad->side[ QUAD_BOTTOM_SIDE ].ToSideIndex( I );
5033 int iTop = quad->side[ QUAD_TOP_SIDE ].ToSideIndex( I );
5035 newSide.AddContact ( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
5036 newSide2.AddContact( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
5037 newSide.AddContact ( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
5038 newSide2.AddContact( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
5039 // cout << "Contact: L " << &newSide << " "<< newSide.NbPoints()
5040 // << " R " << &newSide2 << " "<< newSide2.NbPoints()
5041 // << " B " << &quad->side[ QUAD_BOTTOM_SIDE ] << " "<< quad->side[ QUAD_BOTTOM_SIDE].NbPoints()
5042 // << " T " << &quad->side[ QUAD_TOP_SIDE ] << " "<< quad->side[ QUAD_TOP_SIDE].NbPoints()<< endl;
5044 newQuad->side[ QUAD_BOTTOM_SIDE ].from = iBot;
5045 newQuad->side[ QUAD_TOP_SIDE ].from = iTop;
5046 newQuad->name = ( TComm("Right of I=") << I );
5048 bool bRev = quad->side[ QUAD_BOTTOM_SIDE ].IsReversed();
5049 bool tRev = quad->side[ QUAD_TOP_SIDE ].IsReversed();
5050 quad->side[ QUAD_BOTTOM_SIDE ].to = iBot + ( bRev ? -1 : +1 );
5051 quad->side[ QUAD_TOP_SIDE ].to = iTop + ( tRev ? -1 : +1 );
5052 quad->uv_grid.clear();
5054 return QUAD_LEFT_SIDE;
5056 else if ( J > 0 && J <= quad->jSize-2 ) //// split horizontally, a new quad is below an old one
5058 points.reserve( quad->iSize );
5059 for ( int iP = 0; iP < quad->iSize; ++iP )
5060 points.push_back( quad->UVPt( iP, J ));
5062 newQuad->side.resize( 4 );
5063 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
5064 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
5065 newQuad->side[ QUAD_TOP_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
5066 newQuad->side[ QUAD_LEFT_SIDE ] = quad->side[ QUAD_LEFT_SIDE ];
5068 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_TOP_SIDE ];
5069 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_BOTTOM_SIDE ];
5071 quad->side[ QUAD_BOTTOM_SIDE ] = newSide;
5073 int iLft = quad->side[ QUAD_LEFT_SIDE ].ToSideIndex( J );
5074 int iRgt = quad->side[ QUAD_RIGHT_SIDE ].ToSideIndex( J );
5076 newSide.AddContact ( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5077 newSide2.AddContact( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5078 newSide.AddContact ( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5079 newSide2.AddContact( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5080 // cout << "Contact: T " << &newSide << " "<< newSide.NbPoints()
5081 // << " B " << &newSide2 << " "<< newSide2.NbPoints()
5082 // << " L " << &quad->side[ QUAD_LEFT_SIDE ] << " "<< quad->side[ QUAD_LEFT_SIDE].NbPoints()
5083 // << " R " << &quad->side[ QUAD_RIGHT_SIDE ] << " "<< quad->side[ QUAD_RIGHT_SIDE].NbPoints()<< endl;
5085 bool rRev = newQuad->side[ QUAD_RIGHT_SIDE ].IsReversed();
5086 bool lRev = newQuad->side[ QUAD_LEFT_SIDE ].IsReversed();
5087 newQuad->side[ QUAD_RIGHT_SIDE ].to = iRgt + ( rRev ? -1 : +1 );
5088 newQuad->side[ QUAD_LEFT_SIDE ].to = iLft + ( lRev ? -1 : +1 );
5089 newQuad->name = ( TComm("Below J=") << J );
5091 quad->side[ QUAD_RIGHT_SIDE ].from = iRgt;
5092 quad->side[ QUAD_LEFT_SIDE ].from = iLft;
5093 quad->uv_grid.clear();
5095 return QUAD_TOP_SIDE;
5098 myQuadList.pop_back();
5102 //================================================================================
5104 * \brief Updates UV of a side after moving its node
5106 //================================================================================
5108 void StdMeshers_Quadrangle_2D::updateSideUV( FaceQuadStruct::Side& side,
5110 const TQuadsBySide& quadsBySide,
5115 side.forced_nodes.insert( iForced );
5117 // update parts of the side before and after iForced
5119 set<int>::iterator iIt = side.forced_nodes.upper_bound( iForced );
5120 int iEnd = Min( side.NbPoints()-1, ( iIt == side.forced_nodes.end() ) ? int(1e7) : *iIt );
5121 if ( iForced + 1 < iEnd )
5122 updateSideUV( side, iForced, quadsBySide, &iEnd );
5124 iIt = side.forced_nodes.lower_bound( iForced );
5125 int iBeg = Max( 0, ( iIt == side.forced_nodes.begin() ) ? 0 : *--iIt );
5126 if ( iForced - 1 > iBeg )
5127 updateSideUV( side, iForced, quadsBySide, &iBeg );
5132 const int iFrom = Min ( iForced, *iNext );
5133 const int iTo = Max ( iForced, *iNext ) + 1;
5134 const size_t sideSize = iTo - iFrom;
5136 vector<UVPtStruct> points[4]; // side points of a temporary quad
5138 // from the quads get grid points adjacent to the side
5139 // to make two sides of a temporary quad
5140 vector< FaceQuadStruct::Ptr > quads = quadsBySide.find( side )->second; // copy!
5141 for ( int is2nd = 0; is2nd < 2; ++is2nd )
5143 points[ is2nd ].reserve( sideSize );
5145 while ( points[is2nd].size() < sideSize )
5147 int iCur = iFrom + points[is2nd].size() - int( !points[is2nd].empty() );
5149 // look for a quad adjacent to iCur-th point of the side
5150 for ( size_t iQ = 0; iQ < quads.size(); ++iQ )
5152 FaceQuadStruct::Ptr q = quads[ iQ ];
5156 for ( iS = 0; iS < q->side.size(); ++iS )
5157 if ( side.grid == q->side[ iS ].grid )
5159 if ( iS == q->side.size() )
5162 if ( !q->side[ iS ].IsReversed() )
5163 isOut = ( q->side[ iS ].from > iCur || q->side[ iS ].to-1 <= iCur );
5165 isOut = ( q->side[ iS ].to >= iCur || q->side[ iS ].from <= iCur );
5168 if ( !setNormalizedGrid( q ))
5171 // found - copy points
5173 if ( iS % 2 ) // right or left
5175 i = ( iS == QUAD_LEFT_SIDE ) ? 1 : q->iSize-2;
5176 j = q->side[ iS ].ToQuadIndex( iCur );
5178 dj = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5179 nb = ( q->side[ iS ].IsReversed() ) ? j+1 : q->jSize-j;
5181 else // bottom or top
5183 i = q->side[ iS ].ToQuadIndex( iCur );
5184 j = ( iS == QUAD_BOTTOM_SIDE ) ? 1 : q->jSize-2;
5185 di = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5187 nb = ( q->side[ iS ].IsReversed() ) ? i+1 : q->iSize-i;
5189 if ( !points[is2nd].empty() )
5191 gp_UV lastUV = points[is2nd].back().UV();
5192 gp_UV quadUV = q->UVPt( i, j ).UV();
5193 if ( ( lastUV - quadUV ).SquareModulus() > 1e-10 )
5194 continue; // quad is on the other side of the side
5195 i += di; j += dj; --nb;
5197 for ( ; nb > 0 ; --nb )
5199 points[ is2nd ].push_back( q->UVPt( i, j ));
5200 if ( points[is2nd].size() >= sideSize )
5204 quads[ iQ ].reset(); // not to use this quad anymore
5206 if ( points[is2nd].size() >= sideSize )
5210 if ( nbLoops++ > quads.size() )
5211 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::updateSideUV() bug: infinite loop" );
5213 } // while ( points[is2nd].size() < sideSize )
5214 } // two loops to fill points[0] and points[1]
5216 // points for other pair of opposite sides of the temporary quad
5218 enum { L,R,B,T }; // side index of points[]
5220 points[B].push_back( points[L].front() );
5221 points[B].push_back( side.GetUVPtStruct()[ iFrom ]);
5222 points[B].push_back( points[R].front() );
5224 points[T].push_back( points[L].back() );
5225 points[T].push_back( side.GetUVPtStruct()[ iTo-1 ]);
5226 points[T].push_back( points[R].back() );
5228 // make the temporary quad
5229 FaceQuadStruct::Ptr tmpQuad
5230 ( new FaceQuadStruct( TopoDS::Face( myHelper->GetSubShape() ), "tmpQuad"));
5231 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[B] )); // bottom
5232 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[R] )); // right
5233 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[T] ));
5234 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[L] ));
5236 // compute new UV of the side
5237 setNormalizedGrid( tmpQuad );
5238 gp_UV uv = tmpQuad->UVPt(1,0).UV();
5239 tmpQuad->updateUV( uv, 1,0, /*isVertical=*/true );
5241 // update UV of the side
5242 vector<UVPtStruct>& sidePoints = (vector<UVPtStruct>&) side.GetUVPtStruct();
5243 for ( int i = iFrom; i < iTo; ++i )
5245 const uvPtStruct& uvPt = tmpQuad->UVPt( 1, i-iFrom );
5246 sidePoints[ i ].u = uvPt.u;
5247 sidePoints[ i ].v = uvPt.v;
5251 //================================================================================
5253 * \brief Finds indices of a grid quad enclosing the given enforced UV
5255 //================================================================================
5257 bool FaceQuadStruct::findCell( const gp_XY& UV, int & I, int & J )
5259 // setNormalizedGrid() must be called before!
5260 if ( uv_box.IsOut( UV ))
5263 // find an approximate position
5264 double x = 0.5, y = 0.5;
5265 gp_XY t0 = UVPt( iSize - 1, 0 ).UV();
5266 gp_XY t1 = UVPt( 0, jSize - 1 ).UV();
5267 gp_XY t2 = UVPt( 0, 0 ).UV();
5268 SMESH_MeshAlgos::GetBarycentricCoords( UV, t0, t1, t2, x, y );
5269 x = Min( 1., Max( 0., x ));
5270 y = Min( 1., Max( 0., y ));
5272 // precise the position
5273 normPa2IJ( x,y, I,J );
5274 if ( !isNear( UV, I,J ))
5276 // look for the most close IJ by traversing uv_grid in the middle
5277 double dist2, minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5278 for ( int isU = 0; isU < 2; ++isU )
5280 int ind1 = isU ? 0 : iSize / 2;
5281 int ind2 = isU ? jSize / 2 : 0;
5282 int di1 = isU ? Max( 2, iSize / 20 ) : 0;
5283 int di2 = isU ? 0 : Max( 2, jSize / 20 );
5284 int i,nb = isU ? iSize / di1 : jSize / di2;
5285 for ( i = 0; i < nb; ++i, ind1 += di1, ind2 += di2 )
5286 if (( dist2 = ( UV - UVPt( ind1,ind2 ).UV() ).SquareModulus() ) < minDist2 )
5290 if ( isNear( UV, I,J ))
5292 minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5295 if ( !isNear( UV, I,J, Max( iSize, jSize ) /2 ))
5301 //================================================================================
5303 * \brief Find indices (i,j) of a point in uv_grid by normalized parameters (x,y)
5305 //================================================================================
5307 void FaceQuadStruct::normPa2IJ(double X, double Y, int & I, int & J )
5310 I = Min( int ( iSize * X ), iSize - 2 );
5311 J = Min( int ( jSize * Y ), jSize - 2 );
5317 while ( X <= UVPt( I,J ).x && I != 0 )
5319 while ( X > UVPt( I+1,J ).x && I+2 < iSize )
5321 while ( Y <= UVPt( I,J ).y && J != 0 )
5323 while ( Y > UVPt( I,J+1 ).y && J+2 < jSize )
5325 } while ( oldI != I || oldJ != J );
5328 //================================================================================
5330 * \brief Looks for UV in quads around a given (I,J) and precise (I,J)
5332 //================================================================================
5334 bool FaceQuadStruct::isNear( const gp_XY& UV, int & I, int & J, int nbLoops )
5336 if ( I+1 >= iSize ) I = iSize - 2;
5337 if ( J+1 >= jSize ) J = jSize - 2;
5340 gp_XY uvI, uvJ, uv0, uv1;
5341 for ( int iLoop = 0; iLoop < nbLoops; ++iLoop )
5343 int oldI = I, oldJ = J;
5345 uvI = UVPt( I+1, J ).UV();
5346 uvJ = UVPt( I, J+1 ).UV();
5347 uv0 = UVPt( I, J ).UV();
5348 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5349 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5352 if ( I > 0 && bcI < 0. ) --I;
5353 if ( I+2 < iSize && bcI > 1. ) ++I;
5354 if ( J > 0 && bcJ < 0. ) --J;
5355 if ( J+2 < jSize && bcJ > 1. ) ++J;
5357 uv1 = UVPt( I+1,J+1).UV();
5358 if ( I != oldI || J != oldJ )
5360 uvI = UVPt( I+1, J ).UV();
5361 uvJ = UVPt( I, J+1 ).UV();
5363 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5364 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5367 if ( I > 0 && bcI > 1. ) --I;
5368 if ( I+2 < iSize && bcI < 0. ) ++I;
5369 if ( J > 0 && bcJ > 1. ) --J;
5370 if ( J+2 < jSize && bcJ < 0. ) ++J;
5372 if ( I == oldI && J == oldJ )
5375 if ( iLoop+1 == nbLoops )
5377 uvI = UVPt( I+1, J ).UV();
5378 uvJ = UVPt( I, J+1 ).UV();
5379 uv0 = UVPt( I, J ).UV();
5380 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5381 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5384 uv1 = UVPt( I+1,J+1).UV();
5385 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5386 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5393 //================================================================================
5395 * \brief Checks if a given UV is equal to a given grid point
5397 //================================================================================
5399 bool FaceQuadStruct::isEqual( const gp_XY& UV, int I, int J )
5401 TopLoc_Location loc;
5402 Handle(Geom_Surface) surf = BRep_Tool::Surface( face, loc );
5403 gp_Pnt p1 = surf->Value( UV.X(), UV.Y() );
5404 gp_Pnt p2 = surf->Value( UVPt( I,J ).u, UVPt( I,J ).v );
5406 double dist2 = 1e100;
5407 for ( int di = -1; di < 2; di += 2 )
5410 if ( i < 0 || i+1 >= iSize ) continue;
5411 for ( int dj = -1; dj < 2; dj += 2 )
5414 if ( j < 0 || j+1 >= jSize ) continue;
5417 p2.SquareDistance( surf->Value( UVPt( i,j ).u, UVPt( i,j ).v )));
5420 double tol2 = dist2 / 1000.;
5421 return p1.SquareDistance( p2 ) < tol2;
5424 //================================================================================
5426 * \brief Recompute UV of grid points around a moved point in one direction
5428 //================================================================================
5430 void FaceQuadStruct::updateUV( const gp_XY& UV, int I, int J, bool isVertical )
5432 UVPt( I, J ).u = UV.X();
5433 UVPt( I, J ).v = UV.Y();
5438 if ( J+1 < jSize-1 )
5440 gp_UV a0 = UVPt( 0, J ).UV();
5441 gp_UV a1 = UVPt( iSize-1, J ).UV();
5442 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5443 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5445 gp_UV p0 = UVPt( I, J ).UV();
5446 gp_UV p2 = UVPt( I, jSize-1 ).UV();
5447 const double y0 = UVPt( I, J ).y, dy = 1. - y0;
5448 for (int j = J+1; j < jSize-1; j++)
5450 gp_UV p1 = UVPt( iSize-1, j ).UV();
5451 gp_UV p3 = UVPt( 0, j ).UV();
5453 UVPtStruct& uvPt = UVPt( I, j );
5454 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5462 gp_UV a0 = UVPt( 0, 0 ).UV();
5463 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5464 gp_UV a2 = UVPt( iSize-1, J ).UV();
5465 gp_UV a3 = UVPt( 0, J ).UV();
5467 gp_UV p0 = UVPt( I, 0 ).UV();
5468 gp_UV p2 = UVPt( I, J ).UV();
5469 const double y0 = 0., dy = UVPt( I, J ).y - y0;
5470 for (int j = 1; j < J; j++)
5472 gp_UV p1 = UVPt( iSize-1, j ).UV();
5473 gp_UV p3 = UVPt( 0, j ).UV();
5475 UVPtStruct& uvPt = UVPt( I, j );
5476 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5482 else // horizontally
5487 gp_UV a0 = UVPt( 0, 0 ).UV();
5488 gp_UV a1 = UVPt( I, 0 ).UV();
5489 gp_UV a2 = UVPt( I, jSize-1 ).UV();
5490 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5492 gp_UV p1 = UVPt( I, J ).UV();
5493 gp_UV p3 = UVPt( 0, J ).UV();
5494 const double x0 = 0., dx = UVPt( I, J ).x - x0;
5495 for (int i = 1; i < I; i++)
5497 gp_UV p0 = UVPt( i, 0 ).UV();
5498 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5500 UVPtStruct& uvPt = UVPt( i, J );
5501 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5507 if ( I+1 < iSize-1 )
5509 gp_UV a0 = UVPt( I, 0 ).UV();
5510 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5511 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5512 gp_UV a3 = UVPt( I, jSize-1 ).UV();
5514 gp_UV p1 = UVPt( iSize-1, J ).UV();
5515 gp_UV p3 = UVPt( I, J ).UV();
5516 const double x0 = UVPt( I, J ).x, dx = 1. - x0;
5517 for (int i = I+1; i < iSize-1; i++)
5519 gp_UV p0 = UVPt( i, 0 ).UV();
5520 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5522 UVPtStruct& uvPt = UVPt( i, J );
5523 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5531 //================================================================================
5533 * \brief Side copying
5535 //================================================================================
5537 FaceQuadStruct::Side& FaceQuadStruct::Side::operator=(const Side& otherSide)
5539 grid = otherSide.grid;
5540 from = otherSide.from;
5543 forced_nodes = otherSide.forced_nodes;
5544 contacts = otherSide.contacts;
5545 nbNodeOut = otherSide.nbNodeOut;
5547 for ( size_t iC = 0; iC < contacts.size(); ++iC )
5549 FaceQuadStruct::Side* oSide = contacts[iC].other_side;
5550 for ( size_t iOC = 0; iOC < oSide->contacts.size(); ++iOC )
5551 if ( oSide->contacts[iOC].other_side == & otherSide )
5553 // cout << "SHIFT old " << &otherSide << " " << otherSide.NbPoints()
5554 // << " -> new " << this << " " << this->NbPoints() << endl;
5555 oSide->contacts[iOC].other_side = this;
5561 //================================================================================
5563 * \brief Converts node index of a quad to node index of this side
5565 //================================================================================
5567 int FaceQuadStruct::Side::ToSideIndex( int quadNodeIndex ) const
5569 return from + di * quadNodeIndex;
5572 //================================================================================
5574 * \brief Converts node index of this side to node index of a quad
5576 //================================================================================
5578 int FaceQuadStruct::Side::ToQuadIndex( int sideNodeIndex ) const
5580 return ( sideNodeIndex - from ) * di;
5583 //================================================================================
5585 * \brief Reverse the side
5587 //================================================================================
5589 bool FaceQuadStruct::Side::Reverse(bool keepGrid)
5597 std::swap( from, to );
5608 //================================================================================
5610 * \brief Checks if a node is enforced
5611 * \param [in] nodeIndex - an index of a node in a size
5612 * \return bool - \c true if the node is forced
5614 //================================================================================
5616 bool FaceQuadStruct::Side::IsForced( int nodeIndex ) const
5618 if ( nodeIndex < 0 || nodeIndex >= grid->NbPoints() )
5619 throw SALOME_Exception( " FaceQuadStruct::Side::IsForced(): wrong index" );
5621 if ( forced_nodes.count( nodeIndex ) )
5624 for ( size_t i = 0; i < this->contacts.size(); ++i )
5625 if ( contacts[ i ].point == nodeIndex &&
5626 contacts[ i ].other_side->forced_nodes.count( contacts[ i ].other_point ))
5632 //================================================================================
5634 * \brief Sets up a contact between this and another side
5636 //================================================================================
5638 void FaceQuadStruct::Side::AddContact( int ip, Side* side, int iop )
5640 if ( ip >= (int) GetUVPtStruct().size() ||
5641 iop >= (int) side->GetUVPtStruct().size() )
5642 throw SALOME_Exception( "FaceQuadStruct::Side::AddContact(): wrong point" );
5643 if ( ip < from || ip >= to )
5646 contacts.resize( contacts.size() + 1 );
5647 Contact& c = contacts.back();
5649 c.other_side = side;
5650 c.other_point = iop;
5653 side->contacts.resize( side->contacts.size() + 1 );
5654 Contact& c = side->contacts.back();
5656 c.other_side = this;
5661 //================================================================================
5663 * \brief Returns a normalized parameter of a point indexed within a quadrangle
5665 //================================================================================
5667 double FaceQuadStruct::Side::Param( int i ) const
5669 const vector<UVPtStruct>& points = GetUVPtStruct();
5670 return (( points[ from + i * di ].normParam - points[ from ].normParam ) /
5671 ( points[ to - 1 * di ].normParam - points[ from ].normParam ));
5674 //================================================================================
5676 * \brief Returns UV by a parameter normalized within a quadrangle
5678 //================================================================================
5680 gp_XY FaceQuadStruct::Side::Value2d( double x ) const
5682 const vector<UVPtStruct>& points = GetUVPtStruct();
5683 double u = ( points[ from ].normParam +
5684 x * ( points[ to-di ].normParam - points[ from ].normParam ));
5685 return grid->Value2d( u ).XY();
5688 //================================================================================
5690 * \brief Returns side length
5692 //================================================================================
5694 double FaceQuadStruct::Side::Length(int theFrom, int theTo) const
5696 if ( IsReversed() != ( theTo < theFrom ))
5697 std::swap( theTo, theFrom );
5699 const vector<UVPtStruct>& points = GetUVPtStruct();
5701 if ( theFrom == theTo && theTo == -1 )
5702 r = Abs( First().normParam -
5703 Last ().normParam );
5704 else if ( IsReversed() )
5705 r = Abs( points[ Max( to, theTo+1 ) ].normParam -
5706 points[ Min( from, theFrom ) ].normParam );
5708 r = Abs( points[ Min( to, theTo-1 ) ].normParam -
5709 points[ Max( from, theFrom ) ].normParam );
5710 return r * grid->Length();