using System; using System.Windows; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; namespace HelixToolkit { /// /// Helper class for Media3D.Camera. /// public class CameraHelper { /// /// Creates a default perspective camera. /// /// public static PerspectiveCamera CreateDefaultCamera() { var cam = new PerspectiveCamera(); Reset(cam); return cam; } /// /// Resets the specified camera. /// /// The camera. public static void Reset(Camera camera) { var pcamera = camera as PerspectiveCamera; if (pcamera != null) Reset(pcamera); var ocamera = camera as OrthographicCamera; if (ocamera != null) Reset(ocamera); } /// /// Resets the specified perspective camera. /// /// The camera. public static void Reset(PerspectiveCamera camera) { if (camera == null) return; camera.Position = new Point3D(2, 16, 20); camera.LookDirection = new Vector3D(-2, -16, -20); camera.UpDirection = new Vector3D(0, 0, 1); camera.FieldOfView = 45; camera.NearPlaneDistance = 0.1; camera.FarPlaneDistance = 100000; } /// /// Resets the specified orthographic camera. /// /// The camera. public static void Reset(OrthographicCamera camera) { if (camera == null) return; camera.Position = new Point3D(2, 16, 20); camera.LookDirection = new Vector3D(-2, -16, -20); camera.UpDirection = new Vector3D(0, 0, 1); camera.Width = 40; camera.NearPlaneDistance = 0.1; camera.FarPlaneDistance = 100000; } /// /// Copies all members of the source . /// /// Source camera. /// Destination camera. public static void Copy(PerspectiveCamera source, PerspectiveCamera dest) { if (source == null || dest == null) return; dest.LookDirection = source.LookDirection; dest.Position = source.Position; dest.UpDirection = source.UpDirection; dest.FieldOfView = source.FieldOfView; dest.NearPlaneDistance = source.NearPlaneDistance; dest.FarPlaneDistance = source.FarPlaneDistance; } /// /// Copy the direction of the source only. Used for the CoordinateSystem view. /// /// /// /// public static void CopyDirectionOnly(ProjectionCamera source, ProjectionCamera dest, double distance) { if (source == null || dest == null) return; Vector3D dir = source.LookDirection; dir.Normalize(); dir *= distance; dest.LookDirection = dir; dest.Position = new Point3D(-dest.LookDirection.X, -dest.LookDirection.Y, -dest.LookDirection.Z); dest.UpDirection = source.UpDirection; } /// /// Animates the camera position and direction /// /// Camera /// The position to animate to /// The direction to animate to /// The up direction to animate to /// Animation time in milliseconds public static void AnimateTo(ProjectionCamera camera, Point3D newPosition, Vector3D newDirection, Vector3D newUpDirection, double animationTime) { var fromPosition = camera.Position; var fromDirection = camera.LookDirection; var fromUpDirection = camera.UpDirection; camera.Position = newPosition; camera.LookDirection = newDirection; camera.UpDirection = newUpDirection; if (animationTime > 0) { var a1 = new Point3DAnimation(fromPosition, newPosition, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.PositionProperty, a1); var a2 = new Vector3DAnimation(fromDirection, newDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.LookDirectionProperty, a2); var a3 = new Vector3DAnimation(fromUpDirection, newUpDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.UpDirectionProperty, a3); } } /// /// Animates the camera position and direction /// /// An ortographic camera. /// The Width to animate to. /// Animation time in milliseconds public static void AnimateWidth(OrthographicCamera camera, double newWidth, double animationTime) { double fromWidth = camera.Width; camera.Width = newWidth; if (animationTime > 0) { var a1 = new DoubleAnimation(fromWidth, newWidth, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(OrthographicCamera.WidthProperty, a1); } } } }