Beispiel #1
0
        public static void line_line_basic_intersection_result()
        {
            var a = new Line2(new Point2(3, 3), new Vector2(1, 1));
            var b = new Line2(new Point2(0, 1), new Vector2(0, 1));
            var c = new Line2(new Point2(2, 2), new Vector2(-1, -1));
            var d = new Line2(new Point2(2, 3), new Vector2(-1, -1));

            a.Intersection(b).Should().Be(new Point2(0, 0));
            a.Intersection(c).Should().Be(c);
            c.Intersection(a).Should().Be(c);
            a.Intersection(d).Should().BeNull();
        }
        private BezierSubdivision TangentIntersectionApproximation(Neuron neuron, out double length)
        {
            var firstPoint = neuron.Nucleus.Pos;
            var lastPoint  = neuron.Terminal.Nucleus.Pos;
            var dir        = Math.Sign(neuron.Terminal.Nucleus.SegmentIndex - neuron.Nucleus.SegmentIndex);
            var line1      = Line2.FromPoints(neuron.Nucleus.PathPoint, _path[neuron.Nucleus.SegmentIndex + dir]);
            var line2      = Line2.FromPoints(neuron.Terminal.Nucleus.PathPoint, _path[neuron.Terminal.Nucleus.SegmentIndex - dir]);

            BezierSubdivision bs;

            if (Line2.Intersection(line1, line2, out var intersection))
            {
                bs = new BezierSubdivision(new List <Vector2> {
                    firstPoint, intersection, lastPoint
                });
                length = bs.SubdividedApproximationLength();
            }
            else
            {
                bs = new BezierSubdivision(new List <Vector2> {
                    firstPoint, lastPoint
                });
                length = Vector2.Distance(firstPoint, lastPoint);
            }

            return(bs);
        }
Beispiel #3
0
        public void IntersectionTest2()
        {
            var l1           = Line2.FromSlopeAndYIntercept(slope: 1, yIntercept: 1);
            var l2           = Line2.FromSlopeAndYIntercept(slope: -1, yIntercept: 1);
            var intersection = Line2.Intersection(l1, l2);

            Assert.Equal(new Vector2(0, 1), intersection);
        }
Beispiel #4
0
        public void IntersectionTest1()
        {
            var l1           = Line2.XEquals(3);
            var l2           = Line2.YEquals(4);
            var intersection = Line2.Intersection(l1, l2);

            Assert.Equal(new Vector2(3, 4), intersection);
        }
Beispiel #5
0
        public static void line_line_intersection_result()
        {
            var a = new Line2(A, B);
            var b = new Line2(C, D);

            var forward = a.Intersection(b);
            var reverse = b.Intersection(a);

            forward.Should().NotBeNull();
            forward.Should().Be(reverse);
        }
        public static void line_line_intersection_result()
        {
            var a = new Line2(A, B);
            var b = new Line2(C, D);

            var forward = a.Intersection(b);
            var reverse = b.Intersection(a);

            forward.Should().NotBeNull();
            forward.Should().Be(reverse);
        }
Beispiel #7
0
        public override void DrawYourself(DrawingContext context, CoordinateConverter converter, RelevantObjectPreferences preferences, Pen pen)
        {
            if (!Line2.Intersection(new Box2 {
                Left = -1000, Top = -1000, Right = 1512, Bottom = 1384
            }, Child, out var points))
            {
                return;
            }
            var cPos1 = converter.ToDpi(converter.EditorToRelativeCoordinate(points[0]));
            var cPos2 = converter.ToDpi(converter.EditorToRelativeCoordinate(points[1]));

            context.DrawLine(pen, new Point(cPos1.X, cPos1.Y), new Point(cPos2.X, cPos2.Y));
        }
        public RelevantLine[] GetRelevantObjects(RelevantLine line1, RelevantLine line2)
        {
            if (!Line2.Intersection(line1.Child, line2.Child, out var intersection))
            {
                return(null);
            }
            var dir1Norm = Vector2.Normalize(line1.Child.DirectionVector);
            var dir2Norm = Vector2.Normalize(line2.Child.DirectionVector);

            return(new[] {
                new RelevantLine(new Line2(intersection, dir1Norm + dir2Norm)),
                new RelevantLine(new Line2(intersection, dir1Norm - dir2Norm))
            });
        }
        public static void line_ray_intersection_result()
        {
            var a1 = new Line2(A, B);
            var a2 = new Line2(B, A);
            var b = new Ray2(C, D);

            var forward1 = a1.Intersection(b);
            var forward2 = a2.Intersection(b);
            var reverse1 = b.Intersection(a1);
            var reverse2 = b.Intersection(a2);

            forward1.Should().NotBeNull();
            forward1.Should().Be(forward2);
            forward1.Should().Be(reverse1);
            forward1.Should().Be(reverse2);
        }
