Ejemplo n.º 1
0
 /// <summary>
 /// Reflect sphere in given line
 /// </summary>
 public Sphere ReflectIn(Line3d l)
 {
     return(new Sphere(this.Center.ReflectIn(l), this.R));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Reflect segment in given line
 /// </summary>
 public virtual Segment3d ReflectIn(Line3d l)
 {
     return(new Segment3d(P1.ReflectIn(l), P2.ReflectIn(l)));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Reflect plane in given line
 /// </summary>
 public Plane3d ReflectIn(Line3d l)
 {
     return(new Plane3d(this.Point.ReflectIn(l), this.Normal.ReflectIn(l)));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Orthogonal projection of the sphere to the line
        /// </summary>
        public Segment3d ProjectionTo(Line3d l)
        {
            Point3d p = this.Center.ProjectionTo(l);

            return(new Segment3d(p.Translate(this.R * l.Direction.Normalized), p.Translate(-this.R * l.Direction.Normalized)));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Reflect ellipse in given line
 /// </summary>
 public Ellipse ReflectIn(Line3d l)
 {
     return(new Ellipse(this.Center.ReflectIn(l), _v1.ReflectIn(l), _v2.ReflectIn(l)));
 }
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>
        /// Intersection of ellipsoid with line.
        /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Segment3d'.
        /// </summary>
        public object IntersectionWith(Line3d s)
        {
            // Analytical solution from:
            // https://johannesbuchner.github.io/intersection/intersection_line_ellipsoid.html

            // Define local cordinate system for ellipsoid
            // and present line in parametric form in local coordinate system
            // x: t + x0
            // y: k * t + y0
            // z: l * t + z0
            // For numerical stability choose local X axis such that k<=1 and l<=1 !!!

            Coord3d  lc = new Coord3d(_point, _v1, _v2);
            Vector3d v0 = s.Direction.ConvertTo(lc);

            if (Abs(v0.Y) > Abs(v0.X) || Abs(v0.Z) > Abs(v0.X))
            {
                // Bad choice of X axis, try again
                lc = new Coord3d(_point, _v2, _v3);
                v0 = s.Direction.ConvertTo(lc);
                if (Abs(v0.Y) > Abs(v0.X) || Abs(v0.Z) > Abs(v0.X))
                {
                    lc = new Coord3d(_point, _v3, _v1);
                    v0 = s.Direction.ConvertTo(lc);
                }
            }
            // Normalize direction vector
            double k = v0.Y / v0.X;
            double l = v0.Z / v0.X;

            Point3d p0 = s.Point.ConvertTo(lc);
            double  x0 = p0.X;
            double  y0 = p0.Y;
            double  z0 = p0.Z;

            double a2b2 = A * A * B * B;
            double a2c2 = A * A * C * C;
            double b2c2 = B * B * C * C;

            double det = a2b2 * C * C * (a2b2 * l * l + a2c2 * k * k - A * A * k * k * z0 * z0 +
                                         2 * A * A * k * l * y0 * z0 - A * A * l * l * y0 * y0 + b2c2 -
                                         B * B * l * l * x0 * x0 + 2 * B * B * l * x0 * z0 - B * B * z0 * z0 -
                                         C * C * k * k * x0 * x0 + 2 * C * C * k * x0 * y0 - C * C * y0 * y0);

            if (det < -GeometRi3D.Tolerance)
            {
                return(null);
            }

            double sum1 = a2b2 * l * z0 + a2c2 * k * y0 + b2c2 * x0;
            double sum2 = a2b2 * l * l + a2c2 * k * k + b2c2;

            if (Abs(det) <= GeometRi3D.Tolerance)
            {
                // Intersection is point
                double t = -sum1 / sum2;
                return(new Point3d(t + x0, k * t + y0, l * t + z0, lc));
            }
            else
            {
                double  t  = -(sum1 + Sqrt(det)) / sum2;
                Point3d p1 = new Point3d(t + x0, k * t + y0, l * t + z0, lc);
                t = -(sum1 - Sqrt(det)) / sum2;
                Point3d p2 = new Point3d(t + x0, k * t + y0, l * t + z0, lc);
                return(new Segment3d(p1, p2));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Reflect point in given line
        /// </summary>
        public Point3d ReflectIn(Line3d l)
        {
            Vector3d v = new Vector3d(this, this.ProjectionTo(l));

            return(this.Translate(2 * v));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns shortest distance to the line
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        public double DistanceTo(Line3d l)
        {
            Vector3d v = new Vector3d(this, l.Point);

            return(v.Cross(l.Direction).Norm / l.Direction.Norm);
        }