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
30 changes: 24 additions & 6 deletions Sources/MapConductorCore/marker/MarkerHitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,24 @@ public enum MarkerHitTest {
project: (GeoPointProtocol) -> CGPoint?,
searchRadius: CGFloat = searchRadius
) -> GeoRectBounds? {
let epsilon = 1e-4
guard let north = project(GeoPoint(latitude: position.latitude + epsilon, longitude: position.longitude)),
let east = project(GeoPoint(latitude: position.latitude, longitude: position.longitude + epsilon))
else { return nil }
let ptPerDegLat = Double(hypot(north.x - touchScreen.x, north.y - touchScreen.y)) / epsilon
let ptPerDegLon = Double(hypot(east.x - touchScreen.x, east.y - touchScreen.y)) / 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 ptPerDegLat = 0.0
var ptPerDegLon = 0.0
while true {
guard let north = project(GeoPoint(latitude: position.latitude + epsilon, longitude: position.longitude)),
let east = project(GeoPoint(latitude: position.latitude, longitude: position.longitude + epsilon))
else { return nil }
let dLat = Double(hypot(north.x - touchScreen.x, north.y - touchScreen.y))
let dLon = Double(hypot(east.x - touchScreen.x, east.y - touchScreen.y))
ptPerDegLat = dLat / epsilon
ptPerDegLon = dLon / epsilon
if (dLat >= minMeasured && dLon >= minMeasured) || epsilon >= maxEpsilonDegrees { break }
epsilon *= 10
}
guard ptPerDegLat > 0, ptPerDegLon > 0 else { return nil }
let halfLat = Double(searchRadius) / ptPerDegLat
let halfLon = Double(searchRadius) / ptPerDegLon
Expand All @@ -185,4 +197,10 @@ public enum MarkerHitTest {
/// 候補を探す半径(pt)。ラベルの長さの上限の目安で、これより遠くまで伸びたラベルの端は拾えない。
@_spi(MapConductorDriver)
public static let searchRadius: CGFloat = 256

/// The scale is measured over at least this many points (see ``searchBounds(position:touchScreen:project:searchRadius:)``).
private static let minMeasured = 4.0

/// The widest step tried when measuring the scale: a 1° step covers any zoom that shows markers.
private static let maxEpsilonDegrees = 1.0
}
18 changes: 18 additions & 0 deletions Tests/MapConductorCoreTests/MarkerHitTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,22 @@ final class MarkerHitTestPickTests: XCTestCase {
XCTAssertEqual(MarkerHitTest.pickIndex(touch: CGPoint(x: 170, y: 105), rects: [far, near], tolerance: 40), 1)
XCTAssertNil(MarkerHitTest.pickIndex(touch: CGPoint(x: 400, y: 400), rects: [far, near], tolerance: 14))
}

/// 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.
func testSearchBoundsWorksWithAProjectionRoundedToWholePixels() throws {
let worldPx = 512.0 * pow(2.0, 9.0) // zoom 9: 728 px a degree
func project(_ p: GeoPointProtocol) -> CGPoint? {
let x = (p.longitude + 180) / 360 * worldPx
let s = sin(p.latitude * .pi / 180)
let y = (0.5 - log((1 + s) / (1 - s)) / (4 * .pi)) * worldPx
return CGPoint(x: x.rounded(), y: y.rounded())
}
let tap = GeoPoint(latitude: 21.31, longitude: -157.86)
let bounds = try XCTUnwrap(MarkerHitTest.searchBounds(position: tap, touchScreen: project(tap)!, project: project, searchRadius: 512),
"bounds were not made")
let halfLon = (bounds.northEast!.longitude - bounds.southWest!.longitude) / 2
XCTAssert(halfLon > 0.63 && halfLon < 0.77, "half width \(halfLon)") // 512 px is about 0.70°
}
}
Loading