예제 #1
0
 void setCamera(camera c, Rhino.Display.RhinoViewport vp)
 {
     if (c.parallel)
     {
         vp.ChangeToParallelProjection(true);
     }
     else
     {
         vp.ChangeToPerspectiveProjection(false, c.lens);
     }
     vp.SetCameraLocations(c.target, c.location);
     vp.CameraUp             = c.up;
     vp.Camera35mmLensLength = c.lens;
     vp.Size        = c.size;
     vp.DisplayMode = c.displayMode;
 }
예제 #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <Plane> camPlanes = new List <Plane>();

            DA.GetDataList <Plane>(0, camPlanes);

            string folder = string.Empty;

            DA.GetData <string>(1, ref folder);
            folder = Path.GetFullPath(folder);
            if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                folder += Path.DirectorySeparatorChar;
            }

            string prefix = string.Empty;

            DA.GetData <string>(2, ref prefix);

            int imageWidth = 0;

            DA.GetData <int>(3, ref imageWidth);
            imageWidth = imageWidth / 4;
            Size size = new Size(imageWidth, imageWidth);

            string displayMode = string.Empty;

            DA.GetData <string>(4, ref displayMode);

            bool run = false;

            DA.GetData <bool>(5, ref run);

            int pad = camPlanes.Count.ToString().Length;

            List <string> cubemaps = new List <string>();

            ///Save the intial camera
            saveCam = camFromVP(Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport);

            ///Set the display mode to be used for bitmaps
            DisplayModeDescription viewMode = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.DisplayMode;

            if (DisplayModeDescription.FindByName(displayMode) != null)
            {
                viewMode = DisplayModeDescription.FindByName(displayMode);
            }

            Message = viewMode.EnglishName;

            if (run)
            {
                for (int i = 0; i < camPlanes.Count; i++)
                {
                    ///Setup camera
                    Rhino.Display.RhinoView     view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView;
                    Rhino.Display.RhinoViewport vp   = view.ActiveViewport;

                    ///Get the bounding box of all visible object in the doc for use in setting up the camera
                    ///target so that the far frustrum plane doesn't clip anything
                    double zoomDistance = Rhino.RhinoDoc.ActiveDoc.Objects.BoundingBoxVisible.Diagonal.Length;

                    Plane    camPlane = camPlanes[i];
                    Point3d  camPoint = camPlane.Origin;
                    Vector3d camDir   = camPlane.YAxis;
                    Point3d  tarPoint = Transform.Translation(camDir * zoomDistance / 2) * camPoint;


                    vp.ChangeToPerspectiveProjection(false, 12.0);
                    //vp.Size = size;
                    vp.DisplayMode = viewMode;
                    //view.Redraw();


                    ///Set up final bitmap
                    Bitmap cubemap = new Bitmap(imageWidth * 4, imageWidth * 3);

                    ///Place the images on cubemap bitmap
                    using (Graphics gr = Graphics.FromImage(cubemap))
                    {
                        ///Grab bitmap

                        ///Set up camera directions
                        Point3d        tarLeft    = Transform.Translation(-camPlane.XAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarFront   = Transform.Translation(camPlane.YAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarRight   = Transform.Translation(camPlane.XAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarBack    = Transform.Translation(-camPlane.YAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarUp      = Transform.Translation(camPlane.ZAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarDown    = Transform.Translation(-camPlane.ZAxis * zoomDistance / 2) * camPoint;
                        List <Point3d> camTargets = new List <Point3d>()
                        {
                            tarLeft, tarFront, tarRight, tarBack, tarUp, tarDown
                        };

                        ///Loop through pano directions
                        int insertLoc = 0;
                        for (int d = 0; d < 4; d++)
                        {
                            ///Set camera direction
                            vp.SetCameraLocations(camTargets[d], camPoint);

                            ///Redraw
                            //view.Redraw();

                            gr.DrawImage(view.CaptureToBitmap(size, viewMode), insertLoc, imageWidth);

                            insertLoc = insertLoc + imageWidth;
                        }

                        ///Get up and down views
                        ///Get up view
                        vp.SetCameraLocations(tarUp, camPoint);
                        ///Redraw
                        view.Redraw();
                        var bmTop = view.CaptureToBitmap(size, viewMode);
                        bmTop.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        gr.DrawImage(bmTop, imageWidth, 0);

                        ///Get down view
                        vp.SetCameraLocations(tarDown, camPoint);

                        ///Redraw
                        view.Redraw();
                        var bmBottom = view.CaptureToBitmap(size, viewMode);
                        gr.DrawImage(view.CaptureToBitmap(size, viewMode), imageWidth, imageWidth * 2);
                    }
                    ///End cubemap construction loop

                    ///Save cubemap bitmap
                    string s        = i.ToString().PadLeft(pad, '0');
                    string saveText = folder + prefix + "_" + s + ".png";
                    cubemap.Save(saveText, System.Drawing.Imaging.ImageFormat.Png);
                    cubemaps.Add(saveText);
                    cubemap.Dispose();
                }
            }

            ///Restore initial camera
            setCamera(saveCam, Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport);
            Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.Redraw();

            DA.SetDataList(0, cubemaps);
        }
예제 #3
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <Plane> camPlanes = new List <Plane>();

            DA.GetDataList <Plane>(0, camPlanes);

            string folder = string.Empty;

            DA.GetData <string>(1, ref folder);
            bool saveCubemaps = !string.IsNullOrEmpty(folder);

            if (saveCubemaps)
            {
                folder = Path.GetFullPath(folder);
                if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    folder += Path.DirectorySeparatorChar;
                }
            }

            string prefix = string.Empty;

            DA.GetData <string>(2, ref prefix);

            int imageWidth = 0;

            DA.GetData <int>(3, ref imageWidth);
            imageWidth = imageWidth / 4;
            Size size = new Size(imageWidth, imageWidth);

            string displayMode = string.Empty;

            DA.GetData <string>(4, ref displayMode);

            List <Color> colors = new List <Color>();

            DA.GetDataList <Color>(5, colors);
            bool filterColors = colors.Any();

            GH_Structure <GH_Mesh> ghObstacles = new GH_Structure <GH_Mesh>();

            DA.GetDataTree <GH_Mesh>(6, out ghObstacles);

            ///Flatten obstacle meshes and join them into one mesh
            ghObstacles.FlattenData();
            Mesh obstacles = new Mesh();
            bool showRays  = false;

            if (ghObstacles.DataCount > 0)
            {
                showRays = true;
                foreach (var obstacle in ghObstacles)
                {
                    Mesh temp = new Mesh();
                    GH_Convert.ToMesh(obstacle, ref temp, GH_Conversion.Primary);
                    obstacles.Append(temp);
                }
            }


            bool run = false;

            DA.GetData <bool>(7, ref run);

            int pad = camPlanes.Count.ToString().Length;

            List <string> cubemaps = new List <string>();

            GH_Structure <GH_Line>   rayTree   = new GH_Structure <GH_Line>();
            GH_Structure <GH_Colour> colorTree = new GH_Structure <GH_Colour>();

            ///Save the intial camera
            saveCam = camFromVP(Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport);

            ///Set the display mode to be used for bitmaps
            ///TODO: Add menu item to use "Heron View Analysis" display mode
            DisplayModeDescription viewMode = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.DisplayMode;

            if (DisplayModeDescription.FindByName(displayMode) != null)
            {
                viewMode = DisplayModeDescription.FindByName(displayMode);
            }

            Message = viewMode.EnglishName;

            if (run)
            {
                for (int i = 0; i < camPlanes.Count; i++)
                {
                    ///TODO: setup ability to save cameras to the Rhino doc
                    ///Setup camera
                    Rhino.Display.RhinoView     view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView;
                    Rhino.Display.RhinoViewport vp   = view.ActiveViewport;

                    ///Get the bounding box of all visible object in the doc for use in setting up the camera
                    ///target so that the far frustrum plane doesn't clip anything
                    double zoomDistance = Rhino.RhinoDoc.ActiveDoc.Objects.BoundingBoxVisible.Diagonal.Length;

                    Plane    camPlane = camPlanes[i];
                    Point3d  camPoint = camPlane.Origin;
                    Vector3d camDir   = camPlane.YAxis;
                    Point3d  tarPoint = Transform.Translation(camDir * zoomDistance / 2) * camPoint;


                    vp.ChangeToPerspectiveProjection(false, 12.0);
                    vp.Size        = size;
                    vp.DisplayMode = viewMode;
                    //view.Redraw();

                    ///Set up final bitmap
                    Bitmap cubemap = new Bitmap(imageWidth * 4, imageWidth * 3);

                    ///Place the images on cubemap bitmap
                    using (Graphics gr = Graphics.FromImage(cubemap))
                    {
                        ///Grab bitmap

                        ///Set up camera directions
                        Point3d        tarLeft    = Transform.Translation(-camPlane.XAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarFront   = Transform.Translation(camPlane.YAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarRight   = Transform.Translation(camPlane.XAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarBack    = Transform.Translation(-camPlane.YAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarUp      = Transform.Translation(camPlane.ZAxis * zoomDistance / 2) * camPoint;
                        Point3d        tarDown    = Transform.Translation(-camPlane.ZAxis * zoomDistance / 2) * camPoint;
                        List <Point3d> camTargets = new List <Point3d>()
                        {
                            tarLeft, tarFront, tarRight, tarBack, tarUp, tarDown
                        };

                        ///Loop through pano directions
                        int insertLoc = 0;
                        for (int d = 0; d < 4; d++)
                        {
                            ///Set camera direction
                            vp.SetCameraLocations(camTargets[d], camPoint);
                            //view.Redraw();

                            Bitmap bitmap = new Bitmap(view.CaptureToBitmap(size, viewMode));

                            if (saveCubemaps)
                            {
                                gr.DrawImage(bitmap, insertLoc, imageWidth);
                            }

                            if (showRays)
                            {
                                GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmap);
                                Color           col     = Color.Transparent;
                                for (int x = 0; x < bitmap.Width; x++)
                                {
                                    for (int y = 0; y < bitmap.Height; y++)
                                    {
                                        if (sampler.Sample(x, y, ref col))
                                        {
                                            if (colors.Contains(col))
                                            {
                                                GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                                Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                                Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                                double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                                Point3d rayIntersection = ray.PointAt(rayEnd);
                                                Line    ln = new Line(camPoint, rayIntersection);

                                                if (ln.IsValid & rayEnd > 0)
                                                {
                                                    rayTree.Append(new GH_Line(ln), path);
                                                    colorTree.Append(new GH_Colour(col), path);
                                                }
                                            }
                                            else if (!filterColors)
                                            {
                                                colors.Add(col);
                                                GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                                Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                                Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                                double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                                Point3d rayIntersection = ray.PointAt(rayEnd);
                                                Line    ln = new Line(camPoint, rayIntersection);

                                                if (ln.IsValid & rayEnd > 0)
                                                {
                                                    rayTree.Append(new GH_Line(ln), path);
                                                    colorTree.Append(new GH_Colour(col), path);
                                                }
                                            }
                                        }
                                    }
                                }
                                sampler.Release(false);
                            }

                            insertLoc = insertLoc + imageWidth;

                            bitmap.Dispose();
                        }


                        ///Get up and down views

                        ///Get up view
                        vp.SetCameraLocations(tarUp, camPoint);
                        view.Redraw();

                        Bitmap bitmapUp = new Bitmap(view.CaptureToBitmap(size, viewMode));

                        if (showRays)
                        {
                            GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmapUp);
                            Color           col     = Color.Transparent;
                            for (int x = 0; x < bitmapUp.Width; x++)
                            {
                                for (int y = 0; y < bitmapUp.Height; y++)
                                {
                                    if (sampler.Sample(x, y, ref col))
                                    {
                                        if (colors.Contains(col))
                                        {
                                            GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                            Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                            Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                            double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                            Point3d rayIntersection = ray.PointAt(rayEnd);
                                            Line    ln = new Line(camPoint, rayIntersection);

                                            if (ln.IsValid & rayEnd > 0)
                                            {
                                                rayTree.Append(new GH_Line(ln), path);
                                                colorTree.Append(new GH_Colour(col), path);
                                            }
                                        }
                                        else if (!filterColors)
                                        {
                                            colors.Add(col);
                                            GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                            Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                            Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                            double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                            Point3d rayIntersection = ray.PointAt(rayEnd);
                                            Line    ln = new Line(camPoint, rayIntersection);

                                            if (ln.IsValid & rayEnd > 0)
                                            {
                                                rayTree.Append(new GH_Line(ln), path);
                                                colorTree.Append(new GH_Colour(col), path);
                                            }
                                        }
                                    }
                                }
                            }
                            sampler.Release(false);
                        }

                        bitmapUp.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        if (saveCubemaps)
                        {
                            gr.DrawImage(bitmapUp, imageWidth, 0);
                        }

                        bitmapUp.Dispose();


                        ///Get down view
                        vp.SetCameraLocations(tarDown, camPoint);
                        view.Redraw();

                        Bitmap bitmapDown = new Bitmap(view.CaptureToBitmap(size, viewMode));

                        if (saveCubemaps)
                        {
                            gr.DrawImage(bitmapDown, imageWidth, imageWidth * 2);
                        }

                        if (showRays)
                        {
                            GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmapDown);
                            Color           col     = Color.Transparent;
                            for (int x = 0; x < bitmapDown.Width; x++)
                            {
                                for (int y = 0; y < bitmapDown.Height; y++)
                                {
                                    if (sampler.Sample(x, y, ref col))
                                    {
                                        if (colors.Contains(col))
                                        {
                                            GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                            Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                            Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                            double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                            Point3d rayIntersection = ray.PointAt(rayEnd);
                                            Line    ln = new Line(camPoint, rayIntersection);

                                            if (ln.IsValid & rayEnd > 0)
                                            {
                                                rayTree.Append(new GH_Line(ln), path);
                                                colorTree.Append(new GH_Colour(col), path);
                                            }
                                        }

                                        else if (!filterColors)
                                        {
                                            colors.Add(col);
                                            GH_Path path = new GH_Path(i, colors.IndexOf(col));

                                            Line line = vp.ClientToWorld(new System.Drawing.Point(x, y));

                                            Ray3d   ray             = new Ray3d(vp.CameraLocation, -line.Direction);
                                            double  rayEnd          = (double)Rhino.Geometry.Intersect.Intersection.MeshRay(obstacles, ray);
                                            Point3d rayIntersection = ray.PointAt(rayEnd);
                                            Line    ln = new Line(camPoint, rayIntersection);

                                            if (ln.IsValid & rayEnd > 0)
                                            {
                                                rayTree.Append(new GH_Line(ln), path);
                                                colorTree.Append(new GH_Colour(col), path);
                                            }
                                        }
                                    }
                                }
                            }
                            sampler.Release(false);
                        }

                        bitmapDown.Dispose();
                    }
                    ///End pano directions loop

                    if (saveCubemaps)
                    {
                        ///Save cubemap bitmap
                        string s        = i.ToString().PadLeft(pad, '0');
                        string saveText = folder + prefix + "_" + s + ".png";
                        cubemap.Save(saveText, System.Drawing.Imaging.ImageFormat.Png);
                        cubemaps.Add(saveText);
                    }
                    cubemap.Dispose();
                }
            }

            ///Restore initial camera
            setCamera(saveCam, Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport);
            Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.Redraw();

            DA.SetDataList(0, cubemaps);
            DA.SetDataTree(1, rayTree);
            DA.SetDataTree(2, colorTree);
        }
예제 #4
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            bool    run      = false;
            Point3d location = new Point3d();
            Point3d target   = new Point3d();
            double  length   = 50;
            double  bias     = 0;
            int     type     = 0;
            Point3d uv       = new Point3d();

            Rhino.Display.RhinoViewport viewPort = null;

            DA.GetData("Run", ref run);
            DA.GetData("CameraLocation", ref location);
            DA.GetData("CameraTarget", ref target);
            DA.GetData("FocalLength", ref length);
            DA.GetData("Bias", ref bias);
            DA.GetData("CameraType", ref type);
            DA.GetData("UV", ref uv);

            string name     = "";
            bool   flag     = true;
            bool   useFrame = false;

            if (DA.GetData("ViewName", ref name))
            {
                foreach (RhinoView view in Rhino.RhinoDoc.ActiveDoc.Views)
                {
                    if (view.ActiveViewport.Name == name)
                    {
                        viewPort = view.ActiveViewport;
                        flag     = false;
                        break;
                    }
                }
            }

            if (flag)
            {
                try
                {
                    viewPort = Frame.Component.RhinoView.ActiveViewport;
                    useFrame = true;
                }
                catch
                {
                    viewPort = RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport;
                }
            }
            Message = viewPort.Name;
            //viewPort.DisplayMode.DisplayAttributes.FillMode = DisplayPipelineAttributes.FrameBufferFillMode.SolidColor;
            //viewPort.DisplayMode.DisplayAttributes.SetFill()

            if (run)
            {
                //basic settings
                viewPort.SetCameraLocations(target, location);
                viewPort.CameraUp = Vector3d.ZAxis;

                switch (type)
                {
                case 0:
                    viewPort.ChangeToParallelProjection(true);
                    break;

                case 1:
                    viewPort.ChangeToTwoPointPerspectiveProjection(length);
                    break;

                case 2:
                    viewPort.ChangeToPerspectiveProjection(true, length);
                    break;

                default:
                    this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input Type must be in 0-2!");
                    viewPort.ChangeToPerspectiveProjection(true, length);
                    break;
                }

                if (((Param_Number)Params.Input[5]).UseDegrees)
                {
                    bias = RhinoMath.ToRadians(bias);
                }
                Vector3d z = Vector3d.ZAxis;
                z.Rotate(bias, viewPort.CameraZ);
                viewPort.CameraUp = z;

                Point3d[] nearCorners = viewPort.GetNearRect();
                Point3d[] farCorners  = viewPort.GetFarRect();


                Plane  targetPl = new Plane(target, viewPort.CameraX, viewPort.CameraY);
                double param;
                Line   ln1 = new Line(nearCorners[0], farCorners[0]);
                Line   ln2 = new Line(nearCorners[3], farCorners[3]);
                Intersection.LinePlane(ln1, targetPl, out param);
                Rectangle3d rectTarget = new Rectangle3d(targetPl, ln1.PointAt(param), ln2.PointAt(param));
                Point3d     X          = rectTarget.PointAt(uv.X, uv.Y);

                viewPort.SetCameraLocations(NewTarget(location, target, X), location);

                nearCorners = viewPort.GetNearRect();
                farCorners  = viewPort.GetFarRect();
                Point3d[] corners = new Point3d[4]
                {
                    (nearCorners[0] * 0.99 + farCorners[0] * 0.01),
                    (nearCorners[1] * 0.99 + farCorners[1] * 0.01),
                    (nearCorners[2] * 0.99 + farCorners[2] * 0.01),
                    (nearCorners[3] * 0.99 + farCorners[3] * 0.01)
                };

                Plane       recPlane = new Plane(corners[0], corners[1], corners[2]);
                Rectangle3d rect     = new Rectangle3d(recPlane, corners[0], corners[3]);


                double viewRectWidth = corners[0].DistanceTo(corners[1]);
                double unitPerPx;
                if (useFrame)
                {
                    unitPerPx = viewRectWidth / (Frame.Component.Play ? Frame.Component.PictWidth : viewPort.Size.Width);
                    Message  += "\n" + (Frame.Component.Play ? "Product" : "Preview");
                }
                else
                {
                    unitPerPx = viewRectWidth / viewPort.Size.Width;
                }



                DA.SetData("Rectangle", rect);
                DA.SetData("UnitPerPx", unitPerPx);
            }
        }