Tweaks and performance fixes

This commit is contained in:
Deukhoofd committed 2025-08-18 20:25:04 +02:00
1 parent 29ddcd8d74
commit ede4a90639
4 files changed
+54 -38

No files matched your search

+22 -9
View File
@@ -30,7 +30,7 @@ public static class Program
morph.SetDensity(2); // Higher than 1 sets fluid to 0. morph.SetDensity(2); // Higher than 1 sets fluid to 0.
morph.SetMotion(InterpolationMethod.Spline); morph.SetMotion(InterpolationMethod.Spline);
morph.SetFading(InterpolationMethod.Perlin); morph.SetFading(InterpolationMethod.Perlin);
morph.SetThreads(80); morph.SetThreads(16);
morph.SetCycleLength(10000); morph.SetCycleLength(10000);
morph.SetFeather(0); morph.SetFeather(0);
morph.SetKeepBackground(false); morph.SetKeepBackground(false);
@@ -48,7 +48,7 @@ public static class Program
if (options.Verbose) if (options.Verbose)
{ {
var duration = programEnd - programStart; var duration = programEnd - programStart;
Console.WriteLine($"Program completed in {duration.TotalSeconds:0.00} seconds."); Console.WriteLine($"Program completed in {duration.TotalSeconds:F2} seconds.");
} }
return 0; return 0;
@@ -150,6 +150,7 @@ public static class Program
ulong match_blobs = 0; ulong match_blobs = 0;
ulong morph_atoms = 0; ulong morph_atoms = 0;
var fs = options.Files.Count; var fs = options.Files.Count;
var stepsWithoutEnergyChange = 0;
while (true) while (true)
{ {
@@ -187,9 +188,9 @@ public static class Program
{ {
double e = morph.GetEnergy(); double e = morph.GetEnergy();
if (last_energy < e) if (last_energy < e)
Console.WriteLine($"Matching blobs, absolute energy was {e:30.10f}."); Console.WriteLine($"Matching blobs, absolute energy was {e:F2}.");
else else
Console.WriteLine($"Matching blobs, energy decreased by {last_energy - e:30.10f}."); Console.WriteLine($"Matching blobs, energy decreased by {last_energy - e:F2}.");
last_energy = e; last_energy = e;
} }
if (++match_blobs >= options.MatchTime) if (++match_blobs >= options.MatchTime)
@@ -200,15 +201,27 @@ public static class Program
} }
else if (morph_state == MorphState.AtomMorphing) else if (morph_state == MorphState.AtomMorphing)
{ {
double e = morph.GetEnergy();
if (options.Verbose) if (options.Verbose)
{ {
double e = morph.GetEnergy();
if (last_energy < e) if (last_energy < e)
Console.WriteLine($"Matching atoms, absolute energy was {e:30.10f}."); Console.WriteLine($"Matching atoms, absolute energy was {e:F2}.");
else else
Console.WriteLine($"Matching atoms, energy decreased by {last_energy - e:30.10f}."); Console.WriteLine(
last_energy = e; $"Matching atoms, energy decreased by {last_energy - e:F2}");
} }
if (last_energy == e)
{
stepsWithoutEnergyChange++;
if (stepsWithoutEnergyChange > 3)
{
if (options.Verbose)
Console.WriteLine("No energy change for 3 steps, moving to next state.");
morph.NextState();
}
}
last_energy = e;
if (++morph_atoms >= options.MorphTime) if (++morph_atoms >= options.MorphTime)
{ {
morph.NextState(); morph.NextState();
@@ -233,7 +246,7 @@ public static class Program
if (options.Verbose) if (options.Verbose)
{ {
Console.WriteLine("Frame %lu had the most blobs (%lu).\n", largest_frame, blob_count); Console.WriteLine($"Frame {largest_frame} had the most blobs {blob_count}.\n");
} }
return; return;
+16 -13
View File
@@ -553,12 +553,14 @@ public class AtomorphThread : IDisposable
if (x2 >= x1) if (x2 >= x1)
x2++; x2++;
var x1_y_prev = map[x1][y_prev]; var x1Row = map[x1];
var x2_y_prev = map[x2][y_prev]; var x2Row = map[x2];
var x1_y = map[x1][y]; var x1_y_prev = x1Row[y_prev];
var x2_y = map[x2][y]; var x2_y_prev = x2Row[y_prev];
var x1_y_next = map[x1][y_next]; var x1_y = x1Row[y];
var x2_y_next = map[x2][y_next]; var x2_y = x2Row[y];
var x1_y_next = x1Row[y_next];
var x2_y_next = x2Row[y_next];
double x1_e_before = AtomorphUtilities.PointDistance(x1_y_prev, x1_y) + double x1_e_before = AtomorphUtilities.PointDistance(x1_y_prev, x1_y) +
AtomorphUtilities.PointDistance(x1_y, x1_y_next); AtomorphUtilities.PointDistance(x1_y, x1_y_next);
@@ -576,9 +578,9 @@ public class AtomorphThread : IDisposable
{ {
lock (morph_mutex) lock (morph_mutex)
{ {
if (map[x1][y].Word != x1_y.Word || map[x2][y].Word != x2_y.Word) if (x1Row[y].Word != x1_y.Word || x2Row[y].Word != x2_y.Word)
continue; continue;
(map[x2][y], map[x1][y]) = (map[x1][y], map[x2][y]); (x2Row[y], x1Row[y]) = (x1Row[y], x2Row[y]);
gain.Gain += c1 - c2; gain.Gain += c1 - c2;
} }
} }
@@ -675,9 +677,9 @@ public class AtomorphThread : IDisposable
f.Blobs.RandomShuffle(_random); f.Blobs.RandomShuffle(_random);
sz = (ulong)f.Blobs.Count; sz = (ulong)f.Blobs.Count;
for (ushort bb = 0; bb < sz; ++bb) for (ulong bb = 0; bb < sz; ++bb)
{ {
f.Blobs[bb]!.Index = bb; f.Blobs[(int)bb]!.Index = bb;
} }
if (f.Blobs.Count == 0 || _blobMaxSize == 1) return true; if (f.Blobs.Count == 0 || _blobMaxSize == 1) return true;
@@ -728,9 +730,8 @@ public class AtomorphThread : IDisposable
foreach (var it in bl.Border) foreach (var it in bl.Border)
{ {
pos = it; pos = it;
var ownerPos = f.Owners[pos]; if (bl.Surface.Contains(pos) || !f.Owners.TryGetValue(pos, out var ownerPos) || checkedBlobs.Contains(ownerPos) ||
if (!HasPixel(frameKey, pos) || bl.Surface.Contains(pos) || !f.Owners.ContainsKey(pos) || !HasPixel(frameKey, pos))
checkedBlobs.Contains(ownerPos))
{ {
// Don't expand into itself and empty positions. // Don't expand into itself and empty positions.
if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize) if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize)
@@ -869,6 +870,8 @@ public class AtomorphThread : IDisposable
if (_blobBoxSamples == 0 || blobs_before <= _blobNumber) return true; if (_blobBoxSamples == 0 || blobs_before <= _blobNumber) return true;
Again: Again:
ubl = null;
best = null;
samples = 0; samples = 0;
NotEnoughSamples: NotEnoughSamples:
+1 -1
View File
@@ -86,7 +86,7 @@ internal class FluidModel : IDisposable
_mxPrev = _mx; _mxPrev = _mx;
_myPrev = _my; _myPrev = _my;
foreach (var node in _active) foreach (var node in _active)
node.Clear(); node?.Clear();
_activeCount = 0; _activeCount = 0;
double phi; double phi;
+15 -15
View File
@@ -1194,21 +1194,21 @@ public class Morph : IDisposable
if (weight_x1y1 > 0.0) if (weight_x1y1 > 0.0)
{ {
colors[pos].Add(c); colors.AddToDictionaryList(pos, c);
weights[pos].Add(weight_x1y1); weights.AddToDictionaryList(pos, weight_x1y1);
} }
if ((x < _bBoxX2 || x + 1 < _width) && weight_x2y1 > 0.0) if ((x < _bBoxX2 || x + 1 < _width) && weight_x2y1 > 0.0)
{ {
pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), y); pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), y);
colors[pos].Add(c); colors.AddToDictionaryList(pos, c);
weights[pos].Add(weight_x2y1); weights.AddToDictionaryList(pos, weight_x2y1);
} }
if ((y < _bBoxY2 || y + 1 < _height) && weight_x1y2 > 0.0) if ((y < _bBoxY2 || y + 1 < _height) && weight_x1y2 > 0.0)
{ {
pos = AtomorphUtilities.XY2Pos(x, (ushort)(y + 1)); pos = AtomorphUtilities.XY2Pos(x, (ushort)(y + 1));
colors[pos].Add(c); colors.AddToDictionaryList(pos, c);
weights[pos].Add(weight_x1y2); weights.AddToDictionaryList(pos, weight_x1y2);
} }
if (weight_x2y2 > 0.0) if (weight_x2y2 > 0.0)
@@ -1216,8 +1216,8 @@ public class Morph : IDisposable
if ((y < _bBoxY2 && x < _bBoxX2) || (y + 1 < _height && x + 1 < _width)) if ((y < _bBoxY2 && x < _bBoxX2) || (y + 1 < _height && x + 1 < _width))
{ {
pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), (ushort)(y + 1)); pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), (ushort)(y + 1));
colors[pos].Add(c); colors.AddToDictionaryList(pos, c);
weights[pos].Add(weight_x2y2); weights.AddToDictionaryList(pos, weight_x2y2);
} }
} }
} }
@@ -1446,8 +1446,8 @@ public class Morph : IDisposable
{ {
// Map this particle as an existing particle. // Map this particle as an existing particle.
active_count++; active_count++;
particles_by_destination[p.DestinationPos].Add(p); particles_by_destination.AddToDictionaryList(p.DestinationPos, p);
particles_by_source[p.SourcePos].Add(p); particles_by_source.AddToDictionaryList(p.SourcePos, p);
update_particles.Add(p); update_particles.Add(p);
} }
else else
@@ -1501,7 +1501,7 @@ public class Morph : IDisposable
} }
p.SourcePos = pos; p.SourcePos = pos;
particles_by_source[pos].Add(p); particles_by_source.AddToDictionaryList(pos, p);
source_found = true; source_found = true;
{ {
@@ -1512,7 +1512,7 @@ public class Morph : IDisposable
var pt2 = points[x][y_next]; var pt2 = points[x][y_next];
ulong destination_pos = AtomorphUtilities.XY2Pos(pt2.X, pt2.Y); ulong destination_pos = AtomorphUtilities.XY2Pos(pt2.X, pt2.Y);
p.DestinationPos = destination_pos; p.DestinationPos = destination_pos;
particles_by_destination[destination_pos].Add(p); particles_by_destination.AddToDictionaryList(destination_pos, p);
} }
} }
@@ -1535,7 +1535,7 @@ public class Morph : IDisposable
// Free destination was found, take it. // Free destination was found, take it.
p.DestinationPos = pos; p.DestinationPos = pos;
particles_by_destination[pos].Add(p); particles_by_destination.AddToDictionaryList(pos, p);
destination_found = true; destination_found = true;
{ {
@@ -1546,7 +1546,7 @@ public class Morph : IDisposable
var pt1 = points[x][y]; var pt1 = points[x][y];
ulong source_pos = AtomorphUtilities.XY2Pos(pt1.X, pt1.Y); ulong source_pos = AtomorphUtilities.XY2Pos(pt1.X, pt1.Y);
p.SourcePos = source_pos; p.SourcePos = source_pos;
particles_by_source[source_pos].Add(p); particles_by_source.AddToDictionaryList(source_pos, p);
source_found = true; source_found = true;
} }
@@ -1559,7 +1559,7 @@ public class Morph : IDisposable
ulong pos = p.SourcePos; ulong pos = p.SourcePos;
// Find a suitable starting position for this particle. // Find a suitable starting position for this particle.
if (particles_by_source.TryGetValue(pos, out var prt) && prt.Any()) if (particles_by_source.TryGetValue(pos, out var prt) && prt.Count > 1)
{ {
// Another particle exists with the same source, // Another particle exists with the same source,
// copy its location for seamless creation. // copy its location for seamless creation.