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
2 changes: 2 additions & 0 deletions api/android-sdk-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2953,7 +2953,9 @@ public final class com.mapconductor.core.projection.WebMercatorKt {
public final class com.mapconductor.core.projection.WebMercatorScreenProjection {
public static final com.mapconductor.core.projection.WebMercatorScreenProjection INSTANCE;
public final androidx.compose.ui.geometry.Offset toScreenOffset-broig7I(com.mapconductor.core.features.GeoPointInterface, com.mapconductor.core.map.MapCameraPosition, float, float);
public final androidx.compose.ui.geometry.Offset toScreenOffset-17k57fo(com.mapconductor.core.features.GeoPointInterface, com.mapconductor.core.map.MapCameraPosition, float, float, java.lang.Double);
public final com.mapconductor.core.features.GeoPoint fromScreenOffset-M_7yMNQ(long, com.mapconductor.core.map.MapCameraPosition, float, float);
public final com.mapconductor.core.features.GeoPoint fromScreenOffset-ULxng0E(long, com.mapconductor.core.map.MapCameraPosition, float, float, java.lang.Double);
}
final class com.mapconductor.core.projection.WebMercatorScreenProjection$Normalized {
public final double getX();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mapconductor.core.map.MapCameraPosition
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.sin
import kotlin.math.tan

/**
* 地理座標 ⇄ 画面座標を、カメラとビューの大きさだけから計算する。
Expand All @@ -26,11 +27,12 @@ import kotlin.math.sin
*
* - **Web Mercator でない地図**(3D globe の HERE、球面の Cesium 等)。
* これらは地図SDK自身の投影を使うこと。
* - **tilt が 0 でないとき。** 傾いたカメラは平面の相似変換にならないので誤差が出る。
* (必要になったら `visibleRegion` の 4 隅からホモグラフィを組む方式へ拡張できる。
* 平面地図なら透視投影はホモグラフィそのものなので厳密に扱える。)
*
* bearing(回転)には対応している。
* bearing(回転)に対応している。tilt は、カメラの上下視野角(`fovDegrees`)を渡したときだけ
* 透視(ピンホールカメラ)で扱う。MapLibre GL JS と同じカメラ模型で、画面中央の 1px が
* 世界の 1px(`256 * 2^zoom`)になり、カメラは中央の地点から焦点距離
* `(高さ / 2) / tan(fov / 2)` だけ離れて tilt 分だけ傾いている。視野角を渡さない呼び出しは
* 従来どおり平面の相似変換で、tilt があると奥ほどずれる(tilt 60 で 250px 近く)。
*/
object WebMercatorScreenProjection {
/**
Expand All @@ -53,6 +55,19 @@ object WebMercatorScreenProjection {
camera: MapCameraPosition,
widthPx: Float,
heightPx: Float,
): Offset? = toScreenOffset(position, camera, widthPx, heightPx, fovDegrees = null)

/**
* [toScreenOffset] の tilt 対応版。[fovDegrees] はカメラの上下視野角(地図エンジンの値)。
* null か 0 以下、または tilt が 0 なら平面の式と同じ結果になる。カメラの背後や
* 地平線より向こうに当たる点は null。
*/
fun toScreenOffset(
position: GeoPointInterface,
camera: MapCameraPosition,
widthPx: Float,
heightPx: Float,
fovDegrees: Double?,
): Offset? {
if (widthPx <= 0f || heightPx <= 0f) return null
val worldSize = WORLD_SIZE_AT_ZOOM_0 * 2.0.pow(camera.zoom)
Expand All @@ -75,6 +90,17 @@ object WebMercatorScreenProjection {
sx = rx
sy = ry
}
val distance = cameraDistance(camera, heightPx, fovDegrees)
if (distance != null) {
// 地面上で中央から sy 手前(画面の下向き)の点。カメラは中央から distance 離れて
// tilt だけ傾いているので、奥行きは distance - sy·sin(tilt)。
val tilt = camera.tilt * Math.PI / 180.0
val depth = distance - sy * sin(tilt)
if (depth <= 0.0) return null
val scale = distance / depth
sx *= scale
sy *= cos(tilt) * scale
}
val x = widthPx / 2.0 + sx
val y = heightPx / 2.0 + sy
if (!x.isFinite() || !y.isFinite()) return null
Expand All @@ -87,10 +113,29 @@ object WebMercatorScreenProjection {
camera: MapCameraPosition,
widthPx: Float,
heightPx: Float,
): GeoPoint? = fromScreenOffset(offset, camera, widthPx, heightPx, fovDegrees = null)

/** tilt 対応版の [toScreenOffset] の逆。地平線より上の点は null。 */
fun fromScreenOffset(
offset: Offset,
camera: MapCameraPosition,
widthPx: Float,
heightPx: Float,
fovDegrees: Double?,
): GeoPoint? {
if (widthPx <= 0f || heightPx <= 0f) return null
var sx = offset.x - widthPx / 2.0
var sy = offset.y - heightPx / 2.0
val distance = cameraDistance(camera, heightPx, fovDegrees)
if (distance != null) {
// toScreenOffset の透視を外す: y = D·g·cos t / (D − g·sin t) を g について解く。
val tilt = camera.tilt * Math.PI / 180.0
val denominator = distance * cos(tilt) + sy * sin(tilt)
if (denominator <= 0.0) return null
val ground = sy * distance / denominator
sx *= (distance - ground * sin(tilt)) / distance
sy = ground
}
if (camera.bearing != 0.0) {
val angle = CameraBearing.toNativeHeading(camera.bearing) * Math.PI / 180.0
val rx = sx * cos(angle) - sy * sin(angle)
Expand All @@ -114,6 +159,20 @@ object WebMercatorScreenProjection {
return GeoPoint(latitude = point.latitude, longitude = point.longitude, altitude = 0.0)
}

/**
* 画面中央の地点からカメラまでの距離([heightPx] と同じ単位)。tilt が無いか視野角が
* 分からなければ null(平面の式を使う)。
*/
private fun cameraDistance(
camera: MapCameraPosition,
heightPx: Float,
fovDegrees: Double?,
): Double? {
if (fovDegrees == null || fovDegrees <= 0.0 || camera.tilt == 0.0) return null
val halfFov = fovDegrees * Math.PI / 360.0
return heightPx / 2.0 / tan(halfFov)
}

/** Web Mercator のメートル座標を [0,1] へ。y は北が 0。 */
private fun normalize(position: GeoPointInterface): Normalized {
val projected = WebMercator.project(position)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.mapconductor.core.projection

import androidx.compose.ui.geometry.Offset
import com.mapconductor.core.features.GeoPoint
import com.mapconductor.core.map.MapCameraPosition
import com.mapconductor.core.spherical.Spherical
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.sin
import kotlin.math.tan
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test

/**
* 透視(fov あり)の WebMercatorScreenProjection を、独立に組んだ 3D のカメラ
* (MapLibre GL JS と同じ模型)と突き合わせる。
*/
class WebMercatorScreenProjectionTiltTest {
private val width = 866f
private val height = 1370f
private val fov = 36.87
private val center = GeoPoint.fromLatLong(35.6812, 139.7671)

/** 地面の世界ピクセル座標 (dx 東, dy 南) → 画面。カメラを 3D に置いて光線を投げる。 */
private fun reference(
dxEast: Double,
dySouth: Double,
tiltDeg: Double,
headingDeg: Double,
): Offset {
// 世界を -heading 回して画面の上をカメラの向きに合わせる。
val a = -headingDeg * Math.PI / 180.0
val gx = dxEast * cos(a) - dySouth * sin(a)
val gy = dxEast * sin(a) + dySouth * cos(a)
val t = tiltDeg * Math.PI / 180.0
val d = height / 2.0 / tan(fov * Math.PI / 360.0)
// カメラは中央から画面の下側へ d·sin t、上へ d·cos t。
val vx = gx
val vy = gy - d * sin(t)
val vz = -d * cos(t)
val depth = -vy * sin(t) - vz * cos(t)
val up = -vy * cos(t) + vz * sin(t)
return Offset((width / 2.0 + d * vx / depth).toFloat(), (height / 2.0 - d * up / depth).toFloat())
}

private fun worldOffset(
point: GeoPoint,
zoom: Double,
): Pair<Double, Double> {
val world = 256.0 * 2.0.pow(zoom)
val extent = 2.0 * WEB_MERCATOR_MAX_EXTENT_METERS
val c = WebMercator.project(center)
val p = WebMercator.project(point)
return Pair((p.x - c.x) / extent * world, -(p.y - c.y) / extent * world)
}

@Test
fun matchesAnIndependentCameraAtEveryTiltAndBearing() {
val points =
listOf(
GeoPoint.fromLatLong(35.6852, 139.7671),
GeoPoint.fromLatLong(35.6772, 139.7711),
GeoPoint.fromLatLong(35.6812, 139.7611),
GeoPoint.fromLatLong(35.6840, 139.7700),
)
for (tilt in listOf(0.0, 30.0, 60.0)) {
for (bearing in listOf(0.0, 90.0, -90.0, 30.0)) {
val camera = MapCameraPosition(position = center, zoom = 15.0, bearing = bearing, tilt = tilt)
for (point in points) {
val (dx, dy) = worldOffset(point, 15.0)
val heading = -bearing
val expected = reference(dx, dy, tilt, heading)
val actual = WebMercatorScreenProjection.toScreenOffset(point, camera, width, height, fov)
assertNotNull(actual)
assertEquals("x tilt=$tilt bearing=$bearing", expected.x, actual!!.x, 0.05f)
assertEquals("y tilt=$tilt bearing=$bearing", expected.y, actual.y, 0.05f)
}
}
}
}

@Test
fun roundTripsWithinCentimetres() {
for (tilt in listOf(0.0, 30.0, 60.0)) {
for (bearing in listOf(0.0, 90.0, -90.0)) {
val camera = MapCameraPosition(position = center, zoom = 16.0, bearing = bearing, tilt = tilt)
for ((x, y) in listOf(100f to 100f, 433f to 685f, 800f to 1300f, 433f to 20f)) {
val geo = WebMercatorScreenProjection.fromScreenOffset(Offset(x, y), camera, width, height, fov)
assertNotNull(geo)
val back = WebMercatorScreenProjection.toScreenOffset(geo!!, camera, width, height, fov)!!
assertEquals(x, back.x, 0.01f)
assertEquals(y, back.y, 0.01f)
}
}
}
}

@Test
fun withoutFovOrTiltItIsTheFlatProjection() {
val point = GeoPoint.fromLatLong(35.6852, 139.7700)
val flat = MapCameraPosition(position = center, zoom = 15.0, bearing = 30.0, tilt = 0.0)
assertEquals(
WebMercatorScreenProjection.toScreenOffset(point, flat, width, height),
WebMercatorScreenProjection.toScreenOffset(point, flat, width, height, fov),
)
val tilted = flat.copy(tilt = 45.0)
assertEquals(
WebMercatorScreenProjection.toScreenOffset(point, tilted, width, height),
WebMercatorScreenProjection.toScreenOffset(point, tilted, width, height, null),
)
}

@Test
fun aRayAboveTheHorizonHasNoGroundPoint() {
// tilt 85 で画面最上部の光線は 85 + 18.4 > 90 度。
val camera = MapCameraPosition(position = center, zoom = 15.0, tilt = 85.0)
assertNull(WebMercatorScreenProjection.fromScreenOffset(Offset(433f, 0f), camera, width, height, fov))
// 中央は必ず解ける。
val mid = WebMercatorScreenProjection.fromScreenOffset(Offset(433f, 685f), camera, width, height, fov)
assertEquals(0.0, Spherical.computeDistanceBetween(mid!!, center), 0.01)
}
}
Loading