Ejemplo n.º 1
0
 /// <summary>
 /// Reflect triangle in given plane
 /// </summary>
 public Triangle ReflectIn(Plane3d s)
 {
     return(new Triangle(_a.ReflectIn(s), _b.ReflectIn(s), _c.ReflectIn(s)));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Reflect ray in given plane
 /// </summary>
 public Ray3d ReflectIn(Plane3d s)
 {
     return(new Ray3d(this.Point.ReflectIn(s), this.Direction.ReflectIn(s)));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Reflect ellipse in given plane
 /// </summary>
 public Ellipse ReflectIn(Plane3d s)
 {
     return(new Ellipse(this.Center.ReflectIn(s), _v1.ReflectIn(s), _v2.ReflectIn(s)));
 }
Ejemplo n.º 4
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);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Intersection of ellipsoid with plane.
        /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Ellipse'.
        /// </summary>
        public object IntersectionWith(Plane3d plane)
        {
            // Solution 1:
            // Peter Paul Klein
            // On the Ellipsoid and Plane Intersection Equation
            // Applied Mathematics, 2012, 3, 1634-1640 (DOI:10.4236/am.2012.311226)

            // Solution 2:
            // Sebahattin Bektas
            // Intersection of an Ellipsoid and a Plane
            // International Journal of Research in Engineering and Applied Sciences, VOLUME 6, ISSUE 6 (June, 2016)

            Coord3d lc = new Coord3d(_point, _v1, _v2, "LC1");

            plane.SetCoord(lc);
            double Ax, Ay, Az, Ad;
            double a, b, c;

            if (Abs(plane.C) >= Abs(plane.A) && Abs(plane.C) >= Abs(plane.B))
            {
                a = this.A; b = this.B; c = this.C;
            }
            else
            {
                lc = new Coord3d(_point, _v2, _v3, "LC2");
                plane.SetCoord(lc);
                if (Abs(plane.C) >= Abs(plane.A) && Abs(plane.C) >= Abs(plane.B))
                {
                    a = this.B; b = this.C; c = this.A;
                }
                else
                {
                    lc = new Coord3d(_point, _v3, _v1, "LC3");
                    plane.SetCoord(lc);
                    a = this.C; b = this.A; c = this.B;
                }
            }

            Ax = plane.A; Ay = plane.B; Az = plane.C; Ad = plane.D;
            double tmp = (Az * Az * c * c);
            double AA  = 1.0 / (a * a) + Ax * Ax / tmp;
            double BB  = 2.0 * Ax * Ay / tmp;
            double CC  = 1.0 / (b * b) + Ay * Ay / tmp;
            double DD  = 2.0 * Ax * Ad / tmp;
            double EE  = 2.0 * Ay * Ad / tmp;
            double FF  = Ad * Ad / tmp - 1.0;

            double det = 4.0 * AA * CC - BB * BB;

            if (GeometRi3D.AlmostEqual(det, 0))
            {
                return(null);
            }
            double X0 = (BB * EE - 2 * CC * DD) / det;
            double Y0 = (BB * DD - 2 * AA * EE) / det;
            double Z0 = -(Ax * X0 + Ay * Y0 + Ad) / Az;

            Point3d P0 = new Point3d(X0, Y0, Z0, lc);

            if (P0.BelongsTo(this))
            {
                // the plane is tangent to ellipsoid
                return(P0);
            }
            else if (P0.IsInside(this))
            {
                Vector3d q  = P0.ToVector.ConvertTo(lc);
                Matrix3d D1 = Matrix3d.DiagonalMatrix(1 / a, 1 / b, 1 / c);
                Vector3d r  = plane.Normal.ConvertTo(lc).OrthogonalVector.Normalized;
                Vector3d s  = plane.Normal.ConvertTo(lc).Cross(r).Normalized;

                double omega = 0;
                double qq, qr, qs, rr, ss, rs;
                if (!GeometRi3D.AlmostEqual((D1 * r) * (D1 * s), 0))
                {
                    rr = (D1 * r) * (D1 * r);
                    rs = (D1 * r) * (D1 * s);
                    ss = (D1 * s) * (D1 * s);
                    if (GeometRi3D.AlmostEqual(rr - ss, 0))
                    {
                        omega = PI / 4;
                    }
                    else
                    {
                        omega = 0.5 * Atan(2.0 * rs / (rr - ss));
                    }
                    Vector3d rprim = Cos(omega) * r + Sin(omega) * s;
                    Vector3d sprim = -Sin(omega) * r + Cos(omega) * s;
                    r = rprim;
                    s = sprim;
                }

                qq = (D1 * q) * (D1 * q);
                qr = (D1 * q) * (D1 * r);
                qs = (D1 * q) * (D1 * s);
                rr = (D1 * r) * (D1 * r);
                ss = (D1 * s) * (D1 * s);

                double d = qq - qr * qr / rr - qs * qs / ss;
                AA = Sqrt((1 - d) / rr);
                BB = Sqrt((1 - d) / ss);

                return(new Ellipse(P0, AA * r, BB * s));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Intersection of ellipse with plane.
        /// Returns 'null' (no intersection) or object of type 'Ellipse', 'Point3d' or 'Segment3d'.
        /// </summary>
        public object IntersectionWith(Plane3d s)
        {
            if (this.Normal.IsParallelTo(s.Normal))
            {
                if (this.Center.BelongsTo(s))
                {
                    // coplanar objects
                    return(this.Copy());
                }
                else
                {
                    // parallel objects
                    return(null);
                }
            }
            else
            {
                Line3d   l           = (Line3d)s.IntersectionWith(new Plane3d(this.Center, this.Normal));
                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;
                }

                // Find intersection of line and ellipse (2D)
                // Solution from: http://www.ambrsoft.com/TrigoCalc/Circles2/Ellipse/EllipseLine.htm

                // 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 (GeometRi3D.AlmostEqual(det, 0))
                {
                    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
                {
                    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)));
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Reflect sphere in given plane
 /// </summary>
 public Sphere ReflectIn(Plane3d s)
 {
     return(new Sphere(this.Center.ReflectIn(s), this.R));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Intersection of two circles.
        /// Returns 'null' (no intersection) or object of type 'Circle3d', 'Point3d' or 'Segment3d'.
        /// In 2D (coplanar circles) the segment will define two intersection points.
        /// </summary>
        public object IntersectionWith(Circle3d c)
        {
            // Relative tolerance ================================
            if (!GeometRi3D.UseAbsoluteTolerance)
            {
                double tol = GeometRi3D.Tolerance;
                GeometRi3D.Tolerance            = tol * Max(this.R, c.R);
                GeometRi3D.UseAbsoluteTolerance = true;
                object result = this.IntersectionWith(c);
                GeometRi3D.UseAbsoluteTolerance = false;
                GeometRi3D.Tolerance            = tol;
                return(result);
            }
            //====================================================

            if (this.Normal.IsParallelTo(c.Normal))
            {
                if (this.Center.BelongsTo(new Plane3d(c.Center, c.Normal)))
                {
                    // Coplanar objects
                    // Search 2D intersection of two circles

                    // Equal circles
                    if (this.Center == c.Center && GeometRi3D.AlmostEqual(this.R, c.R))
                    {
                        return(this.Copy());
                    }

                    double d = this.Center.DistanceTo(c.Center);

                    // Separated circles
                    if (GeometRi3D.Greater(d, this.R + c.R))
                    {
                        return(null);
                    }

                    // One circle inside the other
                    if (d < Abs(this.R - c.R) - GeometRi3D.Tolerance)
                    {
                        return(null);
                    }

                    // Outer tangency
                    if (GeometRi3D.AlmostEqual(d, this.R + c.R))
                    {
                        Vector3d vec = new Vector3d(this.Center, c.Center);
                        return(this.Center.Translate(this.R * vec.Normalized));
                    }

                    // Inner tangency
                    if (Abs(Abs(this.R - c.R) - d) < GeometRi3D.Tolerance)
                    {
                        Vector3d vec = new Vector3d(this.Center, c.Center);
                        if (this.R > c.R)
                        {
                            return(this.Center.Translate(this.R * vec.Normalized));
                        }
                        else
                        {
                            return(this.Center.Translate(-this.R * vec.Normalized));
                        }
                    }

                    // intersecting circles
                    // Create local CS with origin in circle's center
                    Vector3d vec1     = new Vector3d(this.Center, c.Center);
                    Vector3d vec2     = vec1.Cross(this.Normal);
                    Coord3d  local_cs = new Coord3d(this.Center, vec1, vec2);

                    double  x  = 0.5 * (d * d - c.R * c.R + this.R * this.R) / d;
                    double  y  = 0.5 * Sqrt((-d + c.R - this.R) * (-d - c.R + this.R) * (-d + c.R + this.R) * (d + c.R + this.R)) / d;
                    Point3d p1 = new Point3d(x, y, 0, local_cs);
                    Point3d p2 = new Point3d(x, -y, 0, local_cs);
                    return(new Segment3d(p1, p2));
                }
                else
                {
                    // parallel objects
                    return(null);
                }
            }
            else
            {
                // Check 3D intersection
                Plane3d plane = new Plane3d(this.Center, this.Normal);
                object  obj   = plane.IntersectionWith(c);

                if (obj == null)
                {
                    return(null);
                }
                else if (obj.GetType() == typeof(Point3d))
                {
                    Point3d p = (Point3d)obj;
                    if (p.BelongsTo(c))
                    {
                        return(p);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Segment3d s = (Segment3d)obj;
                    return(s.IntersectionWith(this));
                }
            }
        }
Ejemplo n.º 9
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);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Orthogonal projection of the sphere to the plane
        /// </summary>
        public Circle3d ProjectionTo(Plane3d s)
        {
            Point3d p = this.Center.ProjectionTo(s);

            return(new Circle3d(p, this.R, s.Normal));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Reflect point in given plane
        /// </summary>
        public Point3d ReflectIn(Plane3d s)
        {
            Vector3d v = new Vector3d(this, this.ProjectionTo(s));

            return(this.Translate(2 * v));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Returns shortest distance from point to the plane
 /// </summary>
 public double DistanceTo(Plane3d s)
 {
     s.SetCoord(this.Coord);
     return(Abs(X * s.A + Y * s.B + Z * s.C + s.D) / Sqrt(s.A * s.A + s.B * s.B + s.C * s.C));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Reflect segment in given plane
 /// </summary>
 public virtual Segment3d ReflectIn(Plane3d s)
 {
     return(new Segment3d(P1.ReflectIn(s), P2.ReflectIn(s)));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Reflect plane in given plane
 /// </summary>
 public Plane3d ReflectIn(Plane3d s)
 {
     return(new Plane3d(this.Point.ReflectIn(s), this.Normal.ReflectIn(s)));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Get intersection of line with plane.
 /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Line3d'.
 /// </summary>
 public virtual object IntersectionWith(Plane3d s)
 {
     return(s.IntersectionWith(this));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Orthogonal projection of the circle to plane
 /// </summary>
 public Ellipse ProjectionTo(Plane3d s)
 {
     return(this.ToEllipse.ProjectionTo(s));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Reflect line in given plane
 /// </summary>
 public virtual Line3d ReflectIn(Plane3d s)
 {
     return(new Line3d(this.Point.ReflectIn(s), this.Direction.ReflectIn(s)));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Reflect circle in given plane
 /// </summary>
 public Circle3d ReflectIn(Plane3d s)
 {
     return(new Circle3d(this.Center.ReflectIn(s), this.R, this.Normal.ReflectIn(s)));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Returns shortest distance from point to the plane
 /// </summary>
 public double DistanceTo(Plane3d s)
 {
     s.SetCoord(this.Coord);
     return(Abs(X * s.A + Y * s.B + Z * s.C + s.D) / Sqrt(Math.Pow(s.A, 2) + Math.Pow(s.B, 2) + Math.Pow(s.C, 2)));
 }