Beispiel #1
0
        /*
         * public static void ConvertToTree(GH_Structure<GH_Point> structure, ref DataTree<Point3d> tree)
         * {
         *  for (int i = 0; i < structure.Paths.Count; i++)
         *  {
         *      var path = structure.Paths[i];
         *      for (int j = 0; j < structure[path].Count; j++)
         *      {
         *          tree.Add(structure[path][j].Value, path);
         *      }
         *  }
         * }
         */

        public static List <double> DistNearPt(List <Point3d> pts)
        {
            var rtnList = new List <double>();

            for (int i = 0; i < pts.Count; i++)
            {
                var others = new List <Point3d>(pts);
                others.RemoveAt(i);
                var pc         = new Rhino.Geometry.PointCloud(others);
                var closestIdx = pc.ClosestPoint(pts[i]);

                rtnList.Add(pts[i].DistanceTo(others[closestIdx]));
            }

            return(rtnList);
        }
    public void Compute(int gridSize, int x_Ex, int y_Ex, int offsetValue, int offsetValue2, int reduceNum, int minRoomNum, Point3d center
                        , ref List <Rhino.Geometry.Point3d> shapedGrid, ref PolylineCurve shapeCrv, ref Polyline offsetShapeCrv, ref Rectangle3d originalShape)
    {
        var grid        = RhinoWrapper.MakeGrid(x_Ex, y_Ex, gridSize);
        var rectMain    = RhinoWrapper.MakeRect(center, x_Ex, y_Ex, gridSize);
        var rectMainCrv = rectMain.ToPolyline().ToPolylineCurve();

        originalShape = rectMain;

        //減らす部分の四角形をつくるための元のcorner
        var corners = new Rhino.Geometry.Point3d[4];

        for (int i = 0; i < 4; i++)
        {
            corners[i] = rectMain.Corner(i);
        }

        //引くための四角形を作る範囲
        var rectSub  = RhinoWrapper.MakeRect(center, x_Ex, y_Ex, gridSize, offsetValue);
        var rectSub2 = RhinoWrapper.MakeRect(center, x_Ex, y_Ex, gridSize, offsetValue2);


        var populate = RhinoWrapper.RandomPt(rectSub, reduceNum);
        var randPts  = populate.Where(pt => RhinoWrapper.IsInside(pt, rectSub2.ToPolyline()) == false).ToList();

        //点が近いところにあるとオフセットがうまく機能しない。
        for (int i = 0; i < randPts.Count; i++)
        {
            for (int j = 0; j < randPts.Count; j++)
            {
                if (i == j)
                {
                    continue;
                }
                if (randPts[i].DistanceTo(randPts[j]) < gridSize * (minRoomNum + 1))
                {
                    randPts.RemoveAt(j);
                    j--;
                }
            }
        }

        //Reduce Rectsを作ってる
        var reduceRects = new List <Rhino.Geometry.PolylineCurve>();
        var planeXY     = new Rhino.Geometry.Plane(Point3d.Origin, Vector3d.ZAxis);

        for (int i = 0; i < randPts.Count; i++)
        {
            var pc         = new Rhino.Geometry.PointCloud(corners);
            int closestIdx = pc.ClosestPoint(randPts[i]);
            var reduceRect = new Rectangle3d(planeXY, randPts[i], corners[closestIdx]);
            var polyCrv    = reduceRect.ToPolyline().ToPolylineCurve();
            reduceRects.Add(polyCrv);
        }


        var shape = Curve.CreateBooleanDifference(rectMainCrv, reduceRects, 0.1);

        offsetShapeCrv = null;
        //正四角形でない形がでたとき
        if (shape.Length > 0)
        {
            shape[0].TryGetPolyline(out Polyline polyShape);

            //ref
            shapedGrid = grid.Where(pt => RhinoWrapper.IsInside(pt, polyShape)).ToList();

            //ref
            shapeCrv = polyShape.ToPolylineCurve();

            //ref
            var plane = new Rhino.Geometry.Plane(AreaMassProperties.Compute(shapeCrv).Centroid, Vector3d.ZAxis);
            shapeCrv.Offset(plane, (-gridSize * minRoomNum), 1, CurveOffsetCornerStyle.Sharp)[0].TryGetPolyline(out offsetShapeCrv);
            if (offsetShapeCrv == null)
            {
                offsetShapeCrv = new Rectangle3d(plane, 1, 1).ToPolyline();
            }
        }
        else//正四角形が残ったとき。(ひくためのRectangleができない)
        {
            shapedGrid = grid.Where(pt => RhinoWrapper.IsInside(pt, rectMainCrv.ToPolyline())).ToList();

            //ref
            shapeCrv = rectMainCrv.ToPolyline().ToPolylineCurve();

            //ref
            var plane = new Rhino.Geometry.Plane(AreaMassProperties.Compute(shapeCrv).Centroid, Vector3d.ZAxis);
            shapeCrv.Offset(plane, (-gridSize * minRoomNum), 1, CurveOffsetCornerStyle.Sharp)[0].TryGetPolyline(out offsetShapeCrv);
            if (offsetShapeCrv == null)
            {
                offsetShapeCrv = new Rectangle3d(plane, 1, 1).ToPolyline();
            }
        }
    }
