public static Rhino.Commands.Result AddBackgroundBitmap(Rhino.RhinoDoc doc)
    {
        Rhino.RhinoApp.WriteLine ("hey");
        // Allow the user to select a bitmap file
        Rhino.UI.OpenFileDialog fd = new Rhino.UI.OpenFileDialog();
        fd.Filter = "Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg";
        if (!fd.ShowDialog())
          return Rhino.Commands.Result.Cancel;

        // Verify the file that was selected
        System.Drawing.Image image;
        try
        {
          image = System.Drawing.Image.FromFile(fd.FileName);
        }
        catch (Exception)
        {
          return Rhino.Commands.Result.Failure;
        }

        // Allow the user to pick the bitmap origin
        Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint();
        gp.SetCommandPrompt("Bitmap Origin");
        gp.ConstrainToConstructionPlane(true);
        gp.Get();
        if (gp.CommandResult() != Rhino.Commands.Result.Success)
          return gp.CommandResult();

        // Get the view that the point was picked in.
        // This will be the view that the bitmap appears in.
        Rhino.Display.RhinoView view = gp.View();
        if (view == null)
        {
          view = doc.Views.ActiveView;
          if (view == null)
        return Rhino.Commands.Result.Failure;
        }

        // Allow the user to specify the bitmap with in model units
        Rhino.Input.Custom.GetNumber gn = new Rhino.Input.Custom.GetNumber();
        gn.SetCommandPrompt("Bitmap width");
        gn.SetLowerLimit(1.0, false);
        gn.Get();
        if (gn.CommandResult() != Rhino.Commands.Result.Success)
          return gn.CommandResult();

        // Cook up some scale factors
        double w = gn.Number();
        double image_width = image.Width;
        double image_height = image.Height;
        double h = w * (image_height / image_width);

        Rhino.Geometry.Plane plane = view.ActiveViewport.ConstructionPlane();
        plane.Origin = gp.Point();
        view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h, false, false);
        view.Redraw();
        return Rhino.Commands.Result.Success;
    }
    public static Rhino.Commands.Result AddBackgroundBitmap(Rhino.RhinoDoc doc)
    {
        // Allow the user to select a bitmap file
        var fd = new Rhino.UI.OpenFileDialog {
            Filter = "Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg"
        };

        if (!fd.ShowOpenDialog())
        {
            return(Rhino.Commands.Result.Cancel);
        }

        // Verify the file that was selected
        System.Drawing.Image image;
        try
        {
            image = System.Drawing.Image.FromFile(fd.FileName);
        }
        catch (Exception)
        {
            return(Rhino.Commands.Result.Failure);
        }

        // Allow the user to pick the bitmap origin
        var gp = new Rhino.Input.Custom.GetPoint();

        gp.SetCommandPrompt("Bitmap Origin");
        gp.ConstrainToConstructionPlane(true);
        gp.Get();
        if (gp.CommandResult() != Rhino.Commands.Result.Success)
        {
            return(gp.CommandResult());
        }

        // Get the view that the point was picked in.
        // This will be the view that the bitmap appears in.
        var view = gp.View();

        if (view == null)
        {
            view = doc.Views.ActiveView;
            if (view == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
        }

        // Allow the user to specify the bitmap width in model units
        var gn = new Rhino.Input.Custom.GetNumber();

        gn.SetCommandPrompt("Bitmap width");
        gn.SetLowerLimit(1.0, false);
        gn.Get();
        if (gn.CommandResult() != Rhino.Commands.Result.Success)
        {
            return(gn.CommandResult());
        }

        // Cook up some scale factors
        var w            = gn.Number();
        var image_width  = image.Width;
        var image_height = image.Height;
        var h            = w * (image_height / image_width);

        var plane = view.ActiveViewport.ConstructionPlane();

        plane.Origin = gp.Point();
        view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h, false, false);
        view.Redraw();
        return(Rhino.Commands.Result.Success);
    }
