1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <ChFi2d_AnaFilletAlgo.hxx>
21 #include <gp_Circ.hxx>
22 #include <gp_Lin2d.hxx>
23 #include <gp_Circ2d.hxx>
25 #include <Standard_TypeMismatch.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <GeomAPI_ExtremaCurveCurve.hxx>
32 #include <IntAna2d_AnaIntersection.hxx>
33 #include <ShapeAnalysis_Wire.hxx>
34 #include <Geom_Circle.hxx>
36 #include <BRepAdaptor_Curve.hxx>
37 #include <BRep_Tool.hxx>
40 #include <TopoDS_Iterator.hxx>
42 #include <ProjLib.hxx>
46 // Compute the flag: CW || CCW
47 static Standard_Boolean isCW(const BRepAdaptor_Curve& AC)
49 const Standard_Real f = AC.FirstParameter();
50 const Standard_Real l = AC.LastParameter();
51 Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(AC.Curve().Curve());
52 gp_Pnt start = AC.Value(f);
53 gp_Pnt end = AC.Value(l);
54 gp_Pnt center = AC.Circle().Location();
55 gp_Ax3 plane = AC.Circle().Position();
57 // Get point on circle at half angle
59 circle->D0(0.5 * (f + l), m);
61 // Compare angles between vectors to middle point and to the end point.
62 gp_Vec startv(center, start), endv(center, end), middlev(center, m);
63 double middlea = startv.AngleWithRef(middlev, plane.Direction());
65 middlea += 2.0 * M_PI;
66 double enda = startv.AngleWithRef(endv, plane.Direction());
70 Standard_Boolean is_cw = middlea > enda ? Standard_True : Standard_False;
74 // Equality of points computed through square distance between the points.
75 static Standard_Boolean IsEqual(const gp_Pnt& p1, const gp_Pnt& p2)
77 return p1.SquareDistance(p2) < Precision::SquareConfusion();
79 static Standard_Boolean IsEqual(const gp_Pnt2d& p1, const gp_Pnt2d& p2)
81 return p1.SquareDistance(p2) < Precision::SquareConfusion();
84 // An empty constructor.
85 // Use the method Init() to initialize the class.
86 ChFi2d_AnaFilletAlgo::ChFi2d_AnaFilletAlgo()
92 // It expects two edges having a common point of type:
95 ChFi2d_AnaFilletAlgo::ChFi2d_AnaFilletAlgo(const TopoDS_Wire& theWire,
96 const gp_Pln& thePlane)
98 Init(theWire, thePlane);
102 // It expects two edges having a common point of type:
105 ChFi2d_AnaFilletAlgo::ChFi2d_AnaFilletAlgo(const TopoDS_Edge& theEdge1,
106 const TopoDS_Edge& theEdge2,
107 const gp_Pln& thePlane)
109 // Make a wire consisting of two edges.
110 Init(theEdge1, theEdge2, thePlane);
113 // Initializes the class by a wire consisting of two edges.
114 void ChFi2d_AnaFilletAlgo::Init(const TopoDS_Wire& theWire, const gp_Pln& thePlane)
117 TopoDS_Iterator itr(theWire);
118 for (; itr.More(); itr.Next())
121 e1 = TopoDS::Edge(itr.Value());
122 else if (e2.IsNull())
123 e2 = TopoDS::Edge(itr.Value());
125 if (e1.IsNull() || e2.IsNull())
126 Standard_TypeMismatch::Raise("The algorithm expects a wire consisting of two linear or circular edges.");
129 BRepAdaptor_Curve AC1(e1);
130 if (AC1.GetType() != GeomAbs_Line && AC1.GetType() != GeomAbs_Circle)
131 Standard_TypeMismatch::Raise("A segment or an arc of circle is expected.");
133 TopoDS_Vertex v1, v2;
134 TopExp::Vertices(e1, v1, v2, Standard_True);
135 if (v1.IsNull() || v2.IsNull())
136 Standard_Failure::Raise("An infinite edge.");
138 gp_Pnt P1 = BRep_Tool::Pnt(v1);
139 gp_Pnt P2 = BRep_Tool::Pnt(v2);
140 gp_Pnt2d p1 = ProjLib::Project(thePlane, P1);
141 gp_Pnt2d p2 = ProjLib::Project(thePlane, P2);
146 if (AC1.GetType() == GeomAbs_Circle)
149 gp_Circ c = AC1.Circle();
151 gp_Pnt2d loc = ProjLib::Project(thePlane, c.Location());
154 radius1 = c.Radius();
159 BRepAdaptor_Curve AC2(e2);
160 if (AC2.GetType() != GeomAbs_Line && AC2.GetType() != GeomAbs_Circle)
161 Standard_TypeMismatch::Raise("A segment or an arc of circle is expected.");
163 TopExp::Vertices(e2, v1, v2, Standard_True);
164 if (v1.IsNull() || v2.IsNull())
165 Standard_Failure::Raise("An infinite edge.");
167 P1 = BRep_Tool::Pnt(v1);
168 P2 = BRep_Tool::Pnt(v2);
169 p1 = ProjLib::Project(thePlane, P1);
170 p2 = ProjLib::Project(thePlane, P2);
175 if (AC2.GetType() == GeomAbs_Circle)
178 gp_Circ c = AC2.Circle();
180 gp_Pnt2d loc = ProjLib::Project(thePlane, c.Location());
183 radius2 = c.Radius();
188 // Initializes the class by two edges.
189 void ChFi2d_AnaFilletAlgo::Init(const TopoDS_Edge& theEdge1, const TopoDS_Edge& theEdge2,
190 const gp_Pln& thePlane)
192 // Make a wire consisting of two edges.
195 TopoDS_Vertex v11, v12, v21, v22;
196 TopExp::Vertices(theEdge1, v11, v12, Standard_True);
197 TopExp::Vertices(theEdge2, v21, v22, Standard_True);
198 if (v11.IsNull() || v12.IsNull() || v21.IsNull() || v22.IsNull())
199 Standard_Failure::Raise("An infinite edge.");
201 gp_Pnt p11 = BRep_Tool::Pnt(v11);
202 gp_Pnt p12 = BRep_Tool::Pnt(v12);
203 gp_Pnt p21 = BRep_Tool::Pnt(v21);
204 gp_Pnt p22 = BRep_Tool::Pnt(v22);
207 if (IsEqual(p11, p21) || IsEqual(p11, p22))
211 else if (IsEqual(p12, p21) || IsEqual(p12, p22))
216 Standard_Failure::Raise("The edges have no common point.");
218 // Reverse the edges in case of need (to construct a wire).
219 Standard_Boolean is1stReversed(Standard_False), is2ndReversed(Standard_False);
220 if (IsEqual(pcommon, p11))
221 is1stReversed = Standard_True;
222 else if (IsEqual(pcommon, p22))
223 is2ndReversed = Standard_True;
226 BRepBuilderAPI_MakeWire mkWire;
228 mkWire.Add(TopoDS::Edge(theEdge1.Reversed()));
230 mkWire.Add(theEdge1);
232 mkWire.Add(TopoDS::Edge(theEdge2.Reversed()));
234 mkWire.Add(theEdge2);
235 if (!mkWire.IsDone())
236 Standard_Failure::Raise("Can't make a wire.");
238 const TopoDS_Wire& W = mkWire.Wire();
242 // Calculates a fillet.
243 Standard_Boolean ChFi2d_AnaFilletAlgo::Perform(const Standard_Real radius)
245 Standard_Boolean bRet(false);
246 if (e1.IsNull() || e2.IsNull() ||
247 radius < Precision::Confusion())
252 // Fillet definition.
253 Standard_Real xc = 0.0, yc = 0.0;
254 Standard_Real start = 0.0, end = 0.0; // parameters on neighbours
255 Standard_Real xstart = DBL_MAX, ystart = DBL_MAX; // point on left neighbour
256 Standard_Real xend = DBL_MAX, yend = DBL_MAX; // point on right neighbour
257 Standard_Boolean cw = Standard_False;
259 // Analytical algorithm works for non-intersecting arcs only.
260 // Check arcs on self-intersection.
261 Standard_Boolean isCut(Standard_False);
262 if (!segment1 || !segment2)
264 BRepBuilderAPI_MakeWire mkWire(e1, e2);
267 const TopoDS_Wire& W = mkWire.Wire();
268 BRepBuilderAPI_MakeFace mkFace(plane);
271 const TopoDS_Face& F = mkFace.Face();
272 ShapeAnalysis_Wire analyzer(W, F, Precision::Confusion());
273 if (analyzer.CheckSelfIntersection() == Standard_True)
275 // Cut the edges at the point of intersection.
276 isCut = Standard_True;
277 if (!Cut(plane, e1, e2))
279 return Standard_False;
284 }// a case of segment - segment
287 BRepAdaptor_Curve AC1(e1), AC2(e2);
288 if (segment1 && segment2)
290 bRet = SegmentFilletSegment(radius, xc, yc, cw, start, end);
292 else if (segment1 && !segment2)
294 bRet = SegmentFilletArc(radius, xc, yc, cw, start, end, xend, yend);
296 else if (!segment1 && segment2)
298 bRet = ArcFilletSegment(radius, xc, yc, cw, start, end, xstart, ystart);
300 else if (!segment1 && !segment2)
302 bRet = ArcFilletArc(radius, xc, yc, cw, start, end);
306 return Standard_False;
308 // Invert the fillet for left-handed plane.
309 if (plane.Position().Direct() == Standard_False)
312 // Construct a fillet.
314 gp_Pnt center = ElSLib::Value(xc, yc, plane);
315 const gp_Dir& normal = plane.Position().Direction();
316 gp_Circ circ(gp_Ax2(center, cw ? -normal : normal), radius);
318 // Fillet may only shrink a neighbour edge, it can't prolongate it.
319 const Standard_Real delta1 = AC1.LastParameter() - AC1.FirstParameter();
320 const Standard_Real delta2 = AC2.LastParameter() - AC2.FirstParameter();
321 if (!isCut && (start > delta1 || end > delta2))
323 // Check a case when a neighbour edge almost disappears:
324 // try to reduce the fillet radius for a little (1.e-5 mm).
325 const Standard_Real little = 100.0 * Precision::Confusion();
326 const Standard_Real d1 = fabs(start - delta1);
327 const Standard_Real d2 = fabs(end - delta2);
328 if (d1 < little || d2 < little)
330 if (segment1 && segment2)
332 bRet = SegmentFilletSegment(radius - little, xc, yc, cw, start, end);
334 else if (segment1 && !segment2)
336 bRet = SegmentFilletArc(radius - little, xc, yc, cw, start, end, xend, yend);
338 else if (!segment1 && segment2)
340 bRet = ArcFilletSegment(radius - little, xc, yc, cw, start, end, xstart, ystart);
342 else if (!segment1 && !segment2)
344 bRet = ArcFilletArc(radius - little, xc, yc, cw, start, end);
348 // Invert the fillet for left-handed planes.
349 if (plane.Position().Direct() == Standard_False)
352 // Make the circle again.
353 center = ElSLib::Value(xc, yc, plane);
354 circ.SetLocation(center);
355 circ.SetRadius(radius - little);
359 return Standard_False;
364 return Standard_False;
369 // start: (xstart, ystart) - pstart.
371 if (xstart != DBL_MAX)
373 pstart = ElSLib::Value(xstart, ystart, plane);
377 if (e1.Orientation() == TopAbs_FORWARD)
378 pstart = AC1.Value(AC1.LastParameter() - start);
380 pstart = AC1.Value(AC1.FirstParameter() + start);
382 // end: (xend, yend) -> pend.
386 pend = ElSLib::Value(xend, yend, plane);
390 if (e2.Orientation() == TopAbs_FORWARD)
391 pend = AC2.Value(AC2.FirstParameter() + end);
393 pend = AC2.Value(AC2.LastParameter() - end);
397 BRepBuilderAPI_MakeEdge mkEdge(circ, pstart, pend);
398 bRet = mkEdge.IsDone();
401 fillet = mkEdge.Edge();
403 // Limit the neighbours.
408 BRepBuilderAPI_MakeEdge mkSegment1;
409 if (e1.Orientation() == TopAbs_FORWARD)
410 mkSegment1.Init(AC1.Curve().Curve(), AC1.FirstParameter(), AC1.LastParameter() - start);
412 mkSegment1.Init(AC1.Curve().Curve(), AC1.FirstParameter() + start, AC1.LastParameter());
413 if (mkSegment1.IsDone())
414 shrinke1 = mkSegment1.Edge();
418 BRepBuilderAPI_MakeEdge mkCirc1;
419 if (e1.Orientation() == TopAbs_FORWARD)
420 mkCirc1.Init(AC1.Curve().Curve(), AC1.FirstParameter(), AC1.LastParameter() - start);
422 mkCirc1.Init(AC1.Curve().Curve(), AC1.FirstParameter() + start, AC1.LastParameter());
423 if (mkCirc1.IsDone())
424 shrinke1 = mkCirc1.Edge();
431 BRepBuilderAPI_MakeEdge mkSegment2;
432 if (e2.Orientation() == TopAbs_FORWARD)
433 mkSegment2.Init(AC2.Curve().Curve(), AC2.FirstParameter() + end, AC2.LastParameter());
435 mkSegment2.Init(AC2.Curve().Curve(), AC2.FirstParameter(), AC2.LastParameter() - end);
436 if (mkSegment2.IsDone())
437 shrinke2 = mkSegment2.Edge();
441 BRepBuilderAPI_MakeEdge mkCirc2;
442 if (e2.Orientation() == TopAbs_FORWARD)
443 mkCirc2.Init(AC2.Curve().Curve(), AC2.FirstParameter() + end, AC2.LastParameter());
445 mkCirc2.Init(AC2.Curve().Curve(), AC2.FirstParameter(), AC2.LastParameter() - end);
446 if (mkCirc2.IsDone())
447 shrinke2 = mkCirc2.Edge();
450 bRet = !shrinke1.IsNull() && !shrinke2.IsNull();
451 }// fillet edge is done
452 }// shrinking is good
457 // Retrieves a result (fillet and shrinked neighbours).
458 const TopoDS_Edge& ChFi2d_AnaFilletAlgo::Result(TopoDS_Edge& theE1, TopoDS_Edge& theE2)
465 // WW5 method to compute fillet.
466 // It returns a constructed fillet definition:
467 // center point (xc, yc)
468 // point on the 1st segment (xstart, ystart)
469 // point on the 2nd segment (xend, yend)
470 // is the arc of fillet clockwise (cw = true) or counterclockwise (cw = false).
471 Standard_Boolean ChFi2d_AnaFilletAlgo::SegmentFilletSegment(const Standard_Real radius,
472 Standard_Real& xc, Standard_Real& yc,
473 Standard_Boolean& cw,
474 Standard_Real& start, Standard_Real& end)
476 // Make normalized vectors at p12.
477 gp_Pnt2d p11(x11, y11);
478 gp_Pnt2d p12(x12, y12);
479 gp_Pnt2d p22(x22, y22);
481 // Check length of segments.
482 if (IsEqual(p12, p11) || IsEqual(p12, p22))
484 return Standard_False;
488 gp_Vec2d v1(p12, p11);
489 gp_Vec2d v2(p12, p22);
494 gp_Vec2d bisec = 0.5 * (v1 + v2);
496 // Check bisectrissa.
497 if (bisec.SquareMagnitude() < Precision::SquareConfusion())
498 return Standard_False;
500 // Normalize the bisectrissa.
503 // Angle at bisectrissa.
504 Standard_Real beta = v1.Angle(bisec);
506 // Length along the bisectrissa till the center of fillet.
507 Standard_Real L = radius / sin(fabs(beta));
509 // Center point of fillet.
510 gp_Pnt2d pc = p12.Translated(L * bisec);
513 // Shrinking length along segments.
514 start = sqrt(L * L - radius * radius);
517 // Orientation of fillet.
519 return Standard_True;
522 // A function constructs a fillet between a segment and an arc.
523 Standard_Boolean ChFi2d_AnaFilletAlgo::SegmentFilletArc(const Standard_Real radius,
524 Standard_Real& xc, Standard_Real& yc,
525 Standard_Boolean& cw,
526 Standard_Real& start, Standard_Real& end,
527 Standard_Real& xend, Standard_Real& yend)
529 // Make a line parallel to the segment at the side of center point of fillet.
530 // This side may be defined through making a bisectrissa for vectors at p12 (or p21).
533 gp_Pnt2d p12(x12, y12);
534 gp_Pnt2d p11(x11, y11);
535 gp_Pnt2d pc2(xc2, yc2);
537 // Check length of segment.
538 if (p11.SquareDistance(p12) < gp::Resolution())
539 return Standard_False;
542 gp_Vec2d v1(p12, p11);
543 gp_Vec2d v2(p12, pc2);
545 // Rotate the arc vector to become tangential at p21.
551 // If vectors coincide (segment and arc are tangent),
552 // the algorithm doesn't work...
553 Standard_Real angle = v1.Angle(v2);
554 if (fabs(angle) < Precision::Angular())
555 return Standard_False;
557 // Make a bissectrisa of vectors at p12.
560 gp_Vec2d bisec = 0.5 * (v1 + v2);
562 // If segment and arc look in opposite direction,
563 // no fillet is possible.
564 if (bisec.SquareMagnitude() < gp::Resolution())
565 return Standard_False;
567 // Define an appropriate point to choose center of fillet.
569 gp_Pnt2d nearp = p12.Translated(radius * bisec);
570 gp_Lin2d nearl(p12, bisec);
572 // Make a line parallel to segment and
573 // passing near the "near" point.
575 gp_Lin2d line(p11, -d1);
577 line.Translate(radius * d1);
578 if (line.Distance(nearp) > radius)
579 line.Translate(-2.0 * radius * d1);
581 // Make a circle of radius of the arc +/- fillet radius.
582 gp_Ax2d axes(pc2, gp::DX2d());
583 gp_Circ2d circ(axes, radius2 + radius);
584 if (radius2 > radius && circ.Distance(nearp) > radius)
585 circ.SetRadius(radius2 - radius);
587 // Calculate intersection of the line and the circle.
588 IntAna2d_AnaIntersection intersector(line, circ);
589 if (!intersector.IsDone() || !intersector.NbPoints())
590 return Standard_False;
592 // Find center point of fillet.
594 Standard_Real minDist = DBL_MAX;
595 for (i = 1; i <= intersector.NbPoints(); ++i)
597 const IntAna2d_IntPoint& intp = intersector.Point(i);
598 const gp_Pnt2d& p = intp.Value();
600 Standard_Real d = nearl.Distance(p);
608 // Shrink of segment.
610 Standard_Real L2 = pc.SquareDistance(p12);
611 const Standard_Real Rf2 = radius * radius;
612 start = sqrt(L2 - Rf2);
615 gp_Vec2d pcc(pc2, pc);
616 end = fabs(gp_Vec2d(pc2, p12).Angle(pcc));
618 // Duplicate the information on shrink the arc:
619 // calculate a point on the arc coinciding with the end of fillet.
620 line.SetLocation(pc2);
621 line.SetDirection(pcc);
622 circ.SetLocation(pc2);
623 circ.SetRadius(radius2);
624 intersector.Perform(line, circ);
625 if (!intersector.IsDone() || !intersector.NbPoints())
626 return Standard_False;
630 for (i = 1; i <= intersector.NbPoints(); ++i)
632 const IntAna2d_IntPoint& intp = intersector.Point(i);
633 const gp_Pnt2d& p = intp.Value();
635 const Standard_Real d2 = p.SquareDistance(pc);
636 if (fabs(d2 - Rf2) < Precision::Confusion())
643 // Orientation of the fillet.
644 angle = v1.Angle(v2);
646 return Standard_True;
649 // A function constructs a fillet between an arc and a segment.
650 Standard_Boolean ChFi2d_AnaFilletAlgo::ArcFilletSegment(const Standard_Real radius,
651 Standard_Real& xc, Standard_Real& yc,
652 Standard_Boolean& cw,
653 Standard_Real& start, Standard_Real& end,
654 Standard_Real& xstart, Standard_Real& ystart)
656 // Make a line parallel to the segment at the side of center point of fillet.
657 // This side may be defined through making a bisectrissa for vectors at p12 (or p21).
660 gp_Pnt2d p12(x12, y12);
661 gp_Pnt2d p22(x22, y22);
662 gp_Pnt2d pc1(xc1, yc1);
664 // Check length of segment.
665 if (p12.SquareDistance(p22) < gp::Resolution())
666 return Standard_False;
669 gp_Vec2d v1(p12, pc1);
670 gp_Vec2d v2(p12, p22);
672 // Rotate the arc vector to become tangential at p21.
678 // If vectors coincide (segment and arc are tangent),
679 // the algorithm doesn't work...
680 Standard_Real angle = v1.Angle(v2);
681 if (fabs(angle) < Precision::Angular())
682 return Standard_False;
684 // Make a bisectrissa of vectors at p12.
687 gp_Vec2d bisec = 0.5 * (v1 + v2);
689 // If segment and arc look in opposite direction,
690 // no fillet is possible.
691 if (bisec.SquareMagnitude() < gp::Resolution())
692 return Standard_False;
694 // Define an appropriate point to choose center of fillet.
696 gp_Pnt2d nearPoint = p12.Translated(radius * bisec);
697 gp_Lin2d nearLine(p12, bisec);
699 // Make a line parallel to segment and
700 // passing near the "near" point.
702 gp_Lin2d line(p22, -d2);
704 line.Translate(radius * d2);
705 if (line.Distance(nearPoint) > radius)
706 line.Translate(-2.0 * radius * d2);
708 // Make a circle of radius of the arc +/- fillet radius.
709 gp_Ax2d axes(pc1, gp::DX2d());
710 gp_Circ2d circ(axes, radius1 + radius);
711 if (radius1 > radius && circ.Distance(nearPoint) > radius)
712 circ.SetRadius(radius1 - radius);
714 // Calculate intersection of the line and the big circle.
715 IntAna2d_AnaIntersection intersector(line, circ);
716 if (!intersector.IsDone() || !intersector.NbPoints())
717 return Standard_False;
719 // Find center point of fillet.
721 Standard_Real minDist = DBL_MAX;
722 for (i = 1; i <= intersector.NbPoints(); ++i)
724 const IntAna2d_IntPoint& intp = intersector.Point(i);
725 const gp_Pnt2d& p = intp.Value();
727 Standard_Real d = nearLine.Distance(p);
735 // Shrink of segment.
737 Standard_Real L2 = pc.SquareDistance(p12);
738 const Standard_Real Rf2 = radius * radius;
739 end = sqrt(L2 - Rf2);
742 gp_Vec2d pcc(pc1, pc);
743 start = fabs(gp_Vec2d(pc1, p12).Angle(pcc));
745 // Duplicate the information on shrink the arc:
746 // calculate a point on the arc coinciding with the start of fillet.
747 line.SetLocation(pc1);
748 line.SetDirection(pcc);
749 circ.SetLocation(pc1);
750 circ.SetRadius(radius1);
751 intersector.Perform(line, circ);
752 if (!intersector.IsDone() || !intersector.NbPoints())
753 return Standard_False;
757 for (i = 1; i <= intersector.NbPoints(); ++i)
759 const IntAna2d_IntPoint& intp = intersector.Point(i);
760 const gp_Pnt2d& p = intp.Value();
762 const Standard_Real d2 = p.SquareDistance(pc);
763 if (fabs(d2 - Rf2) < Precision::SquareConfusion())
765 p.Coord(xstart, ystart);
770 // Orientation of the fillet.
771 angle = v2.Angle(v1);
773 return Standard_True;
776 // WW5 method to compute fillet: arc - arc.
777 // It returns a constructed fillet definition:
778 // center point (xc, yc)
779 // shrinking parameter of the 1st circle (start)
780 // shrinking parameter of the 2nd circle (end)
781 // if the arc of fillet clockwise (cw = true) or counterclockwise (cw = false).
782 Standard_Boolean ChFi2d_AnaFilletAlgo::ArcFilletArc(const Standard_Real radius,
783 Standard_Real& xc, Standard_Real& yc,
784 Standard_Boolean& cw,
785 Standard_Real& start, Standard_Real& end)
788 const gp_Pnt2d pc1(xc1, yc1);
789 const gp_Pnt2d pc2(xc2, yc2);
790 const gp_Pnt2d p12(x12, y12);
792 // Make vectors at p12.
793 gp_Vec2d v1(pc1, p12);
794 gp_Vec2d v2(pc2, p12);
796 // Rotate the vectors so that they are tangent to circles at p12.
806 // Make a "check" point for choosing an offset circle.
809 gp_Vec2d bisec = 0.5 * (v1 + v2);
810 if (bisec.SquareMagnitude() < gp::Resolution())
811 return Standard_False;
813 const gp_Pnt2d checkp = p12.Translated(radius * bisec);
814 const gp_Lin2d checkl(p12, bisec);
816 // Make two circles of radius r1 +/- r and r2 +/- r
817 // with center point equal to pc1 and pc2.
819 gp_Ax2d axes(pc1, gp::DX2d());
820 gp_Circ2d c1(axes, radius1 + radius);
821 if (radius1 > radius && c1.Distance(checkp) > radius)
822 c1.SetRadius(radius1 - radius);
824 axes.SetLocation(pc2);
825 gp_Circ2d c2(axes, radius2 + radius);
826 if (radius2 > radius && c2.Distance(checkp) > radius)
827 c2.SetRadius(radius2 - radius);
829 // Calculate an intersection point of these two circles
830 // and choose the one closer to the "check" point.
831 IntAna2d_AnaIntersection intersector(c1, c2);
832 if (!intersector.IsDone() || !intersector.NbPoints())
833 return Standard_False;
835 // Find center point of fillet.
837 Standard_Real minDist = DBL_MAX;
838 for (int i = 1; i <= intersector.NbPoints(); ++i)
840 const IntAna2d_IntPoint& intp = intersector.Point(i);
841 const gp_Pnt2d& p = intp.Value();
843 Standard_Real d = checkp.SquareDistance(p);
852 // Orientation of fillet.
853 Standard_Real angle = v1.Angle(v2);
854 if (fabs(angle) < Precision::Angular())
856 angle = gp_Vec2d(pc, pc1).Angle(gp_Vec2d(pc, pc2));
864 // Shrinking of circles.
865 start = fabs(gp_Vec2d(pc1, p12).Angle(gp_Vec2d(pc1, pc)));
866 end = fabs(gp_Vec2d(pc2, p12).Angle(gp_Vec2d(pc2, pc)));
867 return Standard_True;
870 // Cuts intersecting edges of a contour.
871 Standard_Boolean ChFi2d_AnaFilletAlgo::Cut(const gp_Pln& thePlane, TopoDS_Edge& theE1, TopoDS_Edge& theE2)
874 Standard_Boolean found(Standard_False);
875 Standard_Real param1 = 0.0, param2 = 0.0;
876 Standard_Real f1, l1, f2, l2;
877 Handle(Geom_Curve) c1 = BRep_Tool::Curve(theE1, f1, l1);
878 Handle(Geom_Curve) c2 = BRep_Tool::Curve(theE2, f2, l2);
879 GeomAPI_ExtremaCurveCurve extrema(c1, c2, f1, l1, f2, l2);
880 if (extrema.NbExtrema())
882 Standard_Integer i, nb = extrema.NbExtrema();
883 for (i = 1; i <= nb; ++i)
885 const Standard_Real d = extrema.Distance(i);
886 if (d < Precision::Confusion())
888 extrema.Parameters(i, param1, param2);
889 if (fabs(l1 - param1) > Precision::Confusion() &&
890 fabs(f2 - param2) > Precision::Confusion())
892 found = Standard_True;
893 extrema.Points(i, p, p);
902 BRepBuilderAPI_MakeEdge mkEdge1(c1, f1, param1);
903 if (mkEdge1.IsDone())
905 theE1 = mkEdge1.Edge();
907 BRepBuilderAPI_MakeEdge mkEdge2(c2, param2, l2);
908 if (mkEdge2.IsDone())
910 theE2 = mkEdge2.Edge();
912 gp_Pnt2d p2d = ProjLib::Project(thePlane, p);
916 return Standard_True;
920 return Standard_False;