Modern Apps git

← Modern-Apps.git

astronomy: fix AR display direction #422

Commit
41f442bfe7ed59bea53ff92e3750d384f89a5c18
Author
Vayun Mathur <[email protected]>
When
2026-08-04 13:51 UTC (4 months ago)
Parents
de976bf

Diff

astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/StereographicProjection.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/StereographicProjection.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/StereographicProjection.ktindex f2763929cc..0a411072d0 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/StereographicProjection.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/StereographicProjection.kt@@ -88,7 +88,10 @@ class StereographicProjection(private val viewState: ViewState) : SkyProjection         val dAz = (altAz.azRad - centerAz).let { ((it + PI) % (2*PI)) - PI }         val sinDaz = sin(dAz); val cosDaz = cos(dAz)         val phi = atan2(sinDaz * cosAlt, cosCenterAlt * sinAlt - sinCenterAlt * cosAlt * cosDaz)-        val x = rho * sin(phi); val y = rho * cos(phi) // inversion fix: move up shows up+        // Forward-facing window view: +x = increasing azimuth (right), higher altitude+        // than center (phi≈0) maps to -y (up on screen). This is the exact inverse of+        // `unproject`'s atan2(x, -y).+        val x = rho * sin(phi); val y = -rho * cos(phi)         val cosR = cos(viewState.rotationRad); val sinR = sin(viewState.rotationRad)         val xr = x * cosR - y * sinR; val yr = x * sinR + y * cosR         return Offset((viewState.screenW / 2 + xr).toFloat(), (viewState.screenH / 2 + yr).toFloat())
astronomy/src/main/java/com/vayunmathur/astronomy/domain/sensor/OrientationManager.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/sensor/OrientationManager.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/sensor/OrientationManager.ktindex c5bcc1cdfd..a08bf7a15a 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/sensor/OrientationManager.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/sensor/OrientationManager.kt@@ -20,10 +20,13 @@ data class DeviceOrientation(     val rollDeg: Double,     val accuracy: Int = SensorManager.SENSOR_STATUS_ACCURACY_HIGH,     val declinationDeg: Double = 0.0,-    // Full 3-axis device pointing: screen normal +Z in world ENU-    // Flat screen-up => +Z Up => alt +90 zenith, vertical => horizon 0°, flat down => nadir -90°-    // Az uses +Z projection onto horizontal plane (stable vs charging-port up/down)-    // Roll fixes backwards: viewRotation = roll (inverted) so content tracks phone+    // "Window" model: the view looks where the BACK of the phone points, i.e. the+    // device -Z axis (the camera's optical axis), expressed in world ENU. Hold the+    // phone up like a pane of glass and the sky drawn matches what's behind it (and+    // what the AR camera sees). Flat screen-up => back faces the ground => nadir;+    // vertical => horizon; back facing the zenith => alt +90.+    // viewRotationDeg is the true roll about that -Z axis, so content stays fixed to+    // the sky as the phone rolls.     val pointingAzTrueDeg: Double = azimuthTrueDeg,     val pointingAltDeg: Double = 0.0,     val viewRotationDeg: Double = 0.0@@ -49,6 +52,7 @@ class OrientationManager(private val context: Context) : SensorEventListener {     private var smoothedRoll = 0.0     private var smoothedPointingAzMag = 0.0     private var smoothedPointingAlt = 0.0+    private var smoothedViewRot = 0.0     private var first = true     private val alpha = 0.15f     private val alphaPointing = 0.15f@@ -114,10 +118,12 @@ class OrientationManager(private val context: Context) : SensorEventListener {         val pitch = Math.toDegrees(o[1].toDouble()); val roll = Math.toDegrees(o[2].toDouble())          val (pointAzMag, pointAlt) = pointingFromDeviceToWorld(RdeviceToWorld)+        val vrot = viewRotationFromDeviceToWorld(RdeviceToWorld)          if (first) {             smoothedAz = azMag; smoothedPitch = pitch; smoothedRoll = roll             smoothedPointingAzMag = pointAzMag; smoothedPointingAlt = pointAlt+            smoothedViewRot = if (vrot.isNaN()) 0.0 else vrot             first = false         } else {             var d = azMag - smoothedAz; d = ((d + 540) % 360) - 180@@ -127,12 +133,10 @@ class OrientationManager(private val context: Context) : SensorEventListener {             var dp = pointAzMag - smoothedPointingAzMag; dp = ((dp + 540) % 360) - 180             smoothedPointingAzMag = (smoothedPointingAzMag + dp * alphaPointing + 360) % 360             smoothedPointingAlt = smoothedPointingAlt + (pointAlt - smoothedPointingAlt) * alphaPointing+            if (!vrot.isNaN()) { var dv = vrot - smoothedViewRot; dv = ((dv + 540) % 360) - 180; smoothedViewRot += dv * alpha }         }         val trueAz = (smoothedAz + declinationDeg + 360) % 360         val pointTrueAz = (smoothedPointingAzMag + declinationDeg + 360) % 360-        // Inversion fix: roll and altitude were backwards (move up shows down)-        // Alt: screen normal +Z gives correct anchor (flat 90, vertical 0). Keep true for numbers.-        // Roll: invert so sky rotates with device, not against.         _orientation.value = DeviceOrientation(             azimuthTrueDeg = trueAz,             azimuthMagDeg = smoothedAz,@@ -142,21 +146,48 @@ class OrientationManager(private val context: Context) : SensorEventListener {             declinationDeg = declinationDeg,             pointingAzTrueDeg = pointTrueAz,             pointingAltDeg = smoothedPointingAlt,-            viewRotationDeg = -smoothedRoll+            viewRotationDeg = smoothedViewRot         )     } -    // Screen-normal +Z pointing: flat up = +90 zenith, vertical = 0 horizon, flat down = -90 nadir-    // Stable vs charging-port up/down (uses column2, not Y)+    // Window model: the view forward is the back of the phone = device -Z axis (the+    // camera's optical axis) in world ENU. -Z is the negated third column of the+    // device->world matrix. Back facing the zenith => alt +90, horizon => 0, ground+    // => -90. Uses the whole axis (not Y) so it's stable to charging-port up/down.     private fun pointingFromDeviceToWorld(R: FloatArray): Pair<Double, Double> {-        val east = R[2].toDouble()-        val north = R[5].toDouble()-        val up = R[8].toDouble()+        val east = -R[2].toDouble()+        val north = -R[5].toDouble()+        val up = -R[8].toDouble()         var az = Math.toDegrees(atan2(east, north)); az = (az + 360) % 360         val alt = Math.toDegrees(asin(up.coerceIn(-1.0, 1.0)))         return az to alt     } +    // Roll about the viewing axis, returned as the rotation (deg) to apply to the+    // projected sky so celestial-up lines up with the phone's physical up. Derived+    // straight from the rotation matrix (getOrientation's "roll" is about device Y,+    // which is the wrong axis once the phone is held vertical to look at the sky).+    // Returns NaN near the zenith/nadir where roll is undefined.+    private fun viewRotationFromDeviceToWorld(R: FloatArray): Double {+        // Camera forward = device -Z (out the back, toward the sky).+        val fx = -R[2].toDouble(); val fy = -R[5].toDouble(); val fz = -R[8].toDouble()+        // Device screen "up" = +Y column (top edge of the phone), already ⟂ to f.+        val ux = R[1].toDouble(); val uy = R[4].toDouble(); val uz = R[7].toDouble()+        // Celestial-up reference: world zenith (0,0,1) projected into the screen plane.+        val zf = fz // world zenith · f+        var px = -zf * fx; var py = -zf * fy; var pz = 1.0 - zf * fz+        val pl = sqrt(px * px + py * py + pz * pz)+        if (pl < 1e-6) return Double.NaN // looking near straight up/down: roll undefined+        px /= pl; py /= pl; pz /= pl+        val dot = px * ux + py * uy + pz * uz+        // (P_up × u) · f => signed angle of device-up from celestial-up about f.+        val cx = py * uz - pz * uy; val cy = pz * ux - px * uz; val cz = px * uy - py * ux+        val crossDotF = cx * fx + cy * fy + cz * fz+        val rollRad = atan2(crossDotF, dot)+        // Rotate the drawn sky by -roll so it stays fixed to the world as the phone rolls.+        return Math.toDegrees(-rollRad)+    }+     @Suppress("DEPRECATION")     private fun tryFallback() {         if (!hasAccel || !hasMag) return@@ -176,9 +207,11 @@ class OrientationManager(private val context: Context) : SensorEventListener {         val azMag = (Math.toDegrees(o[0].toDouble()) + 360) % 360         val pitch = Math.toDegrees(o[1].toDouble()); val roll = Math.toDegrees(o[2].toDouble())         val (pAz, pAlt) = pointingFromDeviceToWorld(R)+        val vrot = viewRotationFromDeviceToWorld(R)         if (first) {             smoothedAz = azMag; smoothedPitch = pitch; smoothedRoll = roll             smoothedPointingAzMag = pAz; smoothedPointingAlt = pAlt+            smoothedViewRot = if (vrot.isNaN()) 0.0 else vrot             first = false         } else {             var d = azMag - smoothedAz; d = ((d + 540) % 360) - 180@@ -188,6 +221,7 @@ class OrientationManager(private val context: Context) : SensorEventListener {             var dp = pAz - smoothedPointingAzMag; dp = ((dp + 540) % 360) - 180             smoothedPointingAzMag = (smoothedPointingAzMag + dp * alphaPointing + 360) % 360             smoothedPointingAlt = smoothedPointingAlt + (pAlt - smoothedPointingAlt) * alphaPointing+            if (!vrot.isNaN()) { var dv = vrot - smoothedViewRot; dv = ((dv + 540) % 360) - 180; smoothedViewRot += dv * alpha }         }         val trueAz = (smoothedAz + declinationDeg + 360) % 360         val pTrueAz = (smoothedPointingAzMag + declinationDeg + 360) % 360@@ -200,7 +234,7 @@ class OrientationManager(private val context: Context) : SensorEventListener {             declinationDeg = declinationDeg,             pointingAzTrueDeg = pTrueAz,             pointingAltDeg = smoothedPointingAlt,-            viewRotationDeg = -smoothedRoll+            viewRotationDeg = smoothedViewRot         )     } }
astronomy/src/main/rust/src/lib.rs
diff --git a/astronomy/src/main/rust/src/lib.rs b/astronomy/src/main/rust/src/lib.rsindex bfb1193730..1a12fb3553 100644--- a/astronomy/src/main/rust/src/lib.rs+++ b/astronomy/src/main/rust/src/lib.rs@@ -91,6 +91,22 @@ mod tests {         assert!((out[1] as f64 - h / 2.0).abs() < 1e-3, "y not centered: {}", out[1]);     } +    #[test]+    fn project_forward_view_chirality() {+        // Window/forward view centered on the north horizon (az=0, alt=0).+        let w = 1000.0;+        let h = 1000.0;+        let ten = 10.0_f64.to_radians();+        // Higher altitude than center -> above center on screen (smaller y).+        let up = batch_project(&[0.0, ten], 0.0, 0.0, 60.0, w, h, 0.0);+        assert!((up[0] as f64 - w / 2.0).abs() < 1e-3, "up: x should stay centered");+        assert!((up[1] as f64) < h / 2.0, "higher object should be above center, got y={}", up[1]);+        // Higher azimuth (toward east) -> right of center (larger x).+        let east = batch_project(&[ten, 0.0], 0.0, 0.0, 60.0, w, h, 0.0);+        assert!((east[0] as f64) > w / 2.0, "east object should be right of center, got x={}", east[0]);+        assert!((east[1] as f64 - h / 2.0).abs() < 1e-3, "east: y should stay centered");+    }+     #[test]     fn project_outside_fov_returns_nan() {         let w = 1080.0;
astronomy/src/main/rust/src/projection.rs
diff --git a/astronomy/src/main/rust/src/projection.rs b/astronomy/src/main/rust/src/projection.rsindex 32d5456af6..2771c90553 100644--- a/astronomy/src/main/rust/src/projection.rs+++ b/astronomy/src/main/rust/src/projection.rs@@ -92,8 +92,10 @@ impl Projector {         let cos_daz = d_az.cos();         let phi = (sin_daz * cos_alt)             .atan2(self.cos_center_alt * sin_alt - self.sin_center_alt * cos_alt * cos_daz);+        // Forward-facing window view: +x = increasing azimuth (right), higher altitude+        // than center (phi≈0) maps to -y (up on screen). Matches Kotlin projectCore.         let x = rho * phi.sin();-        let y = rho * phi.cos();+        let y = -rho * phi.cos();         let cos_r = self.rotation_rad.cos();         let sin_r = self.rotation_rad.sin();         let xr = x * cos_r - y * sin_r;
astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ProjectionTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ProjectionTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ProjectionTest.ktindex e690a86564..c75db25087 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ProjectionTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ProjectionTest.kt@@ -40,6 +40,20 @@ class ProjectionTest {         }     } +    @Test+    fun stereographic_forward_view_chirality() {+        // Window/forward view on the north horizon: higher altitude renders above+        // center, higher azimuth (east) renders to the right.+        val vs = ViewState(0.0, 0.0, 60f, 1000f, 1000f)+        val proj = StereographicProjection(vs)+        val up = proj.project(AltAz(0.0, 10.0.toRad()))!!+        assertTrue(abs(up.x - 500f) < 1f, "up: x should stay centered, got ${up.x}")+        assertTrue(up.y < 500f, "higher object should be above center, got y=${up.y}")+        val east = proj.project(AltAz(10.0.toRad(), 0.0))!!+        assertTrue(east.x > 500f, "east object should be right of center, got x=${east.x}")+        assertTrue(abs(east.y - 500f) < 1f, "east: y should stay centered, got ${east.y}")+    }+     @Test     fun stereographic_horizon_culling() {         val vs = ViewState(0.0, (30.0).toRad(), 60f, 1080f, 1920f)