trelau / AFEM

Airframe Finite Element Modeler
GNU Lesser General Public License v2.1
40 stars 16 forks source link

Intersecting Line with Circle does not work #8

Open mnesarco opened 3 years ago

mnesarco commented 3 years ago

Intersecting a Line with a Circle must result in zero, one or two intersection points, but in this example it returns zero points where the Line is actually intersecting the circle at two points:

circle = CircleByNormal(Point(0, 0, 0), Direction(0, 0, 1), 20).circle
line = LineByVector(Point(0, 0, 0), Direction(0, 1, 0)).line
points = IntersectCurveCurve(circle, line).points
print(f"points={points}")

Result:

points=[]

trelau commented 3 years ago

Looks like an OCCT bug. When I use a fininte curve (a line is infinite) the intersection works:

circle = CircleByNormal(Point(0, 0, 0), Direction(0, 0, 1), 20).circle
line = LineByVector(Point(0, 0, 0), Direction(0, 1, 0)).line
line = TrimmedCurveByPoints(line, Point(0, -100, 0), Point(0, 100, 0)).curve
points = IntersectCurveCurve(circle, line).points

image

I'll add a unit test for this failing case with a line and hopefully a future OCCT update will fix this case. Until then, are you able to work around it by using a finite curve (either trim the line as shown or interpolate two points using a NURBS curve)?

mnesarco commented 3 years ago

Hi @trelau , Yes I have noted that intersections with infinite curves does not work in some cases but i don't know why or when. I am currently using only NURBS based line segments to do intersections as a workaround.

BTW I am using this code that maybe can be part of AFEM:

class LineSegmentByPoints:
    """
    Create a Line segment between two points.

    :param vector_like p1: Start point.
    :param vector_like p2: End point.
    """

    __slots__ = ('_segment',)

    def __init__(self, p1, p2):
        self._segment = NurbsCurveByInterp([p1, p2]).curve

    @property
    def segment(self):
        return self._segment

... And for Beziers:

class BezierCurve(Curve):
    """
    Bezier curve around ``Geom_BezierCurve``.
    """
    _OCC_TYPE = Geom_BezierCurve

    @classmethod
    def by_data(cls, cp, weights=None):
        """
        Create a Bezier curve by data.

        :param collections.Sequence(point_like) cp: Control points.
        :param collections.Sequence(float) weights: Weights of control points.
        """
        tcol_cp = occ_utils.to_tcolgp_array1_pnt(cp)
        if weights is None:
            weights = [1.] * tcol_cp.Length()
        tcol_weights = occ_utils.to_tcolstd_array1_real(weights)

        geom_crv = Geom_BezierCurve(tcol_cp, tcol_weights)
        return cls(geom_crv)

class BezierCurveByPoints:
    """
    Create a Bezier curve by control points.

    :param collections.Sequence(point_like) qp: Control points.
    :param collections.Sequence(float) weights: Poles weights.
    """

    __slots__ = ('_c',)

    def __init__(self, qp, weights=None):
        self._c = BezierCurve.by_data(qp, weights)

    @property
    def curve(self) -> BezierCurve:
        """
        The Bezier curve.
        """
        return self._c