< Summary

Information
Class: Trailblazer.Heightmaps.HeightmapCompression
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Heightmaps/HeightmapCompression.cs
Line coverage
94%
Covered lines: 17
Uncovered lines: 1
Coverable lines: 18
Total lines: 71
Line coverage: 94.4%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsValid()100%11100%
.ctor(...)100%22100%
Decompress(...)100%11100%
CompressClamped(...)100%44100%
ThrowIfInvalid()50%2266.66%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Heightmaps/HeightmapCompression.cs

#LineLine coverage
 1using FixedMathSharp;
 2using System;
 3
 4namespace Trailblazer.Heightmaps;
 5
 6/// <summary>
 7/// Describes deterministic quantization for compact heightmap samples.
 8/// </summary>
 9public readonly struct HeightmapCompression
 10{
 11    /// <summary>
 12    /// Ground height represented by a compressed value of zero.
 13    /// </summary>
 14    public Fixed64 ReferenceHeight { get; }
 15
 16    /// <summary>
 17    /// World-height delta represented by one compressed unit.
 18    /// </summary>
 19    public Fixed64 HeightStep { get; }
 20
 21    /// <summary>
 22    /// Gets whether this compression metadata can be used for height conversion.
 23    /// </summary>
 19424    public bool IsValid => HeightStep > Fixed64.Zero;
 25
 26    /// <summary>
 27    /// Creates compression metadata for short-backed height samples.
 28    /// </summary>
 29    /// <param name="referenceHeight">Ground height represented by compressed value zero.</param>
 30    /// <param name="heightStep">Positive world-height delta represented by one compressed unit.</param>
 31    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="heightStep"/> is not positive.</except
 32    public HeightmapCompression(Fixed64 referenceHeight, Fixed64 heightStep)
 33    {
 4734        if (heightStep <= Fixed64.Zero)
 235            throw new ArgumentOutOfRangeException(nameof(heightStep), "Height step must be positive.");
 36
 4537        ReferenceHeight = referenceHeight;
 4538        HeightStep = heightStep;
 4539    }
 40
 41    /// <summary>
 42    /// Converts a compressed sample into environment ground/contact Y.
 43    /// </summary>
 44    public Fixed64 Decompress(short compressed)
 45    {
 11146        ThrowIfInvalid();
 11147        return ReferenceHeight + compressed * HeightStep;
 48    }
 49
 50    /// <summary>
 51    /// Quantizes a ground/contact Y value to the nearest representable short sample.
 52    /// </summary>
 53    public short CompressClamped(Fixed64 groundY)
 54    {
 4355        ThrowIfInvalid();
 56
 4357        Fixed64 scaled = (groundY - ReferenceHeight) / HeightStep;
 4358        if (scaled >= (Fixed64)short.MaxValue)
 159            return short.MaxValue;
 4260        if (scaled <= (Fixed64)short.MinValue)
 161            return short.MinValue;
 62
 4163        return (short)scaled.RoundToInt();
 64    }
 65
 66    private void ThrowIfInvalid()
 67    {
 15468        if (!IsValid)
 069            throw new InvalidOperationException("Heightmap compression requires a positive height step.");
 15470    }
 71}