Skip to content

Commit

Permalink
Adjust namespaces, added path builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris committed Aug 2, 2024
1 parent 168d525 commit fc59b9c
Show file tree
Hide file tree
Showing 36 changed files with 477 additions and 31 deletions.
41 changes: 39 additions & 2 deletions GdsGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GdsSharp.Lib.Builders;
using System.Numerics;
using GdsSharp.Lib.Builders;
using GdsSharp.Lib.NonTerminals;
using GdsSharp.Lib.NonTerminals.Elements;

Expand Down Expand Up @@ -53,7 +54,43 @@
new GdsPoint(-1250, 0)
]
}
}
},

// Use the path builder to create a path
new PathBuilder(
100f,
new Vector2(-3100, -3300),
Vector2.UnitX)
// Straight ahead for 2000 units
.Straight(2000)

// Bend 45 degrees to the left with a radius of 500 units
.BendDeg(-45, 500)

// Generate shape like <=>
.Straight(100, widthEnd: 250)
.Straight(100)
.Straight(100, widthEnd: 100)

// Some more bends
.BendDeg(-45, 500)
.Straight(100)
.Straight(200, 250)
.BendDeg(180, 300)
.BendDeg(-180, 300)

// Example of using a function to change the width
.BendDeg(-180, 900, f => MathF.Cos(f * 50) * 100 + 150)

// PathBuilder also supports Bézier curves
.Bezier(b => b
.AddPoint(0, 0)
.AddPoint(0, 1000)
.AddPoint(2000, 1000)
.AddPoint(1000, 0),
t => 250 - (250 - 50) * t)
.Straight(800)
.Build()
]
});

Expand Down
84 changes: 84 additions & 0 deletions GdsSharp.Lib.Test/VectorExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Numerics;

namespace GdsSharp.Lib.Test;

[TestFixture]
public class VectorExtensionsTests
{
private const float TwoPi = 2 * MathF.PI;

[Test]
public void Rotate_Rotate90Degrees_CorrectResult()
{
var vec = new Vector2(1, 0);
const float radians = MathF.PI / 2;

var result = vec.Rotate(radians);

Assert.Multiple(() =>
{
Assert.That(result.X, Is.EqualTo(0).Within(1e-6)); // allowing a small tolerance for floating point errors
Assert.That(result.Y, Is.EqualTo(1).Within(1e-6));
});
}

[Test]
public void Rotate_Rotate180Degrees_CorrectResult()
{
var vec = new Vector2(1, 0);
const float radians = MathF.PI;

var result = vec.Rotate(radians);

Assert.Multiple(() =>
{
Assert.That(result.X, Is.EqualTo(-1).Within(1e-6));
Assert.That(result.Y, Is.EqualTo(0).Within(1e-6));
});
}

[Test]
public void Rotate_Rotate360Degrees_CorrectResult()
{
var vec = new Vector2(1, 0);
const float radians = 2 * MathF.PI;

var result = vec.Rotate(radians);

Assert.Multiple(() =>
{
Assert.That(result.X, Is.EqualTo(1).Within(1e-6));
Assert.That(result.Y, Is.EqualTo(0).Within(1e-6));
});
}

[Test]
public void Angle_VectorAt45Degrees_CorrectResult()
{
var vec = new Vector2(1, 1);

var result = vec.Angle();

Assert.That(result, Is.EqualTo(MathF.PI / 4).Within(1e-6));
}

[Test]
public void Angle_VectorAt90Degrees_CorrectResult()
{
var vec = new Vector2(0, 1);

var result = vec.Angle();

Assert.That(result, Is.EqualTo(MathF.PI / 2).Within(1e-6));
}

[Test]
public void Angle_VectorAtNegative45Degrees_CorrectResult()
{
var vec = new Vector2(1, -1);

var result = vec.Angle();

Assert.That(result, Is.EqualTo(-MathF.PI / 4).Within(1e-6));
}
}
2 changes: 1 addition & 1 deletion GdsSharp.Lib/Abstractions/GdsStreamOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using GdsSharp.Lib.Terminals.Abstractions;
using GdsSharp.Lib.Terminals.Records;

namespace GdsSharp.Lib;
namespace GdsSharp.Lib.Abstractions;

public class GdsStreamOperator
{
Expand Down
2 changes: 1 addition & 1 deletion GdsSharp.Lib/Binary/GdsBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text;
using GdsSharp.Lib.Terminals;

namespace GdsSharp.Lib;
namespace GdsSharp.Lib.Binary;

public class GdsBinaryReader : BinaryReader
{
Expand Down
2 changes: 1 addition & 1 deletion GdsSharp.Lib/Binary/GdsBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text;
using GdsSharp.Lib.Terminals;

namespace GdsSharp.Lib;
namespace GdsSharp.Lib.Binary;

public class GdsBinaryWriter : BinaryWriter
{
Expand Down
14 changes: 12 additions & 2 deletions GdsSharp.Lib/Builders/BezierBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ public GdsElement BuildPolygon(int width, int numVertices = 64)
}
}

private Vector2 Evaluate(float t)
/// <summary>
/// Evaluates the Bézier curve at a given t.
/// </summary>
/// <param name="t">[0,1]</param>
/// <returns>Position on the curve at <see cref="t" />.</returns>
public Vector2 Evaluate(float t)
{
var n = _controlPoints.Count - 1;
var x = 0.0f;
Expand All @@ -162,7 +167,12 @@ private Vector2 Evaluate(float t)
return new Vector2(x, y);
}

private Vector2 EvaluateTangent(float t)
/// <summary>
/// Evaluates the tangent of the Bézier curve at a given t.
/// </summary>
/// <param name="t">[0,1]</param>
/// <returns>Tangent vector of the curve at <see cref="t" />.</returns>
public Vector2 EvaluateTangent(float t)
{
var n = _controlPoints.Count - 1;
var nMinusOne = n - 1;
Expand Down
Loading

0 comments on commit fc59b9c

Please sign in to comment.