Refactor
This commit is contained in:
@@ -24,7 +24,9 @@ public struct float4(float r, float g, float b, float a) {
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
public record ImageData(MagickImage Image, float3[,] Pixels, List<float2> Edges);
|
public record ImageData(MagickImage Image, float3[,] Pixels, List<float2> Edges);
|
||||||
|
|
||||||
public record MaskData(float3[,] Mask, ImageData Image, List<float2> Edges);
|
public record MaskData(float3[,] Mask, ImageData Image, List<float2> Edges);
|
||||||
|
|
||||||
public record TransitionMaskData(float3[,] Mask, ImageData ImageA, ImageData ImageB);
|
public record TransitionMaskData(float3[,] Mask, ImageData ImageA, ImageData ImageB);
|
||||||
|
|
||||||
public record SDFData(float3[,] SDF);
|
public record SDFData(float3[,] SDF);
|
||||||
@@ -62,8 +64,8 @@ public class Program {
|
|||||||
private const bool outputGradients = true;
|
private const bool outputGradients = true;
|
||||||
static readonly ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = MAX_THREADS };
|
static readonly ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = MAX_THREADS };
|
||||||
static List<ImageData> Images = new();
|
static List<ImageData> Images = new();
|
||||||
static List<TransitionMaskData> TransitionMasks = new();
|
|
||||||
static List<MaskData> ImageMasks = new();
|
static List<MaskData> ImageMasks = new();
|
||||||
|
static List<TransitionMaskData> TransitionMasks = new();
|
||||||
static List<SDFData> SDFs = new();
|
static List<SDFData> SDFs = new();
|
||||||
static List<float3[,]> Gradients = new();
|
static List<float3[,]> Gradients = new();
|
||||||
|
|
||||||
@@ -114,8 +116,7 @@ public class Program {
|
|||||||
for (int i = 0; i < Images.Count; i++) {
|
for (int i = 0; i < Images.Count; i++) {
|
||||||
ImageMasks.Add(new(SelfMask(Images[i].Pixels, width, height), Images[i], new()));
|
ImageMasks.Add(new(SelfMask(Images[i].Pixels, width, height), Images[i], new()));
|
||||||
|
|
||||||
if (i < Images.Count - 1)
|
if (i < Images.Count - 1) {
|
||||||
{
|
|
||||||
Console.WriteLine($"Creating mask {i}...");
|
Console.WriteLine($"Creating mask {i}...");
|
||||||
var mask = GetABMask(Images[i].Pixels, Images[i + 1].Pixels, width, height);
|
var mask = GetABMask(Images[i].Pixels, Images[i + 1].Pixels, width, height);
|
||||||
TransitionMasks.Add(new(mask, Images[i], Images[i + 1]));
|
TransitionMasks.Add(new(mask, Images[i], Images[i + 1]));
|
||||||
@@ -124,12 +125,15 @@ public class Program {
|
|||||||
|
|
||||||
Console.WriteLine("Edge detecting masks...");
|
Console.WriteLine("Edge detecting masks...");
|
||||||
//EdgeDetect all masks
|
//EdgeDetect all masks
|
||||||
foreach (var t in ImageMasks) { EdgeDetect(t); }
|
foreach (var t in ImageMasks) {
|
||||||
|
EdgeDetect(t);
|
||||||
|
}
|
||||||
|
|
||||||
if (outputMasks) {
|
if (outputMasks) {
|
||||||
Console.WriteLine("Writing masks...");
|
Console.WriteLine("Writing masks...");
|
||||||
for (int i = 0; i < TransitionMasks.Count; i++) {
|
for (int i = 0; i < TransitionMasks.Count; i++) {
|
||||||
var mask = new MagickImage(MagickColors.Black, (uint)TransitionMasks[i].Mask.GetLength(0), (uint)TransitionMasks[i].Mask.GetLength(1));
|
var mask = new MagickImage(MagickColors.Black, (uint)TransitionMasks[i].Mask.GetLength(0),
|
||||||
|
(uint)TransitionMasks[i].Mask.GetLength(1));
|
||||||
mask.GetPixels().SetPixels(TransitionMasks[i].Mask.ToFloatArray());
|
mask.GetPixels().SetPixels(TransitionMasks[i].Mask.ToFloatArray());
|
||||||
mask.Write($"mask{i}.png", MagickFormat.Png24);
|
mask.Write($"mask{i}.png", MagickFormat.Png24);
|
||||||
}
|
}
|
||||||
@@ -151,7 +155,8 @@ public class Program {
|
|||||||
var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]);
|
var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]);
|
||||||
Gradients.Add(gradientData);
|
Gradients.Add(gradientData);
|
||||||
if (!outputGradients) continue;
|
if (!outputGradients) continue;
|
||||||
var gradient = new MagickImage(MagickColors.Black, (uint)TransitionMasks[i].Mask.GetLength(0), (uint)TransitionMasks[i].Mask.GetLength(1));
|
var gradient = new MagickImage(MagickColors.Black, (uint)TransitionMasks[i].Mask.GetLength(0),
|
||||||
|
(uint)TransitionMasks[i].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);
|
||||||
}
|
}
|
||||||
@@ -186,7 +191,8 @@ public class Program {
|
|||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
sw.Start();
|
sw.Start();
|
||||||
Console.WriteLine("Running edge detection...");
|
Console.WriteLine("Running edge detection...");
|
||||||
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);
|
||||||
|
|
||||||
@@ -197,11 +203,13 @@ public class Program {
|
|||||||
lock (maskData.Edges) maskData.Edges.Add(new(x, y));
|
lock (maskData.Edges) maskData.Edges.Add(new(x, y));
|
||||||
iterCount++;
|
iterCount++;
|
||||||
if (iterCount % (width * height / 100) == 0) {
|
if (iterCount % (width * height / 100) == 0) {
|
||||||
ConsoleUpdateLine($"Progress: {iterCount/(float)(width*height):P} | {iterCount/(sw.Elapsed.TotalSeconds):N0} pixels/s");
|
ConsoleUpdateLine(
|
||||||
|
$"Progress: {iterCount / (float)(width * height):P} | {iterCount / (sw.Elapsed.TotalSeconds):N0} pixels/s");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Console.WriteLine($"Edge pixels: {maskData.Edges.Count} | {maskData.Edges.Count/sw.ElapsedMilliseconds} pixels/s\n Time: {sw.Elapsed.TotalSeconds:F4}s");
|
Console.WriteLine(
|
||||||
|
$"\nEdge pixels: {maskData.Edges.Count} | {maskData.Edges.Count / sw.ElapsedMilliseconds} pixels/s\n Time: {sw.Elapsed.TotalSeconds:F4}s");
|
||||||
}
|
}
|
||||||
|
|
||||||
static float3[,] Gradient(TransitionMaskData mask, SDFData sdfA, SDFData sdfB) {
|
static float3[,] Gradient(TransitionMaskData mask, SDFData sdfA, SDFData sdfB) {
|
||||||
@@ -216,7 +224,8 @@ public class Program {
|
|||||||
var max = MIN;
|
var max = MIN;
|
||||||
|
|
||||||
Console.WriteLine("Running gradient generation...");
|
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);
|
||||||
|
|
||||||
@@ -232,7 +241,8 @@ public class Program {
|
|||||||
if (gradient > max) max = 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)");
|
||||||
Console.WriteLine($"Min: {min} | Max: {max}");
|
Console.WriteLine($"Min: {min} | Max: {max}");
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
@@ -245,7 +255,8 @@ public class Program {
|
|||||||
int iterCount = 0;
|
int iterCount = 0;
|
||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
sw.Start();
|
sw.Start();
|
||||||
Parallel.For(0, width * height, parallelOptions, (i) => {
|
Parallel.For(0, width * height, parallelOptions, (i) =>
|
||||||
|
{
|
||||||
//convert 1D index to 2D index
|
//convert 1D index to 2D index
|
||||||
var x = (int)(i % width);
|
var x = (int)(i % width);
|
||||||
var y = (int)(i / width);
|
var y = (int)(i / width);
|
||||||
@@ -263,13 +274,16 @@ public class Program {
|
|||||||
if (minDist > AbsMax) AbsMax = minDist;
|
if (minDist > AbsMax) AbsMax = minDist;
|
||||||
iterCount++;
|
iterCount++;
|
||||||
if (iterCount % (width * height / 100) == 0) {
|
if (iterCount % (width * height / 100) == 0) {
|
||||||
ConsoleUpdateLine($"Progress: {iterCount/(width*height):P}% | {iterCount/(sw.Elapsed.TotalSeconds):N0} pixels/s");
|
ConsoleUpdateLine(
|
||||||
|
$"Progress: {iterCount / (float)(width * height):P}% | {iterCount / (sw.Elapsed.TotalSeconds):N0} pixels/s");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Console.WriteLine($"SDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount/sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
Console.WriteLine(
|
||||||
|
$"\nSDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount / sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
Parallel.For(0, width * height, parallelOptions, (i) => {
|
Parallel.For(0, width * height, parallelOptions, (i) =>
|
||||||
|
{
|
||||||
//convert 1D index to 2D index
|
//convert 1D index to 2D index
|
||||||
var x = (int)(i % width);
|
var x = (int)(i % width);
|
||||||
var y = (int)(i / width);
|
var y = (int)(i / width);
|
||||||
@@ -277,7 +291,8 @@ public class Program {
|
|||||||
});
|
});
|
||||||
sw.Stop();
|
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);
|
||||||
}
|
}
|
||||||
@@ -298,7 +313,8 @@ public class Program {
|
|||||||
|
|
||||||
static float3[,] GetABMask(float3[,] A, float3[,] B, uint resX, uint resY) {
|
static float3[,] GetABMask(float3[,] A, float3[,] B, uint resX, uint resY) {
|
||||||
var temp = new float3[resX, resY];
|
var temp = new float3[resX, resY];
|
||||||
Parallel.For(0, resX*resY, parallelOptions, (i) => {
|
Parallel.For(0, resX * resY, parallelOptions, (i) =>
|
||||||
|
{
|
||||||
uint x = (uint)(i % resX);
|
uint x = (uint)(i % resX);
|
||||||
uint y = (uint)(i / resX);
|
uint y = (uint)(i / resX);
|
||||||
var pixelA = A[x, y];
|
var pixelA = A[x, y];
|
||||||
@@ -313,7 +329,8 @@ public class Program {
|
|||||||
|
|
||||||
static float3[,] SelfMask(float3[,] A, uint resX, uint resY) {
|
static float3[,] SelfMask(float3[,] A, uint resX, uint resY) {
|
||||||
var temp = new float3[resX, resY];
|
var temp = new float3[resX, resY];
|
||||||
Parallel.For(0, resX*resY, parallelOptions, (i) => {
|
Parallel.For(0, resX * resY, parallelOptions, (i) =>
|
||||||
|
{
|
||||||
uint x = (uint)(i % resX);
|
uint x = (uint)(i % resX);
|
||||||
uint y = (uint)(i / resX);
|
uint y = (uint)(i / resX);
|
||||||
var pixelA = A[x, y];
|
var pixelA = A[x, y];
|
||||||
|
|||||||
Reference in New Issue
Block a user