@@ -48,8 +48,11 @@ internal class VanishingRouteLine {
4848 private var indexOfLastStepPointsLoadedInTree = 0
4949 private val distanceToLastStepPointInMeters = 20.0
5050 private var stepPointRange: Range <Int >? = null
51- private val stepPointRangeSize = 5
52- private val maxAllowedFillerPointListsInTree = 3
51+ private val stepPointRangeSize = 2
52+ // private val maxAllowedFillerPointListsInTree = 3
53+ private val maxTrailingFillerPoints = 10
54+ private val treeAdjustmentFrequency = 10
55+ private var treeAdjustmentCounter = 0
5356
5457 fun setScope (scope : CoroutineScope ) {
5558 this .scope = scope
@@ -183,6 +186,7 @@ internal class VanishingRouteLine {
183186 fillerPointsInTree.clear()
184187 indexOfLastStepPointsLoadedInTree = 0
185188 vanishPointOffset = 0.0
189+ treeAdjustmentCounter = 0
186190
187191 if (distances.flatStepDistances.isNotEmpty()) {
188192 val endRange = if (distances.flatStepDistances.size > stepPointRangeSize) {
@@ -257,7 +261,7 @@ internal class VanishingRouteLine {
257261 locationSearchTree.addAll(fillerPoints)
258262 fillerPointsInTree.addAll(fillerPoints)
259263 }
260- indexOfLastStepPointsLoadedInTree = endOfRange
264+ indexOfLastStepPointsLoadedInTree = range.lower
261265 }
262266 }
263267 }
@@ -282,52 +286,60 @@ internal class VanishingRouteLine {
282286 * range are added to the search tree and the points passed are removed.
283287 */
284288 private fun adjustTree (point : Point , nearestNeighbor : RouteLineDistancesIndex ? ) {
285- scope?.launch(Dispatchers .Main .immediate) {
286- if (fillerPointsInTree.isNotEmpty()) {
287- val nearEndStepPoint = fillerPointsInTree.last()
288- val distanceToNearEndStepPoint = TurfMeasurement .distance(
289- point,
290- nearEndStepPoint.point,
291- TurfConstants .UNIT_METERS
292- )
293- if (distanceToNearEndStepPoint <= distanceToLastStepPointInMeters) {
294- stepPointRange = ifNonNull(stepPointRange, granularDistances)
295- { currentStepPointRange, distances ->
296- val endOfRange =
297- if (
298- currentStepPointRange.upper + stepPointRangeSize
299- < distances.flatStepDistances.lastIndex
300- ) {
301- currentStepPointRange.upper + stepPointRangeSize
302- } else {
303- distances.flatStepDistances.lastIndex
304- }
305- Range (currentStepPointRange.upper - 1 , endOfRange).also {
306- val fillerPoints = getFillerPointsForRange(
307- it,
308- distances.flatStepDistances
309- )
310- if (fillerPoints.isNotEmpty()) {
311- locationSearchTree.addAll(fillerPoints)
312- fillerPointsInTree.addAll(fillerPoints)
313- }
314- indexOfLastStepPointsLoadedInTree = endOfRange
289+ treeAdjustmentCounter++
290+ if (treeAdjustmentCounter >= treeAdjustmentFrequency) {
291+ treeAdjustmentCounter = 0
292+ scope?.launch(Dispatchers .Main .immediate) {
293+ loadNextRange(point)
294+ trimTree(nearestNeighbor)
295+ }
296+ }
297+ }
298+
299+ private fun loadNextRange (point : Point ) {
300+ if (fillerPointsInTree.isNotEmpty()) {
301+ val nearEndStepPoint = fillerPointsInTree.last()
302+ val distanceToNearEndStepPoint = TurfMeasurement .distance(
303+ point,
304+ nearEndStepPoint.point,
305+ TurfConstants .UNIT_METERS
306+ )
307+ if (distanceToNearEndStepPoint <= distanceToLastStepPointInMeters) {
308+ stepPointRange = ifNonNull(stepPointRange, granularDistances)
309+ { currentStepPointRange, distances ->
310+ val endOfRange =
311+ if (
312+ currentStepPointRange.upper + stepPointRangeSize
313+ < distances.flatStepDistances.lastIndex
314+ ) {
315+ currentStepPointRange.upper + stepPointRangeSize
316+ } else {
317+ distances.flatStepDistances.lastIndex
315318 }
319+ Range (currentStepPointRange.upper - 1 , endOfRange).also {
320+ val fillerPoints = getFillerPointsForRange(
321+ it,
322+ distances.flatStepDistances
323+ )
324+ if (fillerPoints.isNotEmpty()) {
325+ locationSearchTree.addAll(fillerPoints)
326+ fillerPointsInTree.addAll(fillerPoints)
327+ }
328+ indexOfLastStepPointsLoadedInTree = endOfRange
316329 }
317- // if (fillerPointsInTree.size == maxAllowedFillerPointListsInTree) {
318- // val pointsToDrop = fillerPointsInTree.removeFirst()
319- // locationSearchTree.removeAll(pointsToDrop)
320- // }
321330 }
331+ }
332+ }
333+ }
322334
323- ifNonNull (nearestNeighbor) {
324- val index = fillerPointsInTree.indexOf(it )
325- if (index > 10 ) {
326- fillerPointsInTree.take(index). apply {
327- fillerPointsInTree.removeAll( this )
328- locationSearchTree.removeAll( this )
329- }
330- }
335+ private fun trimTree (nearestNeighbor : RouteLineDistancesIndex ? ) {
336+ // Log.e("foobar", "trimming tree" )
337+ ifNonNull(nearestNeighbor ) {
338+ val index = fillerPointsInTree.indexOf(it)
339+ if (index > maxTrailingFillerPoints) {
340+ fillerPointsInTree.take(index). apply {
341+ fillerPointsInTree.removeAll( this )
342+ locationSearchTree.removeAll( this )
331343 }
332344 }
333345 }
0 commit comments