Example #1
0
        /// <summary>
        /// Get intersection of line with triangle.
        /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'.
        /// </summary>
        public object IntersectionWith(Line3d l)
        {
            // Relative tolerance ================================
            if (!GeometRi3D.UseAbsoluteTolerance)
            {
                double tol = GeometRi3D.Tolerance;
                GeometRi3D.Tolerance            = tol * Max(AB, Max(BC, AC));
                GeometRi3D.UseAbsoluteTolerance = true;
                object result = this.IntersectionWith(l);
                GeometRi3D.UseAbsoluteTolerance = false;
                GeometRi3D.Tolerance            = tol;
                return(result);
            }
            //====================================================

            Plane3d s = new Plane3d(this.A, this.Normal);

            object obj = l.IntersectionWith(s);

            if (obj == null)
            {
                return(null);
            }
            else
            {
                if (obj.GetType() == typeof(Line3d))
                {
                    // Coplanar line and triangle

                    // Check intersection in one corner
                    // or in corner and opposite side
                    if (_a.BelongsTo(l))
                    {
                        object obj2 = new Segment3d(_b, _c).IntersectionWith(l);
                        if (obj2 != null && obj2.GetType() == typeof(Point3d))
                        {
                            return(new Segment3d(_a, (Point3d)obj2));
                        }
                        else
                        {
                            return(A);
                        }
                    }

                    if (_b.BelongsTo(l))
                    {
                        object obj2 = new Segment3d(_a, _c).IntersectionWith(l);
                        if (obj2 != null && obj2.GetType() == typeof(Point3d))
                        {
                            return(new Segment3d(_b, (Point3d)obj2));
                        }
                        else
                        {
                            return(B);
                        }
                    }

                    if (_c.BelongsTo(l))
                    {
                        object obj2 = new Segment3d(_a, _b).IntersectionWith(l);
                        if (obj2 != null && obj2.GetType() == typeof(Point3d))
                        {
                            return(new Segment3d(_c, (Point3d)obj2));
                        }
                        else
                        {
                            return(C);
                        }
                    }

                    // Check intersection with two sides
                    object objAB = new Segment3d(_a, _b).IntersectionWith(l);
                    object objBC = new Segment3d(_b, _c).IntersectionWith(l);
                    if (objAB != null && objAB.GetType() == typeof(Point3d))
                    {
                        if (objBC != null && objBC.GetType() == typeof(Point3d))
                        {
                            return(new Segment3d((Point3d)objAB, (Point3d)objBC));
                        }
                        else
                        {
                            object objAC = new Segment3d(_a, _c).IntersectionWith(l);
                            if (objAC != null && objAC.GetType() == typeof(Point3d))
                            {
                                return(new Segment3d((Point3d)objAB, (Point3d)objAC));
                            }
                            else
                            {
                                return((Point3d)objAB);
                            }
                        }
                    }

                    if (objBC != null && objBC.GetType() == typeof(Point3d))
                    {
                        object objAC = new Segment3d(_a, _c).IntersectionWith(l);
                        if (objAC != null && objAC.GetType() == typeof(Point3d))
                        {
                            return(new Segment3d((Point3d)objBC, (Point3d)objAC));
                        }
                        else
                        {
                            return((Point3d)objBC);
                        }
                    }

                    object objAC2 = new Segment3d(_a, _c).IntersectionWith(l);
                    if (objAC2 != null && objAC2.GetType() == typeof(Point3d))
                    {
                        return((Point3d)objAC2);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    // result of intersection is point
                    Point3d p = (Point3d)obj;
                    if (p.BelongsTo(this))
                    {
                        return(p);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Get intersection of line with triangle.
        /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'.
        /// </summary>
        public object IntersectionWith(Line3d l)
        {
            // Relative tolerance ================================
            if (!GeometRi3D.UseAbsoluteTolerance)
            {
                double tol = GeometRi3D.Tolerance;
                GeometRi3D.Tolerance            = tol * Max(AB, Max(BC, AC));
                GeometRi3D.UseAbsoluteTolerance = true;
                object result = this.IntersectionWith(l);
                GeometRi3D.UseAbsoluteTolerance = false;
                GeometRi3D.Tolerance            = tol;
                return(result);
            }
            //====================================================

            Plane3d s = new Plane3d(this.A, this.Normal);

            object obj = l.IntersectionWith(s);

            if (obj == null)
            {
                return(null);
            }
            else
            {
                if (obj.GetType() == typeof(Line3d))
                {
                    Segment3d sAB = new Segment3d(A, B);
                    Segment3d sBC = new Segment3d(B, C);
                    Segment3d sAC = new Segment3d(A, C);

                    // Line coincides with one side, return segment
                    if (sAB.BelongsTo(l))
                    {
                        return(sAB);
                    }
                    if (sBC.BelongsTo(l))
                    {
                        return(sBC);
                    }
                    if (sAC.BelongsTo(l))
                    {
                        return(sAC);
                    }

                    Point3d pAB = (Point3d)sAB.IntersectionWith(l);
                    Point3d pBC = (Point3d)sBC.IntersectionWith(l);
                    Point3d pAC = (Point3d)sAC.IntersectionWith(l);

                    bool bAB = (object.ReferenceEquals(null, pAB)) ? false : pAB.BelongsTo(sAB);
                    bool bAC = (object.ReferenceEquals(null, pAC)) ? false : pAC.BelongsTo(sAC);
                    bool bBC = (object.ReferenceEquals(null, pBC)) ? false : pBC.BelongsTo(sBC);

                    // Line crosses one corner, return point
                    if (bAB && bBC && pAB == pBC && !bAC)
                    {
                        return(pAB);
                    }
                    if (bAB && bAC && pAB == pAC && !bBC)
                    {
                        return(pAB);
                    }
                    if (bAC && bBC && pAC == pBC && !bAB)
                    {
                        return(pAC);
                    }

                    // Line crosses two sides, return segment
                    if (bAB && bBC && !bAC)
                    {
                        return(new Segment3d(pAB, pBC));
                    }
                    if (bAB && bAC && !bBC)
                    {
                        return(new Segment3d(pAB, pAC));
                    }
                    if (bAC && bBC && !bAB)
                    {
                        return(new Segment3d(pAC, pBC));
                    }

                    // Line crosses one corner and one side, return segment
                    if (pAB == pBC && bAC)
                    {
                        return(new Segment3d(pAB, pAC));
                    }
                    if (pAB == pAC && bBC)
                    {
                        return(new Segment3d(pAB, pBC));
                    }
                    if (pAC == pBC && bAB)
                    {
                        return(new Segment3d(pAB, pAC));
                    }

                    //else
                    return(null);
                }
                else
                {
                    // result of intersection is point
                    Point3d p = (Point3d)obj;
                    if (p.BelongsTo(this))
                    {
                        return(p);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Intersection of ellipse with line.
        /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'.
        /// </summary>
        public object IntersectionWith(Line3d l)
        {
            // Relative tolerance ================================
            if (!GeometRi3D.UseAbsoluteTolerance)
            {
                double tol = GeometRi3D.Tolerance;
                GeometRi3D.Tolerance            = tol * this.A;
                GeometRi3D.UseAbsoluteTolerance = true;
                object result = this.IntersectionWith(l);
                GeometRi3D.UseAbsoluteTolerance = false;
                GeometRi3D.Tolerance            = tol;
                return(result);
            }
            //====================================================


            if (l.Direction.IsOrthogonalTo(this.Normal))
            {
                if (l.Point.BelongsTo(new Plane3d(this.Center, this.Normal)))
                {
                    // coplanar objects
                    // Find intersection of line and ellipse (2D)
                    // Solution from: http://www.ambrsoft.com/TrigoCalc/Circles2/Ellipse/EllipseLine.htm

                    Coord3d  local_coord = new Coord3d(this.Center, this._v1, this._v2);
                    Point3d  p           = l.Point.ConvertTo(local_coord);
                    Vector3d v           = l.Direction.ConvertTo(local_coord);
                    double   a           = this.A;
                    double   b           = this.B;

                    if (Abs(v.Y / v.X) > 100)
                    {
                        // line is almost vertical, rotate local coord
                        local_coord = new Coord3d(this.Center, this._v2, this._v1);
                        p           = l.Point.ConvertTo(local_coord);
                        v           = l.Direction.ConvertTo(local_coord);
                        a           = this.B;
                        b           = this.A;
                    }

                    // Line equation in form: y = mx + c
                    double m = v.Y / v.X;
                    double c = p.Y - m * p.X;

                    double amb = Math.Pow(a, 2) * Math.Pow(m, 2) + Math.Pow(b, 2);
                    double det = amb - Math.Pow(c, 2);
                    if (det < -GeometRi3D.Tolerance)
                    {
                        return(null);
                    }
                    else if (det > 1e-12)
                    {
                        double x1 = (-Math.Pow(a, 2) * m * c + a * b * Sqrt(det)) / amb;
                        double x2 = (-Math.Pow(a, 2) * m * c - a * b * Sqrt(det)) / amb;
                        double y1 = (Math.Pow(b, 2) * c + a * b * m * Sqrt(det)) / amb;
                        double y2 = (Math.Pow(b, 2) * c - a * b * m * Sqrt(det)) / amb;
                        return(new Segment3d(new Point3d(x1, y1, 0, local_coord), new Point3d(x2, y2, 0, local_coord)));
                    }
                    else
                    {
                        double x = -Math.Pow(a, 2) * m * c / amb;
                        double y = Math.Pow(b, 2) * c / amb;
                        return(new Point3d(x, y, 0, local_coord));
                    }
                }
                else
                {
                    // parallel objects
                    return(null);
                }
            }
            else
            {
                // Line intersects ellipse' plane
                Point3d p = (Point3d)l.IntersectionWith(new Plane3d(this.Center, this.Normal));
                if (p.BelongsTo(this))
                {
                    return(p);
                }
                else
                {
                    return(null);
                }
            }
        }