-
Notifications
You must be signed in to change notification settings - Fork 90
Various fixes and improvements #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27e62c9
Updated publish profiles
HeDo88TH 5818581
Improved test suite + fixes
HeDo88TH e1faedb
Improved test
HeDo88TH 9874d57
Minor improvements
HeDo88TH 205f0db
Fixed warnings
HeDo88TH ddfd885
Fixed orphaned usemtl and kd-only materials
HeDo88TH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| using System.IO; | ||
| using NUnit.Framework; | ||
| using Obj2Tiles.Library.Geometry; | ||
| using Shouldly; | ||
|
|
||
| namespace Obj2Tiles.Library.Test; | ||
|
|
||
| /// <summary> | ||
| /// Tests for Box3 bounding box and mesh Bounds calculation. | ||
| /// </summary> | ||
| public class BoundsTests | ||
| { | ||
| private const string TestDataPath = "TestData"; | ||
|
|
||
| [Test] | ||
| public void Bounds_Triangle_Correct() | ||
| { | ||
| var mesh = MeshUtils.LoadMesh(Path.Combine(TestDataPath, "triangle.obj")); | ||
| var bounds = mesh.Bounds; | ||
|
|
||
| bounds.Min.X.ShouldBe(0.0); | ||
| bounds.Min.Y.ShouldBe(0.0); | ||
| bounds.Min.Z.ShouldBe(0.0); | ||
| bounds.Max.X.ShouldBe(1.0); | ||
| bounds.Max.Y.ShouldBe(1.0); | ||
| bounds.Max.Z.ShouldBe(0.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Bounds_Cube3D_SymmetricAroundOrigin() | ||
| { | ||
| var mesh = MeshUtils.LoadMesh(Path.Combine(TestDataPath, "cube-3d.obj")); | ||
| var bounds = mesh.Bounds; | ||
|
|
||
| bounds.Min.X.ShouldBe(-1.0); | ||
| bounds.Min.Y.ShouldBe(-1.0); | ||
| bounds.Min.Z.ShouldBe(-1.0); | ||
| bounds.Max.X.ShouldBe(1.0); | ||
| bounds.Max.Y.ShouldBe(1.0); | ||
| bounds.Max.Z.ShouldBe(1.0); | ||
|
|
||
| bounds.Width.ShouldBe(2.0); | ||
| bounds.Height.ShouldBe(2.0); | ||
| bounds.Depth.ShouldBe(2.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Bounds_Center_IsCorrect() | ||
| { | ||
| var mesh = MeshUtils.LoadMesh(Path.Combine(TestDataPath, "cube-3d.obj")); | ||
| var center = mesh.Bounds.Center; | ||
|
|
||
| center.X.ShouldBe(0.0); | ||
| center.Y.ShouldBe(0.0); | ||
| center.Z.ShouldBe(0.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Bounds_AfterSplit_SubsetsOfOriginal() | ||
| { | ||
| var mesh = MeshUtils.LoadMesh(Path.Combine(TestDataPath, "cube-3d.obj")); | ||
| var originalBounds = mesh.Bounds; | ||
| var xutils = new VertexUtilsX(); | ||
|
|
||
| mesh.Split(xutils, 0.0, out var left, out var right); | ||
|
|
||
| var lb = left.Bounds; | ||
| lb.Min.X.ShouldBeGreaterThanOrEqualTo(originalBounds.Min.X - 1e-9); | ||
| lb.Max.X.ShouldBeLessThanOrEqualTo(0.0 + 1e-9); | ||
|
|
||
| var rb = right.Bounds; | ||
| rb.Min.X.ShouldBeGreaterThanOrEqualTo(0.0 - 1e-9); | ||
| rb.Max.X.ShouldBeLessThanOrEqualTo(originalBounds.Max.X + 1e-9); | ||
| } | ||
|
|
||
| // --- Box3 Split --- | ||
|
|
||
| [Test] | ||
| public void Box3_SplitX_CoversFull() | ||
| { | ||
| var box = new Box3(-1, -1, -1, 1, 1, 1); | ||
| var halves = box.Split(Axis.X); | ||
|
|
||
| halves.Length.ShouldBe(2); | ||
| halves[0].Min.X.ShouldBe(-1.0); | ||
| halves[0].Max.X.ShouldBe(0.0); | ||
| halves[1].Min.X.ShouldBe(0.0); | ||
| halves[1].Max.X.ShouldBe(1.0); | ||
|
|
||
| // Y and Z unchanged | ||
| halves[0].Min.Y.ShouldBe(-1.0); | ||
| halves[0].Max.Y.ShouldBe(1.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Box3_SplitY_CoversFull() | ||
| { | ||
| var box = new Box3(-1, -1, -1, 1, 1, 1); | ||
| var halves = box.Split(Axis.Y); | ||
|
|
||
| halves[0].Max.Y.ShouldBe(0.0); | ||
| halves[1].Min.Y.ShouldBe(0.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Box3_SplitZ_CoversFull() | ||
| { | ||
| var box = new Box3(-1, -1, -1, 1, 1, 1); | ||
| var halves = box.Split(Axis.Z); | ||
|
|
||
| halves[0].Max.Z.ShouldBe(0.0); | ||
| halves[1].Min.Z.ShouldBe(0.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Box3_SplitAtPosition_Correct() | ||
| { | ||
| var box = new Box3(0, 0, 0, 10, 10, 10); | ||
| var halves = box.Split(Axis.X, 3.0); | ||
|
|
||
| halves[0].Max.X.ShouldBe(3.0); | ||
| halves[1].Min.X.ShouldBe(3.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Box3_Center_Calculation() | ||
| { | ||
| var box = new Box3(2, 4, 6, 8, 10, 12); | ||
| var center = box.Center; | ||
|
|
||
| center.X.ShouldBe(5.0); | ||
| center.Y.ShouldBe(7.0); | ||
| center.Z.ShouldBe(9.0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Box3_Equality() | ||
| { | ||
| var a = new Box3(0, 0, 0, 1, 1, 1); | ||
| var b = new Box3(0, 0, 0, 1, 1, 1); | ||
| var c = new Box3(0, 0, 0, 2, 1, 1); | ||
|
|
||
| (a == b).ShouldBeTrue(); | ||
| (a != c).ShouldBeTrue(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| using System.IO; | ||
| using System.Linq; | ||
| using NUnit.Framework; | ||
| using Obj2Tiles.Library.Materials; | ||
| using Shouldly; | ||
|
|
||
| namespace Obj2Tiles.Library.Test; | ||
|
|
||
| /// <summary> | ||
| /// Tests for Material.ReadMtl — the MTL parser. | ||
| /// Covers multi-material files, all property keywords, and edge cases. | ||
| /// </summary> | ||
| public class MtlParsingTests | ||
| { | ||
| private const string TestDataPath = "TestData"; | ||
|
|
||
| [Test] | ||
| public void ReadMtl_MultiMaterial_ParsesBothMaterials() | ||
| { | ||
| var materials = Material.ReadMtl(Path.Combine(TestDataPath, "multi-material.mtl"), out var deps); | ||
|
|
||
| materials.Length.ShouldBe(2); | ||
| materials[0].Name.ShouldBe("Red"); | ||
| materials[1].Name.ShouldBe("Blue"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadMtl_AllProperties_ParsedCorrectly() | ||
| { | ||
| var materials = Material.ReadMtl(Path.Combine(TestDataPath, "multi-material.mtl"), out _); | ||
|
|
||
| // Red material | ||
| var red = materials[0]; | ||
| red.AmbientColor.ShouldNotBeNull(); | ||
| red.AmbientColor!.R.ShouldBe(0.1, tolerance: 0.001); | ||
| red.DiffuseColor.ShouldNotBeNull(); | ||
| red.DiffuseColor!.R.ShouldBe(0.8, tolerance: 0.001); | ||
| red.SpecularColor.ShouldNotBeNull(); | ||
| red.SpecularExponent!.Value.ShouldBe(100.0); | ||
| red.Dissolve!.Value.ShouldBe(1.0); | ||
| red.IlluminationModel.ShouldBe(Materials.IlluminationModel.HighlightOn); | ||
|
|
||
| // Blue material | ||
| var blue = materials[1]; | ||
| blue.DiffuseColor.ShouldNotBeNull(); | ||
| blue.DiffuseColor!.B.ShouldBe(0.8, tolerance: 0.001); | ||
| blue.SpecularExponent!.Value.ShouldBe(50.0); | ||
| blue.Dissolve!.Value.ShouldBe(0.9, tolerance: 0.001); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadMtl_MaterialWithoutTexture_TextureIsNull() | ||
| { | ||
| var materials = Material.ReadMtl(Path.Combine(TestDataPath, "multi-material.mtl"), out var deps); | ||
|
|
||
| materials[0].Texture.ShouldBeNull(); | ||
| materials[1].Texture.ShouldBeNull(); | ||
| deps.Length.ShouldBe(0); // no texture files as dependencies | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadMtl_Tr_InvertsToDissolve() | ||
| { | ||
| // Create a temp MTL with Tr instead of d | ||
| var tempFile = Path.GetTempFileName(); | ||
| try | ||
| { | ||
| File.WriteAllText(tempFile, "newmtl TestMat\nTr 0.3\n"); | ||
| var materials = Material.ReadMtl(tempFile, out _); | ||
|
|
||
| materials.Length.ShouldBe(1); | ||
| materials[0].Dissolve!.Value.ShouldBe(0.7, tolerance: 0.001); // d = 1 - Tr | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadMtl_EmptyFile_ReturnsSingleEmptyMaterial() | ||
| { | ||
| // The parser always adds the last material on exit | ||
| var tempFile = Path.GetTempFileName(); | ||
| try | ||
| { | ||
| File.WriteAllText(tempFile, "newmtl Empty\n"); | ||
| var materials = Material.ReadMtl(tempFile, out _); | ||
|
|
||
| materials.Length.ShouldBe(1); | ||
| materials[0].Name.ShouldBe("Empty"); | ||
| materials[0].DiffuseColor.ShouldBeNull(); | ||
| materials[0].Texture.ShouldBeNull(); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadMtl_ToMtl_RoundTrip() | ||
| { | ||
| var materials = Material.ReadMtl(Path.Combine(TestDataPath, "multi-material.mtl"), out _); | ||
|
|
||
| // Write and re-read | ||
| var tempFile = Path.GetTempFileName(); | ||
| try | ||
| { | ||
| var content = string.Join("\n", materials.Select(m => m.ToMtl())); | ||
| File.WriteAllText(tempFile, content); | ||
|
|
||
| var reread = Material.ReadMtl(tempFile, out _); | ||
| reread.Length.ShouldBe(2); | ||
| reread[0].Name.ShouldBe("Red"); | ||
| reread[1].Name.ShouldBe("Blue"); | ||
| reread[0].SpecularExponent.ShouldBe(100.0); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
|
|
||
| // --- DiffuseColor-only material (Issue #36 context) --- | ||
|
|
||
| [Test] | ||
| public void ReadMtl_DiffuseColorOnly_KdParsed() | ||
| { | ||
| // Materials with only Kd (no map_Kd) should still have DiffuseColor set. | ||
| // BUG: Issue #36 — the Converter.cs ignores Kd when no texture is present, | ||
| // producing grey materials in glTF. This test verifies the MTL parser works correctly; | ||
| // the downstream glTF conversion bug is separate. | ||
| var tempFile = Path.GetTempFileName(); | ||
| try | ||
| { | ||
| File.WriteAllText(tempFile, "newmtl ColorOnly\nKd 0.8 0.2 0.3\n"); | ||
| var materials = Material.ReadMtl(tempFile, out _); | ||
|
|
||
| materials.Length.ShouldBe(1); | ||
| materials[0].DiffuseColor.ShouldNotBeNull(); | ||
| materials[0].DiffuseColor!.R.ShouldBe(0.8, tolerance: 0.001); | ||
| materials[0].DiffuseColor!.G.ShouldBe(0.2, tolerance: 0.001); | ||
| materials[0].DiffuseColor!.B.ShouldBe(0.3, tolerance: 0.001); | ||
| materials[0].Texture.ShouldBeNull(); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.