简体   繁体   中英

How to zoom to a model in a helix-toolkit sharpdx viewport?

Following setup: I have an application with two viewports. When choosing an element on the main viewport, a copy of the model geometry is supposed to be displayed enlarged in the second viewport.

I uploaded a screenshot for some visual aid :-) http://imgur.com/ILG8Ylk

My take on this (among others) is to use the ZoomExtents method:

MainViewModel.cs

    public void SetDetailedModelGeometry(MeshGeometryModel3D geometry)
    {

        MyDetailedModel = new Element3DCollection();
        MyModelViewportDetailed.Reset();

        if (geometry != null)
        {
            var bounds = geometry.Bounds;

            //var rec = new Rect3D(bounds.Minimum.X, bounds.Minimum.Y, bounds.Minimum.Z, bounds.Maximum.X,bounds.Maximum.Y,bounds.Maximum.Z);

            this.MyDetailedModel.Add(geometry);
            MyDetailedModel[0].Attach(MyModelViewportDetailed.RenderHost);

            this.OnPropertyChanged("MyDetailedModel");

            MyModelViewportDetailed.ZoomExtents();
            //MyModelViewportDetailed.ZoomExtents(rec);

        }

    }

I guess both overloads should fit the scene nicely to the bound model, but I always end up with a very zoomed out view and most of the models are not centered in the viewport.

I tried to hack a solution by translating the model into the origin and by applying a zoom-force, the result is better but still kind of meh.

    public void SetDetailedModelGeometry(MeshGeometryModel3D geometry)
    {

        MyDetailedModel = new Element3DCollection();
        MyModelViewportDetailed.Reset();

        if (geometry != null)
        {

            var bounds = geometry.Bounds;
            var rec = new Rect3D( bounds.Minimum.X, bounds.Minimum.Y, bounds.Minimum.Z,  bounds.Maximum.X, bounds.Maximum.Y, bounds.Maximum.Z);
            // move model 

            var delta = new Point3D(Math.Abs(bounds.Maximum.X - bounds.Minimum.X), Math.Abs(bounds.Maximum.Y - bounds.Minimum.Y), Math.Abs(bounds.Maximum.Z - bounds.Minimum.Z));
            var center = new Point3D(bounds.Minimum.X+delta.X, bounds.Minimum.Y+delta.Y, bounds.Minimum.Z+delta.Z);

            TransformDetailed = new TranslateTransform3D(-center.X,-center.Y,-center.Z);
            geometry.Transform = TransformDetailed;

            // notice property change
            this.MyDetailedModel.Add(geometry);
            MyDetailedModel[0].Attach(MyModelViewportDetailed.RenderHost);

            this.OnPropertyChanged("MyDetailedModel");

            MyModelViewportDetailed.ZoomExtents();
            MyModelViewportDetailed.CameraController.AddZoomForce(-1.0);


        }

Does somebody have a clue why the zoomExtends() method is not working for me? And if not, maybe has suggestions for a more sophisticated hack xD.

I'm quite the junior developer, so I'm open for any comments for improvements, thanks.

You could try a few things. First make sure IsZoomEnabled and ZoomExtentsWhenLoaded on your viewport are set to true. You can calculate the Bounds by yourselft, for alot off MeshModels this would be a bootleneck but for just on Geometry it will be sufficient.

            var mx= (this.ModelGeometry[0] as MeshGeometryModel3D).Geometry.Positions.Min(item => item.X);
            var my = (this.ModelGeometry[0] as MeshGeometryModel3D).Geometry.Positions.Min(item => item.Y);
            var mz = (this.ModelGeometry[0] as MeshGeometryModel3D).Geometry.Positions.Min(item => item.Z);
            BoundingBox bounds = new BoundingBox(new Vector3(mx, my, mz), new Vector3(x, y, z));

Another hint, I had a small timing problem in my application, I was loading and attaching step files with 10000++ MeshModels and before calling ZoomExtends I had to wait some 100 ms. You could do this via a timer or task delay.

I see you are using the PerspectiveCamera. Via OrthotraphicCamera you could set the Zoom yourself by setting the width property of the camera.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM