< Summary

Information
Class: Trailblazer.Heightmaps.HeightmapCompression
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Heightmaps/HeightmapCompression.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
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%11100%
Decompress(...)100%11100%
CompressClamped(...)100%44100%
ThrowIfInvalid()100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// HeightmapCompression.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using FixedMathSharp;
 10
 11namespace Trailblazer.Heightmaps;
 12
 13/// <summary>
 14/// Describes deterministic quantization for compact heightmap samples.
 15/// </summary>
 16public readonly struct HeightmapCompression
 17{
 18    /// <summary>
 19    /// Ground height represented by a compressed value of zero.
 20    /// </summary>
 21    public Fixed64 ReferenceHeight { get; }
 22
 23    /// <summary>
 24    /// World-height delta represented by one compressed unit.
 25    /// </summary>
 26    public Fixed64 HeightStep { get; }
 27
 28    /// <summary>
 29    /// Gets whether this compression metadata can be used for height conversion.
 30    /// </summary>
 24131    public bool IsValid => HeightStep > Fixed64.Zero;
 32
 33    /// <summary>
 34    /// Creates compression metadata for short-backed height samples.
 35    /// </summary>
 36    /// <param name="referenceHeight">Ground height represented by compressed value zero.</param>
 37    /// <param name="heightStep">Positive world-height delta represented by one compressed unit.</param>
 38    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="heightStep"/> is not positive.</except
 39    public HeightmapCompression(Fixed64 referenceHeight, Fixed64 heightStep)
 40    {
 5641        SwiftThrowHelper.ThrowIfArgumentOutOfRange(
 5642            heightStep <= Fixed64.Zero,
 5643            actualValue: null,
 5644            paramName: nameof(heightStep),
 5645            message: "Height step must be positive.");
 46
 5447        ReferenceHeight = referenceHeight;
 5448        HeightStep = heightStep;
 5449    }
 50
 51    /// <summary>
 52    /// Converts a compressed sample into environment ground/contact Y.
 53    /// </summary>
 54    public Fixed64 Decompress(short compressed)
 55    {
 14056        ThrowIfInvalid();
 13957        return ReferenceHeight + compressed * HeightStep;
 58    }
 59
 60    /// <summary>
 61    /// Quantizes a ground/contact Y value to the nearest representable short sample.
 62    /// </summary>
 63    public short CompressClamped(Fixed64 groundY)
 64    {
 5065        ThrowIfInvalid();
 66
 4967        Fixed64 scaled = (groundY - ReferenceHeight) / HeightStep;
 4968        if (scaled >= (Fixed64)short.MaxValue)
 169            return short.MaxValue;
 4870        if (scaled <= (Fixed64)short.MinValue)
 171            return short.MinValue;
 72
 4773        return (short)scaled.RoundToInt();
 74    }
 75
 76    private void ThrowIfInvalid()
 77    {
 19078        SwiftThrowHelper.ThrowIfTrue(
 19079            !IsValid,
 19080            message: "Heightmap compression requires a positive height step.");
 18881    }
 82}