/// <summary>
        /// Clips given ray on box, or returns null if ray does not intersect box.
        /// </summary>
        private static Line3d?Clip(Box3d box, Ray3d ray0)
        {
            ray0.Direction = ray0.Direction.Normalized;

            if (!box.Intersects(ray0, out double t0))
            {
                return(null);
            }
            var p0 = ray0.GetPointOnRay(t0);

            var ray1 = new Ray3d(ray0.GetPointOnRay(t0 + box.Size.Length), -ray0.Direction);

            if (!box.Intersects(ray1, out double t1))
            {
                throw new InvalidOperationException();
            }
            var p1 = ray1.GetPointOnRay(t1);

            return(new Line3d(p0, p1));
        }
Beispiel #2
0
        /// <summary>
        /// Projects a point onto the plane along given direction.
        /// </summary>
        public static V3d Project(this Plane3d plane, V3d p, V3d direction)
        {
            var r = new Ray3d(p, direction);

            if (r.Intersects(plane, out double t))
            {
                return(r.GetPointOnRay(t));
            }
            else
            {
                throw new Exception(string.Format(
                                        "Failed to project point {0} onto plane {1} along direction {2}.", p, plane, direction)
                                    );
            }
        }
Beispiel #3
0
        /// <summary>
        /// Projects points onto plane along given direction.
        /// </summary>
        public static V3d[] Project(this Plane3d plane, V3d[] pointArray, V3d direction, int startIndex = 0, int count = 0)
        {
            if (pointArray == null)
            {
                throw new ArgumentNullException();
            }
            if (startIndex < 0 || startIndex >= pointArray.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (count < 0 || startIndex + count >= pointArray.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (count == 0)
            {
                count = pointArray.Length - startIndex;
            }

            var result = new V3d[count];

            for (int i = startIndex, j = 0; j < count; i++, j++)
            {
                var r = new Ray3d(pointArray[i], direction);
                if (r.Intersects(plane, out double t))
                {
                    result[j] = r.GetPointOnRay(t);
                }
                else
                {
                    throw new Exception(string.Format(
                                            "Failed to project point {0} onto plane {1} along direction {2}.", pointArray[i], plane, direction)
                                        );
                }
            }
            ;
            return(result);
        }