Skip to content
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
28 changes: 23 additions & 5 deletions src/main/java/com/mapconductor/core/marker/MarkerHitPick.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,23 @@ object MarkerHitPick {
project: (GeoPointInterface) -> Offset?,
density: Double = ResourceProvider.getDensity().toDouble(),
): GeoRectBounds? {
val epsilon = 1e-4
val north = project(GeoPoint(position.latitude + epsilon, position.longitude)) ?: return null
val east = project(GeoPoint(position.latitude, position.longitude + epsilon)) ?: return null
val pxPerDegLat = hypot((north.x - touchScreen.x).toDouble(), (north.y - touchScreen.y).toDouble()) / epsilon
val pxPerDegLon = hypot((east.x - touchScreen.x).toDouble(), (east.y - touchScreen.y).toDouble()) / epsilon
// The scale round the tap, from points a little north and east of it. Some
// projections round to whole pixels (ArcGIS's 3D SceneView): zoomed out, 1e-4°
// is under a pixel and both points land on the tap, so widen the step until
// they move far enough to measure.
var epsilon = 1e-4
var pxPerDegLat: Double
var pxPerDegLon: Double
while (true) {
val north = project(GeoPoint(position.latitude + epsilon, position.longitude)) ?: return null
val east = project(GeoPoint(position.latitude, position.longitude + epsilon)) ?: return null
val dLat = hypot((north.x - touchScreen.x).toDouble(), (north.y - touchScreen.y).toDouble())
val dLon = hypot((east.x - touchScreen.x).toDouble(), (east.y - touchScreen.y).toDouble())
pxPerDegLat = dLat / epsilon
pxPerDegLon = dLon / epsilon
if ((dLat >= MIN_MEASURED_PX && dLon >= MIN_MEASURED_PX) || epsilon >= MAX_EPSILON_DEG) break
epsilon *= 10
}
if (pxPerDegLat <= 0.0 || pxPerDegLon <= 0.0) return null
val radiusPx = SEARCH_RADIUS_DP * density
val halfLat = radiusPx / pxPerDegLat
Expand Down Expand Up @@ -142,4 +154,10 @@ object MarkerHitPick {
* 候補を探す半径(dp)。ラベルの長さの上限の目安で、これより遠くまで伸びたラベルの端は拾えない。
*/
const val SEARCH_RADIUS_DP: Double = 256.0

/** The scale is measured over at least this many pixels (see [searchBounds]). */
private const val MIN_MEASURED_PX = 4.0

/** The widest step tried when measuring the scale: a 1° step covers any zoom that shows markers. */
private const val MAX_EPSILON_DEG = 1.0
}
22 changes: 22 additions & 0 deletions src/test/java/com/mapconductor/core/marker/MarkerHitTestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,26 @@ class MarkerHitTestPickTest {
assertTrue(MarkerHitPick.pickIndex(touch, listOf(far, near), 40.0) == 1)
assertTrue(MarkerHitPick.pickIndex(Offset(400f, 400f), listOf(far, near), tolerance) == null)
}

/**
* A projection that rounds to whole pixels (ArcGIS's 3D SceneView does): zoomed out,
* 1e-4° is under a pixel, and the scale must still be measured. Without that the
* search area could not be made and no marker tap was recognised.
*/
@Test
fun searchBoundsWorksWithAProjectionRoundedToWholePixels() {
val worldPx = 512.0 * Math.pow(2.0, 9.0) // zoom 9
fun project(p: com.mapconductor.core.features.GeoPointInterface): Offset {
val x = (p.longitude + 180.0) / 360.0 * worldPx
val s = Math.sin(Math.toRadians(p.latitude))
val y = (0.5 - Math.log((1 + s) / (1 - s)) / (4 * Math.PI)) * worldPx
return Offset(Math.round(x).toFloat(), Math.round(y).toFloat())
}
val tap = com.mapconductor.core.features.GeoPoint(21.31, -157.86)
val bounds = MarkerHitPick.searchBounds(tap, project(tap), ::project, density = 2.0)
assertTrue("bounds were not made", bounds != null)
// 256 dp at density 2 is 512 px; at zoom 9 (728 px a degree) that is about 0.70° each way.
val halfLon = (bounds!!.northEast!!.longitude - bounds.southWest!!.longitude) / 2
assertTrue("half width $halfLon", halfLon in 0.63..0.77)
}
}
Loading