Skip to content

Commit

Permalink
feat: GsaGridLine always provide the clockwise Angle
Browse files Browse the repository at this point in the history
  • Loading branch information
psarras committed Dec 18, 2024
1 parent d1820c0 commit ec4b25a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
20 changes: 16 additions & 4 deletions GsaGH/Parameters/0_Model/GsaGridLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public GsaGridLine(Arc arc, string label = "") {
if (arc.Plane.ZAxis.Z < 0) {
arc = new Arc(arc.EndPoint, arc.MidPoint, arc.StartPoint);
}

Curve = new PolyCurve();
Curve.Append(arc);

Expand All @@ -60,12 +61,18 @@ public GsaGridLine(Arc arc, string label = "") {
}

public GsaGridLine(Line line, string label = "") {
bool IsCounterClockwise = Vector3d.CrossProduct(Vector3d.XAxis, line.UnitTangent).Z > 0;
double vectorAngle = Vector3d.VectorAngle(new Vector3d(1, 0, 0), line.UnitTangent) * 180 / Math.PI;
if (!IsCounterClockwise) {
vectorAngle = 360 - vectorAngle;
}

GridLine = new GridLine(label) {
Shape = GridLineShape.Line,
X = line.From.X,
Y = line.From.Y,
Length = line.Length,
Theta1 = Vector3d.VectorAngle(new Vector3d(1, 0, 0), line.UnitTangent) * 180 / Math.PI
Theta1 = vectorAngle
};
Curve = new PolyCurve();
Curve.Append(line);
Expand Down Expand Up @@ -93,6 +100,7 @@ internal static PolyCurve ToCurve(GridLine gridLine) {
Arc arc = ToArc(gridLine);
curve.Append(arc);
}

return curve;
}

Expand Down Expand Up @@ -126,8 +134,8 @@ public GsaGridLine(GsaGridLine other) {
public override string ToString() {
string label = GridLine.Label != "" ? $"{GridLine.Label} " : string.Empty;
string type = GridLine.Shape == GridLineShape.Arc ? "Shape: Arc " : string.Empty;
string s = $"{label}{type}X:{GridLine.X} Y:{GridLine.Y} Length:" +
$"{GridLine.Length} Orientation:{GridLine.Theta1}°";
string s = $"{label}{type}X:{GridLine.X} Y:{GridLine.Y} Length:"
+ $"{GridLine.Length} Orientation:{GridLine.Theta1}°";
if (GridLine.Shape == GridLineShape.Arc) {
s.Replace("Orientation", "Theta1");
s += " Theta2:" + GridLine.Theta2 + "°";
Expand All @@ -147,8 +155,12 @@ internal void UpdatePreview() {
if (segment == null) {
return;
}

if (Curve.IsLinear()) {
Points = new Point3d[2] { segment.PointAtStart, segment.PointAtEnd };
Points = new Point3d[2] {
segment.PointAtStart,
segment.PointAtEnd
};
} else {
Points = segment.DivideEquidistant(segment.GetLength() / 360.0);
}
Expand Down
28 changes: 28 additions & 0 deletions GsaGHTests/3_Components/0_Model/CreateGridLineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,32 @@ private static void SelectMetersInDropdown(GH_OasysDropDownComponent component)
component.SetSelected(0, 2);
}
}

[Collection("GrasshopperFixture collection")]
public class GsaGridLineTests {

[Fact]
public void ShouldCalculateAngleBetweenXAxisAndLine() {
var line = new Line(Point3d.Origin, new Point3d(1, 1, 0));
var gsaGridLine = new GsaGridLine(line, string.Empty);

Assert.Equal(45, gsaGridLine.GridLine.Theta1, precision: 5);
}

[Fact]
public void ShouldProvideTheClockwiseAngle() {
var line = new Line(Point3d.Origin, new Point3d(1, -1, 0));
var gsaGridLine = new GsaGridLine(line, string.Empty);

Assert.Equal(315, gsaGridLine.GridLine.Theta1, precision: 5);
}
[Fact]
public void ShouldProvideTheClockwiseAngle2() {
var line = new Line(Point3d.Origin, new Point3d(-1, -1, 0));
var gsaGridLine = new GsaGridLine(line, string.Empty);

Assert.Equal(225, gsaGridLine.GridLine.Theta1, precision: 5);
}

}
}

0 comments on commit ec4b25a

Please sign in to comment.