Skip to content

Commit

Permalink
Improve handling of detecting intersections across multiple rings
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanwins committed Feb 13, 2021
1 parent a9a224b commit f61d0a7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.2.0
- Improve handling of detecting intersections across multiple rings

## 1.1.2
- Fixed up some typos in the documentation.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sweepline-intersections",
"version": "1.1.2",
"version": "1.2.0",
"description": "A module to check if a polygon self-intersects using a sweepline algorithm",
"main": "dist/sweeplineIntersections.js",
"module": "dist/sweeplineIntersections.esm.js",
Expand Down
3 changes: 2 additions & 1 deletion src/Event.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

export default class Event {

constructor (p) {
constructor (p, ringId) {
this.p = {
x: p[0],
y: p[1]
}
this.ringId = ringId

this.otherEvent = null
this.isLeftEndpoint = null
Expand Down
8 changes: 5 additions & 3 deletions src/fillQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function fillEventQueue (geojson, eventQueue) {
processFeature(geojson, eventQueue)
}
}

let ringId = 0
function processFeature (featureOrGeometry, eventQueue) {
const geom = featureOrGeometry.type === 'Feature' ? featureOrGeometry.geometry : featureOrGeometry
let coords = geom.coordinates
Expand All @@ -22,12 +24,12 @@ function processFeature (featureOrGeometry, eventQueue) {
for (let ii = 0; ii < coords[i].length; ii++) {
let currentP = coords[i][ii][0]
let nextP = null

ringId = ringId + 1
for (let iii = 0; iii < coords[i][ii].length - 1; iii++) {
nextP = coords[i][ii][iii + 1]

const e1 = new Event(currentP)
const e2 = new Event(nextP)
const e1 = new Event(currentP, ringId)
const e2 = new Event(nextP, ringId)

e1.otherEvent = e2
e2.otherEvent = e1
Expand Down
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export function areCoordsSame (p1, p2) {
export function testSegmentIntersect (seg1, seg2) {
if (seg1 === null || seg2 === null) return false

if (seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
if (seg1.leftSweepEvent.ringId === seg2.leftSweepEvent.ringId &&
(seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
seg1.rightSweepEvent.isSamePoint(seg2.rightSweepEvent) ||
seg1.leftSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
seg1.leftSweepEvent.isSamePoint(seg2.rightSweepEvent)) return false
seg1.leftSweepEvent.isSamePoint(seg2.rightSweepEvent))) return false

const x1 = seg1.leftSweepEvent.p.x
const y1 = seg1.leftSweepEvent.p.y
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/notSimple/chileKinked.geojson

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions test/fixtures/notSimple/itersect-shared-vertice.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "Feature",
"properties": {
"expectedIntersections": 3
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[
[7, 50],
[8, 50],
[9, 50]
],
[
[8, 49],
[8, 50],
[8, 51]
]]
}
}
23 changes: 23 additions & 0 deletions test/fixtures/notSimple/touching.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "Feature",
"properties": {
"expectedIntersections": 2
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[
[120, -20],
[122.5, -16],
[125, -15],
[127.5, -16],
[130, -20]
],[
[120, -20],
[122.5, -24],
[125, -25],
[127.5, -24],
[130, -20]
]
]
}
}

0 comments on commit f61d0a7

Please sign in to comment.