1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshers_Quadrangle_2D.cxx
25 // Moved here from SMESH_Quadrangle_2D.cxx
26 // Author : Paul RASCLE, EDF
31 #include "StdMeshers_Quadrangle_2D.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_subMesh.hxx"
36 #include "SMDS_MeshElement.hxx"
37 #include "SMDS_MeshNode.hxx"
38 #include "SMDS_EdgePosition.hxx"
39 #include "SMDS_FacePosition.hxx"
41 #include <BRep_Tool.hxx>
42 #include <BRepTools.hxx>
43 #include <BRepTools_WireExplorer.hxx>
45 #include <Geom_Surface.hxx>
46 #include <Geom_Curve.hxx>
47 #include <Geom2d_Curve.hxx>
48 #include <GeomAdaptor_Curve.hxx>
49 #include <GCPnts_UniformAbscissa.hxx>
51 #include <Precision.hxx>
52 #include <gp_Pnt2d.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include "utilities.h"
56 #include "Utils_ExceptHandlers.hxx"
59 //=============================================================================
63 //=============================================================================
65 StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D (int hypId, int studyId, SMESH_Gen* gen)
66 : SMESH_2D_Algo(hypId, studyId, gen)
68 MESSAGE("StdMeshers_Quadrangle_2D::StdMeshers_Quadrangle_2D");
69 _name = "Quadrangle_2D";
70 // _shapeType = TopAbs_FACE;
71 _shapeType = (1 << TopAbs_FACE);
74 //=============================================================================
78 //=============================================================================
80 StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D()
82 MESSAGE("StdMeshers_Quadrangle_2D::~StdMeshers_Quadrangle_2D");
85 //=============================================================================
89 //=============================================================================
91 bool StdMeshers_Quadrangle_2D::CheckHypothesis
93 const TopoDS_Shape& aShape,
94 SMESH_Hypothesis::Hypothesis_Status& aStatus)
96 //MESSAGE("StdMeshers_Quadrangle_2D::CheckHypothesis");
99 aStatus = SMESH_Hypothesis::HYP_OK;
106 //=============================================================================
110 //=============================================================================
112 bool StdMeshers_Quadrangle_2D::Compute (SMESH_Mesh& aMesh,
113 const TopoDS_Shape& aShape) throw (SALOME_Exception)
115 Unexpect aCatch(SalomeException);
116 //MESSAGE("StdMeshers_Quadrangle_2D::Compute");
117 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
118 aMesh.GetSubMesh(aShape);
120 FaceQuadStruct *quad = CheckAnd2Dcompute(aMesh, aShape);
124 // --- compute 3D values on points, store points & quadrangles
126 int nbdown = quad->nbPts[0];
127 int nbup = quad->nbPts[2];
128 // bool isDownOut = (nbdown > nbup);
129 // bool isUpOut = (nbdown < nbup);
131 int nbright = quad->nbPts[1];
132 int nbleft = quad->nbPts[3];
133 // bool isRightOut = (nbright > nbleft);
134 // bool isLeftOut = (nbright < nbleft);
136 int nbhoriz = Min(nbdown, nbup);
137 int nbvertic = Min(nbright, nbleft);
139 //int nbVertices = nbhoriz * nbvertic;
140 //int nbQuad = (nbhoriz - 1) * (nbvertic - 1);
141 //SCRUTE(nbVertices);
144 // const TopoDS_Face& FF = TopoDS::Face(aShape);
145 // bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
146 // TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
147 const TopoDS_Face& F = TopoDS::Face(aShape);
148 //bool faceIsForward = (F.Orientation() == TopAbs_FORWARD);
149 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
151 // internal mesh nodes
152 int i, j, faceID = meshDS->ShapeToIndex( F );
153 for (i = 1; i < nbhoriz - 1; i++) {
154 for (j = 1; j < nbvertic - 1; j++) {
155 int ij = j * nbhoriz + i;
156 double u = quad->uv_grid[ij].u;
157 double v = quad->uv_grid[ij].v;
158 gp_Pnt P = S->Value(u, v);
159 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
160 meshDS->SetNodeOnFace(node, faceID, u, v);
161 quad->uv_grid[ij].node = node;
168 // --.--.--.--.--.-- nbvertic
174 // ---.----.----.--- 0
175 // 0 > > > > > > > > nbhoriz
181 int iup = nbhoriz - 1;
182 if (quad->isEdgeOut[3]) { ilow++; } else { if (quad->isEdgeOut[1]) iup--; }
185 int jup = nbvertic - 1;
186 if (quad->isEdgeOut[0]) { jlow++; } else { if (quad->isEdgeOut[2]) jup--; }
188 // regular quadrangles
189 // bool isQuadForward = ( faceIsForward == quad->isEdgeForward[0]);
190 for (i = ilow; i < iup; i++) {
191 for (j = jlow; j < jup; j++) {
192 const SMDS_MeshNode *a, *b, *c, *d;
193 a = quad->uv_grid[j * nbhoriz + i].node;
194 b = quad->uv_grid[j * nbhoriz + i + 1].node;
195 c = quad->uv_grid[(j + 1) * nbhoriz + i + 1].node;
196 d = quad->uv_grid[(j + 1) * nbhoriz + i].node;
197 SMDS_MeshFace * face = meshDS->AddFace(a, b, c, d);
198 meshDS->SetMeshElementOnShape(face, faceID);
202 UVPtStruct *uv_e0 = quad->uv_edges[0];
203 UVPtStruct *uv_e1 = quad->uv_edges[1];
204 UVPtStruct *uv_e2 = quad->uv_edges[2];
205 UVPtStruct *uv_e3 = quad->uv_edges[3];
207 double eps = Precision::Confusion();
209 // Boundary quadrangles
211 if (quad->isEdgeOut[0]) {
214 // |___|___|___|___|___|___|
216 // |___|___|___|___|___|___|
218 // |___|___|___|___|___|___| __ first row of the regular grid
219 // . . . . . . . . . __ down edge nodes
221 // >->->->->->->->->->->->-> -- direction of processing
223 int g = 0; // number of last processed node in the regular grid
225 // number of last node of the down edge to be processed
226 int stop = nbdown - 1;
227 // if right edge is out, we will stop at a node, previous to the last one
228 if (quad->isEdgeOut[1]) stop--;
230 // for each node of the down edge find nearest node
231 // in the first row of the regular grid and link them
232 for (i = 0; i < stop; i++) {
233 const SMDS_MeshNode *a, *b, *c, *d;
235 b = uv_e0[i + 1].node;
236 gp_Pnt pb (b->X(), b->Y(), b->Z());
238 // find node c in the regular grid, which will be linked with node b
241 // right bound reached, link with the rightmost node
243 c = quad->uv_grid[nbhoriz + iup].node;
245 // find in the grid node c, nearest to the b
246 double mind = RealLast();
247 for (int k = g; k <= iup; k++) {
249 const SMDS_MeshNode *nk;
250 if (k < ilow) // this can be, if left edge is out
251 nk = uv_e3[1].node; // get node from the left edge
253 nk = quad->uv_grid[nbhoriz + k].node; // get one of middle nodes
255 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
256 double dist = pb.Distance(pnk);
257 if (dist < mind - eps) {
267 if (near == g) { // make triangle
268 SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
269 meshDS->SetMeshElementOnShape(face, F);
270 } else { // make quadrangle
274 d = quad->uv_grid[nbhoriz + near - 1].node;
275 SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
276 meshDS->SetMeshElementOnShape(face, faceID);
278 // if node d is not at position g - make additional triangles
280 for (int k = near - 1; k > g; k--) {
281 c = quad->uv_grid[nbhoriz + k].node;
285 d = quad->uv_grid[nbhoriz + k - 1].node;
286 SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
287 meshDS->SetMeshElementOnShape(face, faceID);
294 if (quad->isEdgeOut[2]) {
297 // <-<-<-<-<-<-<-<-<-<-<-<-< -- direction of processing
299 // . . . . . . . . . __ up edge nodes
300 // ___ ___ ___ ___ ___ ___ __ first row of the regular grid
302 // |___|___|___|___|___|___|
304 // |___|___|___|___|___|___|
307 int g = nbhoriz - 1; // last processed node in the regular grid
310 // if left edge is out, we will stop at a second node
311 if (quad->isEdgeOut[3]) stop++;
313 // for each node of the up edge find nearest node
314 // in the first row of the regular grid and link them
315 for (i = nbup - 1; i > stop; i--) {
316 const SMDS_MeshNode *a, *b, *c, *d;
318 b = uv_e2[i - 1].node;
319 gp_Pnt pb (b->X(), b->Y(), b->Z());
321 // find node c in the grid, which will be linked with node b
323 if (i == stop + 1) { // left bound reached, link with the leftmost node
324 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + ilow].node;
327 // find node c in the grid, nearest to the b
328 double mind = RealLast();
329 for (int k = g; k >= ilow; k--) {
330 const SMDS_MeshNode *nk;
332 nk = uv_e1[nbright - 2].node;
334 nk = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
335 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
336 double dist = pb.Distance(pnk);
337 if (dist < mind - eps) {
347 if (near == g) { // make triangle
348 SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
349 meshDS->SetMeshElementOnShape(face, faceID);
350 } else { // make quadrangle
352 d = uv_e1[nbright - 2].node;
354 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + near + 1].node;
355 SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
356 meshDS->SetMeshElementOnShape(face, faceID);
358 if (near + 1 < g) { // if d not is at g - make additional triangles
359 for (int k = near + 1; k < g; k++) {
360 c = quad->uv_grid[nbhoriz*(nbvertic - 2) + k].node;
362 d = uv_e1[nbright - 2].node;
364 d = quad->uv_grid[nbhoriz*(nbvertic - 2) + k + 1].node;
365 SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
366 meshDS->SetMeshElementOnShape(face, faceID);
375 // right or left boundary quadrangles
376 if (quad->isEdgeOut[1]) {
377 // MESSAGE("right edge is out");
378 int g = 0; // last processed node in the grid
379 int stop = nbright - 1;
380 if (quad->isEdgeOut[2]) stop--;
381 for (i = 0; i < stop; i++) {
382 const SMDS_MeshNode *a, *b, *c, *d;
384 b = uv_e1[i + 1].node;
385 gp_Pnt pb (b->X(), b->Y(), b->Z());
387 // find node c in the grid, nearest to the b
389 if (i == stop - 1) { // up bondary reached
390 c = quad->uv_grid[nbhoriz*(jup + 1) - 2].node;
393 double mind = RealLast();
394 for (int k = g; k <= jup; k++) {
395 const SMDS_MeshNode *nk;
397 nk = uv_e0[nbdown - 2].node;
399 nk = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
400 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
401 double dist = pb.Distance(pnk);
402 if (dist < mind - eps) {
412 if (near == g) { // make triangle
413 SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
414 meshDS->SetMeshElementOnShape(face, faceID);
415 } else { // make quadrangle
417 d = uv_e0[nbdown - 2].node;
419 d = quad->uv_grid[nbhoriz*near - 2].node;
420 SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
421 meshDS->SetMeshElementOnShape(face, faceID);
423 if (near - 1 > g) { // if d not is at g - make additional triangles
424 for (int k = near - 1; k > g; k--) {
425 c = quad->uv_grid[nbhoriz*(k + 1) - 2].node;
427 d = uv_e0[nbdown - 2].node;
429 d = quad->uv_grid[nbhoriz*k - 2].node;
430 SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
431 meshDS->SetMeshElementOnShape(face, faceID);
438 if (quad->isEdgeOut[3]) {
439 // MESSAGE("left edge is out");
440 int g = nbvertic - 1; // last processed node in the grid
442 if (quad->isEdgeOut[0]) stop++;
443 for (i = nbleft - 1; i > stop; i--) {
444 const SMDS_MeshNode *a, *b, *c, *d;
446 b = uv_e3[i - 1].node;
447 gp_Pnt pb (b->X(), b->Y(), b->Z());
449 // find node c in the grid, nearest to the b
451 if (i == stop + 1) { // down bondary reached
452 c = quad->uv_grid[nbhoriz*jlow + 1].node;
455 double mind = RealLast();
456 for (int k = g; k >= jlow; k--) {
457 const SMDS_MeshNode *nk;
461 nk = quad->uv_grid[nbhoriz*k + 1].node;
462 gp_Pnt pnk (nk->X(), nk->Y(), nk->Z());
463 double dist = pb.Distance(pnk);
464 if (dist < mind - eps) {
474 if (near == g) { // make triangle
475 SMDS_MeshFace* face = meshDS->AddFace(a, b, c);
476 meshDS->SetMeshElementOnShape(face, faceID);
477 } else { // make quadrangle
481 d = quad->uv_grid[nbhoriz*(near + 1) + 1].node;
482 SMDS_MeshFace* face = meshDS->AddFace(a, b, c, d);
483 meshDS->SetMeshElementOnShape(face, faceID);
485 if (near + 1 < g) { // if d not is at g - make additional triangles
486 for (int k = near + 1; k < g; k++) {
487 c = quad->uv_grid[nbhoriz*k + 1].node;
491 d = quad->uv_grid[nbhoriz*(k + 1) + 1].node;
492 SMDS_MeshFace* face = meshDS->AddFace(a, c, d);
493 meshDS->SetMeshElementOnShape(face, faceID);
507 //=============================================================================
511 //=============================================================================
513 FaceQuadStruct *StdMeshers_Quadrangle_2D::CheckAnd2Dcompute
514 (SMESH_Mesh & aMesh, const TopoDS_Shape & aShape) throw(SALOME_Exception)
516 Unexpect aCatch(SalomeException);
517 // MESSAGE("StdMeshers_Quadrangle_2D::CheckAnd2Dcompute");
519 //SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
521 // const TopoDS_Face& FF = TopoDS::Face(aShape);
522 // bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
523 // TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
524 const TopoDS_Face & F = TopoDS::Face(aShape);
525 //bool faceIsForward = (F.Orientation() == TopAbs_FORWARD);
527 // verify 1 wire only, with 4 edges
529 if (NumberOfWires(F) != 1)
531 MESSAGE("only 1 wire by face (quadrangles)");
533 //throw SALOME_Exception(LOCALIZED("only 1 wire by face (quadrangles)"));
535 // const TopoDS_Wire WW = BRepTools::OuterWire(F);
536 // TopoDS_Wire W = TopoDS::Wire(WW.Oriented(TopAbs_FORWARD));
537 const TopoDS_Wire& W = BRepTools::OuterWire(F);
538 BRepTools_WireExplorer wexp (W, F);
540 FaceQuadStruct *quad = new FaceQuadStruct;
541 for (int i = 0; i < 4; i++)
542 quad->uv_edges[i] = 0;
546 for (wexp.Init(W, F); wexp.More(); wexp.Next())
548 // const TopoDS_Edge& EE = wexp.Current();
549 // TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
550 const TopoDS_Edge& E = wexp.Current();
551 int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
554 quad->edge[nbEdges] = E;
555 quad->nbPts[nbEdges] = nb + 2; // internal points + 2 extrema
562 MESSAGE("face must have 4 edges /quadrangles");
565 //throw SALOME_Exception(LOCALIZED("face must have 4 edges /quadrangles"));
568 // if (quad->nbPts[0] != quad->nbPts[2]) {
569 // MESSAGE("different point number-opposed edge");
572 // //throw SALOME_Exception(LOCALIZED("different point number-opposed edge"));
575 // if (quad->nbPts[1] != quad->nbPts[3]) {
576 // MESSAGE("different point number-opposed edge");
579 // //throw SALOME_Exception(LOCALIZED("different point number-opposed edge"));
582 // set normalized grid on unit square in parametric domain
584 SetNormalizedGrid(aMesh, F, quad);
589 //=============================================================================
593 //=============================================================================
595 void StdMeshers_Quadrangle_2D::QuadDelete (FaceQuadStruct * quad)
597 //MESSAGE("StdMeshers_Quadrangle_2D::QuadDelete");
600 for (int i = 0; i < 4; i++)
602 if (quad->uv_edges[i])
603 delete [] quad->uv_edges[i];
604 quad->edge[i].Nullify();
607 delete [] quad->uv_grid;
612 //=============================================================================
616 //=============================================================================
618 void StdMeshers_Quadrangle_2D::SetNormalizedGrid (SMESH_Mesh & aMesh,
619 const TopoDS_Shape& aShape,
620 FaceQuadStruct* quad) throw (SALOME_Exception)
622 Unexpect aCatch(SalomeException);
623 // Algorithme décrit dans "Génération automatique de maillages"
624 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
625 // traitement dans le domaine paramétrique 2d u,v
626 // transport - projection sur le carré unité
628 // MESSAGE("StdMeshers_Quadrangle_2D::SetNormalizedGrid");
629 const TopoDS_Face& F = TopoDS::Face(aShape);
631 // 1 --- find orientation of the 4 edges, by test on extrema
634 // |<----north-2-------^ a3 -------------> a2
636 // west-3 east-1 =right | |
640 // v----south-0--------> a0 -------------> a1
645 Handle(Geom2d_Curve) c2d[4];
648 for (int i = 0; i < 4; i++)
650 c2d[i] = BRep_Tool::CurveOnSurface(quad->edge[i], F,
651 quad->first[i], quad->last[i]);
652 pf[i] = c2d[i]->Value(quad->first[i]);
653 pl[i] = c2d[i]->Value(quad->last[i]);
654 quad->isEdgeForward[i] = false;
657 double eps2d = 1.e-3; // *** utiliser plutot TopExp::CommonVertex, puis
658 // distances si piece fausse
660 if ((pf[1].Distance(pl[0]) < eps2d) || (pl[1].Distance(pl[0]) < eps2d))
662 quad->isEdgeForward[0] = true;
664 double tmp = quad->first[0];
665 quad->first[0] = quad->last[0];
667 pf[0] = c2d[0]->Value(quad->first[0]);
668 pl[0] = c2d[0]->Value(quad->last[0]);
671 for (int i = 1; i < 4; i++)
673 quad->isEdgeForward[i] = (pf[i].Distance(pl[i - 1]) < eps2d);
674 if (!quad->isEdgeForward[i])
676 double tmp = quad->first[i];
677 quad->first[i] = quad->last[i];
679 pf[i] = c2d[i]->Value(quad->first[i]);
680 pl[i] = c2d[i]->Value(quad->last[i]);
681 //SCRUTE(pf[i].Distance(pl[i-1]));
682 ASSERT(pf[i].Distance(pl[i - 1]) < eps2d);
685 //SCRUTE(pf[0].Distance(pl[3]));
686 ASSERT(pf[0].Distance(pl[3]) < eps2d);
688 // for (int i=0; i<4; i++)
690 // SCRUTE(quad->isEdgeForward[i]);
691 // MESSAGE(" -first "<<i<<" "<<pf[i].X()<<" "<<pf[i].Y());
692 // MESSAGE(" -last "<<i<<" "<<pl[i].X()<<" "<<pl[i].Y());
695 // 2 --- load 2d edge points (u,v) with orientation and value on unit square
698 for (int i = 0; i < 2; i++)
700 quad->uv_edges[i] = LoadEdgePoints(aMesh, F, quad->edge[i],
701 quad->first[i], quad->last[i]);
702 if (!quad->uv_edges[i]) loadOk = false;
703 // quad->isEdgeForward[i]);
706 for (int i = 2; i < 4; i++)
708 quad->uv_edges[i] = LoadEdgePoints(aMesh, F, quad->edge[i],
709 quad->last[i], quad->first[i]);
710 if (!quad->uv_edges[i]) loadOk = false;
711 // !quad->isEdgeForward[i]);
716 // MESSAGE("StdMeshers_Quadrangle_2D::SetNormalizedGrid - LoadEdgePoints failed");
721 // 3 --- 2D normalized values on unit square [0..1][0..1]
723 // int nbdown = quad->nbPts[0];
724 // int nbright = quad->nbPts[1];
725 int nbhoriz = Min(quad->nbPts[0], quad->nbPts[2]);
726 int nbvertic = Min(quad->nbPts[1], quad->nbPts[3]);
727 // MESSAGE("nbhoriz, nbvertic = " << nbhoriz << nbvertic);
729 quad->isEdgeOut[0] = (quad->nbPts[0] > quad->nbPts[2]);
730 quad->isEdgeOut[1] = (quad->nbPts[1] > quad->nbPts[3]);
731 quad->isEdgeOut[2] = (quad->nbPts[2] > quad->nbPts[0]);
732 quad->isEdgeOut[3] = (quad->nbPts[3] > quad->nbPts[1]);
734 quad->uv_grid = new UVPtStruct[nbvertic * nbhoriz];
736 UVPtStruct *uv_grid = quad->uv_grid;
737 UVPtStruct *uv_e0 = quad->uv_edges[0];
738 UVPtStruct *uv_e1 = quad->uv_edges[1];
739 UVPtStruct *uv_e2 = quad->uv_edges[2];
740 UVPtStruct *uv_e3 = quad->uv_edges[3];
742 // nodes Id on "in" edges
743 if (! quad->isEdgeOut[0]) {
745 for (int i = 0; i < nbhoriz; i++) { // down
746 int ij = j * nbhoriz + i;
747 uv_grid[ij].node = uv_e0[i].node;
750 if (! quad->isEdgeOut[1]) {
752 for (int j = 0; j < nbvertic; j++) { // right
753 int ij = j * nbhoriz + i;
754 uv_grid[ij].node = uv_e1[j].node;
757 if (! quad->isEdgeOut[2]) {
758 int j = nbvertic - 1;
759 for (int i = 0; i < nbhoriz; i++) { // up
760 int ij = j * nbhoriz + i;
761 uv_grid[ij].node = uv_e2[i].node;
764 if (! quad->isEdgeOut[3]) {
766 for (int j = 0; j < nbvertic; j++) { // left
767 int ij = j * nbhoriz + i;
768 uv_grid[ij].node = uv_e3[j].node;
772 // falsificate "out" edges
773 if (quad->isEdgeOut[0]) // down
774 uv_e0 = MakeEdgePoints
775 (aMesh, F, quad->edge[0], quad->first[0], quad->last[0], nbhoriz - 1);
776 else if (quad->isEdgeOut[2]) // up
777 uv_e2 = MakeEdgePoints
778 (aMesh, F, quad->edge[2], quad->last[2], quad->first[2], nbhoriz - 1);
780 if (quad->isEdgeOut[1]) // right
781 uv_e1 = MakeEdgePoints
782 (aMesh, F, quad->edge[1], quad->first[1], quad->last[1], nbvertic - 1);
783 else if (quad->isEdgeOut[3]) // left
784 uv_e3 = MakeEdgePoints
785 (aMesh, F, quad->edge[3], quad->last[3], quad->first[3], nbvertic - 1);
787 // normalized 2d values on grid
788 for (int i = 0; i < nbhoriz; i++)
790 for (int j = 0; j < nbvertic; j++)
792 int ij = j * nbhoriz + i;
793 // --- droite i cste : x = x0 + y(x1-x0)
794 double x0 = uv_e0[i].normParam; // bas - sud
795 double x1 = uv_e2[i].normParam; // haut - nord
796 // --- droite j cste : y = y0 + x(y1-y0)
797 double y0 = uv_e3[j].normParam; // gauche-ouest
798 double y1 = uv_e1[j].normParam; // droite - est
799 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
800 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
801 double y = y0 + x * (y1 - y0);
804 //MESSAGE("-xy-01 "<<x0<<" "<<x1<<" "<<y0<<" "<<y1);
805 //MESSAGE("-xy-norm "<<i<<" "<<j<<" "<<x<<" "<<y);
809 // 4 --- projection on 2d domain (u,v)
815 for (int i = 0; i < nbhoriz; i++)
817 for (int j = 0; j < nbvertic; j++)
819 int ij = j * nbhoriz + i;
820 double x = uv_grid[ij].x;
821 double y = uv_grid[ij].y;
822 double param_0 = uv_e0[0].param + x * (uv_e0[nbhoriz - 1].param - uv_e0[0].param); // sud
823 double param_2 = uv_e2[0].param + x * (uv_e2[nbhoriz - 1].param - uv_e2[0].param); // nord
824 double param_1 = uv_e1[0].param + y * (uv_e1[nbvertic - 1].param - uv_e1[0].param); // est
825 double param_3 = uv_e3[0].param + y * (uv_e3[nbvertic - 1].param - uv_e3[0].param); // ouest
827 //MESSAGE("params "<<param_0<<" "<<param_1<<" "<<param_2<<" "<<param_3);
828 gp_Pnt2d p0 = c2d[0]->Value(param_0);
829 gp_Pnt2d p1 = c2d[1]->Value(param_1);
830 gp_Pnt2d p2 = c2d[2]->Value(param_2);
831 gp_Pnt2d p3 = c2d[3]->Value(param_3);
833 double u = (1 - y) * p0.X() + x * p1.X() + y * p2.X() + (1 - x) * p3.X();
834 double v = (1 - y) * p0.Y() + x * p1.Y() + y * p2.Y() + (1 - x) * p3.Y();
836 u -= (1 - x) * (1 - y) * a0.X() + x * (1 - y) * a1.X() +
837 x * y * a2.X() + (1 - x) * y * a3.X();
838 v -= (1 - x) * (1 - y) * a0.Y() + x * (1 - y) * a1.Y() +
839 x * y * a2.Y() + (1 - x) * y * a3.Y();
844 //MESSAGE("-uv- "<<i<<" "<<j<<" "<<uv_grid[ij].u<<" "<<uv_grid[ij].v);
849 //=============================================================================
853 //=============================================================================
854 UVPtStruct* StdMeshers_Quadrangle_2D::LoadEdgePoints (SMESH_Mesh & aMesh,
855 const TopoDS_Face& F,
856 const TopoDS_Edge& E,
857 double first, double last)
860 //MESSAGE("StdMeshers_Quadrangle_2D::LoadEdgePoints");
862 //SMDS_Mesh* meshDS = aMesh.GetMeshDS();
864 // --- IDNodes of first and last Vertex
866 TopoDS_Vertex VFirst, VLast;
867 TopExp::Vertices(E, VFirst, VLast); // corresponds to f and l
869 ASSERT(!VFirst.IsNull());
870 SMDS_NodeIteratorPtr lid = aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
873 MESSAGE ( "NO NODE BUILT ON VERTEX" );
876 const SMDS_MeshNode* idFirst = lid->next();
878 ASSERT(!VLast.IsNull());
879 lid = aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
882 MESSAGE ( "NO NODE BUILT ON VERTEX" );
885 const SMDS_MeshNode* idLast = lid->next();
887 // --- edge internal IDNodes (relies on good order storage, not checked)
889 map<double, const SMDS_MeshNode *> params;
890 SMDS_NodeIteratorPtr ite = aMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
894 const SMDS_MeshNode* node = ite->next();
895 const SMDS_EdgePosition* epos =
896 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
897 double param = epos->GetUParameter();
898 params[param] = node;
901 int nbPoints = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
902 if (nbPoints != params.size())
904 MESSAGE( "BAD NODE ON EDGE POSITIONS" );
907 UVPtStruct* uvslf = new UVPtStruct[nbPoints + 2];
910 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
912 bool isForward = (((l - f) * (last - first)) > 0);
919 gp_Pnt2d p = C2d->Value(f); // first point = Vertex Forward
923 uvslf[0].node = idFirst;
924 //MESSAGE("__ f "<<f<<" "<<uvslf[0].x <<" "<<uvslf[0].y);
925 map < double, const SMDS_MeshNode* >::iterator itp = params.begin();
926 for (int i = 1; i <= nbPoints; i++) // nbPoints internal
928 double param = (*itp).first;
929 gp_Pnt2d p = C2d->Value(param);
932 uvslf[i].param = param;
933 uvslf[i].node = (*itp).second;
934 //MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[i].x <<" "<<uvslf[i].y);
937 p = C2d->Value(l); // last point = Vertex Reversed
938 uvslf[nbPoints + 1].x = p.X();
939 uvslf[nbPoints + 1].y = p.Y();
940 uvslf[nbPoints + 1].param = l;
941 uvslf[nbPoints + 1].node = idLast;
942 //MESSAGE("__ l "<<l<<" "<<uvslf[nbPoints+1].x <<" "<<uvslf[nbPoints+1].y);
947 gp_Pnt2d p = C2d->Value(l); // first point = Vertex Reversed
951 uvslf[0].node = idLast;
952 //MESSAGE("__ l "<<l<<" "<<uvslf[0].x <<" "<<uvslf[0].y);
953 map < double, const SMDS_MeshNode* >::reverse_iterator itp = params.rbegin();
955 for (int j = nbPoints; j >= 1; j--) // nbPoints internal
957 double param = (*itp).first;
958 int i = nbPoints + 1 - j;
959 gp_Pnt2d p = C2d->Value(param);
962 uvslf[i].param = param;
963 uvslf[i].node = (*itp).second;
964 //MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[i].x <<" "<<uvslf[i].y);
967 p = C2d->Value(f); // last point = Vertex Forward
968 uvslf[nbPoints + 1].x = p.X();
969 uvslf[nbPoints + 1].y = p.Y();
970 uvslf[nbPoints + 1].param = f;
971 uvslf[nbPoints + 1].node = idFirst;
972 //MESSAGE("__ f "<<f<<" "<<uvslf[nbPoints+1].x <<" "<<uvslf[nbPoints+1].y);
975 ASSERT(paramin != paramax);
976 for (int i = 0; i < nbPoints + 2; i++)
978 uvslf[i].normParam = (uvslf[i].param - paramin) / (paramax - paramin);
979 //SCRUTE(uvslf[i].normParam);
985 //=============================================================================
989 //=============================================================================
990 UVPtStruct* StdMeshers_Quadrangle_2D::MakeEdgePoints (SMESH_Mesh & aMesh,
991 const TopoDS_Face& F,
992 const TopoDS_Edge& E,
993 double first, double last,
996 // MESSAGE("StdMeshers_Quadrangle_2D::MakeEdgePoints");
998 UVPtStruct* uvslf = new UVPtStruct[nb_segm + 1];
1001 // --- edge internal points
1003 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, fi, li);
1004 if (!Curve.IsNull()) {
1006 GeomAdaptor_Curve C3d (Curve);
1007 double length = EdgeLength(E);
1008 double eltSize = length / nb_segm;
1009 GCPnts_UniformAbscissa Discret (C3d, eltSize, fi, li);
1010 if (!Discret.IsDone()) return false;
1011 int NbPoints = Discret.NbPoints();
1012 for (int i = 1; i <= NbPoints; i++) {
1013 double param = Discret.Parameter(i);
1014 params.push_back(param);
1017 catch (Standard_Failure) {
1023 // Edge is a degenerated Edge
1024 BRep_Tool::Range(E, fi, li);
1025 double du = (li - fi) / nb_segm;
1026 for (int i = 1; i <= nb_segm + 1; i++)
1028 double param = fi + (i - 1) * du;
1029 params.push_back(param);
1034 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
1037 bool isForward = (((l - f) * (last - first)) > 0);
1039 list<double>::iterator itU = params.begin();
1040 for (int i = 0; i <= nb_segm; i++) // nbPoints internal
1042 double param = *itU;
1043 gp_Pnt2d p = C2d->Value(param);
1046 uvslf[i].param = param;
1047 uvslf[i].normParam = (param - f) / (l - f);
1051 list<double>::reverse_iterator itU = params.rbegin();
1052 for (int j = nb_segm; j >= 0; j--) // nbPoints internal
1054 double param = *itU;
1055 int i = nb_segm - j;
1056 gp_Pnt2d p = C2d->Value(param);
1059 uvslf[i].param = param;
1060 uvslf[i].normParam = (param - l) / (f - l);
1069 //=============================================================================
1073 //=============================================================================
1075 ostream & StdMeshers_Quadrangle_2D::SaveTo(ostream & save)
1080 //=============================================================================
1084 //=============================================================================
1086 istream & StdMeshers_Quadrangle_2D::LoadFrom(istream & load)
1091 //=============================================================================
1095 //=============================================================================
1097 ostream & operator <<(ostream & save, StdMeshers_Quadrangle_2D & hyp)
1099 return hyp.SaveTo( save );
1102 //=============================================================================
1106 //=============================================================================
1108 istream & operator >>(istream & load, StdMeshers_Quadrangle_2D & hyp)
1110 return hyp.LoadFrom( load );