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 #include <boost/container/flat_set.hpp>
69 typedef NCollection_Array2<const SMDS_MeshNode*> StdMeshers_Array2OfNode;
72 typedef SMESH_Comment TComm;
76 //=============================================================================
80 //=============================================================================
82 StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D (int hypId,
84 : SMESH_2D_Algo(hypId, gen),
85 myQuadranglePreference(false),
86 myTrianglePreference(false),
91 myQuadType(QUAD_STANDARD),
94 _name = "Quadrangle_2D";
95 _shapeType = (1 << TopAbs_FACE);
96 _compatibleHypothesis.push_back("QuadrangleParams");
97 _compatibleHypothesis.push_back("QuadranglePreference");
98 _compatibleHypothesis.push_back("TrianglePreference");
99 _compatibleHypothesis.push_back("ViscousLayers2D");
102 //=============================================================================
106 //=============================================================================
108 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
112 //=============================================================================
116 //=============================================================================
118 bool StdMeshers_Quadrangle_2D::CheckHypothesis
120 const TopoDS_Shape& aShape,
121 SMESH_Hypothesis::Hypothesis_Status& aStatus)
124 myQuadType = QUAD_STANDARD;
125 myQuadranglePreference = false;
126 myTrianglePreference = false;
127 myHelper = (SMESH_MesherHelper*)NULL;
131 aStatus = SMESH_Hypothesis::HYP_OK;
133 const list <const SMESHDS_Hypothesis * >& hyps =
134 GetUsedHypothesis(aMesh, aShape, false);
135 const SMESHDS_Hypothesis * aHyp = 0;
137 bool isFirstParams = true;
139 // First assigned hypothesis (if any) is processed now
140 if (hyps.size() > 0) {
142 if (strcmp("QuadrangleParams", aHyp->GetName()) == 0)
144 myParams = (const StdMeshers_QuadrangleParams*)aHyp;
145 myTriaVertexID = myParams->GetTriaVertex();
146 myQuadType = myParams->GetQuadType();
147 if (myQuadType == QUAD_QUADRANGLE_PREF ||
148 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
149 myQuadranglePreference = true;
150 else if (myQuadType == QUAD_TRIANGLE_PREF)
151 myTrianglePreference = true;
153 else if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
154 isFirstParams = false;
155 myQuadranglePreference = true;
157 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
158 isFirstParams = false;
159 myTrianglePreference = true;
162 isFirstParams = false;
166 // Second(last) assigned hypothesis (if any) is processed now
167 if (hyps.size() > 1) {
170 if (strcmp("QuadranglePreference", aHyp->GetName()) == 0) {
171 myQuadranglePreference = true;
172 myTrianglePreference = false;
173 myQuadType = QUAD_STANDARD;
175 else if (strcmp("TrianglePreference", aHyp->GetName()) == 0){
176 myQuadranglePreference = false;
177 myTrianglePreference = true;
178 myQuadType = QUAD_STANDARD;
181 else if (const StdMeshers_QuadrangleParams* aHyp2 =
182 dynamic_cast<const StdMeshers_QuadrangleParams*>( aHyp ))
184 myTriaVertexID = aHyp2->GetTriaVertex();
186 if (!myQuadranglePreference && !myTrianglePreference) { // priority of hypos
187 myQuadType = aHyp2->GetQuadType();
188 if (myQuadType == QUAD_QUADRANGLE_PREF ||
189 myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
190 myQuadranglePreference = true;
191 else if (myQuadType == QUAD_TRIANGLE_PREF)
192 myTrianglePreference = true;
197 error( StdMeshers_ViscousLayers2D::CheckHypothesis( aMesh, aShape, aStatus ));
199 return aStatus == HYP_OK;
202 //=============================================================================
206 //=============================================================================
208 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
209 const TopoDS_Shape& aShape)
211 const TopoDS_Face& F = TopoDS::Face(aShape);
212 aMesh.GetSubMesh( F );
214 // do not initialize my fields before this as StdMeshers_ViscousLayers2D
215 // can call Compute() recursively
216 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
220 myProxyMesh = proxyMesh;
222 SMESH_MesherHelper helper (aMesh);
225 _quadraticMesh = myHelper->IsQuadraticSubMesh(aShape);
226 myHelper->SetElementsOnShape( true );
227 myNeedSmooth = false;
230 FaceQuadStruct::Ptr quad = CheckNbEdges( aMesh, F, /*considerMesh=*/true, myHelper );
234 myQuadList.push_back( quad );
236 if ( !getEnforcedUV() )
239 updateDegenUV( quad );
241 int n1 = quad->side[0].NbPoints();
242 int n2 = quad->side[1].NbPoints();
243 int n3 = quad->side[2].NbPoints();
244 int n4 = quad->side[3].NbPoints();
246 enum { NOT_COMPUTED = -1, COMPUTE_FAILED = 0, COMPUTE_OK = 1 };
247 int res = NOT_COMPUTED;
248 if ( myQuadranglePreference )
250 int nfull = n1+n2+n3+n4;
251 if ((nfull % 2) == 0 && ((n1 != n3) || (n2 != n4)))
253 // special path genarating only quandrangle faces
254 res = computeQuadPref( aMesh, F, quad );
257 else if ( myQuadType == QUAD_REDUCED )
261 int n13tmp = n13/2; n13tmp = n13tmp*2;
262 int n24tmp = n24/2; n24tmp = n24tmp*2;
263 if ((n1 == n3 && n2 != n4 && n24tmp == n24) ||
264 (n2 == n4 && n1 != n3 && n13tmp == n13))
266 res = computeReduced( aMesh, F, quad );
270 if ( n1 != n3 && n2 != n4 )
271 error( COMPERR_WARNING,
272 "To use 'Reduced' transition, "
273 "two opposite sides should have same number of segments, "
274 "but actual number of segments is different on all sides. "
275 "'Standard' transion has been used.");
276 else if ( ! ( n1 == n3 && n2 == n4 ))
277 error( COMPERR_WARNING,
278 "To use 'Reduced' transition, "
279 "two opposite sides should have an even difference in number of segments. "
280 "'Standard' transion has been used.");
284 if ( res == NOT_COMPUTED )
286 if ( n1 != n3 || n2 != n4 )
287 res = computeTriangles( aMesh, F, quad );
289 res = computeQuadDominant( aMesh, F );
292 if ( res == COMPUTE_OK && myNeedSmooth )
295 if ( res == COMPUTE_OK )
298 return ( res == COMPUTE_OK );
301 //================================================================================
303 * \brief Compute quadrangles and triangles on the quad
305 //================================================================================
307 bool StdMeshers_Quadrangle_2D::computeTriangles(SMESH_Mesh& aMesh,
308 const TopoDS_Face& aFace,
309 FaceQuadStruct::Ptr quad)
311 int nb = quad->side[0].grid->NbPoints();
312 int nr = quad->side[1].grid->NbPoints();
313 int nt = quad->side[2].grid->NbPoints();
314 int nl = quad->side[3].grid->NbPoints();
316 // rotate the quad to have nbNodeOut sides on TOP [and LEFT]
318 quad->shift( nl > nr ? 3 : 2, true );
320 quad->shift( 1, true );
322 quad->shift( nt > nb ? 0 : 3, true );
324 if ( !setNormalizedGrid( quad ))
327 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
329 splitQuad( quad, 0, quad->jSize-2 );
331 if ( quad->nbNodeOut( QUAD_BOTTOM_SIDE )) // this should not happen
333 splitQuad( quad, 0, 1 );
335 FaceQuadStruct::Ptr newQuad = myQuadList.back();
336 if ( quad != newQuad ) // split done
338 { // update left side limit till where to make triangles
339 FaceQuadStruct::Ptr botQuad = // a bottom part
340 ( quad->side[ QUAD_LEFT_SIDE ].from == 0 ) ? quad : newQuad;
341 if ( botQuad->nbNodeOut( QUAD_LEFT_SIDE ) > 0 )
342 botQuad->side[ QUAD_LEFT_SIDE ].to += botQuad->nbNodeOut( QUAD_LEFT_SIDE );
343 else if ( botQuad->nbNodeOut( QUAD_RIGHT_SIDE ) > 0 )
344 botQuad->side[ QUAD_RIGHT_SIDE ].to += botQuad->nbNodeOut( QUAD_RIGHT_SIDE );
346 // make quad be a greatest one
347 if ( quad->side[ QUAD_LEFT_SIDE ].NbPoints() == 2 ||
348 quad->side[ QUAD_RIGHT_SIDE ].NbPoints() == 2 )
350 if ( !setNormalizedGrid( quad ))
354 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
356 splitQuad( quad, quad->iSize-2, 0 );
358 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
360 splitQuad( quad, 1, 0 );
362 if ( quad->nbNodeOut( QUAD_TOP_SIDE ))
364 newQuad = myQuadList.back();
365 if ( newQuad == quad ) // too narrow to split
367 // update left side limit till where to make triangles
368 quad->side[ QUAD_LEFT_SIDE ].to--;
372 FaceQuadStruct::Ptr leftQuad =
373 ( quad->side[ QUAD_BOTTOM_SIDE ].from == 0 ) ? quad : newQuad;
374 leftQuad->nbNodeOut( QUAD_TOP_SIDE ) = 0;
379 if ( ! computeQuadDominant( aMesh, aFace ))
382 // try to fix zero-area triangles near straight-angle corners
387 //================================================================================
389 * \brief Compute quadrangles and possibly triangles on all quads of myQuadList
391 //================================================================================
393 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
394 const TopoDS_Face& aFace)
396 if ( !addEnforcedNodes() )
399 std::list< FaceQuadStruct::Ptr >::iterator quad = myQuadList.begin();
400 for ( ; quad != myQuadList.end(); ++quad )
401 if ( !computeQuadDominant( aMesh, aFace, *quad ))
407 //================================================================================
409 * \brief Compute quadrangles and possibly triangles
411 //================================================================================
413 bool StdMeshers_Quadrangle_2D::computeQuadDominant(SMESH_Mesh& aMesh,
414 const TopoDS_Face& aFace,
415 FaceQuadStruct::Ptr quad)
417 // --- set normalized grid on unit square in parametric domain
419 if ( !setNormalizedGrid( quad ))
422 // --- create nodes on points, and create quadrangles
424 int nbhoriz = quad->iSize;
425 int nbvertic = quad->jSize;
427 // internal mesh nodes
428 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
429 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
430 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
431 for (i = 1; i < nbhoriz - 1; i++)
432 for (j = 1; j < nbvertic - 1; j++)
434 UVPtStruct& uvPnt = quad->UVPt( i, j );
435 gp_Pnt P = S->Value( uvPnt.u, uvPnt.v );
436 uvPnt.node = meshDS->AddNode(P.X(), P.Y(), P.Z());
437 meshDS->SetNodeOnFace( uvPnt.node, geomFaceID, uvPnt.u, uvPnt.v );
443 // --.--.--.--.--.-- nbvertic
449 // ---.----.----.--- 0
450 // 0 > > > > > > > > nbhoriz
455 int iup = nbhoriz - 1;
456 if (quad->nbNodeOut(3)) { ilow++; } else { if (quad->nbNodeOut(1)) iup--; }
459 int jup = nbvertic - 1;
460 if (quad->nbNodeOut(0)) { jlow++; } else { if (quad->nbNodeOut(2)) jup--; }
462 // regular quadrangles
463 for (i = ilow; i < iup; i++) {
464 for (j = jlow; j < jup; j++) {
465 const SMDS_MeshNode *a, *b, *c, *d;
466 a = quad->uv_grid[ j * nbhoriz + i ].node;
467 b = quad->uv_grid[ j * nbhoriz + i + 1].node;
468 c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
469 d = quad->uv_grid[(j + 1) * nbhoriz + i ].node;
470 myHelper->AddFace(a, b, c, d);
474 // Boundary elements (must always be on an outer boundary of the FACE)
476 const vector<UVPtStruct>& uv_e0 = quad->side[0].grid->GetUVPtStruct();
477 const vector<UVPtStruct>& uv_e1 = quad->side[1].grid->GetUVPtStruct();
478 const vector<UVPtStruct>& uv_e2 = quad->side[2].grid->GetUVPtStruct();
479 const vector<UVPtStruct>& uv_e3 = quad->side[3].grid->GetUVPtStruct();
481 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
482 return error(COMPERR_BAD_INPUT_MESH);
484 double eps = Precision::Confusion();
486 int nbdown = (int) uv_e0.size();
487 int nbup = (int) uv_e2.size();
488 int nbright = (int) uv_e1.size();
489 int nbleft = (int) uv_e3.size();
491 if (quad->nbNodeOut(0) && nbvertic == 2) // this should not occur
495 // |___|___|___|___|___|___|
497 // |___|___|___|___|___|___|
499 // |___|___|___|___|___|___| __ first row of the regular grid
500 // . . . . . . . . . __ down edge nodes
502 // >->->->->->->->->->->->-> -- direction of processing
504 int g = 0; // number of last processed node in the regular grid
506 // number of last node of the down edge to be processed
507 int stop = nbdown - 1;
508 // if right edge is out, we will stop at a node, previous to the last one
509 //if (quad->nbNodeOut(1)) stop--;
510 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
511 quad->UVPt( nbhoriz-1, 1 ).node = uv_e1[1].node;
512 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
513 quad->UVPt( 0, 1 ).node = uv_e3[1].node;
515 // for each node of the down edge find nearest node
516 // in the first row of the regular grid and link them
517 for (i = 0; i < stop; i++) {
518 const SMDS_MeshNode *a, *b, *c=0, *d;
520 b = uv_e0[i + 1].node;
521 gp_Pnt pb (b->X(), b->Y(), b->Z());
523 // find node c in the regular grid, which will be linked with node b
526 // right bound reached, link with the rightmost node
528 c = quad->uv_grid[nbhoriz + iup].node;
531 // find in the grid node c, nearest to the b
533 double mind = RealLast();
534 for (int k = g; k <= iup; k++) {
536 const SMDS_MeshNode *nk;
537 if (k < ilow) // this can be, if left edge is out
538 nk = uv_e3[1].node; // get node from the left edge
540 nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
542 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
543 double dist = pb.Distance(pnk);
544 if (dist < mind - eps) {
554 if (near == g) { // make triangle
555 myHelper->AddFace(a, b, c);
557 else { // make quadrangle
561 d = quad->uv_grid[nbhoriz + near - 1].node;
562 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
564 if (!myTrianglePreference){
565 myHelper->AddFace(a, b, c, d);
568 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
571 // if node d is not at position g - make additional triangles
573 for (int k = near - 1; k > g; k--) {
574 c = quad->uv_grid[nbhoriz + k].node;
578 d = quad->uv_grid[nbhoriz + k - 1].node;
579 myHelper->AddFace(a, c, d);
586 if (quad->nbNodeOut(2) && nbvertic == 2)
590 // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
592 // . . . . . . . . . __ up edge nodes
593 // ___ ___ ___ ___ ___ ___ __ first row of the regular grid
595 // |___|___|___|___|___|___|
597 // |___|___|___|___|___|___|
600 int g = nbhoriz - 1; // last processed node in the regular grid
606 if ( quad->side[3].grid->Edge(0).IsNull() ) // left side is simulated one
608 if ( nbright == 2 ) // quad divided at I but not at J (2D_mesh_QuadranglePreference_01/B1)
609 stop++; // we stop at a second node
613 if ( quad->nbNodeOut( QUAD_RIGHT_SIDE ))
614 quad->UVPt( nbhoriz-1, 0 ).node = uv_e1[ nbright-2 ].node;
615 if ( quad->nbNodeOut( QUAD_LEFT_SIDE ))
616 quad->UVPt( 0, 0 ).node = uv_e3[ nbleft-2 ].node;
618 if ( nbright > 2 ) // there was a split at J
619 quad->nbNodeOut( QUAD_LEFT_SIDE ) = 0;
621 const SMDS_MeshNode *a, *b, *c, *d;
623 // avoid creating zero-area triangles near a straight-angle corner
627 c = uv_e1[nbright-2].node;
628 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
629 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
630 if ( Abs( area ) < 1e-20 )
633 d = quad->UVPt( g, nbvertic-2 ).node;
634 if ( myTrianglePreference )
636 myHelper->AddFace(a, d, c);
640 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
642 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
643 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
645 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
646 "Bad quality quad created"));
647 err->myBadElements.push_back( face );
654 // for each node of the up edge find nearest node
655 // in the first row of the regular grid and link them
656 for ( ; i > stop; i--)
659 b = uv_e2[i - 1].node;
660 gp_Pnt pb = SMESH_TNodeXYZ( b );
662 // find node c in the grid, which will be linked with node b
664 if (i == stop + 1) { // left bound reached, link with the leftmost node
665 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
668 // find node c in the grid, nearest to the b
669 double mind = RealLast();
670 for (int k = g; k >= ilow; k--) {
671 const SMDS_MeshNode *nk;
673 nk = uv_e1[nbright - 2].node;
675 nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
676 gp_Pnt pnk = SMESH_TNodeXYZ( nk );
677 double dist = pb.Distance(pnk);
678 if (dist < mind - eps) {
688 if (near == g) { // make triangle
689 myHelper->AddFace(a, b, c);
691 else { // make quadrangle
693 d = uv_e1[nbright - 2].node;
695 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
696 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
697 if (!myTrianglePreference){
698 myHelper->AddFace(a, b, c, d);
701 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
704 if (near + 1 < g) { // if d is not at g - make additional triangles
705 for (int k = near + 1; k < g; k++) {
706 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
708 d = uv_e1[nbright - 2].node;
710 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
711 myHelper->AddFace(a, c, d);
720 // right or left boundary quadrangles
721 if (quad->nbNodeOut( QUAD_RIGHT_SIDE ) && nbhoriz == 2) // this should not occur
723 int g = 0; // last processed node in the grid
724 int stop = nbright - 1;
726 if (quad->side[ QUAD_RIGHT_SIDE ].from != i ) i++;
727 if (quad->side[ QUAD_RIGHT_SIDE ].to != stop ) stop--;
728 for ( ; i < stop; i++) {
729 const SMDS_MeshNode *a, *b, *c, *d;
731 b = uv_e1[i + 1].node;
732 gp_Pnt pb (b->X(), b->Y(), b->Z());
734 // find node c in the grid, nearest to the b
737 if (i == stop - 1) { // up boundary reached
738 c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
741 double mind = RealLast();
742 for (int k = g; k <= jup; k++) {
743 const SMDS_MeshNode *nk;
745 nk = uv_e0[nbdown - 2].node;
747 nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
748 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
749 double dist = pb.Distance(pnk);
750 if (dist < mind - eps) {
760 if (near == g) { // make triangle
761 myHelper->AddFace(a, b, c);
763 else { // make quadrangle
765 d = uv_e0[nbdown - 2].node;
767 d = quad->uv_grid[nbhoriz*near - 2].node;
768 //SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
770 if (!myTrianglePreference){
771 myHelper->AddFace(a, b, c, d);
774 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
777 if (near - 1 > g) { // if d not is at g - make additional triangles
778 for (int k = near - 1; k > g; k--) {
779 c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
781 d = uv_e0[nbdown - 2].node;
783 d = quad->uv_grid[nbhoriz*k - 2].node;
784 myHelper->AddFace(a, c, d);
791 if (quad->nbNodeOut(3) && nbhoriz == 2)
793 int g = nbvertic - 1; // last processed node in the grid
795 i = quad->side[ QUAD_LEFT_SIDE ].to-1; // nbleft - 1;
797 const SMDS_MeshNode *a, *b, *c, *d;
798 // avoid creating zero-area triangles near a straight-angle corner
802 c = quad->UVPt( 1, g ).node;
803 SMESH_TNodeXYZ pa( a ), pb( b ), pc( c );
804 double area = 0.5 * (( pb - pa ) ^ ( pc - pa )).Modulus();
805 if ( Abs( area ) < 1e-20 )
808 d = quad->UVPt( 1, g ).node;
809 if ( myTrianglePreference )
811 myHelper->AddFace(a, d, c);
815 if ( SMDS_MeshFace* face = myHelper->AddFace(a, b, d, c))
817 SMESH_ComputeErrorPtr& err = aMesh.GetSubMesh( aFace )->GetComputeError();
818 if ( !err || err->IsOK() || err->myName < COMPERR_WARNING )
820 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
821 "Bad quality quad created"));
822 err->myBadElements.push_back( face );
829 for (; i > stop; i--) // loop on nodes on the left side
832 b = uv_e3[i - 1].node;
833 gp_Pnt pb (b->X(), b->Y(), b->Z());
835 // find node c in the grid, nearest to the b
837 if (i == stop + 1) { // down boundary reached
838 c = quad->uv_grid[nbhoriz*jlow + 1].node;
842 double mind = RealLast();
843 for (int k = g; k >= jlow; k--) {
844 const SMDS_MeshNode *nk;
846 nk = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
848 nk = quad->uv_grid[nbhoriz*k + 1].node;
849 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
850 double dist = pb.Distance(pnk);
851 if (dist < mind - eps) {
861 if (near == g) { // make triangle
862 myHelper->AddFace(a, b, c);
864 else { // make quadrangle
866 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
868 d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
869 if (!myTrianglePreference) {
870 myHelper->AddFace(a, b, c, d);
873 splitQuadFace(meshDS, geomFaceID, a, b, c, d);
876 if (near + 1 < g) { // if d not is at g - make additional triangles
877 for (int k = near + 1; k < g; k++) {
878 c = quad->uv_grid[nbhoriz*k + 1].node;
880 d = quad->uv_grid[nbhoriz*jup + 1].node; //uv_e2[1].node;
882 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
883 myHelper->AddFace(a, c, d);
897 //=============================================================================
901 //=============================================================================
903 bool StdMeshers_Quadrangle_2D::Evaluate(SMESH_Mesh& aMesh,
904 const TopoDS_Shape& aFace,
905 MapShapeNbElems& aResMap)
908 aMesh.GetSubMesh(aFace);
910 std::vector<int> aNbNodes(4);
911 bool IsQuadratic = false;
912 if (!checkNbEdgesForEvaluate(aMesh, aFace, aResMap, aNbNodes, IsQuadratic)) {
913 std::vector<int> aResVec(SMDSEntity_Last);
914 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
915 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
916 aResMap.insert(std::make_pair(sm,aResVec));
917 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
918 smError.reset(new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
922 if (myQuadranglePreference) {
923 int n1 = aNbNodes[0];
924 int n2 = aNbNodes[1];
925 int n3 = aNbNodes[2];
926 int n4 = aNbNodes[3];
927 int nfull = n1+n2+n3+n4;
930 if (nfull==ntmp && ((n1!=n3) || (n2!=n4))) {
931 // special path for using only quandrangle faces
932 return evaluateQuadPref(aMesh, aFace, aNbNodes, aResMap, IsQuadratic);
937 int nbdown = aNbNodes[0];
938 int nbup = aNbNodes[2];
940 int nbright = aNbNodes[1];
941 int nbleft = aNbNodes[3];
943 int nbhoriz = Min(nbdown, nbup);
944 int nbvertic = Min(nbright, nbleft);
946 int dh = Max(nbdown, nbup) - nbhoriz;
947 int dv = Max(nbright, nbleft) - nbvertic;
954 int nbNodes = (nbhoriz-2)*(nbvertic-2);
955 //int nbFaces3 = dh + dv + kdh*(nbvertic-1)*2 + kdv*(nbhoriz-1)*2;
956 int nbFaces3 = dh + dv;
957 //if (kdh==1 && kdv==1) nbFaces3 -= 2;
958 //if (dh>0 && dv>0) nbFaces3 -= 2;
959 //int nbFaces4 = (nbhoriz-1-kdh)*(nbvertic-1-kdv);
960 int nbFaces4 = (nbhoriz-1)*(nbvertic-1);
962 std::vector<int> aVec(SMDSEntity_Last);
963 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
965 aVec[SMDSEntity_Quad_Triangle] = nbFaces3;
966 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4;
967 int nbbndedges = nbdown + nbup + nbright + nbleft -4;
968 int nbintedges = (nbFaces4*4 + nbFaces3*3 - nbbndedges) / 2;
969 aVec[SMDSEntity_Node] = nbNodes + nbintedges;
970 if (aNbNodes.size()==5) {
971 aVec[SMDSEntity_Quad_Triangle] = nbFaces3 + aNbNodes[3] -1;
972 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces4 - aNbNodes[3] +1;
976 aVec[SMDSEntity_Node] = nbNodes;
977 aVec[SMDSEntity_Triangle] = nbFaces3;
978 aVec[SMDSEntity_Quadrangle] = nbFaces4;
979 if (aNbNodes.size()==5) {
980 aVec[SMDSEntity_Triangle] = nbFaces3 + aNbNodes[3] - 1;
981 aVec[SMDSEntity_Quadrangle] = nbFaces4 - aNbNodes[3] + 1;
984 SMESH_subMesh * sm = aMesh.GetSubMesh(aFace);
985 aResMap.insert(std::make_pair(sm,aVec));
990 //================================================================================
992 * \brief Return true if the algorithm can mesh this shape
993 * \param [in] aShape - shape to check
994 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
995 * else, returns OK if at least one shape is OK
997 //================================================================================
999 bool StdMeshers_Quadrangle_2D::IsApplicable( const TopoDS_Shape & aShape, bool toCheckAll )
1001 int nbFoundFaces = 0;
1002 for (TopExp_Explorer exp( aShape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFoundFaces )
1004 const TopoDS_Shape& aFace = exp.Current();
1005 int nbWire = SMESH_MesherHelper::Count( aFace, TopAbs_WIRE, false );
1006 if ( nbWire != 1 ) {
1007 if ( toCheckAll ) return false;
1011 int nbNoDegenEdges = 0, totalNbEdges = 0;
1012 TopExp_Explorer eExp( aFace, TopAbs_EDGE );
1013 for ( ; eExp.More() && nbNoDegenEdges < 3; eExp.Next(), ++totalNbEdges ) {
1014 if ( !SMESH_Algo::isDegenerated( TopoDS::Edge( eExp.Current() )))
1017 if ( toCheckAll && ( totalNbEdges < 4 && nbNoDegenEdges < 3 )) return false;
1018 if ( !toCheckAll && ( totalNbEdges >= 4 || nbNoDegenEdges >= 3 )) return true;
1020 return ( toCheckAll && nbFoundFaces != 0 );
1023 //================================================================================
1025 * \brief Return true if only two given edges meat at their common vertex
1027 //================================================================================
1029 static bool twoEdgesMeatAtVertex(const TopoDS_Edge& e1,
1030 const TopoDS_Edge& e2,
1034 if (!TopExp::CommonVertex(e1, e2, v))
1036 TopTools_ListIteratorOfListOfShape ancestIt(mesh.GetAncestors(v));
1037 for (; ancestIt.More() ; ancestIt.Next())
1038 if (ancestIt.Value().ShapeType() == TopAbs_EDGE)
1039 if (!e1.IsSame(ancestIt.Value()) && !e2.IsSame(ancestIt.Value()))
1044 //=============================================================================
1048 //=============================================================================
1050 FaceQuadStruct::Ptr StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh & aMesh,
1051 const TopoDS_Shape & aShape,
1052 const bool considerMesh,
1053 SMESH_MesherHelper* aFaceHelper)
1055 if ( !myQuadList.empty() && myQuadList.front()->face.IsSame( aShape ))
1056 return myQuadList.front();
1058 TopoDS_Face F = TopoDS::Face(aShape);
1059 if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
1060 const bool ignoreMediumNodes = _quadraticMesh;
1062 // verify 1 wire only
1063 list< TopoDS_Edge > edges;
1064 list< int > nbEdgesInWire;
1065 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1067 error(COMPERR_BAD_SHAPE, TComm("Wrong number of wires: ") << nbWire);
1068 return FaceQuadStruct::Ptr();
1071 // find corner vertices of the quad
1072 myHelper = ( aFaceHelper && aFaceHelper->GetSubShape() == aShape ) ? aFaceHelper : NULL;
1073 vector<TopoDS_Vertex> corners;
1074 int nbDegenEdges, nbSides = getCorners( F, aMesh, edges, corners, nbDegenEdges, considerMesh );
1077 return FaceQuadStruct::Ptr();
1079 FaceQuadStruct::Ptr quad( new FaceQuadStruct );
1080 quad->side.reserve(nbEdgesInWire.front());
1083 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1084 if ( nbSides == 3 ) // 3 sides and corners[0] is a vertex with myTriaVertexID
1086 for ( int iSide = 0; iSide < 3; ++iSide )
1088 list< TopoDS_Edge > sideEdges;
1089 TopoDS_Vertex nextSideV = corners[( iSide + 1 ) % 3 ];
1090 while ( edgeIt != edges.end() &&
1091 !nextSideV.IsSame( SMESH_MesherHelper::IthVertex( 0, *edgeIt )))
1092 if ( SMESH_Algo::isDegenerated( *edgeIt ))
1095 sideEdges.push_back( *edgeIt++ );
1096 if ( !sideEdges.empty() )
1097 quad->side.push_back( StdMeshers_FaceSide::New(F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1098 ignoreMediumNodes, myHelper, myProxyMesh));
1102 const vector<UVPtStruct>& UVPSleft = quad->side[0].GetUVPtStruct(true,0);
1103 /* vector<UVPtStruct>& UVPStop = */quad->side[1].GetUVPtStruct(false,1);
1104 /* vector<UVPtStruct>& UVPSright = */quad->side[2].GetUVPtStruct(true,1);
1105 const SMDS_MeshNode* aNode = UVPSleft[0].node;
1106 gp_Pnt2d aPnt2d = UVPSleft[0].UV();
1107 quad->side.push_back( StdMeshers_FaceSide::New( quad->side[1].grid.get(), aNode, &aPnt2d ));
1108 myNeedSmooth = ( nbDegenEdges > 0 );
1113 myNeedSmooth = ( corners.size() == 4 && nbDegenEdges > 0 );
1114 int iSide = 0, nbUsedDegen = 0, nbLoops = 0;
1115 for ( ; edgeIt != edges.end(); ++nbLoops )
1117 list< TopoDS_Edge > sideEdges;
1118 TopoDS_Vertex nextSideV = corners[( iSide + 1 - nbUsedDegen ) % corners.size() ];
1119 bool nextSideVReached = false;
1122 const TopoDS_Edge& edge = *edgeIt;
1123 nextSideVReached = nextSideV.IsSame( myHelper->IthVertex( 1, edge ));
1124 if ( SMESH_Algo::isDegenerated( edge ))
1126 if ( !myNeedSmooth ) // need to make a side on a degen edge
1128 if ( sideEdges.empty() )
1130 sideEdges.push_back( edge );
1132 nextSideVReached = true;
1140 else //if ( !myHelper || !myHelper->IsRealSeam( edge ))
1142 sideEdges.push_back( edge );
1146 while ( edgeIt != edges.end() && !nextSideVReached );
1148 if ( !sideEdges.empty() )
1150 quad->side.push_back
1151 ( StdMeshers_FaceSide::New( F, sideEdges, &aMesh, iSide < QUAD_TOP_SIDE,
1152 ignoreMediumNodes, myHelper, myProxyMesh ));
1155 if ( quad->side.size() == 4 )
1159 error(TComm("Bug: infinite loop in StdMeshers_Quadrangle_2D::CheckNbEdges()"));
1164 if ( quad && quad->side.size() != 4 )
1166 error(TComm("Bug: ") << quad->side.size() << " sides found instead of 4");
1175 //=============================================================================
1179 //=============================================================================
1181 bool StdMeshers_Quadrangle_2D::checkNbEdgesForEvaluate(SMESH_Mesh& aMesh,
1182 const TopoDS_Shape & aShape,
1183 MapShapeNbElems& aResMap,
1184 std::vector<int>& aNbNodes,
1188 const TopoDS_Face & F = TopoDS::Face(aShape);
1190 // verify 1 wire only, with 4 edges
1191 list< TopoDS_Edge > edges;
1192 list< int > nbEdgesInWire;
1193 int nbWire = SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1201 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
1202 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1203 MapShapeNbElemsItr anIt = aResMap.find(sm);
1204 if (anIt==aResMap.end()) {
1207 std::vector<int> aVec = (*anIt).second;
1208 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
1209 if (nbEdgesInWire.front() == 3) { // exactly 3 edges
1210 if (myTriaVertexID>0) {
1211 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
1212 TopoDS_Vertex V = TopoDS::Vertex(meshDS->IndexToShape(myTriaVertexID));
1214 TopoDS_Edge E1,E2,E3;
1215 for (; edgeIt != edges.end(); ++edgeIt) {
1216 TopoDS_Edge E = TopoDS::Edge(*edgeIt);
1217 TopoDS_Vertex VF, VL;
1218 TopExp::Vertices(E, VF, VL, true);
1221 else if (VL.IsSame(V))
1226 SMESH_subMesh * sm = aMesh.GetSubMesh(E1);
1227 MapShapeNbElemsItr anIt = aResMap.find(sm);
1228 if (anIt==aResMap.end()) return false;
1229 std::vector<int> aVec = (*anIt).second;
1231 aNbNodes[0] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1233 aNbNodes[0] = aVec[SMDSEntity_Node] + 2;
1234 sm = aMesh.GetSubMesh(E2);
1235 anIt = aResMap.find(sm);
1236 if (anIt==aResMap.end()) return false;
1237 aVec = (*anIt).second;
1239 aNbNodes[1] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1241 aNbNodes[1] = aVec[SMDSEntity_Node] + 2;
1242 sm = aMesh.GetSubMesh(E3);
1243 anIt = aResMap.find(sm);
1244 if (anIt==aResMap.end()) return false;
1245 aVec = (*anIt).second;
1247 aNbNodes[2] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1249 aNbNodes[2] = aVec[SMDSEntity_Node] + 2;
1250 aNbNodes[3] = aNbNodes[1];
1256 if (nbEdgesInWire.front() == 4) { // exactly 4 edges
1257 for (; edgeIt != edges.end(); edgeIt++) {
1258 SMESH_subMesh * sm = aMesh.GetSubMesh(*edgeIt);
1259 MapShapeNbElemsItr anIt = aResMap.find(sm);
1260 if (anIt==aResMap.end()) {
1263 std::vector<int> aVec = (*anIt).second;
1265 aNbNodes[nbSides] = (aVec[SMDSEntity_Node]-1)/2 + 2;
1267 aNbNodes[nbSides] = aVec[SMDSEntity_Node] + 2;
1271 else if (nbEdgesInWire.front() > 4) { // more than 4 edges - try to unite some
1272 list< TopoDS_Edge > sideEdges;
1273 while (!edges.empty()) {
1275 sideEdges.splice(sideEdges.end(), edges, edges.begin()); // edges.front() -> sideEdges.end()
1276 bool sameSide = true;
1277 while (!edges.empty() && sameSide) {
1278 sameSide = SMESH_Algo::IsContinuous(sideEdges.back(), edges.front());
1280 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1282 if (nbSides == 0) { // go backward from the first edge
1284 while (!edges.empty() && sameSide) {
1285 sameSide = SMESH_Algo::IsContinuous(sideEdges.front(), edges.back());
1287 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1290 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1291 aNbNodes[nbSides] = 1;
1292 for (; ite!=sideEdges.end(); ite++) {
1293 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1294 MapShapeNbElemsItr anIt = aResMap.find(sm);
1295 if (anIt==aResMap.end()) {
1298 std::vector<int> aVec = (*anIt).second;
1300 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1302 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1306 // issue 20222. Try to unite only edges shared by two same faces
1309 SMESH_Block::GetOrderedEdges (F, edges, nbEdgesInWire);
1310 while (!edges.empty()) {
1312 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1313 bool sameSide = true;
1314 while (!edges.empty() && sameSide) {
1316 SMESH_Algo::IsContinuous(sideEdges.back(), edges.front()) &&
1317 twoEdgesMeatAtVertex(sideEdges.back(), edges.front(), aMesh);
1319 sideEdges.splice(sideEdges.end(), edges, edges.begin());
1321 if (nbSides == 0) { // go backward from the first edge
1323 while (!edges.empty() && sameSide) {
1325 SMESH_Algo::IsContinuous(sideEdges.front(), edges.back()) &&
1326 twoEdgesMeatAtVertex(sideEdges.front(), edges.back(), aMesh);
1328 sideEdges.splice(sideEdges.begin(), edges, --edges.end());
1331 list<TopoDS_Edge>::iterator ite = sideEdges.begin();
1332 aNbNodes[nbSides] = 1;
1333 for (; ite!=sideEdges.end(); ite++) {
1334 SMESH_subMesh * sm = aMesh.GetSubMesh(*ite);
1335 MapShapeNbElemsItr anIt = aResMap.find(sm);
1336 if (anIt==aResMap.end()) {
1339 std::vector<int> aVec = (*anIt).second;
1341 aNbNodes[nbSides] += (aVec[SMDSEntity_Node]-1)/2 + 1;
1343 aNbNodes[nbSides] += aVec[SMDSEntity_Node] + 1;
1351 nbSides = nbEdgesInWire.front();
1352 error(COMPERR_BAD_SHAPE, TComm("Face must have 4 sides but not ") << nbSides);
1360 //=============================================================================
1364 //=============================================================================
1367 StdMeshers_Quadrangle_2D::CheckAnd2Dcompute (SMESH_Mesh & aMesh,
1368 const TopoDS_Shape & aShape,
1369 const bool CreateQuadratic)
1371 _quadraticMesh = CreateQuadratic;
1373 FaceQuadStruct::Ptr quad = CheckNbEdges(aMesh, aShape);
1376 // set normalized grid on unit square in parametric domain
1377 if ( ! setNormalizedGrid( quad ))
1385 inline const vector<UVPtStruct>& getUVPtStructIn(FaceQuadStruct::Ptr& quad, int i, int nbSeg)
1387 bool isXConst = (i == QUAD_BOTTOM_SIDE || i == QUAD_TOP_SIDE);
1388 double constValue = (i == QUAD_BOTTOM_SIDE || i == QUAD_LEFT_SIDE) ? 0 : 1;
1390 quad->nbNodeOut(i) ?
1391 quad->side[i].grid->SimulateUVPtStruct(nbSeg,isXConst,constValue) :
1392 quad->side[i].grid->GetUVPtStruct (isXConst,constValue);
1394 inline gp_UV calcUV(double x, double y,
1395 const gp_UV& a0,const gp_UV& a1,const gp_UV& a2,const gp_UV& a3,
1396 const gp_UV& p0,const gp_UV& p1,const gp_UV& p2,const gp_UV& p3)
1399 ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
1400 ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
1404 //=============================================================================
1408 //=============================================================================
1410 bool StdMeshers_Quadrangle_2D::setNormalizedGrid (FaceQuadStruct::Ptr quad)
1412 if ( !quad->uv_grid.empty() )
1415 // Algorithme décrit dans "Génération automatique de maillages"
1416 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
1417 // traitement dans le domaine paramétrique 2d u,v
1418 // transport - projection sur le carré unité
1421 // |<----north-2-------^ a3 -------------> a2
1423 // west-3 east-1 =right | |
1427 // v----south-0--------> a0 -------------> a1
1431 const FaceQuadStruct::Side & bSide = quad->side[0];
1432 const FaceQuadStruct::Side & rSide = quad->side[1];
1433 const FaceQuadStruct::Side & tSide = quad->side[2];
1434 const FaceQuadStruct::Side & lSide = quad->side[3];
1436 int nbhoriz = Min( bSide.NbPoints(), tSide.NbPoints() );
1437 int nbvertic = Min( rSide.NbPoints(), lSide.NbPoints() );
1438 if ( nbhoriz < 1 || nbvertic < 1 )
1439 return error("Algo error: empty quad");
1441 if ( myQuadList.size() == 1 )
1443 // all sub-quads must have NO sides with nbNodeOut > 0
1444 quad->nbNodeOut(0) = Max( 0, bSide.grid->NbPoints() - tSide.grid->NbPoints() );
1445 quad->nbNodeOut(1) = Max( 0, rSide.grid->NbPoints() - lSide.grid->NbPoints() );
1446 quad->nbNodeOut(2) = Max( 0, tSide.grid->NbPoints() - bSide.grid->NbPoints() );
1447 quad->nbNodeOut(3) = Max( 0, lSide.grid->NbPoints() - rSide.grid->NbPoints() );
1449 const vector<UVPtStruct>& uv_e0 = bSide.GetUVPtStruct();
1450 const vector<UVPtStruct>& uv_e1 = rSide.GetUVPtStruct();
1451 const vector<UVPtStruct>& uv_e2 = tSide.GetUVPtStruct();
1452 const vector<UVPtStruct>& uv_e3 = lSide.GetUVPtStruct();
1453 if (uv_e0.empty() || uv_e1.empty() || uv_e2.empty() || uv_e3.empty())
1454 //return error("Can't find nodes on sides");
1455 return error(COMPERR_BAD_INPUT_MESH);
1457 quad->uv_grid.resize( nbvertic * nbhoriz );
1458 quad->iSize = nbhoriz;
1459 quad->jSize = nbvertic;
1460 UVPtStruct *uv_grid = & quad->uv_grid[0];
1462 quad->uv_box.Clear();
1464 // copy data of face boundary
1466 FaceQuadStruct::SideIterator sideIter;
1470 const double x0 = bSide.First().normParam;
1471 const double dx = bSide.Last().normParam - bSide.First().normParam;
1472 for ( sideIter.Init( bSide ); sideIter.More(); sideIter.Next() ) {
1473 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1474 sideIter.UVPt().y = 0.;
1475 uv_grid[ j * nbhoriz + sideIter.Count() ] = sideIter.UVPt();
1476 quad->uv_box.Add( sideIter.UVPt().UV() );
1480 const int i = nbhoriz - 1;
1481 const double y0 = rSide.First().normParam;
1482 const double dy = rSide.Last().normParam - rSide.First().normParam;
1483 sideIter.Init( rSide );
1484 if ( quad->UVPt( i, sideIter.Count() ).node )
1485 sideIter.Next(); // avoid copying from a split emulated side
1486 for ( ; sideIter.More(); sideIter.Next() ) {
1487 sideIter.UVPt().x = 1.;
1488 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1489 uv_grid[ sideIter.Count() * nbhoriz + i ] = sideIter.UVPt();
1490 quad->uv_box.Add( sideIter.UVPt().UV() );
1494 const int j = nbvertic - 1;
1495 const double x0 = tSide.First().normParam;
1496 const double dx = tSide.Last().normParam - tSide.First().normParam;
1497 int i = 0, nb = nbhoriz;
1498 sideIter.Init( tSide );
1499 if ( quad->UVPt( nb-1, j ).node ) --nb; // avoid copying from a split emulated side
1500 for ( ; i < nb; i++, sideIter.Next()) {
1501 sideIter.UVPt().x = ( sideIter.UVPt().normParam - x0 ) / dx;
1502 sideIter.UVPt().y = 1.;
1503 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1504 quad->uv_box.Add( sideIter.UVPt().UV() );
1509 const double y0 = lSide.First().normParam;
1510 const double dy = lSide.Last().normParam - lSide.First().normParam;
1511 int j = 0, nb = nbvertic;
1512 sideIter.Init( lSide );
1513 if ( quad->UVPt( i, j ).node )
1514 ++j, sideIter.Next(); // avoid copying from a split emulated side
1515 if ( quad->UVPt( i, nb-1 ).node )
1517 for ( ; j < nb; j++, sideIter.Next()) {
1518 sideIter.UVPt().x = 0.;
1519 sideIter.UVPt().y = ( sideIter.UVPt().normParam - y0 ) / dy;
1520 uv_grid[ j * nbhoriz + i ] = sideIter.UVPt();
1521 quad->uv_box.Add( sideIter.UVPt().UV() );
1525 // normalized 2d parameters on grid
1527 for (int i = 1; i < nbhoriz-1; i++)
1529 const double x0 = quad->UVPt( i, 0 ).x;
1530 const double x1 = quad->UVPt( i, nbvertic-1 ).x;
1531 for (int j = 1; j < nbvertic-1; j++)
1533 const double y0 = quad->UVPt( 0, j ).y;
1534 const double y1 = quad->UVPt( nbhoriz-1, j ).y;
1535 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
1536 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1537 double y = y0 + x * (y1 - y0);
1538 int ij = j * nbhoriz + i;
1541 uv_grid[ij].node = NULL;
1545 // projection on 2d domain (u,v)
1547 gp_UV a0 = quad->UVPt( 0, 0 ).UV();
1548 gp_UV a1 = quad->UVPt( nbhoriz-1, 0 ).UV();
1549 gp_UV a2 = quad->UVPt( nbhoriz-1, nbvertic-1 ).UV();
1550 gp_UV a3 = quad->UVPt( 0, nbvertic-1 ).UV();
1552 for (int i = 1; i < nbhoriz-1; i++)
1554 gp_UV p0 = quad->UVPt( i, 0 ).UV();
1555 gp_UV p2 = quad->UVPt( i, nbvertic-1 ).UV();
1556 for (int j = 1; j < nbvertic-1; j++)
1558 gp_UV p1 = quad->UVPt( nbhoriz-1, j ).UV();
1559 gp_UV p3 = quad->UVPt( 0, j ).UV();
1561 int ij = j * nbhoriz + i;
1562 double x = uv_grid[ij].x;
1563 double y = uv_grid[ij].y;
1565 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1567 uv_grid[ij].u = uv.X();
1568 uv_grid[ij].v = uv.Y();
1574 //=======================================================================
1575 //function : ShiftQuad
1576 //purpose : auxiliary function for computeQuadPref
1577 //=======================================================================
1579 void StdMeshers_Quadrangle_2D::shiftQuad(FaceQuadStruct::Ptr& quad, const int num )
1581 quad->shift( num, /*ori=*/true, /*keepGrid=*/myQuadList.size() > 1 );
1584 //================================================================================
1586 * \brief Rotate sides of a quad CCW by given nb of quartes
1587 * \param nb - number of rotation quartes
1588 * \param ori - to keep orientation of sides as in an unit quad or not
1589 * \param keepGrid - if \c true Side::grid is not changed, Side::from and Side::to
1590 * are altered instead
1592 //================================================================================
1594 void FaceQuadStruct::shift( size_t nb, bool ori, bool keepGrid )
1596 if ( nb == 0 ) return;
1598 nb = nb % NB_QUAD_SIDES;
1600 vector< Side > newSides( side.size() );
1601 vector< Side* > sidePtrs( side.size() );
1602 for (int i = QUAD_BOTTOM_SIDE; i < NB_QUAD_SIDES; ++i)
1604 int id = (i + nb) % NB_QUAD_SIDES;
1607 bool wasForward = (i < QUAD_TOP_SIDE);
1608 bool newForward = (id < QUAD_TOP_SIDE);
1609 if ( wasForward != newForward )
1610 side[ i ].Reverse( keepGrid );
1612 newSides[ id ] = side[ i ];
1613 sidePtrs[ i ] = & side[ i ];
1615 // make newSides refer newSides via Side::Contact's
1616 for ( size_t i = 0; i < newSides.size(); ++i )
1618 FaceQuadStruct::Side& ns = newSides[ i ];
1619 for ( size_t iC = 0; iC < ns.contacts.size(); ++iC )
1621 FaceQuadStruct::Side* oSide = ns.contacts[iC].other_side;
1622 vector< Side* >::iterator sIt = std::find( sidePtrs.begin(), sidePtrs.end(), oSide );
1623 if ( sIt != sidePtrs.end() )
1624 ns.contacts[iC].other_side = & newSides[ *sIt - sidePtrs[0] ];
1627 newSides.swap( side );
1629 if ( keepGrid && !uv_grid.empty() )
1631 if ( nb == 2 ) // "PI"
1633 std::reverse( uv_grid.begin(), uv_grid.end() );
1637 FaceQuadStruct newQuad;
1638 newQuad.uv_grid.resize( uv_grid.size() );
1639 newQuad.iSize = jSize;
1640 newQuad.jSize = iSize;
1641 int i, j, iRev, jRev;
1642 int *iNew = ( nb == 1 ) ? &jRev : &j;
1643 int *jNew = ( nb == 1 ) ? &i : &iRev;
1644 for ( i = 0, iRev = iSize-1; i < iSize; ++i, --iRev )
1645 for ( j = 0, jRev = jSize-1; j < jSize; ++j, --jRev )
1646 newQuad.UVPt( *iNew, *jNew ) = UVPt( i, j );
1648 std::swap( iSize, jSize );
1649 std::swap( uv_grid, newQuad.uv_grid );
1658 //=======================================================================
1660 //purpose : auxiliary function for computeQuadPref
1661 //=======================================================================
1663 static gp_UV calcUV(double x0, double x1, double y0, double y1,
1664 FaceQuadStruct::Ptr& quad,
1665 const gp_UV& a0, const gp_UV& a1,
1666 const gp_UV& a2, const gp_UV& a3)
1668 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1669 double y = y0 + x * (y1 - y0);
1671 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1672 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1673 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1674 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1676 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1681 //=======================================================================
1682 //function : calcUV2
1683 //purpose : auxiliary function for computeQuadPref
1684 //=======================================================================
1686 static gp_UV calcUV2(double x, double y,
1687 FaceQuadStruct::Ptr& quad,
1688 const gp_UV& a0, const gp_UV& a1,
1689 const gp_UV& a2, const gp_UV& a3)
1691 gp_UV p0 = quad->side[QUAD_BOTTOM_SIDE].grid->Value2d(x).XY();
1692 gp_UV p1 = quad->side[QUAD_RIGHT_SIDE ].grid->Value2d(y).XY();
1693 gp_UV p2 = quad->side[QUAD_TOP_SIDE ].grid->Value2d(x).XY();
1694 gp_UV p3 = quad->side[QUAD_LEFT_SIDE ].grid->Value2d(y).XY();
1696 gp_UV uv = calcUV(x,y, a0,a1,a2,a3, p0,p1,p2,p3);
1702 //=======================================================================
1704 * Create only quandrangle faces
1706 //=======================================================================
1708 bool StdMeshers_Quadrangle_2D::computeQuadPref (SMESH_Mesh & aMesh,
1709 const TopoDS_Face& aFace,
1710 FaceQuadStruct::Ptr quad)
1712 const bool OldVersion = (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED);
1713 const bool WisF = true;
1715 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1716 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
1717 int i,j, geomFaceID = meshDS->ShapeToIndex(aFace);
1719 int nb = quad->side[0].NbPoints();
1720 int nr = quad->side[1].NbPoints();
1721 int nt = quad->side[2].NbPoints();
1722 int nl = quad->side[3].NbPoints();
1723 int dh = abs(nb-nt);
1724 int dv = abs(nr-nl);
1726 if ( myForcedPnts.empty() )
1728 // rotate sides to be as in the picture below and to have
1729 // dh >= dv and nt > nb
1731 shiftQuad( quad, ( nt > nb ) ? 0 : 2 );
1733 shiftQuad( quad, ( nr > nl ) ? 1 : 3 );
1737 // rotate the quad to have nt > nb [and nr > nl]
1739 shiftQuad ( quad, nr > nl ? 1 : 2 );
1741 shiftQuad( quad, nb == nt ? 1 : 0 );
1743 shiftQuad( quad, 3 );
1746 nb = quad->side[0].NbPoints();
1747 nr = quad->side[1].NbPoints();
1748 nt = quad->side[2].NbPoints();
1749 nl = quad->side[3].NbPoints();
1752 int nbh = Max(nb,nt);
1753 int nbv = Max(nr,nl);
1757 // Orientation of face and 3 main domain for future faces
1758 // ----------- Old version ---------------
1764 // left | |__| | right
1771 // ----------- New version ---------------
1777 // left |/________\| right
1785 //const int bfrom = quad->side[0].from;
1786 //const int rfrom = quad->side[1].from;
1787 const int tfrom = quad->side[2].from;
1788 //const int lfrom = quad->side[3].from;
1790 const vector<UVPtStruct>& uv_eb_vec = quad->side[0].GetUVPtStruct(true,0);
1791 const vector<UVPtStruct>& uv_er_vec = quad->side[1].GetUVPtStruct(false,1);
1792 const vector<UVPtStruct>& uv_et_vec = quad->side[2].GetUVPtStruct(true,1);
1793 const vector<UVPtStruct>& uv_el_vec = quad->side[3].GetUVPtStruct(false,0);
1794 if (uv_eb_vec.empty() ||
1795 uv_er_vec.empty() ||
1796 uv_et_vec.empty() ||
1798 return error(COMPERR_BAD_INPUT_MESH);
1800 FaceQuadStruct::SideIterator uv_eb, uv_er, uv_et, uv_el;
1801 uv_eb.Init( quad->side[0] );
1802 uv_er.Init( quad->side[1] );
1803 uv_et.Init( quad->side[2] );
1804 uv_el.Init( quad->side[3] );
1806 gp_UV a0,a1,a2,a3, p0,p1,p2,p3, uv;
1809 a0 = uv_eb[ 0 ].UV();
1810 a1 = uv_er[ 0 ].UV();
1811 a2 = uv_er[ nr-1 ].UV();
1812 a3 = uv_et[ 0 ].UV();
1814 if ( !myForcedPnts.empty() )
1816 if ( dv != 0 && dh != 0 ) // here myQuadList.size() == 1
1818 const int dmin = Min( dv, dh );
1820 // Make a side separating domains L and Cb
1821 StdMeshers_FaceSidePtr sideLCb;
1822 UVPtStruct p3dom; // a point where 3 domains meat
1824 vector<UVPtStruct> pointsLCb( dmin+1 ); // 1--------1
1825 pointsLCb[0] = uv_eb[0]; // | | |
1826 for ( int i = 1; i <= dmin; ++i ) // | |Ct|
1828 x = uv_et[ i ].normParam; // | |__|
1829 y = uv_er[ i ].normParam; // | / |
1830 p0 = quad->side[0].grid->Value2d( x ).XY(); // | / Cb |dmin
1831 p1 = uv_er[ i ].UV(); // |/ |
1832 p2 = uv_et[ i ].UV(); // 0--------0
1833 p3 = quad->side[3].grid->Value2d( y ).XY();
1834 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1835 pointsLCb[ i ].u = uv.X();
1836 pointsLCb[ i ].v = uv.Y();
1838 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1839 p3dom = pointsLCb.back();
1841 gp_Pnt xyz = S->Value( p3dom.u, p3dom.v );
1842 p3dom.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, p3dom.u, p3dom.v );
1843 pointsLCb.back() = p3dom;
1845 // Make a side separating domains L and Ct
1846 StdMeshers_FaceSidePtr sideLCt;
1848 vector<UVPtStruct> pointsLCt( nl );
1849 pointsLCt[0] = p3dom;
1850 pointsLCt.back() = uv_et[ dmin ];
1851 x = uv_et[ dmin ].normParam;
1852 p0 = quad->side[0].grid->Value2d( x ).XY();
1853 p2 = uv_et[ dmin ].UV();
1854 double y0 = uv_er[ dmin ].normParam;
1855 for ( int i = 1; i < nl-1; ++i )
1857 y = y0 + i / ( nl-1. ) * ( 1. - y0 );
1858 p1 = quad->side[1].grid->Value2d( y ).XY();
1859 p3 = quad->side[3].grid->Value2d( y ).XY();
1860 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1861 pointsLCt[ i ].u = uv.X();
1862 pointsLCt[ i ].v = uv.Y();
1864 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
1866 // Make a side separating domains Cb and Ct
1867 StdMeshers_FaceSidePtr sideCbCt;
1869 vector<UVPtStruct> pointsCbCt( nb );
1870 pointsCbCt[0] = p3dom;
1871 pointsCbCt.back() = uv_er[ dmin ];
1872 y = uv_er[ dmin ].normParam;
1873 p1 = uv_er[ dmin ].UV();
1874 p3 = quad->side[3].grid->Value2d( y ).XY();
1875 double x0 = uv_et[ dmin ].normParam;
1876 for ( int i = 1; i < nb-1; ++i )
1878 x = x0 + i / ( nb-1. ) * ( 1. - x0 );
1879 p2 = quad->side[2].grid->Value2d( x ).XY();
1880 p0 = quad->side[0].grid->Value2d( x ).XY();
1881 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1882 pointsCbCt[ i ].u = uv.X();
1883 pointsCbCt[ i ].v = uv.Y();
1885 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
1888 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
1889 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
1890 qCb->side.resize(4);
1891 qCb->side[0] = quad->side[0];
1892 qCb->side[1] = quad->side[1];
1893 qCb->side[2] = sideCbCt;
1894 qCb->side[3] = sideLCb;
1895 qCb->side[1].to = dmin+1;
1897 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
1898 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
1900 qL->side[0] = sideLCb;
1901 qL->side[1] = sideLCt;
1902 qL->side[2] = quad->side[2];
1903 qL->side[3] = quad->side[3];
1904 qL->side[2].to = dmin+1;
1905 // Make Ct from the main quad
1906 FaceQuadStruct::Ptr qCt = quad;
1907 qCt->side[0] = sideCbCt;
1908 qCt->side[3] = sideLCt;
1909 qCt->side[1].from = dmin;
1910 qCt->side[2].from = dmin;
1911 qCt->uv_grid.clear();
1915 qCb->side[3].AddContact( dmin, & qCb->side[2], 0 );
1916 qCb->side[3].AddContact( dmin, & qCt->side[3], 0 );
1917 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
1918 qCt->side[0].AddContact( 0, & qL ->side[0], dmin );
1919 qL ->side[0].AddContact( dmin, & qL ->side[1], 0 );
1920 qL ->side[0].AddContact( dmin, & qCb->side[2], 0 );
1923 return computeQuadDominant( aMesh, aFace );
1925 return computeQuadPref( aMesh, aFace, qCt );
1927 } // if ( dv != 0 && dh != 0 )
1929 //const int db = quad->side[0].IsReversed() ? -1 : +1;
1930 //const int dr = quad->side[1].IsReversed() ? -1 : +1;
1931 const int dt = quad->side[2].IsReversed() ? -1 : +1;
1932 //const int dl = quad->side[3].IsReversed() ? -1 : +1;
1934 // Case dv == 0, here possibly myQuadList.size() > 1
1946 const int lw = dh/2; // lateral width
1950 double lL = quad->side[3].Length();
1951 double lLwL = quad->side[2].Length( tfrom,
1952 tfrom + ( lw ) * dt );
1953 yCbL = lLwL / ( lLwL + lL );
1955 double lR = quad->side[1].Length();
1956 double lLwR = quad->side[2].Length( tfrom + ( lw + nb-1 ) * dt,
1957 tfrom + ( lw + nb-1 + lw ) * dt);
1958 yCbR = lLwR / ( lLwR + lR );
1960 // Make sides separating domains Cb and L and R
1961 StdMeshers_FaceSidePtr sideLCb, sideRCb;
1962 UVPtStruct pTBL, pTBR; // points where 3 domains meat
1964 vector<UVPtStruct> pointsLCb( lw+1 ), pointsRCb( lw+1 );
1965 pointsLCb[0] = uv_eb[ 0 ];
1966 pointsRCb[0] = uv_eb[ nb-1 ];
1967 for ( int i = 1, i2 = nt-2; i <= lw; ++i, --i2 )
1969 x = quad->side[2].Param( i );
1971 p0 = quad->side[0].Value2d( x );
1972 p1 = quad->side[1].Value2d( y );
1973 p2 = uv_et[ i ].UV();
1974 p3 = quad->side[3].Value2d( y );
1975 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1976 pointsLCb[ i ].u = uv.X();
1977 pointsLCb[ i ].v = uv.Y();
1978 pointsLCb[ i ].x = x;
1980 x = quad->side[2].Param( i2 );
1982 p1 = quad->side[1].Value2d( y );
1983 p0 = quad->side[0].Value2d( x );
1984 p2 = uv_et[ i2 ].UV();
1985 p3 = quad->side[3].Value2d( y );
1986 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
1987 pointsRCb[ i ].u = uv.X();
1988 pointsRCb[ i ].v = uv.Y();
1989 pointsRCb[ i ].x = x;
1991 sideLCb = StdMeshers_FaceSide::New( pointsLCb, aFace );
1992 sideRCb = StdMeshers_FaceSide::New( pointsRCb, aFace );
1993 pTBL = pointsLCb.back();
1994 pTBR = pointsRCb.back();
1996 gp_Pnt xyz = S->Value( pTBL.u, pTBL.v );
1997 pTBL.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, pTBL.u, pTBL.v );
1998 pointsLCb.back() = pTBL;
2001 gp_Pnt xyz = S->Value( pTBR.u, pTBR.v );
2002 pTBR.node = myHelper->AddNode( xyz.X(), xyz.Y(), xyz.Z(), 0, pTBR.u, pTBR.v );
2003 pointsRCb.back() = pTBR;
2006 // Make sides separating domains Ct and L and R
2007 StdMeshers_FaceSidePtr sideLCt, sideRCt;
2009 vector<UVPtStruct> pointsLCt( nl ), pointsRCt( nl );
2010 pointsLCt[0] = pTBL;
2011 pointsLCt.back() = uv_et[ lw ];
2012 pointsRCt[0] = pTBR;
2013 pointsRCt.back() = uv_et[ lw + nb - 1 ];
2015 p0 = quad->side[0].Value2d( x );
2016 p2 = uv_et[ lw ].UV();
2017 int iR = lw + nb - 1;
2019 gp_UV p0R = quad->side[0].Value2d( xR );
2020 gp_UV p2R = uv_et[ iR ].UV();
2021 for ( int i = 1; i < nl-1; ++i )
2023 y = yCbL + ( 1. - yCbL ) * i / (nl-1.);
2024 p1 = quad->side[1].Value2d( y );
2025 p3 = quad->side[3].Value2d( y );
2026 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
2027 pointsLCt[ i ].u = uv.X();
2028 pointsLCt[ i ].v = uv.Y();
2030 y = yCbR + ( 1. - yCbR ) * i / (nl-1.);
2031 p1 = quad->side[1].Value2d( y );
2032 p3 = quad->side[3].Value2d( y );
2033 uv = calcUV( xR,y, a0,a1,a2,a3, p0R,p1,p2R,p3 );
2034 pointsRCt[ i ].u = uv.X();
2035 pointsRCt[ i ].v = uv.Y();
2037 sideLCt = StdMeshers_FaceSide::New( pointsLCt, aFace );
2038 sideRCt = StdMeshers_FaceSide::New( pointsRCt, aFace );
2040 // Make a side separating domains Cb and Ct
2041 StdMeshers_FaceSidePtr sideCbCt;
2043 vector<UVPtStruct> pointsCbCt( nb );
2044 pointsCbCt[0] = pTBL;
2045 pointsCbCt.back() = pTBR;
2046 p1 = quad->side[1].Value2d( yCbR );
2047 p3 = quad->side[3].Value2d( yCbL );
2048 for ( int i = 1; i < nb-1; ++i )
2050 x = quad->side[2].Param( i + lw );
2051 y = yCbL + ( yCbR - yCbL ) * i / (nb-1.);
2052 p2 = uv_et[ i + lw ].UV();
2053 p0 = quad->side[0].Value2d( x );
2054 uv = calcUV( x,y, a0,a1,a2,a3, p0,p1,p2,p3 );
2055 pointsCbCt[ i ].u = uv.X();
2056 pointsCbCt[ i ].v = uv.Y();
2058 sideCbCt = StdMeshers_FaceSide::New( pointsCbCt, aFace );
2061 FaceQuadStruct* qCb = new FaceQuadStruct( quad->face, "Cb" );
2062 myQuadList.push_back( FaceQuadStruct::Ptr( qCb ));
2063 qCb->side.resize(4);
2064 qCb->side[0] = quad->side[0];
2065 qCb->side[1] = sideRCb;
2066 qCb->side[2] = sideCbCt;
2067 qCb->side[3] = sideLCb;
2069 FaceQuadStruct* qL = new FaceQuadStruct( quad->face, "L" );
2070 myQuadList.push_back( FaceQuadStruct::Ptr( qL ));
2072 qL->side[0] = sideLCb;
2073 qL->side[1] = sideLCt;
2074 qL->side[2] = quad->side[2];
2075 qL->side[3] = quad->side[3];
2076 qL->side[2].to = ( lw + 1 ) * dt + tfrom;
2078 FaceQuadStruct* qR = new FaceQuadStruct( quad->face, "R" );
2079 myQuadList.push_back( FaceQuadStruct::Ptr( qR ));
2081 qR->side[0] = sideRCb;
2082 qR->side[0].from = lw;
2083 qR->side[0].to = -1;
2084 qR->side[0].di = -1;
2085 qR->side[1] = quad->side[1];
2086 qR->side[2] = quad->side[2];
2087 qR->side[2].from = ( lw + nb-1 ) * dt + tfrom;
2088 qR->side[3] = sideRCt;
2089 // Make Ct from the main quad
2090 FaceQuadStruct::Ptr qCt = quad;
2091 qCt->side[0] = sideCbCt;
2092 qCt->side[1] = sideRCt;
2093 qCt->side[2].from = ( lw ) * dt + tfrom;
2094 qCt->side[2].to = ( lw + nb ) * dt + tfrom;
2095 qCt->side[3] = sideLCt;
2096 qCt->uv_grid.clear();
2100 qCb->side[3].AddContact( lw, & qCb->side[2], 0 );
2101 qCb->side[3].AddContact( lw, & qCt->side[3], 0 );
2102 qCt->side[3].AddContact( 0, & qCt->side[0], 0 );
2103 qCt->side[0].AddContact( 0, & qL ->side[0], lw );
2104 qL ->side[0].AddContact( lw, & qL ->side[1], 0 );
2105 qL ->side[0].AddContact( lw, & qCb->side[2], 0 );
2107 qCb->side[1].AddContact( lw, & qCb->side[2], nb-1 );
2108 qCb->side[1].AddContact( lw, & qCt->side[1], 0 );
2109 qCt->side[0].AddContact( nb-1, & qCt->side[1], 0 );
2110 qCt->side[0].AddContact( nb-1, & qR ->side[0], lw );
2111 qR ->side[3].AddContact( 0, & qR ->side[0], lw );
2112 qR ->side[3].AddContact( 0, & qCb->side[2], nb-1 );
2114 return computeQuadDominant( aMesh, aFace );
2116 } // if ( !myForcedPnts.empty() )
2127 // arrays for normalized params
2128 TColStd_SequenceOfReal npb, npr, npt, npl;
2129 for (i=0; i<nb; i++) {
2130 npb.Append(uv_eb[i].normParam);
2132 for (i=0; i<nr; i++) {
2133 npr.Append(uv_er[i].normParam);
2135 for (i=0; i<nt; i++) {
2136 npt.Append(uv_et[i].normParam);
2138 for (i=0; i<nl; i++) {
2139 npl.Append(uv_el[i].normParam);
2144 // add some params to right and left after the first param
2147 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
2148 for (i=1; i<=dr; i++) {
2149 npr.InsertAfter(1,npr.Value(2)-dpr);
2153 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
2154 for (i=1; i<=dl; i++) {
2155 npl.InsertAfter(1,npl.Value(2)-dpr);
2159 int nnn = Min(nr,nl);
2160 // auxiliary sequence of XY for creation nodes
2161 // in the bottom part of central domain
2162 // Length of UVL and UVR must be == nbv-nnn
2163 TColgp_SequenceOfXY UVL, UVR, UVT;
2166 // step1: create faces for left domain
2167 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
2169 for (j=1; j<=nl; j++)
2170 NodesL.SetValue(1,j,uv_el[j-1].node);
2173 for (i=1; i<=dl; i++)
2174 NodesL.SetValue(i+1,nl,uv_et[i].node);
2175 // create and add needed nodes
2176 TColgp_SequenceOfXY UVtmp;
2177 for (i=1; i<=dl; i++) {
2178 double x0 = npt.Value(i+1);
2181 double y0 = npl.Value(i+1);
2182 double y1 = npr.Value(i+1);
2183 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2184 gp_Pnt P = S->Value(UV.X(),UV.Y());
2185 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2186 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2187 NodesL.SetValue(i+1,1,N);
2188 if (UVL.Length()<nbv-nnn) UVL.Append(UV);
2190 for (j=2; j<nl; j++) {
2191 double y0 = npl.Value(dl+j);
2192 double y1 = npr.Value(dl+j);
2193 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2194 gp_Pnt P = S->Value(UV.X(),UV.Y());
2195 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2196 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2197 NodesL.SetValue(i+1,j,N);
2198 if (i==dl) UVtmp.Append(UV);
2201 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn; i++) {
2202 UVL.Append(UVtmp.Value(i));
2205 for (i=1; i<=dl; i++) {
2206 for (j=1; j<nl; j++) {
2208 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
2209 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
2215 // fill UVL using c2d
2216 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn; i++) {
2217 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
2221 // step2: create faces for right domain
2222 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
2224 for (j=1; j<=nr; j++)
2225 NodesR.SetValue(1,j,uv_er[nr-j].node);
2228 for (i=1; i<=dr; i++)
2229 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
2230 // create and add needed nodes
2231 TColgp_SequenceOfXY UVtmp;
2232 for (i=1; i<=dr; i++) {
2233 double x0 = npt.Value(nt-i);
2236 double y0 = npl.Value(i+1);
2237 double y1 = npr.Value(i+1);
2238 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2239 gp_Pnt P = S->Value(UV.X(),UV.Y());
2240 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2241 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2242 NodesR.SetValue(i+1,nr,N);
2243 if (UVR.Length()<nbv-nnn) UVR.Append(UV);
2245 for (j=2; j<nr; j++) {
2246 double y0 = npl.Value(nbv-j+1);
2247 double y1 = npr.Value(nbv-j+1);
2248 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2249 gp_Pnt P = S->Value(UV.X(),UV.Y());
2250 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2251 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2252 NodesR.SetValue(i+1,j,N);
2253 if (i==dr) UVtmp.Prepend(UV);
2256 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn; i++) {
2257 UVR.Append(UVtmp.Value(i));
2260 for (i=1; i<=dr; i++) {
2261 for (j=1; j<nr; j++) {
2263 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
2264 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
2270 // fill UVR using c2d
2271 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn; i++) {
2272 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
2276 // step3: create faces for central domain
2277 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
2278 // add first line using NodesL
2279 for (i=1; i<=dl+1; i++)
2280 NodesC.SetValue(1,i,NodesL(i,1));
2281 for (i=2; i<=nl; i++)
2282 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
2283 // add last line using NodesR
2284 for (i=1; i<=dr+1; i++)
2285 NodesC.SetValue(nb,i,NodesR(i,nr));
2286 for (i=1; i<nr; i++)
2287 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
2288 // add top nodes (last columns)
2289 for (i=dl+2; i<nbh-dr; i++)
2290 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
2291 // add bottom nodes (first columns)
2292 for (i=2; i<nb; i++)
2293 NodesC.SetValue(i,1,uv_eb[i-1].node);
2295 // create and add needed nodes
2296 // add linear layers
2297 for (i=2; i<nb; i++) {
2298 double x0 = npt.Value(dl+i);
2300 for (j=1; j<nnn; j++) {
2301 double y0 = npl.Value(nbv-nnn+j);
2302 double y1 = npr.Value(nbv-nnn+j);
2303 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
2304 gp_Pnt P = S->Value(UV.X(),UV.Y());
2305 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2306 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2307 NodesC.SetValue(i,nbv-nnn+j,N);
2312 // add diagonal layers
2313 gp_UV A2 = UVR.Value(nbv-nnn);
2314 gp_UV A3 = UVL.Value(nbv-nnn);
2315 for (i=1; i<nbv-nnn; i++) {
2316 gp_UV p1 = UVR.Value(i);
2317 gp_UV p3 = UVL.Value(i);
2318 double y = i / double(nbv-nnn);
2319 for (j=2; j<nb; j++) {
2320 double x = npb.Value(j);
2321 gp_UV p0( uv_eb[j-1].u, uv_eb[j-1].v );
2322 gp_UV p2 = UVT.Value( j-1 );
2323 gp_UV UV = calcUV(x, y, a0, a1, A2, A3, p0,p1,p2,p3 );
2324 gp_Pnt P = S->Value(UV.X(),UV.Y());
2325 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2326 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2327 NodesC.SetValue(j,i+1,N);
2331 for (i=1; i<nb; i++) {
2332 for (j=1; j<nbv; j++) {
2334 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2335 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2341 else { // New version (!OldVersion)
2342 // step1: create faces for bottom rectangle domain
2343 StdMeshers_Array2OfNode NodesBRD(1,nb,1,nnn-1);
2344 // fill UVL and UVR using c2d
2345 for (j=0; j<nb; j++) {
2346 NodesBRD.SetValue(j+1,1,uv_eb[j].node);
2348 for (i=1; i<nnn-1; i++) {
2349 NodesBRD.SetValue(1,i+1,uv_el[i].node);
2350 NodesBRD.SetValue(nb,i+1,uv_er[i].node);
2351 for (j=2; j<nb; j++) {
2352 double x = npb.Value(j);
2353 double y = (1-x) * npl.Value(i+1) + x * npr.Value(i+1);
2354 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2355 gp_Pnt P = S->Value(UV.X(),UV.Y());
2356 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2357 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(),UV.Y());
2358 NodesBRD.SetValue(j,i+1,N);
2361 for (j=1; j<nnn-1; j++) {
2362 for (i=1; i<nb; i++) {
2364 myHelper->AddFace(NodesBRD.Value(i,j), NodesBRD.Value(i+1,j),
2365 NodesBRD.Value(i+1,j+1), NodesBRD.Value(i,j+1));
2369 int drl = abs(nr-nl);
2370 // create faces for region C
2371 StdMeshers_Array2OfNode NodesC(1,nb,1,drl+1+addv);
2372 // add nodes from previous region
2373 for (j=1; j<=nb; j++) {
2374 NodesC.SetValue(j,1,NodesBRD.Value(j,nnn-1));
2376 if ((drl+addv) > 0) {
2381 TColgp_SequenceOfXY UVtmp;
2382 double drparam = npr.Value(nr) - npr.Value(nnn-1);
2383 double dlparam = npl.Value(nnn) - npl.Value(nnn-1);
2384 double y0 = 0, y1 = 0;
2385 for (i=1; i<=drl; i++) {
2386 // add existed nodes from right edge
2387 NodesC.SetValue(nb,i+1,uv_er[nnn+i-2].node);
2388 //double dtparam = npt.Value(i+1);
2389 y1 = npr.Value(nnn+i-1); // param on right edge
2390 double dpar = (y1 - npr.Value(nnn-1))/drparam;
2391 y0 = npl.Value(nnn-1) + dpar*dlparam; // param on left edge
2392 double dy = y1 - y0;
2393 for (j=1; j<nb; j++) {
2394 double x = npt.Value(i+1) + npb.Value(j)*(1-npt.Value(i+1));
2395 double y = y0 + dy*x;
2396 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2397 gp_Pnt P = S->Value(UV.X(),UV.Y());
2398 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2399 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2400 NodesC.SetValue(j,i+1,N);
2403 double dy0 = (1-y0)/(addv+1);
2404 double dy1 = (1-y1)/(addv+1);
2405 for (i=1; i<=addv; i++) {
2406 double yy0 = y0 + dy0*i;
2407 double yy1 = y1 + dy1*i;
2408 double dyy = yy1 - yy0;
2409 for (j=1; j<=nb; j++) {
2410 double x = npt.Value(i+1+drl) +
2411 npb.Value(j) * (npt.Value(nt-i) - npt.Value(i+1+drl));
2412 double y = yy0 + dyy*x;
2413 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2414 gp_Pnt P = S->Value(UV.X(),UV.Y());
2415 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2416 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2417 NodesC.SetValue(j,i+drl+1,N);
2424 TColgp_SequenceOfXY UVtmp;
2425 double dlparam = npl.Value(nl) - npl.Value(nnn-1);
2426 double drparam = npr.Value(nnn) - npr.Value(nnn-1);
2427 double y0 = npl.Value(nnn-1);
2428 double y1 = npr.Value(nnn-1);
2429 for (i=1; i<=drl; i++) {
2430 // add existed nodes from right edge
2431 NodesC.SetValue(1,i+1,uv_el[nnn+i-2].node);
2432 y0 = npl.Value(nnn+i-1); // param on left edge
2433 double dpar = (y0 - npl.Value(nnn-1))/dlparam;
2434 y1 = npr.Value(nnn-1) + dpar*drparam; // param on right edge
2435 double dy = y1 - y0;
2436 for (j=2; j<=nb; j++) {
2437 double x = npb.Value(j)*npt.Value(nt-i);
2438 double y = y0 + dy*x;
2439 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2440 gp_Pnt P = S->Value(UV.X(),UV.Y());
2441 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2442 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2443 NodesC.SetValue(j,i+1,N);
2446 double dy0 = (1-y0)/(addv+1);
2447 double dy1 = (1-y1)/(addv+1);
2448 for (i=1; i<=addv; i++) {
2449 double yy0 = y0 + dy0*i;
2450 double yy1 = y1 + dy1*i;
2451 double dyy = yy1 - yy0;
2452 for (j=1; j<=nb; j++) {
2453 double x = npt.Value(i+1) +
2454 npb.Value(j) * (npt.Value(nt-i-drl) - npt.Value(i+1));
2455 double y = yy0 + dyy*x;
2456 gp_UV UV = calcUV2(x, y, quad, a0, a1, a2, a3);
2457 gp_Pnt P = S->Value(UV.X(),UV.Y());
2458 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
2459 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
2460 NodesC.SetValue(j,i+drl+1,N);
2465 for (j=1; j<=drl+addv; j++) {
2466 for (i=1; i<nb; i++) {
2468 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
2469 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
2474 StdMeshers_Array2OfNode NodesLast(1,nt,1,2);
2475 for (i=1; i<=nt; i++) {
2476 NodesLast.SetValue(i,2,uv_et[i-1].node);
2479 for (i=n1; i<drl+addv+1; i++) {
2481 NodesLast.SetValue(nnn,1,NodesC.Value(1,i));
2483 for (i=1; i<=nb; i++) {
2485 NodesLast.SetValue(nnn,1,NodesC.Value(i,drl+addv+1));
2487 for (i=drl+addv; i>=n2; i--) {
2489 NodesLast.SetValue(nnn,1,NodesC.Value(nb,i));
2491 for (i=1; i<nt; i++) {
2493 myHelper->AddFace(NodesLast.Value(i,1), NodesLast.Value(i+1,1),
2494 NodesLast.Value(i+1,2), NodesLast.Value(i,2));
2497 } // if ((drl+addv) > 0)
2499 } // end new version implementation
2506 //=======================================================================
2508 * Evaluate only quandrangle faces
2510 //=======================================================================
2512 bool StdMeshers_Quadrangle_2D::evaluateQuadPref(SMESH_Mesh & aMesh,
2513 const TopoDS_Shape& aShape,
2514 std::vector<int>& aNbNodes,
2515 MapShapeNbElems& aResMap,
2518 // Auxiliary key in order to keep old variant
2519 // of meshing after implementation new variant
2520 // for bug 0016220 from Mantis.
2521 bool OldVersion = false;
2522 if (myQuadType == QUAD_QUADRANGLE_PREF_REVERSED)
2525 const TopoDS_Face& F = TopoDS::Face(aShape);
2526 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
2528 int nb = aNbNodes[0];
2529 int nr = aNbNodes[1];
2530 int nt = aNbNodes[2];
2531 int nl = aNbNodes[3];
2532 int dh = abs(nb-nt);
2533 int dv = abs(nr-nl);
2537 // it is a base case => not shift
2540 // we have to shift on 2
2549 // we have to shift quad on 1
2556 // we have to shift quad on 3
2566 int nbh = Max(nb,nt);
2567 int nbv = Max(nr,nl);
2582 // add some params to right and left after the first param
2589 int nnn = Min(nr,nl);
2594 // step1: create faces for left domain
2596 nbNodes += dl*(nl-1);
2597 nbFaces += dl*(nl-1);
2599 // step2: create faces for right domain
2601 nbNodes += dr*(nr-1);
2602 nbFaces += dr*(nr-1);
2604 // step3: create faces for central domain
2605 nbNodes += (nb-2)*(nnn-1) + (nbv-nnn-1)*(nb-2);
2606 nbFaces += (nb-1)*(nbv-1);
2608 else { // New version (!OldVersion)
2609 nbNodes += (nnn-2)*(nb-2);
2610 nbFaces += (nnn-2)*(nb-1);
2611 int drl = abs(nr-nl);
2612 nbNodes += drl*(nb-1) + addv*nb;
2613 nbFaces += (drl+addv)*(nb-1) + (nt-1);
2614 } // end new version implementation
2616 std::vector<int> aVec(SMDSEntity_Last);
2617 for (int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
2619 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces;
2620 aVec[SMDSEntity_Node] = nbNodes + nbFaces*4;
2621 if (aNbNodes.size()==5) {
2622 aVec[SMDSEntity_Quad_Triangle] = aNbNodes[3] - 1;
2623 aVec[SMDSEntity_Quad_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2627 aVec[SMDSEntity_Node] = nbNodes;
2628 aVec[SMDSEntity_Quadrangle] = nbFaces;
2629 if (aNbNodes.size()==5) {
2630 aVec[SMDSEntity_Triangle] = aNbNodes[3] - 1;
2631 aVec[SMDSEntity_Quadrangle] = nbFaces - aNbNodes[3] + 1;
2634 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
2635 aResMap.insert(std::make_pair(sm,aVec));
2640 //=============================================================================
2641 /*! Split quadrangle in to 2 triangles by smallest diagonal
2644 //=============================================================================
2646 void StdMeshers_Quadrangle_2D::splitQuadFace(SMESHDS_Mesh * theMeshDS,
2648 const SMDS_MeshNode* theNode1,
2649 const SMDS_MeshNode* theNode2,
2650 const SMDS_MeshNode* theNode3,
2651 const SMDS_MeshNode* theNode4)
2653 if ( SMESH_TNodeXYZ( theNode1 ).SquareDistance( theNode3 ) >
2654 SMESH_TNodeXYZ( theNode2 ).SquareDistance( theNode4 ) )
2656 myHelper->AddFace(theNode2, theNode4 , theNode1);
2657 myHelper->AddFace(theNode2, theNode3, theNode4);
2661 myHelper->AddFace(theNode1, theNode2 ,theNode3);
2662 myHelper->AddFace(theNode1, theNode3, theNode4);
2668 enum uvPos { UV_A0, UV_A1, UV_A2, UV_A3, UV_B, UV_R, UV_T, UV_L, UV_SIZE };
2670 inline SMDS_MeshNode* makeNode( UVPtStruct & uvPt,
2672 FaceQuadStruct::Ptr& quad,
2674 SMESH_MesherHelper* helper,
2675 Handle(Geom_Surface) S)
2677 const vector<UVPtStruct>& uv_eb = quad->side[QUAD_BOTTOM_SIDE].GetUVPtStruct();
2678 const vector<UVPtStruct>& uv_et = quad->side[QUAD_TOP_SIDE ].GetUVPtStruct();
2679 double rBot = ( uv_eb.size() - 1 ) * uvPt.normParam;
2680 double rTop = ( uv_et.size() - 1 ) * uvPt.normParam;
2681 int iBot = int( rBot );
2682 int iTop = int( rTop );
2683 double xBot = uv_eb[ iBot ].normParam + ( rBot - iBot ) * ( uv_eb[ iBot+1 ].normParam - uv_eb[ iBot ].normParam );
2684 double xTop = uv_et[ iTop ].normParam + ( rTop - iTop ) * ( uv_et[ iTop+1 ].normParam - uv_et[ iTop ].normParam );
2685 double x = xBot + y * ( xTop - xBot );
2687 gp_UV uv = calcUV(/*x,y=*/x, y,
2688 /*a0,...=*/UVs[UV_A0], UVs[UV_A1], UVs[UV_A2], UVs[UV_A3],
2689 /*p0=*/quad->side[QUAD_BOTTOM_SIDE].grid->Value2d( x ).XY(),
2691 /*p2=*/quad->side[QUAD_TOP_SIDE ].grid->Value2d( x ).XY(),
2692 /*p3=*/UVs[ UV_L ]);
2693 gp_Pnt P = S->Value( uv.X(), uv.Y() );
2696 return helper->AddNode(P.X(), P.Y(), P.Z(), 0, uv.X(), uv.Y() );
2699 void reduce42( const vector<UVPtStruct>& curr_base,
2700 vector<UVPtStruct>& next_base,
2702 int & next_base_len,
2703 FaceQuadStruct::Ptr& quad,
2706 SMESH_MesherHelper* helper,
2707 Handle(Geom_Surface)& S)
2709 // add one "HH": nodes a,b,c,d,e and faces 1,2,3,4,5,6
2711 // .-----a-----b i + 1
2722 const SMDS_MeshNode*& Na = next_base[ ++next_base_len ].node;
2724 Na = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2727 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2729 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2732 double u = (curr_base[j + 2].u + next_base[next_base_len - 2].u) / 2.0;
2733 double v = (curr_base[j + 2].v + next_base[next_base_len - 2].v) / 2.0;
2734 gp_Pnt P = S->Value(u,v);
2735 SMDS_MeshNode* Nc = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2738 u = (curr_base[j + 2].u + next_base[next_base_len - 1].u) / 2.0;
2739 v = (curr_base[j + 2].v + next_base[next_base_len - 1].v) / 2.0;
2741 SMDS_MeshNode* Nd = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2744 u = (curr_base[j + 2].u + next_base[next_base_len].u) / 2.0;
2745 v = (curr_base[j + 2].v + next_base[next_base_len].v) / 2.0;
2747 SMDS_MeshNode* Ne = helper->AddNode(P.X(), P.Y(), P.Z(), 0, u, v);
2750 helper->AddFace(curr_base[j + 0].node,
2751 curr_base[j + 1].node, Nc,
2752 next_base[next_base_len - 2].node);
2754 helper->AddFace(curr_base[j + 1].node,
2755 curr_base[j + 2].node, Nd, Nc);
2757 helper->AddFace(curr_base[j + 2].node,
2758 curr_base[j + 3].node, Ne, Nd);
2760 helper->AddFace(curr_base[j + 3].node,
2761 curr_base[j + 4].node, Nb, Ne);
2763 helper->AddFace(Nc, Nd, Na, next_base[next_base_len - 2].node);
2765 helper->AddFace(Nd, Ne, Nb, Na);
2768 void reduce31( const vector<UVPtStruct>& curr_base,
2769 vector<UVPtStruct>& next_base,
2771 int & next_base_len,
2772 FaceQuadStruct::Ptr& quad,
2775 SMESH_MesherHelper* helper,
2776 Handle(Geom_Surface)& S)
2778 // add one "H": nodes b,c,e and faces 1,2,4,5
2780 // .---------b i + 1
2791 const SMDS_MeshNode*& Nb = next_base[ ++next_base_len ].node;
2793 Nb = makeNode( next_base[ next_base_len ], y, quad, UVs, helper, S );
2796 double u1 = (curr_base[ j ].u + next_base[ next_base_len-1 ].u ) / 2.0;
2797 double u2 = (curr_base[ j+3 ].u + next_base[ next_base_len ].u ) / 2.0;
2798 double u3 = (u2 - u1) / 3.0;
2800 double v1 = (curr_base[ j ].v + next_base[ next_base_len-1 ].v ) / 2.0;
2801 double v2 = (curr_base[ j+3 ].v + next_base[ next_base_len ].v ) / 2.0;
2802 double v3 = (v2 - v1) / 3.0;
2806 gp_Pnt P = S->Value(u,v);
2807 SMDS_MeshNode* Nc = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2812 SMDS_MeshNode* Ne = helper->AddNode( P.X(), P.Y(), P.Z(), 0, u, v );
2816 helper->AddFace( curr_base[ j + 0 ].node,
2817 curr_base[ j + 1 ].node,
2819 next_base[ next_base_len - 1 ].node);
2821 helper->AddFace( curr_base[ j + 1 ].node,
2822 curr_base[ j + 2 ].node, Ne, Nc);
2824 helper->AddFace( curr_base[ j + 2 ].node,
2825 curr_base[ j + 3 ].node, Nb, Ne);
2827 helper->AddFace(Nc, Ne, Nb,
2828 next_base[ next_base_len - 1 ].node);
2831 typedef void (* PReduceFunction) ( const vector<UVPtStruct>& curr_base,
2832 vector<UVPtStruct>& next_base,
2834 int & next_base_len,
2835 FaceQuadStruct::Ptr & quad,
2838 SMESH_MesherHelper* helper,
2839 Handle(Geom_Surface)& S);
2843 //=======================================================================
2845 * Implementation of Reduced algorithm (meshing with quadrangles only)
2847 //=======================================================================
2849 bool StdMeshers_Quadrangle_2D::computeReduced (SMESH_Mesh & aMesh,
2850 const TopoDS_Face& aFace,
2851 FaceQuadStruct::Ptr quad)
2853 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
2854 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
2855 int i,j,geomFaceID = meshDS->ShapeToIndex(aFace);
2857 int nb = quad->side[0].NbPoints(); // bottom
2858 int nr = quad->side[1].NbPoints(); // right
2859 int nt = quad->side[2].NbPoints(); // top
2860 int nl = quad->side[3].NbPoints(); // left
2862 // Simple Reduce 10->8->6->4 (3 steps) Multiple Reduce 10->4 (1 step)
2864 // .-----.-----.-----.-----. .-----.-----.-----.-----.
2865 // | / \ | / \ | | / \ | / \ |
2866 // | / .--.--. \ | | / \ | / \ |
2867 // | / / | \ \ | | / .----.----. \ |
2868 // .---.---.---.---.---.---. | / / \ | / \ \ |
2869 // | / / \ | / \ \ | | / / \ | / \ \ |
2870 // | / / .-.-. \ \ | | / / .---.---. \ \ |
2871 // | / / / | \ \ \ | | / / / \ | / \ \ \ |
2872 // .--.--.--.--.--.--.--.--. | / / / \ | / \ \ \ |
2873 // | / / / \ | / \ \ \ | | / / / .-.-. \ \ \ |
2874 // | / / / .-.-. \ \ \ | | / / / / | \ \ \ \ |
2875 // | / / / / | \ \ \ \ | | / / / / | \ \ \ \ |
2876 // .-.-.-.--.--.--.--.-.-.-. .-.-.-.--.--.--.--.-.-.-.
2878 bool MultipleReduce = false;
2890 else if (nb == nt) {
2891 nr1 = nb; // and == nt
2905 // number of rows and columns
2906 int nrows = nr1 - 1;
2907 int ncol_top = nt1 - 1;
2908 int ncol_bot = nb1 - 1;
2909 // number of rows needed to reduce ncol_bot to ncol_top using simple 3->1 "tree" (see below)
2911 int( ceil( log( double(ncol_bot) / ncol_top) / log( 3.))); // = log x base 3
2912 if ( nrows < nrows_tree31 )
2914 MultipleReduce = true;
2915 error( COMPERR_WARNING,
2916 SMESH_Comment("To use 'Reduced' transition, "
2917 "number of face rows should be at least ")
2918 << nrows_tree31 << ". Actual number of face rows is " << nrows << ". "
2919 "'Quadrangle preference (reversed)' transion has been used.");
2923 if (MultipleReduce) { // == computeQuadPref QUAD_QUADRANGLE_PREF_REVERSED
2924 //==================================================
2925 int dh = abs(nb-nt);
2926 int dv = abs(nr-nl);
2930 // it is a base case => not shift quad but may be replacement is need
2934 // we have to shift quad on 2
2940 // we have to shift quad on 1
2944 // we have to shift quad on 3
2949 nb = quad->side[0].NbPoints();
2950 nr = quad->side[1].NbPoints();
2951 nt = quad->side[2].NbPoints();
2952 nl = quad->side[3].NbPoints();
2955 int nbh = Max(nb,nt);
2956 int nbv = Max(nr,nl);
2969 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
2970 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
2971 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
2972 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
2974 if ((int) uv_eb.size() != nb || (int) uv_er.size() != nr ||
2975 (int) uv_et.size() != nt || (int) uv_el.size() != nl)
2976 return error(COMPERR_BAD_INPUT_MESH);
2978 // arrays for normalized params
2979 TColStd_SequenceOfReal npb, npr, npt, npl;
2980 for (j = 0; j < nb; j++) {
2981 npb.Append(uv_eb[j].normParam);
2983 for (i = 0; i < nr; i++) {
2984 npr.Append(uv_er[i].normParam);
2986 for (j = 0; j < nt; j++) {
2987 npt.Append(uv_et[j].normParam);
2989 for (i = 0; i < nl; i++) {
2990 npl.Append(uv_el[i].normParam);
2994 // orientation of face and 3 main domain for future faces
3000 // left | | | | right
3007 // add some params to right and left after the first param
3010 double dpr = (npr.Value(2) - npr.Value(1))/(dr+1);
3011 for (i=1; i<=dr; i++) {
3012 npr.InsertAfter(1,npr.Value(2)-dpr);
3016 dpr = (npl.Value(2) - npl.Value(1))/(dl+1);
3017 for (i=1; i<=dl; i++) {
3018 npl.InsertAfter(1,npl.Value(2)-dpr);
3021 gp_XY a0 (uv_eb.front().u, uv_eb.front().v);
3022 gp_XY a1 (uv_eb.back().u, uv_eb.back().v);
3023 gp_XY a2 (uv_et.back().u, uv_et.back().v);
3024 gp_XY a3 (uv_et.front().u, uv_et.front().v);
3026 int nnn = Min(nr,nl);
3027 // auxiliary sequence of XY for creation of nodes
3028 // in the bottom part of central domain
3029 // it's length must be == nbv-nnn-1
3030 TColgp_SequenceOfXY UVL;
3031 TColgp_SequenceOfXY UVR;
3032 //==================================================
3034 // step1: create faces for left domain
3035 StdMeshers_Array2OfNode NodesL(1,dl+1,1,nl);
3037 for (j=1; j<=nl; j++)
3038 NodesL.SetValue(1,j,uv_el[j-1].node);
3041 for (i=1; i<=dl; i++)
3042 NodesL.SetValue(i+1,nl,uv_et[i].node);
3043 // create and add needed nodes
3044 TColgp_SequenceOfXY UVtmp;
3045 for (i=1; i<=dl; i++) {
3046 double x0 = npt.Value(i+1);
3049 double y0 = npl.Value(i+1);
3050 double y1 = npr.Value(i+1);
3051 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3052 gp_Pnt P = S->Value(UV.X(),UV.Y());
3053 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3054 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3055 NodesL.SetValue(i+1,1,N);
3056 if (UVL.Length()<nbv-nnn-1) UVL.Append(UV);
3058 for (j=2; j<nl; j++) {
3059 double y0 = npl.Value(dl+j);
3060 double y1 = npr.Value(dl+j);
3061 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3062 gp_Pnt P = S->Value(UV.X(),UV.Y());
3063 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3064 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3065 NodesL.SetValue(i+1,j,N);
3066 if (i==dl) UVtmp.Append(UV);
3069 for (i=1; i<=UVtmp.Length() && UVL.Length()<nbv-nnn-1; i++) {
3070 UVL.Append(UVtmp.Value(i));
3073 for (i=1; i<=dl; i++) {
3074 for (j=1; j<nl; j++) {
3075 myHelper->AddFace(NodesL.Value(i,j), NodesL.Value(i+1,j),
3076 NodesL.Value(i+1,j+1), NodesL.Value(i,j+1));
3081 // fill UVL using c2d
3082 for (i=1; i<npl.Length() && UVL.Length()<nbv-nnn-1; i++) {
3083 UVL.Append(gp_UV (uv_el[i].u, uv_el[i].v));
3087 // step2: create faces for right domain
3088 StdMeshers_Array2OfNode NodesR(1,dr+1,1,nr);
3090 for (j=1; j<=nr; j++)
3091 NodesR.SetValue(1,j,uv_er[nr-j].node);
3094 for (i=1; i<=dr; i++)
3095 NodesR.SetValue(i+1,1,uv_et[nt-1-i].node);
3096 // create and add needed nodes
3097 TColgp_SequenceOfXY UVtmp;
3098 for (i=1; i<=dr; i++) {
3099 double x0 = npt.Value(nt-i);
3102 double y0 = npl.Value(i+1);
3103 double y1 = npr.Value(i+1);
3104 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3105 gp_Pnt P = S->Value(UV.X(),UV.Y());
3106 SMDS_MeshNode * N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3107 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3108 NodesR.SetValue(i+1,nr,N);
3109 if (UVR.Length()<nbv-nnn-1) UVR.Append(UV);
3111 for (j=2; j<nr; j++) {
3112 double y0 = npl.Value(nbv-j+1);
3113 double y1 = npr.Value(nbv-j+1);
3114 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3115 gp_Pnt P = S->Value(UV.X(),UV.Y());
3116 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3117 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3118 NodesR.SetValue(i+1,j,N);
3119 if (i==dr) UVtmp.Prepend(UV);
3122 for (i=1; i<=UVtmp.Length() && UVR.Length()<nbv-nnn-1; i++) {
3123 UVR.Append(UVtmp.Value(i));
3126 for (i=1; i<=dr; i++) {
3127 for (j=1; j<nr; j++) {
3128 myHelper->AddFace(NodesR.Value(i,j), NodesR.Value(i+1,j),
3129 NodesR.Value(i+1,j+1), NodesR.Value(i,j+1));
3134 // fill UVR using c2d
3135 for (i=1; i<npr.Length() && UVR.Length()<nbv-nnn-1; i++) {
3136 UVR.Append(gp_UV(uv_er[i].u, uv_er[i].v));
3140 // step3: create faces for central domain
3141 StdMeshers_Array2OfNode NodesC(1,nb,1,nbv);
3142 // add first line using NodesL
3143 for (i=1; i<=dl+1; i++)
3144 NodesC.SetValue(1,i,NodesL(i,1));
3145 for (i=2; i<=nl; i++)
3146 NodesC.SetValue(1,dl+i,NodesL(dl+1,i));
3147 // add last line using NodesR
3148 for (i=1; i<=dr+1; i++)
3149 NodesC.SetValue(nb,i,NodesR(i,nr));
3150 for (i=1; i<nr; i++)
3151 NodesC.SetValue(nb,dr+i+1,NodesR(dr+1,nr-i));
3152 // add top nodes (last columns)
3153 for (i=dl+2; i<nbh-dr; i++)
3154 NodesC.SetValue(i-dl,nbv,uv_et[i-1].node);
3155 // add bottom nodes (first columns)
3156 for (i=2; i<nb; i++)
3157 NodesC.SetValue(i,1,uv_eb[i-1].node);
3159 // create and add needed nodes
3160 // add linear layers
3161 for (i=2; i<nb; i++) {
3162 double x0 = npt.Value(dl+i);
3164 for (j=1; j<nnn; j++) {
3165 double y0 = npl.Value(nbv-nnn+j);
3166 double y1 = npr.Value(nbv-nnn+j);
3167 gp_UV UV = calcUV(x0, x1, y0, y1, quad, a0, a1, a2, a3);
3168 gp_Pnt P = S->Value(UV.X(),UV.Y());
3169 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3170 meshDS->SetNodeOnFace(N, geomFaceID, UV.X(), UV.Y());
3171 NodesC.SetValue(i,nbv-nnn+j,N);
3174 // add diagonal layers
3175 for (i=1; i<nbv-nnn; i++) {
3176 double du = UVR.Value(i).X() - UVL.Value(i).X();
3177 double dv = UVR.Value(i).Y() - UVL.Value(i).Y();
3178 for (j=2; j<nb; j++) {
3179 double u = UVL.Value(i).X() + du*npb.Value(j);
3180 double v = UVL.Value(i).Y() + dv*npb.Value(j);
3181 gp_Pnt P = S->Value(u,v);
3182 SMDS_MeshNode* N = meshDS->AddNode(P.X(), P.Y(), P.Z());
3183 meshDS->SetNodeOnFace(N, geomFaceID, u, v);
3184 NodesC.SetValue(j,i+1,N);
3188 for (i=1; i<nb; i++) {
3189 for (j=1; j<nbv; j++) {
3190 myHelper->AddFace(NodesC.Value(i,j), NodesC.Value(i+1,j),
3191 NodesC.Value(i+1,j+1), NodesC.Value(i,j+1));
3194 } // end Multiple Reduce implementation
3195 else { // Simple Reduce (!MultipleReduce)
3196 //=========================================================
3199 // it is a base case => not shift quad
3200 //shiftQuad(quad,0,true);
3203 // we have to shift quad on 2
3209 // we have to shift quad on 1
3213 // we have to shift quad on 3
3218 nb = quad->side[0].NbPoints();
3219 nr = quad->side[1].NbPoints();
3220 nt = quad->side[2].NbPoints();
3221 nl = quad->side[3].NbPoints();
3223 // number of rows and columns
3224 int nrows = nr - 1; // and also == nl - 1
3225 int ncol_top = nt - 1;
3226 int ncol_bot = nb - 1;
3227 int npair_top = ncol_top / 2;
3228 // maximum number of bottom elements for "linear" simple reduce 4->2
3229 int max_lin42 = ncol_top + npair_top * 2 * nrows;
3230 // maximum number of bottom elements for "linear" simple reduce 3->1
3231 int max_lin31 = ncol_top + ncol_top * 2 * nrows;
3232 // maximum number of bottom elements for "tree" simple reduce 4->2
3234 // number of rows needed to reduce ncol_bot to ncol_top using simple 4->2 "tree"
3235 int nrows_tree42 = int( log( (double)(ncol_bot / ncol_top) )/log((double)2) ); // needed to avoid overflow at pow(2) while computing max_tree42
3236 if (nrows_tree42 < nrows) {
3237 max_tree42 = npair_top * pow(2.0, nrows + 1);
3238 if ( ncol_top > npair_top * 2 ) {
3239 int delta = ncol_bot - max_tree42;
3240 for (int irow = 1; irow < nrows; irow++) {
3241 int nfour = delta / 4;
3244 if (delta <= (ncol_top - npair_top * 2))
3245 max_tree42 = ncol_bot;
3248 // maximum number of bottom elements for "tree" simple reduce 3->1
3249 //int max_tree31 = ncol_top * pow(3.0, nrows);
3250 bool is_lin_31 = false;
3251 bool is_lin_42 = false;
3252 bool is_tree_31 = false;
3253 bool is_tree_42 = false;
3254 int max_lin = max_lin42;
3255 if (ncol_bot > max_lin42) {
3256 if (ncol_bot <= max_lin31) {
3258 max_lin = max_lin31;
3262 // if ncol_bot is a 3*n or not 2*n
3263 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3265 max_lin = max_lin31;
3271 if (ncol_bot > max_lin) { // not "linear"
3272 is_tree_31 = (ncol_bot > max_tree42);
3273 if (ncol_bot <= max_tree42) {
3274 if ((ncol_bot/3)*3 == ncol_bot || (ncol_bot/2)*2 != ncol_bot) {
3283 const vector<UVPtStruct>& uv_eb = quad->side[0].GetUVPtStruct(true,0);
3284 const vector<UVPtStruct>& uv_er = quad->side[1].GetUVPtStruct(false,1);
3285 const vector<UVPtStruct>& uv_et = quad->side[2].GetUVPtStruct(true,1);
3286 const vector<UVPtStruct>& uv_el = quad->side[3].GetUVPtStruct(false,0);
3288 if ((int) uv_eb.size() != nb || (int) uv_er.size() != nr ||
3289 (int) uv_et.size() != nt || (int) uv_el.size() != nl)
3290 return error(COMPERR_BAD_INPUT_MESH);
3292 gp_UV uv[ UV_SIZE ];
3293 uv[ UV_A0 ].SetCoord( uv_eb.front().u, uv_eb.front().v);
3294 uv[ UV_A1 ].SetCoord( uv_eb.back().u, uv_eb.back().v );
3295 uv[ UV_A2 ].SetCoord( uv_et.back().u, uv_et.back().v );
3296 uv[ UV_A3 ].SetCoord( uv_et.front().u, uv_et.front().v);
3298 vector<UVPtStruct> curr_base = uv_eb, next_base;
3300 UVPtStruct nullUVPtStruct;
3301 nullUVPtStruct.node = 0;
3302 nullUVPtStruct.x = nullUVPtStruct.y = nullUVPtStruct.u = nullUVPtStruct.v = 0;
3303 nullUVPtStruct.param = 0;
3306 int curr_base_len = nb;
3307 int next_base_len = 0;
3310 { // ------------------------------------------------------------------
3311 // New algorithm implemented by request of IPAL22856
3312 // "2D quadrangle mesher of reduced type works wrong"
3313 // http://bugtracker.opencascade.com/show_bug.cgi?id=22856
3315 // the algorithm is following: all reduces are centred in horizontal
3316 // direction and are distributed among all rows
3318 if (ncol_bot > max_tree42) {
3322 if ((ncol_top/3)*3 == ncol_top ) {
3330 const int col_top_size = is_lin_42 ? 2 : 1;
3331 const int col_base_size = is_lin_42 ? 4 : 3;
3333 // Compute nb of "columns" (like in "linear" simple reducing) in all rows
3335 vector<int> nb_col_by_row;
3337 int delta_all = nb - nt;
3338 int delta_one_col = nrows * 2;
3339 int nb_col = delta_all / delta_one_col;
3340 int remainder = delta_all - nb_col * delta_one_col;
3341 if (remainder > 0) {
3344 if ( nb_col * col_top_size >= nt ) // == "tree" reducing situation
3346 // top row is full (all elements reduced), add "columns" one by one
3347 // in rows below until all bottom elements are reduced
3348 nb_col = ( nt - 1 ) / col_top_size;
3349 nb_col_by_row.resize( nrows, nb_col );
3350 int nbrows_not_full = nrows - 1;
3351 int cur_top_size = nt - 1;
3352 remainder = delta_all - nb_col * delta_one_col;
3353 while ( remainder > 0 )
3355 delta_one_col = nbrows_not_full * 2;
3356 int nb_col_add = remainder / delta_one_col;
3357 cur_top_size += 2 * nb_col_by_row[ nbrows_not_full ];
3358 int nb_col_free = cur_top_size / col_top_size - nb_col_by_row[ nbrows_not_full-1 ];
3359 if ( nb_col_add > nb_col_free )
3360 nb_col_add = nb_col_free;
3361 for ( int irow = 0; irow < nbrows_not_full; ++irow )
3362 nb_col_by_row[ irow ] += nb_col_add;
3364 remainder -= nb_col_add * delta_one_col;
3367 else // == "linear" reducing situation
3369 nb_col_by_row.resize( nrows, nb_col );
3371 for ( int irow = remainder / 2; irow < nrows; ++irow )
3372 nb_col_by_row[ irow ]--;
3377 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3379 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3381 for (i = 1; i < nr; i++) // layer by layer
3383 nb_col = nb_col_by_row[ i-1 ];
3384 int nb_next = curr_base_len - nb_col * 2;
3385 if (nb_next < nt) nb_next = nt;
3387 const double y = uv_el[ i ].normParam;
3389 if ( i + 1 == nr ) // top
3396 next_base.resize( nb_next, nullUVPtStruct );
3397 next_base.front() = uv_el[i];
3398 next_base.back() = uv_er[i];
3400 // compute normalized param u
3401 double du = 1. / ( nb_next - 1 );
3402 next_base[0].normParam = 0.;
3403 for ( j = 1; j < nb_next; ++j )
3404 next_base[j].normParam = next_base[j-1].normParam + du;
3406 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3407 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3409 int free_left = ( curr_base_len - 1 - nb_col * col_base_size ) / 2;
3410 int free_middle = curr_base_len - 1 - nb_col * col_base_size - 2 * free_left;
3412 // not reduced left elements
3413 for (j = 0; j < free_left; j++)
3416 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3418 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3420 myHelper->AddFace(curr_base[ j ].node,
3421 curr_base[ j+1 ].node,
3423 next_base[ next_base_len-1 ].node);
3426 for (int icol = 1; icol <= nb_col; icol++)
3429 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3431 j += reduce_grp_size;
3433 // elements in the middle of "columns" added for symmetry
3434 if ( free_middle > 0 && ( nb_col % 2 == 0 ) && icol == nb_col / 2 )
3436 for (int imiddle = 1; imiddle <= free_middle; imiddle++) {
3437 // f (i + 1, j + imiddle)
3438 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3440 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3442 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3443 curr_base[ j +imiddle ].node,
3445 next_base[ next_base_len-1 ].node);
3451 // not reduced right elements
3452 for (; j < curr_base_len-1; j++) {
3454 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3456 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3458 myHelper->AddFace(curr_base[ j ].node,
3459 curr_base[ j+1 ].node,
3461 next_base[ next_base_len-1 ].node);
3464 curr_base_len = next_base_len + 1;
3466 curr_base.swap( next_base );
3470 else if ( is_tree_42 || is_tree_31 )
3472 // "tree" simple reduce "42": 2->4->8->16->32->...
3474 // .-------------------------------.-------------------------------. nr
3476 // | \ .---------------.---------------. / |
3478 // .---------------.---------------.---------------.---------------.
3479 // | \ | / | \ | / |
3480 // | \ .-------.-------. / | \ .-------.-------. / |
3481 // | | | | | | | | |
3482 // .-------.-------.-------.-------.-------.-------.-------.-------. i
3483 // |\ | /|\ | /|\ | /|\ | /|
3484 // | \.---.---./ | \.---.---./ | \.---.---./ | \.---.---./ |
3485 // | | | | | | | | | | | | | | | | |
3486 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
3487 // |\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|
3488 // | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. | .-.-. |
3489 // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3490 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3493 // "tree" simple reduce "31": 1->3->9->27->...
3495 // .-----------------------------------------------------. nr
3497 // | .-----------------. |
3499 // .-----------------.-----------------.-----------------.
3500 // | \ / | \ / | \ / |
3501 // | .-----. | .-----. | .-----. | i
3502 // | | | | | | | | | |
3503 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.
3504 // |\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|\ /|
3505 // | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. | .-. |
3506 // | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3507 // .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. 1
3510 PReduceFunction reduceFunction = & ( is_tree_42 ? reduce42 : reduce31 );
3512 const int reduce_grp_size = is_tree_42 ? 4 : 3;
3514 for (i = 1; i < nr; i++) // layer by layer
3516 // to stop reducing, if number of nodes reaches nt
3517 int delta = curr_base_len - nt;
3519 // to calculate normalized parameter, we must know number of points in next layer
3520 int nb_reduce_groups = (curr_base_len - 1) / reduce_grp_size;
3521 int nb_next = nb_reduce_groups * (reduce_grp_size-2) + (curr_base_len - nb_reduce_groups*reduce_grp_size);
3522 if (nb_next < nt) nb_next = nt;
3524 const double y = uv_el[ i ].normParam;
3526 if ( i + 1 == nr ) // top
3533 next_base.resize( nb_next, nullUVPtStruct );
3534 next_base.front() = uv_el[i];
3535 next_base.back() = uv_er[i];
3537 // compute normalized param u
3538 double du = 1. / ( nb_next - 1 );
3539 next_base[0].normParam = 0.;
3540 for ( j = 1; j < nb_next; ++j )
3541 next_base[j].normParam = next_base[j-1].normParam + du;
3543 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3544 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3546 for (j = 0; j+reduce_grp_size < curr_base_len && delta > 0; j+=reduce_grp_size, delta-=2)
3548 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3551 // not reduced side elements (if any)
3552 for (; j < curr_base_len-1; j++)
3555 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3557 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3559 myHelper->AddFace(curr_base[ j ].node,
3560 curr_base[ j+1 ].node,
3562 next_base[ next_base_len-1 ].node);
3564 curr_base_len = next_base_len + 1;
3566 curr_base.swap( next_base );
3568 } // end "tree" simple reduce
3570 else if ( is_lin_42 || is_lin_31 ) {
3571 // "linear" simple reduce "31": 2->6->10->14
3573 // .-----------------------------.-----------------------------. nr
3575 // | .---------. | .---------. |
3577 // .---------.---------.---------.---------.---------.---------.
3578 // | / \ / \ | / \ / \ |
3579 // | / .-----. \ | / .-----. \ | i
3580 // | / | | \ | / | | \ |
3581 // .-----.-----.-----.-----.-----.-----.-----.-----.-----.-----.
3582 // | / / \ / \ \ | / / \ / \ \ |
3583 // | / / .-. \ \ | / / .-. \ \ |
3584 // | / / / \ \ \ | / / / \ \ \ |
3585 // .--.----.---.-----.---.-----.-.--.----.---.-----.---.-----.-. 1
3588 // "linear" simple reduce "42": 4->8->12->16
3590 // .---------------.---------------.---------------.---------------. nr
3591 // | \ | / | \ | / |
3592 // | \ .-------.-------. / | \ .-------.-------. / |
3593 // | | | | | | | | |
3594 // .-------.-------.-------.-------.-------.-------.-------.-------.
3595 // | / \ | / \ | / \ | / \ |
3596 // | / \.----.----./ \ | / \.----.----./ \ | i
3597 // | / | | | \ | / | | | \ |
3598 // .-----.----.----.----.----.-----.-----.----.----.----.----.-----.
3599 // | / / \ | / \ \ | / / \ | / \ \ |
3600 // | / / .-.-. \ \ | / / .-.-. \ \ |
3601 // | / / / | \ \ \ | / / / | \ \ \ |
3602 // .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1
3605 // nt = 5, nb = 7, nr = 4
3606 //int delta_all = 2;
3607 //int delta_one_col = 6;
3609 //int remainder = 2;
3610 //if (remainder > 0) nb_col++;
3612 //int free_left = 1;
3614 //int free_middle = 4;
3616 int delta_all = nb - nt;
3617 int delta_one_col = (nr - 1) * 2;
3618 int nb_col = delta_all / delta_one_col;
3619 int remainder = delta_all - nb_col * delta_one_col;
3620 if (remainder > 0) {
3623 const int col_top_size = is_lin_42 ? 2 : 1;
3624 int free_left = ((nt - 1) - nb_col * col_top_size) / 2;
3625 free_left += nr - 2;
3626 int free_middle = (nr - 2) * 2;
3627 if (remainder > 0 && nb_col == 1) {
3628 int nb_rows_short_col = remainder / 2;
3629 int nb_rows_thrown = (nr - 1) - nb_rows_short_col;
3630 free_left -= nb_rows_thrown;
3633 // nt = 5, nb = 17, nr = 4
3634 //int delta_all = 12;
3635 //int delta_one_col = 6;
3637 //int remainder = 0;
3638 //int free_left = 2;
3639 //int free_middle = 4;
3641 PReduceFunction reduceFunction = & ( is_lin_42 ? reduce42 : reduce31 );
3643 const int reduce_grp_size = is_lin_42 ? 4 : 3;
3645 for (i = 1; i < nr; i++, free_middle -= 2, free_left -= 1) // layer by layer
3647 // to calculate normalized parameter, we must know number of points in next layer
3648 int nb_next = curr_base_len - nb_col * 2;
3649 if (remainder > 0 && i > remainder / 2)
3650 // take into account short "column"
3652 if (nb_next < nt) nb_next = nt;
3654 const double y = uv_el[ i ].normParam;
3656 if ( i + 1 == nr ) // top
3663 next_base.resize( nb_next, nullUVPtStruct );
3664 next_base.front() = uv_el[i];
3665 next_base.back() = uv_er[i];
3667 // compute normalized param u
3668 double du = 1. / ( nb_next - 1 );
3669 next_base[0].normParam = 0.;
3670 for ( j = 1; j < nb_next; ++j )
3671 next_base[j].normParam = next_base[j-1].normParam + du;
3673 uv[ UV_L ].SetCoord( next_base.front().u, next_base.front().v );
3674 uv[ UV_R ].SetCoord( next_base.back().u, next_base.back().v );
3676 // not reduced left elements
3677 for (j = 0; j < free_left; j++)
3680 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3682 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3684 myHelper->AddFace(curr_base[ j ].node,
3685 curr_base[ j+1 ].node,
3687 next_base[ next_base_len-1 ].node);
3690 for (int icol = 1; icol <= nb_col; icol++) {
3692 if (remainder > 0 && icol == nb_col && i > remainder / 2)
3693 // stop short "column"
3697 reduceFunction( curr_base, next_base, j, next_base_len, quad, uv, y, myHelper, S );
3699 j += reduce_grp_size;
3701 // not reduced middle elements
3702 if (icol < nb_col) {
3703 if (remainder > 0 && icol == nb_col - 1 && i > remainder / 2)
3704 // pass middle elements before stopped short "column"
3707 int free_add = free_middle;
3708 if (remainder > 0 && icol == nb_col - 1)
3709 // next "column" is short
3710 free_add -= (nr - 1) - (remainder / 2);
3712 for (int imiddle = 1; imiddle <= free_add; imiddle++) {
3713 // f (i + 1, j + imiddle)
3714 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3716 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3718 myHelper->AddFace(curr_base[ j-1+imiddle ].node,
3719 curr_base[ j +imiddle ].node,
3721 next_base[ next_base_len-1 ].node);
3727 // not reduced right elements
3728 for (; j < curr_base_len-1; j++) {
3730 const SMDS_MeshNode*& Nf = next_base[++next_base_len].node;
3732 Nf = makeNode( next_base[ next_base_len ], y, quad, uv, myHelper, S );
3734 myHelper->AddFace(curr_base[ j ].node,
3735 curr_base[ j+1 ].node,
3737 next_base[ next_base_len-1 ].node);
3740 curr_base_len = next_base_len + 1;
3742 curr_base.swap( next_base );
3745 } // end "linear" simple reduce
3750 } // end Simple Reduce implementation
3756 //================================================================================
3757 namespace // data for smoothing
3760 // --------------------------------------------------------------------------------
3762 * \brief Structure used to check validity of node position after smoothing.
3763 * It holds two nodes connected to a smoothed node and belonging to
3770 TTriangle( TSmoothNode* n1=0, TSmoothNode* n2=0 ): _n1(n1), _n2(n2) {}
3772 inline bool IsForward( gp_UV uv ) const;
3774 // --------------------------------------------------------------------------------
3776 * \brief Data of a smoothed node
3782 vector< TTriangle > _triangles; // if empty, then node is not movable
3784 // --------------------------------------------------------------------------------
3785 inline bool TTriangle::IsForward( gp_UV uv ) const
3787 gp_Vec2d v1( uv, _n1->_uv ), v2( uv, _n2->_uv );
3791 //================================================================================
3793 * \brief Returns area of a triangle
3795 //================================================================================
3797 double getArea( const gp_UV uv1, const gp_UV uv2, const gp_UV uv3 )
3799 gp_XY v1 = uv1 - uv2, v2 = uv3 - uv2;
3805 //================================================================================
3807 * \brief Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3809 * WARNING: this method must be called AFTER retrieving UVPtStruct's from quad
3811 //================================================================================
3813 void StdMeshers_Quadrangle_2D::updateDegenUV(FaceQuadStruct::Ptr quad)
3817 // Set UV of nodes on degenerated VERTEXes in the middle of degenerated EDGE
3818 // --------------------------------------------------------------------------
3819 for ( unsigned i = 0; i < quad->side.size(); ++i )
3821 const vector<UVPtStruct>& uvVec = quad->side[i].GetUVPtStruct();
3823 // find which end of the side is on degenerated shape
3825 if ( myHelper->IsDegenShape( uvVec[0].node->getshapeId() ))
3827 else if ( myHelper->IsDegenShape( uvVec.back().node->getshapeId() ))
3828 degenInd = uvVec.size() - 1;
3832 // find another side sharing the degenerated shape
3833 bool isPrev = ( degenInd == 0 );
3834 if ( i >= QUAD_TOP_SIDE )
3836 int i2 = ( isPrev ? ( i + 3 ) : ( i + 1 )) % 4;
3837 const vector<UVPtStruct>& uvVec2 = quad->side[ i2 ].GetUVPtStruct();
3839 if ( uvVec[ degenInd ].node == uvVec2.front().node )
3841 else if ( uvVec[ degenInd ].node == uvVec2.back().node )
3842 degenInd2 = uvVec2.size() - 1;
3844 throw SALOME_Exception( LOCALIZED( "Logical error" ));
3846 // move UV in the middle
3847 uvPtStruct& uv1 = const_cast<uvPtStruct&>( uvVec [ degenInd ]);
3848 uvPtStruct& uv2 = const_cast<uvPtStruct&>( uvVec2[ degenInd2 ]);
3849 uv1.u = uv2.u = 0.5 * ( uv1.u + uv2.u );
3850 uv1.v = uv2.v = 0.5 * ( uv1.v + uv2.v );
3853 else if ( quad->side.size() == 4 /*&& myQuadType == QUAD_STANDARD*/)
3855 // Set number of nodes on a degenerated side to be same as on an opposite side
3856 // ----------------------------------------------------------------------------
3857 for ( size_t i = 0; i < quad->side.size(); ++i )
3859 StdMeshers_FaceSidePtr degSide = quad->side[i];
3860 if ( !myHelper->IsDegenShape( degSide->EdgeID(0) ))
3862 StdMeshers_FaceSidePtr oppSide = quad->side[( i+2 ) % quad->side.size() ];
3863 if ( degSide->NbSegments() == oppSide->NbSegments() )
3866 // make new side data
3867 const vector<UVPtStruct>& uvVecDegOld = degSide->GetUVPtStruct();
3868 const SMDS_MeshNode* n = uvVecDegOld[0].node;
3869 Handle(Geom2d_Curve) c2d = degSide->Curve2d(0);
3870 double f = degSide->FirstU(0), l = degSide->LastU(0);
3871 gp_Pnt2d p1 = uvVecDegOld.front().UV();
3872 gp_Pnt2d p2 = uvVecDegOld.back().UV();
3874 quad->side[i] = StdMeshers_FaceSide::New( oppSide.get(), n, &p1, &p2, c2d, f, l );
3878 //================================================================================
3880 * \brief Perform smoothing of 2D elements on a FACE with ignored degenerated EDGE
3882 //================================================================================
3884 void StdMeshers_Quadrangle_2D::smooth (FaceQuadStruct::Ptr quad)
3886 if ( !myNeedSmooth ) return;
3888 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
3889 const double tol = BRep_Tool::Tolerance( quad->face );
3890 Handle(ShapeAnalysis_Surface) surface = myHelper->GetSurface( quad->face );
3892 if ( myHelper->HasDegeneratedEdges() && myForcedPnts.empty() )
3894 // "smooth" by computing node positions using 3D TFI and further projection
3896 list< FaceQuadStruct::Ptr >::iterator q = myQuadList.begin();
3897 for ( ; q != myQuadList.end() ; ++q )
3900 int nbhoriz = quad->iSize;
3901 int nbvertic = quad->jSize;
3903 SMESH_TNodeXYZ a0( quad->UVPt( 0, 0 ).node );
3904 SMESH_TNodeXYZ a1( quad->UVPt( nbhoriz-1, 0 ).node );
3905 SMESH_TNodeXYZ a2( quad->UVPt( nbhoriz-1, nbvertic-1 ).node );
3906 SMESH_TNodeXYZ a3( quad->UVPt( 0, nbvertic-1 ).node );
3908 for (int i = 1; i < nbhoriz-1; i++)
3910 SMESH_TNodeXYZ p0( quad->UVPt( i, 0 ).node );
3911 SMESH_TNodeXYZ p2( quad->UVPt( i, nbvertic-1 ).node );
3912 for (int j = 1; j < nbvertic-1; j++)
3914 SMESH_TNodeXYZ p1( quad->UVPt( nbhoriz-1, j ).node );
3915 SMESH_TNodeXYZ p3( quad->UVPt( 0, j ).node );
3917 UVPtStruct& uvp = quad->UVPt( i, j );
3919 gp_Pnt p = myHelper->calcTFI(uvp.x,uvp.y, a0,a1,a2,a3, p0,p1,p2,p3);
3920 gp_Pnt2d uv = surface->NextValueOfUV( uvp.UV(), p, 10*tol );
3921 gp_Pnt pnew = surface->Value( uv );
3923 meshDS->MoveNode( uvp.node, pnew.X(), pnew.Y(), pnew.Z() );
3932 // Get nodes to smooth
3934 typedef map< const SMDS_MeshNode*, TSmoothNode, TIDCompare > TNo2SmooNoMap;
3935 TNo2SmooNoMap smooNoMap;
3938 boost::container::flat_set< const SMDS_MeshNode* > fixedNodes;
3939 for ( size_t i = 0; i < myForcedPnts.size(); ++i )
3941 fixedNodes.insert( myForcedPnts[i].node );
3942 if ( myForcedPnts[i].node->getshapeId() != myHelper->GetSubShapeID() )
3944 TSmoothNode & sNode = smooNoMap[ myForcedPnts[i].node ];
3945 sNode._uv = myForcedPnts[i].uv;
3946 sNode._xyz = SMESH_TNodeXYZ( myForcedPnts[i].node );
3949 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( quad->face );
3950 SMDS_NodeIteratorPtr nIt = fSubMesh->GetNodes();
3951 while ( nIt->more() ) // loop on nodes bound to a FACE
3953 const SMDS_MeshNode* node = nIt->next();
3954 TSmoothNode & sNode = smooNoMap[ node ];
3955 sNode._uv = myHelper->GetNodeUV( quad->face, node );
3956 sNode._xyz = SMESH_TNodeXYZ( node );
3957 if ( fixedNodes.count( node ))
3958 continue; // fixed - no triangles
3960 // set sNode._triangles
3961 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator( SMDSAbs_Face );
3962 while ( fIt->more() )
3964 const SMDS_MeshElement* face = fIt->next();
3965 const int nbN = face->NbCornerNodes();
3966 const int nInd = face->GetNodeIndex( node );
3967 const int prevInd = myHelper->WrapIndex( nInd - 1, nbN );
3968 const int nextInd = myHelper->WrapIndex( nInd + 1, nbN );
3969 const SMDS_MeshNode* prevNode = face->GetNode( prevInd );
3970 const SMDS_MeshNode* nextNode = face->GetNode( nextInd );
3971 sNode._triangles.push_back( TTriangle( & smooNoMap[ prevNode ],
3972 & smooNoMap[ nextNode ]));
3975 // set _uv of smooth nodes on FACE boundary
3976 set< StdMeshers_FaceSide* > sidesOnEdge;
3977 list< FaceQuadStruct::Ptr >::iterator q = myQuadList.begin();
3978 for ( ; q != myQuadList.end() ; ++q )
3979 for ( size_t i = 0; i < (*q)->side.size(); ++i )
3980 if ( ! (*q)->side[i].grid->Edge(0).IsNull() &&
3981 //(*q)->nbNodeOut( i ) == 0 &&
3982 sidesOnEdge.insert( (*q)->side[i].grid.get() ).second )
3984 const vector<UVPtStruct>& uvVec = (*q)->side[i].grid->GetUVPtStruct();
3985 for ( unsigned j = 0; j < uvVec.size(); ++j )
3987 TSmoothNode & sNode = smooNoMap[ uvVec[j].node ];
3988 sNode._uv = uvVec[j].UV();
3989 sNode._xyz = SMESH_TNodeXYZ( uvVec[j].node );
3993 // define reference orientation in 2D
3994 TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
3995 for ( ; n2sn != smooNoMap.end(); ++n2sn )
3996 if ( !n2sn->second._triangles.empty() )
3998 if ( n2sn == smooNoMap.end() ) return;
3999 const TSmoothNode & sampleNode = n2sn->second;
4000 const bool refForward = ( sampleNode._triangles[0].IsForward( sampleNode._uv ));
4004 for ( int iLoop = 0; iLoop < 5; ++iLoop )
4006 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
4008 TSmoothNode& sNode = n2sn->second;
4009 if ( sNode._triangles.empty() )
4010 continue; // not movable node
4013 bool isValid = false;
4014 bool use3D = ( iLoop > 2 ); // 3 loops in 2D and 2, in 3D
4018 // compute a new XYZ
4019 gp_XYZ newXYZ (0,0,0);
4020 for ( size_t i = 0; i < sNode._triangles.size(); ++i )
4021 newXYZ += sNode._triangles[i]._n1->_xyz;
4022 newXYZ /= sNode._triangles.size();
4024 // compute a new UV by projection
4025 newUV = surface->NextValueOfUV( sNode._uv, newXYZ, 10*tol ).XY();
4027 // check validity of the newUV
4028 for ( size_t i = 0; i < sNode._triangles.size() && isValid; ++i )
4029 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4033 // compute a new UV by averaging
4034 newUV.SetCoord(0.,0.);
4035 for ( unsigned i = 0; i < sNode._triangles.size(); ++i )
4036 newUV += sNode._triangles[i]._n1->_uv;
4037 newUV /= sNode._triangles.size();
4039 // check validity of the newUV
4041 for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
4042 isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
4047 sNode._xyz = surface->Value( newUV ).XYZ();
4052 // Set new XYZ to the smoothed nodes
4054 for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
4056 TSmoothNode& sNode = n2sn->second;
4057 if ( sNode._triangles.empty() )
4058 continue; // not movable node
4060 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( n2sn->first );
4061 gp_Pnt xyz = surface->Value( sNode._uv );
4062 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4065 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( sNode._uv.X(), sNode._uv.Y() )));
4069 // Move medium nodes in quadratic mesh
4070 if ( _quadraticMesh )
4072 const TLinkNodeMap& links = myHelper->GetTLinkNodeMap();
4073 TLinkNodeMap::const_iterator linkIt = links.begin();
4074 for ( ; linkIt != links.end(); ++linkIt )
4076 const SMESH_TLink& link = linkIt->first;
4077 SMDS_MeshNode* node = const_cast< SMDS_MeshNode*>( linkIt->second );
4079 if ( node->getshapeId() != myHelper->GetSubShapeID() )
4080 continue; // medium node is on EDGE or VERTEX
4082 gp_XYZ pm = 0.5 * ( SMESH_TNodeXYZ( link.node1() ) + SMESH_TNodeXYZ( link.node2() ));
4083 gp_XY uvm = myHelper->GetNodeUV( quad->face, node );
4085 gp_Pnt2d uv = surface->NextValueOfUV( uvm, pm, 10*tol );
4086 gp_Pnt xyz = surface->Value( uv );
4088 node->SetPosition( SMDS_PositionPtr( new SMDS_FacePosition( uv.X(), uv.Y() )));
4089 meshDS->MoveNode( node, xyz.X(), xyz.Y(), xyz.Z() );
4095 //================================================================================
4097 * \brief Checks validity of generated faces
4099 //================================================================================
4101 bool StdMeshers_Quadrangle_2D::check()
4103 const bool isOK = true;
4104 if ( !myCheckOri || myQuadList.empty() || !myQuadList.front() || !myHelper )
4107 TopoDS_Face geomFace = TopoDS::Face( myHelper->GetSubShape() );
4108 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
4109 SMESHDS_SubMesh* fSubMesh = meshDS->MeshElements( geomFace );
4111 if ( geomFace.Orientation() >= TopAbs_INTERNAL ) geomFace.Orientation( TopAbs_FORWARD );
4113 // Get a reference orientation sign
4118 TSideVector wireVec =
4119 StdMeshers_FaceSide::GetFaceWires( geomFace, *myHelper->GetMesh(), true, err, myHelper );
4120 StdMeshers_FaceSidePtr wire = wireVec[0];
4122 // find a right angle VERTEX
4124 double maxAngle = -1e100;
4125 for ( int i = 0; i < wire->NbEdges(); ++i )
4127 int iPrev = myHelper->WrapIndex( i-1, wire->NbEdges() );
4128 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4129 const TopoDS_Edge& e2 = wire->Edge( i );
4130 double angle = myHelper->GetAngle( e1, e2, geomFace, wire->FirstVertex( i ));
4131 if (( maxAngle < angle ) &&
4132 ( 5.* M_PI/180 < angle && angle < 175.* M_PI/180 ))
4138 if ( maxAngle < -2*M_PI ) return isOK;
4140 // get a sign of 2D area of a corner face
4142 int iPrev = myHelper->WrapIndex( iVertex-1, wire->NbEdges() );
4143 const TopoDS_Edge& e1 = wire->Edge( iPrev );
4144 const TopoDS_Edge& e2 = wire->Edge( iVertex );
4146 gp_Vec2d v1, v2; gp_Pnt2d p;
4149 bool rev = ( e1.Orientation() == TopAbs_REVERSED );
4150 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e1, geomFace, u[0], u[1] );
4151 c->D1( u[ !rev ], p, v1 );
4156 bool rev = ( e2.Orientation() == TopAbs_REVERSED );
4157 Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface( e2, geomFace, u[0], u[1] );
4158 c->D1( u[ rev ], p, v2 );
4169 // Look for incorrectly oriented faces
4171 std::list<const SMDS_MeshElement*> badFaces;
4173 const SMDS_MeshNode* nn [ 8 ]; // 8 is just for safety
4175 SMDS_ElemIteratorPtr fIt = fSubMesh->GetElements();
4176 while ( fIt->more() ) // loop on faces bound to a FACE
4178 const SMDS_MeshElement* f = fIt->next();
4180 const int nbN = f->NbCornerNodes();
4181 for ( int i = 0; i < nbN; ++i )
4182 nn[ i ] = f->GetNode( i );
4184 const SMDS_MeshNode* nInFace = 0;
4185 if ( myHelper->HasSeam() )
4186 for ( int i = 0; i < nbN && !nInFace; ++i )
4187 if ( !myHelper->IsSeamShape( nn[i]->getshapeId() ))
4190 gp_XY uv = myHelper->GetNodeUV( geomFace, nInFace );
4191 if ( myHelper->IsOnSeam( uv ))
4196 for ( int i = 0; i < nbN; ++i )
4197 uv[ i ] = myHelper->GetNodeUV( geomFace, nn[i], nInFace, &toCheckUV );
4203 double sign1 = getArea( uv[0], uv[1], uv[2] );
4204 double sign2 = getArea( uv[0], uv[2], uv[3] );
4205 if ( sign1 * sign2 < 0 )
4207 sign2 = getArea( uv[1], uv[2], uv[3] );
4208 sign1 = getArea( uv[1], uv[3], uv[0] );
4209 if ( sign1 * sign2 < 0 )
4210 continue; // this should not happen
4212 isBad = ( sign1 * okSign < 0 );
4217 double sign = getArea( uv[0], uv[1], uv[2] );
4218 isBad = ( sign * okSign < 0 );
4224 // if ( isBad && myHelper->HasRealSeam() )
4226 // // detect a case where a face intersects the seam
4227 // for ( int iPar = 1; iPar < 3; ++iPar )
4228 // if ( iPar & myHelper->GetPeriodicIndex() )
4230 // double min = uv[0].Coord( iPar ), max = uv[0].Coord( iPar );
4231 // for ( int i = 1; i < nbN; ++i )
4233 // min = Min( min, uv[i].Coord( iPar ));
4234 // max = Max( max, uv[i].Coord( iPar ));
4239 badFaces.push_back ( f );
4242 if ( !badFaces.empty() )
4244 SMESH_subMesh* fSM = myHelper->GetMesh()->GetSubMesh( geomFace );
4245 SMESH_ComputeErrorPtr& err = fSM->GetComputeError();
4246 err.reset ( new SMESH_ComputeError( COMPERR_ALGO_FAILED,
4247 "Inverted elements generated"));
4248 err->myBadElements.swap( badFaces );
4256 //================================================================================
4258 * \brief Finds vertices at the most sharp face corners
4259 * \param [in] theFace - the FACE
4260 * \param [in,out] theWire - the ordered edges of the face. It can be modified to
4261 * have the first VERTEX of the first EDGE in \a vertices
4262 * \param [out] theVertices - the found corner vertices in the order corresponding to
4263 * the order of EDGEs in \a theWire
4264 * \param [out] theNbDegenEdges - nb of degenerated EDGEs in theFace
4265 * \param [in] theConsiderMesh - if \c true, only meshed VERTEXes are considered
4266 * as possible corners
4267 * \return int - number of quad sides found: 0, 3 or 4
4269 //================================================================================
4271 int StdMeshers_Quadrangle_2D::getCorners(const TopoDS_Face& theFace,
4272 SMESH_Mesh & theMesh,
4273 std::list<TopoDS_Edge>& theWire,
4274 std::vector<TopoDS_Vertex>& theVertices,
4275 int & theNbDegenEdges,
4276 const bool theConsiderMesh)
4278 theNbDegenEdges = 0;
4280 SMESH_MesherHelper helper( theMesh );
4282 helper.CopySubShapeInfo( *myHelper );
4283 StdMeshers_FaceSide faceSide( theFace, theWire, &theMesh,
4284 /*isFwd=*/true, /*skipMedium=*/true, &helper );
4286 // sort theVertices by angle
4287 multimap<double, TopoDS_Vertex> vertexByAngle;
4288 TopTools_DataMapOfShapeReal angleByVertex;
4289 TopoDS_Edge prevE = theWire.back();
4290 if ( SMESH_Algo::isDegenerated( prevE ))
4292 list<TopoDS_Edge>::reverse_iterator edge = ++theWire.rbegin();
4293 while ( SMESH_Algo::isDegenerated( *edge ) /*|| helper.IsRealSeam( *edge )*/)
4295 if ( edge == theWire.rend() )
4299 list<TopoDS_Edge>::iterator edge = theWire.begin();
4300 for ( int iE = 0; edge != theWire.end(); ++edge, ++iE )
4302 if ( SMESH_Algo::isDegenerated( *edge ) /*|| helper.IsRealSeam( *edge )*/)
4307 if ( !theConsiderMesh || faceSide.VertexNode( iE ))
4309 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4310 double angle = helper.GetAngle( prevE, *edge, theFace, v );
4311 vertexByAngle.insert( make_pair( angle, v ));
4312 angleByVertex.Bind( v, angle );
4317 // find out required nb of corners (3 or 4)
4319 TopoDS_Shape triaVertex = helper.GetMeshDS()->IndexToShape( myTriaVertexID );
4320 if ( !triaVertex.IsNull() &&
4321 triaVertex.ShapeType() == TopAbs_VERTEX &&
4322 helper.IsSubShape( triaVertex, theFace ) &&
4323 ( vertexByAngle.size() != 4 || vertexByAngle.begin()->first < 5 * M_PI/180. ))
4326 triaVertex.Nullify();
4328 // check nb of available corners
4329 if ( faceSide.NbEdges() < nbCorners )
4330 return error(COMPERR_BAD_SHAPE,
4331 TComm("Face must have 4 sides but not ") << faceSide.NbEdges() );
4333 if ( theConsiderMesh )
4335 const int nbSegments = Max( faceSide.NbPoints()-1, faceSide.NbSegments() );
4336 if ( nbSegments < nbCorners )
4337 return error(COMPERR_BAD_INPUT_MESH, TComm("Too few boundary nodes: ") << nbSegments);
4340 if ( nbCorners == 3 )
4342 if ( vertexByAngle.size() < 3 )
4343 return error(COMPERR_BAD_SHAPE,
4344 TComm("Face must have 3 sides but not ") << vertexByAngle.size() );
4348 if ( vertexByAngle.size() == 3 && theNbDegenEdges == 0 )
4350 if ( myTriaVertexID < 1 )
4351 return error(COMPERR_BAD_PARMETERS,
4352 "No Base vertex provided for a trilateral geometrical face");
4354 TComm comment("Invalid Base vertex: ");
4355 comment << myTriaVertexID << " its ID is not among [ ";
4356 multimap<double, TopoDS_Vertex>::iterator a2v = vertexByAngle.begin();
4357 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4358 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << ", "; a2v++;
4359 comment << helper.GetMeshDS()->ShapeToIndex( a2v->second ) << " ]";
4360 return error(COMPERR_BAD_PARMETERS, comment );
4362 if ( vertexByAngle.size() + ( theNbDegenEdges > 0 ) < 4 &&
4363 vertexByAngle.size() + theNbDegenEdges != 4 )
4364 return error(COMPERR_BAD_SHAPE,
4365 TComm("Face must have 4 sides but not ") << vertexByAngle.size() );
4368 // put all corner vertices in a map
4369 TopTools_MapOfShape vMap;
4370 if ( nbCorners == 3 )
4371 vMap.Add( triaVertex );
4372 multimap<double, TopoDS_Vertex>::reverse_iterator a2v = vertexByAngle.rbegin();
4373 for ( int iC = 0; a2v != vertexByAngle.rend() && iC < nbCorners; ++a2v, ++iC )
4374 vMap.Add( (*a2v).second );
4376 // check if there are possible variations in choosing corners
4377 bool haveVariants = false;
4378 if ((int) vertexByAngle.size() > nbCorners )
4380 double lostAngle = a2v->first;
4381 double lastAngle = ( --a2v, a2v->first );
4382 haveVariants = ( lostAngle * 1.1 >= lastAngle );
4385 const double angleTol = 5.* M_PI/180;
4386 myCheckOri = ( (int)vertexByAngle.size() > nbCorners ||
4387 vertexByAngle.begin()->first < angleTol );
4389 // make theWire begin from a corner vertex or triaVertex
4390 if ( nbCorners == 3 )
4391 while ( !triaVertex.IsSame( ( helper.IthVertex( 0, theWire.front() ))) ||
4392 SMESH_Algo::isDegenerated( theWire.front() ))
4393 theWire.splice( theWire.end(), theWire, theWire.begin() );
4395 while ( !vMap.Contains( helper.IthVertex( 0, theWire.front() )) ||
4396 SMESH_Algo::isDegenerated( theWire.front() ))
4397 theWire.splice( theWire.end(), theWire, theWire.begin() );
4399 // fill the result vector and prepare for its refinement
4400 theVertices.clear();
4401 vector< double > angles;
4402 vector< TopoDS_Edge > edgeVec;
4403 vector< int > cornerInd, nbSeg;
4405 angles .reserve( vertexByAngle.size() );
4406 edgeVec.reserve( vertexByAngle.size() );
4407 nbSeg .reserve( vertexByAngle.size() );
4408 cornerInd.reserve( nbCorners );
4409 for ( edge = theWire.begin(); edge != theWire.end(); ++edge )
4411 if ( SMESH_Algo::isDegenerated( *edge ))
4413 TopoDS_Vertex v = helper.IthVertex( 0, *edge );
4414 bool isCorner = vMap.Contains( v );
4417 theVertices.push_back( v );
4418 cornerInd.push_back( angles.size() );
4420 angles .push_back( angleByVertex.IsBound( v ) ? angleByVertex( v ) : -M_PI );
4421 edgeVec.push_back( *edge );
4422 if ( theConsiderMesh && haveVariants )
4424 if ( SMESHDS_SubMesh* sm = helper.GetMeshDS()->MeshElements( *edge ))
4425 nbSeg.push_back( sm->NbNodes() + 1 );
4427 nbSeg.push_back( 0 );
4428 nbSegTot += nbSeg.back();
4432 // refine the result vector - make sides equal by length if
4433 // there are several equal angles
4436 if ( nbCorners == 3 )
4437 angles[0] = 2 * M_PI; // not to move the base triangle VERTEX
4439 // here we refer to VERTEX'es and EDGEs by indices in angles and edgeVec vectors
4440 typedef int TGeoIndex;
4442 // for each vertex find a vertex till which there are nbSegHalf segments
4443 const int nbSegHalf = ( nbSegTot % 2 || nbCorners == 3 ) ? 0 : nbSegTot / 2;
4444 vector< TGeoIndex > halfDivider( angles.size(), -1 );
4445 int nbHalfDividers = 0;
4448 // get min angle of corners
4449 double minAngle = 10.;
4450 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4451 minAngle = Min( minAngle, angles[ cornerInd[ iC ]]);
4453 // find halfDivider's
4454 for ( TGeoIndex iV1 = 0; iV1 < TGeoIndex( angles.size() ); ++iV1 )
4457 TGeoIndex iV2 = iV1;
4459 nbSegs += nbSeg[ iV2 ];
4460 iV2 = helper.WrapIndex( iV2 + 1, nbSeg.size() );
4461 } while ( nbSegs < nbSegHalf );
4463 if ( nbSegs == nbSegHalf &&
4464 angles[ iV1 ] + angleTol >= minAngle &&
4465 angles[ iV2 ] + angleTol >= minAngle )
4467 halfDivider[ iV1 ] = iV2;
4473 set< TGeoIndex > refinedCorners, treatedCorners;
4474 for ( size_t iC = 0; iC < cornerInd.size(); ++iC )
4476 TGeoIndex iV = cornerInd[iC];
4477 if ( !treatedCorners.insert( iV ).second )
4479 list< TGeoIndex > equVerts; // inds of vertices that can become corners
4480 equVerts.push_back( iV );
4481 int nbC[2] = { 0, 0 };
4482 // find equal angles backward and forward from the iV-th corner vertex
4483 for ( int isFwd = 0; isFwd < 2; ++isFwd )
4485 int dV = isFwd ? +1 : -1;
4486 int iCNext = helper.WrapIndex( iC + dV, cornerInd.size() );
4487 TGeoIndex iVNext = helper.WrapIndex( iV + dV, angles.size() );
4488 while ( iVNext != iV )
4490 bool equal = Abs( angles[iV] - angles[iVNext] ) < angleTol;
4492 equVerts.insert( isFwd ? equVerts.end() : equVerts.begin(), iVNext );
4493 if ( iVNext == cornerInd[ iCNext ])
4497 if ( angles[iV] < angles[iVNext] )
4498 refinedCorners.insert( iVNext );
4502 treatedCorners.insert( cornerInd[ iCNext ] );
4503 iCNext = helper.WrapIndex( iCNext + dV, cornerInd.size() );
4505 iVNext = helper.WrapIndex( iVNext + dV, angles.size() );
4508 break; // all angles equal
4511 const bool allCornersSame = ( nbC[0] == 3 );
4512 if ( allCornersSame && nbHalfDividers > 0 )
4514 // select two halfDivider's as corners
4515 TGeoIndex hd1, hd2 = -1;
4517 for ( iC2 = 0; iC2 < cornerInd.size() && hd2 < 0; ++iC2 )
4519 hd1 = cornerInd[ iC2 ];
4520 hd2 = halfDivider[ hd1 ];
4521 if ( std::find( equVerts.begin(), equVerts.end(), hd2 ) == equVerts.end() )
4522 hd2 = -1; // hd2-th vertex can't become a corner
4528 angles[ hd1 ] = 2 * M_PI; // make hd1-th vertex no more "equal"
4529 angles[ hd2 ] = 2 * M_PI;
4530 refinedCorners.insert( hd1 );
4531 refinedCorners.insert( hd2 );
4532 treatedCorners = refinedCorners;
4534 equVerts.push_front( equVerts.back() );
4535 equVerts.push_back( equVerts.front() );
4536 list< TGeoIndex >::iterator hdPos =
4537 std::find( equVerts.begin(), equVerts.end(), hd2 );
4538 if ( hdPos == equVerts.end() ) break;
4539 cornerInd[ helper.WrapIndex( iC2 + 0, cornerInd.size()) ] = hd1;
4540 cornerInd[ helper.WrapIndex( iC2 + 1, cornerInd.size()) ] = *( --hdPos );
4541 cornerInd[ helper.WrapIndex( iC2 + 2, cornerInd.size()) ] = hd2;
4542 cornerInd[ helper.WrapIndex( iC2 + 3, cornerInd.size()) ] = *( ++hdPos, ++hdPos );
4544 theVertices[ 0 ] = helper.IthVertex( 0, edgeVec[ cornerInd[0] ]);
4545 theVertices[ 1 ] = helper.IthVertex( 0, edgeVec[ cornerInd[1] ]);
4546 theVertices[ 2 ] = helper.IthVertex( 0, edgeVec[ cornerInd[2] ]);
4547 theVertices[ 3 ] = helper.IthVertex( 0, edgeVec[ cornerInd[3] ]);
4553 // move corners to make sides equal by length
4554 int nbEqualV = equVerts.size();
4555 int nbExcessV = nbEqualV - ( 1 + nbC[0] + nbC[1] );
4556 if ( nbExcessV > 0 ) // there are nbExcessV vertices that can become corners
4558 // calculate normalized length of each "side" enclosed between neighbor equVerts
4559 vector< double > accuLength;
4560 double totalLen = 0;
4561 vector< TGeoIndex > evVec( equVerts.begin(), equVerts.end() );
4563 TGeoIndex iE = cornerInd[ helper.WrapIndex( iC - nbC[0] - 1, cornerInd.size() )];
4564 TGeoIndex iEEnd = cornerInd[ helper.WrapIndex( iC + nbC[1] + 1, cornerInd.size() )];
4565 while ((int) accuLength.size() < nbEqualV + int( !allCornersSame ) )
4567 // accumulate length of edges before iEV-th equal vertex
4568 accuLength.push_back( totalLen );
4570 accuLength.back() += SMESH_Algo::EdgeLength( edgeVec[ iE ]);
4571 iE = helper.WrapIndex( iE + 1, edgeVec.size());
4572 if ( iEV < evVec.size() && iE == evVec[ iEV ] ) {
4574 break; // equal vertex reached
4577 while( iE != iEEnd );
4578 totalLen = accuLength.back();
4580 accuLength.resize( equVerts.size() );
4581 for ( size_t iS = 0; iS < accuLength.size(); ++iS )
4582 accuLength[ iS ] /= totalLen;
4584 // find equVerts most close to the ideal sub-division of all sides
4586 int iCorner = helper.WrapIndex( iC - nbC[0], cornerInd.size() );
4587 int nbSides = Min( nbCorners, 2 + nbC[0] + nbC[1] );
4588 for ( int iS = 1; iS < nbSides; ++iS, ++iBestEV )
4590 double idealLen = iS / double( nbSides );
4591 double d, bestDist = 2.;
4592 for ( iEV = iBestEV; iEV < accuLength.size(); ++iEV )
4594 d = Abs( idealLen - accuLength[ iEV ]);
4596 // take into account presence of a corresponding halfDivider
4597 const double cornerWgt = 0.5 / nbSides;
4598 const double vertexWgt = 0.25 / nbSides;
4599 TGeoIndex hd = halfDivider[ evVec[ iEV ]];
4602 else if( refinedCorners.count( hd ))
4607 // choose vertex with the best d
4614 if ( iBestEV > iS-1 + nbExcessV )
4615 iBestEV = iS-1 + nbExcessV;
4616 theVertices[ iCorner ] = helper.IthVertex( 0, edgeVec[ evVec[ iBestEV ]]);
4617 cornerInd [ iCorner ] = evVec[ iBestEV ];
4618 refinedCorners.insert( evVec[ iBestEV ]);
4619 iCorner = helper.WrapIndex( iCorner + 1, cornerInd.size() );
4622 } // if ( nbExcessV > 0 )
4625 refinedCorners.insert( cornerInd[ iC ]);
4627 } // loop on cornerInd
4629 // make theWire begin from the cornerInd[0]-th EDGE
4630 while ( !theWire.front().IsSame( edgeVec[ cornerInd[0] ]))
4631 theWire.splice( theWire.begin(), theWire, --theWire.end() );
4633 } // if ( haveVariants )
4638 //================================================================================
4640 * \brief Constructor of a side of quad
4642 //================================================================================
4644 FaceQuadStruct::Side::Side(StdMeshers_FaceSidePtr theGrid)
4645 : grid(theGrid), from(0), to(theGrid ? theGrid->NbPoints() : 0 ), di(1), nbNodeOut(0)
4649 //=============================================================================
4651 * \brief Constructor of a quad
4653 //=============================================================================
4655 FaceQuadStruct::FaceQuadStruct(const TopoDS_Face& F, const std::string& theName)
4656 : face( F ), name( theName )
4661 //================================================================================
4663 * \brief Fills myForcedPnts
4665 //================================================================================
4667 bool StdMeshers_Quadrangle_2D::getEnforcedUV()
4669 myForcedPnts.clear();
4670 if ( !myParams ) return true; // missing hypothesis
4672 std::vector< TopoDS_Shape > shapes;
4673 std::vector< gp_Pnt > points;
4674 myParams->GetEnforcedNodes( shapes, points );
4676 TopTools_IndexedMapOfShape vMap;
4677 for ( size_t i = 0; i < shapes.size(); ++i )
4678 if ( !shapes[i].IsNull() )
4679 TopExp::MapShapes( shapes[i], TopAbs_VERTEX, vMap );
4681 size_t nbPoints = points.size();
4682 for ( int i = 1; i <= vMap.Extent(); ++i )
4683 points.push_back( BRep_Tool::Pnt( TopoDS::Vertex( vMap( i ))));
4685 // find out if all points must be in the FACE, which is so if
4686 // myParams is a local hypothesis on the FACE being meshed
4687 bool isStrictCheck = false;
4689 SMESH_HypoFilter paramFilter( SMESH_HypoFilter::Is( myParams ));
4690 TopoDS_Shape assignedTo;
4691 if ( myHelper->GetMesh()->GetHypothesis( myHelper->GetSubShape(),
4695 isStrictCheck = ( assignedTo.IsSame( myHelper->GetSubShape() ));
4698 multimap< double, ForcedPoint > sortedFP; // sort points by distance from EDGEs
4700 Standard_Real u1,u2,v1,v2;
4701 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4702 const double tol = BRep_Tool::Tolerance( face );
4703 Handle(ShapeAnalysis_Surface) project = myHelper->GetSurface( face );
4704 project->Bounds( u1,u2,v1,v2 );
4706 BRepBndLib::Add( face, bbox );
4707 double farTol = 0.01 * sqrt( bbox.SquareExtent() );
4709 // get internal VERTEXes of the FACE to use them instead of equal points
4710 typedef map< pair< double, double >, TopoDS_Vertex > TUV2VMap;
4712 for ( TopExp_Explorer vExp( face, TopAbs_VERTEX, TopAbs_EDGE ); vExp.More(); vExp.Next() )
4714 TopoDS_Vertex v = TopoDS::Vertex( vExp.Current() );
4715 gp_Pnt2d uv = project->ValueOfUV( BRep_Tool::Pnt( v ), tol );
4716 uv2intV.insert( make_pair( make_pair( uv.X(), uv.Y() ), v ));
4719 for ( size_t iP = 0; iP < points.size(); ++iP )
4721 gp_Pnt2d uv = project->ValueOfUV( points[ iP ], tol );
4722 if ( project->Gap() > farTol )
4724 if ( isStrictCheck && iP < nbPoints )
4726 (COMPERR_BAD_PARMETERS, TComm("An enforced point is too far from the face, dist = ")
4727 << points[ iP ].Distance( project->Value( uv )) << " - ("
4728 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4731 BRepClass_FaceClassifier clsf ( face, uv, tol );
4732 switch ( clsf.State() ) {
4735 double edgeDist = ( Min( Abs( uv.X() - u1 ), Abs( uv.X() - u2 )) +
4736 Min( Abs( uv.Y() - v1 ), Abs( uv.Y() - v2 )));
4739 fp.xyz = points[ iP ].XYZ();
4740 if ( iP >= nbPoints )
4741 fp.vertex = TopoDS::Vertex( vMap( iP - nbPoints + 1 ));
4743 TUV2VMap::iterator uv2v = uv2intV.lower_bound( make_pair( uv.X()-tol, uv.Y()-tol ));
4744 for ( ; uv2v != uv2intV.end() && uv2v->first.first <= uv.X()+tol; ++uv2v )
4745 if ( uv.SquareDistance( gp_Pnt2d( uv2v->first.first, uv2v->first.second )) < tol*tol )
4747 fp.vertex = uv2v->second;
4752 if ( myHelper->IsSubShape( fp.vertex, myHelper->GetMesh() ))
4754 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( fp.vertex );
4755 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
4756 fp.node = SMESH_Algo::VertexNode( fp.vertex, myHelper->GetMeshDS() );
4760 fp.node = myHelper->AddNode( fp.xyz.X(), fp.xyz.Y(), fp.xyz.Z(),
4761 0, fp.uv.X(), fp.uv.Y() );
4763 sortedFP.insert( make_pair( edgeDist, fp ));
4768 if ( isStrictCheck && iP < nbPoints )
4770 (COMPERR_BAD_PARMETERS, TComm("An enforced point is out of the face boundary - ")
4771 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4776 if ( isStrictCheck && iP < nbPoints )
4778 (COMPERR_BAD_PARMETERS, TComm("An enforced point is on the face boundary - ")
4779 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4784 if ( isStrictCheck && iP < nbPoints )
4786 (TComm("Classification of an enforced point ralative to the face boundary failed - ")
4787 << points[ iP ].X() << ", "<< points[ iP ].Y() << ", "<< points[ iP ].Z() << " )");
4792 multimap< double, ForcedPoint >::iterator d2uv = sortedFP.begin();
4793 for ( ; d2uv != sortedFP.end(); ++d2uv )
4794 myForcedPnts.push_back( (*d2uv).second );
4799 //================================================================================
4801 * \brief Splits quads by adding points of enforced nodes and create nodes on
4802 * the sides shared by quads
4804 //================================================================================
4806 bool StdMeshers_Quadrangle_2D::addEnforcedNodes()
4808 // if ( myForcedPnts.empty() )
4811 // make a map of quads sharing a side
4812 map< StdMeshers_FaceSidePtr, vector< FaceQuadStruct::Ptr > > quadsBySide;
4813 list< FaceQuadStruct::Ptr >::iterator quadIt = myQuadList.begin();
4814 for ( ; quadIt != myQuadList.end(); ++quadIt )
4815 for ( size_t iSide = 0; iSide < (*quadIt)->side.size(); ++iSide )
4817 if ( !setNormalizedGrid( *quadIt ))
4819 quadsBySide[ (*quadIt)->side[iSide] ].push_back( *quadIt );
4822 const TopoDS_Face& face = TopoDS::Face( myHelper->GetSubShape() );
4823 Handle(Geom_Surface) surf = BRep_Tool::Surface( face );
4825 for ( size_t iFP = 0; iFP < myForcedPnts.size(); ++iFP )
4827 bool isNodeEnforced = false;
4829 // look for a quad enclosing an enforced point
4830 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4832 FaceQuadStruct::Ptr quad = *quadIt;
4833 if ( !setNormalizedGrid( *quadIt ))
4836 if ( !quad->findCell( myForcedPnts[ iFP ], i, j ))
4839 // a grid cell is found, select a node of the cell to move
4840 // to the enforced point to and to split the quad at
4841 multimap< double, pair< int, int > > ijByDist;
4842 for ( int di = 0; di < 2; ++di )
4843 for ( int dj = 0; dj < 2; ++dj )
4845 double dist2 = ( myForcedPnts[ iFP ].uv - quad->UVPt( i+di,j+dj ).UV() ).SquareModulus();
4846 ijByDist.insert( make_pair( dist2, make_pair( di,dj )));
4848 // try all nodes starting from the closest one
4849 set< FaceQuadStruct::Ptr > changedQuads;
4850 multimap< double, pair< int, int > >::iterator d2ij = ijByDist.begin();
4851 for ( ; !isNodeEnforced && d2ij != ijByDist.end(); ++d2ij )
4853 int di = d2ij->second.first;
4854 int dj = d2ij->second.second;
4856 // check if a node is at a side
4858 if ( dj== 0 && j == 0 )
4859 iSide = QUAD_BOTTOM_SIDE;
4860 else if ( dj == 1 && j+2 == quad->jSize )
4861 iSide = QUAD_TOP_SIDE;
4862 else if ( di == 0 && i == 0 )
4863 iSide = QUAD_LEFT_SIDE;
4864 else if ( di == 1 && i+2 == quad->iSize )
4865 iSide = QUAD_RIGHT_SIDE;
4867 if ( iSide > -1 ) // ----- node is at a side
4869 FaceQuadStruct::Side& side = quad->side[ iSide ];
4870 // check if this node can be moved
4871 if ( quadsBySide[ side ].size() < 2 )
4872 continue; // its a face boundary -> can't move the node
4874 int quadNodeIndex = ( iSide % 2 ) ? j : i;
4875 int sideNodeIndex = side.ToSideIndex( quadNodeIndex );
4876 if ( side.IsForced( sideNodeIndex ))
4878 // the node is already moved to another enforced point
4879 isNodeEnforced = quad->isEqual( myForcedPnts[ iFP ], i, j );
4882 // make a node of a side forced
4883 vector<UVPtStruct>& points = (vector<UVPtStruct>&) side.GetUVPtStruct();
4884 points[ sideNodeIndex ].u = myForcedPnts[ iFP ].U();
4885 points[ sideNodeIndex ].v = myForcedPnts[ iFP ].V();
4886 points[ sideNodeIndex ].node = myForcedPnts[ iFP ].node;
4888 updateSideUV( side, sideNodeIndex, quadsBySide );
4890 // update adjacent sides
4891 set< StdMeshers_FaceSidePtr > updatedSides;
4892 updatedSides.insert( side );
4893 for ( size_t i = 0; i < side.contacts.size(); ++i )
4894 if ( side.contacts[i].point == sideNodeIndex )
4896 const vector< FaceQuadStruct::Ptr >& adjQuads =
4897 quadsBySide[ *side.contacts[i].other_side ];
4898 if ( adjQuads.size() > 1 &&
4899 updatedSides.insert( * side.contacts[i].other_side ).second )
4901 updateSideUV( *side.contacts[i].other_side,
4902 side.contacts[i].other_point,
4905 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4907 const vector< FaceQuadStruct::Ptr >& adjQuads = quadsBySide[ side ];
4908 changedQuads.insert( adjQuads.begin(), adjQuads.end() );
4910 isNodeEnforced = true;
4912 else // ------------------ node is inside the quad
4916 // make a new side passing through IJ node and split the quad
4917 int indForced, iNewSide;
4918 if ( quad->iSize < quad->jSize ) // split vertically
4920 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/true );
4922 iNewSide = splitQuad( quad, i, 0 );
4926 quad->updateUV( myForcedPnts[ iFP ].uv, i, j, /*isVert=*/false );
4928 iNewSide = splitQuad( quad, 0, j );
4930 FaceQuadStruct::Ptr newQuad = myQuadList.back();
4931 FaceQuadStruct::Side& newSide = newQuad->side[ iNewSide ];
4933 vector<UVPtStruct>& points = (vector<UVPtStruct>&) newSide.GetUVPtStruct();
4934 points[ indForced ].node = myForcedPnts[ iFP ].node;
4936 newSide.forced_nodes.insert( indForced );
4937 quad->side[( iNewSide+2 ) % 4 ].forced_nodes.insert( indForced );
4939 quadsBySide[ newSide ].push_back( quad );
4940 quadsBySide[ newQuad->side[0] ].push_back( newQuad );
4941 quadsBySide[ newQuad->side[1] ].push_back( newQuad );
4942 quadsBySide[ newQuad->side[2] ].push_back( newQuad );
4943 quadsBySide[ newQuad->side[3] ].push_back( newQuad );
4945 isNodeEnforced = true;
4947 } // end of "node is inside the quad"
4949 } // loop on nodes of the cell
4951 // remove out-of-date uv grid of changedQuads
4952 set< FaceQuadStruct::Ptr >::iterator qIt = changedQuads.begin();
4953 for ( ; qIt != changedQuads.end(); ++qIt )
4954 (*qIt)->uv_grid.clear();
4956 if ( isNodeEnforced )
4961 if ( !isNodeEnforced )
4963 if ( !myForcedPnts[ iFP ].vertex.IsNull() )
4964 return error(TComm("Unable to move any node to vertex #")
4965 <<myHelper->GetMeshDS()->ShapeToIndex( myForcedPnts[ iFP ].vertex ));
4967 return error(TComm("Unable to move any node to point ( ")
4968 << myForcedPnts[iFP].xyz.X() << ", "
4969 << myForcedPnts[iFP].xyz.Y() << ", "
4970 << myForcedPnts[iFP].xyz.Z() << " )");
4972 myNeedSmooth = true;
4974 } // loop on enforced points
4976 // Compute nodes on all sides, where not yet present
4978 for ( quadIt = myQuadList.begin(); quadIt != myQuadList.end(); ++quadIt )
4980 FaceQuadStruct::Ptr quad = *quadIt;
4981 for ( int iSide = 0; iSide < 4; ++iSide )
4983 FaceQuadStruct::Side & side = quad->side[ iSide ];
4984 if ( side.nbNodeOut > 0 )
4985 continue; // emulated side
4986 vector< FaceQuadStruct::Ptr >& quadVec = quadsBySide[ side ];
4987 if ( quadVec.size() <= 1 )
4988 continue; // outer side
4990 const vector<UVPtStruct>& points = side.grid->GetUVPtStruct();
4991 for ( size_t iC = 0; iC < side.contacts.size(); ++iC )
4993 if ( side.contacts[iC].point < side.from ||
4994 side.contacts[iC].point >= side.to )
4996 if ( side.contacts[iC].other_point < side.contacts[iC].other_side->from ||
4997 side.contacts[iC].other_point >= side.contacts[iC].other_side->to )
4999 const vector<UVPtStruct>& oGrid = side.contacts[iC].other_side->grid->GetUVPtStruct();
5000 const UVPtStruct& uvPt = points[ side.contacts[iC].point ];
5001 if ( side.contacts[iC].other_point >= (int) oGrid .size() ||
5002 side.contacts[iC].point >= (int) points.size() )
5003 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::addEnforcedNodes(): wrong contact" );
5004 if ( oGrid[ side.contacts[iC].other_point ].node )
5005 (( UVPtStruct& ) uvPt).node = oGrid[ side.contacts[iC].other_point ].node;
5008 bool missedNodesOnSide = false;
5009 for ( size_t iP = 0; iP < points.size(); ++iP )
5010 if ( !points[ iP ].node )
5012 UVPtStruct& uvPnt = ( UVPtStruct& ) points[ iP ];
5013 gp_Pnt P = surf->Value( uvPnt.u, uvPnt.v );
5014 uvPnt.node = myHelper->AddNode(P.X(), P.Y(), P.Z(), 0, uvPnt.u, uvPnt.v );
5015 missedNodesOnSide = true;
5017 if ( missedNodesOnSide )
5019 // clear uv_grid where nodes are missing
5020 for ( size_t iQ = 0; iQ < quadVec.size(); ++iQ )
5021 quadVec[ iQ ]->uv_grid.clear();
5029 //================================================================================
5031 * \brief Splits a quad at I or J. Returns an index of a new side in the new quad
5033 //================================================================================
5035 int StdMeshers_Quadrangle_2D::splitQuad(FaceQuadStruct::Ptr quad, int I, int J)
5037 FaceQuadStruct* newQuad = new FaceQuadStruct( quad->face );
5038 myQuadList.push_back( FaceQuadStruct::Ptr( newQuad ));
5040 vector<UVPtStruct> points;
5041 if ( I > 0 && I <= quad->iSize-2 )
5043 points.reserve( quad->jSize );
5044 for ( int jP = 0; jP < quad->jSize; ++jP )
5045 points.push_back( quad->UVPt( I, jP ));
5047 newQuad->side.resize( 4 );
5048 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
5049 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
5050 newQuad->side[ QUAD_TOP_SIDE ] = quad->side[ QUAD_TOP_SIDE ];
5051 newQuad->side[ QUAD_LEFT_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
5053 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_LEFT_SIDE ];
5054 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_RIGHT_SIDE ];
5056 quad->side[ QUAD_RIGHT_SIDE ] = newSide;
5058 int iBot = quad->side[ QUAD_BOTTOM_SIDE ].ToSideIndex( I );
5059 int iTop = quad->side[ QUAD_TOP_SIDE ].ToSideIndex( I );
5061 newSide.AddContact ( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
5062 newSide2.AddContact( 0, & quad->side[ QUAD_BOTTOM_SIDE ], iBot );
5063 newSide.AddContact ( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
5064 newSide2.AddContact( quad->jSize - 1, & quad->side[ QUAD_TOP_SIDE ], iTop );
5065 // cout << "Contact: L " << &newSide << " "<< newSide.NbPoints()
5066 // << " R " << &newSide2 << " "<< newSide2.NbPoints()
5067 // << " B " << &quad->side[ QUAD_BOTTOM_SIDE ] << " "<< quad->side[ QUAD_BOTTOM_SIDE].NbPoints()
5068 // << " T " << &quad->side[ QUAD_TOP_SIDE ] << " "<< quad->side[ QUAD_TOP_SIDE].NbPoints()<< endl;
5070 newQuad->side[ QUAD_BOTTOM_SIDE ].from = iBot;
5071 newQuad->side[ QUAD_TOP_SIDE ].from = iTop;
5072 newQuad->name = ( TComm("Right of I=") << I );
5074 bool bRev = quad->side[ QUAD_BOTTOM_SIDE ].IsReversed();
5075 bool tRev = quad->side[ QUAD_TOP_SIDE ].IsReversed();
5076 quad->side[ QUAD_BOTTOM_SIDE ].to = iBot + ( bRev ? -1 : +1 );
5077 quad->side[ QUAD_TOP_SIDE ].to = iTop + ( tRev ? -1 : +1 );
5078 quad->uv_grid.clear();
5080 return QUAD_LEFT_SIDE;
5082 else if ( J > 0 && J <= quad->jSize-2 ) //// split horizontally, a new quad is below an old one
5084 points.reserve( quad->iSize );
5085 for ( int iP = 0; iP < quad->iSize; ++iP )
5086 points.push_back( quad->UVPt( iP, J ));
5088 newQuad->side.resize( 4 );
5089 newQuad->side[ QUAD_BOTTOM_SIDE ] = quad->side[ QUAD_BOTTOM_SIDE ];
5090 newQuad->side[ QUAD_RIGHT_SIDE ] = quad->side[ QUAD_RIGHT_SIDE ];
5091 newQuad->side[ QUAD_TOP_SIDE ] = StdMeshers_FaceSide::New( points, quad->face );
5092 newQuad->side[ QUAD_LEFT_SIDE ] = quad->side[ QUAD_LEFT_SIDE ];
5094 FaceQuadStruct::Side& newSide = newQuad->side[ QUAD_TOP_SIDE ];
5095 FaceQuadStruct::Side& newSide2 = quad->side [ QUAD_BOTTOM_SIDE ];
5097 quad->side[ QUAD_BOTTOM_SIDE ] = newSide;
5099 int iLft = quad->side[ QUAD_LEFT_SIDE ].ToSideIndex( J );
5100 int iRgt = quad->side[ QUAD_RIGHT_SIDE ].ToSideIndex( J );
5102 newSide.AddContact ( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5103 newSide2.AddContact( 0, & quad->side[ QUAD_LEFT_SIDE ], iLft );
5104 newSide.AddContact ( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5105 newSide2.AddContact( quad->iSize - 1, & quad->side[ QUAD_RIGHT_SIDE ], iRgt );
5106 // cout << "Contact: T " << &newSide << " "<< newSide.NbPoints()
5107 // << " B " << &newSide2 << " "<< newSide2.NbPoints()
5108 // << " L " << &quad->side[ QUAD_LEFT_SIDE ] << " "<< quad->side[ QUAD_LEFT_SIDE].NbPoints()
5109 // << " R " << &quad->side[ QUAD_RIGHT_SIDE ] << " "<< quad->side[ QUAD_RIGHT_SIDE].NbPoints()<< endl;
5111 bool rRev = newQuad->side[ QUAD_RIGHT_SIDE ].IsReversed();
5112 bool lRev = newQuad->side[ QUAD_LEFT_SIDE ].IsReversed();
5113 newQuad->side[ QUAD_RIGHT_SIDE ].to = iRgt + ( rRev ? -1 : +1 );
5114 newQuad->side[ QUAD_LEFT_SIDE ].to = iLft + ( lRev ? -1 : +1 );
5115 newQuad->name = ( TComm("Below J=") << J );
5117 quad->side[ QUAD_RIGHT_SIDE ].from = iRgt;
5118 quad->side[ QUAD_LEFT_SIDE ].from = iLft;
5119 quad->uv_grid.clear();
5121 return QUAD_TOP_SIDE;
5124 myQuadList.pop_back();
5128 //================================================================================
5130 * \brief Updates UV of a side after moving its node
5132 //================================================================================
5134 void StdMeshers_Quadrangle_2D::updateSideUV( FaceQuadStruct::Side& side,
5136 const TQuadsBySide& quadsBySide,
5141 side.forced_nodes.insert( iForced );
5143 // update parts of the side before and after iForced
5145 set<int>::iterator iIt = side.forced_nodes.upper_bound( iForced );
5146 int iEnd = Min( side.NbPoints()-1, ( iIt == side.forced_nodes.end() ) ? int(1e7) : *iIt );
5147 if ( iForced + 1 < iEnd )
5148 updateSideUV( side, iForced, quadsBySide, &iEnd );
5150 iIt = side.forced_nodes.lower_bound( iForced );
5151 int iBeg = Max( 0, ( iIt == side.forced_nodes.begin() ) ? 0 : *--iIt );
5152 if ( iForced - 1 > iBeg )
5153 updateSideUV( side, iForced, quadsBySide, &iBeg );
5158 const int iFrom = Min ( iForced, *iNext );
5159 const int iTo = Max ( iForced, *iNext ) + 1;
5160 const size_t sideSize = iTo - iFrom;
5162 vector<UVPtStruct> points[4]; // side points of a temporary quad
5164 // from the quads get grid points adjacent to the side
5165 // to make two sides of a temporary quad
5166 vector< FaceQuadStruct::Ptr > quads = quadsBySide.find( side )->second; // copy!
5167 for ( int is2nd = 0; is2nd < 2; ++is2nd )
5169 points[ is2nd ].reserve( sideSize );
5171 while ( points[is2nd].size() < sideSize )
5173 int iCur = iFrom + points[is2nd].size() - int( !points[is2nd].empty() );
5175 // look for a quad adjacent to iCur-th point of the side
5176 for ( size_t iQ = 0; iQ < quads.size(); ++iQ )
5178 FaceQuadStruct::Ptr q = quads[ iQ ];
5182 for ( iS = 0; iS < q->side.size(); ++iS )
5183 if ( side.grid == q->side[ iS ].grid )
5185 if ( iS == q->side.size() )
5188 if ( !q->side[ iS ].IsReversed() )
5189 isOut = ( q->side[ iS ].from > iCur || q->side[ iS ].to-1 <= iCur );
5191 isOut = ( q->side[ iS ].to >= iCur || q->side[ iS ].from <= iCur );
5194 if ( !setNormalizedGrid( q ))
5197 // found - copy points
5199 if ( iS % 2 ) // right or left
5201 i = ( iS == QUAD_LEFT_SIDE ) ? 1 : q->iSize-2;
5202 j = q->side[ iS ].ToQuadIndex( iCur );
5204 dj = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5205 nb = ( q->side[ iS ].IsReversed() ) ? j+1 : q->jSize-j;
5207 else // bottom or top
5209 i = q->side[ iS ].ToQuadIndex( iCur );
5210 j = ( iS == QUAD_BOTTOM_SIDE ) ? 1 : q->jSize-2;
5211 di = ( q->side[ iS ].IsReversed() ) ? -1 : +1;
5213 nb = ( q->side[ iS ].IsReversed() ) ? i+1 : q->iSize-i;
5215 if ( !points[is2nd].empty() )
5217 gp_UV lastUV = points[is2nd].back().UV();
5218 gp_UV quadUV = q->UVPt( i, j ).UV();
5219 if ( ( lastUV - quadUV ).SquareModulus() > 1e-10 )
5220 continue; // quad is on the other side of the side
5221 i += di; j += dj; --nb;
5223 for ( ; nb > 0 ; --nb )
5225 points[ is2nd ].push_back( q->UVPt( i, j ));
5226 if ( points[is2nd].size() >= sideSize )
5230 quads[ iQ ].reset(); // not to use this quad anymore
5232 if ( points[is2nd].size() >= sideSize )
5236 if ( nbLoops++ > quads.size() )
5237 throw SALOME_Exception( "StdMeshers_Quadrangle_2D::updateSideUV() bug: infinite loop" );
5239 } // while ( points[is2nd].size() < sideSize )
5240 } // two loops to fill points[0] and points[1]
5242 // points for other pair of opposite sides of the temporary quad
5244 enum { L,R,B,T }; // side index of points[]
5246 points[B].push_back( points[L].front() );
5247 points[B].push_back( side.GetUVPtStruct()[ iFrom ]);
5248 points[B].push_back( points[R].front() );
5250 points[T].push_back( points[L].back() );
5251 points[T].push_back( side.GetUVPtStruct()[ iTo-1 ]);
5252 points[T].push_back( points[R].back() );
5254 // make the temporary quad
5255 FaceQuadStruct::Ptr tmpQuad
5256 ( new FaceQuadStruct( TopoDS::Face( myHelper->GetSubShape() ), "tmpQuad"));
5257 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[B] )); // bottom
5258 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[R] )); // right
5259 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[T] ));
5260 tmpQuad->side.push_back( StdMeshers_FaceSide::New( points[L] ));
5262 // compute new UV of the side
5263 setNormalizedGrid( tmpQuad );
5264 gp_UV uv = tmpQuad->UVPt(1,0).UV();
5265 tmpQuad->updateUV( uv, 1,0, /*isVertical=*/true );
5267 // update UV of the side
5268 vector<UVPtStruct>& sidePoints = (vector<UVPtStruct>&) side.GetUVPtStruct();
5269 for ( int i = iFrom; i < iTo; ++i )
5271 const uvPtStruct& uvPt = tmpQuad->UVPt( 1, i-iFrom );
5272 sidePoints[ i ].u = uvPt.u;
5273 sidePoints[ i ].v = uvPt.v;
5277 //================================================================================
5279 * \brief Finds indices of a grid quad enclosing the given enforced UV
5281 //================================================================================
5283 bool FaceQuadStruct::findCell( const gp_XY& UV, int & I, int & J )
5285 // setNormalizedGrid() must be called before!
5286 if ( uv_box.IsOut( UV ))
5289 // find an approximate position
5290 double x = 0.5, y = 0.5;
5291 gp_XY t0 = UVPt( iSize - 1, 0 ).UV();
5292 gp_XY t1 = UVPt( 0, jSize - 1 ).UV();
5293 gp_XY t2 = UVPt( 0, 0 ).UV();
5294 SMESH_MeshAlgos::GetBarycentricCoords( UV, t0, t1, t2, x, y );
5295 x = Min( 1., Max( 0., x ));
5296 y = Min( 1., Max( 0., y ));
5298 // precise the position
5299 normPa2IJ( x,y, I,J );
5300 if ( !isNear( UV, I,J ))
5302 // look for the most close IJ by traversing uv_grid in the middle
5303 double dist2, minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5304 for ( int isU = 0; isU < 2; ++isU )
5306 int ind1 = isU ? 0 : iSize / 2;
5307 int ind2 = isU ? jSize / 2 : 0;
5308 int di1 = isU ? Max( 2, iSize / 20 ) : 0;
5309 int di2 = isU ? 0 : Max( 2, jSize / 20 );
5310 int i,nb = isU ? iSize / di1 : jSize / di2;
5311 for ( i = 0; i < nb; ++i, ind1 += di1, ind2 += di2 )
5312 if (( dist2 = ( UV - UVPt( ind1,ind2 ).UV() ).SquareModulus() ) < minDist2 )
5316 if ( isNear( UV, I,J ))
5318 minDist2 = ( UV - UVPt( I,J ).UV() ).SquareModulus();
5321 if ( !isNear( UV, I,J, Max( iSize, jSize ) /2 ))
5327 //================================================================================
5329 * \brief Find indices (i,j) of a point in uv_grid by normalized parameters (x,y)
5331 //================================================================================
5333 void FaceQuadStruct::normPa2IJ(double X, double Y, int & I, int & J )
5336 I = Min( int ( iSize * X ), iSize - 2 );
5337 J = Min( int ( jSize * Y ), jSize - 2 );
5343 while ( X <= UVPt( I,J ).x && I != 0 )
5345 while ( X > UVPt( I+1,J ).x && I+2 < iSize )
5347 while ( Y <= UVPt( I,J ).y && J != 0 )
5349 while ( Y > UVPt( I,J+1 ).y && J+2 < jSize )
5351 } while ( oldI != I || oldJ != J );
5354 //================================================================================
5356 * \brief Looks for UV in quads around a given (I,J) and precise (I,J)
5358 //================================================================================
5360 bool FaceQuadStruct::isNear( const gp_XY& UV, int & I, int & J, int nbLoops )
5362 if ( I+1 >= iSize ) I = iSize - 2;
5363 if ( J+1 >= jSize ) J = jSize - 2;
5366 gp_XY uvI, uvJ, uv0, uv1;
5367 for ( int iLoop = 0; iLoop < nbLoops; ++iLoop )
5369 int oldI = I, oldJ = J;
5371 uvI = UVPt( I+1, J ).UV();
5372 uvJ = UVPt( I, J+1 ).UV();
5373 uv0 = UVPt( I, J ).UV();
5374 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5375 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5378 if ( I > 0 && bcI < 0. ) --I;
5379 if ( I+2 < iSize && bcI > 1. ) ++I;
5380 if ( J > 0 && bcJ < 0. ) --J;
5381 if ( J+2 < jSize && bcJ > 1. ) ++J;
5383 uv1 = UVPt( I+1,J+1).UV();
5384 if ( I != oldI || J != oldJ )
5386 uvI = UVPt( I+1, J ).UV();
5387 uvJ = UVPt( I, J+1 ).UV();
5389 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5390 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5393 if ( I > 0 && bcI > 1. ) --I;
5394 if ( I+2 < iSize && bcI < 0. ) ++I;
5395 if ( J > 0 && bcJ > 1. ) --J;
5396 if ( J+2 < jSize && bcJ < 0. ) ++J;
5398 if ( I == oldI && J == oldJ )
5401 if ( iLoop+1 == nbLoops )
5403 uvI = UVPt( I+1, J ).UV();
5404 uvJ = UVPt( I, J+1 ).UV();
5405 uv0 = UVPt( I, J ).UV();
5406 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv0, bcI, bcJ );
5407 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5410 uv1 = UVPt( I+1,J+1).UV();
5411 SMESH_MeshAlgos::GetBarycentricCoords( UV, uvI, uvJ, uv1, bcI, bcJ );
5412 if ( bcI >= 0. && bcJ >= 0. && bcI + bcJ <= 1.)
5419 //================================================================================
5421 * \brief Checks if a given UV is equal to a given grid point
5423 //================================================================================
5425 bool FaceQuadStruct::isEqual( const gp_XY& UV, int I, int J )
5427 TopLoc_Location loc;
5428 Handle(Geom_Surface) surf = BRep_Tool::Surface( face, loc );
5429 gp_Pnt p1 = surf->Value( UV.X(), UV.Y() );
5430 gp_Pnt p2 = surf->Value( UVPt( I,J ).u, UVPt( I,J ).v );
5432 double dist2 = 1e100;
5433 for ( int di = -1; di < 2; di += 2 )
5436 if ( i < 0 || i+1 >= iSize ) continue;
5437 for ( int dj = -1; dj < 2; dj += 2 )
5440 if ( j < 0 || j+1 >= jSize ) continue;
5443 p2.SquareDistance( surf->Value( UVPt( i,j ).u, UVPt( i,j ).v )));
5446 double tol2 = dist2 / 1000.;
5447 return p1.SquareDistance( p2 ) < tol2;
5450 //================================================================================
5452 * \brief Recompute UV of grid points around a moved point in one direction
5454 //================================================================================
5456 void FaceQuadStruct::updateUV( const gp_XY& UV, int I, int J, bool isVertical )
5458 UVPt( I, J ).u = UV.X();
5459 UVPt( I, J ).v = UV.Y();
5464 if ( J+1 < jSize-1 )
5466 gp_UV a0 = UVPt( 0, J ).UV();
5467 gp_UV a1 = UVPt( iSize-1, J ).UV();
5468 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5469 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5471 gp_UV p0 = UVPt( I, J ).UV();
5472 gp_UV p2 = UVPt( I, jSize-1 ).UV();
5473 const double y0 = UVPt( I, J ).y, dy = 1. - y0;
5474 for (int j = J+1; j < jSize-1; j++)
5476 gp_UV p1 = UVPt( iSize-1, j ).UV();
5477 gp_UV p3 = UVPt( 0, j ).UV();
5479 UVPtStruct& uvPt = UVPt( I, j );
5480 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5488 gp_UV a0 = UVPt( 0, 0 ).UV();
5489 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5490 gp_UV a2 = UVPt( iSize-1, J ).UV();
5491 gp_UV a3 = UVPt( 0, J ).UV();
5493 gp_UV p0 = UVPt( I, 0 ).UV();
5494 gp_UV p2 = UVPt( I, J ).UV();
5495 const double y0 = 0., dy = UVPt( I, J ).y - y0;
5496 for (int j = 1; j < J; j++)
5498 gp_UV p1 = UVPt( iSize-1, j ).UV();
5499 gp_UV p3 = UVPt( 0, j ).UV();
5501 UVPtStruct& uvPt = UVPt( I, j );
5502 gp_UV uv = calcUV( uvPt.x, ( uvPt.y - y0 ) / dy, a0,a1,a2,a3, p0,p1,p2,p3);
5508 else // horizontally
5513 gp_UV a0 = UVPt( 0, 0 ).UV();
5514 gp_UV a1 = UVPt( I, 0 ).UV();
5515 gp_UV a2 = UVPt( I, jSize-1 ).UV();
5516 gp_UV a3 = UVPt( 0, jSize-1 ).UV();
5518 gp_UV p1 = UVPt( I, J ).UV();
5519 gp_UV p3 = UVPt( 0, J ).UV();
5520 const double x0 = 0., dx = UVPt( I, J ).x - x0;
5521 for (int i = 1; i < I; i++)
5523 gp_UV p0 = UVPt( i, 0 ).UV();
5524 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5526 UVPtStruct& uvPt = UVPt( i, J );
5527 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5533 if ( I+1 < iSize-1 )
5535 gp_UV a0 = UVPt( I, 0 ).UV();
5536 gp_UV a1 = UVPt( iSize-1, 0 ).UV();
5537 gp_UV a2 = UVPt( iSize-1, jSize-1 ).UV();
5538 gp_UV a3 = UVPt( I, jSize-1 ).UV();
5540 gp_UV p1 = UVPt( iSize-1, J ).UV();
5541 gp_UV p3 = UVPt( I, J ).UV();
5542 const double x0 = UVPt( I, J ).x, dx = 1. - x0;
5543 for (int i = I+1; i < iSize-1; i++)
5545 gp_UV p0 = UVPt( i, 0 ).UV();
5546 gp_UV p2 = UVPt( i, jSize-1 ).UV();
5548 UVPtStruct& uvPt = UVPt( i, J );
5549 gp_UV uv = calcUV(( uvPt.x - x0 ) / dx , uvPt.y, a0,a1,a2,a3, p0,p1,p2,p3);
5557 //================================================================================
5559 * \brief Side copying
5561 //================================================================================
5563 FaceQuadStruct::Side& FaceQuadStruct::Side::operator=(const Side& otherSide)
5565 grid = otherSide.grid;
5566 from = otherSide.from;
5569 forced_nodes = otherSide.forced_nodes;
5570 contacts = otherSide.contacts;
5571 nbNodeOut = otherSide.nbNodeOut;
5573 for ( size_t iC = 0; iC < contacts.size(); ++iC )
5575 FaceQuadStruct::Side* oSide = contacts[iC].other_side;
5576 for ( size_t iOC = 0; iOC < oSide->contacts.size(); ++iOC )
5577 if ( oSide->contacts[iOC].other_side == & otherSide )
5579 // cout << "SHIFT old " << &otherSide << " " << otherSide.NbPoints()
5580 // << " -> new " << this << " " << this->NbPoints() << endl;
5581 oSide->contacts[iOC].other_side = this;
5587 //================================================================================
5589 * \brief Converts node index of a quad to node index of this side
5591 //================================================================================
5593 int FaceQuadStruct::Side::ToSideIndex( int quadNodeIndex ) const
5595 return from + di * quadNodeIndex;
5598 //================================================================================
5600 * \brief Converts node index of this side to node index of a quad
5602 //================================================================================
5604 int FaceQuadStruct::Side::ToQuadIndex( int sideNodeIndex ) const
5606 return ( sideNodeIndex - from ) * di;
5609 //================================================================================
5611 * \brief Reverse the side
5613 //================================================================================
5615 bool FaceQuadStruct::Side::Reverse(bool keepGrid)
5623 std::swap( from, to );
5634 //================================================================================
5636 * \brief Checks if a node is enforced
5637 * \param [in] nodeIndex - an index of a node in a size
5638 * \return bool - \c true if the node is forced
5640 //================================================================================
5642 bool FaceQuadStruct::Side::IsForced( int nodeIndex ) const
5644 if ( nodeIndex < 0 || nodeIndex >= grid->NbPoints() )
5645 throw SALOME_Exception( " FaceQuadStruct::Side::IsForced(): wrong index" );
5647 if ( forced_nodes.count( nodeIndex ) )
5650 for ( size_t i = 0; i < this->contacts.size(); ++i )
5651 if ( contacts[ i ].point == nodeIndex &&
5652 contacts[ i ].other_side->forced_nodes.count( contacts[ i ].other_point ))
5658 //================================================================================
5660 * \brief Sets up a contact between this and another side
5662 //================================================================================
5664 void FaceQuadStruct::Side::AddContact( int ip, Side* side, int iop )
5666 if ( ip >= (int) GetUVPtStruct().size() ||
5667 iop >= (int) side->GetUVPtStruct().size() )
5668 throw SALOME_Exception( "FaceQuadStruct::Side::AddContact(): wrong point" );
5669 if ( ip < from || ip >= to )
5672 contacts.resize( contacts.size() + 1 );
5673 Contact& c = contacts.back();
5675 c.other_side = side;
5676 c.other_point = iop;
5679 side->contacts.resize( side->contacts.size() + 1 );
5680 Contact& c = side->contacts.back();
5682 c.other_side = this;
5687 //================================================================================
5689 * \brief Returns a normalized parameter of a point indexed within a quadrangle
5691 //================================================================================
5693 double FaceQuadStruct::Side::Param( int i ) const
5695 const vector<UVPtStruct>& points = GetUVPtStruct();
5696 return (( points[ from + i * di ].normParam - points[ from ].normParam ) /
5697 ( points[ to - 1 * di ].normParam - points[ from ].normParam ));
5700 //================================================================================
5702 * \brief Returns UV by a parameter normalized within a quadrangle
5704 //================================================================================
5706 gp_XY FaceQuadStruct::Side::Value2d( double x ) const
5708 const vector<UVPtStruct>& points = GetUVPtStruct();
5709 double u = ( points[ from ].normParam +
5710 x * ( points[ to-di ].normParam - points[ from ].normParam ));
5711 return grid->Value2d( u ).XY();
5714 //================================================================================
5716 * \brief Returns side length
5718 //================================================================================
5720 double FaceQuadStruct::Side::Length(int theFrom, int theTo) const
5722 if ( IsReversed() != ( theTo < theFrom ))
5723 std::swap( theTo, theFrom );
5725 const vector<UVPtStruct>& points = GetUVPtStruct();
5727 if ( theFrom == theTo && theTo == -1 )
5728 r = Abs( First().normParam -
5729 Last ().normParam );
5730 else if ( IsReversed() )
5731 r = Abs( points[ Max( to, theTo+1 ) ].normParam -
5732 points[ Min( from, theFrom ) ].normParam );
5734 r = Abs( points[ Min( to, theTo-1 ) ].normParam -
5735 points[ Max( from, theFrom ) ].normParam );
5736 return r * grid->Length();