コード例 #1
0
    private void SetMask()
    {
        //進んだらエリア外に出る選択肢にマスクをする。

        var nextRhigt = Vec3ToPt3d(transform.position + new Vector3(gridSize, 0, 0f));

        var nextLeft = Vec3ToPt3d(transform.position + new Vector3(-gridSize, 0, 0f));

        var nextUp = Vec3ToPt3d(transform.position + new Vector3(0f, gridSize, 0));

        var nextDown = Vec3ToPt3d(transform.position + new Vector3(0f, -gridSize, 0));


        if (!RhinoWrapper.IsInside(nextRhigt, offsetShape))
        {
            SetActionMask(k_Right);
        }

        if (!RhinoWrapper.IsInside(nextLeft, offsetShape))
        {
            SetActionMask(k_Left);
        }

        if (!RhinoWrapper.IsInside(nextUp, offsetShape))
        {
            SetActionMask(k_Up);
        }

        if (!RhinoWrapper.IsInside(nextDown, offsetShape))
        {
            SetActionMask(k_Down);
        }
    }
コード例 #2
0
    // Start is called before the first frame update
    void Start()
    {
        var plane    = new Rhino.Geometry.Plane(Point3d.Origin, Vector3d.ZAxis);
        var interval = new Interval(-0.5, 0.5);

        rect = new Rectangle3d(plane, interval, interval);
        var intervalR = new Interval(-5, 5);

        region = new Rectangle3d(plane, intervalR, intervalR);
        pts    = RhinoWrapper.RandomPt(rect, numSphere);

        _relax  = gameObject.AddComponent <Relax>();
        spheres = new List <GameObject>();

        var col = new Color(0.5f, 0, 0, 0.01f);

        for (int i = 0; i < pts.Count; i++)
        {
            var sphere = GameObject.CreatePrimitive(PrimitiveType.Quad);
            sphere.GetComponent <MeshRenderer>().material.color = Random.ColorHSV(1f, 1f, 1f, 1f, 0, 0);
            spheres.Add(sphere);
        }


        RhinoPreview.PolyLineShow(region.ToPolyline(), col, 0.3f);
    }
コード例 #3
0
    public override float[] Heuristic()
    {
        var nextRhigt = Vec3ToPt3d(transform.position + new Vector3(gridSize, 0, 0f));

        var nextLeft = Vec3ToPt3d(transform.position + new Vector3(-gridSize, 0, 0f));

        var nextUp = Vec3ToPt3d(transform.position + new Vector3(0f, gridSize, 0));

        var nextDown = Vec3ToPt3d(transform.position + new Vector3(0f, -gridSize, 0));


        selectable.Clear();
        selectable.Add(k_NoAction);

        if (RhinoWrapper.IsInside(nextRhigt, offsetShape))
        {
            selectable.Add(k_Right);
        }

        if (RhinoWrapper.IsInside(nextLeft, offsetShape))
        {
            selectable.Add(k_Left);
        }

        if (RhinoWrapper.IsInside(nextUp, offsetShape))
        {
            selectable.Add(k_Up);
        }

        if (RhinoWrapper.IsInside(nextDown, offsetShape))
        {
            selectable.Add(k_Down);
        }

        if (selectable.Count > 1)
        {
            var idx = Random.Range(1, selectable.Count);
            rtnArr[0] = selectable[idx - 1];
        }
        else
        {
            rtnArr[0] = selectable[0];
        }

        return(rtnArr);
    }
コード例 #4
0
    // Update is called once per frame
    void Update()
    {
        count++;
        if (count > 7)
        {
            pts.RemoveAt(0);
            pts.Add(RhinoWrapper.RandomPt(rect, 1)[0]);
            count = 0;
        }
        _relax.Compute(ref pts, 2, region.ToPolyline(), 0.05);
        var pos = pts.ToHost();

        for (int i = 0; i < pos.Count; i++)
        {
            spheres[i].transform.position = pos[i];
        }
    }
コード例 #5
0
    public void Compute(List <Point3d> AreaCenters, List <Point3d> gridPts, int gridSize, ref List <List <Point3d> > rectPts, ref List <Point3d> othersPts)
    {
        /*
         * if (rectPts.Count == 0)
         * {
         *  rectPts = new List<List<Point3d>>();
         * }
         * for (int i = 0; i < AreaCenters.Count; i++)
         * {
         *  rectPts.Add(new List<Point3d>());
         * }
         */

        var dists     = RhinoWrapper.DistNearPt(AreaCenters);
        var intervals = MakeInterval(dists, gridSize);

        rects.Clear();
        for (int i = 0; i < AreaCenters.Count; i++)
        {
            var plane = new Rhino.Geometry.Plane(AreaCenters[i], Vector3d.ZAxis);
            rects.Add(new Rectangle3d(plane, intervals[i], intervals[i]));
        }

        rectPts = SelRectPts(gridPts, rects);

        otherPtsBuff.Clear();
        var allRectPts = GetAllData(rectPts);

        foreach (var pt in gridPts)
        {
            if (allRectPts.Contains(pt) == false)
            {
                otherPtsBuff.Add(pt);
            }
        }
        othersPts = otherPtsBuff;
    }
コード例 #6
0
ファイル: Relax.cs プロジェクト: kazukiave/Unity_CustomAgents
    public void Compute(ref List <Point3d> centers, double minDist, Polyline region, double moveStep)
    {
        segments.Clear();
        for (int i = 0; i < region.SegmentCount; i++)
        {
            segments.Add(region.SegmentAt(i));
        }


        for (int k = 0; k < 1000; k++)
        {
            vectors.Clear();
            counts.Clear();

            for (int i = 0; i < centers.Count; i++)
            {
                vectors.Add(Vector3d.Zero);
                counts.Add(0.0);


                for (int j = 0; j < centers.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    Vector3d vec  = centers[i] - centers[j];
                    double   dist = vec.Length;

                    if (dist > minDist)
                    {
                        continue;
                    }
                    vec.Unitize();
                    vectors[i] += vec;
                    counts[i]  += 1.0;
                }
            }



            //to in wall
            for (int i = 0; i < centers.Count; i++)
            {
                if (counts[i] != 0)
                {
                    centers[i] += (vectors[i] / counts[i]) * moveStep;
                }

                if (RhinoWrapper.IsInside(centers[i], region))
                {
                    continue;
                }
                counts[i]++;
                var nearWall = region.ClosestPoint(centers[i]);
                // var vector = nearWall - centers[i];
                centers[i] = nearWall;

                /*
                 * vector.Unitize();
                 * centers[i] += vector * minDist;
                 */
            }
        }
    }
コード例 #7
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();
    }
コード例 #8
0
    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();
            }
        }
    }