Example #1
0
    public void ReComputeShape()
    {
        //Shape
        Rhino.Geometry.Point3d origin = originVec.ToRhino();
        _shapedGrid.Compute(gridSize, x_Ex, y_Ex, offsetValue, offsetValue2, maxReduceNum, minGridNum, origin, ref shapedGrid, ref shape, ref offsetShape, ref originalShape);
        if (Rhino.Geometry.AreaMassProperties.Compute(offsetShape.ToPolylineCurve()).Area <
            (Rhino.Geometry.AreaMassProperties.Compute(shape).Area / 2f))
        {
            ReComputeShape();
            Debug.Log("Failed RecomputeShape and start RecomputeShape again");
            return;
        }

        _areaRatio.Compute(shape, minAreaRatio, roomNum, ref areaSize, ref areaRatio);
        targetAreaSize = areaSize.ToIntList();
        if (debug)
        {
            debugPreview.Add(RhinoPreview.PtsShow(shapedGrid, gridSize, Color.white));
        }
        if (show)
        {
            if (polyLines.Count != 0)
            {
                foreach (var line in polyLines)
                {
                    Destroy(line);
                }
            }

            polyLines.Add(RhinoPreview.PolyLineShow(shape.ToPolyline(), Color.cyan, 0.3f, "shape"));
            polyLines.Add(RhinoPreview.PolyLineShow(offsetShape, Color.blue, 0.3f, "offsetShape"));
            polyLines.Add(RhinoPreview.PolyLineShow(originalShape.ToPolyline(), Color.gray, 0.3f, "originalShape"));
        }


        // make first agent position
        var center = Rhino.Geometry.AreaMassProperties.Compute(shape).Centroid;

        rectMin = RhinoWrapper.MakeRect(center, gridSize, gridSize);
        var areaCenters = RhinoWrapper.RandomPt(rectMin, roomNum);


        if (debug)
        {
            debugPreview.Add(RhinoPreview.PolyLineShow(rectMin.ToPolyline(), Color.green, 0.3f, "minRect"));
            debugPreview.Add(RhinoPreview.PtsShow(areaCenters, gridSize, Color.black));

            var centersList = new List <Rhino.Geometry.Point3d>();
            centersList.Add(center);
            debugPreview.Add(RhinoPreview.PtsShow(centersList, gridSize, Color.magenta));
        }

        //relax agent position
        minDist = (int)Math.Ceiling(Math.Sqrt(2) * gridSize) * (minGridNum + 1);
        _relax.Compute(ref areaCenters, minDist, offsetShape, (double)gridSize / 2.0);
        if (debug)
        {
            debugPreview.Add(RhinoPreview.PtsShow(areaCenters, gridSize, Color.red));
        }
        agentPosition = areaCenters.ToHost();
    }
    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();
            }
        }
    }