From 45e4d8daa005b001a0f17b92ff773253097cafe1 Mon Sep 17 00:00:00 2001 From: unitycoder Date: Mon, 30 Jun 2025 11:12:51 +0300 Subject: [PATCH 1/2] test memory optimization for randomize points --- Tools/Tools.cs | 63 ++++++++++++++++++++++++++++++++++------------- Writers/PCROOT.cs | 47 ++++++++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/Tools/Tools.cs b/Tools/Tools.cs index 31c95e6..0771f34 100644 --- a/Tools/Tools.cs +++ b/Tools/Tools.cs @@ -140,33 +140,62 @@ public static Vector2 SuperUnpacker(float f, float GridSizeAndPackMagic) // return (ax * bx + ay * by + az * bz); //} - public static void ShuffleInPlace(params IList[] arrays) - { - //ResetRandom(); + //public static void ShuffleInPlace(params IList[] arrays) + //{ + // //ResetRandom(); + + // // Assume all lists are the same length + // if (arrays.Length == 0 || arrays[0] == null) + // return; + + // int count = arrays[0].Count; - // Assume all lists are the same length - if (arrays.Length == 0 || arrays[0] == null) - return; + // for (int i = count - 1; i > 0; i--) + // { + // int rand = frnd.Next(0, i + 1); - int count = arrays[0].Count; + // foreach (var list in arrays) + // { + // if (list == null || list.Count <= i || list.Count <= rand) + // continue; + // object temp = list[i]; + // list[i] = list[rand]; + // list[rand] = temp; + // } + // } + //} + + public static void ShuffleInPlaceDynamic(List arrays, int count) + { + foreach (var list in arrays) + { + Type type = list.GetType().GetGenericArguments()[0]; + + if (type == typeof(float)) + ShuffleInPlace((List)list, count); + else if (type == typeof(ushort)) + ShuffleInPlace((List)list, count); + else if (type == typeof(byte)) + ShuffleInPlace((List)list, count); + else if (type == typeof(double)) + ShuffleInPlace((List)list, count); + else + throw new NotSupportedException($"Shuffle not supported for {type}"); + } + } + + private static void ShuffleInPlace(List list, int count) + { for (int i = count - 1; i > 0; i--) { int rand = frnd.Next(0, i + 1); - - foreach (var list in arrays) - { - if (list == null || list.Count <= i || list.Count <= rand) - continue; - - object temp = list[i]; - list[i] = list[rand]; - list[rand] = temp; - } + (list[i], list[rand]) = (list[rand], list[i]); } } + public static void Shuffle(ref List array) { ResetRandom(); diff --git a/Writers/PCROOT.cs b/Writers/PCROOT.cs index de7e500..3d33646 100644 --- a/Writers/PCROOT.cs +++ b/Writers/PCROOT.cs @@ -463,6 +463,9 @@ unsafe void IntToBytes(int value, byte[] buffer, int offset) } } + private readonly List _shuffleListBuffer = new(4096*4); + + private readonly List[] _tempArray = new List[4096 * 4]; // returns list of saved files void IWriter.Save(int fileIndex) @@ -536,31 +539,57 @@ void IWriter.Save(int fileIndex) } // randomize points in this node + //if (importSettings.randomize) + //{ + // var listsToShuffle = new List { nodeTempX, nodeTempY, nodeTempZ }; + + // if (importSettings.importRGB) + // { + // listsToShuffle.Add(nodeTempR); + // listsToShuffle.Add(nodeTempG); + // listsToShuffle.Add(nodeTempB); + // } + + // if (importSettings.importIntensity) + // listsToShuffle.Add(nodeTempIntensity); + + // if (importSettings.importClassification) + // listsToShuffle.Add(nodeTempClassification); + + // if (importSettings.averageTimestamp) + // listsToShuffle.Add(nodeTempTime); + + // Tools.ShuffleInPlace(listsToShuffle.ToArray()); + //} + if (importSettings.randomize) { - var listsToShuffle = new List { nodeTempX, nodeTempY, nodeTempZ }; + _shuffleListBuffer.Clear(); + _shuffleListBuffer.Add(nodeTempX); + _shuffleListBuffer.Add(nodeTempY); + _shuffleListBuffer.Add(nodeTempZ); if (importSettings.importRGB) { - listsToShuffle.Add(nodeTempR); - listsToShuffle.Add(nodeTempG); - listsToShuffle.Add(nodeTempB); + _shuffleListBuffer.Add(nodeTempR); + _shuffleListBuffer.Add(nodeTempG); + _shuffleListBuffer.Add(nodeTempB); } if (importSettings.importIntensity) - listsToShuffle.Add(nodeTempIntensity); + _shuffleListBuffer.Add(nodeTempIntensity); if (importSettings.importClassification) - listsToShuffle.Add(nodeTempClassification); + _shuffleListBuffer.Add(nodeTempClassification); if (importSettings.averageTimestamp) - listsToShuffle.Add(nodeTempTime); - - Tools.ShuffleInPlace(listsToShuffle.ToArray()); + _shuffleListBuffer.Add(nodeTempTime); + Tools.ShuffleInPlaceDynamic(_shuffleListBuffer, nodeTempX.Count); } + // get this node bounds, TODO but we know node(grid cell) x,y,z values? float minX = float.PositiveInfinity; float minY = float.PositiveInfinity; From 11c120d64a2ff30fd23f9eeba03659e5d2c18135 Mon Sep 17 00:00:00 2001 From: unitycoder Date: Mon, 30 Jun 2025 11:20:01 +0300 Subject: [PATCH 2/2] version --- MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index c13d315..83840c3 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -29,7 +29,7 @@ namespace PointCloudConverter { public partial class MainWindow : Window { - static readonly string version = "17.05.2025"; + static readonly string version = "30.06.2025"; static readonly string appname = "PointCloud Converter - " + version; static readonly string rootFolder = AppDomain.CurrentDomain.BaseDirectory;