Пример #1
0
        internal static Point3D GetCameraPosition(this Viewport3DX viewport)
        {
            var pos = Vector3.TransformCoordinate(viewport.Camera.Position.ToVector3(), WorldToModelMatrix());

            return(new Point3D(pos.X, pos.Y, pos.Z));
        }
Пример #2
0
 /// <summary>
 /// Gets the camera transform.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <returns>
 /// The camera transform.
 /// </returns>
 public static Matrix3D GetViewProjectionMatrix3D(this Viewport3DX viewport)
 {
     return(GetViewProjectionMatrix(viewport).ToMatrix3D());
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZoomHandler"/> class.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <param name="changeFieldOfView">
 /// if set to <c>true</c> [change field of view].
 /// </param>
 public ZoomHandler(Viewport3DX viewport, bool changeFieldOfView = false)
     : base(viewport)
 {
     this.changeFieldOfView = changeFieldOfView;
 }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="viewport"></param>
        /// <param name="p"></param>
        /// <param name="position"></param>
        /// <param name="normal"></param>
        /// <returns></returns>
        public static Vector3?UnProjectOnPlane(this Viewport3DX viewport, Vector2 p, Vector3 position, Vector3 normal)
        {
            var plane = new Plane(position, normal);

            return(UnProjectOnPlane(viewport, p, plane));
        }
Пример #5
0
 /// <summary>
 /// Saves the viewport to a file.
 /// </summary>
 /// <param name="view">The view.</param>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="background">The background brush.</param>
 /// <param name="m">The oversampling multiplier.</param>
 public static void SaveBitmap(this Viewport3DX view, string fileName, Brush background = null, int m = 1)
 {
     // var exporter = new BitmapExporter(fileName) { Background = background, OversamplingMultiplier = m };
     // exporter.Export(view);
 }
 public override void OnMouse3DUp(object sender, RoutedEventArgs e)
 {
     base.OnMouse3DUp(sender, e);
     if (this.isCaptured)
     {
         this.isCaptured = false;
         this.camera = null;
         this.viewport = null;
     }
 }
Пример #7
0
        /// <summary>
        /// Un-projects the specified 2D screen point to a ray.
        /// </summary>
        /// <param name="viewport">The viewport.</param>
        /// <param name="point2d">The point.</param>
        /// <returns>The ray.</returns>
        public static Ray3D UnProjectToRay(this Viewport3DX viewport, Point point2d)
        {
            var r = viewport.UnProject(point2d.ToVector2());

            return(new Ray3D(r.Position.ToPoint3D(), r.Direction.ToVector3D()));
        }
Пример #8
0
 /// <summary>
 /// Exports the viewport.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 protected virtual void ExportViewport(Viewport3DX viewport)
 {
 }
 public static Matrix3D GetViewportMatrix3D(this Viewport3DX viewport)
 {
     return(viewport.GetViewportMatrix().ToMatrix3D());
 }
Пример #10
0
        /// <summary>
        /// 連續點擊兩下產生出球
        /// </summary>
        public void OnMouseDoubleClickHandler(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //如果當前可點擊的狀態為否則直接return
            if (!MainViewModel.ProjData.CanSelectPoints)
            {
                return;
            }

            Viewport3DX viewport = sender as Viewport3DX;

            if (viewport == null)
            {
                return;
            }
            var point    = e.GetPosition(viewport);
            var hitTests = viewport.FindHits(point);

            //null或是點到的東西為少於零個
            if (hitTests == null || hitTests.Count <= 0)
            {
                return;
            }

            foreach (var hit in hitTests)
            {
                //有可能點到對稱面,不是BoneModel的話換下一個模型
                if (!(hit.ModelHit is BoneModel))
                {
                    continue;
                }

                BallModel ball = new BallModel
                {
                    BallName = "Ball",
                };

                //這邊很怪 拿不到該有的屬性資訊只有一些基底類別的資訊
                BoneModel model = hit.ModelHit as BoneModel;
                foreach (BoneModel modeltem in MainViewModel.ProjData.BoneCollection)
                {
                    //先找出當前點到的BoneModel是哪個,並將點擊的球的ModelType設定成跟該Model一樣
                    if (modeltem.Geometry.Positions.Count == model.Geometry.Positions.Count)
                    {
                        ball.ModelType = modeltem.ModelType;
                        Binding binding = new Binding("Transform");
                        binding.Source = modeltem;
                        binding.Mode   = BindingMode.OneWay;
                        BindingOperations.SetBinding(ball, HelixToolkit.Wpf.SharpDX.Model3D.TransformProperty, binding);
                    }
                }


                var ballContainer = new HelixToolkit.Wpf.SharpDX.MeshBuilder();
                ball.BallCenter = new Vector3(Convert.ToSingle(hit.PointHit.X), Convert.ToSingle(hit.PointHit.Y), Convert.ToSingle(hit.PointHit.Z));
                ballContainer.AddSphere(ball.BallCenter, 1.5);
                ball.Geometry = ballContainer.ToMeshGeometry3D();
                ball.Material = PhongMaterials.White;

                MainViewModel.ProjData.BallCollection.Add(ball);

                break;
            }
        }
Пример #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZoomRectangleHandler"/> class.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 public ZoomRectangleHandler(Viewport3DX viewport)
     : base(viewport)
 {
 }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PanHandler"/> class.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 public PanHandler(Viewport3DX viewport)
     : base(viewport)
 {
 }
Пример #13
0
        /// <summary>
        /// Zooms the camera to the specified rectangle.
        /// </summary>
        /// <param name="camera">
        /// The camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="zoomRectangle">
        /// The zoom rectangle.
        /// </param>
        public static void ZoomToRectangle(this Camera camera, Viewport3DX viewport, Rect zoomRectangle)
        {
            var pcam = camera as ProjectionCamera;
            if (pcam == null)
            {
                return;
            }

            var topLeftRay = viewport.UnProjectToRay(zoomRectangle.TopLeft);
            var topRightRay = viewport.UnProjectToRay(zoomRectangle.TopRight);
            var centerRay =
                viewport.UnProjectToRay(
                    new Point(
                        (zoomRectangle.Left + zoomRectangle.Right) * 0.5,
                        (zoomRectangle.Top + zoomRectangle.Bottom) * 0.5));

            if (topLeftRay == null || topRightRay == null || centerRay == null)
            {
                // could not invert camera matrix
                return;
            }

            var u = topLeftRay.Direction;
            var v = topRightRay.Direction;
            var w = centerRay.Direction;
            u.Normalize();
            v.Normalize();
            w.Normalize();
            var perspectiveCamera = camera as PerspectiveCamera;
            if (perspectiveCamera != null)
            {
                var distance = pcam.LookDirection.Length;

                // option 1: change distance
                var newDistance = distance * zoomRectangle.Width / viewport.ActualWidth;
                var newLookDirection = newDistance * w;
                var newPosition = perspectiveCamera.Position + ((distance - newDistance) * w);
                var newTarget = newPosition + newLookDirection;
                LookAt(pcam, newTarget, newLookDirection, 200);

                // option 2: change fov
                // double newFieldOfView = Math.Acos(Vector3D.DotProduct(u, v));
                // var newTarget = camera.Position + distance * w;
                // pcamera.FieldOfView = newFieldOfView * 180 / Math.PI;
                // LookAt(camera, newTarget, distance * w, 0);
            }

            var orthographicCamera = camera as OrthographicCamera;
            if (orthographicCamera != null)
            {
                orthographicCamera.Width *= zoomRectangle.Width / viewport.ActualWidth;
                var oldTarget = pcam.Position + pcam.LookDirection;
                var distance = pcam.LookDirection.Length;
                var newTarget = centerRay.PlaneIntersection(oldTarget, w);
                if (newTarget != null)
                {
                    orthographicCamera.LookDirection = w * distance;
                    orthographicCamera.Position = newTarget.Value - orthographicCamera.LookDirection;
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Zooms to fit the specified sphere.
        /// </summary>
        /// <param name="camera">
        /// The camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="center">
        /// The center of the sphere.
        /// </param>
        /// <param name="radius">
        /// The radius of the sphere.
        /// </param>
        /// <param name="animationTime">
        /// The animation time.
        /// </param>
        public static void ZoomExtents(
            this Camera camera, Viewport3DX viewport, Point3D center, double radius, double animationTime = 0)
        {
            var projectionCamera = camera as ProjectionCamera;
            if (projectionCamera == null)
            {
                return;
            }

            // var target = Camera.Position + Camera.LookDirection;
            if (camera is PerspectiveCamera)
            {
                var pcam = camera as PerspectiveCamera;
                double disth = radius / Math.Tan(0.5 * pcam.FieldOfView * Math.PI / 180);
                double vfov = pcam.FieldOfView / viewport.ActualWidth * viewport.ActualHeight;
                double distv = radius / Math.Tan(0.5 * vfov * Math.PI / 180);

                double dist = Math.Max(disth, distv);
                var dir = projectionCamera.LookDirection;
                dir.Normalize();
                LookAt(projectionCamera, center, dir * dist, animationTime);
            }

            if (camera is OrthographicCamera)
            {
                LookAt(projectionCamera, center, projectionCamera.LookDirection, animationTime);
                double newWidth = radius * 2;

                if (viewport.ActualWidth > viewport.ActualHeight)
                {
                    newWidth = radius * 2 * viewport.ActualWidth / viewport.ActualHeight;
                }

                AnimateWidth(camera as OrthographicCamera, newWidth, animationTime);
            }
        }
Пример #15
0
 public void DettachViewport3DX(Viewport3DX viewport)
 {
     viewports.Remove(viewport);
 }
        /// <summary>
        /// Finds the bounding box of the viewport.
        /// </summary>
        /// <param name="viewport">The viewport.</param>
        /// <returns>The bounding box.</returns>
        public static Rect3D FindBounds3D(this Viewport3DX viewport)
        {
            var bounds = viewport.FindBounds();

            return(new Rect3D(bounds.Minimum.ToPoint3D(), (bounds.Maximum - bounds.Minimum).ToSize3D()));
        }
Пример #17
0
 public MouseMove3DEventArgs(object source, HitTestResult hitTestResult, Point position, Viewport3DX viewport = null)
     : base(GeometryModel3D.MouseMove3DEvent, source, hitTestResult, position, viewport)
 {
 }
 /// <summary>
 /// Finds the hits for a given 2D viewport position.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <param name="position">
 /// The position.
 /// </param>
 /// <returns>
 /// List of hits, sorted with the nearest hit first.
 /// </returns>
 public static IList <HitTestResult> FindHits(this Viewport3DX viewport, Point position)
 {
     return(viewport.FindHits(position.ToVector2()));
 }
Пример #19
0
        /// <summary>
        /// Zooms to fit the specified bounding rectangle.
        /// </summary>
        /// <param name="camera">
        /// The actual camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="bounds">
        /// The bounding rectangle.
        /// </param>
        /// <param name="animationTime">
        /// The animation time.
        /// </param>
        public static void ZoomExtents(
            this Camera camera, Viewport3DX viewport, Rect3D bounds, double animationTime = 0)
        {
            var projectionCamera = camera as ProjectionCamera;
            if (projectionCamera == null)
            {
                return;
            }

            var diagonal = new Vector3D(bounds.SizeX, bounds.SizeY, bounds.SizeZ);
            var center = bounds.Location + (diagonal * 0.5);
            double radius = diagonal.Length * 0.5;
            ZoomExtents(projectionCamera, viewport, center, radius, animationTime);
        }
 /// <summary>
 /// Un-projects a 2D screen point.
 /// </summary>
 /// <param name="viewport">The viewport.</param>
 /// <param name="point2d">The input point.</param>
 /// <returns>The ray.</returns>
 public static Ray UnProject(this Viewport3DX viewport, Vector2 point2d)
 {
     viewport.UnProject(point2d, out var ray);
     return(ray);
 }
Пример #21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="viewport"></param>
        /// <param name="point2d"></param>
        /// <returns></returns>
        public static Ray UnProjectToRay(this Viewport3DX viewport, Vector2 point2d)
        {
            var r = viewport.UnProject(point2d);

            return(new Ray(r.Position, r.Direction));
        }
 /// <summary>
 /// Un-projects a 2D screen point.
 /// </summary>
 /// <param name="viewport">The viewport.</param>
 /// <param name="point2d">The input point.</param>
 /// <returns>The ray.</returns>
 public static Ray UnProject(this Viewport3DX viewport, Point point2d)
 {
     return(viewport.UnProject(point2d.ToVector2()));
 }
Пример #23
0
 /// <summary>
 /// Copies the specified viewport to the clipboard.
 /// </summary>
 /// <param name="view">The viewport.</param>
 /// <param name="m">The oversampling multiplier.</param>
 public static void Copy(this Viewport3DX view, int m = 1)
 {
     Clipboard.SetImage(RenderBitmap(view, Brushes.White, m));
 }
        /// <summary>
        /// Projects the specified 3D point to a 2D screen point.
        /// </summary>
        /// <param name="viewport">The viewport.</param>
        /// <param name="point">The 3D point.</param>
        /// <returns>The point.</returns>
        public static Point Project(this Viewport3DX viewport, Point3D point)
        {
            var p = viewport.Project(point.ToVector3());

            return(new Point(p.X, p.Y));
        }
Пример #25
0
 /// <summary>
 /// Copies the specified viewport to the clipboard.
 /// </summary>
 /// <param name="view">The view.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="background">The background.</param>
 /// <param name="m">The oversampling multiplier.</param>
 public static void Copy(this Viewport3DX view, double width, double height, Brush background, int m = 1)
 {
     Clipboard.SetImage(RenderBitmap(view, width, height, background));
 }
 /// <summary>
 /// Zooms to the extents of the specified bounding box.
 /// </summary>
 /// <param name="viewport">The viewport.</param>
 /// <param name="bounds">The bounding rectangle.</param>
 /// <param name="animationTime">The animation time.</param>
 public static void ZoomExtents(this Viewport3DX viewport, Rect3D bounds, double animationTime = 0)
 {
     viewport.Camera.ZoomExtents(viewport, bounds, animationTime);
 }
Пример #27
0
        public void SharpDX_Export_Triangle_Valid()
        {
            var b1 = new MeshBuilder();

            b1.AddTriangle(new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
            var meshGeometry = b1.ToMeshGeometry3D();

            var mesh = new MeshGeometryModel3D();

            mesh.Geometry  = meshGeometry;
            mesh.Material  = PhongMaterials.Green;
            mesh.Transform = new TranslateTransform3D(2, 0, 0);

            var viewport = new Viewport3DX();

            viewport.Items.Add(mesh);

            string temp    = Path.GetTempPath();
            var    objPath = temp + "model.obj";
            var    mtlPath = temp + "model.mtl";

            try
            {
                using (var exporter = new ObjExporter(objPath))
                {
                    exporter.Export(viewport);
                }

                string contentObj  = File.ReadAllText(objPath);
                string expectedObj = @"mtllib ./model.mtl
o object1
g group1
usemtl mat1
v 2 0 0
v 2 1 0
v 2 0 -1
# 3 vertices
vt 0 1
vt 1 1
vt 0 0
# 3 texture coordinates
f 1/1 2/2 3/3
# 1 faces

";

                Assert.AreEqual(expectedObj.Replace("\r\n", "\n"), contentObj.Replace("\r\n", "\n"));

                string contentMtl = File.ReadAllText(mtlPath);
            }
            finally
            {
                if (File.Exists(objPath))
                {
                    File.Delete(objPath);
                }

                if (File.Exists(mtlPath))
                {
                    File.Delete(mtlPath);
                }
            }
        }
 /// <summary>
 /// Zooms to the extents of the specified bounding sphere.
 /// </summary>
 /// <param name="viewport">The viewport.</param>
 /// <param name="center">The center of the sphere.</param>
 /// <param name="radius">The radius of the sphere.</param>
 /// <param name="animationTime">The animation time.</param>
 public static void ZoomExtents(this Viewport3DX viewport, Point3D center, double radius, double animationTime = 0)
 {
     viewport.Camera.ZoomExtents(viewport, center, radius, animationTime);
 }
Пример #29
0
        internal static Ray3D Point2DToRay3D(this Viewport3DX viewport, System.Windows.Point point2d)
        {
            var r = viewport.Point2DToRay(point2d.ToVector2());

            return(new Ray3D(r.Position.ToPoint3D(), r.Direction.ToVector3D()));
        }
 /// <summary>
 /// Copies the specified viewport to the clipboard.
 /// </summary>
 public static void Copy(this Viewport3DX view)
 {
     Clipboard.SetImage(RenderBitmap(view));
 }
Пример #31
0
 public void AttachViewport3DX(Viewport3DX viewport)
 {
     viewports.Add(viewport);
     viewport.RenderTechnique = this.RenderTechnique;
     viewport.EffectsManager  = this.EffectsManager;
 }
 public static Matrix3D GetScreenViewProjectionMatrix3D(this Viewport3DX viewport)
 {
     return(viewport.GetScreenViewProjectionMatrix().ToMatrix3D());
 }
Пример #33
0
 public Mouse3DEventArgs(RoutedEvent routedEvent, object source, HitTestResult hitTestResult, Point position, Viewport3DX viewport = null)
     : base(routedEvent, source)
 {
     this.HitTestResult = hitTestResult;
     this.Position      = position;
     this.Viewport      = viewport;
 }
Пример #34
0
 /// <summary>
 /// Gets the camera transform.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <returns>
 /// The camera transform.
 /// </returns>
 public static Matrix GetViewProjectionMatrix(this Viewport3DX viewport)
 {
     return(viewport.Camera.GetViewProjectionMatrix(viewport.ActualWidth / viewport.ActualHeight));
 }
Пример #35
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZoomHandler"/> class.
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <param name="changeFieldOfView">
 /// if set to <c>true</c> [change field of view].
 /// </param>
 public ZoomHandler(Viewport3DX viewport, bool changeFieldOfView = false)
     : base(viewport)
 {
     this.changeFieldOfView = changeFieldOfView;
 }
Пример #36
0
 /// <summary>
 /// Gets the total transform for a Viewport3DX.
 /// Old name of this function: GetTotalTransform
 /// New name of the function: GetScreenViewProjectionTransform
 /// </summary>
 /// <param name="viewport">The viewport.</param>
 /// <returns>The total transform.</returns>
 public static Matrix GetScreenViewProjectionMatrix(this Viewport3DX viewport)
 {
     return(GetViewProjectionMatrix(viewport) * GetViewportMatrix(viewport));
 }
        public override void OnMouse3DDown(object sender, RoutedEventArgs e)
        {
            base.OnMouse3DDown(sender, e);

            var args = e as Mouse3DEventArgs;
            if (args == null) return;
            if (args.Viewport == null) return;

            this.isCaptured = true;
            this.viewport = args.Viewport;
            this.camera = args.Viewport.Camera;
            this.lastHitPos = args.HitTestResult.PointHit;
        }
Пример #38
0
        /// <summary>
        /// Zooms to fit the extents of the specified viewport.
        /// </summary>
        /// <param name="camera">
        /// The actual camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="animationTime">
        /// The animation time.
        /// </param>
        public static void ZoomExtents(
            this Camera camera, Viewport3DX viewport, double animationTime = 0)
        {
            var bounds = viewport.FindBounds();
            var diagonal = new Vector3D(bounds.SizeX, bounds.SizeY, bounds.SizeZ);

            if (bounds.IsEmpty || diagonal.LengthSquared.Equals(0))
            {
                return;
            }

            ZoomExtents(camera as ProjectionCamera, viewport, bounds, animationTime);
        }