diff --git a/orx-hash-grid/src/commonMain/kotlin/HashGrid.kt b/orx-hash-grid/src/commonMain/kotlin/HashGrid.kt index 52bbea38..0ad666c0 100644 --- a/orx-hash-grid/src/commonMain/kotlin/HashGrid.kt +++ b/orx-hash-grid/src/commonMain/kotlin/HashGrid.kt @@ -21,9 +21,9 @@ private class Cell( var yMin: Double = Double.POSITIVE_INFINITY, var yMax: Double = Double.NEGATIVE_INFINITY, ) { - val points = mutableListOf() - fun insert(point: Vector2) { - points.add(point) + val points = mutableListOf>() + fun insert(point: Vector2, owner: Any?) { + points.add(Pair(point, owner)) xMin = min(xMin, point.x) xMax = max(xMax, point.x) yMin = min(yMin, point.y) @@ -59,16 +59,16 @@ class HashGrid(val radius: Double) { } fun random(random: Random = Random.Default) : Vector2 { - return cells.values.random(random).points.random() + return cells.values.random(random).points.random().first } - fun insert(point: Vector2) { + fun insert(point: Vector2, owner:Any? = null) { val gc = coords(point) val cell = cells.getOrPut(gc) { Cell() } - cell.insert(point) + cell.insert(point, owner) } - fun isFree(query: Vector2): Boolean { + fun isFree(query: Vector2, ignoreOwners:Set = emptySet()): Boolean { val c = coords(query) if (cells[c] == null) { for (j in -2..2) { @@ -80,8 +80,11 @@ class HashGrid(val radius: Double) { val nc = cells[n] if (nc != null && nc.squaredDistanceTo(query) <= radius * radius) { for (p in nc.points) { - if (p.squaredDistanceTo(query) <= radius * radius) { - return false + + if (p.second == null || p.second !in ignoreOwners) { + if (p.first.squaredDistanceTo(query) <= radius * radius) { + return false + } } } } @@ -89,7 +92,7 @@ class HashGrid(val radius: Double) { } return true } else { - return false + return cells[c]!!.points.all { it.second != null && it.second in ignoreOwners } } } } \ No newline at end of file diff --git a/orx-noise/src/commonMain/kotlin/PoissonDisk.kt b/orx-noise/src/commonMain/kotlin/PoissonDisk.kt index 181abb0d..92e9b4a1 100644 --- a/orx-noise/src/commonMain/kotlin/PoissonDisk.kt +++ b/orx-noise/src/commonMain/kotlin/PoissonDisk.kt @@ -56,7 +56,7 @@ fun poissonDiskSampling( for (ohg in obstacleHashGrids) { for (point in ohg.points()) { - queue.add(Pair(point, ohg.radius)) + queue.add(Pair(point.first, ohg.radius)) } }