Skip to content

Commit

Permalink
Merge pull request #19 from B3zaleel/create-encoders
Browse files Browse the repository at this point in the history
Create encoders
  • Loading branch information
B3zaleel authored Sep 10, 2024
2 parents f3cd817 + e363176 commit 9c156a8
Show file tree
Hide file tree
Showing 46 changed files with 1,616 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/Draco/Draco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>B3zaleel.Draco</PackageId>
<Version>0.1.0</Version>
<Version>0.2.0</Version>
<Authors>Bezaleel Olakunori</Authors>
<Description>Decode and encode Draco streams.</Description>
<Copyright>Copyright © Bezaleel Olakunori 2024</Copyright>
Expand Down
5 changes: 5 additions & 0 deletions src/Draco/IO/Attributes/AttributeOctahedronTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public override void DecodeParameters(DecoderBuffer decoderBuffer, PointAttribut
_quantizationBits = decoderBuffer.ReadByte();
}

public override void EncodeParameters(EncoderBuffer encoderBuffer)
{
encoderBuffer.WriteByte((byte)_quantizationBits);
}

public override void TransformAttribute(PointAttribute attribute, List<uint> pointIds, PointAttribute targetAttribute)
{
var portableAttributeDataPosition = targetAttribute.GetAddress(0);
Expand Down
65 changes: 65 additions & 0 deletions src/Draco/IO/Attributes/AttributeQuantizationTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,58 @@ public override void CopyToAttributeTransformData(AttributeTransformData data)
data.AppendParameterValue(Range);
}

public void SetParameters(int quantizationBits, List<float> minValues, int numComponents, float range)
{
Assertions.ThrowIfNot(IsQuantizationValid(quantizationBits));
QuantizationBits = quantizationBits;
MinValues = minValues;
Range = range;
}

public void ComputeParameters(PointAttribute attribute, int quantizationBits)
{
Assertions.ThrowIf(QuantizationBits != -1, "Already initialized.");
Assertions.ThrowIfNot(IsQuantizationValid(quantizationBits));
QuantizationBits = quantizationBits;
Range = 0.0f;
var attributeValue = attribute.GetValue<float>(0, attribute.NumComponents);
MinValues = new List<float>(attribute.GetValue<float>(0, attribute.NumComponents));
var maxValues = attribute.GetValue<float>(0, attribute.NumComponents);

for (uint i = 1; i < attribute.UniqueEntriesCount; ++i)
{
attributeValue = attribute.GetValue<float>(i, attribute.NumComponents);

for (int c = 0; c < attribute.NumComponents; ++c)
{
Assertions.ThrowIf(float.IsNaN(attributeValue[c]), "NaN values are not supported.");
if (MinValues[c] > attributeValue[c])
{
MinValues[c] = attributeValue[c];
}
if (maxValues[c] < attributeValue[c])
{
maxValues[c] = attributeValue[c];
}
}
}

for (int c = 0; c < attribute.NumComponents; ++c)
{
Assertions.ThrowIf(float.IsNaN(MinValues[c]) || float.IsInfinity(MinValues[c]) || float.IsNaN(maxValues[c]) || float.IsInfinity(maxValues[c]), "NaN values are not supported.");
var diff = maxValues[c] - MinValues[c];
if (diff > Range)
{
Range = diff;
}
}

if (Range == 0.0f)
{
Range = 1.0f;
}
}

