Skip to content

Commit

Permalink
Remove support for curved AR lines (performance issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jan 13, 2024
1 parent f7fb4fa commit 1098344
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ARGridLayer(
private var westString: String = ""

private val lineLayer = ARLineLayer()

private val resolution = spacing / 5

init {
val regularLines = mutableListOf<List<ARPoint>>()

Expand All @@ -40,7 +43,7 @@ class ARGridLayer(
}

val line = mutableListOf<ARPoint>()
for (azimuth in 0..360 step spacing) {
for (azimuth in 0..360 step resolution) {
line.add(
SphericalARPoint(
azimuth.toFloat(),
Expand All @@ -61,7 +64,7 @@ class ARGridLayer(
}

val line = mutableListOf<ARPoint>()
for (elevation in -90..90 step spacing) {
for (elevation in -90..90 step resolution) {
line.add(
SphericalARPoint(
azimuth.toFloat(),
Expand All @@ -75,13 +78,13 @@ class ARGridLayer(

// North line
val northLine = mutableListOf<ARPoint>()
for (elevation in -90..90 step spacing) {
for (elevation in -90..90 step resolution) {
northLine.add(SphericalARPoint(0f, elevation.toFloat(), isTrueNorth = useTrueNorth))
}

// Horizon line
val horizonLine = mutableListOf<ARPoint>()
for (azimuth in 0..360 step spacing) {
for (azimuth in 0..360 step resolution) {
horizonLine.add(SphericalARPoint(azimuth.toFloat(), 0f, isTrueNorth = useTrueNorth))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ data class ARLine(
val points: List<ARPoint>,
@ColorInt val color: Int,
val thickness: Float,
val thicknessUnits: ThicknessUnits = ThicknessUnits.Dp,
val curved: Boolean = true
val thicknessUnits: ThicknessUnits = ThicknessUnits.Dp
) {
// TODO: Extract this
enum class ThicknessUnits {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class ARLineLayer : ARLayer {
}

override fun draw(drawer: ICanvasDrawer, view: AugmentedRealityView) {
val maxAngle = hypot(view.fov.width, view.fov.height) * 1.5f
val resolutionDegrees = (maxAngle / 10f).roundToInt().coerceIn(1, 5)



drawer.noFill()
drawer.strokeJoin(StrokeJoin.Round)
drawer.strokeCap(StrokeCap.Round)
Expand All @@ -61,88 +56,14 @@ class ARLineLayer : ARLayer {
}
drawer.strokeWeight(thicknessPx)

if (line.curved) {
// TODO: Only calculate this higher resolution path once or only for the visible portion
val pixels = getLinePixels(
view,
line.points,
resolutionDegrees.toFloat()
)
render(pixels, view, path)
} else {
render(line.points.map { it.getAugmentedRealityCoordinate(view) }, view, path)
}
render(line.points.map { it.getAugmentedRealityCoordinate(view) }, view, path)

drawer.path(path)
}

drawer.noStroke()
}

/**
* Given a line it returns the pixels that make up the line.
* It will split long lines into smaller chunks, using the resolutionDegrees.
* It will also clip the line to the view.
*/
private fun getLinePixels(
view: AugmentedRealityView,
line: List<ARPoint>,
resolutionDegrees: Float,
): List<AugmentedRealityCoordinate> {
val pixels = mutableListOf<AugmentedRealityCoordinate>()
var previousCoordinate: AugmentedRealityCoordinate? = null
for (point in line) {
val coord = point.getAugmentedRealityCoordinate(view)
pixels.addAll(
if (previousCoordinate != null) {
splitLine(previousCoordinate, coord, resolutionDegrees)
} else {
listOf(coord)
}
)

previousCoordinate = coord
}

return pixels
}

// TODO: Should this operate on pixels or coordinates? - if it is pixels, it will be linear, if it is coordinates it will be curved
private fun splitLine(
start: AugmentedRealityCoordinate,
end: AugmentedRealityCoordinate,
resolutionDegrees: Float
): List<AugmentedRealityCoordinate> {
val coordinates = mutableListOf<AugmentedRealityCoordinate>()
coordinates.add(start)
val bearingDelta = if (start.hasBearing && end.hasBearing) {
SolMath.deltaAngle(start.bearing, end.bearing)
} else {
0f
}
val elevationDelta = end.elevation - start.elevation
val distanceDelta = end.distance - start.distance
val distance = hypot(bearingDelta, elevationDelta)
val steps = (distance / resolutionDegrees).roundToInt()
val bearingStep = bearingDelta / steps
val elevationStep = elevationDelta / steps
val distanceStep = distanceDelta / steps
for (i in 1..steps) {
coordinates.add(
AugmentedRealityCoordinate.fromSpherical(
normalizeAngle((if (start.hasBearing) start.bearing else end.bearing) + bearingStep * i),
start.elevation + elevationStep * i,
start.distance + distanceStep * i
)
)
}

// Add the end point
coordinates.add(end)

return coordinates
}

override fun invalidate() {
// Do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ class ClinometerFragment : BoundFragment<FragmentClinometerBinding>() {
)
),
Color.WHITE,
1f,
curved = false
1f
)
)
)
Expand Down

0 comments on commit 1098344

Please sign in to comment.