Fixed sdf usage in gradient and added test cases
This commit is contained in:
2
SDFMapCreator.sln.DotSettings.user
Normal file
2
SDFMapCreator.sln.DotSettings.user
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVector3_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003Fhome_003Fmm00_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6edafe13d8727aa238b865f5dc91dbc984b5abfbc60bece3744f6311c2c_003FVector3_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
|
||||||
@@ -88,8 +88,13 @@ public class Program {
|
|||||||
|
|
||||||
var imagesPath = "images";
|
var imagesPath = "images";
|
||||||
|
|
||||||
|
/*
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
LoadImage($"./{imagesPath}{Path.DirectorySeparatorChar}{i+1:00}.png");
|
LoadImage($"./{imagesPath}{Path.DirectorySeparatorChar}{i+1:00}.png");
|
||||||
|
*/
|
||||||
|
|
||||||
|
LoadImage("spherecut.png");
|
||||||
|
LoadImage("spherefull.png");
|
||||||
|
|
||||||
//LoadImage("1.png");
|
//LoadImage("1.png");
|
||||||
//LoadImage("2.png");
|
//LoadImage("2.png");
|
||||||
@@ -132,14 +137,16 @@ public class Program {
|
|||||||
|
|
||||||
Console.WriteLine("Creating gradients...");
|
Console.WriteLine("Creating gradients...");
|
||||||
for (var i = 0; i < Masks.Count - 1; i++) {
|
for (var i = 0; i < Masks.Count - 1; i++) {
|
||||||
var gradientData = Gradient(Masks[i], SDFs[i], SDFs[i + 1]);
|
var gradientData = Gradient(Masks[i+1], SDFs[i], SDFs[i + 1]);
|
||||||
Gradients.Add(gradientData);
|
Gradients.Add(gradientData);
|
||||||
if(!outputGradients) continue;
|
if(!outputGradients) continue;
|
||||||
var gradient = new MagickImage(MagickColors.Black, (uint)Masks[i].Mask.GetLength(0), (uint)Masks[i].Mask.GetLength(1));
|
var gradient = new MagickImage(MagickColors.Black, (uint)Masks[i+1].Mask.GetLength(0), (uint)Masks[i+1].Mask.GetLength(1));
|
||||||
gradient.GetPixels().SetPixels(gradientData.ToFloatArray());
|
gradient.GetPixels().SetPixels(gradientData.ToFloatArray());
|
||||||
gradient.Write($"gradient{i}.png", MagickFormat.Png24);
|
gradient.Write($"gradient{i}.png", MagickFormat.Png24);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
Console.WriteLine("Preparing transitions...");
|
Console.WriteLine("Preparing transitions...");
|
||||||
//for each gradient read the corresponding mask
|
//for each gradient read the corresponding mask
|
||||||
//for each pixel in the mask, lerp the pixel value with the gradient value
|
//for each pixel in the mask, lerp the pixel value with the gradient value
|
||||||
@@ -150,14 +157,35 @@ public class Program {
|
|||||||
var mask = Masks[i + 1];
|
var mask = Masks[i + 1];
|
||||||
var gradient = Gradients[i];
|
var gradient = Gradients[i];
|
||||||
var transition = new float3[width, height];
|
var transition = new float3[width, height];
|
||||||
|
var absMax = MIN;
|
||||||
|
var absMin = MAX;
|
||||||
for (var x = 0; x < mask.Mask.GetLength(0); x++) {
|
for (var x = 0; x < mask.Mask.GetLength(0); x++) {
|
||||||
for (var y = 0; y < mask.Mask.GetLength(1); y++) {
|
for (var y = 0; y < mask.Mask.GetLength(1); y++) {
|
||||||
if (mask.Mask[x, y].X == 0) continue;
|
if (mask.Mask[x, y].X == 0 || mask.Mask[x, y].Y > 0) continue;
|
||||||
transition[x, y] = new(Lerp(Images[i].Pixels[x, y].X, MAX, gradient[x, y].X));
|
transition[x, y] = new(Lerp(Images[i].Pixels[x, y].X, MAX, gradient[x, y].X));
|
||||||
|
if (transition[x, y].X > absMax) absMax = transition[x, y].X;
|
||||||
|
if (transition[x, y].X < absMin) absMin = transition[x, y].X;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absMax = MathF.Min(absMax, MAX);
|
||||||
|
absMin = MathF.Max(absMin, MIN);
|
||||||
|
|
||||||
|
//normalize
|
||||||
|
Parallel.For(0, width * height, parallelOptions, (j) => {
|
||||||
|
var x = (int)(j % width);
|
||||||
|
var y = (int)(j / width);
|
||||||
|
//Console.WriteLine($"Transition {i} | Min: {absMin} | Max: {absMax}, {transition[x, y].X}");
|
||||||
|
transition[x, y] = new(Remap(transition[x, y].X, absMin, absMax, MIN, MAX));
|
||||||
|
});
|
||||||
|
|
||||||
|
Console.WriteLine($"Transition {i} | Min: {absMin} | Max: {absMax}");
|
||||||
|
Console.WriteLine($"Transition {i} | Min: {transition.Cast<float3>().Min(x => x.X)} | Max: {transition.Cast<float3>().Max(x => x.X)}");
|
||||||
|
|
||||||
var transitionImage = new MagickImage(MagickColors.Black, (uint)mask.Mask.GetLength(0), (uint)mask.Mask.GetLength(1));
|
var transitionImage = new MagickImage(MagickColors.Black, (uint)mask.Mask.GetLength(0), (uint)mask.Mask.GetLength(1));
|
||||||
transitionImage.GetPixels().SetPixels(transition.ToFloatArray());
|
transitionImage.GetPixels().SetPixels(transition.ToFloatArray());
|
||||||
|
|
||||||
|
|
||||||
transitionImage.Write($"transition{i}.png", MagickFormat.Png24);
|
transitionImage.Write($"transition{i}.png", MagickFormat.Png24);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,21 +224,28 @@ public class Program {
|
|||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
sw.Start();
|
sw.Start();
|
||||||
float3[,] temp = new float3[width, height];
|
float3[,] temp = new float3[width, height];
|
||||||
Console.WriteLine("Running edge detection...");
|
|
||||||
|
var min = MAX;
|
||||||
|
var max = MIN;
|
||||||
|
|
||||||
|
Console.WriteLine("Running gradient generation...");
|
||||||
Parallel.For(0, width * height, parallelOptions, (i) => {
|
Parallel.For(0, width * height, parallelOptions, (i) => {
|
||||||
int x = (int)(i % width);
|
int x = (int)(i % width);
|
||||||
int y = (int)(i / width);
|
int y = (int)(i / width);
|
||||||
|
|
||||||
var a = (sdfA.SDF[x,y].X + sdfB.SDF[x,y].Y + sdfB.SDF[x,y].Z )/ 3;
|
if(mask.Mask[x,y].X == 0 || mask.Mask[x,y].Y > 0) return;
|
||||||
var b = (sdfB.SDF[x,y].X + sdfB.SDF[x,y].Y + sdfB.SDF[x,y].Z )/ 3;
|
|
||||||
|
var a = sdfA.SDF[x, y].X;
|
||||||
|
var b = sdfB.SDF[x, y].X;
|
||||||
|
|
||||||
var gradient = a / (a + b);
|
var gradient = a / (a + b);
|
||||||
temp[x, y] = new(Remap(gradient, 0, 1, MIN, MAX));
|
temp[x, y] = new(Remap(gradient, 0, 1, MIN, MAX));
|
||||||
|
|
||||||
|
if (gradient < min) min = gradient;
|
||||||
|
if (gradient > max) max = gradient;
|
||||||
});
|
});
|
||||||
|
|
||||||
Console.WriteLine($"Gradient Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount/(float)sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
Console.WriteLine($"Gradient Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount/(float)sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
||||||
var min = temp.Cast<float3>().Min(x => x.X);
|
|
||||||
var max = temp.Cast<float3>().Max(x => x.X);
|
|
||||||
Console.WriteLine($"Min: {min} | Max: {max}");
|
Console.WriteLine($"Min: {min} | Max: {max}");
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
@@ -253,6 +288,8 @@ public class Program {
|
|||||||
var y = (int)(i / width);
|
var y = (int)(i / width);
|
||||||
temp[x, y] = new(Remap(temp[x, y].X, 0, AbsMax, MIN, MAX));
|
temp[x, y] = new(Remap(temp[x, y].X, 0, AbsMax, MIN, MAX));
|
||||||
});
|
});
|
||||||
|
sw.Stop();
|
||||||
|
|
||||||
Console.WriteLine($"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount/sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
Console.WriteLine($"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount/sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
||||||
Console.WriteLine("AbsMax: " + AbsMax);
|
Console.WriteLine("AbsMax: " + AbsMax);
|
||||||
return new(temp);
|
return new(temp);
|
||||||
@@ -262,7 +299,6 @@ public class Program {
|
|||||||
private static float EuclideanDistance(float2 a, float2 b) =>
|
private static float EuclideanDistance(float2 a, float2 b) =>
|
||||||
MathF.Sqrt(MathF.Pow(a.X - b.X, 2) + MathF.Pow(a.Y - b.Y, 2));
|
MathF.Sqrt(MathF.Pow(a.X - b.X, 2) + MathF.Pow(a.Y - b.Y, 2));
|
||||||
|
|
||||||
|
|
||||||
private static void ImageData(MagickImage image1, IPixelCollection<float> pixels1) {
|
private static void ImageData(MagickImage image1, IPixelCollection<float> pixels1) {
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
$"""
|
$"""
|
||||||
|
|||||||
@@ -46,6 +46,12 @@
|
|||||||
<None Update="images\08.png">
|
<None Update="images\08.png">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="spherecut.png">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="spherefull.png">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
BIN
SDFMapCreator/spherecut.png
Normal file
BIN
SDFMapCreator/spherecut.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.4 KiB |
BIN
SDFMapCreator/spherefull.png
Normal file
BIN
SDFMapCreator/spherefull.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user