private void CreateCars()
    {
        if (Cars.Length == 0)
        {
            return;
        }
        var points  = new RandomQueue <GameObject>(GameObject.FindGameObjectsWithTag("StartPoint"));
        int numCars = points.Count / 100 + 10;

        numCars = Mathf.Min(numCars, 1050);
        numCars = Mathf.Max(numCars, 1000);
        Debug.Log($"# of cars: {numCars}");
        Debug.Log($"Queue Size: {points.Count}");
        for (int i = 0; i < numCars; i++)
        {
            var newCar     = Instantiate <Car>(Cars[Random.Range(0, Cars.Length)]);
            var spawnPoint = points.Dequeue();
            while (spawnPoint != null && spawnPoint.GetComponent <RoadPoint>().IsTrafficLightRoad)
            {
                spawnPoint = points.Dequeue();
            }
            if (spawnPoint == null)
            {
                break;
            }
            // newCar.SetNextPoint(spawnPoint, spawnPoint);
            newCar.SetNextPoint(spawnPoint);
            newCar.transform.position = spawnPoint.transform.position + Vector3.up * 5;
            newCar.transform.rotation = spawnPoint.transform.rotation;
            // Camera.main.transform.position = newCar.transform.position;
        }
    }
 public override void Setup()
 {
     cellSize     = minDistance / Mathf.Sqrt(2);
     grid         = new Grid2D(Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize));
     processList  = new RandomQueue <Vector2>();
     samplePoints = new List <Vector2>();
     SetBounds(width, height);
 }
Example #3
0
    public PoissonDiskSampling2(float width, float height, float depth, float minDist, int k = 30, int maxPointCount = 10000)
        : base(new Vector3(0, 0, 0), new Vector3(width, height, depth))
    {
        _minDist       = minDist;
        _k             = k;
        _maxPointCount = maxPointCount;

        float cellSize = _minDist / Mathf.Sqrt(3.0f);      // sqrt of dimension, in this case 3D

        _grid = new Grid3D2(new Triple(
                                Mathf.CeilToInt(_size.x / cellSize),             // grid width
                                Mathf.CeilToInt(_size.y / cellSize),             // grid height
                                Mathf.CeilToInt(_size.z / cellSize)), cellSize); // grid depth

        //RandomQueue works like a queue, except that it
        //pops a random element from the queue instead of
        //the element at the head of the queue
        _activeList = new RandomQueue <int>();
    }
Example #4
0
        public static List <Vector2> GeneratePoisson(Rect rect, float minDist, int newPointsCount)
        {
            //Create the grid
            float cellSize = minDist / Mathf.Sqrt(2);

            var gridWidth  = Mathf.CeilToInt(rect.width / cellSize);
            var gridHeight = Mathf.CeilToInt(rect.height / cellSize);
            var grid       = RectGrid <Vector2?> .Rectangle(gridWidth, gridHeight);

            var map = new RectMap(Vector2.one)
                      .AnchorCellBottomLeft()
                      .WithWindow(rect)
                      .Stretch(grid);

            var processList  = new RandomQueue <Vector2>();
            var samplePoints = new List <Vector2>();

            //generate the first point randomly
            //and updates
            var firstPoint = new Vector2(Random.value * rect.width, Random.value * rect.height) + new Vector2(rect.xMin, rect.yMin);

            //update containers
            processList.Push(firstPoint);
            samplePoints.Add(firstPoint);
            grid[map[firstPoint]] = firstPoint;

            //generate other points from points in queue.
            while (!processList.IsEmpty())
            {
                var point = processList.Pop();

                for (int i = 0; i < newPointsCount; i++)
                {
                    var newPoint = GenerateRandomPointAround(point, minDist);
                    //check that the point is in the image region
                    //and no points exists in the point's neighbourhood

                    if (rect.Contains(newPoint) && !IsInNeighbourhood(grid, map, newPoint, minDist))
                    {
                        if (grid.Contains(map[newPoint]))                         //TODO: why is this necessary?
                        {
                            //update containers
                            processList.Push(newPoint);
                            samplePoints.Add(newPoint);

                            grid[map[newPoint]] = newPoint;
                        }

                        /*
                         * else
                         * {
                         * Debug.Log(newPoint);
                         * Debug.Log(map[newPoint]);
                         * Debug.Break();
                         * }
                         */
                    }
                }
            }
            return(samplePoints);
        }
Example #5
0
    void generate_poisson(float width, float height, float depth)
    {
        //Create the grid
        float cellSize = _minDist / Mathf.Sqrt(3.0f);      // sqrt of dimension, in this case 3D

        _data = new Grid3D(new Triple(
                               Mathf.CeilToInt(width / cellSize),             // grid width
                               Mathf.CeilToInt(height / cellSize),            // grid height
                               Mathf.CeilToInt(depth / cellSize)), cellSize); // grid depth


        //RandomQueue works like a queue, except that it
        //pops a random element from the queue instead of
        //the element at the head of the queue
        RandomQueue <Vector3> processList = new RandomQueue <Vector3>();

        Vector3[] samples = new Vector3[_maxPointCount];


        // generate the first point randomly
        // and update

        Vector3 firstPoint = new Vector3(UnityEngine.Random.value * width, UnityEngine.Random.value * height, UnityEngine.Random.value * depth);

        //update containers
        processList.Add(firstPoint);
        int nSamples = 0;

        samples[nSamples] = firstPoint;
        _data[firstPoint] = nSamples;
        nSamples++;

        //generate other points from points in queue.
        while (processList.Count > 0 && nSamples < _maxPointCount)
        {
            // Select random point
            Vector3 point = processList.pop();
            // if we want less points than k (which is usually fixed to 30), don't try more.
            int  k             = Mathf.Min(_k, _maxPointCount);
            bool pointInserted = false;
            for (int i = 0; i < k; i++)
            {
                Vector3 newPoint = generateRandomPointAround(point);
                //check that the point is in the image region
                //and no points exists in the point's neighbourhood
                if (inCube(newPoint, width, height, depth) && !inNeighbourhood(_data, newPoint))
                {
                    //update containers
                    processList.Add(newPoint);
                    samples[nSamples] = newPoint;
                    _data[newPoint]   = nSamples;
                    pointInserted     = true;
                    nSamples++;
                    break;
                }
            }
            // reinsert the sample, if a new point was generated
            if (pointInserted)
            {
                processList.Add(point);
            }
        }

        // Resize to actual number of samples generated
        var obj = samples;

        Array.Resize(ref obj, nSamples);
        _data.samples = obj;
    }