public override void DecodeParameters(DecoderBuffer decoderBuffer, PointAttribute targetAttribute)
{
MinValues = [];
Expand All @@ -68,6 +120,19 @@ public override void DecodeParameters(DecoderBuffer decoderBuffer, PointAttribut
Assertions.ThrowIfNot(IsQuantizationValid(QuantizationBits));
}

public override void EncodeParameters(EncoderBuffer encoderBuffer)
{
if (IsInitialized)
{
for (int i = 0; i < MinValues.Count; ++i)
{
encoderBuffer.Write(MinValues[i]);
}
encoderBuffer.WriteSingle(Range);
encoderBuffer.WriteByte((byte)QuantizationBits);
}
}

public override void TransformAttribute(PointAttribute attribute, List<uint> pointIds, PointAttribute targetAttribute)
{
Assertions.ThrowIfNot(IsInitialized);
Expand Down
1 change: 1 addition & 0 deletions src/Draco/IO/Attributes/AttributeTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public PointAttribute InitTransformedAttribute(PointAttribute srcAttribute, int
public abstract void TransformAttribute(PointAttribute attribute, List<uint> pointIds, PointAttribute targetAttribute);
public abstract void InverseTransformAttribute(PointAttribute attribute, PointAttribute targetAttribute);
public abstract void DecodeParameters(DecoderBuffer decoderBuffer, PointAttribute targetAttribute);
public abstract void EncodeParameters(EncoderBuffer encoderBuffer);
public abstract DataType GetTransformedDataType(PointAttribute attribute);
public abstract int GetTransformedNumComponents(PointAttribute attribute);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

namespace Draco.IO.Attributes.PredictionSchemes;

internal class PredictionSchemeDecodingTransform<TDataType> : PredictionSchemeDecodingTransform<TDataType, TDataType>
where TDataType : struct,
IComparisonOperators<TDataType, TDataType, bool>,
IComparable,
IEqualityOperators<TDataType, TDataType, bool>,
IAdditionOperators<TDataType, TDataType, TDataType>,
ISubtractionOperators<TDataType, TDataType, TDataType>,
IDivisionOperators<TDataType, TDataType, TDataType>,
IMultiplyOperators<TDataType, TDataType, TDataType>,
IDecrementOperators<TDataType>,
IBitwiseOperators<TDataType, TDataType, TDataType>,
IMinMaxValue<TDataType>
{ }

internal class PredictionSchemeDecodingTransform<TDataType, TCorrectedType>
internal interface IPredictionSchemeDecodingTransform<TDataType, TCorrectedType>
where TDataType : struct,
IComparisonOperators<TDataType, TDataType, bool>,
IComparable,
Expand All @@ -40,8 +26,8 @@ internal class PredictionSchemeDecodingTransform<TDataType, TCorrectedType>
IBitwiseOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
IMinMaxValue<TCorrectedType>
{
protected int ComponentsCount { get; set; }
public virtual int QuantizationBits { get; } = -1;
public int ComponentsCount { get; set; }
public int QuantizationBits { get; }

public virtual void Init(int componentsCount)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Draco.IO.Attributes.PredictionSchemes;

internal interface IPredictionSchemeEncoder<TDataType> : IPredictionSchemeEncoder<TDataType, TDataType>
{
}

internal interface IPredictionSchemeEncoder<TDataType, TCorrectedType> : IPredictionSchemeEncoder
{
public TCorrectedType[] ComputeCorrectionValues(TDataType[] data, int size, int numComponents, List<uint> entryToPointMap);
}

internal interface IPredictionSchemeEncoder : IPredictionScheme
{
public void EncodePredictionData(EncoderBuffer encoderBuffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Numerics;
using Draco.IO.Enums;

namespace Draco.IO.Attributes.PredictionSchemes;

internal interface IPredictionSchemeEncodingTransform<TDataType, TCorrectedType>
where TDataType : struct,
IComparisonOperators<TDataType, TDataType, bool>,
IComparable,
IEqualityOperators<TDataType, TDataType, bool>,
IAdditionOperators<TDataType, TDataType, TDataType>,
ISubtractionOperators<TDataType, TDataType, TDataType>,
IDivisionOperators<TDataType, TDataType, TDataType>,
IMultiplyOperators<TDataType, TDataType, TDataType>,
IDecrementOperators<TDataType>,
IBitwiseOperators<TDataType, TDataType, TDataType>,
IMinMaxValue<TDataType>
where TCorrectedType : struct,
IComparisonOperators<TCorrectedType, TCorrectedType, bool>,
IComparable,
IEqualityOperators<TCorrectedType, TCorrectedType, bool>,
IAdditionOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
ISubtractionOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
IDivisionOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
IMultiplyOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
IDecrementOperators<TCorrectedType>,
IBitwiseOperators<TCorrectedType, TCorrectedType, TCorrectedType>,
IMinMaxValue<TCorrectedType>
{
public int ComponentsCount { get; set; }
public int QuantizationBits { get; }
public PredictionSchemeTransformType Type { get => PredictionSchemeTransformType.Delta; }

public virtual void Init(TDataType[] originalData, int size, int componentsCount)
{
ComponentsCount = componentsCount;
}

public virtual TCorrectedType[] ComputeCorrectionValue(TDataType[] originalValues, TDataType[] predictedValues)
{
var correctionValues = new TCorrectedType[ComponentsCount];

for (int i = 0; i < ComponentsCount; ++i)
{
correctionValues[i] = (TCorrectedType)Convert.ChangeType(originalValues[i] - predictedValues[i], typeof(TCorrectedType))!;
}
return correctionValues;
}

public virtual void EncodeTransformData(EncoderBuffer encoderBuffer) { }

public virtual bool AreCorrectionsPositive()
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ internal class MeshPredictionSchemeConstrainedMultiParallelogramDecoder<TDataTyp
IDecrementOperators<TDataType>,
IBitwiseOperators<TDataType, TDataType, TDataType>,
IMinMaxValue<TDataType>
where TTransform : PredictionSchemeDecodingTransform<TDataType, TDataType>
where TTransform : IPredictionSchemeDecodingTransform<TDataType, TDataType>
{
private const int kMaxNumParallelograms = 4;
private MultiParallelogramMode _selectedMode = MultiParallelogramMode.Optimal;
private readonly List<bool>[] _isCreaseEdge = new List<bool>[kMaxNumParallelograms];
private readonly List<bool>[] _isCreaseEdge = new List<bool>[Constants.ConstrainedMultiParallelogramMaxNumParallelograms];

public override PredictionSchemeMethod Method { get => PredictionSchemeMethod.ConstrainedMultiParallelogram; }

public override TDataType[] ComputeOriginalValues(TDataType[] correctedData, int _, int numComponents, List<uint> __)
{
Transform.Init(numComponents);
var predictedValues = new List<TDataType>[kMaxNumParallelograms];
var predictedValues = new List<TDataType>[Constants.ConstrainedMultiParallelogramMaxNumParallelograms];
var originalValues = new TDataType[MeshData.DataToCornerMap!.Count * numComponents];

for (int i = 0; i < kMaxNumParallelograms; ++i)
for (int i = 0; i < Constants.ConstrainedMultiParallelogramMaxNumParallelograms; ++i)
{
predictedValues[i] = new List<TDataType>(correctedData.Length);
predictedValues[i].Fill(correctedData.Length, (TDataType)default);
}
var isCreaseEdgePos = new int[kMaxNumParallelograms];
var isCreaseEdgePos = new int[Constants.ConstrainedMultiParallelogramMaxNumParallelograms];
Array.Fill(isCreaseEdgePos, 0);
var multiPredValues = new TDataType[numComponents];
Array.Fill(multiPredValues, default);
Expand All @@ -55,7 +54,7 @@ public override TDataType[] ComputeOriginalValues(TDataType[] correctedData, int
if (MeshPredictionSchemeParallelogramDecoder<TDataType, TTransform>.TryComputeParallelogramPrediction(p, cornerId, MeshData.CornerTable!, MeshData.VertexToDataMap!, originalValues, numComponents, out TDataType[] parallelogramPredictedValues))
{
++numParallelograms;
if (numParallelograms == kMaxNumParallelograms)
if (numParallelograms == Constants.ConstrainedMultiParallelogramMaxNumParallelograms)
{
break;
}
Expand Down Expand Up @@ -117,7 +116,7 @@ public void DecodeTransformData(DecoderBuffer decoderBuffer)
{
_selectedMode = (MultiParallelogramMode)decoderBuffer.ReadByte();
}
for (int i = 0; i < kMaxNumParallelograms; ++i)
for (int i = 0; i < Constants.ConstrainedMultiParallelogramMaxNumParallelograms; ++i)
{
var numFlags = (uint)decoderBuffer.DecodeVarIntUnsigned();
if (numFlags > 0)
Expand Down
Loading

0 comments on commit 9c156a8

Please sign in to comment.