Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(csi) : add element2d openings on receive #2838

Merged
merged 4 commits into from Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,4 @@
using CSiAPIv1;
using CSiAPIv1;
using Objects.Structural.Analysis;
using Objects.Structural.CSI.Analysis;
using Objects.Structural.CSI.Geometry;
Expand Down Expand Up @@ -155,7 +155,7 @@ public object ConvertToNative(Base @object)
LinkPropertyToNative(o, ref appObj);
break;
case CSIProperty2D o:
Property2DToNative(o, ref appObj);
Property2DToNative(o);
break;
case CSISpringProperty o:
SpringPropertyToNative(o, ref appObj);
Expand Down
Expand Up @@ -11,6 +11,11 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ConverterCSI.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConverterCSIUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ArcExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CurveExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ICurveExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\LineExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\PolycurveExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Partial Classes\Analysis\ConvertModelSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Partial Classes\Analysis\ConvertModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Partial Classes\Analysis\ConvertModelInfo.cs" />
Expand Down
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Numerics;
using System.Text;
using Objects.Geometry;

namespace ConverterCSIShared.Extensions
{
internal static class ArcExtensions
{
public static IEnumerable<Point> ToPoints(this Arc arc)
{
var startPoint = new Vector3((float)arc.startPoint.x, (float)arc.startPoint.y, (float)arc.startPoint.z);
var endPoint = new Vector3((float)arc.endPoint.x, (float)arc.endPoint.y, (float)arc.endPoint.z);
var midPoint = new Vector3((float)arc.midPoint.x, (float)arc.midPoint.y, (float)arc.midPoint.z);

// Calculate the radius and center of the arc
Vector3 center = CalculateArcCenter(startPoint, midPoint, endPoint);
float radius = Vector3.Distance(startPoint, center);

// Calculate the plane defined by the start, center, and end points
Vector3 normal = Vector3.Normalize(Vector3.Cross(startPoint - center, endPoint - center));

// Calculate the angles between the start, center, and end points
float startAngle = CalculateAngle(center, startPoint, normal);
float endAngle = CalculateAngle(center, endPoint, normal);
float angleRange = endAngle - startAngle;

// Calculate the angular increment for each point
float angularIncrement = angleRange / 9; // 10 points including start and end points

// Generate the arc points
for (int i = 0; i <= 10; i++)
{
float currentAngle = startAngle + (i * angularIncrement);
Vector3 point = RotatePoint(startPoint, center, normal, currentAngle);
yield return new Point(point.X, point.Y, point.Z);
}
}

private static Vector3 CalculateArcCenter(Vector3 startPoint, Vector3 midPoint, Vector3 endPoint)
{
// Calculate the perpendicular bisectors of the line segments between start-mid and mid-end
Vector3 startMidMidpoint = (startPoint + midPoint) / 2;
Vector3 midEndMidpoint = (midPoint + endPoint) / 2;
Vector3 startMidDirection = Vector3.Normalize(midPoint - startPoint);
Vector3 midEndDirection = Vector3.Normalize(endPoint - midPoint);

// Calculate the normal to the plane containing the bisectors
Vector3 normal = Vector3.Normalize(Vector3.Cross(startMidDirection, midEndDirection));

// Calculate the intersection point of the plane and the perpendicular bisectors (arc center)
float startMidDot = Vector3.Dot(startMidMidpoint, normal);
float midEndDot = Vector3.Dot(midEndMidpoint, normal);
float normalDot = Vector3.Dot(normal, normal);
float t = (startMidDot - midEndDot) / normalDot;
Vector3 center = startMidMidpoint - t * normal;

return center;
}

private static float CalculateAngle(Vector3 center, Vector3 point, Vector3 normal)
{
Vector3 direction = point - center;
float angle = (float)Math.Atan2(Vector3.Dot(normal, Vector3.Cross(center, point)), Vector3.Dot(center, point));
return angle;
}

private static Vector3 RotatePoint(Vector3 point, Vector3 center, Vector3 axis, float angle)
{
Quaternion rotation = Quaternion.CreateFromAxisAngle(axis, angle);
Vector3 rotatedPoint = Vector3.Transform(point - center, rotation);
return rotatedPoint + center;
}
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using Objects.Geometry;

namespace ConverterCSIShared.Extensions
{
internal static class CurveExtensions
{
public static IEnumerable<Point> ToPoints(this Curve curve)
{
for (int i = 0; i < curve.points.Count; i += 3)
{
yield return new Point(curve.points[i], curve.points[i + 1], curve.points[i + 2], curve.units);
}
}
}
}
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using Objects.Geometry;
using Objects;

namespace ConverterCSIShared.Extensions
{
internal static class ICurveExtensions
{
public static IEnumerable<Point> ToPoints(this ICurve crv)
{
return crv switch
{
Line line => line.ToPoints(),
Arc arc => arc.ToPoints(),
Circle circle => throw new NotImplementedException("Circular openings are not yet supported"),
Ellipse ellipse => throw new NotImplementedException("Ellipse openings are not yet supported"),
Spiral spiral => throw new NotImplementedException("Spiral openings are not yet supported"),
Curve curve => curve.ToPoints(),
Polyline poly => poly.GetPoints(),
Polycurve plc => plc.ToPoints(),
_ => throw new Speckle.Core.Logging.SpeckleException("The provided geometry is not a valid curve"),
};
}
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using Objects.Geometry;

namespace ConverterCSIShared.Extensions
{
internal static class LineExtensions
{
public static IEnumerable<Point> ToPoints(this Line line)
{
yield return line.start;
yield return line.end;
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using Objects.Geometry;

namespace ConverterCSIShared.Extensions
{
internal static class PolycurveExtensions
{
public static IEnumerable<Point> ToPoints(this Polycurve polycurve)
{
var prevPoint = new Point(double.NaN, double.NaN, double.NaN);
foreach (var seg in polycurve.segments)
{
foreach (var point in seg.ToPoints())
{
if (Math.Abs(point.x - prevPoint.x) < .01
&& Math.Abs(point.y - prevPoint.y) < .01
&& Math.Abs(point.z - prevPoint.z) < .01
)
{
continue;
}
prevPoint = point;
yield return point;
}
}
}
}
}