Skip to content

Commit

Permalink
Use BinaryReader / BinaryWriter (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Metapyziks committed Sep 14, 2023
1 parent 44f41bb commit 3c0bd60
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 90 deletions.
71 changes: 36 additions & 35 deletions libsdf/code/2D/Sdf2D.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace Sandbox.Sdf;

Expand Down Expand Up @@ -129,19 +130,19 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Min );
writer.Write( Max );
writer.Write( CornerRadius );
}

public static RectSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static RectSdf ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new RectSdf(
reader.Read<Vector2>(),
reader.Read<Vector2>(),
reader.Read<float>() );
reader.ReadVector2(),
reader.ReadVector2(),
reader.ReadSingle() );
}
}

Expand All @@ -158,17 +159,17 @@ public record struct CircleSdf( Vector2 Center, float Radius ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => (pos - Center).Length - Radius;

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Center );
writer.Write( Radius );
}

public static CircleSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static CircleSdf ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new CircleSdf(
reader.Read<Vector2>(),
reader.Read<float>() );
reader.ReadVector2(),
reader.ReadSingle() );
}
}

Expand Down Expand Up @@ -221,19 +222,19 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( PointA );
writer.Write( PointB );
writer.Write( Radius );
}

public static LineSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static LineSdf ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new LineSdf(
reader.Read<Vector2>(),
reader.Read<Vector2>(),
reader.Read<float>() );
reader.ReadVector2(),
reader.ReadVector2(),
reader.ReadSingle() );
}
}

Expand Down Expand Up @@ -364,23 +365,23 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( _texture.ResourcePath );
writer.Write( _gradientWidthPixels );
writer.Write( _channel );
writer.Write( (byte) _channel );
writer.Write( _worldSize );
writer.Write( _worldOffset );
}

public static TextureSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static TextureSdf ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TextureSdf(
Texture.Load( FileSystem.Mounted, reader.ReadString() ),
reader.Read<int>(),
reader.Read<ColorChannel>(),
reader.Read<Vector2>(),
reader.Read<Vector2>() );
reader.ReadInt32(),
(ColorChannel) reader.ReadByte(),
reader.ReadVector2(),
reader.ReadVector2() );
}
}

Expand Down Expand Up @@ -416,22 +417,22 @@ public TransformedSdf2D( T sdf, Transform2D transform )
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[Transform.InverseTransformPoint( pos )] * Transform.InverseScale;

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer, sdfTypes );
writer.Write( Transform.Position );
writer.Write( Transform.Rotation );
writer.Write( Transform.Scale );
}

public static TransformedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static TransformedSdf2D<T> ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TransformedSdf2D<T>(
(T)ISdf2D.Read( ref reader, sdfTypes ),
(T)ISdf2D.Read( reader, sdfTypes ),
new Transform2D(
reader.Read<Vector2>(),
reader.Read<Rotation2D>(),
reader.Read<float>() ) );
reader.ReadVector2(),
reader.ReadVector2(),
reader.ReadSingle() ) );
}
}

Expand All @@ -447,17 +448,17 @@ public record struct TranslatedSdf2D<T>( T Sdf, Vector2 Offset ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[pos - Offset];

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer, sdfTypes );
writer.Write( Offset );
}

public static TranslatedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static TranslatedSdf2D<T> ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TranslatedSdf2D<T>(
(T) ISdf2D.Read( ref reader, sdfTypes ),
reader.Read<Vector2>() );
(T) ISdf2D.Read( reader, sdfTypes ),
reader.ReadVector2() );
}
}

Expand All @@ -473,16 +474,16 @@ public record struct ExpandedSdf2D<T>( T Sdf, float Margin ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[pos] - Margin;

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer, sdfTypes );
writer.Write( Margin );
}

public static ExpandedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
public static ExpandedSdf2D<T> ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new ExpandedSdf2D<T>(
(T) ISdf2D.Read( ref reader, sdfTypes ),
reader.Read<float>() );
(T) ISdf2D.Read( reader, sdfTypes ),
reader.ReadSingle() );
}
}
16 changes: 16 additions & 0 deletions libsdf/code/2D/Transform2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ public static implicit operator Rotation2D( float degrees )
return new Rotation2D( degrees );
}

/// <summary>
/// Converts a rotation to its vector representation.
/// </summary>
public static implicit operator Vector2( Rotation2D rotation )
{
return new Vector2( rotation.Cos, rotation.Sin );
}

/// <summary>
/// Converts a rotation from its vector representation.
/// </summary>
public static implicit operator Rotation2D( Vector2 vector )
{
return new Rotation2D( vector.x, vector.y );
}

/// <summary>
/// Represents a rotation of 0 degrees.
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions libsdf/code/3D/Noise/Cellular.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -139,16 +140,16 @@ async Task ISdf3D.SampleRangeAsync( Transform transform, float[] output, (int X,
}
}

public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
public void WriteRaw( BinaryWriter writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Seed );
writer.Write( CellSize );
writer.Write( DistanceOffset );
}

public static CellularNoiseSdf3D ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
public static CellularNoiseSdf3D ReadRaw( BinaryReader reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new CellularNoiseSdf3D( reader.Read<int>(), reader.Read<Vector3>(), reader.Read<float>() );
return new CellularNoiseSdf3D( reader.ReadInt32(), reader.ReadVector3(), reader.ReadSingle() );
}
}
}
Loading

0 comments on commit 3c0bd60

Please sign in to comment.