浏览代码

super sampled poisson points to prevent edge case bias

/main
sleal-unity 4 年前
当前提交
38402b33
共有 1 个文件被更改,包括 60 次插入8 次删除
  1. 68
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs

68
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs


/// <param name="minimumRadius">The minimum distance required between each sampled point</param>
/// <param name="seed">The random seed used to initialize the algorithm state</param>
/// <param name="samplingResolution">The number of potential points sampled around every valid point</param>
/// <param name="allocator">The allocator to use for the samples container</param>
/// <returns>The list of generated poisson points</returns>
public static NativeList<float2> GenerateSamples(
float width,

int samplingResolution = k_DefaultSamplingResolution)
int samplingResolution = k_DefaultSamplingResolution,
Allocator allocator = Allocator.TempJob)
{
if (width < 0)
throw new ArgumentException($"Width {width} cannot be negative");

if (samplingResolution <= 0)
throw new ArgumentException($"SamplingAttempts {samplingResolution} cannot be <= 0");
var samples = new NativeList<float2>(Allocator.TempJob);
new SampleJob
var superSampledPoints = new NativeList<float2>(allocator);
var sampleJob = new SampleJob
width = width,
height = height,
width = width + minimumRadius * 2,
height = height + minimumRadius * 2,
samples = samples
}.Schedule().Complete();
return samples;
samples = superSampledPoints
}.Schedule();
var croppedSamples = new NativeList<float2>(allocator);
new CropJob
{
width = width,
height = height,
minimumRadius = minimumRadius,
superSampledPoints = superSampledPoints.AsDeferredJobArray(),
croppedSamples = croppedSamples
}.Schedule(sampleJob).Complete();
superSampledPoints.Dispose();
return croppedSamples;
}
[BurstCompile]

var newSamples = Sample(width, height, minimumRadius, seed, samplingResolution, Allocator.Temp);
samples.AddRange(newSamples);
newSamples.Dispose();
}
}
/// <summary>
/// This job is for filtering out all super sampled Poisson points that are found outside of the originally
/// specified 2D region. This job will also shift the cropped points back to their original region.
/// </summary>
[BurstCompile]
struct CropJob : IJob
{
public float width;
public float height;
public float minimumRadius;
[ReadOnly] public NativeArray<float2> superSampledPoints;
public NativeList<float2> croppedSamples;
public void Execute()
{
var results = new NativeArray<bool>(
superSampledPoints.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
for (var i = 0; i < superSampledPoints.Length; i++)
{
var point = superSampledPoints[i];
results[i] = point.x >= minimumRadius && point.x <= width + minimumRadius
&& point.y >= minimumRadius && point.y <= height + minimumRadius;
}
for (var i = 0; i < superSampledPoints.Length; i++)
{
if (results[i])
{
var p = superSampledPoints[i];
croppedSamples.Add(new float2(p.x - minimumRadius, p.y - minimumRadius));
}
}
results.Dispose();
}
}

正在加载...
取消
保存