/// <summary> /// Gets the transform to viewport space. /// </summary> /// <param name="visual"> /// The visual. /// </param> /// <returns> /// A transformation matrix. /// </returns> public static Matrix3D GetViewportTransform(Visual3D visual) { var totalTransform = Matrix3D.Identity; DependencyObject obj = visual; while (obj != null) { var viewport3DVisual = obj as Viewport3DVisual; if (viewport3DVisual != null) { var viewportTotalTransform = Viewport3DHelper.GetTotalTransform(viewport3DVisual); totalTransform.Append(viewportTotalTransform); return totalTransform; } var mv = obj as ModelVisual3D; if (mv != null) { if (mv.Transform != null) { totalTransform.Append(mv.Transform.Value); } } obj = VisualTreeHelper.GetParent(obj); } throw new InvalidOperationException("The visual is not added to a Viewport3D."); // At this point, we know obj is Viewport3DVisual }
/// <summary> /// Get the ray into the view volume given by the position in 2D (screen coordinates) /// </summary> /// <param name="position"></param> /// <returns></returns> public Ray3D GetRay(Point position) { Point3D point1, point2; bool ok = Viewport3DHelper.Point2DtoPoint3D(Viewport, position, out point1, out point2); if (!ok) { return(null); } return(new Ray3D { Origin = point1, Direction = point2 - point1 }); }
/// <summary> /// Calculate the screen position of a 3D point /// </summary> /// <param name="p"></param> /// <returns></returns> public Point Project(Point3D p) { return(Viewport3DHelper.Point3DtoPoint2D(Viewport, p)); }
/// <summary> /// Finds the nearest visual. /// </summary> /// <param name="pt"> /// The pt. /// </param> /// <returns> /// A visual. /// </returns> public Visual3D FindNearestVisual(Point pt) { return(Viewport3DHelper.FindNearestVisual(this.Viewport, pt)); }
/// <summary> /// Finds the nearest point. /// </summary> /// <param name="pt"> /// The pt. /// </param> /// <returns> /// A point. /// </returns> public Point3D?FindNearestPoint(Point pt) { return(Viewport3DHelper.FindNearestPoint(this.Viewport, pt)); }
/// <summary> /// Finds the nearest object. /// </summary> /// <param name="pt"> /// The pt. /// </param> /// <param name="pos"> /// The pos. /// </param> /// <param name="normal"> /// The normal. /// </param> /// <param name="obj"> /// The obj. /// </param> /// <returns> /// The find nearest. /// </returns> public bool FindNearest(Point pt, out Point3D pos, out Vector3D normal, out DependencyObject obj) { return(Viewport3DHelper.FindNearest(this.Viewport, pt, out pos, out normal, out obj)); }
/// <summary> /// Copies the view to the clipboard. /// </summary> public void Copy() { Viewport3DHelper.Copy( this.Viewport, this.Viewport.ActualWidth * 2, this.Viewport.ActualHeight * 2, Brushes.White); }