Build portable planetary field runtime
This commit is contained in:
parent
15f27173dd
commit
42fef7cdd7
24 changed files with 2214 additions and 0 deletions
|
|
@ -65,6 +65,22 @@ public sealed class HlslMirrorTests
|
|||
Assert.Contains("max(length(value), CULTMATH_NORMALIZE_EPSILON)", include);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlslMirrorPublishesPlanetaryTopologyAndField()
|
||||
{
|
||||
var root = GetRepositoryRoot();
|
||||
var include = File.ReadAllText(Path.Combine(root, "shaders", "CultMath.hlsl"));
|
||||
var planetary = File.ReadAllText(Path.Combine(root, "shaders", "Planetary.hlsl"));
|
||||
Assert.Contains("Planetary.hlsl", include);
|
||||
Assert.Contains("cultmath_planetary_face_direction", planetary);
|
||||
Assert.Contains("cultmath_planetary_face_coordinate", planetary);
|
||||
Assert.Contains("cultmath_planetary_page_local", planetary);
|
||||
Assert.Contains("cultmath_planetary_field_sample", planetary);
|
||||
Assert.Contains("cultmath_planetary_equirectangular_forward", planetary);
|
||||
Assert.Contains("cultmath_planetary_equal_earth_forward", planetary);
|
||||
Assert.Contains("CultMathPlanetarySurfaceSample", planetary);
|
||||
}
|
||||
|
||||
private static string GetRepositoryRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
|
|
|
|||
80
tests/CultMath.Tests/PlanetaryFieldTests.cs
Normal file
80
tests/CultMath.Tests/PlanetaryFieldTests.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryFieldTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Definition = new(
|
||||
0x7f59_6d11_02ab_9c31UL,
|
||||
8.4f,
|
||||
42,
|
||||
AdvancedErosionParameters.Default with { Scale = 0.63f, Strength = 0.12f, Octaves = 7 });
|
||||
|
||||
[Fact]
|
||||
public void SampleIsDeterministicScaleAwareAndTangent()
|
||||
{
|
||||
var direction = math.normalize(new float3(0.31f, -0.72f, 0.61f));
|
||||
var source = new FixtureField();
|
||||
var coarse = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.1f));
|
||||
var fine = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.01f));
|
||||
var repeated = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.01f));
|
||||
|
||||
Assert.Equal(fine, repeated);
|
||||
Assert.Equal(Definition.FieldVersion, fine.FieldVersion);
|
||||
Assert.True(fine.UnresolvedHeightBound <= coarse.UnresolvedHeightBound);
|
||||
Assert.True(fine.FinestResolvedWavelength <= coarse.FinestResolvedWavelength);
|
||||
Assert.InRange(MathF.Abs(math.dot(fine.UnitDirection, fine.TangentGradient)), 0, 2.0e-5f);
|
||||
Assert.Equal(1, math.length(fine.SurfaceNormal), 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchAndDoublePositionUseThePointContract()
|
||||
{
|
||||
var directions = new[]
|
||||
{
|
||||
math.normalize(new float3(1, 2, 3)),
|
||||
math.normalize(new float3(-2, 0.5f, 1)),
|
||||
math.normalize(new float3(0.1f, -0.2f, -1)),
|
||||
};
|
||||
var output = new PlanetarySurfaceSample[directions.Length];
|
||||
var source = new FixtureField();
|
||||
var scale = PlanetaryQueryScale.AtFootprint(0.05f);
|
||||
PlanetaryField.SampleBatch(Definition, directions, scale, source, output);
|
||||
|
||||
for (var i = 0; i < directions.Length; i++)
|
||||
{
|
||||
var normalized = math.normalize(directions[i]);
|
||||
var expected = PlanetaryField.Sample(Definition, normalized, source.Sample(normalized), scale);
|
||||
Assert.InRange(MathF.Abs(expected.RadialDisplacement - output[i].RadialDisplacement), 0, 1.0e-6f);
|
||||
Assert.InRange(math.distance(expected.TangentGradient, output[i].TangentGradient), 0, 1.0e-6f);
|
||||
}
|
||||
|
||||
var d = directions[0];
|
||||
var position = new double3(d.x * 8.0e11, d.y * 8.0e11, d.z * 8.0e11);
|
||||
var positioned = PlanetaryField.SamplePosition(Definition, position, scale, source);
|
||||
Assert.InRange(math.distance(positioned.UnitDirection, d), 0, 2.0e-6f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FieldIdentityChangesWithEveryLoadBearingInput()
|
||||
{
|
||||
var erosion = AdvancedErosionParameters.Default;
|
||||
var baseline = PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion);
|
||||
Assert.Equal(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(4, 8.4f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.5f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 8, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion with { Gain = 0.49f }).FieldVersion);
|
||||
}
|
||||
|
||||
private readonly struct FixtureField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var value = direction.x * 0.6f + direction.y * direction.z * 0.3f;
|
||||
var fieldGradient = new float3(0.6f, direction.z * 0.3f, direction.y * 0.3f);
|
||||
fieldGradient -= direction * math.dot(fieldGradient, direction);
|
||||
return new(value * 0.1f, fieldGradient * 0.1f, value, fieldGradient, math.clamp(value, -1, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
57
tests/CultMath.Tests/PlanetaryPageTests.cs
Normal file
57
tests/CultMath.Tests/PlanetaryPageTests.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryPageTests
|
||||
{
|
||||
[Fact]
|
||||
public void BilinearSamplingAndSummaryUseOnePageContract()
|
||||
{
|
||||
var layout = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 0, 0, 0), 3, 1);
|
||||
var samples = new PlanetaryPageSample[layout.StorageSize * layout.StorageSize];
|
||||
for (var y = 0; y < layout.StorageSize; y++)
|
||||
for (var x = 0; x < layout.StorageSize; x++)
|
||||
samples[y * layout.StorageSize + x] = new(x + y * 10, new float3(x, y, 0), x / 10.0f, y / 10.0f);
|
||||
|
||||
var sample = PlanetaryPageSampling.Bilinear(samples, layout, new float2(0.25f, 0.75f));
|
||||
Assert.Equal(26.5f, sample.Height, 4);
|
||||
Assert.Equal(new float3(1.5f, 2.5f, 0), sample.TangentGradient);
|
||||
var summary = PlanetaryPageSampling.Summarize(samples, layout, 0.125f);
|
||||
Assert.Equal(0, summary.MinimumHeight);
|
||||
Assert.Equal(44, summary.MaximumHeight);
|
||||
Assert.Equal(0.125f, summary.UnresolvedHeightBound);
|
||||
Assert.True(summary.MaximumSlope > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChildPageStoresOnlyResidualAndComposesBackToDirectField()
|
||||
{
|
||||
var field = new PlanetaryFieldDefinition(7, 8.4f, 3, AdvancedErosionParameters.Default with
|
||||
{
|
||||
Scale = 0.63f,
|
||||
Strength = 0.12f,
|
||||
Octaves = 7,
|
||||
});
|
||||
var source = new FixtureField();
|
||||
var root = PlanetaryPageBaker.Bake(field, new(new(PlanetaryCubeFace.PositiveZ, 0, 0, 0), 17, 2), source);
|
||||
var child = PlanetaryPageBaker.Bake(field, new(new(PlanetaryCubeFace.PositiveZ, 1, 1, 0), 17, 2), source);
|
||||
var direction = PlanetaryPageSampling.DirectionAtLocal(child.Layout, 0.5, 0.5);
|
||||
var pages = new[] { new PlanetaryResidentPage(root, 1), new PlanetaryResidentPage(child, 1) };
|
||||
Assert.True(PlanetaryPageSetSampling.TrySample(pages, direction, out var composed));
|
||||
var spacing = PlanetaryPageSampling.NominalAngularTexelSize(child.Layout) * field.Radius;
|
||||
var direct = PlanetaryField.Sample(field, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(spacing));
|
||||
Assert.InRange(MathF.Abs(composed.Height - direct.RadialDisplacement), 0, 2.0e-5f);
|
||||
Assert.InRange(math.distance(composed.TangentGradient, direct.TangentGradient), 0, 3.0e-5f);
|
||||
}
|
||||
|
||||
private readonly struct FixtureField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var value = direction.x * 0.4f - direction.y * 0.2f + direction.z * 0.5f;
|
||||
var gradient = new float3(0.4f, -0.2f, 0.5f);
|
||||
gradient -= direction * math.dot(gradient, direction);
|
||||
return new(value * 0.08f, gradient * 0.08f, value, gradient, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
tests/CultMath.Tests/PlanetaryPatchTests.cs
Normal file
40
tests/CultMath.Tests/PlanetaryPatchTests.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryPatchTests
|
||||
{
|
||||
[Fact]
|
||||
public void CubeSpherePatchMeshUsesCanonicalTopology()
|
||||
{
|
||||
var meshes = PlanetaryPatch.CreateCubeSphere(8);
|
||||
Assert.Equal(6, meshes.Length);
|
||||
foreach (var mesh in meshes)
|
||||
{
|
||||
Assert.Equal(81, mesh.Vertices.Length);
|
||||
Assert.Equal(8 * 8 * 6, mesh.Indices.Length);
|
||||
Assert.All(mesh.Vertices, vertex => Assert.Equal(1, math.length(vertex.UnitDirection), 5));
|
||||
foreach (var index in mesh.Indices) Assert.InRange(index, 0, mesh.Vertices.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NeighborFaceCornersAreSingleValued()
|
||||
{
|
||||
var directions = PlanetaryPatch.CreateCubeSphere(1)
|
||||
.SelectMany(mesh => mesh.Vertices.Select(vertex => vertex.UnitDirection))
|
||||
.ToArray();
|
||||
var groups = directions.GroupBy(direction => new
|
||||
{
|
||||
X = Math.Sign(direction.x),
|
||||
Y = Math.Sign(direction.y),
|
||||
Z = Math.Sign(direction.z),
|
||||
});
|
||||
Assert.Equal(8, groups.Count());
|
||||
Assert.All(groups, group =>
|
||||
{
|
||||
var first = group.First();
|
||||
Assert.All(group, direction => Assert.InRange(math.distance(first, direction), 0, 1.0e-6f));
|
||||
});
|
||||
}
|
||||
}
|
||||
73
tests/CultMath.Tests/PlanetaryProjectionTests.cs
Normal file
73
tests/CultMath.Tests/PlanetaryProjectionTests.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryProjectionTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(PlanetaryProjectionKind.Equirectangular)]
|
||||
[InlineData(PlanetaryProjectionKind.WebMercator)]
|
||||
[InlineData(PlanetaryProjectionKind.EqualEarth)]
|
||||
[InlineData(PlanetaryProjectionKind.Orthographic)]
|
||||
[InlineData(PlanetaryProjectionKind.AzimuthalEquidistant)]
|
||||
[InlineData(PlanetaryProjectionKind.AzimuthalEqualArea)]
|
||||
[InlineData(PlanetaryProjectionKind.LocalTangent)]
|
||||
public void ProjectionRoundTripsVisibleDirections(PlanetaryProjectionKind kind)
|
||||
{
|
||||
var parameters = new PlanetaryProjectionParameters(kind, 0.31, -0.22);
|
||||
foreach (var longitude in new[] { -0.4, -0.1, 0.2, 0.65 })
|
||||
foreach (var latitude in new[] { -0.6, -0.15, 0.3, 0.7 })
|
||||
{
|
||||
var direction = Direction(longitude + parameters.CenterLongitude, latitude);
|
||||
if (!PlanetaryProjection.TryForward(direction, parameters, out var map)) continue;
|
||||
Assert.True(PlanetaryProjection.TryInverse(map, parameters, out var roundTrip));
|
||||
Assert.InRange(math.distance(direction, roundTrip), 0, 3.0e-5f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CubeAtlasRoundTripsEveryFaceInterior()
|
||||
{
|
||||
var projection = new PlanetaryProjectionParameters(PlanetaryProjectionKind.CubeAtlas);
|
||||
foreach (var face in Enum.GetValues<PlanetaryCubeFace>())
|
||||
{
|
||||
var direction = PlanetaryTopology.Direction(new(face, 0.23, -0.41));
|
||||
Assert.True(PlanetaryProjection.TryForward(direction, projection, out var map));
|
||||
Assert.True(PlanetaryProjection.TryInverse(map, projection, out var roundTrip));
|
||||
Assert.InRange(math.distance(direction, roundTrip), 0, 2.0e-6f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MercatorRejectsPolarSingularity()
|
||||
{
|
||||
var northPole = new float3(0, 0, 1);
|
||||
Assert.False(PlanetaryProjection.TryForward(northPole, new(PlanetaryProjectionKind.WebMercator), out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MapTileBakerCarriesFieldIdentityAndProjectionValidity()
|
||||
{
|
||||
var field = new PlanetaryFieldDefinition(99, 10, 0, AdvancedErosionParameters.Default);
|
||||
var layout = new PlanetaryMapTileLayout(
|
||||
new(PlanetaryProjectionKind.Orthographic), 0, 0, 0, 9, 1);
|
||||
var key = new PlanetaryMapTileKey(field.FieldVersion, 1, 7, layout, PlanetaryQueryScale.AtFootprint(0.1f));
|
||||
var tile = PlanetaryMapTileBaker.Bake(field, key, new FlatField());
|
||||
Assert.Equal(key, tile.Key);
|
||||
Assert.Equal(layout.StorageSize * layout.StorageSize, tile.Samples.Length);
|
||||
Assert.Contains(tile.Validity, valid => valid);
|
||||
Assert.Contains(tile.Validity, valid => !valid);
|
||||
Assert.All(tile.Samples.Where((_, index) => tile.Validity[index]), sample => Assert.Equal(field.FieldVersion, sample.FieldVersion));
|
||||
}
|
||||
|
||||
private static float3 Direction(double longitude, double latitude)
|
||||
{
|
||||
var cos = Math.Cos(latitude);
|
||||
return new((float)(cos * Math.Cos(longitude)), (float)(cos * Math.Sin(longitude)), (float)Math.Sin(latitude));
|
||||
}
|
||||
|
||||
private readonly struct FlatField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 unitDirection) => new(0, float3.zero, 0, float3.zero, 0);
|
||||
}
|
||||
}
|
||||
46
tests/CultMath.Tests/PlanetaryQueryTests.cs
Normal file
46
tests/CultMath.Tests/PlanetaryQueryTests.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryQueryTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Field = new(12, 10, 0, AdvancedErosionParameters.Default with { Strength = 0, Octaves = 0 });
|
||||
private static readonly PlanetaryQueryScale Scale = PlanetaryQueryScale.AtFootprint(0.1f, 0);
|
||||
|
||||
[Fact]
|
||||
public void RayIntersectionAndClearanceUseTheCpuSurfaceContract()
|
||||
{
|
||||
var source = new BulgedSphere();
|
||||
Assert.True(PlanetaryQueries.TryIntersectRay(
|
||||
Field, new double3(0, 0, 20), new double3(0, 0, -1), 30,
|
||||
Scale, new PlanetaryRadialBounds(-1, 1), source, out var hit));
|
||||
Assert.InRange(hit.Distance, 8.99999, 9.00001);
|
||||
Assert.InRange(hit.PlanetLocalPosition.z, 10.99999, 11.00001);
|
||||
var clearance = PlanetaryQueries.SegmentClearance(
|
||||
Field, new double3(-2, 0, 12), new double3(2, 0, 12), 0.1, Scale, source);
|
||||
Assert.True(clearance > 0.8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GreatCircleAndRegionQueriesReturnVersionedSamples()
|
||||
{
|
||||
var source = new BulgedSphere();
|
||||
var path = PlanetaryQueries.SampleGreatCircle(Field, new float3(1, 0, 0), new float3(0, 1, 0), 8, Scale, source);
|
||||
Assert.Equal(9, path.Length);
|
||||
Assert.Equal(MathF.PI * 5, path[^1].Distance, 4);
|
||||
Assert.All(path, point => Assert.Equal(Field.FieldVersion, point.Surface.FieldVersion));
|
||||
var summary = PlanetaryQueries.Summarize(Field, path.Select(point => point.Surface.UnitDirection).ToArray(), Scale, source);
|
||||
Assert.True(summary.MaximumDisplacement >= summary.MinimumDisplacement);
|
||||
Assert.True(summary.MaximumSlope >= 0);
|
||||
}
|
||||
|
||||
private readonly struct BulgedSphere : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var displacement = direction.z;
|
||||
var gradient = new float3(0, 0, 1) - direction * direction.z;
|
||||
return new(displacement, gradient, 0, float3.zero, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
tests/CultMath.Tests/PlanetaryResidencyTests.cs
Normal file
57
tests/CultMath.Tests/PlanetaryResidencyTests.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryResidencyTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Field = new(
|
||||
1, 8.4f, 0, AdvancedErosionParameters.Default with { Scale = 0.63f, Strength = 0.12f, Octaves = 7 });
|
||||
private static readonly PlanetaryLodParameters Lod = new(5, 17, 2, 720, 8.4f / 65536);
|
||||
|
||||
[Fact]
|
||||
public void SelectorReturnsSixRootsAndOneAncestorChain()
|
||||
{
|
||||
var direction = math.normalize(new float3(-0.2f, 0.91f, 0.37f));
|
||||
var selected = PlanetaryLodSelector.SelectAncestorChain(Field, direction, 8.45f, Lod);
|
||||
Assert.Equal(6, selected.Count(tile => tile.Level == 0));
|
||||
var descendants = selected.Where(tile => tile.Level > 0).OrderBy(tile => tile.Level).ToArray();
|
||||
for (var i = 1; i < descendants.Length; i++) Assert.Equal(descendants[i - 1], descendants[i].Parent());
|
||||
if (descendants.Length > 0) Assert.Equal(PlanetaryTopology.TileAt(direction, descendants[^1].Level), descendants[^1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResidencyFadesResidualsWithoutChangingContentDuringPresentation()
|
||||
{
|
||||
var root = new PlanetaryTileAddress(PlanetaryCubeFace.PositiveX, 0, 0, 0);
|
||||
var child = root.Child(1, 0);
|
||||
var residency = new PlanetaryResidualResidency();
|
||||
var arrival = residency.Update(new[] { root, child }, 0, 1);
|
||||
var midpoint = residency.Update(new[] { root, child }, 0.5f, 1);
|
||||
var settled = residency.Update(new[] { root, child }, 1, 1);
|
||||
Assert.Equal(arrival.ContentVersion, midpoint.ContentVersion);
|
||||
Assert.Equal(midpoint.ContentVersion, settled.ContentVersion);
|
||||
Assert.Equal(0, arrival.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(0.5f, midpoint.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(1, settled.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
|
||||
var leaving = residency.Update(new[] { root }, 1.25f, 1);
|
||||
Assert.True(leaving.Tiles.Single(tile => tile.Tile == child).Departing);
|
||||
Assert.Equal(1, leaving.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
var gone = residency.Update(new[] { root }, 2.25f, 1);
|
||||
Assert.DoesNotContain(gone.Tiles, tile => tile.Tile == child);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReversingAnArrivalDepartsFromItsCurrentWeightWithoutJumping()
|
||||
{
|
||||
var root = new PlanetaryTileAddress(PlanetaryCubeFace.PositiveX, 0, 0, 0);
|
||||
var child = root.Child(0, 1);
|
||||
var residency = new PlanetaryResidualResidency();
|
||||
_ = residency.Update(new[] { root, child }, 0, 1);
|
||||
var arriving = residency.Update(new[] { root, child }, 0.25f, 1);
|
||||
var departing = residency.Update(new[] { root }, 0.25f, 1);
|
||||
Assert.Equal(0.25f, arriving.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(0.25f, departing.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.True(departing.Tiles.Single(tile => tile.Tile == child).Departing);
|
||||
}
|
||||
}
|
||||
55
tests/CultMath.Tests/PlanetaryTopologyTests.cs
Normal file
55
tests/CultMath.Tests/PlanetaryTopologyTests.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using Xunit;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
public sealed class PlanetaryTopologyTests
|
||||
{
|
||||
[Fact]
|
||||
public void FaceDirectionRoundTripsAcrossEveryFace()
|
||||
{
|
||||
foreach (var face in Enum.GetValues<PlanetaryCubeFace>())
|
||||
foreach (var u in new[] { -0.9, -0.25, 0.0, 0.4, 0.9 })
|
||||
foreach (var v in new[] { -0.85, -0.1, 0.3, 0.88 })
|
||||
{
|
||||
var direction = PlanetaryTopology.Direction(new(face, u, v));
|
||||
var roundTrip = PlanetaryTopology.FaceCoordinate(direction);
|
||||
Assert.Equal(face, roundTrip.Face);
|
||||
Assert.Equal(u, roundTrip.U, 5);
|
||||
Assert.Equal(v, roundTrip.V, 5);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SiblingPagesShareBitStableBoundaryDirections()
|
||||
{
|
||||
var left = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 1, 0, 0), 17, 2);
|
||||
var right = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 1, 1, 0), 17, 2);
|
||||
for (var y = 0; y < left.InteriorSize; y++)
|
||||
{
|
||||
var a = PlanetaryPageSampling.Direction(left, left.BorderSize + left.InteriorSize - 1, left.BorderSize + y);
|
||||
var b = PlanetaryPageSampling.Direction(right, right.BorderSize, right.BorderSize + y);
|
||||
Assert.Equal(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DirectionSelectsContainingTileAndLocalCoordinate()
|
||||
{
|
||||
var expected = new PlanetaryTileAddress(PlanetaryCubeFace.NegativeY, 5, 19, 7);
|
||||
var direction = PlanetaryPageSampling.DirectionAtLocal(new(expected, 17, 2), 0.37, 0.61);
|
||||
Assert.Equal(expected, PlanetaryTopology.TileAt(direction, expected.Level));
|
||||
Assert.True(PlanetaryTopology.TryLocalCoordinate(direction, expected, out var local));
|
||||
Assert.Equal(0.37f, local.x, 4);
|
||||
Assert.Equal(0.61f, local.y, 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BorderSamplesDoNotRequireNeighborResidency()
|
||||
{
|
||||
var page = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveX, 0, 0, 0), 17, 3);
|
||||
var edge = PlanetaryPageSampling.Direction(page, page.BorderSize + page.InteriorSize - 1, page.BorderSize + 8);
|
||||
var border = PlanetaryPageSampling.Direction(page, page.StorageSize - 1, page.BorderSize + 8);
|
||||
Assert.NotEqual(edge, border);
|
||||
Assert.Equal(1, math.length(border), 5);
|
||||
}
|
||||
}
|
||||
Reference in a new issue