Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions Obj2Tiles.Library.Test/BoundsTests.cs
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();
}
}
152 changes: 152 additions & 0 deletions Obj2Tiles.Library.Test/MtlParsingTests.cs
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.
Comment thread
HeDo88TH marked this conversation as resolved.
Outdated
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);
}
}
}
57 changes: 57 additions & 0 deletions Obj2Tiles.Library.Test/Obj2Tiles.Library.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,63 @@
<None Update="TestData\cube-colors-textured\pic1.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-normals.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-vt-vn.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-vt-vn.mtl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\quad.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\ngon.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\degenerate-faces.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\empty.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\only-vertices.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\comments-and-blanks.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\scientific-notation.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-uv-negative.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-uv-negative.mtl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\triangle-with-lines.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\multi-material.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\multi-material.mtl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\cube-3d.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\cube-colors-3d.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\mtllib-empty.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\mtllib-missing.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading
Loading