Beispiel #10
0
        public static void line_ray_intersection_result()
        {
            var a1 = new Line2(A, B);
            var a2 = new Line2(B, A);
            var b  = new Ray2(C, D);

            var forward1 = a1.Intersection(b);
            var forward2 = a2.Intersection(b);
            var reverse1 = b.Intersection(a1);
            var reverse2 = b.Intersection(a2);

            forward1.Should().NotBeNull();
            forward1.Should().Be(forward2);
            forward1.Should().Be(reverse1);
            forward1.Should().Be(reverse2);
        }
Beispiel #11
0
        public static void line_segment_intersection_result()
        {
            var a1 = new Line2(A, B);
            var a2 = new Line2(B, A);
            var b1 = new Segment2(C, D);
            var b2 = new Segment2(D, C);

            var forward1 = a1.Intersection(b1);
            var forward2 = a2.Intersection(b2);
            var reverse1 = b1.Intersection(a1);
            var reverse2 = b2.Intersection(a2);

            forward1.Should().NotBeNull();
            forward1.Should().Be(forward2);
            forward1.Should().Be(reverse1);
            forward1.Should().Be(reverse2);
        }
        public static void line_segment_intersection_result()
        {
            var a1 = new Line2(A, B);
            var a2 = new Line2(B, A);
            var b1 = new Segment2(C, D);
            var b2 = new Segment2(D, C);

            var forward1 = a1.Intersection(b1);
            var forward2 = a2.Intersection(b2);
            var reverse1 = b1.Intersection(a1);
            var reverse2 = b2.Intersection(a2);

            forward1.Should().NotBeNull();
            forward1.Should().Be(forward2);
            forward1.Should().Be(reverse1);
            forward1.Should().Be(reverse2);
        }
Beispiel #13
0
        public static void line_ray_basic_intersection_result()
        {
            var a = new Line2(new Point2(3, 3), new Vector2(1, 1));
            var b = new Ray2(new Point2(0, 1), new Vector2(0, 1));
            var c = new Ray2(new Point2(2, 2), new Vector2(-1, -1));
            var d = new Ray2(new Point2(2, 3), new Vector2(-1, -1));

            a.Intersection(b.GetReverse()).Should().Be(Point2.Zero);
            a.Intersection(c).Should().Be(c);
            a.Intersection(c.GetReverse()).Should().Be(c.GetReverse());
            a.Intersection(b).Should().BeNull();
            a.Intersection(d).Should().BeNull();
            a.Intersection(d.GetReverse()).Should().BeNull();
        }
Beispiel #14
0
        public override bool Intersection(IRelevantObject other, out Vector2[] intersections)
        {
            switch (other)
            {
            case RelevantPoint point:
                intersections = new[] { point.Child };
                return(Precision.AlmostEquals(Line2.Distance(Child, point.Child), 0));

            case RelevantLine line: {
                bool isIntersecting = Line2.Intersection(Child, line.Child, out var intersection);
                intersections = new[] { intersection };
                return(isIntersecting);
            }

            case RelevantCircle circle:
                return(Circle.Intersection(circle.Child, Child, out intersections));

            default:
                intersections = new Vector2[0];
                return(false);
            }
        }
Beispiel #15
0
        private Vector2?TangentIntersectionApproximation(double startIndex, double endIndex)
        {
            var p1 = GetContinuousPosition(startIndex);
            var p2 = GetContinuousPosition(endIndex);

            var a1 = GetContinuousAngle(startIndex);
            var a2 = GetContinuousAngle(endIndex);

            if (Math.Abs(GetSmallestAngle(a1, a2)) > 0.1)
            {
                var t1 = new Line2(p1, a1);
                var t2 = new Line2(p2, a2);

                var middleAnchor = Line2.Intersection(t1, t2);
                if (middleAnchor != Vector2.NaN &&
                    Vector2.DistanceSquared(p1, middleAnchor) > 0.5 &&
                    Vector2.DistanceSquared(p2, middleAnchor) > 0.5)
                {
                    return(middleAnchor);
                }
            }

            return(null);
        }
Beispiel #16
0
 public RelevantPoint GetLineLineIntersection(RelevantLine line1, RelevantLine line2)
 {
     return(Line2.Intersection(line1.Child, line2.Child, out var intersection) ? new RelevantPoint(intersection) : null);
 }
Beispiel #17
0
        public static void line_line_basic_intersection_result() {
            var a = new Line2(new Point2(3, 3), new Vector2(1, 1));
            var b = new Line2(new Point2(0, 1), new Vector2(0, 1));
            var c = new Line2(new Point2(2, 2), new Vector2(-1, -1));
            var d = new Line2(new Point2(2, 3), new Vector2(-1, -1));

            a.Intersection(b).Should().Be(new Point2(0, 0));
            a.Intersection(c).Should().Be(c);
            c.Intersection(a).Should().Be(c);
            a.Intersection(d).Should().BeNull();
        }
