views:

818

answers:

1

I have a cuboid who's dimensions are imported from XML so i need to be sure that no matter what the size of the model, the camera can always see all of it. This is for preview purposes. I'll likely render a caption over the top showing the scale for clarity.

I think i need some function which will tell me whether the ModelVisual3D fits inside the bounds of the FieldOfView of the camera or maybe the ViewPort3D itself.

So far i have the static callback (the dimension properties are DependencyPropertys) when dimensions properties change define as below. It's pretty crude at the moment but you get the idea I'm looking at. The commented out section shows roughly what kind of logic I'm looking for

    private static void OnCubeDimensionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!valueSemaphore)
        {
            //while(mainModel.WillClip(mainCamera))
            //{
            //     mainCamera.FieldOfView--;
            //}

            valueSemaphore = true;
            double propertyValue = 0.0;
            Product3D p = d as Product3D;

            switch (e.Property.Name)
            {
                case "CubeHeight":
                    propertyValue = (double.Parse(e.NewValue.ToString()) / 100) * 8;
                    p.CubeHeight = propertyValue;
                    break;
                case "CubeWidth":
                    propertyValue = (double.Parse(e.NewValue.ToString()) / 100) * 5.3;
                    p.CubeWidth = propertyValue;
                    break;
                case "CubeDepth":
                    propertyValue = (double.Parse(e.NewValue.ToString()) / 100) * 2.6;
                    p.CubeDepth = propertyValue;
                    break;
            }
            valueSemaphore = false;
        }
    }

If anyone knows what should go where the commented-out section is I'd be eternally grateful.
Thanks in advance.

+2  A: 

What you need to get hold of is the extent of the 2D projection of the cube on the view plane. You can then do a simple check on the min and max X & Y values to see whether the whole of the cube is visible.

Adding a tolerance factor to the extent will take care of any rounding errors.

I've copied what looks to be the relevant section of code from here. You can then set the size of the viewport accordingly.

public static Rect Get2DBoundingBox(ModelVisual3D mv3d)
{
    bool bOK;

    Matrix3D m = MathUtils.TryWorldToViewportTransform(vpv, out bOK);

    bool bFirst = true;    
    Rect r = new Rect();

    if (mv3d.Content is GeometryModel3D)
    {
        GeometryModel3D gm3d = (GeometryModel3D) mv3d.Content;

        if (gm3d.Geometry is MeshGeometry3D)
        {
            MeshGeometry3D mg3d = (MeshGeometry3D)gm3d.Geometry;

            foreach (Point3D p3d in mg3d.Positions)
            {
                Point3D pb = m.Transform(p3d);
                Point p2d = new Point(pb.X, pb.Y);
                if (bFirst)
                {
                    r = new Rect(p2d, new Size(1, 1));
                    bFirst = false;
                }
                else
                {
                    r.Union(p2d);
                }
            }
        }
    }
ChrisF