1 // Created on: 1993-12-21
2 // Created by: Isabelle GRIGNON
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAdaptor_Curve2d.hxx>
21 #include <BRepAdaptor_Surface.hxx>
23 #include <ChFi3d_Builder_0.hxx>
25 #include <gp_Pnt2d.hxx>
27 #include <gp_Vec2d.hxx>
28 #include <Precision.hxx>
29 #include <TopExp_Explorer.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <BRepTools.hxx>
33 #include <IntTools_Tools.hxx>
34 #include <BRepAdaptor_HSurface.hxx>
35 #include <BRepTopAdaptor_TopolTool.hxx>
36 #include <LocalAnalysis_SurfaceContinuity.hxx>
37 #include <TopOpeBRepTool_TOOL.hxx>
40 static void Correct2dPoint(const TopoDS_Face& theF, gp_Pnt2d& theP2d);
43 //=======================================================================
44 //function : DefineConnectType
46 //=======================================================================
47 ChFiDS_TypeOfConcavity ChFi3d::DefineConnectType(const TopoDS_Edge& E,
48 const TopoDS_Face& F1,
49 const TopoDS_Face& F2,
50 const Standard_Real SinTol,
51 const Standard_Boolean CorrectPoint)
53 const Handle(Geom_Surface)& S1 = BRep_Tool::Surface(F1);
54 const Handle(Geom_Surface)& S2 = BRep_Tool::Surface(F2);
57 Handle (Geom2d_Curve) C1 = BRep_Tool::CurveOnSurface(E,F1,f,l);
58 //For the case of seam edge
62 Handle (Geom2d_Curve) C2 = BRep_Tool::CurveOnSurface(EE,F2,f,l);
63 if (C1.IsNull() || C2.IsNull())
66 BRepAdaptor_Curve C(E);
67 f = C.FirstParameter();
68 l = C.LastParameter();
70 Standard_Real ParOnC = 0.5*(f+l);
71 gp_Vec T1 = C.DN(ParOnC,1);
72 if (T1.SquareMagnitude() <= gp::Resolution())
74 ParOnC = IntTools_Tools::IntermediatePoint(f,l);
77 if (T1.SquareMagnitude() > gp::Resolution()) {
81 if (BRepTools::OriEdgeInFace(E,F1) == TopAbs_REVERSED) {
84 if (F1.Orientation() == TopAbs_REVERSED) T1.Reverse();
86 gp_Pnt2d P = C1->Value(ParOnC);
91 Correct2dPoint(F1, P);
93 S1->D1(P.X(),P.Y(),P3,D1U,D1V);
95 if (F1.Orientation() == TopAbs_REVERSED) DN1.Reverse();
97 P = C2->Value(ParOnC);
99 Correct2dPoint(F2, P);
100 S2->D1(P.X(),P.Y(),P3,D1U,D1V);
102 if (F2.Orientation() == TopAbs_REVERSED) DN2.Reverse();
107 gp_Vec ProVec = DN1^DN2;
108 Standard_Real NormProVec = ProVec.Magnitude();
109 if (NormProVec < SinTol) {
111 if (DN1.Dot(DN2) > 0) {
113 return ChFiDS_Tangential;
116 //Mixed not finished!
118 std::cout <<" faces locally mixed"<<std::endl;
120 return ChFiDS_Convex;
124 if (NormProVec > gp::Resolution())
125 ProVec /= NormProVec;
126 Standard_Real Prod = T1.Dot(ProVec);
129 return ChFiDS_Convex;
133 return ChFiDS_Concave;
138 //=======================================================================
139 //function : IsTangentFaces
141 //=======================================================================
142 Standard_Boolean ChFi3d::IsTangentFaces(const TopoDS_Edge& theEdge,
143 const TopoDS_Face& theFace1,
144 const TopoDS_Face& theFace2,
145 const GeomAbs_Shape Order)
147 if (Order == GeomAbs_G1 && BRep_Tool::Continuity(theEdge, theFace1, theFace2) != GeomAbs_C0)
148 return Standard_True;
150 Standard_Real TolC0 = Max(0.001, 1.5*BRep_Tool::Tolerance(theEdge));
152 Standard_Real aFirst;
155 // Obtaining of pcurves of edge on two faces.
156 const Handle(Geom2d_Curve) aC2d1 = BRep_Tool::CurveOnSurface
157 (theEdge, theFace1, aFirst, aLast);
158 //For the case of seam edge
159 TopoDS_Edge EE = theEdge;
160 if (theFace1.IsSame(theFace2))
162 const Handle(Geom2d_Curve) aC2d2 = BRep_Tool::CurveOnSurface
163 (EE, theFace2, aFirst, aLast);
164 if (aC2d1.IsNull() || aC2d2.IsNull())
165 return Standard_False;
167 // Obtaining of two surfaces from adjacent faces.
168 Handle(Geom_Surface) aSurf1 = BRep_Tool::Surface(theFace1);
169 Handle(Geom_Surface) aSurf2 = BRep_Tool::Surface(theFace2);
171 if (aSurf1.IsNull() || aSurf2.IsNull())
172 return Standard_False;
174 // Computation of the number of samples on the edge.
175 BRepAdaptor_Surface aBAS1(theFace1);
176 BRepAdaptor_Surface aBAS2(theFace2);
177 Handle(BRepAdaptor_HSurface) aBAHS1 = new BRepAdaptor_HSurface(aBAS1);
178 Handle(BRepAdaptor_HSurface) aBAHS2 = new BRepAdaptor_HSurface(aBAS2);
179 Handle(BRepTopAdaptor_TopolTool) aTool1 = new BRepTopAdaptor_TopolTool(aBAHS1);
180 Handle(BRepTopAdaptor_TopolTool) aTool2 = new BRepTopAdaptor_TopolTool(aBAHS2);
181 Standard_Integer aNbSamples1 = aTool1->NbSamples();
182 Standard_Integer aNbSamples2 = aTool2->NbSamples();
183 Standard_Integer aNbSamples = Max(aNbSamples1, aNbSamples2);
185 // Computation of the continuity.
187 Standard_Real aDelta = (aLast - aFirst) / (aNbSamples - 1);
188 Standard_Integer i, nbNotDone = 0;
190 for (i = 1, aPar = aFirst; i <= aNbSamples; i++, aPar += aDelta) {
191 if (i == aNbSamples) aPar = aLast;
193 LocalAnalysis_SurfaceContinuity aCont(aC2d1, aC2d2, aPar,
194 aSurf1, aSurf2, Order,
195 0.001, TolC0, 0.1, 0.1, 0.1);
202 if (Order == GeomAbs_G1)
205 return Standard_False;
207 else if (!aCont.IsG2())
208 return Standard_False;
211 if (nbNotDone == aNbSamples)
212 return Standard_False;
214 //Compare normals of tangent faces in the middle point
215 Standard_Real MidPar = (aFirst + aLast) / 2.;
216 gp_Pnt2d uv1 = aC2d1->Value(MidPar);
217 gp_Pnt2d uv2 = aC2d2->Value(MidPar);
218 gp_Dir normal1, normal2;
219 TopOpeBRepTool_TOOL::Nt(uv1, theFace1, normal1);
220 TopOpeBRepTool_TOOL::Nt(uv2, theFace2, normal2);
221 Standard_Real dot = normal1.Dot(normal2);
223 return Standard_False;
224 return Standard_True;
227 //=======================================================================
228 //function : ConcaveSide
229 //purpose : calculate the concave face at the neighborhood of the border of
231 //=======================================================================
232 Standard_Integer ChFi3d::ConcaveSide(const BRepAdaptor_Surface& S1,
233 const BRepAdaptor_Surface& S2,
234 const TopoDS_Edge& E,
235 TopAbs_Orientation& Or1,
236 TopAbs_Orientation& Or2)
239 Standard_Integer ChoixConge;
240 Or1 = Or2 = TopAbs_FORWARD;
241 BRepAdaptor_Curve CE(E);
242 Standard_Real first = CE.FirstParameter();
243 Standard_Real last = CE.LastParameter();
244 Standard_Real par = 0.691254*first + 0.308746*last;
246 gp_Pnt pt, pt1, pt2; gp_Vec tgE, tgE1, tgE2, ns1, ns2, dint1, dint2;
247 TopoDS_Face F1 = S1.Face();
248 TopoDS_Face F2 = S2.Face();
249 //F1.Orientation(TopAbs_FORWARD);
250 //F2.Orientation(TopAbs_FORWARD);
255 if(E.Orientation() == TopAbs_REVERSED) tgE.Reverse();
257 TopoDS_Edge E1 = E, E2 = E;
258 E1.Orientation(TopAbs_FORWARD);
259 E2.Orientation(TopAbs_FORWARD);
261 if(F1.IsSame(F2) && BRep_Tool::IsClosed(E,F1)) {
262 E2.Orientation(TopAbs_REVERSED);
267 Standard_Boolean found = 0;
268 for (Exp.Init(F1,TopAbs_EDGE);
269 Exp.More() && !found;
271 if (E.IsSame(TopoDS::Edge(Exp.Current()))){
272 if(Exp.Current().Orientation() == TopAbs_REVERSED) tgE1.Reverse();
273 found = Standard_True;
276 if (!found) { return 0; }
278 for (Exp.Init(F2,TopAbs_EDGE);
279 Exp.More() && !found;
281 if (E.IsSame(TopoDS::Edge(Exp.Current()))){
282 if(Exp.Current().Orientation() == TopAbs_REVERSED) tgE2.Reverse();
283 found = Standard_True;
286 if (!found) { return 0; }
288 BRepAdaptor_Curve2d pc1(E1,F1);
289 BRepAdaptor_Curve2d pc2(E2,F2);
291 gp_Vec DU1,DV1,DU2,DV2;
292 p2d1 = pc1.Value(par);
293 p2d2 = pc2.Value(par);
294 S1.D1(p2d1.X(),p2d1.Y(),pt1,DU1,DV1);
295 ns1 = DU1.Crossed(DV1);
297 if (F1.Orientation() == TopAbs_REVERSED)
299 S2.D1(p2d2.X(),p2d2.Y(),pt2,DU2,DV2);
300 ns2 = DU2.Crossed(DV2);
302 if (F2.Orientation() == TopAbs_REVERSED)
305 dint1 = ns1.Crossed(tgE1);
306 dint2 = ns2.Crossed(tgE2);
307 Standard_Real ang = ns1.CrossMagnitude(ns2);
308 if(ang > 0.0001*M_PI){
309 Standard_Real scal = ns2.Dot(dint1);
312 Or2 = TopAbs_REVERSED;
314 scal = ns1.Dot(dint2);
317 Or1 = TopAbs_REVERSED;
321 //the faces are locally tangent - this is fake!
322 if(dint1.Dot(dint2) < 0.){
323 //This is a forgotten regularity
324 gp_Vec DDU, DDV, DDUV;
325 S1.D2(p2d1.X(),p2d1.Y(),pt1,DU1,DV1,DDU,DDV,DDUV);
326 DU1 += ( DU1 * dint1 < 0) ? -DDU : DDU;
327 DV1 += ( DV1 * dint1 < 0) ? -DDV : DDV;
328 ns1 = DU1.Crossed(DV1);
330 if (F1.Orientation() == TopAbs_REVERSED)
332 S2.D2(p2d2.X(),p2d2.Y(),pt2,DU2,DV2,DDU,DDV,DDUV);
333 DU2 += ( DU2 * dint2 < 0) ? -DDU : DDU;
334 DV2 += ( DV2 * dint2 < 0) ? -DDV : DDV;
335 ns2 = DU2.Crossed(DV2);
337 if (F2.Orientation() == TopAbs_REVERSED)
340 dint1 = ns1.Crossed(tgE1);
341 dint2 = ns2.Crossed(tgE2);
342 ang = ns1.CrossMagnitude(ns2);
343 if(ang > 0.0001*M_PI){
344 Standard_Real scal = ns2.Dot(dint1);
347 Or2 = TopAbs_REVERSED;
349 scal = ns1.Dot(dint2);
352 Or1 = TopAbs_REVERSED;
357 std::cout<<"ConcaveSide : no concave face"<<std::endl;
359 //This 10 shows that the face at end is in the extension of one of two base faces
364 //here it turns back, the points are taken in faces
365 //neither too close nor too far as much as possible.
368 // Standard_Real deport = 1000*BRep_Tool::Tolerance(E);
370 ChFi3d_Coefficient(dint1,DU1,DV1,u,v);
371 p2d1.SetX(p2d1.X() + u); p2d1.SetY(p2d1.Y() + v);
372 ChFi3d_Coefficient(dint1,DU2,DV2,u,v);
373 p2d2.SetX(p2d2.X() + u); p2d2.SetY(p2d2.Y() + v);
374 S1.D1(p2d1.X(),p2d1.Y(),pt1,DU1,DV1);
375 ns1 = DU1.Crossed(DV1);
376 if (F1.Orientation() == TopAbs_REVERSED)
378 S2.D1(p2d2.X(),p2d2.Y(),pt2,DU2,DV2);
379 ns2 = DU2.Crossed(DV2);
380 if (F2.Orientation() == TopAbs_REVERSED)
382 gp_Vec vref(pt1,pt2);
383 if(ns1.Dot(vref) < 0.){
384 Or1 = TopAbs_REVERSED;
386 if(ns2.Dot(vref) > 0.){
387 Or2 = TopAbs_REVERSED;
393 if (Or1 == TopAbs_FORWARD) {
394 if (Or2 == TopAbs_FORWARD) ChoixConge = 1;
398 if (Or2 == TopAbs_FORWARD) ChoixConge = 3;
401 if ((ns1.Crossed(ns2)).Dot(tgE) >= 0.) ChoixConge++ ;
405 //=======================================================================
406 //function : NextSide
409 //=======================================================================
411 Standard_Integer ChFi3d::NextSide(TopAbs_Orientation& Or1,
412 TopAbs_Orientation& Or2,
413 const TopAbs_Orientation OrSave1,
414 const TopAbs_Orientation OrSave2,
415 const Standard_Integer ChoixSave)
417 if (Or1 == TopAbs_FORWARD){Or1 = OrSave1;}
419 Or1 = TopAbs::Reverse(OrSave1);
421 if (Or2 == TopAbs_FORWARD){Or2 = OrSave2;}
423 Or2 = TopAbs::Reverse(OrSave2);
426 Standard_Integer ChoixConge;
427 if (Or1 == TopAbs_FORWARD) {
428 if (Or2 == TopAbs_FORWARD) ChoixConge = 1;
430 if(ChoixSave < 0) ChoixConge = 3;
435 if (Or2 == TopAbs_FORWARD) {
436 if(ChoixSave < 0) ChoixConge = 7;
441 if (Abs(ChoixSave)%2 == 0) ChoixConge++;
446 //=======================================================================
447 //function : NextSide
450 //=======================================================================
452 void ChFi3d::NextSide(TopAbs_Orientation& Or,
453 const TopAbs_Orientation OrSave,
454 const TopAbs_Orientation OrFace)
456 if (Or == OrFace){Or = OrSave;}
458 Or = TopAbs::Reverse(OrSave);
464 //=======================================================================
465 //function : SameSide
468 //=======================================================================
470 Standard_Boolean ChFi3d::SameSide(const TopAbs_Orientation Or,
471 const TopAbs_Orientation OrSave1,
472 const TopAbs_Orientation OrSave2,
473 const TopAbs_Orientation OrFace1,
474 const TopAbs_Orientation OrFace2)
476 TopAbs_Orientation o1,o2;
477 if (Or == OrFace1){o1 = OrSave1;}
479 o1 = TopAbs::Reverse(OrSave1);
481 if (Or == OrFace2){o2 = OrSave2;}
483 o2 = TopAbs::Reverse(OrSave2);
488 //=======================================================================
489 //function : Correct2dPoint
491 //=======================================================================
492 void Correct2dPoint(const TopoDS_Face& theF, gp_Pnt2d& theP2d)
494 BRepAdaptor_Surface aBAS(theF, Standard_False);
495 if (aBAS.GetType() < GeomAbs_BezierSurface) {
499 const Standard_Real coeff = 0.01;
501 Standard_Real u1, u2, v1, v2;
503 aBAS.Initialize(theF, Standard_True);
504 u1 = aBAS.FirstUParameter();
505 u2 = aBAS.LastUParameter();
506 v1 = aBAS.FirstVParameter();
507 v2 = aBAS.LastVParameter();
508 if (!(Precision::IsInfinite(u1) || Precision::IsInfinite(u2)))
510 eps = Max(coeff*(u2 - u1), Precision::PConfusion());
511 if (Abs(theP2d.X() - u1) < eps)
513 theP2d.SetX(u1 + eps);
515 if (Abs(theP2d.X() - u2) < eps)
517 theP2d.SetX(u2 - eps);
520 if (!(Precision::IsInfinite(v1) || Precision::IsInfinite(v2)))
522 eps = Max(coeff*(v2 - v1), Precision::PConfusion());
523 if (Abs(theP2d.Y() - v1) < eps)
525 theP2d.SetY(v1 + eps);
527 if (Abs(theP2d.Y() - v2) < eps)
529 theP2d.SetY(v2 - eps);