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

project line orthogonally onto another #1086

Merged
merged 4 commits into from
May 30, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
- `ContentConfiguration.AllowRotatation`
- `AdaptiveGrid.Clone`
- `AdditionalProperties` to ContentConfiguration.
- `Line.Projected(Line line)`

### Fixed

25 changes: 24 additions & 1 deletion Elements/src/Geometry/Line.cs
Original file line number Diff line number Diff line change
@@ -1214,7 +1214,7 @@ public Line MergedCollinearLine(Line line)
/// <summary>
/// Projects current line onto a plane
/// </summary>
/// <param name="plane">Plane to project</param>
/// <param name="plane">Plane to project on</param>
/// <returns>New line on a plane</returns>
public Line Projected(Plane plane)
{
@@ -1223,6 +1223,29 @@ public Line Projected(Plane plane)
return new Line(start, end);
}

/// <summary>
/// Projects current line orthogonally onto another line
/// </summary>
/// <param name="line">Line to project on</param>
/// <returns>New line on a line</returns>
public Line Projected(Line line)
{
var lineDirection = line.Direction();
var normalizedDirection = new Vector3(lineDirection.X, lineDirection.Y, lineDirection.Z);

Vector3 ProjectPoint(Vector3 point, Vector3 lineStart)
{
var toPoint = point - lineStart;
var projectionLength = toPoint.Dot(normalizedDirection);
return lineStart + normalizedDirection.Scale(projectionLength);
}

var newLineStart = ProjectPoint(Start, line.Start);
var newLineEnd = ProjectPoint(End, line.Start);

return new Line(newLineStart, newLineEnd);
}

/// <summary>
/// Return an approximate fit line through a set of points using the least squares method.
/// </summary>
12 changes: 11 additions & 1 deletion Elements/src/Geometry/Vector3.cs
Original file line number Diff line number Diff line change
@@ -280,6 +280,16 @@ public double Dot(double x, double y, double z)
return x * this.X + y * this.Y + z * this.Z;
}

/// <summary>
/// Scales the vector by a given scalar value.
/// </summary>
/// <param name="scalar">The scalar value to multiply each component by.</param>
/// <returns>A new vector where each component is scaled by the given scalar.</returns>
public Vector3 Scale(double scalar)
{
return new Vector3(X * scalar, Y * scalar, Z * scalar);
}

/// <summary>
/// The angle in degrees from this vector to the provided vector.
/// Note that for angles in the plane that can be greater than 180 degrees,
@@ -992,7 +1002,7 @@ public static bool AreApproximatelyEqual(IEnumerable<Vector3> points, double tol
// within tolerance of each other. If all points are within
// tolerance/2 of some point, then they must all be within tolerance
// of each other.
return points.All(p => p.IsAlmostEqualTo(average, tolerance / 2.0));
return points.All(p => p.IsAlmostEqualTo(average, tolerance / 2.0));
}

/// <summary>
Loading