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

Fix line trim when line begins on polygon edge #356

Merged
merged 4 commits into from
Jul 31, 2020
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.
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
Expand Up @@ -3,6 +3,7 @@
### Fixed
- CodeGen was failing intermittently
- Elements schemas with Dictionary types were failing to serialize
- [#355](https://github.com/hypar-io/Elements/issues/355)
### Added
- Elements supports the [Hub beta](https://hypar-io.github.io/Elements/Hub.html)
- CodeGen supports `input_schema`
Expand Down
6 changes: 3 additions & 3 deletions src/Elements/Geometry/Line.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Elements.Geometry.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -470,7 +469,8 @@ public List<Line> Trim(Polygon polygon, out List<Line> outsideSegments)

// Add the segment's starting point.
intersections.Add(this.Start);
var StartsOutsidePolygon = !polygon.Contains(this.Start);
polygon.Contains(this.Start, out var containment);
var StartsOutsidePolygon = containment == Containment.Outside;

var hasVertexIntersections = false;

Expand Down Expand Up @@ -514,7 +514,7 @@ public List<Line> Trim(Polygon polygon, out List<Line> outsideSegments)
continue;
}
var segment = new Line(A, B);
if (hasVertexIntersections) // if it passed through a vertex, we can't rely on alternating, so check each midpoint
if (hasVertexIntersections || containment == Containment.CoincidesAtEdge) // if it passed through a vertex, or started at an edge, we can't rely on alternating, so check each midpoint
{
currentlyIn = polygon.Contains((A + B) / 2);
}
Expand Down
13 changes: 13 additions & 0 deletions test/Elements.Tests/LineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using Elements.Tests;
using Newtonsoft.Json;
using Xunit;

namespace Elements.Geometry.Tests
Expand Down Expand Up @@ -174,6 +177,16 @@ public void LineTrimToInYZ()
Assert.NotNull(l3);
}

[Fact]
public void TrimLineThatStartsAtPolygonEdge()
{
var polygon = JsonConvert.DeserializeObject<Polygon>(File.ReadAllText("../../../models/Geometry/ConcavePolygon.json"));
var line = JsonConvert.DeserializeObject<Line>(File.ReadAllText("../../../models/Geometry/LineThatFailsTrim.json"));
var lines = line.Trim(polygon, out var _);

Assert.Equal(4.186147, lines[0].Length(), 2);
}


[Fact]
public void LineTrimWithPolygon()
Expand Down
105 changes: 105 additions & 0 deletions test/Elements.Tests/models/Geometry/ConcavePolygon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"discriminator": "Elements.Geometry.Polygon",
"Vertices": [
{
"X": -10.276991193869598,
"Y": -1.1511904595984275,
"Z": 0.0
},
{
"X": -3.1186796087302824,
"Y": -1.1511904595984301,
"Z": 0.0
},
{
"X": -3.1186796087302824,
"Y": 6.007121125540885,
"Z": 0.0
},
{
"X": 6.0908440680571374,
"Y": 6.007121125540885,
"Z": 0.0
},
{
"X": 6.0908440680571374,
"Y": 0.98374457456592879,
"Z": 0.0
},
{
"X": 10.6118829639346,
"Y": 0.98374457456592879,
"Z": 0.0
},
{
"X": 10.6118829639346,
"Y": -4.5001081602483968,
"Z": 0.0
},
{
"X": 14.002662135842693,
"Y": -4.5001081602483968,
"Z": 0.0
},
{
"X": 14.002662135842693,
"Y": 5.5047834704433907,
"Z": 0.0
},
{
"X": 18.188809261655159,
"Y": 5.5047834704433907,
"Z": 0.0
},
{
"X": 18.188809261655159,
"Y": -8.5606708722864866,
"Z": 0.0
},
{
"X": 5.2954761141527626,
"Y": -8.5606708722864866,
"Z": 0.0
},
{
"X": 5.2954761141527626,
"Y": -3.1605410799884073,
"Z": 0.0
},
{
"X": 0.14651514940343802,
"Y": -3.1605410799884073,
"Z": 0.0
},
{
"X": 0.14651514940343691,
"Y": -9.1885929411583547,
"Z": 0.0
},
{
"X": -5.0861687578621426,
"Y": -9.1885929411583547,
"Z": 0.0
},
{
"X": -5.0861687578621426,
"Y": -4.9605843440877688,
"Z": 0.0
},
{
"X": -7.1792423207683749,
"Y": -4.9605843440877688,
"Z": 0.0
},
{
"X": -7.1792423207683749,
"Y": -7.3885496770589976,
"Z": 0.0
},
{
"X": -10.276991193869598,
"Y": -6.0908440680571321,
"Z": 0.0
}
]
}
13 changes: 13 additions & 0 deletions test/Elements.Tests/models/Geometry/LineThatFailsTrim.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"discriminator": "Elements.Geometry.Line",
"Start": {
"X": 18.188809261655159,
"Y": 1.5664562544790255,
"Z": 0.0
},
"End": {
"X": -10.276991193869598,
"Y": 1.5664562544790255,
"Z": 0.0
}
}