Example #1
0
    void iteration()
    {
        //generate other points from points in queue.
        if (_activeList.Count > 0 && _grid.nSamples < _maxPointCount)
        {
            // Select random point
            int     currentIdx = _activeList.pop();
            Vector3 point      = _grid[currentIdx];
            consideredSample = point;
            // 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, _size.x, _size.y, _size.z) && !inNeighbourhood(newPoint))
                {
                    newSample = newPoint;
                    //update containers

                    int idx = _grid.addSample(newPoint);
                    _activeList.Add(idx);
                    pointInserted = true;
                    break;
                }
            }
            // reinsert the sample, if a new point was generated
            if (pointInserted)
            {
                _activeList.Add(currentIdx);
            }
        }
        else
        {
            // Resize to actual number of samples generated
            finalize();
        }
    }
Example #2
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;
    }