Skip to content

Commit

Permalink
Replace ARPositionStrategy with ARPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Dec 9, 2023
1 parent c5c1bd5 commit 7ab5368
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.kylecorry.andromeda.core.units.PixelCoordinate
import com.kylecorry.sol.units.Distance
import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon
import com.kylecorry.trail_sense.navigation.ui.DrawerBitmapLoader
import com.kylecorry.trail_sense.tools.augmented_reality.position.GeographicARPoint
import kotlin.math.hypot

// TODO: Figure out what to pass for the visible distance: d = 1.2246 * sqrt(h) where d is miles and h is feet (or move it to the consumer)
Expand Down Expand Up @@ -93,37 +94,41 @@ class ARBeaconLayer(
// TODO: Avoid recreating markers every time - it will help if this didn't filter nearby beacons
// if (!areBeaconsUpToDate) {
// TODO: Change opacity if navigating
layer.setMarkers(visible.flatMap {
val beacon = it.first
listOfNotNull(
ARMarkerImpl.geographic(
layer.setMarkers(visible.flatMap {
val beacon = it.first
listOfNotNull(
ARMarker(
GeographicARPoint(
beacon.coordinate,
beacon.elevation,
beaconSize.distance,
CircleCanvasObject(beacon.color, Color.WHITE),
onFocusedFn = {
onFocus(beacon)
},
onClickFn = {
onClick(beacon)
}
),
beacon.icon?.let { icon ->
val color = Colors.mostContrastingColor(Color.WHITE, Color.BLACK, beacon.color)
ARMarkerImpl.geographic(
CircleCanvasObject(beacon.color, Color.WHITE),
onFocusedFn = {
onFocus(beacon)
},
onClickFn = {
onClick(beacon)
}
),
beacon.icon?.let { icon ->
val color = Colors.mostContrastingColor(Color.WHITE, Color.BLACK, beacon.color)
ARMarker(
GeographicARPoint(
beacon.coordinate,
beacon.elevation,
beaconSize.distance,
BitmapCanvasObject(
loader.load(icon.icon, loadedImageSize),
0.75f,
tint = color
),
keepFacingUp = true
)
}
)
})
beaconSize.distance
),
BitmapCanvasObject(
loader.load(icon.icon, loadedImageSize),
0.75f,
tint = color
),
keepFacingUp = true
)
}
)
})
// areBeaconsUpToDate = true
// }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.kylecorry.andromeda.canvas.ICanvasDrawer
import com.kylecorry.andromeda.core.units.PixelCoordinate
import com.kylecorry.sol.math.SolMath
import com.kylecorry.sol.math.SolMath.normalizeAngle
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPositionStrategy
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPoint
import kotlin.math.hypot
import kotlin.math.min
import kotlin.math.roundToInt
Expand All @@ -20,10 +20,10 @@ class ARLineLayer(

private val path = Path()

private val lines = mutableListOf<List<ARPositionStrategy>>()
private val lines = mutableListOf<List<ARPoint>>()
private val lineLock = Any()

fun setLines(lines: List<List<ARPositionStrategy>>) {
fun setLines(lines: List<List<ARPoint>>) {
synchronized(lineLock) {
this.lines.clear()
this.lines.addAll(lines)
Expand Down Expand Up @@ -100,7 +100,7 @@ class ARLineLayer(
*/
private fun getLinePixels(
view: AugmentedRealityView,
line: List<ARPositionStrategy>,
line: List<ARPoint>,
resolutionDegrees: Float,
maxDistance: Float,
): List<List<PixelCoordinate>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
package com.kylecorry.trail_sense.tools.augmented_reality

import com.kylecorry.andromeda.canvas.ICanvasDrawer
import com.kylecorry.sol.units.Coordinate
import com.kylecorry.trail_sense.shared.canvas.PixelCircle
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPositionStrategy
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPoint
import com.kylecorry.trail_sense.tools.augmented_reality.position.GeographicARPoint
import com.kylecorry.trail_sense.tools.augmented_reality.position.SphericalARPoint

interface ARMarker: ARPositionStrategy {
fun draw(view: AugmentedRealityView, drawer: ICanvasDrawer, area: PixelCircle)
fun onFocused(): Boolean
fun onClick(): Boolean
class ARMarker(
private val point: ARPoint,
private val canvasObject: CanvasObject,
private val keepFacingUp: Boolean = false,
private val onFocusedFn: (() -> Boolean) = { false },
private val onClickFn: () -> Boolean = { false }
) {

fun draw(view: AugmentedRealityView, drawer: ICanvasDrawer, area: PixelCircle) {
drawer.push()
if (keepFacingUp) {
drawer.rotate(view.sideInclination, area.center.x, area.center.y)
}
canvasObject.draw(drawer, area)
drawer.pop()
}

/**
* Gets the location of the marker on the screen
* @param view The AR view
* @return The location of the marker
*/
fun getViewLocation(view: AugmentedRealityView): PixelCircle {
val coordinates = point.getHorizonCoordinate(view)
val angularDiameter = point.getAngularDiameter(view)
val diameter = view.sizeToPixel(angularDiameter)
return PixelCircle(
view.toPixel(coordinates),
diameter / 2f
)
}

fun onFocused(): Boolean {
return onFocusedFn()
}

fun onClick(): Boolean {
return onClickFn()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ class ARMarkerLayer(
minimumPixelSize: Float,
maximumPixelSize: Float
): PixelCircle {
val coordinates = marker.getHorizonCoordinate(view)
val angularDiameter = marker.getAngularDiameter(view)
val diameter = view.sizeToPixel(angularDiameter)
return PixelCircle(
view.toPixel(coordinates),
diameter.coerceIn(minimumPixelSize, maximumPixelSize) / 2f
val circle = marker.getViewLocation(view)
return circle.copy(
radius = circle.radius.coerceIn(minimumPixelSize / 2f, maximumPixelSize / 2f)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import com.kylecorry.trail_sense.shared.permissions.alertNoCameraPermission
import com.kylecorry.trail_sense.shared.permissions.requestCamera
import com.kylecorry.trail_sense.shared.sensors.LocationSubsystem
import com.kylecorry.andromeda.fragments.observeFlow
import com.kylecorry.trail_sense.tools.augmented_reality.position.GeographicPositionStrategy
import com.kylecorry.trail_sense.tools.augmented_reality.position.GeographicARPoint
import com.kylecorry.trail_sense.tools.augmented_reality.position.SphericalARPoint
import kotlinx.coroutines.Dispatchers
import java.time.Duration
import java.time.LocalDate
Expand Down Expand Up @@ -104,7 +105,7 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
if (it == null) {
binding.arView.clearGuide()
} else {
binding.arView.guideTo(GeographicPositionStrategy(it.coordinate, it.elevation)) {
binding.arView.guideTo(GeographicARPoint(it.coordinate, it.elevation)) {
// Do nothing when reached
}
}
Expand Down Expand Up @@ -249,11 +250,13 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
moonAfterPathObject
}

ARMarkerImpl.horizon(
astro.getMoonAzimuth(location, it).value,
astro.getMoonAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f,
ARMarker(
SphericalARPoint(
astro.getMoonAzimuth(location, it).value,
astro.getMoonAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f
),
canvasObject = obj,
onFocusedFn = {
binding.arView.focusText =
Expand All @@ -275,11 +278,13 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
sunAfterPathObject
}

ARMarkerImpl.horizon(
astro.getSunAzimuth(location, it).value,
astro.getSunAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f,
ARMarker(
SphericalARPoint(
astro.getSunAzimuth(location, it).value,
astro.getSunAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f
),
canvasObject = obj,
onFocusedFn = {
binding.arView.focusText =
Expand All @@ -304,11 +309,13 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
val moonImageSize = Resources.dp(requireContext(), 24f).toInt()
val moonBitmap = moonIcon?.toBitmapOrNull(moonImageSize, moonImageSize)

val moon = ARMarkerImpl.horizon(
moonAzimuth,
moonAltitude,
isTrueNorth = true,
angularDiameter = 2f,
val moon = ARMarker(
SphericalARPoint(
moonAzimuth,
moonAltitude,
isTrueNorth = true,
angularDiameter = 2f
),
canvasObject = moonBitmap?.let { BitmapCanvasObject(moonBitmap) }
?: CircleCanvasObject(Color.WHITE),
onFocusedFn = {
Expand All @@ -318,11 +325,13 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
}
)

val sun = ARMarkerImpl.horizon(
sunAzimuth,
sunAltitude,
isTrueNorth = true,
angularDiameter = 2f,
val sun = ARMarker(
SphericalARPoint(
sunAzimuth,
sunAltitude,
isTrueNorth = true,
angularDiameter = 2f
),
canvasObject = CircleCanvasObject(AppColor.Yellow.color),
onFocusedFn = {
binding.arView.focusText = getString(R.string.sun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.kylecorry.trail_sense.shared.declination.DeclinationUtils
import com.kylecorry.trail_sense.shared.sensors.SensorService
import com.kylecorry.trail_sense.shared.text
import com.kylecorry.trail_sense.shared.textDimensions
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPositionStrategy
import com.kylecorry.trail_sense.tools.augmented_reality.position.ARPoint
import java.time.Duration
import kotlin.math.atan2

Expand Down Expand Up @@ -109,7 +109,7 @@ class AugmentedRealityView : CanvasView {
private val layerLock = Any()

// Guidance
private var guideStrategy: ARPositionStrategy? = null
private var guideStrategy: ARPoint? = null
private var guideThreshold: Float? = null
private var onGuideReached: (() -> Unit)? = null

Expand Down Expand Up @@ -156,7 +156,7 @@ class AugmentedRealityView : CanvasView {
}

fun guideTo(
guideStrategy: ARPositionStrategy,
guideStrategy: ARPoint,
thresholdDegrees: Float? = null,
onReached: () -> Unit = { clearGuide() }
) {
Expand Down
Loading

0 comments on commit 7ab5368

Please sign in to comment.