Skip to content

Commit

Permalink
include topo transform in ray check
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheumann committed Nov 22, 2023
1 parent e15e19d commit 0493caa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- `IndexedPolycurve` constructor that takes list of `BoundedCurve` now produces `CurveIndices` that share vertices and are withing index range. This means `IndexedPolyline.TransformedPolyline` preserves `CurveIndicies` on new `IndexedPolyline`.
- `BoundedCurve.ToPolyline` now works correctly for `EllipticalArc` class.
- `Ray.Intersects(Topography)` and `Ray.Intersects(Mesh)` would sometimes return a different intersection than the closest one.
- `Ray.Intersects(Topography)` now considers the topography's transform.

### Changed

Expand Down
12 changes: 10 additions & 2 deletions Elements/src/Geometry/Ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,23 @@ public bool Intersects(Plane plane, out Vector3 result, out double t)
}

/// <summary>
/// Does this ray intersect the provided topography? Note that the Topography's transform is not considered.
/// Does this ray intersect the provided topography?
/// </summary>
/// <param name="topo">The topography.</param>
/// <param name="result">The location of intersection.</param>
/// <returns>True if an intersection result occurs.
/// False if no intersection occurs.</returns>
public bool Intersects(Topography topo, out Vector3 result)
{
return Intersects(topo.Mesh, out result);
var transform = topo.Transform;
var inverse = transform.Inverted();
var transformedRay = new Ray(inverse.OfPoint(Origin), Direction);
var intersects = transformedRay.Intersects(topo.Mesh, out result);
if (intersects)
{
result = transform.OfPoint(result);
}
return intersects;
}

/// <summary>
Expand Down
18 changes: 10 additions & 8 deletions Elements/test/RayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Xunit.Abstractions;
using System.Diagnostics;
using Vertex = Elements.Geometry.Vertex;
using Xunit.Sdk;
using System.Linq;

namespace Elements.Tests
{
Expand Down Expand Up @@ -74,23 +76,23 @@ public void RayIntersectsTopography()
e++;
}
}
var topo = new Topography(Vector3.Origin, 10, elevations);
topo.Material = new Material("topo", new Color(0.5, 0.5, 0.5, 0.5));
var topo = new Topography(Vector3.Origin, 10, elevations)
{
Material = new Material("topo", new Color(0.5, 0.5, 0.5, 0.5)),
Transform = new Transform(0, 0, 2)
};
this.Model.AddElement(topo);

var ray = new Ray(Vector3.Origin, Vector3.ZAxis);
this.Model.AddElement(ModelCurveFromRay(ray));
Assert.True(ray.Intersects(topo.Mesh, out var result));
this.Model.AddElements(new Transform().ToModelCurves());
for (int i = 1; i < 9; i++)
{
for (int j = 1; j < 9; j++)
{
var newRay = new Ray(new Vector3(i, j, 40), Vector3.ZAxis.Negate());
var intersect = newRay.Intersects(topo.Mesh, out var result2);
var intersect = newRay.Intersects(topo, out var result2);
Assert.True(intersect);
var line = new Line(result2, newRay.Origin);
Model.AddElement(new ModelCurve(line, BuiltInMaterials.XAxis));
Assert.True(result2.Z > 0 && result2.Z < 40);
Assert.True(result2.Z > elevations.Min() + 2 && result2.Z < elevations.Max() + 2);
}
}
}
Expand Down

0 comments on commit 0493caa

Please sign in to comment.