1 // File: BRepFill_TrimSurfaceTool.cxx
2 // Created: Fri Oct 21 14:56:32 1994
3 // Author: Bruno DUMORTIER
9 #include <BRepFill_TrimSurfaceTool.ixx>
11 #include <Precision.hxx>
13 #include <Adaptor3d_SurfaceOfRevolution.hxx>
14 #include <AppParCurves_MultiCurve.hxx>
15 #include <BRepFill_ComputeCLine.hxx>
16 #include <BRepFill_MultiLine.hxx>
17 #include <BRepIntCurveSurface_Inter.hxx>
18 #include <BRepFill_ApproxSeewing.hxx>
19 #include <BRep_Tool.hxx>
21 #include <BSplCLib.hxx>
24 #include <gp_Sphere.hxx>
25 #include <gp_Cone.hxx>
26 #include <gp_Torus.hxx>
27 #include <gp_Cylinder.hxx>
28 #include <Geom2dInt_GInter.hxx>
29 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
30 #include <Geom2dAdaptor_Curve.hxx>
31 #include <Geom2d_BSplineCurve.hxx>
32 #include <Geom2d_TrimmedCurve.hxx>
33 #include <GeomAdaptor_Surface.hxx>
34 #include <GeomAdaptor_HCurve.hxx>
35 #include <Geom_Plane.hxx>
36 #include <Geom_SurfaceOfRevolution.hxx>
37 #include <Geom_BSplineCurve.hxx>
38 #include <Geom_TrimmedCurve.hxx>
39 #include <GeomProjLib.hxx>
40 #include <IntRes2d_IntersectionPoint.hxx>
41 #include <IntRes2d_IntersectionSegment.hxx>
42 #include <Precision.hxx>
43 #include <StdFail_NotDone.hxx>
44 #include <Standard_NotImplemented.hxx>
45 #include <TColgp_Array1OfPnt.hxx>
46 #include <TColStd_Array1OfReal.hxx>
47 #include <TColStd_Array1OfInteger.hxx>
48 #include <TopAbs_State.hxx>
51 #include <TopoDS_Vertex.hxx>
54 #include <DrawTrSurf.hxx>
58 static Standard_Boolean Affich = Standard_False;
59 static Standard_Integer NBCALL = 1;
62 //=======================================================================
63 //function : BRepFill_TrimSurfaceTool
64 //purpose : Initialisation with two neighbor faces
65 // Edge1 and Edge2 are parallel edges corresponding
66 // to minimum iso on F1 and F2 respectively.
67 // ie Edge1 is Umin or VMin on F1.
68 // Inv1 and Inv2 show if Edge1 and Edge2 are
70 //=======================================================================
72 BRepFill_TrimSurfaceTool::BRepFill_TrimSurfaceTool
73 (const Handle(Geom2d_Curve)& Bis,
74 const TopoDS_Face& Face1,
75 const TopoDS_Face& Face2,
76 const TopoDS_Edge& Edge1,
77 const TopoDS_Edge& Edge2,
78 const Standard_Boolean Inv1,
79 const Standard_Boolean Inv2 ) :
90 cout << " ---------->TrimSurfaceTool : NBCALL = " << NBCALL << endl;
95 sprintf(name,"FACE1_%d",NBCALL);
96 DBRep::Set(name,myFace1);
98 sprintf(name,"FACE2_%d",NBCALL);
99 DBRep::Set(name,myFace2);
101 sprintf(name,"EDGE1_%d",NBCALL);
102 DBRep::Set(name,myEdge1);
104 sprintf(name,"EDGE2_%d",NBCALL);
105 DBRep::Set(name,myEdge2);
107 sprintf(name,"BISSEC_%d",NBCALL);
108 DrawTrSurf::Set(name,myBis);
116 //=======================================================================
118 //purpose : Order the sequence of points by increasing x.
119 //=======================================================================
121 static void Bubble(TColgp_SequenceOfPnt& Seq)
123 Standard_Boolean Invert = Standard_True;
124 Standard_Integer NbPoints = Seq.Length();
126 Invert = Standard_False;
127 for ( Standard_Integer i = 1; i < NbPoints; i++) {
128 gp_Pnt P1 = Seq.Value(i);
129 gp_Pnt P2 = Seq.Value(i+1);
132 Invert = Standard_True;
139 //=======================================================================
140 //function : EvalPhase
142 //=======================================================================
144 static Standard_Real EvalPhase(const TopoDS_Edge& Edge,
145 const TopoDS_Face& Face,
146 const GeomAdaptor_Surface& GAS,
149 gp_Pnt2d PE1,PE2,PF1,PF2;
152 Standard_Real V = 0.;
156 BRep_Tool::UVPoints(Edge,Face,PE1,PE2);
158 TopExp_Explorer Exp(Face,TopAbs_EDGE);
159 for (; Exp.More(); Exp.Next()) {
160 if ( !TopoDS::Edge(Exp.Current()).IsSame(Edge)) {
161 BRep_Tool::UVPoints(TopoDS::Edge(Exp.Current()),Face,PF1,PF2);
162 V = ( Abs(PF1.Y() - VDeg) > Abs(PF2.Y() - VDeg)) ? PF1.Y() : PF2.Y();
166 gp_Pnt P = GAS.Value(0., V);
168 if ( gp_Vec(Axis.Location(), P).Dot(Axis.XDirection()) < 0.)
175 //=======================================================================
176 //function : EvalParameters
178 //=======================================================================
180 static void EvalParameters(const TopoDS_Edge& Edge,
181 const TopoDS_Face& Face,
182 const Handle(Geom2d_Curve)& Bis ,
183 TColgp_SequenceOfPnt& Seq )
185 Standard_Boolean Degener = BRep_Tool::Degenerated(Edge);
186 // return curves 3d associated to edges.
190 Handle(Geom_TrimmedCurve) CT;
191 Handle(Geom_Plane) Plane = new Geom_Plane(0,0,1,0);
193 Geom2dInt_GInter Intersector;
195 Standard_Integer NbPoints, NbSegments;
196 Standard_Real U1, U2;
199 // Standard_Real Tol = Precision::Intersection();
200 // modified by NIZHNY-EAP Wed Dec 22 15:00:51 1999 ___BEGIN___
201 Standard_Real Tol = 1.e-6; // BRepFill_Precision();
202 Standard_Real TolC = 0.;
205 Handle(Geom_Curve) C = BRep_Tool::Curve(Edge,L,f,l);
206 CT = new Geom_TrimmedCurve(C,f,l);
207 CT->Transform(L.Transformation());
208 // projection of 3d curves in the plane xOy
209 Handle(Geom2d_Curve) C2d = GeomProjLib::Curve2d(CT,Plane);
211 Geom2dAdaptor_Curve AC(C2d);
212 Geom2dAdaptor_Curve ABis(Bis);
214 Intersector = Geom2dInt_GInter(ABis, AC, TolC, Tol);
216 if ( !Intersector.IsDone()) {
217 StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
220 NbPoints = Intersector.NbPoints();
223 // try to elongate curves and enlarge tolerance
225 // don't do it rightaway from the beginning in order not to get
226 // extra solutions those would cause *Exception*: incoherent intersection
228 GeomAbs_CurveType CType = AC.GetType(), BisType = ABis.GetType();
229 Standard_Boolean canElongateC = !(CType == GeomAbs_BezierCurve ||
230 CType == GeomAbs_BSplineCurve ||
231 CType == GeomAbs_OtherCurve);
232 Standard_Boolean canElongateBis = !(BisType == GeomAbs_BezierCurve ||
233 BisType == GeomAbs_BSplineCurve ||
234 BisType == GeomAbs_OtherCurve);
236 Handle(Geom2d_TrimmedCurve) TBis = Handle(Geom2d_TrimmedCurve)::DownCast(Bis);
237 Handle(Geom2d_TrimmedCurve) TC2d = Handle(Geom2d_TrimmedCurve)::DownCast(C2d);
240 TC2d->SetTrim(TC2d->FirstParameter() - Tol, TC2d->LastParameter() + Tol);
243 if (canElongateBis) {
244 TBis->SetTrim(TBis->FirstParameter() - Tol, TBis->LastParameter() + Tol);
247 Intersector = Geom2dInt_GInter(ABis, AC, TolC, Tol*10);
249 if ( !Intersector.IsDone()) {
250 StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
253 NbPoints = Intersector.NbPoints();
255 // modified by NIZHNY-EAP Wed Dec 22 15:00:56 1999 ___END___
258 for ( Standard_Integer i = 1; i <= NbPoints; i++) {
259 U1 = Intersector.Point(i).ParamOnFirst();
260 U2 = Intersector.Point(i).ParamOnSecond();
261 P = gp_Pnt(U1,U2,0.);
266 NbSegments = Intersector.NbSegments();
268 if (NbSegments > 0) {
270 cout << " IntersectWith : " << NbSegments
271 << " Segments of intersection" << endl;
273 IntRes2d_IntersectionSegment Seg;
274 for ( Standard_Integer i = 1; i <= NbSegments; i++) {
275 Seg = Intersector.Segment(i);
276 U1 = Seg.FirstPoint().ParamOnFirst();
277 U1 += Seg.LastPoint().ParamOnFirst();
279 U2 = Seg.FirstPoint().ParamOnSecond();
280 U2 += Seg.LastPoint().ParamOnSecond();
282 P = gp_Pnt(U1,U2,0.);
286 // Order the sequence by increasing parameter on the bissectrice.
289 // modified by NIZHNY-EAP Fri Dec 24 18:47:24 1999 ___BEGIN___
290 // Remove double points
292 for ( Standard_Integer i = 1; i < NbPoints; i++) {
295 if ( P2.X()-P1.X() < Tol ) {
296 // cout<<"REMOVE "<<P1.X()<<endl;
301 // modified by NIZHNY-EAP Fri Dec 24 18:47:28 1999 ___END___
304 // the edge is degenerated : the point and it is found if it is
305 // on the bissectrice.
307 gp_Pnt P3d = BRep_Tool::Pnt( TopExp::FirstVertex(Edge));
308 gp_Pnt2d P2d( P3d.X(), P3d.Y());
310 Standard_Real UBis = Bis->FirstParameter();
311 gp_Pnt2d PBis = Bis->Value( UBis);
313 // modified by NIZHNY-EAP Wed Jan 12 11:41:30 2000 ___BEGIN___
314 // inside gp_Pnt2d::Distance
315 // Infinite * Infinite => Exception: DefaultNumericError
316 // Case encounered: UBis < Precision::Infinite()
317 // but PBis.X() > Precision::Infinite()
318 if (Precision::IsPositiveInfinite(Abs(PBis.X())) ||
319 Precision::IsPositiveInfinite(Abs(PBis.Y())) ||
320 PBis.Distance(P2d) > Tol) {
321 // modified by NIZHNY-EAP Wed Jan 12 11:41:40 2000 ___END___
322 UBis = Bis->LastParameter();
323 if (UBis >= Precision::Infinite()) return;
324 PBis = Bis->Value( UBis);
325 if ( PBis.Distance(P2d) > Tol) return;
328 // evaluate parameter intersection.
329 Handle(Geom_Surface) GS = BRep_Tool::Surface(Face);
330 GeomAdaptor_Surface GAS(GS);
333 Standard_Real Phase = 0.;
335 switch ( GAS.GetType()) {
338 Axis = GAS.Sphere().Position(); break;
340 //----------------------------------------------------------
341 // if myFace1 is not at the same side of the apex as the point
342 // of parameter 0 0 on the cone => phase = PI.
343 //----------------------------------------------------------
344 Axis = GAS.Cone().Position();
345 Phase = EvalPhase(Edge,Face,GAS,Axis);
349 Axis = GAS.Torus().Position(); break;
350 case GeomAbs_Cylinder:
351 Axis = GAS.Cylinder().Position(); break;
352 case GeomAbs_SurfaceOfRevolution: {
353 //----------------------------------------------------------
354 // if myFace1 is not at the same side of the apex as the point
355 // of parameter 0 0 on the cone => phase = PI.
356 //----------------------------------------------------------
357 Handle(Geom_SurfaceOfRevolution) GSRev =
358 Handle(Geom_SurfaceOfRevolution)::DownCast(GS);
359 Handle(GeomAdaptor_HCurve) HC =
360 new GeomAdaptor_HCurve(GSRev->BasisCurve());
361 Adaptor3d_SurfaceOfRevolution ASRev(HC,GAS.AxeOfRevolution());
363 Phase = EvalPhase(Edge,Face,GAS,Axis);
367 Standard_NotImplemented::Raise(" BRepFill_TrimSurfaceTool");
370 gp_Vec2d D12d = Bis->DN(UBis,1);
371 gp_Vec D1( D12d.X(), D12d.Y(), 0.);
373 Standard_Real U = Axis.XDirection().
374 AngleWithRef(D1,Axis.XDirection()^Axis.YDirection());
376 if ( U < 0.) U += 2*PI;
378 P = gp_Pnt(Bis->FirstParameter(), U, 0.);
384 //=======================================================================
385 //function : IntersectWith
387 //=======================================================================
389 void BRepFill_TrimSurfaceTool::IntersectWith
390 (const TopoDS_Edge& EdgeOnF1,
391 const TopoDS_Edge& EdgeOnF2,
392 TColgp_SequenceOfPnt& Points )
396 TColgp_SequenceOfPnt Points2;
398 EvalParameters(EdgeOnF1, myFace1, myBis, Points);
399 EvalParameters(EdgeOnF2, myFace2, myBis, Points2);
401 StdFail_NotDone_Raise_if
402 ( Points.Length() != Points2.Length(),
403 "BRepFill_TrimSurfaceTool::IntersectWith: incoherent intersection");
406 Standard_Integer NbPoints = Points.Length();
407 for ( Standard_Integer i = 1; i <= NbPoints; i++) {
409 PSeq.SetZ((Points2.Value(i)).Y());
410 Points.SetValue(i,PSeq);
411 // cout<<"BisPar "<<PSeq.X()<<endl;
416 //=======================================================================
417 //function : IsOnFace
419 //=======================================================================
421 Standard_Boolean BRepFill_TrimSurfaceTool::IsOnFace
422 (const gp_Pnt2d& Point) const
424 gp_Pnt P( Point.X(), Point.Y(), 0.);
425 gp_Lin Line( P, gp::DZ());
427 BRepIntCurveSurface_Inter Inter;
429 // eval if is on face 1
430 // modified by NIZHNY-EAP Fri Jan 21 09:49:09 2000 ___BEGIN___
431 Inter.Init(myFace1, Line, 1e-6);//Precision::PConfusion());
432 if (Inter.More()) return Standard_True;
434 // eval if is on face 2
435 Inter.Init(myFace2, Line, 1e-6);//Precision::PConfusion());
437 // modified by NIZHNY-EAP Fri Jan 21 09:49:14 2000 ___END___
441 //=======================================================================
444 //=======================================================================
446 Standard_Real BRepFill_TrimSurfaceTool::ProjOn(const gp_Pnt2d& Point,
447 const TopoDS_Edge& Edge) const
454 Handle(Geom_Curve) C1 = BRep_Tool::Curve(Edge,L,f,l);
455 Handle(Geom_TrimmedCurve) CT = new Geom_TrimmedCurve(C1,f,l);
456 CT->Transform(L.Transformation());
458 // projection of curves 3d in the plane xOy
459 Handle(Geom_Plane) Plane = new Geom_Plane(0,0,1,0);
460 Handle(Geom2d_Curve) C2d = GeomProjLib::Curve2d(CT,Plane);
462 // evaluate the projection of the point on the curve.
463 Geom2dAPI_ProjectPointOnCurve Projector(Point, C2d);
467 Projector.LowerDistance();
469 if ( Dist > Precision::Confusion() ) {
470 cout << " *** WARNING TrimSurfaceTool: *** " << endl;
471 cout << " --> the point is not on the edge" <<endl;
472 cout << " distance = " << Dist << endl;
476 Standard_Real U = Projector.LowerDistanceParameter();
481 //=======================================================================
484 //=======================================================================
486 void BRepFill_TrimSurfaceTool::Project
487 (const Standard_Real U1,
488 const Standard_Real U2,
489 Handle(Geom_Curve)& Curve,
490 Handle(Geom2d_Curve)& PCurve1,
491 Handle(Geom2d_Curve)& PCurve2,
492 GeomAbs_Shape& Cont) const
494 Standard_Integer Deg1, Deg2;
497 Handle(Geom2d_TrimmedCurve) CT =
498 new Geom2d_TrimmedCurve(myBis,U1,U2);
499 BRepFill_MultiLine ML(myFace1,myFace2,
500 myEdge1,myEdge2,myInv1,myInv2,CT);
502 Cont = ML.Continuity();
504 if ( ML.IsParticularCase()) {
505 ML.Curves(Curve,PCurve1,PCurve2);
508 BRepFill_ApproxSeewing AppSeew(ML);
510 Curve = AppSeew.Curve();
511 PCurve1 = AppSeew.CurveOnF1();
512 PCurve2 = AppSeew.CurveOnF2();