diff --git a/.idea/.idea.Obj2Tiles/.idea/inspectionProfiles/Project_Default.xml b/.idea/.idea.Obj2Tiles/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ee4ca4b --- /dev/null +++ b/.idea/.idea.Obj2Tiles/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/Obj2Tiles.Library.Test/Obj2Tiles.Library.Test.csproj b/Obj2Tiles.Library.Test/Obj2Tiles.Library.Test.csproj index 20bc2b6..7c2adfb 100644 --- a/Obj2Tiles.Library.Test/Obj2Tiles.Library.Test.csproj +++ b/Obj2Tiles.Library.Test/Obj2Tiles.Library.Test.csproj @@ -8,15 +8,15 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Obj2Tiles.Library/Algos/MaxRectanglesBinPack.cs b/Obj2Tiles.Library/Algos/MaxRectanglesBinPack.cs index 441ba33..b55c2f1 100644 --- a/Obj2Tiles.Library/Algos/MaxRectanglesBinPack.cs +++ b/Obj2Tiles.Library/Algos/MaxRectanglesBinPack.cs @@ -26,8 +26,8 @@ public class MaxRectanglesBinPack public int binHeight = 0; public bool allowRotations; - public readonly List usedRectangles = new(); - public readonly List freeRectangles = new(); + public readonly List usedRectangles = []; + public readonly List freeRectangles = []; public MaxRectanglesBinPack(int width, int height, bool rotations = true) @@ -311,7 +311,6 @@ private Rectangle FindPositionForNewNodeBestAreaFit(int width, int height, out i ref int bestShortSideFit) { var bestNode = new Rectangle(); - //memset(&bestNode, 0, sizeof(Rectangle)); bestAreaFit = int.MaxValue; @@ -391,7 +390,6 @@ private int ContactPointScoreNode(int x, int y, int width, int height) private Rectangle FindPositionForNewNodeContactPoint(int width, int height, out int bestContactScore) { var bestNode = new Rectangle(); - //memset(&bestNode, 0, sizeof(Rectangle)); bestContactScore = -1; diff --git a/Obj2Tiles.Library/Common.cs b/Obj2Tiles.Library/Common.cs index 33bd5a6..6e3ee0e 100644 --- a/Obj2Tiles.Library/Common.cs +++ b/Obj2Tiles.Library/Common.cs @@ -7,7 +7,7 @@ namespace Obj2Tiles.Library; public static class Common { - public static double Epsilon = double.Epsilon * 10; + public static readonly double Epsilon = double.Epsilon * 10; public static void CopyImage(Image sourceImage, Image dest, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int destX, int destY) { diff --git a/Obj2Tiles.Library/Extenders.cs b/Obj2Tiles.Library/Extenders.cs index 26133a9..7d2861b 100644 --- a/Obj2Tiles.Library/Extenders.cs +++ b/Obj2Tiles.Library/Extenders.cs @@ -10,15 +10,14 @@ public static int AddIndex(this ICollection collection, T item) public static int AddIndex(this IDictionary dictionary, T item) { - // If the item is not already in the dictionary, add it and return the index - if (!dictionary.ContainsKey(item)) - { - dictionary.Add(item, dictionary.Count); - return dictionary.Count - 1; - } // If the item is already in the dictionary, return the index - return dictionary[item]; + if (dictionary.TryGetValue(item, out var index)) + return index; + + // If the item is not in the dictionary, add it and return the index + dictionary.Add(item, dictionary.Count); + return dictionary.Count - 1; } } \ No newline at end of file diff --git a/Obj2Tiles.Library/Geometry/MeshT.cs b/Obj2Tiles.Library/Geometry/MeshT.cs index 773367d..52cef72 100644 --- a/Obj2Tiles.Library/Geometry/MeshT.cs +++ b/Obj2Tiles.Library/Geometry/MeshT.cs @@ -31,10 +31,10 @@ public class MeshT : IMesh public MeshT(IEnumerable vertices, IEnumerable textureVertices, IEnumerable faces, IEnumerable materials) { - _vertices = new List(vertices); - _textureVertices = new List(textureVertices); - _faces = new List(faces); - _materials = new List(materials); + _vertices = [..vertices]; + _textureVertices = [..textureVertices]; + _faces = [..faces]; + _materials = [..materials]; } public int Split(IVertexUtils utils, double q, out IMesh left, @@ -818,8 +818,8 @@ private static List> GetFacesClusters(IEnumerable facesIndexes, if (remainingFacesIndexes.Count == 0) break; // Let's continue with the next cluster - currentCluster = new List { remainingFacesIndexes[0] }; - currentClusterCache = new HashSet { remainingFacesIndexes[0] }; + currentCluster = [remainingFacesIndexes[0]]; + currentClusterCache = [remainingFacesIndexes[0]]; remainingFacesIndexes.RemoveAt(0); } @@ -847,7 +847,7 @@ private static Dictionary> GetFacesMapper(Dictionary()); + facesMapper.Add(faceIndex, []); for (var index = 0; index < edge.Value.Count; index++) { @@ -876,13 +876,13 @@ private Dictionary> GetEdgesMapper(IReadOnlyList facesIndex var e3 = new Edge(f.TextureIndexA, f.TextureIndexC); if (!edgesMapper.ContainsKey(e1)) - edgesMapper.Add(e1, new List()); + edgesMapper.Add(e1, []); if (!edgesMapper.ContainsKey(e2)) - edgesMapper.Add(e2, new List()); + edgesMapper.Add(e2, []); if (!edgesMapper.ContainsKey(e3)) - edgesMapper.Add(e3, new List()); + edgesMapper.Add(e3, []); edgesMapper[e1].Add(faceIndex); edgesMapper[e2].Add(faceIndex); @@ -991,7 +991,7 @@ public Vertex3 GetVertexBaricenter() public void WriteObj(string path, bool removeUnused = true) { - if (!_materials.Any() || !_textureVertices.Any()) + if (_materials.Count == 0 || _textureVertices.Count == 0) _WriteObjWithoutTexture(path, removeUnused); else _WriteObjWithTexture(path, removeUnused); @@ -1103,8 +1103,10 @@ private void _WriteObjWithTexture(string path, bool removeUnused = true) var materialsPath = Path.ChangeExtension(path, "mtl"); + var folderPath = Path.GetDirectoryName(path) ?? string.Empty; + if (TexturesStrategy == TexturesStrategy.Repack || TexturesStrategy == TexturesStrategy.RepackCompressed) - TrimTextures(Path.GetDirectoryName(path)); + TrimTextures(folderPath); using (var writer = new FormattingStreamWriter(path, CultureInfo.InvariantCulture)) { @@ -1156,43 +1158,48 @@ into g if (material.Texture != null) { - if (TexturesStrategy == TexturesStrategy.KeepOriginal) + switch (TexturesStrategy) { - var folder = Path.GetDirectoryName(path); + case TexturesStrategy.KeepOriginal: + { + var folder = Path.GetDirectoryName(path); - var textureFileName = - $"{Path.GetFileNameWithoutExtension(path)}-texture-{index}{Path.GetExtension(material.Texture)}"; + var textureFileName = + $"{Path.GetFileNameWithoutExtension(path)}-texture-{index}{Path.GetExtension(material.Texture)}"; - var newTexturePath = - folder != null ? Path.Combine(folder, textureFileName) : textureFileName; + var newTexturePath = + folder != null ? Path.Combine(folder, textureFileName) : textureFileName; - if (!File.Exists(newTexturePath)) - File.Copy(material.Texture, newTexturePath, true); + if (!File.Exists(newTexturePath)) + File.Copy(material.Texture, newTexturePath, true); - material.Texture = textureFileName; - } - else if (TexturesStrategy == TexturesStrategy.Compress) - { - var folder = Path.GetDirectoryName(path); + material.Texture = textureFileName; + break; + } + case TexturesStrategy.Compress: + { + var folder = Path.GetDirectoryName(path); - var textureFileName = - $"{Path.GetFileNameWithoutExtension(path)}-texture-{index}.jpg"; + var textureFileName = + $"{Path.GetFileNameWithoutExtension(path)}-texture-{index}.jpg"; - var newTexturePath = - folder != null ? Path.Combine(folder, textureFileName) : textureFileName; + var newTexturePath = + folder != null ? Path.Combine(folder, textureFileName) : textureFileName; - if (File.Exists(newTexturePath)) + if (File.Exists(newTexturePath)) - File.Delete(newTexturePath); + File.Delete(newTexturePath); - Console.WriteLine($" -> Compressing texture '{material.Texture}'"); + Console.WriteLine($" -> Compressing texture '{material.Texture}'"); - using (var image = Image.Load(material.Texture)) - { - image.SaveAsJpeg(newTexturePath, encoder); - } + using (var image = Image.Load(material.Texture)) + { + image.SaveAsJpeg(newTexturePath, encoder); + } - material.Texture = textureFileName; + material.Texture = textureFileName; + break; + } } } diff --git a/Obj2Tiles.Library/Geometry/MeshUtils.cs b/Obj2Tiles.Library/Geometry/MeshUtils.cs index 272c206..d10352d 100644 --- a/Obj2Tiles.Library/Geometry/MeshUtils.cs +++ b/Obj2Tiles.Library/Geometry/MeshUtils.cs @@ -138,7 +138,7 @@ public static IMesh LoadMesh(string fileName, out string[] dependencies) dependencies = deps.ToArray(); - return textureVertices.Any() + return textureVertices.Count != 0 ? new MeshT(vertices, textureVertices, facesT, materials) : new Mesh(vertices, faces); } diff --git a/Obj2Tiles.Library/Obj2Tiles.Library.csproj b/Obj2Tiles.Library/Obj2Tiles.Library.csproj index 816cc2d..fee2b4b 100644 --- a/Obj2Tiles.Library/Obj2Tiles.Library.csproj +++ b/Obj2Tiles.Library/Obj2Tiles.Library.csproj @@ -7,7 +7,7 @@ - + diff --git a/Obj2Tiles.Library/TexturesCache.cs b/Obj2Tiles.Library/TexturesCache.cs index a46229c..61ac4f6 100644 --- a/Obj2Tiles.Library/TexturesCache.cs +++ b/Obj2Tiles.Library/TexturesCache.cs @@ -6,29 +6,26 @@ namespace Obj2Tiles.Library; public static class TexturesCache { - private static readonly ConcurrentDictionary> _textures = new(); + private static readonly ConcurrentDictionary> Textures = new(); public static Image GetTexture(string textureName) { - if (!_textures.ContainsKey(textureName)) - { - var texture = Image.Load(textureName); - - _textures.TryAdd(textureName, texture); - - return texture; - - } - - return _textures[textureName]; + if (Textures.TryGetValue(textureName, out var txout)) + return txout; + + var texture = Image.Load(textureName); + Textures.TryAdd(textureName, texture); + + return texture; + } public static void Clear() { - foreach(var texture in _textures) + foreach(var texture in Textures) { texture.Value.Dispose(); } - _textures.Clear(); + Textures.Clear(); } } \ No newline at end of file diff --git a/Obj2Tiles.Test/Obj2Tiles.Test.csproj b/Obj2Tiles.Test/Obj2Tiles.Test.csproj index 95e6779..50da0d3 100644 --- a/Obj2Tiles.Test/Obj2Tiles.Test.csproj +++ b/Obj2Tiles.Test/Obj2Tiles.Test.csproj @@ -8,10 +8,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Obj2Tiles/Obj2Tiles.csproj b/Obj2Tiles/Obj2Tiles.csproj index 999b843..970e04b 100644 --- a/Obj2Tiles/Obj2Tiles.csproj +++ b/Obj2Tiles/Obj2Tiles.csproj @@ -6,9 +6,10 @@ enable enable false - 1.0.12 - 1.0.12 + 1.0.13 + 1.0.13 true + 1.0.13 diff --git a/Obj2Tiles/Options.cs b/Obj2Tiles/Options.cs index 926a274..5c83713 100644 --- a/Obj2Tiles/Options.cs +++ b/Obj2Tiles/Options.cs @@ -47,7 +47,7 @@ public sealed class Options [Option("keep-intermediate", Required = false, HelpText = "Keeps the intermediate files (do not cleanup)", Default = false)] public bool KeepIntermediateFiles { get; set; } - [Option('t', "y-up-to-z-up", Required = true, HelpText = "Convert the upward Y-axis to the upward Z-axis, which is used in some situations where the upward axis may be the Y-axis or the Z-axis after the obj is exported.", Default = true)] + [Option('t', "y-up-to-z-up", Required = false, HelpText = "Convert the upward Y-axis to the upward Z-axis, which is used in some situations where the upward axis may be the Y-axis or the Z-axis after the obj is exported.", Default = true)] public bool? YUpToZUp { get; set; } } diff --git a/Obj2Tiles/Tiles/B3dm.cs b/Obj2Tiles/Tiles/B3dm.cs index dd7e328..ba7dfdd 100644 --- a/Obj2Tiles/Tiles/B3dm.cs +++ b/Obj2Tiles/Tiles/B3dm.cs @@ -28,14 +28,14 @@ public B3dm(byte[] glb): this() public byte[] ToBytes() { - var header_length = 28; + const int headerLength = 28; - var featureTableJson = BufferPadding.AddPadding(FeatureTableJson, header_length); + var featureTableJson = BufferPadding.AddPadding(FeatureTableJson, headerLength); var batchTableJson = BufferPadding.AddPadding(BatchTableJson); var featureTableBinary = BufferPadding.AddPadding(FeatureTableBinary); var batchTableBinary = BufferPadding.AddPadding(BatchTableBinary); - B3dmHeader.ByteLength = GlbData.Length + header_length + featureTableJson.Length + Encoding.UTF8.GetByteCount(batchTableJson) + batchTableBinary.Length + FeatureTableBinary.Length; + B3dmHeader.ByteLength = GlbData.Length + headerLength + featureTableJson.Length + Encoding.UTF8.GetByteCount(batchTableJson) + batchTableBinary.Length + FeatureTableBinary.Length; B3dmHeader.FeatureTableJsonByteLength = featureTableJson.Length; B3dmHeader.BatchTableJsonByteLength = Encoding.UTF8.GetByteCount(batchTableJson); diff --git a/Obj2Tiles/Tiles/B3dmHeader.cs b/Obj2Tiles/Tiles/B3dmHeader.cs index fd70070..0f1089c 100644 --- a/Obj2Tiles/Tiles/B3dmHeader.cs +++ b/Obj2Tiles/Tiles/B3dmHeader.cs @@ -34,11 +34,7 @@ public B3dmHeader(BinaryReader reader) BatchTableBinaryByteLength = (int)reader.ReadUInt32(); } - public int Length { - get { - return 28 + FeatureTableJsonByteLength + FeatureTableBinaryByteLength + BatchTableJsonByteLength + BatchTableBinaryByteLength; - } - } + public int Length => 28 + FeatureTableJsonByteLength + FeatureTableBinaryByteLength + BatchTableJsonByteLength + BatchTableBinaryByteLength; public byte[] AsBinary() { @@ -64,7 +60,7 @@ public List Validate() { var res = new List(); - var headerByteLength = AsBinary().Count(); + var headerByteLength = AsBinary().Length; var featureTableJsonByteOffset = headerByteLength; var featureTableBinaryByteOffset = featureTableJsonByteOffset + FeatureTableJsonByteLength; var batchTableJsonByteOffset = featureTableBinaryByteOffset + FeatureTableBinaryByteLength; diff --git a/Obj2Tiles/Tiles/B3dmReader.cs b/Obj2Tiles/Tiles/B3dmReader.cs index c59ff33..4839101 100644 --- a/Obj2Tiles/Tiles/B3dmReader.cs +++ b/Obj2Tiles/Tiles/B3dmReader.cs @@ -4,7 +4,7 @@ namespace Obj2Tiles.Tiles { public static class B3dmReader { - public static Obj2Tiles.Tiles.B3dm ReadB3dm(BinaryReader reader) + public static B3dm ReadB3dm(BinaryReader reader) { var b3dmHeader = new B3dmHeader(reader); var featureTableJson = Encoding.UTF8.GetString(reader.ReadBytes(b3dmHeader.FeatureTableJsonByteLength)); @@ -15,7 +15,7 @@ public static Obj2Tiles.Tiles.B3dm ReadB3dm(BinaryReader reader) var glbLength = b3dmHeader.ByteLength - b3dmHeader.Length; var glbBuffer = reader.ReadBytes(glbLength); - var b3dm = new Obj2Tiles.Tiles.B3dm { + var b3dm = new B3dm { B3dmHeader = b3dmHeader, GlbData = glbBuffer, FeatureTableJson = featureTableJson, @@ -26,12 +26,11 @@ public static Obj2Tiles.Tiles.B3dm ReadB3dm(BinaryReader reader) return b3dm; } - public static Obj2Tiles.Tiles.B3dm ReadB3dm(Stream stream) + public static B3dm ReadB3dm(Stream stream) { - using (var reader = new BinaryReader(stream)) { - var b3dm = ReadB3dm(reader); - return b3dm; - } + using var reader = new BinaryReader(stream); + var b3dm = ReadB3dm(reader); + return b3dm; } } } \ No newline at end of file diff --git a/Obj2Tiles/Utils.cs b/Obj2Tiles/Utils.cs index 3e8f410..f0e5b64 100644 --- a/Obj2Tiles/Utils.cs +++ b/Obj2Tiles/Utils.cs @@ -29,7 +29,7 @@ public static IEnumerable GetObjDependencies(string objPath) return dependencies; } - private static IEnumerable GetMtlDependencies(string mtlPath) + private static IList GetMtlDependencies(string mtlPath) { var mtlFile = File.ReadAllLines(mtlPath); @@ -119,13 +119,18 @@ public static BoundingVolume ToBoundingVolume(this Box3 box) { return new BoundingVolume { - Box = new[] { box.Center.X, -box.Center.Z, box.Center.Y, box.Width / 2, 0, 0, 0, -box.Depth / 2, 0, 0, 0, box.Height / 2 } + Box = [ + box.Center.X, -box.Center.Z, box.Center.Y, + box.Width / 2, 0, 0, + 0, -box.Depth / 2, 0, + 0, 0, box.Height / 2 + ] }; } public static void CopyObjDependencies(string input, string output) { - var dependencies = Utils.GetObjDependencies(input); + var dependencies = GetObjDependencies(input); foreach (var dependency in dependencies) { @@ -141,11 +146,11 @@ public static void CopyObjDependencies(string input, string output) if (destFolder != null) Directory.CreateDirectory(destFolder); if (File.Exists(dependencyDestPath)) - { continue; - } - File.Copy(Path.Combine(Path.GetDirectoryName(input), dependency), dependencyDestPath, true); + var directoryName = Path.GetDirectoryName(input) ?? string.Empty; + + File.Copy(Path.Combine(directoryName, dependency), dependencyDestPath, true); Console.WriteLine($" -> Copied {dependency}"); }