Skip to content

Commit

Permalink
fix restricted masking data calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dzinad committed Jul 6, 2023
1 parent daca119 commit 2d7be29
Show file tree
Hide file tree
Showing 7 changed files with 1,113 additions and 151 deletions.
21 changes: 21 additions & 0 deletions libnavui-maps/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,13 @@ package com.mapbox.navigation.ui.maps.route.line.model {
method public int getAlternativeRouteUnknownCongestionColor();
method public kotlin.ranges.IntRange getHeavyCongestionRange();
method public int getInActiveRouteLegsColor();
method public int getInactiveRouteLegClosureColor();
method public int getInactiveRouteLegHeavyCongestionColor();
method public int getInactiveRouteLegLowCongestionColor();
method public int getInactiveRouteLegModerateCongestionColor();
method public int getInactiveRouteLegRestrictedRoadColor();
method public int getInactiveRouteLegSevereCongestionColor();
method public int getInactiveRouteLegUnknownCongestionColor();
method public kotlin.ranges.IntRange getLowCongestionRange();
method public kotlin.ranges.IntRange getModerateCongestionRange();
method public int getRestrictedRoadColor();
Expand All @@ -1227,6 +1234,13 @@ package com.mapbox.navigation.ui.maps.route.line.model {
property public final int alternativeRouteUnknownCongestionColor;
property public final kotlin.ranges.IntRange heavyCongestionRange;
property public final int inActiveRouteLegsColor;
property public final int inactiveRouteLegClosureColor;
property public final int inactiveRouteLegHeavyCongestionColor;
property public final int inactiveRouteLegLowCongestionColor;
property public final int inactiveRouteLegModerateCongestionColor;
property public final int inactiveRouteLegRestrictedRoadColor;
property public final int inactiveRouteLegSevereCongestionColor;
property public final int inactiveRouteLegUnknownCongestionColor;
property public final kotlin.ranges.IntRange lowCongestionRange;
property public final kotlin.ranges.IntRange moderateCongestionRange;
property public final int restrictedRoadColor;
Expand Down Expand Up @@ -1257,6 +1271,13 @@ package com.mapbox.navigation.ui.maps.route.line.model {
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources build();
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder heavyCongestionRange(kotlin.ranges.IntRange range);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inActiveRouteLegsColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegClosureColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegHeavyCongestionColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegLowCongestionColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegModerateCongestionColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegRestrictedRoadColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegSevereCongestionColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder inactiveRouteLegUnknownCongestionColor(@ColorInt int color);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder lowCongestionRange(kotlin.ranges.IntRange range);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder moderateCongestionRange(kotlin.ranges.IntRange range);
method public com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources.Builder restrictedRoadColor(@ColorInt int color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,31 +1730,47 @@ internal object MapboxRouteLineUtils {
}
}

internal fun getRestrictedLineExpressionProducer(
internal fun getNonMaskingRestrictedLineExpressionProducer(
routeData: List<ExtractedRouteRestrictionData>,
vanishingPointOffset: Double,
activeLegIndex: Int,
routeLineOptions: MapboxRouteLineOptions,
) = RouteLineExpressionProvider {
val (restrictedSectionColor, restrictedSectionInactiveColor) =
routeLineOptions.resourceProvider.routeLineColorResources.run {
if (routeLineOptions.styleInactiveRouteLegsIndependently) {
Pair(restrictedRoadColor, inactiveRouteLegRestrictedRoadColor)
} else {
Pair(restrictedRoadColor, restrictedRoadColor)
}
}
getRestrictedLineExpression(
options: MapboxRouteLineOptions
): RouteLineExpressionProvider {
val colorResources = options.resourceProvider.routeLineColorResources
val inactiveColor = if (options.styleInactiveRouteLegsIndependently) {
colorResources.inactiveRouteLegRestrictedRoadColor
} else {
colorResources.restrictedRoadColor
}
return getRestrictedLineExpressionProducer(
routeData,
vanishingPointOffset,
activeLegIndex,
restrictedSectionColor = restrictedSectionColor,
restrictedSectionInactiveColor = restrictedSectionInactiveColor,
routeData
options.displayRestrictedRoadSections,
activeColor = colorResources.restrictedRoadColor,
inactiveColor = inactiveColor,
)
}

internal fun getDisabledRestrictedLineExpressionProducer() = RouteLineExpressionProvider {
color(Color.TRANSPARENT)
internal fun getRestrictedLineExpressionProducer(
routeData: List<ExtractedRouteRestrictionData>,
vanishingPointOffset: Double,
activeLegIndex: Int,
displayRestrictedEnabled: Boolean,
activeColor: Int,
inactiveColor: Int,
) = RouteLineExpressionProvider {
if (displayRestrictedEnabled) {
getRestrictedLineExpression(
vanishingPointOffset,
activeLegIndex,
restrictedSectionColor = activeColor,
restrictedSectionInactiveColor = inactiveColor,
routeData
)
} else {
color(Color.TRANSPARENT)
}
}

internal fun getRestrictedLineExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.extractRouteRestrictionData
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.getCongestionColorForInactiveRouteLegs
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.getMatchingColors
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.getNonMaskingRestrictedLineExpressionProducer
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.getRestrictedLineExpressionProducer
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.granularDistancesProvider
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup1SourceLayerIds
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup2SourceLayerIds
Expand Down Expand Up @@ -1003,10 +1005,14 @@ class MapboxRouteLineApi(
Color.TRANSPARENT,
legIndex
)
val restrictedExpProvider = getRestrictedLineExpressionProducerForLegIndex(
val restrictedExpProvider = getRestrictedLineExpressionProducer(
restrictedExpressionData,
0.0,
legIndex,
routeLineOptions,
restrictedExpressionData
routeLineOptions.displayRestrictedRoadSections,
activeColor = routeLineOptions.resourceProvider.routeLineColorResources
.restrictedRoadColor,
inactiveColor = Color.TRANSPARENT,
)

return RouteLineDynamicData(
Expand Down Expand Up @@ -1172,7 +1178,7 @@ class MapboxRouteLineApi(
true -> null
false -> {
{
MapboxRouteLineUtils.getRestrictedLineExpressionProducer(
getNonMaskingRestrictedLineExpressionProducer(
restrictedExpressionData,
0.0,
legIndexToHighlight,
Expand Down Expand Up @@ -1610,10 +1616,11 @@ class MapboxRouteLineApi(
// If false produce a gradient for the restricted line layer that is completely transparent.
val primaryRouteRestrictedSectionsExpressionDef = jobControl.scope.async {
primaryRoute?.route?.run {
getRestrictedLineExpressionProducerForLegIndex(
getNonMaskingRestrictedLineExpressionProducer(
restrictedExpressionData,
0.0,
legIndex,
routeLineOptions,
restrictedExpressionData
)
}?.generateExpression()
}
Expand Down Expand Up @@ -1939,21 +1946,4 @@ class MapboxRouteLineApi(
jobControl.scope
)
}.cacheResult(alternativelyStyleSegmentsNotInLegCache)

private fun getRestrictedLineExpressionProducerForLegIndex(
activeLegIndex: Int,
options: MapboxRouteLineOptions,
restrictedRouteExpressionData: List<ExtractedRouteRestrictionData>
): RouteLineExpressionProvider {
return if (routeLineOptions.displayRestrictedRoadSections) {
MapboxRouteLineUtils.getRestrictedLineExpressionProducer(
restrictedRouteExpressionData,
vanishingPointOffset = 0.0,
activeLegIndex = activeLegIndex,
options,
)
} else {
MapboxRouteLineUtils.getDisabledRestrictedLineExpressionProducer()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,12 @@ internal class VanishingRouteLine {
}
val restrictedRoadExpressionProvider =
ifNonNull(restrictedLineExpressionData) { expressionData ->
{
val (restrictedSectionColor, restrictedSectionInactiveColor) =
options.resourceProvider.routeLineColorResources.run {
if (options.styleInactiveRouteLegsIndependently) {
Pair(
restrictedRoadColor,
inactiveRouteLegRestrictedRoadColor
)
} else {
Pair(restrictedRoadColor, restrictedRoadColor)
}
}
MapboxRouteLineUtils.getRestrictedLineExpression(
offset,
activeLegIndex,
restrictedSectionColor = restrictedSectionColor,
restrictedSectionInactiveColor = restrictedSectionInactiveColor,
expressionData,
)
}
MapboxRouteLineUtils.getNonMaskingRestrictedLineExpressionProducer(
expressionData,
offset,
activeLegIndex,
options
)
}

VanishingRouteLineExpressions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,7 @@ class MapboxRouteLineUtilsTest {
}

@Test
fun getRestrictedLineExpressionProducer() {
val colorResources = RouteLineColorResources.Builder()
.restrictedRoadColor(Color.CYAN)
.inactiveRouteLegRestrictedRoadColor(Color.GRAY)
.build()
val options = MapboxRouteLineOptions.Builder(ctx)
.withRouteLineResources(
RouteLineResources.Builder()
.routeLineColorResources(colorResources)
.build()
)
.build()
fun getRestrictedLineExpressionProducerRestrictionsEnabled() {
val expectedExpressionContents = listOf(
StringChecker("step"),
StringChecker("[line-progress]"),
Expand All @@ -286,18 +275,32 @@ class MapboxRouteLineUtilsTest {
expData,
0.2,
0,
options
activeColor = Color.CYAN,
inactiveColor = Color.GRAY,
displayRestrictedEnabled = true
).generateExpression()

checkExpression(expectedExpressionContents, expression)
}

@Test
fun getDisabledRestrictedLineExpressionProducer() {
fun getRestrictedLineExpressionProducerRestrictionsDisabled() {
val expectedExpression = "[rgba, 0.0, 0.0, 0.0, 0.0]"

val expression = MapboxRouteLineUtils.getDisabledRestrictedLineExpressionProducer()
.generateExpression()
val route = loadNavigationRoute("route-with-restrictions.json")
val expData = MapboxRouteLineUtils.extractRouteRestrictionData(
route,
MapboxRouteLineUtils.granularDistancesProvider
)

val expression = MapboxRouteLineUtils.getRestrictedLineExpressionProducer(
expData,
0.2,
0,
activeColor = Color.CYAN,
inactiveColor = Color.GRAY,
displayRestrictedEnabled = false
).generateExpression()

assertEquals(expectedExpression, expression.toString())
}
Expand Down Expand Up @@ -2313,6 +2316,123 @@ class MapboxRouteLineUtilsTest {
assertEquals(expectedExpression, result.toString())
}

@Test
fun `getRestrictedLineExpression with restriction across two legs, active index = 0`() {
val expectedExpression = "[step, [line-progress], [rgba, 0.0, 0.0, 0.0, 0.0], 0.0, " +
"[rgba, 0.0, 0.0, 0.0, 0.0], 0.3956457979751531, " +
"[rgba, 0.0, 255.0, 255.0, 1.0], 0.4897719974699625, " +
"[rgba, 136.0, 136.0, 136.0, 1.0], 0.5540039481345271, [rgba, 0.0, 0.0, 0.0, 0.0]]"
val route = loadNavigationRoute("multileg_route_two_legs_with_restrictions.json")
val expressionData = MapboxRouteLineUtils.extractRouteRestrictionData(
route,
MapboxRouteLineUtils.granularDistancesProvider
)

val result = MapboxRouteLineUtils.getRestrictedLineExpression(
vanishingPointOffset = 0.0,
activeLegIndex = 0,
Color.CYAN,
Color.GRAY,
expressionData
)

assertEquals(expectedExpression, result.toString())
}

@Test
fun `getRestrictedLineExpression with restriction across two legs, active index = 1`() {
val expectedExpression = "[step, [line-progress], [rgba, 0.0, 0.0, 0.0, 0.0], 0.0, " +
"[rgba, 0.0, 0.0, 0.0, 0.0], 0.3956457979751531, " +
"[rgba, 136.0, 136.0, 136.0, 1.0], 0.4897719974699625, " +
"[rgba, 0.0, 255.0, 255.0, 1.0], 0.5540039481345271, [rgba, 0.0, 0.0, 0.0, 0.0]]"
val route = loadNavigationRoute("multileg_route_two_legs_with_restrictions.json")
val expressionData = MapboxRouteLineUtils.extractRouteRestrictionData(
route,
MapboxRouteLineUtils.granularDistancesProvider
)

val result = MapboxRouteLineUtils.getRestrictedLineExpression(
vanishingPointOffset = 0.0,
activeLegIndex = 1,
Color.CYAN,
Color.GRAY,
expressionData
)

assertEquals(expectedExpression, result.toString())
}

@Test
fun `getNonMaskingRestrictedLineExpression with restriction across two legs, enabled`() {
val options = MapboxRouteLineOptions.Builder(ctx)
.withRouteLineResources(
RouteLineResources.Builder()
.routeLineColorResources(
RouteLineColorResources.Builder()
.restrictedRoadColor(Color.CYAN)
.inactiveRouteLegRestrictedRoadColor(Color.GRAY)
.build()
)
.build()
)
.styleInactiveRouteLegsIndependently(true)
.displayRestrictedRoadSections(true)
.build()
val expectedExpression = "[step, [line-progress], [rgba, 0.0, 0.0, 0.0, 0.0], 0.0, " +
"[rgba, 0.0, 0.0, 0.0, 0.0], 0.3956457979751531, " +
"[rgba, 136.0, 136.0, 136.0, 1.0], 0.4897719974699625, " +
"[rgba, 0.0, 255.0, 255.0, 1.0], 0.5540039481345271, [rgba, 0.0, 0.0, 0.0, 0.0]]"
val route = loadNavigationRoute("multileg_route_two_legs_with_restrictions.json")
val expressionData = MapboxRouteLineUtils.extractRouteRestrictionData(
route,
MapboxRouteLineUtils.granularDistancesProvider
)

val result = MapboxRouteLineUtils.getNonMaskingRestrictedLineExpressionProducer(
expressionData,
vanishingPointOffset = 0.0,
activeLegIndex = 1,
options
)

assertEquals(expectedExpression, result.generateExpression().toString())
}

@Test
fun `getNonMaskingRestrictedLineExpression with restriction across two legs, disabled`() {
val options = MapboxRouteLineOptions.Builder(ctx)
.withRouteLineResources(
RouteLineResources.Builder()
.routeLineColorResources(
RouteLineColorResources.Builder()
.restrictedRoadColor(Color.CYAN)
.inactiveRouteLegRestrictedRoadColor(Color.GRAY)
.build()
)
.build()
)
.styleInactiveRouteLegsIndependently(false)
.displayRestrictedRoadSections(true)
.build()
val expectedExpression = "[step, [line-progress], [rgba, 0.0, 0.0, 0.0, 0.0], 0.0, " +
"[rgba, 0.0, 0.0, 0.0, 0.0], 0.3956457979751531, " +
"[rgba, 0.0, 255.0, 255.0, 1.0], 0.5540039481345271, [rgba, 0.0, 0.0, 0.0, 0.0]]"
val route = loadNavigationRoute("multileg_route_two_legs_with_restrictions.json")
val expressionData = MapboxRouteLineUtils.extractRouteRestrictionData(
route,
MapboxRouteLineUtils.granularDistancesProvider
)

val result = MapboxRouteLineUtils.getNonMaskingRestrictedLineExpressionProducer(
expressionData,
vanishingPointOffset = 0.0,
activeLegIndex = 1,
options
)

assertEquals(expectedExpression, result.generateExpression().toString())
}

@Test
fun getMaskingLayerDynamicData() {
val expectedExpression = "[literal, [0.0, 0.1]]"
Expand Down
Loading

0 comments on commit 2d7be29

Please sign in to comment.