예제 #3
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: start here modifying the behaviour of your command.
            // ---
            RhinoApp.WriteLine("MeshMachine WIP test", EnglishName);

            Brep SB;

            using (GetObject getBrep = new GetObject())
            {
                getBrep.SetCommandPrompt("Please select the brep to remesh");
                getBrep.Get();
                SB = getBrep.Object(0).Brep();
            }

            //GetNumber TargetLength = new GetNumber();
            //double L = TargetLength.Number();

            Rhino.Input.Custom.GetNumber gn = new Rhino.Input.Custom.GetNumber();
            gn.SetCommandPrompt("Specify a Target Edge Length");
            gn.SetLowerLimit(0.5, false);
            gn.Get();
            if (gn.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gn.CommandResult());
            }
            double L = gn.Number();

            //Point3d pt0;
            //using (GetPoint getPointAction = new GetPoint())
            //{
            //    getPointAction.SetCommandPrompt("Please select the start point");
            //    if (getPointAction.Get() != GetResult.Point)
            //    {
            //        RhinoApp.WriteLine("No start point was selected.");
            //        return getPointAction.CommandResult();
            //    }
            //    pt0 = getPointAction.Point();
            //}

            //Point3d pt1;
            //using (GetPoint getPointAction = new GetPoint())
            //{
            //    getPointAction.SetCommandPrompt("Please select the end point");
            //    getPointAction.SetBasePoint(pt0, true);
            //    getPointAction.DynamicDraw +=
            //      (sender, e) => e.Display.DrawLine(pt0, e.CurrentPoint, System.Drawing.Color.DarkRed);
            //    if (getPointAction.Get() != GetResult.Point)
            //    {
            //        RhinoApp.WriteLine("No end point was selected.");
            //        return getPointAction.CommandResult();
            //    }
            //    pt1 = getPointAction.Point();
            //}

            //doc.Objects.AddLine(pt0, pt1);

            PlanktonMesh P = new PlanktonMesh();

            List <int> AnchorV        = new List <int>();
            List <int> FeatureV       = new List <int>();
            List <int> FeatureE       = new List <int>();
            double     FixT           = 0.00001;
            double     LengthTol      = 0.15; //a tolerance for when to split/collapse edges
            double     SmoothStrength = 0.8;  //smoothing strength
            double     PullStrength   = 0.8;  //pull to target mesh strength
            double     CurvDep        = 0;
            int        Flip           = 1;

            MeshingParameters MeshParams = new MeshingParameters();

            MeshParams.MaximumEdgeLength = 3 * L;
            MeshParams.MinimumEdgeLength = L;
            MeshParams.JaggedSeams       = false;
            MeshParams.SimplePlanes      = false;

            Mesh[] BrepMeshes = Mesh.CreateFromBrep(SB, MeshParams);
            Mesh   M          = new Mesh();

            foreach (var mesh in BrepMeshes)
            {
                M.Append(mesh);
            }

            M.Faces.ConvertQuadsToTriangles();
            P = M.ToPlanktonMesh();

            var FC = new List <Curve>();

            foreach (BrepEdge E in SB.Edges)
            {
                if (!E.IsSmoothManifoldEdge(0.01))
                {
                    FC.Add(E.ToNurbsCurve());
                }
            }
            var            Corners = SB.Vertices;
            List <Point3d> FV      = new List <Point3d>();

            foreach (Point Pt in Corners)
            {
                FV.Add(Pt.Location);
            }

            //Mark any vertices or edges lying on features
            for (int i = 0; i < P.Vertices.Count; i++)
            {
                Point3d Pt = P.Vertices[i].ToPoint3d();
                AnchorV.Add(-1);
                for (int j = 0; j < FV.Count; j++)
                {
                    if (Pt.DistanceTo(FV[j]) < FixT)
                    {
                        AnchorV[AnchorV.Count - 1] = j;
                    }
                }

                FeatureV.Add(-1);
                for (int j = 0; j < FC.Count; j++)
                {
                    double param = new double();
                    FC[j].ClosestPoint(Pt, out param);
                    if (Pt.DistanceTo(FC[j].PointAt(param)) < FixT)
                    {
                        FeatureV[FeatureV.Count - 1] = j;
                    }
                }
            }

            int EdgeCount = P.Halfedges.Count / 2;

            for (int i = 0; i < EdgeCount; i++)
            {
                FeatureE.Add(-1);
                int vStart = P.Halfedges[2 * i].StartVertex;
                int vEnd   = P.Halfedges[2 * i + 1].StartVertex;

                Point3d PStart = P.Vertices[vStart].ToPoint3d();
                Point3d PEnd   = P.Vertices[vEnd].ToPoint3d();

                for (int j = 0; j < FC.Count; j++)
                {
                    double paramS = new double();
                    double paramE = new double();
                    Curve  thisFC = FC[j];
                    thisFC.ClosestPoint(PStart, out paramS);
                    thisFC.ClosestPoint(PEnd, out paramE);
                    if ((PStart.DistanceTo(thisFC.PointAt(paramS)) < FixT) &&
                        (PEnd.DistanceTo(thisFC.PointAt(paramE)) < FixT))
                    {
                        FeatureE[FeatureE.Count - 1] = j;
                    }
                }
            }



            for (int iter = 0; iter < 30; iter++)
            {
                EdgeCount = P.Halfedges.Count / 2;
                double[]    EdgeLength = P.Halfedges.GetLengths();
                List <bool> Visited    = new List <bool>();
                Vector3d[]  Normals    = new Vector3d[P.Vertices.Count];

                for (int i = 0; i < P.Vertices.Count; i++)
                {
                    Visited.Add(false);
                    Normals[i] = Util.Normal(P, i);
                }

                double t      = LengthTol;      //a tolerance for when to split/collapse edges
                double smooth = SmoothStrength; //smoothing strength
                double pull   = PullStrength;   //pull to target mesh strength

                //    Split the edges that are too long
                for (int i = 0; i < EdgeCount; i++)
                {
                    if (P.Halfedges[2 * i].IsUnused == false)
                    {
                        int vStart = P.Halfedges[2 * i].StartVertex;
                        int vEnd   = P.Halfedges[2 * i + 1].StartVertex;

                        if ((Visited[vStart] == false) &&
                            (Visited[vEnd] == false))
                        {
                            double  L2  = L;
                            Point3d Mid = Util.MidPt(P, i);

                            //if (CurvDep > 0)
                            //{
                            //    double NormDiff = Vector3d.VectorAngle(Normals[vStart], Normals[vEnd]);
                            //    L2 = Math.Min((1.0 / (3.0 * NormDiff) * L), 5 * L);

                            //    if (CurvDep != 1)
                            //    {
                            //        L2 = L2 * (CurvDep) + L * (1.0 - CurvDep);
                            //    }

                            //}
                            //if (BoundScale != 1.0)
                            //{
                            //    double MinDist = 99954;

                            //    for (int j = 0; j < FC.Count; j++)
                            //    {
                            //        double param = new double();

                            //        FC[j].ClosestPoint(Mid, out param);
                            //        double ThisDist = Mid.DistanceTo(FC[j].PointAt(param));
                            //        if (ThisDist < MinDist)
                            //        { MinDist = ThisDist; }
                            //    }

                            //    if (MinDist < BoundDist)
                            //    {
                            //        L2 = L2 * BoundScale + (MinDist / BoundDist) * (L2 * (1 - BoundScale));
                            //    }
                            //}

                            //if (SizP.Count > 0)
                            //{
                            //    L2 = WeightedCombo(Mid, SizP, SizV, WExp, L2, BGW);
                            //    //  L2 = (WL * (1.0 - BGW)) + (BGW * L2);
                            //}

                            if (EdgeLength[2 * i] > (1 + t) * (4f / 3f) * L2)
                            {
                                int SplitHEdge = P.Halfedges.TriangleSplitEdge(2 * i);
                                if (SplitHEdge != -1)
                                {
                                    int SplitCenter = P.Halfedges[SplitHEdge].StartVertex;
                                    P.Vertices.SetVertex(SplitCenter, Util.MidPt(P, i));

                                    //update the feature information
                                    FeatureE.Add(FeatureE[i]);
                                    FeatureV.Add(FeatureE[i]);
                                    AnchorV.Add(-1);

                                    //2 additional new edges have also been created (or 1 if split was on a boundary)
                                    //mark these as non-features
                                    int CEdgeCount = P.Halfedges.Count / 2;
                                    while (FeatureE.Count < CEdgeCount)
                                    {
                                        FeatureE.Add(-1);
                                    }

                                    Visited.Add(true);
                                    int[] Neighbours = P.Vertices.GetVertexNeighbours(SplitCenter);
                                    foreach (int n in Neighbours)
                                    {
                                        Visited[n] = true;
                                    }
                                }
                            }
                        }
                    }
                }

                //Collapse the edges that are too short
                for (int i = 0; i < EdgeCount; i++)
                {
                    if (P.Halfedges[2 * i].IsUnused == false)
                    {
                        int vStart = P.Halfedges[2 * i].StartVertex;
                        int vEnd   = P.Halfedges[2 * i + 1].StartVertex;
                        if ((Visited[vStart] == false) &&
                            (Visited[vEnd] == false))
                        {
                            if (!(AnchorV[vStart] != -1 && AnchorV[vEnd] != -1)) // if both ends are anchored, don't collapse
                            {
                                int Collapse_option = 0;                         //0 for none, 1 for collapse to midpt, 2 for towards start, 3 for towards end
                                //if neither are anchorV
                                if (AnchorV[vStart] == -1 && AnchorV[vEnd] == -1)
                                {
                                    // if both on same feature (or neither on a feature)
                                    if (FeatureV[vStart] == FeatureV[vEnd])
                                    {
                                        Collapse_option = 1;
                                    }
                                    // if start is on a feature and end isn't
                                    if ((FeatureV[vStart] != -1) && (FeatureV[vEnd] == -1))
                                    {
                                        Collapse_option = 2;
                                    }
                                    // if end is on a feature and start isn't
                                    if ((FeatureV[vStart] == -1) && (FeatureV[vEnd] != -1))
                                    {
                                        Collapse_option = 3;
                                    }
                                }
                                else // so one end must be an anchor
                                {
                                    // if start is an anchor
                                    if (AnchorV[vStart] != -1)
                                    {
                                        // if both are on same feature, or if the end is not a feature
                                        if ((FeatureE[i] != -1) || (FeatureV[vEnd] == -1))
                                        {
                                            Collapse_option = 2;
                                        }
                                    }
                                    // if end is an anchor
                                    if (AnchorV[vEnd] != -1)
                                    {
                                        // if both are on same feature, or if the start is not a feature
                                        if ((FeatureE[i] != -1) || (FeatureV[vStart] == -1))
                                        {
                                            Collapse_option = 3;
                                        }
                                    }
                                }

                                double  L2  = L;
                                Point3d Mid = Util.MidPt(P, i);

                                if (CurvDep > 0)
                                {
                                    double NormDiff = Vector3d.VectorAngle(Normals[vStart], Normals[vEnd]);
                                    L2 = Math.Min((1.0 / (3.0 * NormDiff) * L), 5 * L);

                                    if (CurvDep != 1)
                                    {
                                        L2 = L2 * (CurvDep) + L * (1.0 - CurvDep);
                                    }
                                }

                                //if (BoundScale != 1.0)
                                //{
                                //    double MinDist = 99954;

                                //    for (int j = 0; j < FC.Count; j++)
                                //    {
                                //        double param = new double();

                                //        FC[j].ClosestPoint(Mid, out param);
                                //        double ThisDist = Mid.DistanceTo(FC[j].PointAt(param));
                                //        if (ThisDist < MinDist)
                                //        { MinDist = ThisDist; }
                                //    }

                                //    if (MinDist < BoundDist)
                                //    {
                                //        L2 = L2 * BoundScale + (MinDist / BoundDist) * (L2 * (1 - BoundScale));
                                //    }
                                //}

                                //if (SizP.Count > 0)
                                //{
                                //    L2 = WeightedCombo(Mid, SizP, SizV, WExp, L2, BGW);
                                //    //double WL = WeightedCombo(Mid, SizP, SizV, WExp);
                                //    //L2 = (WL * (1.0 - BGW)) + (BGW * L2);
                                //}

                                if ((Collapse_option != 0) && (EdgeLength[2 * i] < (1 - t) * 4f / 5f * L2))
                                {
                                    int Collapsed   = -1;
                                    int CollapseRtn = -1;
                                    if (Collapse_option == 1)
                                    {
                                        Collapsed = P.Halfedges[2 * i].StartVertex;
                                        P.Vertices.SetVertex(Collapsed, Util.MidPt(P, i));
                                        CollapseRtn = P.Halfedges.CollapseEdge(2 * i);
                                    }
                                    if (Collapse_option == 2)
                                    {
                                        Collapsed   = P.Halfedges[2 * i].StartVertex;
                                        CollapseRtn = P.Halfedges.CollapseEdge(2 * i);
                                    }
                                    if (Collapse_option == 3)
                                    {
                                        Collapsed   = P.Halfedges[2 * i + 1].StartVertex;
                                        CollapseRtn = P.Halfedges.CollapseEdge(2 * i + 1);
                                    }
                                    if (CollapseRtn != -1)
                                    {
                                        int[] Neighbours = P.Vertices.GetVertexNeighbours(Collapsed);
                                        foreach (int n in Neighbours)
                                        {
                                            Visited[n] = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                EdgeCount = P.Halfedges.Count / 2;

                if ((Flip == 0) && (PullStrength > 0))
                {
                    //Flip edges to reduce valence error
                    for (int i = 0; i < EdgeCount; i++)
                    {
                        if (!P.Halfedges[2 * i].IsUnused &&
                            (P.Halfedges[2 * i].AdjacentFace != -1) &&
                            (P.Halfedges[2 * i + 1].AdjacentFace != -1) &&
                            (FeatureE[i] == -1)   // don't flip feature edges
                            )
                        {
                            int Vert1 = P.Halfedges[2 * i].StartVertex;
                            int Vert2 = P.Halfedges[2 * i + 1].StartVertex;
                            int Vert3 = P.Halfedges[P.Halfedges[P.Halfedges[2 * i].NextHalfedge].NextHalfedge].StartVertex;
                            int Vert4 = P.Halfedges[P.Halfedges[P.Halfedges[2 * i + 1].NextHalfedge].NextHalfedge].StartVertex;

                            int Valence1 = P.Vertices.GetValence(Vert1);
                            int Valence2 = P.Vertices.GetValence(Vert2);
                            int Valence3 = P.Vertices.GetValence(Vert3);
                            int Valence4 = P.Vertices.GetValence(Vert4);

                            if (P.Vertices.NakedEdgeCount(Vert1) > 0)
                            {
                                Valence1 += 2;
                            }
                            if (P.Vertices.NakedEdgeCount(Vert2) > 0)
                            {
                                Valence2 += 2;
                            }
                            if (P.Vertices.NakedEdgeCount(Vert3) > 0)
                            {
                                Valence3 += 2;
                            }
                            if (P.Vertices.NakedEdgeCount(Vert4) > 0)
                            {
                                Valence4 += 2;
                            }

                            int CurrentError =
                                Math.Abs(Valence1 - 6) +
                                Math.Abs(Valence2 - 6) +
                                Math.Abs(Valence3 - 6) +
                                Math.Abs(Valence4 - 6);
                            int FlippedError =
                                Math.Abs(Valence1 - 7) +
                                Math.Abs(Valence2 - 7) +
                                Math.Abs(Valence3 - 5) +
                                Math.Abs(Valence4 - 5);
                            if (CurrentError > FlippedError)
                            {
                                P.Halfedges.FlipEdge(2 * i);
                            }
                        }
                    }
                }
                else
                {
                    //Flip edges based on angle
                    for (int i = 0; i < EdgeCount; i++)
                    {
                        if (!P.Halfedges[2 * i].IsUnused &&
                            (P.Halfedges[2 * i].AdjacentFace != -1) &&
                            (P.Halfedges[2 * i + 1].AdjacentFace != -1) &&
                            (FeatureE[i] == -1)  // don't flip feature edges
                            )
                        {
                            int Vert1 = P.Halfedges[2 * i].StartVertex;
                            int Vert2 = P.Halfedges[2 * i + 1].StartVertex;
                            int Vert3 = P.Halfedges[P.Halfedges[P.Halfedges[2 * i].NextHalfedge].NextHalfedge].StartVertex;
                            int Vert4 = P.Halfedges[P.Halfedges[P.Halfedges[2 * i + 1].NextHalfedge].NextHalfedge].StartVertex;

                            Point3d P1 = P.Vertices[Vert1].ToPoint3d();
                            Point3d P2 = P.Vertices[Vert2].ToPoint3d();
                            Point3d P3 = P.Vertices[Vert3].ToPoint3d();
                            Point3d P4 = P.Vertices[Vert4].ToPoint3d();

                            double A1 = Vector3d.VectorAngle(new Vector3d(P3 - P1), new Vector3d(P4 - P1))
                                        + Vector3d.VectorAngle(new Vector3d(P4 - P2), new Vector3d(P3 - P2));

                            double A2 = Vector3d.VectorAngle(new Vector3d(P1 - P4), new Vector3d(P2 - P4))
                                        + Vector3d.VectorAngle(new Vector3d(P2 - P3), new Vector3d(P1 - P3));

                            if (A2 > A1)
                            {
                                P.Halfedges.FlipEdge(2 * i);
                            }
                        }
                    }
                }

                //if (Minim)
                //{
                //    Vector3d[] SmoothC = LaplacianSmooth(P, 1, smooth);

                //    for (int i = 0; i < P.Vertices.Count; i++)
                //    {
                //        if (AnchorV[i] == -1) // don't smooth feature vertices
                //        {
                //            P.Vertices.MoveVertex(i, 0.5 * SmoothC[i]);
                //        }
                //    }
                //}

                Vector3d[] Smooth = Util.LaplacianSmooth(P, 0, smooth);

                for (int i = 0; i < P.Vertices.Count; i++)
                {
                    if (AnchorV[i] == -1) // don't smooth feature vertices
                    {
                        // make it tangential only
                        Vector3d VNormal    = Util.Normal(P, i);
                        double   ProjLength = Smooth[i] * VNormal;
                        Smooth[i] = Smooth[i] - (VNormal * ProjLength);

                        P.Vertices.MoveVertex(i, Smooth[i]);

                        if (P.Vertices.NakedEdgeCount(i) != 0)//special smoothing for feature edges
                        {
                            int[]   Neighbours = P.Vertices.GetVertexNeighbours(i);
                            int     ncount     = 0;
                            Point3d Avg        = new Point3d();

                            for (int j = 0; j < Neighbours.Length; j++)
                            {
                                if (P.Vertices.NakedEdgeCount(Neighbours[j]) != 0)
                                {
                                    ncount++;
                                    Avg = Avg + P.Vertices[Neighbours[j]].ToPoint3d();
                                }
                            }
                            Avg = Avg * (1.0 / ncount);
                            Vector3d move = Avg - P.Vertices[i].ToPoint3d();
                            move = move * smooth;
                            P.Vertices.MoveVertex(i, move);
                        }

                        if (FeatureV[i] != -1)//special smoothing for feature edges
                        {
                            int[]   Neighbours = P.Vertices.GetVertexNeighbours(i);
                            int     ncount     = 0;
                            Point3d Avg        = new Point3d();

                            for (int j = 0; j < Neighbours.Length; j++)
                            {
                                if ((FeatureV[Neighbours[j]] == FeatureV[i]) || (AnchorV[Neighbours[j]] != -1))
                                {
                                    ncount++;
                                    Avg = Avg + P.Vertices[Neighbours[j]].ToPoint3d();
                                }
                            }
                            Avg = Avg * (1.0 / ncount);
                            Vector3d move = Avg - P.Vertices[i].ToPoint3d();
                            move = move * smooth;
                            P.Vertices.MoveVertex(i, move);
                        }

                        //projecting points onto the target along their normals

                        if (pull > 0)
                        {
                            Point3d  Point  = P.Vertices[i].ToPoint3d();
                            Vector3d normal = Util.Normal(P, i);
                            Ray3d    Ray1   = new Ray3d(Point, normal);
                            Ray3d    Ray2   = new Ray3d(Point, -normal);
                            double   RayPt1 = Rhino.Geometry.Intersect.Intersection.MeshRay(M, Ray1);
                            double   RayPt2 = Rhino.Geometry.Intersect.Intersection.MeshRay(M, Ray2);
                            Point3d  ProjectedPt;

                            if ((RayPt1 < RayPt2) && (RayPt1 > 0) && (RayPt1 < 1.0))
                            {
                                ProjectedPt = Point * (1 - pull) + pull * Ray1.PointAt(RayPt1);
                            }
                            else if ((RayPt2 < RayPt1) && (RayPt2 > 0) && (RayPt2 < 1.0))
                            {
                                ProjectedPt = Point * (1 - pull) + pull * Ray2.PointAt(RayPt2);
                            }
                            else
                            {
                                ProjectedPt = Point * (1 - pull) + pull * M.ClosestPoint(Point);
                            }

                            P.Vertices.SetVertex(i, ProjectedPt);
                        }


                        if (FeatureV[i] != -1) //pull feature vertices onto feature curves
                        {
                            Point3d Point     = P.Vertices[i].ToPoint3d();
                            Curve   CF        = FC[FeatureV[i]];
                            double  param1    = 0.0;
                            Point3d onFeature = new Point3d();
                            CF.ClosestPoint(Point, out param1);
                            onFeature = CF.PointAt(param1);
                            P.Vertices.SetVertex(i, onFeature);
                        }
                    }
                    else
                    {
                        P.Vertices.SetVertex(i, FV[AnchorV[i]]); //pull anchor vertices onto their points
                    }
                }



                AnchorV  = Util.CompactByVertex(P, AnchorV); //compact the fixed points along with the vertices
                FeatureV = Util.CompactByVertex(P, FeatureV);
                FeatureE = Util.CompactByEdge(P, FeatureE);

                P.Compact(); //this cleans the mesh data structure of unused elements
            }


            Mesh MR = P.ToRhinoMesh();

            MR.Unweld(0.4, true);

            doc.Objects.AddMesh(MR);

            doc.Views.Redraw();
            RhinoApp.WriteLine("The {0} command added one mesh to the document.", EnglishName);

            // ---

            return(Result.Success);
        }
예제 #4
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            Rhino.ApplicationSettings.HistorySettings.RecordingEnabled = true;
            double tolerance = doc.ModelAbsoluteTolerance;

            GetObject gc = new GetObject();

            gc.SetCommandPrompt("Select curve(s) for multipipe");
            gc.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;
            gc.GroupSelect    = true;
            gc.GetMultiple(1, 0);

            if (gc.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gc.CommandResult());
            }

            List <Curve> curves = new List <Curve>();

            for (int i = 0; i < gc.ObjectCount; i++)
            {
                Rhino.DocObjects.ObjRef objref = gc.Object(i);
                Rhino.Geometry.Curve    curve  = objref.Curve();
                curves.Add(curve);
                ids.Add(objref.ObjectId);
            }


            Rhino.Input.Custom.GetNumber gn = new Rhino.Input.Custom.GetNumber();
            gn.SetCommandPrompt("Start and end radius");
            int opList = gn.AddOptionList("Cap", caps, capIndex);

            gn.SetDefaultNumber(radius1);

            while (true)
            {
                Rhino.Input.GetResult get_rc = gn.Get();
                if (gn.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return(gn.CommandResult());
                }

                if (get_rc == Rhino.Input.GetResult.Number)
                {
                    radius1 = gn.Number();
                    if (radius1 < 0 || radius1 < 0.0)
                    {
                        Dialogs.ShowMessage("Value must be >= 0.0", "Warning!");
                        continue;
                    }
                }


                else if (get_rc == Rhino.Input.GetResult.Option)
                {
                    if (gn.OptionIndex() == opList)
                    {
                        capIndex = gn.Option().CurrentListOptionIndex;
                    }
                    continue;
                }

                break;
            }



            while (true)
            {
                RhinoGet.GetNumber("Middle radius", false, ref radius2);
                if (radius2 <= 0 || radius2 <= 0.0)
                {
                    Dialogs.ShowMessage("Value must be > 0.0", "Warning!");
                    continue;
                }

                break;
            }



            Brep[] pipe;

            double[]      radiParam = { 0.0, 0.5, 1.0 };
            double[]      radi      = { radius1, radius2, radius1 };
            PipeCapMode[] pCapM     = { PipeCapMode.None, PipeCapMode.Flat, PipeCapMode.Round };

            // Create a history record
            Rhino.DocObjects.HistoryRecord history = new Rhino.DocObjects.HistoryRecord(this, HISTORY_VERSION);
            WriteHistory(history, ids, radius1, radius2, capIndex);

            foreach (Curve x in curves)
            {
                pipe = Brep.CreatePipe(x, radiParam, radi, true, pCapM[capIndex], true, doc.ModelAbsoluteTolerance, doc.ModelAngleToleranceRadians);
                doc.Objects.AddBrep(pipe[0], null, history, false);
                doc.Views.Redraw();
            }


            doc.Views.Redraw();
            return(Result.Success);
        }
예제 #5
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            var getNumber = new GetNumber();
            getNumber.SetLowerLimit(2.0, false);
            getNumber.SetUpperLimit(100000.0, false);
            getNumber.SetDefaultInteger(RcCore.It.EngineSettings.Samples);
            getNumber.SetCommandPrompt("Set render samples");

            var useCustomSettings = new OptionToggle(RcCore.It.EngineSettings.UseCustomQualitySettings, "No", "Yes");

            var minBounce = new OptionInteger(RcCore.It.EngineSettings.MinBounce, 0, 500);
            var maxBounce = new OptionInteger(RcCore.It.EngineSettings.MaxBounce, 0, 500);

            var maxDiffuseBounce = new OptionInteger(RcCore.It.EngineSettings.MaxDiffuseBounce, 0, 200);
            var maxGlossyBounce = new OptionInteger(RcCore.It.EngineSettings.MaxGlossyBounce, 0, 200);
            var maxTransmissionBounce = new OptionInteger(RcCore.It.EngineSettings.MaxTransmissionBounce, 0, 200);
            var maxVolumeBounce = new OptionInteger(RcCore.It.EngineSettings.MaxVolumeBounce, 0, 200);

            var noCaustics = new OptionToggle(RcCore.It.EngineSettings.NoCaustics, "Caustics", "NoCaustics");

            var aaSamples = new OptionInteger(RcCore.It.EngineSettings.AaSamples, 1, 100);
            var diffSamples = new OptionInteger(RcCore.It.EngineSettings.DiffuseSamples, 1, 100);
            var glossySamples = new OptionInteger(RcCore.It.EngineSettings.GlossySamples, 1, 100);

            var seed = new OptionInteger(RcCore.It.EngineSettings.Seed, 0, int.MaxValue);

            var sensorWidth = new OptionDouble(RcCore.It.EngineSettings.SensorWidth, 10.0, 100.0);
            var sensorHeight = new OptionDouble(RcCore.It.EngineSettings.SensorHeight, 10.0, 100.0);

            var transparentMinBounce = new OptionInteger(RcCore.It.EngineSettings.TransparentMinBounce, 0, 200);
            var transparentMaxBounce = new OptionInteger(RcCore.It.EngineSettings.TransparentMaxBounce, 0, 200);
            var transparentShadows = new OptionToggle(RcCore.It.EngineSettings.TransparentShadows, "NoTransparentShadows", "TransparentShadows");

            var branched = new OptionToggle(RcCore.It.EngineSettings.IntegratorMethod==IntegratorMethod.BranchedPath, "Path", "BranchedPath");

            var samplingPattern = new OptionToggle(RcCore.It.EngineSettings.SamplingPattern == SamplingPattern.CMJ, "Sobol", "CMJ");
            var filterGlossy = new OptionDouble(RcCore.It.EngineSettings.FilterGlossy, 0.0, 100.0);
            var sampleClampDirect = new OptionDouble(RcCore.It.EngineSettings.SampleClampDirect, 0.0, 100.0);
            var sampleClampIndirect = new OptionDouble(RcCore.It.EngineSettings.SampleClampIndirect, 0.0, 100.0);
            var sampleAllLights = new OptionToggle(RcCore.It.EngineSettings.SampleAllLights, "no", "yes");
            var sampleAllLightsIndirect = new OptionToggle(RcCore.It.EngineSettings.SampleAllLightsIndirect, "no", "yes");

            getNumber.AddOptionToggle("use_custom_quality_settings", ref useCustomSettings);

            getNumber.AddOptionInteger("min_bounces", ref minBounce);
            getNumber.AddOptionInteger("max_bounces", ref maxBounce);
            getNumber.AddOptionToggle("no_caustics", ref noCaustics);

            getNumber.AddOptionInteger("max_diffuse_bounce", ref maxDiffuseBounce);
            getNumber.AddOptionInteger("max_glossy_bounce", ref maxGlossyBounce);
            getNumber.AddOptionInteger("max_transmission_bounce", ref maxTransmissionBounce);
            getNumber.AddOptionInteger("max_volume_bounce", ref maxVolumeBounce);

            getNumber.AddOptionInteger("transparent_min_bounce", ref transparentMinBounce);
            getNumber.AddOptionInteger("transparent_max_bounce", ref transparentMaxBounce);
            getNumber.AddOptionToggle("transparent_shadows", ref transparentShadows);

            getNumber.AddOptionInteger("aa_samples", ref aaSamples);
            getNumber.AddOptionInteger("diffuse_samples", ref diffSamples);
            getNumber.AddOptionInteger("glossy_samples", ref glossySamples);

            getNumber.AddOptionDouble("sensor_width", ref sensorWidth);
            getNumber.AddOptionDouble("sensor_height", ref sensorHeight);

            getNumber.AddOptionToggle("integrator_method", ref branched);

            getNumber.AddOptionInteger("seed", ref seed, "Seed to use for sampling distribution");

            getNumber.AddOptionToggle("sampling_pattern", ref samplingPattern);
            getNumber.AddOptionDouble("filter_glossy", ref filterGlossy);
            getNumber.AddOptionDouble("sample_clamp_direct", ref sampleClampDirect);
            getNumber.AddOptionDouble("sample_clamp_indirect", ref sampleClampIndirect);
            getNumber.AddOptionToggle("sample_all_lights", ref sampleAllLights);
            getNumber.AddOptionToggle("sample_all_lights_indirect", ref sampleAllLightsIndirect);

            while (true)
            {
                var getRc = getNumber.Get();
                if (getNumber.CommandResult() != Result.Success) return getNumber.CommandResult();
                switch (getRc)
                {
                    case GetResult.Number:
                        RhinoApp.WriteLine($"We got: {getNumber.Number()}, {minBounce.CurrentValue}, {maxBounce.CurrentValue}");
                        RcCore.It.EngineSettings.Samples = (int)getNumber.Number();
                        RcCore.It.EngineSettings.UseCustomQualitySettings = useCustomSettings.CurrentValue;
                        RcCore.It.EngineSettings.Seed = seed.CurrentValue;
                        RcCore.It.EngineSettings.MaxBounce = maxBounce.CurrentValue;
                        RcCore.It.EngineSettings.MinBounce = minBounce.CurrentValue;
                        RcCore.It.EngineSettings.NoCaustics = noCaustics.CurrentValue;
                        RcCore.It.EngineSettings.MaxDiffuseBounce = maxDiffuseBounce.CurrentValue;
                        RcCore.It.EngineSettings.MaxGlossyBounce = maxGlossyBounce.CurrentValue;
                        RcCore.It.EngineSettings.MaxTransmissionBounce = maxTransmissionBounce.CurrentValue;
                        RcCore.It.EngineSettings.MaxVolumeBounce = maxVolumeBounce.CurrentValue;
                        RcCore.It.EngineSettings.TransparentMinBounce = transparentMinBounce.CurrentValue;
                        RcCore.It.EngineSettings.TransparentMaxBounce = transparentMaxBounce.CurrentValue;
                        RcCore.It.EngineSettings.TransparentShadows = transparentShadows.CurrentValue;
                        RcCore.It.EngineSettings.AaSamples = aaSamples.CurrentValue;
                        RcCore.It.EngineSettings.DiffuseSamples = diffSamples.CurrentValue;
                        RcCore.It.EngineSettings.GlossySamples = glossySamples.CurrentValue;
                        RcCore.It.EngineSettings.SensorWidth = (float)sensorWidth.CurrentValue;
                        RcCore.It.EngineSettings.SensorHeight = (float)sensorHeight.CurrentValue;
                        RcCore.It.EngineSettings.IntegratorMethod = branched.CurrentValue ? IntegratorMethod.BranchedPath : IntegratorMethod.Path;
                        RcCore.It.EngineSettings.SamplingPattern = SamplingPattern.Sobol;
                        RcCore.It.EngineSettings.FilterGlossy = 0.0f;
                        RcCore.It.EngineSettings.SampleClampDirect = 0.0f;
                        RcCore.It.EngineSettings.SampleClampIndirect = 0.0f;
                        RcCore.It.EngineSettings.SampleAllLights = true;
                        RcCore.It.EngineSettings.SampleAllLightsIndirect = true;
                        break;
                    case GetResult.Option:
                        continue;
                }

                break;
            }
            return Result.Success;
        }