diff --git a/Atomorph.NET.Demo/Program.cs b/Atomorph.NET.Demo/Program.cs index e8e03b9..676a0b0 100644 --- a/Atomorph.NET.Demo/Program.cs +++ b/Atomorph.NET.Demo/Program.cs @@ -30,7 +30,7 @@ public static class Program morph.SetDensity(2); // Higher than 1 sets fluid to 0. morph.SetMotion(InterpolationMethod.Spline); morph.SetFading(InterpolationMethod.Perlin); - morph.SetThreads(80); + morph.SetThreads(16); morph.SetCycleLength(10000); morph.SetFeather(0); morph.SetKeepBackground(false); @@ -48,7 +48,7 @@ public static class Program if (options.Verbose) { 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; @@ -150,6 +150,7 @@ public static class Program ulong match_blobs = 0; ulong morph_atoms = 0; var fs = options.Files.Count; + var stepsWithoutEnergyChange = 0; while (true) { @@ -187,9 +188,9 @@ public static class Program { double e = morph.GetEnergy(); if (last_energy < e) - Console.WriteLine($"Matching blobs, absolute energy was {e:30.10f}."); + Console.WriteLine($"Matching blobs, absolute energy was {e:F2}."); 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; } if (++match_blobs >= options.MatchTime) @@ -200,15 +201,27 @@ public static class Program } else if (morph_state == MorphState.AtomMorphing) { + double e = morph.GetEnergy(); + if (options.Verbose) { - double e = morph.GetEnergy(); if (last_energy < e) - Console.WriteLine($"Matching atoms, absolute energy was {e:30.10f}."); + Console.WriteLine($"Matching atoms, absolute energy was {e:F2}."); else - Console.WriteLine($"Matching atoms, energy decreased by {last_energy - e:30.10f}."); - last_energy = e; + Console.WriteLine( + $"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) { morph.NextState(); @@ -233,7 +246,7 @@ public static class Program 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; diff --git a/Atomorph.NET/AtomorphThread.cs b/Atomorph.NET/AtomorphThread.cs index 6044cfd..8b64292 100644 --- a/Atomorph.NET/AtomorphThread.cs +++ b/Atomorph.NET/AtomorphThread.cs @@ -553,12 +553,14 @@ public class AtomorphThread : IDisposable if (x2 >= x1) x2++; - var x1_y_prev = map[x1][y_prev]; - var x2_y_prev = map[x2][y_prev]; - var x1_y = map[x1][y]; - var x2_y = map[x2][y]; - var x1_y_next = map[x1][y_next]; - var x2_y_next = map[x2][y_next]; + var x1Row = map[x1]; + var x2Row = map[x2]; + var x1_y_prev = x1Row[y_prev]; + var x2_y_prev = x2Row[y_prev]; + var x1_y = x1Row[y]; + 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) + AtomorphUtilities.PointDistance(x1_y, x1_y_next); @@ -576,9 +578,9 @@ public class AtomorphThread : IDisposable { 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; - (map[x2][y], map[x1][y]) = (map[x1][y], map[x2][y]); + (x2Row[y], x1Row[y]) = (x1Row[y], x2Row[y]); gain.Gain += c1 - c2; } } @@ -675,9 +677,9 @@ public class AtomorphThread : IDisposable f.Blobs.RandomShuffle(_random); 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; @@ -728,9 +730,8 @@ public class AtomorphThread : IDisposable foreach (var it in bl.Border) { pos = it; - var ownerPos = f.Owners[pos]; - if (!HasPixel(frameKey, pos) || bl.Surface.Contains(pos) || !f.Owners.ContainsKey(pos) || - checkedBlobs.Contains(ownerPos)) + if (bl.Surface.Contains(pos) || !f.Owners.TryGetValue(pos, out var ownerPos) || checkedBlobs.Contains(ownerPos) || + !HasPixel(frameKey, pos)) { // Don't expand into itself and empty positions. if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize) @@ -869,6 +870,8 @@ public class AtomorphThread : IDisposable if (_blobBoxSamples == 0 || blobs_before <= _blobNumber) return true; Again: + ubl = null; + best = null; samples = 0; NotEnoughSamples: diff --git a/Atomorph.NET/FluidModel/FluidModel.cs b/Atomorph.NET/FluidModel/FluidModel.cs index 2b50ccf..2242190 100644 --- a/Atomorph.NET/FluidModel/FluidModel.cs +++ b/Atomorph.NET/FluidModel/FluidModel.cs @@ -86,7 +86,7 @@ internal class FluidModel : IDisposable _mxPrev = _mx; _myPrev = _my; foreach (var node in _active) - node.Clear(); + node?.Clear(); _activeCount = 0; double phi; diff --git a/Atomorph.NET/Morph.cs b/Atomorph.NET/Morph.cs index 1dfc13d..c2bdc62 100644 --- a/Atomorph.NET/Morph.cs +++ b/Atomorph.NET/Morph.cs @@ -1194,21 +1194,21 @@ public class Morph : IDisposable if (weight_x1y1 > 0.0) { - colors[pos].Add(c); - weights[pos].Add(weight_x1y1); + colors.AddToDictionaryList(pos, c); + weights.AddToDictionaryList(pos, weight_x1y1); } if ((x < _bBoxX2 || x + 1 < _width) && weight_x2y1 > 0.0) { pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), y); - colors[pos].Add(c); - weights[pos].Add(weight_x2y1); + colors.AddToDictionaryList(pos, c); + weights.AddToDictionaryList(pos, weight_x2y1); } if ((y < _bBoxY2 || y + 1 < _height) && weight_x1y2 > 0.0) { pos = AtomorphUtilities.XY2Pos(x, (ushort)(y + 1)); - colors[pos].Add(c); - weights[pos].Add(weight_x1y2); + colors.AddToDictionaryList(pos, c); + weights.AddToDictionaryList(pos, weight_x1y2); } if (weight_x2y2 > 0.0) @@ -1216,8 +1216,8 @@ public class Morph : IDisposable if ((y < _bBoxY2 && x < _bBoxX2) || (y + 1 < _height && x + 1 < _width)) { pos = AtomorphUtilities.XY2Pos((ushort)(x + 1), (ushort)(y + 1)); - colors[pos].Add(c); - weights[pos].Add(weight_x2y2); + colors.AddToDictionaryList(pos, c); + weights.AddToDictionaryList(pos, weight_x2y2); } } } @@ -1446,8 +1446,8 @@ public class Morph : IDisposable { // Map this particle as an existing particle. active_count++; - particles_by_destination[p.DestinationPos].Add(p); - particles_by_source[p.SourcePos].Add(p); + particles_by_destination.AddToDictionaryList(p.DestinationPos, p); + particles_by_source.AddToDictionaryList(p.SourcePos, p); update_particles.Add(p); } else @@ -1501,7 +1501,7 @@ public class Morph : IDisposable } p.SourcePos = pos; - particles_by_source[pos].Add(p); + particles_by_source.AddToDictionaryList(pos, p); source_found = true; { @@ -1512,7 +1512,7 @@ public class Morph : IDisposable var pt2 = points[x][y_next]; ulong destination_pos = AtomorphUtilities.XY2Pos(pt2.X, pt2.Y); 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. p.DestinationPos = pos; - particles_by_destination[pos].Add(p); + particles_by_destination.AddToDictionaryList(pos, p); destination_found = true; { @@ -1546,7 +1546,7 @@ public class Morph : IDisposable var pt1 = points[x][y]; ulong source_pos = AtomorphUtilities.XY2Pos(pt1.X, pt1.Y); p.SourcePos = source_pos; - particles_by_source[source_pos].Add(p); + particles_by_source.AddToDictionaryList(source_pos, p); source_found = true; } @@ -1559,7 +1559,7 @@ public class Morph : IDisposable ulong pos = p.SourcePos; // 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, // copy its location for seamless creation.