diff --git a/api/android-sdk-core.api b/api/android-sdk-core.api index 4cd9756..3ce5b82 100644 --- a/api/android-sdk-core.api +++ b/api/android-sdk-core.api @@ -941,10 +941,11 @@ public final class com.mapconductor.core.map.LocalMapOverlayRegistryKt { } public final class com.mapconductor.core.map.MapCameraPosition implements com.mapconductor.core.map.MapCameraPositionInterface { public static final com.mapconductor.core.map.MapCameraPosition$Companion Companion; - public com.mapconductor.core.map.MapCameraPosition(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion); - public com.mapconductor.core.map.MapCameraPosition(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion, int, kotlin.jvm.internal.DefaultConstructorMarker); + public com.mapconductor.core.map.MapCameraPosition(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion, java.lang.Double); + public com.mapconductor.core.map.MapCameraPosition(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion, java.lang.Double, int, kotlin.jvm.internal.DefaultConstructorMarker); public com.mapconductor.core.map.MapPaddingsInterface getPaddings(); public com.mapconductor.core.map.VisibleRegion getVisibleRegion(); + public java.lang.Double getFov(); public com.mapconductor.core.features.GeoPoint getPosition(); public double getZoom(); public double getBearing(); @@ -954,7 +955,7 @@ public final class com.mapconductor.core.map.MapCameraPosition implements com.ma public final boolean isTiltSpecified(); public final com.mapconductor.core.map.MapCameraPosition resolvedOnto(com.mapconductor.core.map.MapCameraPositionInterface); public final boolean equals(com.mapconductor.core.map.MapCameraPositionInterface); - public final com.mapconductor.core.map.MapCameraPosition copy(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion); + public final com.mapconductor.core.map.MapCameraPosition copy(com.mapconductor.core.features.GeoPointInterface, java.lang.Double, java.lang.Double, java.lang.Double, com.mapconductor.core.map.MapPaddingsInterface, com.mapconductor.core.map.VisibleRegion, java.lang.Double); public int hashCode(); public com.mapconductor.core.features.GeoPointInterface getPosition(); } @@ -968,6 +969,10 @@ public interface com.mapconductor.core.map.MapCameraPositionInterface { public abstract double getTilt(); public abstract com.mapconductor.core.map.MapPaddingsInterface getPaddings(); public abstract com.mapconductor.core.map.VisibleRegion getVisibleRegion(); + public default java.lang.Double getFov(); +} +public final class com.mapconductor.core.map.MapCameraPositionInterface$DefaultImpls { + public static java.lang.Double getFov(com.mapconductor.core.map.MapCameraPositionInterface); } public final class com.mapconductor.core.map.MapCapability extends java.lang.Enum { public static final com.mapconductor.core.map.MapCapability$Companion Companion; diff --git a/src/main/java/com/mapconductor/core/map/CameraPaddingEmulation.kt b/src/main/java/com/mapconductor/core/map/CameraPaddingEmulation.kt new file mode 100644 index 0000000..686678d --- /dev/null +++ b/src/main/java/com/mapconductor/core/map/CameraPaddingEmulation.kt @@ -0,0 +1,194 @@ +package com.mapconductor.core.map + +import com.mapconductor.core.InternalMapConductorApi +import com.mapconductor.core.features.GeoPoint +import com.mapconductor.core.features.GeoPointInterface +import com.mapconductor.core.spherical.Spherical +import com.mapconductor.core.zoom.AbstractZoomAltitudeConverter +import kotlin.math.abs +import kotlin.math.atan +import kotlin.math.atan2 +import kotlin.math.cos +import kotlin.math.hypot +import kotlin.math.max +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.sin +import kotlin.math.tan + +/** + * カメラのパディング([MapCameraPositionInterface.paddings])を持たない地図SDK向けに、 + * パディングをカメラ中心のずらしで再現する。 + * + * パディングを付けたカメラは「パディングを除いた領域の中央に `position` を置く」。 + * その位置は画面中央から `dx = (left - right) / 2`、`dy = (top - bottom) / 2` + * (dp、右・下が正)だけずれている。ここでは、その画面上のずれが地上で何メートルに + * なるかを解き、ネイティブ SDK へ渡すカメラ中心を求める。逆向きも持つ。 + * + * 視野角はプロバイダが [MapCameraPositionInterface.fov] として持つ値を渡す。 + * コアは視野角の固定値を持たない。 + * + * - `fov > 0`: ピンホールカメラとして遠近込みで解く + * - `fov == 0`: 遠近なしの傾き(縦だけ cos(tilt) 倍に縮む)として解く + * - `fov == null`: 傾きを無視して tilt 0 として解く + * + * 単位: パディングと地図の高さは dp、ズームは統一ズーム(Google Maps 基準、256dp タイル)。 + */ +@InternalMapConductorApi +object CameraPaddingEmulation { + /** 視線が地平線に届かないよう、地面への角度をここで止める。 */ + private const val MAX_RAY_ANGLE_DEG = 89.0 + + /** + * [camera] のパディングを中心のずらしに置き換えた、ネイティブ SDK へ渡すカメラ。 + * 返すカメラの paddings は 0。パディングが無ければ [camera] をそのまま返す。 + * + * @param viewportHeight 地図の高さ(dp) + */ + fun toNativeCamera( + camera: MapCameraPosition, + fov: Double?, + viewportHeight: Double, + ): MapCameraPosition { + if (camera.paddings.isZero()) return camera + val center = + centerForTarget( + target = camera.position, + zoom = camera.zoom, + tilt = camera.tilt, + heading = CameraBearing.toNativeHeading(camera.bearing), + paddings = camera.paddings, + fov = fov, + viewportHeight = viewportHeight, + ) + return camera.copy(position = center, paddings = MapPaddings.Zeros) + } + + /** + * [toNativeCamera] の逆。ネイティブ SDK から読んだカメラ(中心が画面中央)を、 + * [paddings] を付けたカメラ(position がパディング中央)に直す。fov も入れる。 + */ + fun fromNativeCamera( + camera: MapCameraPosition, + paddings: MapPaddingsInterface?, + fov: Double?, + viewportHeight: Double, + ): MapCameraPosition { + val effective = paddings ?: MapPaddings.Zeros + if (effective.isZero()) return camera.copy(paddings = MapPaddings.Zeros, fov = fov) + val target = + targetForCenter( + center = camera.position, + zoom = camera.zoom, + tilt = camera.tilt, + heading = CameraBearing.toNativeHeading(camera.bearing), + paddings = effective, + fov = fov, + viewportHeight = viewportHeight, + ) + return camera.copy(position = target, paddings = effective, fov = fov) + } + + private fun MapPaddingsInterface?.isZero(): Boolean = + this == null || (top == 0.0 && left == 0.0 && bottom == 0.0 && right == 0.0) + + /** + * パディング付きのカメラで [target] を見るために、ネイティブ SDK へ渡すカメラ中心。 + * + * @param heading カメラの向き(北から時計回り、ネイティブ SDK の heading)。 + * MapConductor の bearing からは [CameraBearing.toNativeHeading] で得る。 + */ + fun centerForTarget( + target: GeoPointInterface, + zoom: Double, + tilt: Double, + heading: Double, + paddings: MapPaddingsInterface?, + fov: Double?, + viewportHeight: Double, + ): GeoPoint { + // ズームの縮尺はカメラ中心の緯度で決まるが、中心はまだ分からない。target の緯度で + // 一度解き、求めた中心の緯度で解き直す(差は数十 cm 以下になる)。 + var center: GeoPoint = GeoPoint.from(target) + repeat(2) { + val (forward, right) = + groundOffset(paddings, zoom, center.latitude, tilt, fov, viewportHeight) + ?: return GeoPoint.from(target) + // target は中心から (forward, right) にある。中心はその逆。 + center = move(target, -forward, -right, heading) + } + return center + } + + /** [centerForTarget] の逆。ネイティブ SDK のカメラ中心から、パディング付きの位置を求める。 */ + fun targetForCenter( + center: GeoPointInterface, + zoom: Double, + tilt: Double, + heading: Double, + paddings: MapPaddingsInterface?, + fov: Double?, + viewportHeight: Double, + ): GeoPoint { + val (forward, right) = + groundOffset(paddings, zoom, center.latitude, tilt, fov, viewportHeight) + ?: return GeoPoint.from(center) + return move(center, forward, right, heading) + } + + /** + * 画面中央からパディング中央へのずれを、地上の (前方, 右) メートルで返す。 + * 前方はカメラの向き。ずれが無ければ null。 + */ + internal fun groundOffset( + paddings: MapPaddingsInterface?, + zoom: Double, + latitude: Double, + tilt: Double, + fov: Double?, + viewportHeight: Double, + ): Pair? { + if (paddings == null) return null + val dx = (paddings.left - paddings.right) / 2.0 + val dy = (paddings.top - paddings.bottom) / 2.0 + if (abs(dx) < 1e-9 && abs(dy) < 1e-9) return null + + val metersPerDp = + AbstractZoomAltitudeConverter.WEB_MERCATOR_INITIAL_MPP_256 * + cos(Math.toRadians(latitude.coerceIn(-85.0, 85.0))) / 2.0.pow(zoom) + val t = Math.toRadians(tilt.coerceIn(0.0, MAX_RAY_ANGLE_DEG)) + + // 画面の下向き (dy > 0) は地上では手前(前方の負)。 + if (fov == null || t == 0.0 || viewportHeight <= 0.0) { + return Pair(-dy * metersPerDp, dx * metersPerDp) + } + if (fov <= 0.0) { + return Pair(-dy * metersPerDp / max(cos(t), 0.05), dx * metersPerDp) + } + + val f = (viewportHeight / 2.0) / tan(Math.toRadians(fov) / 2.0) + val distance = f * metersPerDp + val height = distance * cos(t) + val alpha = atan(dy / f) + val ray = min(t - alpha, Math.toRadians(MAX_RAY_ANGLE_DEG)) + // カメラ直下から見た、パディング中央の地点と画面中央の地点までの水平距離の差。 + val back = distance * sin(t) - height * tan(ray) + // 光軸方向の奥行きに比例して横の 1dp が伸びる。 + val depth = height / cos(ray) * cos(alpha) + val right = dx * depth / f + return Pair(-back, right) + } + + private fun move( + origin: GeoPointInterface, + forward: Double, + right: Double, + heading: Double, + ): GeoPoint { + val distance = hypot(forward, right) + if (distance < 1e-9) return GeoPoint.from(origin) + val direction = heading + Math.toDegrees(atan2(right, forward)) + val moved = Spherical.computeOffset(origin, distance, direction) + return GeoPoint(latitude = moved.latitude, longitude = moved.longitude, altitude = origin.altitude ?: 0.0) + } +} diff --git a/src/main/java/com/mapconductor/core/map/MapCameraPositionBase.kt b/src/main/java/com/mapconductor/core/map/MapCameraPositionBase.kt index b683600..f95db02 100644 --- a/src/main/java/com/mapconductor/core/map/MapCameraPositionBase.kt +++ b/src/main/java/com/mapconductor/core/map/MapCameraPositionBase.kt @@ -28,6 +28,20 @@ interface MapCameraPositionInterface { val tilt: Double val paddings: MapPaddingsInterface? val visibleRegion: VisibleRegion? + + /** + * 描画しているカメラの**縦方向**の視野角(度)。今の地図の大きさでの値。 + * + * 地図SDKごとに違う(MapLibre 系は約 37°、Google Maps は約 25° など)。値は + * 各プロバイダが持ち、コアは固定値を持たない。 + * + * - `0.0`: tilt を遠近なしで描いている(傾きをエミュレートしているプロバイダ) + * - `null`: 分からない、または地図を傾けられない + * + * [visibleRegion] と同じく**読み取り専用**: カメラを読んだときに入り、 + * カメラを動かすときには使われない。 + */ + val fov: Double? get() = null } class MapCameraPosition( @@ -37,6 +51,7 @@ class MapCameraPosition( tilt: Double? = null, override val paddings: MapPaddingsInterface? = MapPaddings.Companion.Zeros, override val visibleRegion: VisibleRegion? = null, + override val fov: Double? = null, ) : MapCameraPositionInterface { override val position: GeoPoint = GeoPoint.from(position) @@ -79,6 +94,7 @@ class MapCameraPosition( tilt = givenTilt ?: current.tilt, paddings = paddings, visibleRegion = visibleRegion, + fov = fov, ) } @@ -96,6 +112,7 @@ class MapCameraPosition( tilt: Double? = this.givenTilt, paddings: MapPaddingsInterface? = this.paddings, visibleRegion: VisibleRegion? = this.visibleRegion, + fov: Double? = this.fov, ) = MapCameraPosition( position = position ?: this.position, zoom = zoom ?: this.givenZoom, @@ -103,6 +120,7 @@ class MapCameraPosition( tilt = tilt ?: this.givenTilt, paddings = paddings ?: this.paddings, visibleRegion = visibleRegion ?: this.visibleRegion, + fov = fov ?: this.fov, ) private fun zoomEquals(other: MapCameraPositionInterface): Boolean { diff --git a/src/test/java/com/mapconductor/core/map/CameraPaddingEmulationTest.kt b/src/test/java/com/mapconductor/core/map/CameraPaddingEmulationTest.kt new file mode 100644 index 0000000..9e84bc2 --- /dev/null +++ b/src/test/java/com/mapconductor/core/map/CameraPaddingEmulationTest.kt @@ -0,0 +1,123 @@ +package com.mapconductor.core.map + +import com.mapconductor.core.InternalMapConductorApi +import com.mapconductor.core.features.GeoPoint +import com.mapconductor.core.features.GeoPointInterface +import com.mapconductor.core.spherical.Spherical +import com.mapconductor.core.zoom.AbstractZoomAltitudeConverter +import org.junit.Assert.assertEquals +import org.junit.Test +import kotlin.math.cos +import kotlin.math.pow +import kotlin.math.sin +import kotlin.math.tan + +/** + * パディングのエミュレーションが、独立に書いたピンホールカメラの投影と一致することを確認する。 + * + * ios-sdk の `CameraPaddingEmulationTests`、js-sdk の `CameraPaddingEmulation.test.mjs` と同じ観点。 + */ +@OptIn(InternalMapConductorApi::class) +class CameraPaddingEmulationTest { + private val target = GeoPoint(latitude = 35.6812, longitude = 139.7671) + private val zoom = 15.0 + private val height = 800.0 + + private fun metersPerDp(latitude: Double) = + AbstractZoomAltitudeConverter.WEB_MERCATOR_INITIAL_MPP_256 * cos(Math.toRadians(latitude)) / 2.0.pow(zoom) + + /** + * カメラ中心 [center] から見た [point] の画面位置(中央からの dp、右・下が正)。 + * 地上の (前方, 右) をピンホールカメラで投影する。fov 0 は遠近なし。 + */ + private fun project( + center: GeoPointInterface, + point: GeoPointInterface, + tilt: Double, + heading: Double, + fov: Double, + ): Pair { + val d = Spherical.computeDistanceBetween(center, point) + val dir = Math.toRadians(Spherical.computeHeading(center, point) - heading) + val forward = d * cos(dir) + val right = d * sin(dir) + val mpp = metersPerDp(center.latitude) + val t = Math.toRadians(tilt) + if (fov == 0.0) return Pair(right / mpp, -forward * cos(t) / mpp) + val f = (height / 2) / tan(Math.toRadians(fov) / 2) + val distance = f * mpp + // カメラ座標: 光軸方向の奥行き z、画面下向き y。 + val z = distance + forward * sin(t) + val y = -forward * cos(t) + return Pair(f * right / z, f * y / z) + } + + private fun assertLandsAtPaddedCenter( + paddings: MapPaddings, + tilt: Double, + heading: Double, + fov: Double, + ) { + val center = + CameraPaddingEmulation.centerForTarget(target, zoom, tilt, heading, paddings, fov, height) + val (x, y) = project(center, target, tilt, heading, fov) + assertEquals("x tilt=$tilt heading=$heading fov=$fov", (paddings.left - paddings.right) / 2, x, 0.5) + assertEquals("y tilt=$tilt heading=$heading fov=$fov", (paddings.top - paddings.bottom) / 2, y, 0.5) + val back = CameraPaddingEmulation.targetForCenter(center, zoom, tilt, heading, paddings, fov, height) + assertEquals(target.latitude, back.latitude, 5e-6) + assertEquals(target.longitude, back.longitude, 5e-6) + } + + @Test + fun noPaddingKeepsTarget() { + val center = + CameraPaddingEmulation.centerForTarget(target, zoom, 60.0, 0.0, MapPaddings.Zeros, 36.87, height) + assertEquals(target.latitude, center.latitude, 0.0) + assertEquals(target.longitude, center.longitude, 0.0) + } + + @Test + fun topPaddingAtTiltZeroMovesCenterNorth() { + val center = + CameraPaddingEmulation.centerForTarget(target, zoom, 0.0, 0.0, MapPaddings(top = 300.0), 36.87, height) + val meters = Spherical.computeDistanceBetween(target, center) + assertEquals(150 * metersPerDp(target.latitude), meters, 0.05) + assertEquals(0.0, Spherical.computeHeading(target, center), 0.01) + } + + @Test + fun perspectiveLandsAtPaddedCenter() { + for (tilt in listOf(0.0, 30.0, 45.0, 60.0)) { + for (heading in listOf(0.0, 90.0, 225.0)) { + for (fov in listOf(36.87, 25.0, 32.2)) { + assertLandsAtPaddedCenter(MapPaddings(top = 300.0), tilt, heading, fov) + assertLandsAtPaddedCenter(MapPaddings(top = 120.0, left = 200.0, bottom = 40.0), tilt, heading, fov) + } + } + } + } + + @Test + fun orthographicLandsAtPaddedCenter() { + for (tilt in listOf(30.0, 60.0)) { + assertLandsAtPaddedCenter(MapPaddings(top = 300.0, right = 100.0), tilt, 45.0, 0.0) + } + } + + @Test + fun unknownFovFallsBackToFlat() { + val flat = + CameraPaddingEmulation.centerForTarget(target, zoom, 0.0, 0.0, MapPaddings(top = 300.0), null, height) + val tilted = + CameraPaddingEmulation.centerForTarget(target, zoom, 60.0, 0.0, MapPaddings(top = 300.0), null, height) + assertEquals(flat.latitude, tilted.latitude, 0.0) + } + + @Test + fun fovIsKeptByCopyAndIgnoredByEquals() { + val camera = MapCameraPosition(position = target, zoom = 10.0, fov = 36.87) + assertEquals(36.87, camera.copy(zoom = 11.0).fov!!, 0.0) + val withoutFov: MapCameraPositionInterface = MapCameraPosition(position = target, zoom = 10.0) + assertEquals(true, camera.equals(withoutFov)) + } +}