Skip to content

Commit

Permalink
Support rotation in boundingbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris committed Nov 8, 2024
1 parent d48d5e0 commit 7633368
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion GdsSharp.Lib/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Numerics;
using GdsSharp.Lib.NonTerminals;

namespace GdsSharp.Lib;

public static class GdsExtensions
{
public const float Deg2Rad = MathF.PI / 180;

/// <summary>
/// Rotates a vector by the given angle in radians.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions GdsSharp.Lib/GdsPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ public override string ToString()
{
return $"({X}, {Y})";
}

public GdsPoint Rotate(float sin, float cos)
{
return new GdsPoint(
cos * X - sin * Y,
sin * X + cos * Y
);
}
}
18 changes: 17 additions & 1 deletion GdsSharp.Lib/NonTerminals/Elements/GdsArrayReferenceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ public GdsBoundingBox GetBoundingBox(GdsStructure.StructureProvider structurePro

var boundingBox = structure.GetBoundingBox(structureProvider);
if (boundingBox.IsEmpty) return boundingBox;
var boundingBoxes = Points.Select(p => new GdsBoundingBox(p + boundingBox.Min, p + boundingBox.Max));
IEnumerable<GdsBoundingBox> boundingBoxes;
if (Transformation.Angle != 0)
{
var (sin, cos) = MathF.SinCos((float)Transformation.Angle * GdsExtensions.Deg2Rad);
boundingBoxes = Points.Select(p => new GdsBoundingBox(
p + boundingBox.Min.Rotate(sin, cos),
p + boundingBox.Max.Rotate(sin, cos)
));
}
else
{
boundingBoxes = Points.Select(p => new GdsBoundingBox(
p + boundingBox.Min,
p + boundingBox.Max)
);
}

return new GdsBoundingBox(boundingBoxes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace GdsSharp.Lib.NonTerminals.Elements;

public class GdsStructureReferenceElement : IGdsElement
{
private const float Deg2Rad = MathF.PI / 180;
public required string StructureName { get; set; }
public GdsStrans Transformation { get; set; } = new();
public List<GdsPoint> Points { get; set; } = new();
Expand All @@ -20,7 +21,24 @@ public GdsBoundingBox GetBoundingBox(GdsStructure.StructureProvider structurePro

var boundingBox = structure.GetBoundingBox(structureProvider);
if (boundingBox.IsEmpty) return boundingBox;
var boundingBoxes = Points.Select(p => new GdsBoundingBox(p + boundingBox.Min, p + boundingBox.Max));

IEnumerable<GdsBoundingBox> boundingBoxes;
if (Transformation.Angle != 0)
{
var (sin, cos) = MathF.SinCos((float)Transformation.Angle * GdsExtensions.Deg2Rad);
boundingBoxes = Points.Select(p => new GdsBoundingBox(
p + boundingBox.Min.Rotate(sin, cos),
p + boundingBox.Max.Rotate(sin, cos)
));
}
else
{
boundingBoxes = Points.Select(p => new GdsBoundingBox(
p + boundingBox.Min,
p + boundingBox.Max)
);
}

return new GdsBoundingBox(boundingBoxes);
}
}

0 comments on commit 7633368

Please sign in to comment.