1 // Created on: 1995-01-04
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1995-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.
17 #include <BRepLib_MakeEdge2d.ixx>
19 #include <BRepLib.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRep_Builder.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom2d_Line.hxx>
25 #include <Geom2d_Circle.hxx>
26 #include <Geom2d_Ellipse.hxx>
27 #include <Geom2d_Parabola.hxx>
28 #include <Geom2d_Hyperbola.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <Geom2dAdaptor_Curve.hxx>
31 #include <Extrema_ExtPC2d.hxx>
35 #include <Precision.hxx>
38 //=======================================================================
40 //purpose : make a 3d point on the current plane
41 //=======================================================================
43 static gp_Pnt Point(const gp_Pnt2d& P)
45 return BRepLib::Plane()->Value(P.X(),P.Y());
48 //=======================================================================
50 //purpose : project a vertex on the current plane
51 //=======================================================================
53 static gp_Pnt2d Project(const TopoDS_Vertex& Ve)
55 gp_Pnt P = BRep_Tool::Pnt(Ve);
57 ElSLib::Parameters(BRepLib::Plane()->Pln(),P,U,V);
61 //=======================================================================
63 //purpose : project a vertex on a curve
64 //=======================================================================
66 static Standard_Boolean Project(const Handle(Geom2d_Curve)& C,
67 const TopoDS_Vertex& V,
70 gp_Pnt2d P = Project(V);
71 Geom2dAdaptor_Curve AC(C);
72 if (AC.GetType() == GeomAbs_Line) {
73 p = ElCLib::LineParameter(AC.Line().Position(),P);
75 else if (AC.GetType() == GeomAbs_Circle) {
76 p = ElCLib::CircleParameter(AC.Circle().Position(),P);
79 Extrema_ExtPC2d extrema(P,AC);
80 if (extrema.IsDone()) {
81 Standard_Integer i,n = extrema.NbExt();
83 Standard_Real d2 = RealLast();
84 for (i = 1; i <= n; i++) {
85 //OCC16852:if (extrema.IsMin(i)) {
86 const Standard_Real dd2 = extrema.SquareDistance(i);
89 p = extrema.Point(i).Parameter();
95 return Standard_False;
100 //=======================================================================
101 //function : BRepLib_MakeEdge2d
103 //=======================================================================
105 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const TopoDS_Vertex& V1,
106 const TopoDS_Vertex& V2)
108 gp_Pnt2d P1 = Project(V1);
109 gp_Pnt2d P2 = Project(V2);
110 Standard_Real l = P1.Distance(P2);
111 if (l <= gp::Resolution()) {
112 myError = BRepLib_LineThroughIdenticPoints;
115 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
116 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
120 //=======================================================================
121 //function : BRepLib_MakeEdge2d
123 //=======================================================================
125 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Pnt2d& P1,
128 Standard_Real l = P1.Distance(P2);
129 if (l <= gp::Resolution()) {
130 myError = BRepLib_LineThroughIdenticPoints;
133 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
134 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
139 //=======================================================================
140 //function : BRepLib_MakeEdge2d
142 //=======================================================================
144 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L)
146 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
150 //=======================================================================
151 //function : BRepLib_MakeEdge2d
153 //=======================================================================
155 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
156 const Standard_Real p1,
157 const Standard_Real p2)
159 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
163 //=======================================================================
164 //function : BRepLib_MakeEdge2d
166 //=======================================================================
168 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
172 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
176 //=======================================================================
177 //function : BRepLib_MakeEdge2d
179 //=======================================================================
181 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
182 const TopoDS_Vertex& V1,
183 const TopoDS_Vertex& V2)
185 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
189 //=======================================================================
190 //function : BRepLib_MakeEdge2d
192 //=======================================================================
194 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C)
196 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
200 //=======================================================================
201 //function : BRepLib_MakeEdge2d
203 //=======================================================================
205 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
206 const Standard_Real p1,
207 const Standard_Real p2)
209 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
213 //=======================================================================
214 //function : BRepLib_MakeEdge2d
216 //=======================================================================
218 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
222 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
226 //=======================================================================
227 //function : BRepLib_MakeEdge2d
229 //=======================================================================
231 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
232 const TopoDS_Vertex& V1,
233 const TopoDS_Vertex& V2)
235 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
239 //=======================================================================
240 //function : BRepLib_MakeEdge2d
242 //=======================================================================
244 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E)
246 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
250 //=======================================================================
251 //function : BRepLib_MakeEdge2d
253 //=======================================================================
255 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
256 const Standard_Real p1,
257 const Standard_Real p2)
259 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
263 //=======================================================================
264 //function : BRepLib_MakeEdge2d
266 //=======================================================================
268 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
272 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
276 //=======================================================================
277 //function : BRepLib_MakeEdge2d
279 //=======================================================================
281 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
282 const TopoDS_Vertex& V1,
283 const TopoDS_Vertex& V2)
285 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
289 //=======================================================================
290 //function : BRepLib_MakeEdge2d
292 //=======================================================================
294 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H)
296 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
300 //=======================================================================
301 //function : BRepLib_MakeEdge2d
303 //=======================================================================
305 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
306 const Standard_Real p1,
307 const Standard_Real p2)
309 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
313 //=======================================================================
314 //function : BRepLib_MakeEdge2d
316 //=======================================================================
318 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
322 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
326 //=======================================================================
327 //function : BRepLib_MakeEdge2d
329 //=======================================================================
331 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
332 const TopoDS_Vertex& V1,
333 const TopoDS_Vertex& V2)
335 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
339 //=======================================================================
340 //function : BRepLib_MakeEdge2d
342 //=======================================================================
344 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P)
346 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
350 //=======================================================================
351 //function : BRepLib_MakeEdge2d
353 //=======================================================================
355 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
356 const Standard_Real p1,
357 const Standard_Real p2)
359 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
363 //=======================================================================
364 //function : BRepLib_MakeEdge2d
366 //=======================================================================
368 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
372 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
376 //=======================================================================
377 //function : BRepLib_MakeEdge2d
379 //=======================================================================
381 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
382 const TopoDS_Vertex& V1,
383 const TopoDS_Vertex& V2)
385 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
389 //=======================================================================
390 //function : BRepLib_MakeEdge2d
392 //=======================================================================
394 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L)
399 //=======================================================================
400 //function : BRepLib_MakeEdge2d
402 //=======================================================================
404 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
405 const Standard_Real p1,
406 const Standard_Real p2)
411 //=======================================================================
412 //function : BRepLib_MakeEdge2d
414 //=======================================================================
416 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
423 //=======================================================================
424 //function : BRepLib_MakeEdge2d
426 //=======================================================================
428 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
429 const TopoDS_Vertex& V1,
430 const TopoDS_Vertex& V2)
435 //=======================================================================
436 //function : BRepLib_MakeEdge2d
438 //=======================================================================
440 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
443 const Standard_Real p1,
444 const Standard_Real p2)
449 //=======================================================================
450 //function : BRepLib_MakeEdge2d
452 //=======================================================================
454 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
455 const TopoDS_Vertex& V1,
456 const TopoDS_Vertex& V2,
457 const Standard_Real p1,
458 const Standard_Real p2)
463 //=======================================================================
466 //=======================================================================
468 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C)
470 Init(C,C->FirstParameter(),C->LastParameter());
473 //=======================================================================
476 //=======================================================================
478 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
479 const Standard_Real p1,
480 const Standard_Real p2)
488 //=======================================================================
491 //=======================================================================
493 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
499 B.MakeVertex(V1,Point(P1),Precision::Confusion());
500 if (P1.Distance(P2) < Precision::Confusion())
503 B.MakeVertex(V2,Point(P2),Precision::Confusion());
507 //=======================================================================
510 //=======================================================================
512 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
513 const TopoDS_Vertex& V1,
514 const TopoDS_Vertex& V2)
516 // try projecting the vertices on the curve
521 p1 = C->FirstParameter();
523 if (!Project(C,V1,p1)) {
524 myError = BRepLib_PointProjectionFailed;
528 p2 = C->LastParameter();
530 if (!Project(C,V2,p2)) {
531 myError = BRepLib_PointProjectionFailed;
539 //=======================================================================
542 //=======================================================================
544 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
547 const Standard_Real p1,
548 const Standard_Real p2)
553 B.MakeVertex(V1,Point(P1),Precision::Confusion());
554 if (P1.Distance(P2) < Precision::Confusion())
557 B.MakeVertex(V2,Point(P2),Precision::Confusion());
564 //=======================================================================
566 //purpose : this one really makes the job ...
567 //=======================================================================
569 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& CC,
570 const TopoDS_Vertex& VV1,
571 const TopoDS_Vertex& VV2,
572 const Standard_Real pp1,
573 const Standard_Real pp2)
575 // kill trimmed curves
576 Handle(Geom2d_Curve) C = CC;
577 Handle(Geom2d_TrimmedCurve) CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
578 while (!CT.IsNull()) {
579 C = CT->BasisCurve();
580 CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
584 Standard_Real p1 = pp1;
585 Standard_Real p2 = pp2;
586 Standard_Real cf = C->FirstParameter();
587 Standard_Real cl = C->LastParameter();
588 Standard_Real epsilon = Precision::Confusion();
589 Standard_Boolean periodic = C->IsPeriodic();
595 ElCLib::AdjustPeriodic(cf,cl,epsilon,p1,p2);
608 Standard_Real x = p1;
614 if ((cf - p1 > epsilon) || (p2 - cl > epsilon)) {
615 myError = BRepLib_ParameterOutOfRange;
620 // compute points on the curve
621 Standard_Boolean p1inf = Precision::IsNegativeInfinite(p1);
622 Standard_Boolean p2inf = Precision::IsPositiveInfinite(p2);
624 if (!p1inf) P1 = C->Value(p1);
625 if (!p2inf) P2 = C->Value(p2);
627 Standard_Real preci = Precision::Confusion();
630 // check for closed curve
631 Standard_Boolean closed = Standard_False;
632 if (!p1inf && !p2inf)
633 closed = (P1.Distance(P2) <= preci);
635 // check if the vertices are on the curve
637 if (V1.IsNull() && V2.IsNull()) {
638 B.MakeVertex(V1,Point(P1),preci);
641 else if (V1.IsNull())
643 else if (V2.IsNull())
646 if (!V1.IsSame(V2)) {
647 myError = BRepLib_DifferentPointsOnClosedCurve;
650 else if (Point(P1).Distance(BRep_Tool::Pnt(V1)) > preci) {
651 myError = BRepLib_DifferentPointsOnClosedCurve;
661 myError = BRepLib_PointWithInfiniteParameter;
666 gp_Pnt P = Point(P1);
668 B.MakeVertex(V1,P,preci);
671 // desctivate control (RLE) for speed in sketcher
672 else if (P.Distance(BRep_Tool::Pnt(V1)) > preci) {
673 myError = BRepLib_DifferentsPointAndParameter;
681 myError = BRepLib_PointWithInfiniteParameter;
686 gp_Pnt P = Point(P2);
688 B.MakeVertex(V2,P,preci);
691 // desctivate control (RLE) for speed in sketcher
692 else if (P.Distance(BRep_Tool::Pnt(V2)) > preci){
693 myError = BRepLib_DifferentsPointAndParameter;
700 V1.Orientation(TopAbs_FORWARD);
701 V2.Orientation(TopAbs_REVERSED);
705 TopoDS_Edge& E = TopoDS::Edge(myShape);
707 B.UpdateEdge(E,C,BRepLib::Plane(),TopLoc_Location(),preci);
718 //=======================================================================
721 //=======================================================================
723 BRepLib_EdgeError BRepLib_MakeEdge2d::Error() const
728 //=======================================================================
731 //=======================================================================
733 const TopoDS_Edge& BRepLib_MakeEdge2d::Edge()const
735 return TopoDS::Edge(Shape());
739 //=======================================================================
742 //=======================================================================
744 const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex1()const
751 //=======================================================================
754 //=======================================================================
756 const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex2()const
764 //=======================================================================
765 //function : operator
767 //=======================================================================
769 BRepLib_MakeEdge2d::operator TopoDS_Edge() const