示例#1
0
        /// <summary>
        /// Initializes the smallest box that contains a set of points.
        /// </summary>
        /// <param name="basePlane">Orientation of the box.</param>
        /// <param name="points">Points to include, Invalid points will be ignored.</param>
        public Box(Plane basePlane, IEnumerable <Point3d> points)
        {
            // David: this code is untested.

            m_dx = new Interval(+1, -1);
            m_dy = new Interval(0, 0);
            m_dz = new Interval(0, 0);

            m_plane = basePlane;
            if (!m_plane.IsValid)
            {
                return;
            }
            if (points == null)
            {
                return;
            }

            double x0 = double.MaxValue;
            double x1 = double.MinValue;
            double y0 = double.MaxValue;
            double y1 = double.MinValue;
            double z0 = double.MaxValue;
            double z1 = double.MinValue;

            int N = 0;

            foreach (Point3d pt in points)
            {
                if (!pt.IsValid)
                {
                    continue;
                }

                N++;

                Point3d pt_mapped;
                m_plane.RemapToPlaneSpace(pt, out pt_mapped);

                x0 = Math.Min(x0, pt_mapped.m_x);
                x1 = Math.Max(x1, pt_mapped.m_x);
                y0 = Math.Min(y0, pt_mapped.m_y);
                y1 = Math.Max(y1, pt_mapped.m_y);
                z0 = Math.Min(z0, pt_mapped.m_z);
                z1 = Math.Max(z1, pt_mapped.m_z);
            }

            if (N == 0)
            {
                return;
            }

            m_dx = new Interval(x0, x1);
            m_dy = new Interval(y0, y1);
            m_dz = new Interval(z0, z1);

            MakeValid();
        }
示例#2
0
        /// <summary>
        /// Finds the closest point on or in the Box. The box should be Valid for this to work.
        /// </summary>
        /// <param name="point">Sample point.</param>
        /// <returns>The point on or in the box that is closest to the sample point.</returns>
        public Point3d ClosestPoint(Point3d point)
        {
            // David: This code is untested.

            // Remap point to m_plane coordinates
            Point3d pt;

            if (!m_plane.RemapToPlaneSpace(point, out pt))
            {
                return(Point3d.Unset);
            }

            // Project x, y and z onto/into the box
            double x = pt.m_x;
            double y = pt.m_y;
            double z = pt.m_z;

            x = Math.Max(x, m_dx.T0);
            y = Math.Max(y, m_dy.T0);
            z = Math.Max(z, m_dz.T0);

            x = Math.Min(x, m_dx.T1);
            y = Math.Min(y, m_dy.T1);
            z = Math.Min(z, m_dz.T1);

            return(m_plane.PointAt(x, y, z));
        }