Skip to content

Commit

Permalink
Improve drawing of sun / moon in AR
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Dec 15, 2023
1 parent 6df548d commit f33acaf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ import java.time.ZoneId
import java.time.ZonedDateTime

class ARAstronomyLayer(
private val drawLines: Boolean,
private val onSunFocus: (time: ZonedDateTime) -> Boolean,
private val onMoonFocus: (time: ZonedDateTime) -> Boolean
) : ARLayer {

private val scope = CoroutineScope(Dispatchers.Default)
private val runner = CoroutineQueueRunner()

private val sunLineLayer = ARLineLayer(
AppColor.Yellow.color,
curved = true
)
private val sunLayer = ARMarkerLayer()
private val moonLineLayer = ARLineLayer(
Color.WHITE,
curved = true
)
private val moonLayer = ARMarkerLayer()

private val astro = AstronomyService()
Expand All @@ -50,11 +59,17 @@ class ARAstronomyLayer(
updatePositions(drawer, location, ZonedDateTime.now())
}

if (drawLines) {
moonLineLayer.draw(drawer, view)
sunLineLayer.draw(drawer, view)
}
moonLayer.draw(drawer, view)
sunLayer.draw(drawer, view)
}

override fun invalidate() {
moonLineLayer.invalidate()
sunLineLayer.invalidate()
moonLayer.invalidate()
sunLayer.invalidate()
}
Expand All @@ -80,22 +95,22 @@ class ARAstronomyLayer(

val moonBeforePathObject = CanvasCircle(
Color.WHITE,
opacity = 20
opacity = 60
)

val moonAfterPathObject = CanvasCircle(
Color.WHITE,
opacity = 127
opacity = 200
)

val sunBeforePathObject = CanvasCircle(
AppColor.Yellow.color,
opacity = 20
opacity = 60
)

val sunAfterPathObject = CanvasCircle(
AppColor.Yellow.color,
opacity = 127
opacity = 200
)

val moonPositions = Time.getReadings(
Expand All @@ -114,7 +129,7 @@ class ARAstronomyLayer(
astro.getMoonAzimuth(location, it).value,
astro.getMoonAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f
angularDiameter = 0.5f
),
canvasObject = obj,
onFocusedFn = {
Expand All @@ -137,7 +152,7 @@ class ARAstronomyLayer(
astro.getSunAzimuth(location, it).value,
astro.getSunAltitude(location, it),
isTrueNorth = true,
angularDiameter = 1f
angularDiameter = 0.5f
),
canvasObject = obj,
onFocusedFn = {
Expand Down Expand Up @@ -184,6 +199,12 @@ class ARAstronomyLayer(
}
)

sunLineLayer.setLines(listOf(sunPositions.map { it.point } + listOfNotNull(
sunPositions.firstOrNull()?.point
)))
moonLineLayer.setLines(listOf(moonPositions.map { it.point } + listOfNotNull(
moonPositions.firstOrNull()?.point
)))
sunLayer.setMarkers(sunPositions + sun)
moonLayer.setMarkers(moonPositions + moon)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import kotlin.math.roundToInt
// TODO: Create a generic version of this that works like the path tool. The consumers should be able to specify the line style, color, thickness, and whether it should be curved or straight between points
class ARLineLayer(
@ColorInt private val color: Int = Color.WHITE,
private val thicknessDp: Float = 1f
private val thicknessDp: Float = 1f,
private val curved: Boolean = true
) : ARLayer {

private val path = Path()
Expand Down Expand Up @@ -55,38 +56,40 @@ class ARLineLayer(
path.reset()
drawer.stroke(color)

// TODO: This should split the lines into smaller chunks (which will allow distance splitting) - keeping it this way for now for the clinometer
var previous: PixelCoordinate? = null
for (point in line){
val pixel = view.toPixel(point.getHorizonCoordinate(view))
// TODO: This should split the line if the distance is too great
if (previous != null) {
path.lineTo(pixel.x, pixel.y)
} else {
path.moveTo(pixel.x, pixel.y)
if (curved) {
// Curved + increased resolution
val pixels = getLinePixels(
view,
line,
resolutionDegrees.toFloat(),
maxDistance.toFloat()
)
for (pixelLine in pixels) {
var previous: PixelCoordinate? = null
for (pixel in pixelLine) {
if (previous != null) {
path.lineTo(pixel.x, pixel.y)
} else {
path.moveTo(pixel.x, pixel.y)
}
previous = pixel
}
}
} else {
// TODO: This should split the lines into smaller chunks (which will allow distance splitting) - keeping it this way for now for the clinometer
var previous: PixelCoordinate? = null
for (point in line) {
val pixel = view.toPixel(point.getHorizonCoordinate(view))
// TODO: This should split the line if the distance is too great
if (previous != null) {
path.lineTo(pixel.x, pixel.y)
} else {
path.moveTo(pixel.x, pixel.y)
}
previous = pixel
}
previous = pixel
}

// Curved + increased resolution
// val pixels = getLinePixels(
// view,
// line,
// resolutionDegrees.toFloat(),
// maxDistance.toFloat()
// )
// for (pixelLine in pixels) {
// var previous: PixelCoordinate? = null
// for (pixel in pixelLine) {
// if (previous != null) {
// path.lineTo(pixel.x, pixel.y)
// } else {
// path.moveTo(pixel.x, pixel.y)
// }
// previous = pixel
// }
// }

drawer.path(path)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.kylecorry.trail_sense.tools.augmented_reality.position.GeographicARPo
import com.kylecorry.trail_sense.tools.augmented_reality.position.SphericalARPoint

class ARMarker(
private val point: ARPoint,
val point: ARPoint,
private val canvasObject: CanvasObject,
private val keepFacingUp: Boolean = false,
private val onFocusedFn: (() -> Boolean) = { false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
}

private val astronomyLayer by lazy {
ARAstronomyLayer(this::onSunFocused, this::onMoonFocused)
ARAstronomyLayer(false, this::onSunFocused, this::onMoonFocused)
}

private val navigator by lazy { Navigator.getInstance(requireContext()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ClinometerFragment : BoundFragment<FragmentClinometerBinding>() {

// Augmented reality
private val markerLayer = ARMarkerLayer()
private val lineLayer = ARLineLayer()
private val lineLayer = ARLineLayer(curved = false)
private var startMarker: ARPoint? = null
private var endMarker: ARPoint? = null

Expand Down

0 comments on commit f33acaf

Please sign in to comment.