1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //:r5 abv 06.04.99: ec_turbine-A.stp, #4313: protect against null curve
15 // abv 09.04.99 S4136: add parameter preci (to eliminate BRepAPI::Precision)
17 #include <Adaptor3d_CurveOnSurface.hxx>
18 #include <BRep_Tool.hxx>
20 #include <Extrema_ExtPC.hxx>
21 #include <Geom2d_BoundedCurve.hxx>
22 #include <Geom2d_BSplineCurve.hxx>
23 #include <Geom2d_Curve.hxx>
24 #include <Geom2d_Line.hxx>
25 #include <Geom2dAdaptor_Curve.hxx>
26 #include <Geom2dAdaptor_HCurve.hxx>
27 #include <Geom_Curve.hxx>
28 #include <Geom_SphericalSurface.hxx>
29 #include <Geom_Surface.hxx>
30 #include <GeomAdaptor_HSurface.hxx>
31 #include <GeomAdaptor_Surface.hxx>
33 #include <Precision.hxx>
34 #include <ShapeAnalysis.hxx>
35 #include <ShapeAnalysis_Curve.hxx>
36 #include <ShapeAnalysis_Edge.hxx>
37 #include <ShapeAnalysis_Surface.hxx>
38 #include <ShapeFix_EdgeProjAux.hxx>
39 #include <Standard_ErrorHandler.hxx>
40 #include <Standard_Failure.hxx>
41 #include <Standard_Type.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <Adaptor3d_HCurve.hxx>
47 #include <BSplCLib.hxx>
49 IMPLEMENT_STANDARD_RTTIEXT(ShapeFix_EdgeProjAux,MMgt_TShared)
51 //=======================================================================
52 //function : ShapeFix_EdgeProjAux
54 //=======================================================================
55 ShapeFix_EdgeProjAux::ShapeFix_EdgeProjAux ()
57 myFirstDone = myLastDone = Standard_False;
60 //=======================================================================
61 //function : ShapeFix_EdgeProjAux
63 //=======================================================================
65 ShapeFix_EdgeProjAux::ShapeFix_EdgeProjAux (const TopoDS_Face& F,
71 //=======================================================================
74 //=======================================================================
76 void ShapeFix_EdgeProjAux::Init (const TopoDS_Face& F,
81 myFirstParam = myLastParam = 0.;
82 myFirstDone = myLastDone = Standard_False;
85 //=======================================================================
88 //=======================================================================
90 void ShapeFix_EdgeProjAux::Compute (const Standard_Real preci)
92 myFirstDone = myLastDone = Standard_False;
94 // Project Point3d on Surface
95 // TEMPORARY Call ShapeFix_EdgeProjAux
99 if (IsFirstDone() && IsLastDone()) {
100 Standard_Real U1 = FirstParam();
101 Standard_Real U2 = LastParam();
104 cout << "Parametres inverses ... " << endl;
106 Standard_Real tmp = U1;
110 myFirstDone = Standard_True;
112 myLastDone = Standard_True;
116 //=======================================================================
117 //function : IsFirstDone
119 //=======================================================================
121 Standard_Boolean ShapeFix_EdgeProjAux::IsFirstDone() const
126 //=======================================================================
127 //function : IsLastDone
129 //=======================================================================
131 Standard_Boolean ShapeFix_EdgeProjAux::IsLastDone() const
136 //=======================================================================
137 //function : FirstParam
139 //=======================================================================
141 Standard_Real ShapeFix_EdgeProjAux::FirstParam() const
146 //=======================================================================
147 //function : LastParam
149 //=======================================================================
151 Standard_Real ShapeFix_EdgeProjAux::LastParam() const
156 //=======================================================================
159 //=======================================================================
161 Standard_Boolean ShapeFix_EdgeProjAux::IsIso (const Handle(Geom2d_Curve)& /*theCurve2d*/)
163 // Until an ISO is recognized by Adaptor3d_Curve
165 if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
166 Handle(Geom2d_Line) theLine2d = Handle(Geom2d_Line)::DownCast(theCurve2d);
167 gp_Dir2d theDir2d = theLine2d->Direction();
169 gp_Dir2d dir1(0.,1.);
170 gp_Dir2d dir2(0.,-1.);
172 return (theDir2d.IsEqual(dir1,Precision::Angular()) ||
173 theDir2d.IsEqual(dir2,Precision::Angular()) ||
174 theDir2d.IsNormal(dir1,Precision::Angular()) ||
175 theDir2d.IsNormal(dir2,Precision::Angular()) );
178 return Standard_False;
181 // ----------------------------------------------------------------------------
182 // static : FindParameterWithExt
183 // Purpose : Computes the trimming parameter of Pt1 on COnS
184 // ----------------------------------------------------------------------------
186 static Standard_Boolean FindParameterWithExt (const gp_Pnt& Pt1,
187 const Adaptor3d_CurveOnSurface& COnS,
188 const Standard_Real Uinf,
189 const Standard_Real Usup,
190 const Standard_Real preci,
193 try { // et allez donc !
195 Extrema_ExtPC myExtPC (Pt1, COnS, Uinf, Usup, preci);
197 //ShapeFix_ExtPCOnS myExtPCOnS1 =
198 //ShapeFix_ExtPCOnS(Pt1, COnS, Uinf, Usup, preci);
200 if (myExtPC.IsDone()) {
201 Standard_Integer NbExt1 = myExtPC.NbExt();
202 for (Standard_Integer i=1; i<=NbExt1; i++) {
203 if (myExtPC.IsMin(i)) {
204 //Standard_Real dist = myExtPC.Value(i); //szv#4:S4163:12Mar99 debug mode only
205 w1 = myExtPC.Point(i).Parameter();
208 return Standard_True;
210 else return Standard_False;
212 catch(Standard_Failure) {
213 #ifdef OCCT_DEBUG //:s5
214 cout << "Warning: ShapeFix_EdgeProjAux, FindParameterWithExt(): Exception: ";
215 Standard_Failure::Caught()->Print(cout); cout << endl;
217 return Standard_False;
221 //=======================================================================
224 //=======================================================================
226 void ShapeFix_EdgeProjAux::Init2d (const Standard_Real preci)
228 Standard_Real cl = 0., cf = 0.;
229 // Extract Geometries
230 myFirstDone = myLastDone = Standard_False;
231 Handle(Geom_Surface) theSurface = BRep_Tool::Surface(myFace);
232 Handle(Geom2d_Curve) theCurve2d = BRep_Tool::CurveOnSurface(myEdge, myFace, cf, cl);
233 if ( theCurve2d.IsNull() ) return; //:r5 abv 6 Apr 99: ec_turbine-A.stp, #4313
237 TopExp::Vertices(myEdge, V1, V2);
239 // pdn 28.12.98: r_39-db.stp #605: use ends of 3d curve instead of vertices
240 ShapeAnalysis_Edge sae;
242 Handle(Geom_Curve) C3d;
243 if(sae.Curve3d(myEdge,C3d,a,b,Standard_False)) {
248 Pt1 = BRep_Tool::Pnt(V1);
249 Pt2 = BRep_Tool::Pnt(V2);
251 //:S4136 Standard_Real preci = BRepAPI::Precision();
252 //pdn to manage degenerated case
254 Handle(ShapeAnalysis_Surface) stsu = new ShapeAnalysis_Surface (theSurface);
256 Standard_Real firstpar,lastpar;
257 if (stsu->DegeneratedValues(Pt1,preci,aPt1,aPt2,firstpar,lastpar)){
259 if(theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
260 if (aPt1.IsEqual(theCurve2d->Value(firstpar),preci) &&
261 aPt2.IsEqual(theCurve2d->Value(lastpar),preci)){
262 myFirstParam = firstpar;
263 myLastParam = lastpar;
264 myFirstDone = myLastDone = Standard_True;
269 else cout <<"Other type of deg curve"<<endl;
275 Standard_Boolean parU = Standard_False, parV = Standard_False;
276 GeomAdaptor_Surface SA = GeomAdaptor_Surface(theSurface);
277 Handle(GeomAdaptor_HSurface) myHSur = new GeomAdaptor_HSurface(SA);
279 cf = theCurve2d->FirstParameter();
280 cl = theCurve2d->LastParameter();
281 //pdn cutting pcurve by suface bounds
282 if (Precision::IsInfinite(cf)||Precision::IsInfinite(cl)) {
283 if(theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
284 Standard_Real uf,ul,vf,vl;
285 theSurface->Bounds(uf,ul,vf,vl);
286 //Correct surface limits for extrusion/revolution surfaces based on hyperbola
288 if(SA.GetType() == GeomAbs_SurfaceOfExtrusion)
290 if(SA.BasisCurve()->GetType() == GeomAbs_Hyperbola)
296 if(SA.GetType() == GeomAbs_SurfaceOfRevolution)
298 if(SA.BasisCurve()->GetType() == GeomAbs_Hyperbola)
304 if(!Precision::IsInfinite(uf)&&!Precision::IsInfinite(ul)&&
305 !Precision::IsInfinite(vf)&&!Precision::IsInfinite(vl)) {
306 Standard_Real cfi,cli;
307 Handle(Geom2d_Line) lin = Handle(Geom2d_Line)::DownCast(theCurve2d);
308 gp_Pnt2d pnt = lin->Location();
309 gp_Dir2d dir = lin->Direction();
311 parU = Standard_True;
312 cfi = (uf-pnt.X())/dir.X();
313 cli = (ul-pnt.X())/dir.X();
315 else if (dir.X()==0) {
316 parV = Standard_True;
317 cfi = (vf-pnt.Y())/dir.Y();
318 cli = (vl-pnt.Y())/dir.Y();
321 Standard_Real xfi, xli, yfi, yli;
322 xfi = (uf-pnt.X())/dir.X();
323 xli = (ul-pnt.X())/dir.X();
324 yfi = (vf-pnt.Y())/dir.Y();
325 yli = (vl-pnt.Y())/dir.Y();
326 if (dir.X()*dir.Y() > 0) {
327 cfi = (Abs(xli-xfi) < Abs(xli-yfi)? xfi : yfi);
328 cli = (Abs(xfi-xli) < Abs(xfi-yli)? xli : yli);
330 cfi = (Abs(xli-xfi) < Abs(xli-yli)? xfi : yli);
331 cli = (Abs(yli-xli) < Abs(yli-yfi)? xli : yfi);
334 if (cfi < cli) { cf = cfi; cl = cli; }
335 else { cf = cli; cl = cfi; }
337 else if(!Precision::IsInfinite(uf)&&!Precision::IsInfinite(ul)){
338 Handle(Geom2d_Line) lin = Handle(Geom2d_Line)::DownCast(theCurve2d);
339 gp_Dir2d dir = lin->Direction();
341 if (dir.Y()==0) parU = Standard_True;
342 gp_Pnt2d pnt = lin->Location(); //szv#4:S4163:12Mar99 moved
343 Standard_Real cfi = (uf-pnt.X())/dir.X();
344 Standard_Real cli = (ul-pnt.X())/dir.X();
345 if (cfi < cli) { cf = cfi; cl = cli; }
346 else { cf = cli; cl = cfi; }
356 //pdn not cutted by bounds
358 cout<<"Infinite Surface"<<endl;
363 //pdn not linear case not managed
366 if(theCurve2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
368 //Try to reparametrize (CASE dxf read bug25899)
369 Handle(Geom2d_BSplineCurve) aBspl = Handle(Geom2d_BSplineCurve)::DownCast(theCurve2d->Copy());
370 TColStd_Array1OfReal aNewKnots(1, aBspl->NbKnots());
371 aBspl->Knots(aNewKnots);
372 BSplCLib::Reparametrize(cf, cl, aNewKnots);
373 aBspl->SetKnots(aNewKnots);
378 cout<<"Some infinite curve"<<endl;
383 Geom2dAdaptor_Curve CA = Geom2dAdaptor_Curve(theCurve2d,cf,cl);
384 Handle(Geom2dAdaptor_HCurve) myHCur = new Geom2dAdaptor_HCurve(CA);
386 Adaptor3d_CurveOnSurface COnS = Adaptor3d_CurveOnSurface(myHCur, myHSur);
388 // ----------------------------------------------
389 // --- topological limit == geometric limit ? ---
390 // ----------------------------------------------
391 Standard_Real Uinf = COnS.FirstParameter();
392 Standard_Real Usup = COnS.LastParameter();
394 Standard_Real w1 = 0., w2 = 0.;
395 ShapeAnalysis_Curve sac;
397 Standard_Real dist = sac.Project(COnS,Pt1,preci,pnt,w1,Standard_False);
398 //if distance is infinite then projection is not performed
399 if( Precision::IsInfinite(dist))
402 myFirstDone = Standard_True;
405 dist = sac.Project(COnS,Pt2,preci,pnt,w2,Standard_False);
407 if( Precision::IsInfinite(dist))
410 myLastDone = Standard_True;
413 if(fabs(w1 - w2) < Precision::PConfusion())
415 if(!theSurface->IsUPeriodic() && !theSurface->IsVPeriodic())
419 if ( myFirstParam == Uinf && myLastParam == Usup ) return;
420 if ( myFirstParam == Usup && myLastParam == Uinf ) {
421 myFirstParam = theCurve2d->ReversedParameter(Usup);
422 myLastParam = theCurve2d->ReversedParameter(Uinf);
423 theCurve2d->Reverse();
425 cout << "Warning: ShapeFix_EdgeProjAux: pcurve reversed" << endl;
429 //:abv 29.08.01: SAT: fix for closed case
430 if ( COnS.Value(Uinf).Distance ( COnS.Value(Usup) ) < Precision::Confusion() ) {
431 // 18.11.2002 SKL OCC630 compare values with tolerance Precision::PConfusion() instead of "=="
432 if ( Abs(myFirstParam-Uinf) < ::Precision::PConfusion() &&
433 Abs(myLastParam-Uinf) < ::Precision::PConfusion() )
434 myLastParam = w2 = Usup;
435 // 18.11.2002 SKL OCC630 compare values with tolerance Precision::PConfusion() instead of "=="
436 else if ( Abs(myFirstParam-Usup) < ::Precision::PConfusion() &&
437 Abs(myLastParam-Usup) < ::Precision::PConfusion() )
438 myFirstParam = w1 = Uinf;
441 //pdn adjust parameters in periodic case
443 Standard_Real uf,ul,vf,vl;
444 theSurface->Bounds(uf,ul,vf,vl);
445 Standard_Real period = (parU ? ul-uf : vl-vf);
446 w1+=ShapeAnalysis::AdjustToPeriod(w1,0,period);
448 w2+=ShapeAnalysis::AdjustToPeriod(w2,0,period);
450 Handle(Geom_Curve) C3d1;
451 if(!sae.Curve3d (myEdge, C3d1, cf, cl, Standard_False )) {
452 UpdateParam2d(theCurve2d);
455 gp_Pnt mid = C3d1->Value((cf+cl)/2);
457 sac.Project(COnS,mid,preci,pnt,wmid,Standard_False);
458 wmid+=ShapeAnalysis::AdjustToPeriod(wmid,0,period);
460 if(w2 > wmid) myFirstParam -= period;
462 UpdateParam2d(theCurve2d);
466 cout <<" Added"<<endl;
472 myLastParam -=period;
473 UpdateParam2d(theCurve2d);
475 cout <<" Added & Inverted"<<endl;
477 } else if (w2 < wmid) {
478 myFirstParam += period;
479 UpdateParam2d(theCurve2d);
483 UpdateParam2d(theCurve2d);
487 //=======================================================================
490 //=======================================================================
492 void ShapeFix_EdgeProjAux::Init3d (const Standard_Real preci)
494 Standard_Real cl, cf;
496 // Extract Geometries
497 Handle(Geom_Surface) theSurface = BRep_Tool::Surface(myFace);
498 Handle(Geom2d_Curve) theCurve2d = BRep_Tool::CurveOnSurface(myEdge, myFace, cf, cl);
499 if ( theCurve2d.IsNull() ) return; //:r5 abv 6 Apr 99: ec_turbine-A.stp, #4313
502 V1 = TopExp::FirstVertex(myEdge);
503 V2 = TopExp::LastVertex(myEdge);
504 gp_Pnt Pt1 = BRep_Tool::Pnt(V1);
505 gp_Pnt Pt2 = BRep_Tool::Pnt(V2);
508 GeomAdaptor_Surface SA = GeomAdaptor_Surface(theSurface);
509 Handle(GeomAdaptor_HSurface) myHSur = new GeomAdaptor_HSurface(SA);
511 Geom2dAdaptor_Curve CA = Geom2dAdaptor_Curve(theCurve2d);
512 Handle(Geom2dAdaptor_HCurve) myHCur = new Geom2dAdaptor_HCurve(CA);
514 Adaptor3d_CurveOnSurface COnS = Adaptor3d_CurveOnSurface(myHCur, myHSur);
516 //:S4136 Standard_Real preci = BRepAPI::Precision();
517 Standard_Real Uinf = theCurve2d->FirstParameter();
518 Standard_Real Usup = theCurve2d->LastParameter();
520 // ----------------------------------------------
521 // --- topological limit == geometric limit ? ---
522 // ----------------------------------------------
524 if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_BoundedCurve))) {
526 gp_Pnt Pdeb = COnS.Value(Uinf);
527 gp_Pnt Pfin = COnS.Value(Usup);
529 //szv#4:S4163:12Mar99 optimized
530 if ( Pdeb.IsEqual(Pt1, preci) && Pfin.IsEqual(Pt2, preci) ) {
533 myFirstDone = myLastDone = Standard_True;
538 // ------------------------------------------
539 // --- The CurveOnSurface is not infinite ---
540 // --- Try with Extrema ---
541 // ------------------------------------------
543 Standard_Real w1 = COnS.FirstParameter();
544 Standard_Real w2 = COnS.LastParameter();
546 if ((!Precision::IsInfinite(w1) &&
547 !Precision::IsInfinite(w2) &&
548 theCurve2d->Continuity() != GeomAbs_C0) ||
551 //szv#4:S4163:12Mar99 optimized
552 if ( FindParameterWithExt(Pt1, COnS, Uinf, Usup, preci, w1) &&
553 FindParameterWithExt(Pt2, COnS, Uinf, Usup, preci, w2) ) {
556 UpdateParam2d(theCurve2d);
557 myFirstDone = myLastDone = Standard_True;
561 myFirstDone = myLastDone = Standard_True;
564 //=======================================================================
565 //function : UpdateParam2d
567 //=======================================================================
569 void ShapeFix_EdgeProjAux::UpdateParam2d (const Handle(Geom2d_Curve)& theCurve2d)
571 if (myFirstParam < myLastParam) return;
573 Standard_Real cf = theCurve2d->FirstParameter();
574 Standard_Real cl = theCurve2d->LastParameter();
575 //:S4136 Standard_Real preci = BRepAPI::Precision();
576 Standard_Real preci2d = Precision::PConfusion(); //:S4136: Parametric(preci, 0.01);
578 // 15.11.2002 PTV OCC966
579 if (ShapeAnalysis_Curve::IsPeriodic(theCurve2d)) {
580 ElCLib::AdjustPeriodic(cf,cl,preci2d,myFirstParam,myLastParam);
582 else if (theCurve2d->IsClosed()) {
583 //szv#4:S4163:12Mar99 optimized
584 if ( Abs ( myFirstParam - cl ) <= preci2d ) myFirstParam = cf;
585 else if ( Abs ( myLastParam - cf ) <= preci2d ) myLastParam = cl;
588 cout << "Error : curve 2d range crossing non periodic curve origin";
595 // the curve is closed within the 'file' 2D tolerance
596 else if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
597 Handle(Geom2d_BSplineCurve) aBSpline2d =
598 Handle(Geom2d_BSplineCurve)::DownCast(theCurve2d);
599 if (aBSpline2d->StartPoint().Distance(aBSpline2d->EndPoint()) <= preci2d) {
600 if ( Abs ( myFirstParam - cl ) <= preci2d ) myFirstParam = cf;
601 else if ( Abs ( myLastParam - cf ) <= preci2d ) myLastParam = cl;
606 cout << "Warning : non increasing parameters for 2d curve." << endl;
607 cout << " update parameter 2d uncertain." << endl;
609 Standard_Real tmp1 = myFirstParam, tmp2 = myLastParam;
610 myFirstParam = theCurve2d->ReversedParameter(tmp1);
611 myLastParam = theCurve2d->ReversedParameter(tmp2);
612 theCurve2d->Reverse();
613 //cout<<"Reversed case 2"<<endl;