HelixToolkit.Wpf.BoundingSphere.CreateFromRect3D C# (CSharp) Method

CreateFromRect3D() public static method

Creates a BoundingSphere from a Rect3D.
public static CreateFromRect3D ( System.Windows.Media.Media3D.Rect3D rect ) : BoundingSphere
rect System.Windows.Media.Media3D.Rect3D /// The 3D rectangle. ///
return BoundingSphere
        public static BoundingSphere CreateFromRect3D(Rect3D rect)
        {
            return new BoundingSphere
                {
                    Center = new Point3D(rect.X + (rect.SizeX * 0.5), rect.Y + (rect.SizeY * 0.5), rect.Z + (rect.SizeZ * 0.5)),
                    Radius = 0.5 * Math.Sqrt((rect.SizeX * rect.SizeX) + (rect.SizeY * rect.SizeY) + (rect.SizeZ * rect.SizeZ))
                };
        }

Usage Example

示例#1
0
        /// <summary>
        /// Gets the distance from the camera for the specified visual.
        /// </summary>
        /// <param name="c">
        /// The visual.
        /// </param>
        /// <param name="cameraPos">
        /// The camera position.
        /// </param>
        /// <param name="transform">
        /// The total transform of the visual.
        /// </param>
        /// <returns>
        /// The camera distance.
        /// </returns>
        private double GetCameraDistance(Visual3D c, Point3D cameraPos, Transform3D transform)
        {
            var bounds = Visual3DHelper.FindBounds(c, transform);

            switch (this.Method)
            {
            case SortingMethod.BoundingBoxCenter:
                var mid = new Point3D(
                    bounds.X + bounds.SizeX * 0.5, bounds.Y + bounds.SizeY * 0.5, bounds.Z + bounds.SizeZ * 0.5);
                return((mid - cameraPos).LengthSquared);

            case SortingMethod.BoundingBoxCorners:
                double d = double.MaxValue;
                d = Math.Min(d, cameraPos.DistanceTo(new Point3D(bounds.X, bounds.Y, bounds.Z)));
                d = Math.Min(d, cameraPos.DistanceTo(new Point3D(bounds.X + bounds.SizeX, bounds.Y, bounds.Z)));
                d = Math.Min(
                    d, cameraPos.DistanceTo(new Point3D(bounds.X + bounds.SizeX, bounds.Y + bounds.SizeY, bounds.Z)));
                d = Math.Min(d, cameraPos.DistanceTo(new Point3D(bounds.X, bounds.Y + bounds.SizeY, bounds.Z)));
                d = Math.Min(d, cameraPos.DistanceTo(new Point3D(bounds.X, bounds.Y, bounds.Z + bounds.SizeZ)));
                d = Math.Min(
                    d, cameraPos.DistanceTo(new Point3D(bounds.X + bounds.SizeX, bounds.Y, bounds.Z + bounds.SizeZ)));
                d = Math.Min(
                    d,
                    cameraPos.DistanceTo(
                        new Point3D(bounds.X + bounds.SizeX, bounds.Y + bounds.SizeY, bounds.Z + bounds.SizeZ)));
                d = Math.Min(
                    d, cameraPos.DistanceTo(new Point3D(bounds.X, bounds.Y + bounds.SizeY, bounds.Z + bounds.SizeZ)));
                return(d);

            default:
                var boundingSphere = BoundingSphere.CreateFromRect3D(bounds);
                return(boundingSphere.DistanceFrom(cameraPos));
            }
        }