Beispiel #3
0
        public BrepConverter(SurfaceConverter surfConv, CurveConverter curveConv, Point3dConverter ptConv) :
            base(
                (rb) => {
            List <pps.Surface> faces     = new List <pps.Surface>();
            List <List <int> > adjacency = new List <List <int> >();
            for (int i = 0; i < rb.Faces.Count; i++)
            {
                var surf = (pps.NurbsSurface)surfConv.ToPipe <rh.Surface, pps.Surface>(rb.Faces[i].ToNurbsSurface());
                surf.OuterTrims.Clear();
                surf.OuterTrims.AddRange(rb.Faces[i].Loops.Where((l) =>
                                                                 l.LoopType == rh.BrepLoopType.Outer).Select((l) =>
                                                                                                             curveConv.ToPipe <rh.Curve, ppc.Curve>(l.To3dCurve())));
                surf.InnerTrims.Clear();
                surf.InnerTrims.AddRange(rb.Faces[i].Loops.Where((l) =>
                                                                 l.LoopType == rh.BrepLoopType.Inner).Select((l) =>
                                                                                                             curveConv.ToPipe <rh.Curve, ppc.Curve>(l.To3dCurve())));

                faces.Add(surf);
                adjacency.Add(rb.Faces[i].AdjacentFaces().ToList());
            }
            var polySurf     = new pps.PolySurface(faces, adjacency);
            polySurf.IsSolid = rb.IsSolid;

            return(polySurf);
        },
                (pb) => {
            if (pb.Surfaces.Count <= 0)
            {
                return(null);
            }
            rh.Brep brep = null;
            //trying to create a trimmed brep with built in methods
            if (Util.TryCreateBrepWithBuiltInMethods(pb, out brep, surfConv, curveConv))
            {
                return(brep);
            }

            //attemping to build it from scratch - 15 attempts
            int attempts = 0;
            while (attempts < 15)
            {
                brep        = new rh.Brep();
                var ptCloud = new rh.PointCloud(pb.Vertices().Select((p) => ptConv.FromPipe <rh.Point3d, pp.Vec>(p)));
                var ptList  = ptCloud.GetPoints().ToList();
                ptList.ForEach((p) => brep.Vertices.Add(p, Rhino.RhinoMath.ZeroTolerance));

                var ppEdges = pb.Edges();
                foreach (var ppCur in ppEdges)
                {
                    var rhCurve    = curveConv.FromPipe <rh.Curve, ppc.Curve>(ppCur);
                    int curveIndex = brep.Curves3D.Add(rhCurve);
                    int startIndex = ptCloud.ClosestPoint(rhCurve.PointAtStart);
                    int endIndex   = ptCloud.ClosestPoint(rhCurve.PointAtEnd);
                    var edge       = brep.Edges.Add(startIndex, endIndex, curveIndex, Rhino.RhinoMath.ZeroTolerance);
                }

                foreach (var ppSurf in pb.Surfaces)
                {
                    var rhSurf    = surfConv.FromPipe <rh.Surface, pps.Surface>(ppSurf);
                    var surfIndex = brep.AddSurface(rhSurf);
                    var face      = brep.Faces.Add(surfIndex);
                    var loop      = brep.Loops.Add(rh.BrepLoopType.Outer, face);

                    var surfEdges = ppSurf.Edges();
                    int loopCount = 0;
                    foreach (var ppLoop in surfEdges)
                    {
                        var rhCurve = curveConv.FromPipe <rh.Curve, ppc.Curve>(ppLoop);
                        var curve2d = rhSurf.Pullback(rhCurve, Rhino.RhinoMath.ZeroTolerance);
                        if (curve2d == null)
                        {
                            continue;
                        }
                        loopCount += 1;

                        int c2i = brep.Curves2D.Add(curve2d);
                        int c3i = ppEdges.IndexOf(ppLoop);
                        if (c3i == -1)
                        {
                            c3i = brep.Curves3D.Add(rhCurve);
                        }

                        int startIndex = ptCloud.ClosestPoint(rhCurve.PointAtStart);
                        int endIndex   = ptCloud.ClosestPoint(rhCurve.PointAtEnd);
                        var trim       = brep.Trims.Add(brep.Edges[c3i], false, loop, c2i);
                        trim.IsoStatus = rh.IsoStatus.None;
                        trim.TrimType  = rh.BrepTrimType.Boundary;
                        trim.SetTolerances(0.0, 0.0);
                    }
                    if (loopCount == 0)
                    {
                        var curve2d    = Util.Get2dEdgeLoop(rhSurf);
                        int c2i        = brep.Curves2D.Add(curve2d);
                        var curve3d    = rhSurf.Pushup(curve2d, Rhino.RhinoMath.ZeroTolerance);
                        int c3i        = brep.Curves3D.Add(curve3d);
                        int startIndex = ptCloud.ClosestPoint(curve3d.PointAtStart);
                        int endIndex   = ptCloud.ClosestPoint(curve3d.PointAtEnd);
                        var edge       = brep.Edges.Add(startIndex, endIndex, c3i, Rhino.RhinoMath.ZeroTolerance);
                        var trim       = brep.Trims.Add(brep.Edges[c3i], false, loop, c2i);
                        trim.IsoStatus = rh.IsoStatus.None;
                        trim.TrimType  = rh.BrepTrimType.Boundary;
                        trim.SetTolerances(0.0, 0.0);
                    }

                    //var uSurfDom = rhSurf.Domain(0);
                    //var vSurfDom = rhSurf.Domain(1);
                    //var ufaceDom = face.Domain(0);
                    //var vfaceDom = face.Domain(1);
                    //var surfNorm = rhSurf.NormalAt(uSurfDom.Mid, vSurfDom.Mid);
                    //var faceNorm = face.NormalAt(ufaceDom.Mid, vfaceDom.Mid);
                    //face.OrientationIsReversed = rh.Vector3d.Multiply(surfNorm, faceNorm) < 0;
                    face.OrientationIsReversed = false;
                }
                //updating attempts and breaking if succeeded
                attempts += 1;
                if (brep.IsValid)
                {
                    break;
                }
            }

            string msg;
            if (!brep.IsValidWithLog(out msg))
            {
                System.Diagnostics.Debug.WriteLine(msg);
                brep.Repair(Rhino.RhinoMath.ZeroTolerance);
                if (brep.IsValid)
                {
                    return(brep);
                }

                //finally attemping to create an untrimmed brep
                int attempt = 0;
                while (!brep.IsValid && attempt < 15)
                {
                    brep = rh.Brep.MergeBreps(pb.Surfaces.Select((s) =>
                                                                 rh.Brep.CreateFromSurface(surfConv.FromPipe <rh.Surface, pps.Surface>(s))),
                                              Rhino.RhinoMath.ZeroTolerance);
                    attempt += 1;
                }

                if (!brep.IsValid)
                {
                    throw new InvalidOperationException("Failed to create a valid brep from " +
                                                        "received data because: \n" + msg);
                }
            }
            return(brep);
        }
                )
        { }