Beispiel #18
0
        public static void line_ray_basic_intersection_result() {
            var a = new Line2(new Point2(3, 3), new Vector2(1, 1));
            var b = new Ray2(new Point2(0, 1), new Vector2(0, 1));
            var c = new Ray2(new Point2(2, 2), new Vector2(-1, -1));
            var d = new Ray2(new Point2(2, 3), new Vector2(-1, -1));

            a.Intersection(b.GetReverse()).Should().Be(Point2.Zero);
            a.Intersection(c).Should().Be(c);
            a.Intersection(c.GetReverse()).Should().Be(c.GetReverse());
            a.Intersection(b).Should().BeNull();
            a.Intersection(d).Should().BeNull();
            a.Intersection(d.GetReverse()).Should().BeNull();
        }
    public Vector2 FindTrapezoidDiagonalIntersection()
    {
        List <Line2> edges = new List <Line2>();
        // m_renderEngine.DrawPoints(m_projector.ImageToWorld(this.boundary2), Color.yellow);

        List <Vector2> boundpoints = new List <Vector2>(this.boundary2);
        //find all lines
        double       thred  = 5;
        RansacLine2d ransac = new RansacLine2d(thred, 0.9);

        edges.Clear();
        do
        {
            Line2 oneline = ransac.Estimate(boundpoints);
            inlier_list.Add(new List <Vector2>(ransac.inliers));
            boundpoints = boundpoints.Except(ransac.inliers).ToList();
            edges.Add(oneline);
        } while (edges.Last() != null);
        edges.RemoveAt(edges.Count - 1);
        Console.WriteLine("edges number: {0}", edges.Count());


        //find two most parallel lines, also not the same line
        double minangle = -1;

        for (int i = 0; i < edges.Count - 1; i++)
        {
            for (int j = i + 1; j < edges.Count; j++)
            {
                double angle = Mathf.Abs(Vector3.Dot(edges[i].dir, edges[j].dir));
                if (angle > minangle && MinDisBetweenLine2(edges[i], edges[j]) > 2 * thred)
                {
                    minangle   = angle;
                    firstbase  = i;
                    secondbase = j;
                }
            }
        }
        //refine bases
        foreach (Vector2 p in boundary2)
        {
            if (edges[firstbase].DistanceToLine(p) < 2 * thred)
            {
                this.inlier_list[firstbase].Add(p);
            }
            if (edges[secondbase].DistanceToLine(p) < 2 * thred)
            {
                this.inlier_list[secondbase].Add(p);
            }
        }

        edges[firstbase]  = ransac.Estimate(this.inlier_list[firstbase]);
        edges[secondbase] = ransac.Estimate(this.inlier_list[secondbase]);
        edges[firstbase].UpdateEnd(this.inlier_list[firstbase]);
        edges[secondbase].UpdateEnd(this.inlier_list[secondbase]);
        //m_renderEngine.DrawLine(m_projector.Line2ToLine3(edges[firstbase]), Color.red);
        //m_renderEngine.DrawLine(m_projector.Line2ToLine3(edges[secondbase]), Color.blue);


        corner_points.Add(edges[firstbase].start);
        corner_points.Add(edges[firstbase].end);
        corner_points.Add(edges[secondbase].end);
        corner_points.Add(edges[secondbase].start);

        // compute Diagonal Intersection point as 3d rectangle center
        // same direction
        Line2 diagonal1 = new Line2(edges[firstbase].start, edges[secondbase].end);
        Line2 diagonal2 = new Line2(edges[secondbase].start, edges[firstbase].end);

        // m_renderEngine.DrawLine(m_projector.Line2ToLine3(diagonal1), Color.yellow);
        // m_renderEngine.DrawLine(m_projector.Line2ToLine3(diagonal2), Color.yellow);

        Vector2 inter = diagonal1.Intersection(diagonal2);

        if (Utility.PointInTrapezoid(inter, corner_points))
        {
            return(inter);
        }



        // not same direction
        diagonal1 = new Line2(edges[firstbase].start, edges[secondbase].start);
        diagonal2 = new Line2(edges[secondbase].end, edges[firstbase].end);

        //m_renderEngine.DrawLine(m_projector.Line2ToLine3(diagonal1), Color.white);
        //m_renderEngine.DrawLine(m_projector.Line2ToLine3(diagonal2), Color.white);

        Vector2 inter2 = diagonal1.Intersection(diagonal2);

        if (Utility.PointInTrapezoid(inter2, corner_points))
        {
            edges[secondbase].Flip();
            return(inter2);
        }


        return(new Vector2());

        ////compute area
        //double area = (edges[firstbase].Length() + edges[secondbase].Length()) * MeanDisBetweenLine2(edges[firstbase], edges[secondbase]) / 2.0;
        //double areafromimg = IExtension.GetMaskPoints(this.img).Count();
        //double error = Math.Abs(areafromimg - area);
        //error /= areafromimg;
        //Debug.Log("Like a trapezoid error: " + error);
        //return error;
    }