/** Pass in the constructor from the poisson-disk-sampling module */ public MeshBuilder addPoisson(float spacing, Rander.RandFloat rng) { var generator = new PoissonDiskSampling(new int[] { 1000, 1000 }, spacing, 0, 0, rng); foreach (var p in this.points) { generator.addPoint(new Float[] { p[0], p[1] }); } this.points = generator.fill(); return(this); }
/** * @param {int} d Dimensions * @param {Function} rng * @returns {Array} */ public static Float[] sphereRandom(int d, Rander.RandFloat rng) { var v = new Float[d]; int d2 = (int)Math.Floor(d / 2.0) << 1, i; Float r2 = 0, rr, r, theta, h; for (i = 0; i < d2; i += 2) { rr = (Float)(-2.0 * Math.Log(rng())); r = (Float)Math.Sqrt(rr); theta = (Float)(2.0 * Math.PI * rng()); r2 += rr; v[i] = r * (Float)Math.Cos(theta); v[i + 1] = r * (Float)Math.Sin(theta); } if (d % 2 != 0) { var x = Math.Sqrt(-2.0 * Math.Log(rng())) * Math.Cos(2.0 * Math.PI * rng()); v[d - 1] = (Float)x; r2 += (Float)Math.Pow(x, 2); } h = (Float)(1.0 / Math.Sqrt(r2)); for (i = 0; i < d; ++i) { v[i] *= h; } return(v); }
/** * PoissonDiskSampling constructor * @param {Array} shape Shape of the space * @param {Float} minDistance Minimum distance between each points * @param {Float} [maxDistance] Maximum distance between each points, defaults to minDistance * 2 * @param {int} [maxTries] Number of times the algorithm has to try to place a point in the neighbourhood of another points, defaults to 30 * @param {function|null} [rng] RNG function, defaults to Math.random * @constructor */ public PoissonDiskSampling(int[] shape, Float minDistance, Float maxDistance, int maxTries, Rander.RandFloat rng) { maxDistance = maxDistance > minDistance ? maxDistance : minDistance * 2; this.shape = shape; this.dimension = shape.Length; this.minDistance = minDistance; this.squaredMinDistance = minDistance * minDistance; this.deltaDistance = maxDistance - minDistance; this.cellSize = minDistance / (Float)Math.Sqrt(this.dimension); this.maxTries = maxTries > 0 ? maxTries : 30; this.rng = rng;// || Math.random; this.neighbourhood = getNeighbourhood(this.dimension); this.currentPoint = null; this.processList = new List <Float[]>(); this.samplePoints = new List <Float[]>(); // cache grid this.gridShape = new List <int>(); for (var i = 0; i < this.dimension; i++) { this.gridShape.Add((int)Math.Ceiling(shape[i] / this.cellSize)); } tinyNDArray(this.gridShape, (stride, data) => { this.gridStride = stride; this.gridData = data; }); //will store references to samplePoints }