2 //=============================================================================
3 // File : SMESH_Quadrangle_2D.cxx
4 // Created : sam mai 18 08:11:32 CEST 2002
5 // Author : Paul RASCLE, EDF
7 // Copyright : EDF 2002
9 //=============================================================================
12 #include "SMESH_Quadrangle_2D.hxx"
13 #include "SMESH_Gen.hxx"
14 #include "SMESH_Mesh.hxx"
16 #include "SMDS_MeshElement.hxx"
17 #include "SMDS_MeshNode.hxx"
18 #include "SMDS_EdgePosition.hxx"
19 #include "SMDS_FacePosition.hxx"
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <BRepTools_WireExplorer.hxx>
24 #include <Geom_Surface.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Handle_Geom2d_Curve.hxx>
28 #include <Handle_Geom_Curve.hxx>
29 #include <gp_Pnt2d.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include "utilities.h"
34 //=============================================================================
38 //=============================================================================
40 SMESH_Quadrangle_2D::SMESH_Quadrangle_2D(int hypId,
43 : SMESH_2D_Algo(hypId, studyId, gen)
45 MESSAGE("SMESH_Quadrangle_2D::SMESH_Quadrangle_2D");
46 _name = "Quadrangle_2D";
47 // _shapeType = TopAbs_FACE;
48 _shapeType = (1<<TopAbs_FACE);
51 //=============================================================================
55 //=============================================================================
57 SMESH_Quadrangle_2D::~SMESH_Quadrangle_2D()
59 MESSAGE("SMESH_Quadrangle_2D::~SMESH_Quadrangle_2D");
62 //=============================================================================
66 //=============================================================================
68 bool SMESH_Quadrangle_2D::CheckHypothesis(SMESH_Mesh& aMesh,
69 const TopoDS_Shape& aShape)
71 //MESSAGE("SMESH_Quadrangle_2D::CheckHypothesis");
81 //=============================================================================
85 //=============================================================================
87 bool SMESH_Quadrangle_2D::Compute(SMESH_Mesh& aMesh,
88 const TopoDS_Shape& aShape)
89 throw (SALOME_Exception)
91 //MESSAGE("SMESH_Quadrangle_2D::Compute");
92 const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
93 SMESH_subMesh* theSubMesh = aMesh.GetSubMesh(aShape);
95 FaceQuadStruct* quad = CheckAnd2Dcompute(aMesh, aShape);
96 if (!quad) return false;
98 // --- compute 3D values on points, store points & quadrangles
100 int nbdown = quad->nbPts[0];
101 int nbright = quad->nbPts[1];
102 int nbVertices = nbdown*nbright;
103 int nbQuad = (nbdown-1)*(nbright-1);
104 //SCRUTE(nbVertices);
107 // const TopoDS_Face& FF = TopoDS::Face(aShape);
108 // bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
109 // TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
110 const TopoDS_Face& F = TopoDS::Face(aShape);
111 bool faceIsForward = (F.Orientation() == TopAbs_FORWARD);
112 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
114 for (int i=1; i<nbdown-1; i++)
115 for (int j=1; j<nbright-1; j++) // internal points
117 int ij = j*nbdown +i;
118 double u = quad->uv_grid[ij].u;
119 double v = quad->uv_grid[ij].v;
120 gp_Pnt P = S->Value(u,v);
121 int nodeId = meshDS->AddNode(P.X(), P.Y(), P.Z());
122 //MESSAGE("point "<< nodeId<<" "<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z());
123 Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
124 Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
125 meshDS->SetNodeOnFace(node, F);
126 quad->uv_grid[ij].nodeId = nodeId;
127 // Handle (SMDS_FacePosition) fpos
128 // = new SMDS_FacePosition(theSubMesh->GetId(),i,j); // easier than u,v
129 // node->SetPosition(fpos);
130 Handle (SMDS_FacePosition) fpos
131 = Handle (SMDS_FacePosition)::DownCast(node->GetPosition());
132 fpos->SetUParameter(i);
133 fpos->SetVParameter(j);
136 // bool isQuadForward = ( faceIsForward == quad->isEdgeForward[0]);
137 for (int i=0; i<nbdown-1; i++)
138 for (int j=0; j<nbright-1; j++) // faces
140 int a = quad->uv_grid[ j *nbdown +i ].nodeId;
141 int b = quad->uv_grid[ j *nbdown +i+1].nodeId;
142 int c = quad->uv_grid[(j+1)*nbdown +i+1].nodeId;
143 int d = quad->uv_grid[(j+1)*nbdown +i ].nodeId;
145 // if (isQuadForward) faceId = meshDS->AddFace(a,b,c,d);
146 // else faceId = meshDS->AddFace(a,d,c,b);
147 faceId = meshDS->AddFace(a,b,c,d);
148 Handle (SMDS_MeshElement) elt = meshDS->FindElement(faceId);
149 meshDS->SetMeshElementOnShape(elt, F);
157 //=============================================================================
161 //=============================================================================
164 SMESH_Quadrangle_2D::CheckAnd2Dcompute(SMESH_Mesh& aMesh,
165 const TopoDS_Shape& aShape)
166 throw (SALOME_Exception)
168 //MESSAGE("SMESH_Quadrangle_2D::ComputeWithoutStore");
170 SMESH_subMesh* theSubMesh = aMesh.GetSubMesh(aShape);
172 // const TopoDS_Face& FF = TopoDS::Face(aShape);
173 // bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
174 // TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
175 const TopoDS_Face& F = TopoDS::Face(aShape);
176 bool faceIsForward = (F.Orientation() == TopAbs_FORWARD);
178 // verify 1 wire only, with 4 edges, same number of points on opposite edges
180 if (NumberOfWires (F) != 1)
182 MESSAGE("only 1 wire by face (quadrangles)");
184 //throw SALOME_Exception(LOCALIZED("only 1 wire by face (quadrangles)"));
186 // const TopoDS_Wire WW = BRepTools::OuterWire(F);
187 // TopoDS_Wire W = TopoDS::Wire(WW.Oriented(TopAbs_FORWARD));
188 const TopoDS_Wire& W = BRepTools::OuterWire(F);
189 BRepTools_WireExplorer wexp(W,F);
191 FaceQuadStruct* quad = new FaceQuadStruct;
192 for (int i=0; i<4; i++) quad->uv_edges[i] = 0;
196 for (wexp.Init(W,F);wexp.More(); wexp.Next())
198 // const TopoDS_Edge& EE = wexp.Current();
199 // TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
200 const TopoDS_Edge& E = wexp.Current();
201 int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
204 quad->edge[nbEdges] = E;
205 quad->nbPts[nbEdges] = nb +2; // internal points + 2 extrema
212 MESSAGE("face must have 4 edges /quadrangles");
215 //throw SALOME_Exception(LOCALIZED("face must have 4 edges /quadrangles"));
218 if (quad->nbPts[0] != quad->nbPts[2])
220 MESSAGE("different point number-opposed edge");
223 //throw SALOME_Exception(LOCALIZED("different point number-opposed edge"));
226 if (quad->nbPts[1] != quad->nbPts[3])
228 MESSAGE("different point number-opposed edge");
231 //throw SALOME_Exception(LOCALIZED("different point number-opposed edge"));
234 // set normalized grid on unit square in parametric domain
236 SetNormalizedGrid(aMesh, F, quad);
242 //=============================================================================
246 //=============================================================================
248 void SMESH_Quadrangle_2D::QuadDelete(FaceQuadStruct* quad)
250 //MESSAGE("SMESH_Quadrangle_2D::QuadDelete");
253 for (int i=0; i<4; i++)
255 if (quad->uv_edges[i]) delete [] quad->uv_edges[i];
256 quad->edge[i].Nullify();
258 if (quad->uv_grid) delete [] quad->uv_grid;
263 //=============================================================================
267 //=============================================================================
269 void SMESH_Quadrangle_2D::SetNormalizedGrid(SMESH_Mesh& aMesh,
270 const TopoDS_Shape& aShape,
271 FaceQuadStruct* quad)
272 throw (SALOME_Exception)
274 // Algorithme décrit dans "Génération automatique de maillages"
275 // P.L. GEORGE, MASSON, § 6.4.1 p. 84-85
276 // traitement dans le domaine paramétrique 2d u,v
277 // transport - projection sur le carré unité
279 const TopoDS_Face& F = TopoDS::Face(aShape);
281 // 1 --- find orientation of the 4 edges, by test on extrema
284 // |<----north-2-------^ a3 -------------> a2
286 // west-3 east-1 =right | |
290 // v----south-0--------> a0 -------------> a1
295 Handle (Geom2d_Curve) c2d[4];
298 for (int i=0; i<4; i++)
300 c2d[i] = BRep_Tool::CurveOnSurface(quad->edge[i],
304 pf[i] = c2d[i]->Value(quad->first[i]);
305 pl[i] = c2d[i]->Value(quad->last[i]);
306 quad->isEdgeForward[i] = false;
309 double eps2d = 1.e-3; // *** utiliser plutot TopExp::CommonVertex, puis
310 // distances si piece fausse
312 if ((pf[1].Distance(pl[0]) < eps2d) || (pl[1].Distance(pl[0]) < eps2d))
314 quad->isEdgeForward[0] = true;
318 double tmp =quad->first[0];
319 quad->first[0] = quad->last[0];
321 pf[0] = c2d[0]->Value(quad->first[0]);
322 pl[0] = c2d[0]->Value(quad->last[0]);
324 for (int i=1; i<4; i++)
326 quad->isEdgeForward[i] = (pf[i].Distance(pl[i-1]) < eps2d);
327 if (! quad->isEdgeForward[i])
329 double tmp =quad->first[i];
330 quad->first[i] = quad->last[i];
332 pf[i] = c2d[i]->Value(quad->first[i]);
333 pl[i] = c2d[i]->Value(quad->last[i]);
334 //SCRUTE(pf[i].Distance(pl[i-1]));
335 ASSERT(pf[i].Distance(pl[i-1]) < eps2d);
338 //SCRUTE(pf[0].Distance(pl[3]));
339 ASSERT(pf[0].Distance(pl[3]) < eps2d);
341 // for (int i=0; i<4; i++)
343 // SCRUTE(quad->isEdgeForward[i]);
344 // MESSAGE(" -first "<<i<<" "<<pf[i].X()<<" "<<pf[i].Y());
345 // MESSAGE(" -last "<<i<<" "<<pl[i].X()<<" "<<pl[i].Y());
348 // 2 --- load 2d edge points (u,v) with orientation and value on unit square
350 for (int i=0; i<2; i++)
352 quad->uv_edges[i] = LoadEdgePoints(aMesh, F,
357 // quad->isEdgeForward[i]);
359 for (int i=2; i<4; i++)
361 quad->uv_edges[i] = LoadEdgePoints(aMesh, F,
366 // !quad->isEdgeForward[i]);
369 // 3 --- 2D normalized values on unit square [0..1][0..1]
371 int nbdown = quad->nbPts[0];
372 int nbright = quad->nbPts[1];
373 quad->uv_grid = new UVPtStruct[nbright*nbdown];
375 UVPtStruct* uv_grid = quad->uv_grid;
376 UVPtStruct* uv_e0 = quad->uv_edges[0];
377 UVPtStruct* uv_e1 = quad->uv_edges[1];
378 UVPtStruct* uv_e2 = quad->uv_edges[2];
379 UVPtStruct* uv_e3 = quad->uv_edges[3];
388 for (int i=0; i<nbdown; i++)
390 int ij = j*nbdown +i;
391 uv_grid[ij].nodeId = uv_e0[i].nodeId;
394 for (int j=0; j<nbright; j++)
396 int ij = j*nbdown +i;
397 uv_grid[ij].nodeId = uv_e1[j].nodeId;
400 for (int i=0; i<nbdown; i++)
402 int ij = j*nbdown +i;
403 uv_grid[ij].nodeId = uv_e2[i].nodeId;
406 for (int j=0; j<nbright; j++)
408 int ij = j*nbdown +i;
409 uv_grid[ij].nodeId = uv_e3[j].nodeId;
412 // normalized 2d values on grid
414 for (int i=0; i<nbdown; i++)
415 for (int j=0; j<nbright; j++)
417 int ij = j*nbdown +i;
418 // --- droite i cste : x = x0 + y(x1-x0)
419 double x0 = uv_e0[i].normParam; // bas - sud
420 double x1 = uv_e2[i].normParam; // haut - nord
421 // --- droite j cste : y = y0 + x(y1-y0)
422 double y0 = uv_e3[j].normParam; // gauche-ouest
423 double y1 = uv_e1[j].normParam; // droite - est
424 // --- intersection : x=x0+(y0+x(y1-y0))(x1-x0)
425 double x=(x0+y0*(x1-x0))/(1-(y1-y0)*(x1-x0));
426 double y=y0+x*(y1-y0);
429 //MESSAGE("-xy-01 "<<x0<<" "<<x1<<" "<<y0<<" "<<y1);
430 //MESSAGE("-xy-norm "<<i<<" "<<j<<" "<<x<<" "<<y);
433 // 4 --- projection on 2d domain (u,v)
435 for (int i=0; i<nbdown; i++)
436 for (int j=0; j<nbright; j++)
438 int ij = j*nbdown +i;
439 double x = uv_grid[ij].x;
440 double y = uv_grid[ij].y;
441 double param_0 = uv_e0[0].param
442 + x*(uv_e0[nbdown-1].param -uv_e0[0].param); // sud
443 double param_2 = uv_e2[0].param
444 + x*(uv_e2[nbdown-1].param -uv_e2[0].param); // nord
445 double param_1 = uv_e1[0].param
446 + y*(uv_e1[nbright-1].param -uv_e1[0].param); // est
447 double param_3 = uv_e3[0].param
448 + y*(uv_e3[nbright-1].param -uv_e3[0].param); // ouest
450 //MESSAGE("params "<<param_0<<" "<<param_1<<" "<<param_2<<" "<<param_3);
451 gp_Pnt2d p0 = c2d[0]->Value(param_0);
452 gp_Pnt2d p1 = c2d[1]->Value(param_1);
453 gp_Pnt2d p2 = c2d[2]->Value(param_2);
454 gp_Pnt2d p3 = c2d[3]->Value(param_3);
456 double u = (1-y)*p0.X() + x*p1.X() + y*p2.X() + (1-x)*p3.X();
457 double v = (1-y)*p0.Y() + x*p1.Y() + y*p2.Y() + (1-x)*p3.Y();
459 u -= (1-x)*(1-y)*a0.X() + x*(1-y)*a1.X() + x*y*a2.X() + (1-x)*y*a3.X();
460 v -= (1-x)*(1-y)*a0.Y() + x*(1-y)*a1.Y() + x*y*a2.Y() + (1-x)*y*a3.Y();
465 //MESSAGE("-uv- "<<i<<" "<<j<<" "<<uv_grid[ij].u<<" "<<uv_grid[ij].v);
469 //=============================================================================
473 //=============================================================================
475 UVPtStruct* SMESH_Quadrangle_2D::LoadEdgePoints(SMESH_Mesh& aMesh,
476 const TopoDS_Face& F,
477 const TopoDS_Edge& E,
482 //MESSAGE("SMESH_Quadrangle_2D::LoadEdgePoints");
484 Handle (SMDS_Mesh) meshDS = aMesh.GetMeshDS();
486 // --- IDNodes of first and last Vertex
488 TopoDS_Vertex VFirst, VLast;
489 TopExp::Vertices(E, VFirst, VLast); // corresponds to f and l
491 ASSERT(!VFirst.IsNull());
492 SMESH_subMesh* firstSubMesh = aMesh.GetSubMesh(VFirst);
493 const TColStd_ListOfInteger& lidf
494 = firstSubMesh->GetSubMeshDS()->GetIDNodes();
495 int idFirst= lidf.First();
498 ASSERT(!VLast.IsNull());
499 SMESH_subMesh* lastSubMesh = aMesh.GetSubMesh(VLast);
500 const TColStd_ListOfInteger& lidl
501 = lastSubMesh->GetSubMeshDS()->GetIDNodes();
502 int idLast= lidl.First();
505 // --- edge internal IDNodes (relies on good order storage, not checked)
507 int nbPoints = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
509 UVPtStruct * uvslf = new UVPtStruct[nbPoints+2];
512 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E,F,f,l);
514 const TColStd_ListOfInteger& indElt
515 = aMesh.GetSubMesh(E)->GetSubMeshDS()->GetIDNodes();
516 TColStd_ListIteratorOfListOfInteger ite(indElt);
518 //SCRUTE(indElt.Extent());
519 ASSERT(nbPoints == indElt.Extent());
521 map<double,int> params;
522 for (; ite.More(); ite.Next())
524 int nodeId = ite.Value();
525 Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
526 Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
527 Handle (SMDS_EdgePosition) epos
528 = Handle (SMDS_EdgePosition)::DownCast(node->GetPosition());
529 double param = epos->GetUParameter();
530 params[param] = nodeId;
533 bool isForward = (((l-f)*(last-first)) > 0);
540 gp_Pnt2d p = C2d->Value(f); // first point = Vertex Forward
544 uvslf [0].nodeId = idFirst;
545 //MESSAGE("__ f "<<f<<" "<<uvslf[0].x <<" "<<uvslf[0].y);
546 map<double,int>::iterator itp = params.begin();
547 for (int i = 1; i <= nbPoints; i++) // nbPoints internal
549 double param = (*itp).first;
550 int nodeId = (*itp).second;
551 gp_Pnt2d p = C2d->Value(param);
554 uvslf[i].param = param;
555 uvslf[i].nodeId = nodeId;
556 //MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[i].x <<" "<<uvslf[i].y);
559 p = C2d->Value(l); // last point = Vertex Reversed
560 uvslf [nbPoints+1].x = p.X();
561 uvslf [nbPoints+1].y = p.Y();
562 uvslf [nbPoints+1].param = l;
563 uvslf [nbPoints+1].nodeId = idLast;
564 //MESSAGE("__ l "<<l<<" "<<uvslf[nbPoints+1].x <<" "<<uvslf[nbPoints+1].y);
570 gp_Pnt2d p = C2d->Value(l); // first point = Vertex Reversed
574 uvslf [0].nodeId = idLast;
575 //MESSAGE("__ l "<<l<<" "<<uvslf[0].x <<" "<<uvslf[0].y);
576 map<double,int>::reverse_iterator itp = params.rbegin();
577 for (int j = nbPoints; j >= 1; j--) // nbPoints internal
579 double param = (*itp).first;
580 int nodeId = (*itp).second;
581 int i = nbPoints +1 -j;
582 gp_Pnt2d p = C2d->Value(param);
585 uvslf[i].param = param;
586 uvslf[i].nodeId = nodeId;
587 //MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[i].x <<" "<<uvslf[i].y);
590 p = C2d->Value(f); // last point = Vertex Forward
591 uvslf [nbPoints+1].x = p.X();
592 uvslf [nbPoints+1].y = p.Y();
593 uvslf [nbPoints+1].param = f;
594 uvslf [nbPoints+1].nodeId = idFirst;
595 //MESSAGE("__ f "<<f<<" "<<uvslf[nbPoints+1].x <<" "<<uvslf[nbPoints+1].y);
598 ASSERT(paramin != paramax);
599 for (int i = 0; i< nbPoints+2; i++)
601 uvslf[i].normParam = (uvslf[i].param -paramin)/(paramax -paramin);
602 //SCRUTE(uvslf[i].normParam);
608 //=============================================================================
612 //=============================================================================
614 ostream & SMESH_Quadrangle_2D::SaveTo(ostream & save)
619 //=============================================================================
623 //=============================================================================
625 istream & SMESH_Quadrangle_2D::LoadFrom(istream & load)
627 return load >> (*this);
630 //=============================================================================
634 //=============================================================================
636 ostream & operator << (ostream & save, SMESH_Quadrangle_2D & hyp)
641 //=============================================================================
645 //=============================================================================
647 istream & operator >> (istream & load, SMESH_Quadrangle_2D & hyp)