Skip to content

Commit

Permalink
Merge pull request 'immutable_features' (!24) from immutable_features…
Browse files Browse the repository at this point in the history
… into dev
  • Loading branch information
altavir committed Jul 8, 2024
2 parents 0f5dcf9 + 4e76a25 commit e913874
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 467 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ val kmathVersion: String by extra("0.4.0")

allprojects {
group = "space.kscience"
version = "0.3.1-dev"
version = "0.4.0-dev"

repositories {
mavenLocal()
Expand Down
6 changes: 3 additions & 3 deletions demo/maps-wasm/src/wasmJsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:OptIn(ExperimentalComposeUiApi::class, ExperimentalResourceApi::class)
@file:OptIn(ExperimentalResourceApi::class, ExperimentalComposeUiApi::class)

import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
Expand All @@ -10,7 +10,7 @@ import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import space.kscience.kmath.geometry.Angle
import space.kscience.maps.features.FeatureGroup
import space.kscience.maps.features.FeatureStore
import space.kscience.maps.features.ViewConfig
import space.kscience.maps.features.ViewPoint
import space.kscience.maps.features.color
Expand All @@ -25,7 +25,7 @@ fun App() {
val scope = rememberCoroutineScope()


val features: FeatureGroup<XY> = FeatureGroup.remember(XYCoordinateSpace) {
val features = FeatureStore.remember(XYCoordinateSpace) {
background(1600f, 1200f) {
painterResource(Res.drawable.middle_earth)
}
Expand Down
8 changes: 4 additions & 4 deletions demo/maps/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun App() {

geoJson(javaClass.getResource("/moscow.geo.json")!!)
.color(Color.Blue)
.modifyAttribute(AlphaAttribute, 0.4f)
.alpha(0.4f)

icon(pointOne, Icons.Filled.Home)

Expand Down Expand Up @@ -166,13 +166,13 @@ fun App() {
}.launchIn(scope)

//Add click listeners for all polygons
forEachWithType<Gmc, PolygonFeature<Gmc>> { ref ->
forEachWithType<Gmc, PolygonFeature<Gmc>> { ref, polygon: PolygonFeature<Gmc> ->
ref.onClick(PointerMatcher.Primary) {
println("Click on ${ref.id}")
println("Click on $ref")
//draw in top-level scope
with(this@MapView) {
multiLine(
ref.resolve().points,
polygon.points,
attributes = Attributes(ZAttribute, 10f),
id = "selected",
).modifyAttribute(StrokeAttribute, 4f).color(Color.Magenta)
Expand Down
3 changes: 2 additions & 1 deletion demo/polygon-editor/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun App() {

val myPolygon: SnapshotStateList<XY> = remember { mutableStateListOf<XY>() }

val featureState: FeatureGroup<XY> = FeatureGroup.remember(XYCoordinateSpace) {
val featureState = FeatureStore.remember(XYCoordinateSpace) {
multiLine(
listOf(XY(0f, 0f), XY(0f, 1f), XY(1f, 1f), XY(1f, 0f), XY(0f, 0f)),
id = "frame"
Expand Down Expand Up @@ -55,6 +55,7 @@ fun App() {
}
draggableMultiLine(
pointRefs + pointRefs.first(),
"line"
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions demo/scheme/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import space.kscience.kmath.geometry.Angle
import space.kscience.maps.features.FeatureGroup
import space.kscience.maps.features.FeatureStore
import space.kscience.maps.features.ViewConfig
import space.kscience.maps.features.ViewPoint
import space.kscience.maps.features.color
Expand All @@ -29,7 +29,7 @@ fun App() {
MaterialTheme {
val scope = rememberCoroutineScope()

val features: FeatureGroup<XY> = FeatureGroup.remember(XYCoordinateSpace) {
val features = FeatureStore.remember(XYCoordinateSpace) {
background(1600f, 1200f) { painterResource("middle-earth.jpg") }
circle(410.52737 to 868.7676).color(Color.Blue)
text(410.52737 to 868.7676, "Shire").color(Color.Blue)
Expand Down
6 changes: 3 additions & 3 deletions demo/trajectory-playground/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private fun Vector2D<out Number>.toXY() = XY(x.toFloat(), y.toFloat())

private val random = Random(123)

fun FeatureGroup<XY>.trajectory(
fun FeatureBuilder<XY>.trajectory(
trajectory: Trajectory2D,
colorPicker: (Trajectory2D) -> Color = { Color.Blue },
): FeatureRef<XY, FeatureGroup<XY>> = group {
Expand Down Expand Up @@ -54,12 +54,12 @@ fun FeatureGroup<XY>.trajectory(
}
}

fun FeatureGroup<XY>.obstacle(obstacle: Obstacle, colorPicker: (Trajectory2D) -> Color = { Color.Red }) {
fun FeatureBuilder<XY>.obstacle(obstacle: Obstacle, colorPicker: (Trajectory2D) -> Color = { Color.Red }) {
trajectory(obstacle.circumvention, colorPicker)
polygon(obstacle.arcs.map { it.center.toXY() }).color(Color.Gray)
}

fun FeatureGroup<XY>.pose(pose2D: Pose2D) = with(Float64Space2D) {
fun FeatureBuilder<XY>.pose(pose2D: Pose2D) = with(Float64Space2D) {
line(pose2D.toXY(), (pose2D + Pose2D.bearingToVector(pose2D.bearing)).toXY())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private val logger = KotlinLogging.logger("MapView")
public fun MapView(
mapState: MapCanvasState,
mapTileProvider: MapTileProvider,
features: FeatureGroup<Gmc>,
featureStore: FeatureStore<Gmc>,
modifier: Modifier,
) {
val mapTiles = remember(mapTileProvider) {
Expand Down Expand Up @@ -87,7 +87,7 @@ public fun MapView(
}


FeatureCanvas(mapState, features, modifier = modifier.canvasControls(mapState, features)) {
FeatureCanvas(mapState, featureStore.featureFlow, modifier = modifier.canvasControls(mapState, featureStore)) {
val tileScale = mapState.tileScale

clipRect {
Expand All @@ -112,19 +112,19 @@ public fun MapView(
}

/**
* Create a [MapView] with given [features] group.
* Create a [MapView] with given [featureStore] group.
*/
@Composable
public fun MapView(
mapTileProvider: MapTileProvider,
config: ViewConfig<Gmc>,
features: FeatureGroup<Gmc>,
featureStore: FeatureStore<Gmc>,
initialViewPoint: ViewPoint<Gmc>? = null,
initialRectangle: Rectangle<Gmc>? = null,
modifier: Modifier,
) {
val mapState = MapCanvasState.remember(mapTileProvider, config, initialViewPoint, initialRectangle)
MapView(mapState, mapTileProvider, features, modifier)
MapView(mapState, mapTileProvider, featureStore, modifier)
}

/**
Expand All @@ -141,9 +141,9 @@ public fun MapView(
initialViewPoint: ViewPoint<Gmc>? = null,
initialRectangle: Rectangle<Gmc>? = null,
modifier: Modifier = Modifier.fillMaxSize(),
buildFeatures: FeatureGroup<Gmc>.() -> Unit = {},
buildFeatures: FeatureStore<Gmc>.() -> Unit = {},
) {
val featureState = FeatureGroup.remember(WebMercatorSpace, buildFeatures)
val featureState = FeatureStore.remember(WebMercatorSpace, buildFeatures)
val computedRectangle = initialRectangle ?: featureState.getBoundingBox()
MapView(mapTileProvider, config, featureState, initialViewPoint, computedRectangle, modifier)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import space.kscience.maps.features.*
import kotlin.math.ceil


internal fun FeatureGroup<Gmc>.coordinatesOf(pair: Pair<Number, Number>) =
internal fun FeatureBuilder<Gmc>.coordinatesOf(pair: Pair<Number, Number>) =
GeodeticMapCoordinates.ofDegrees(pair.first.toDouble(), pair.second.toDouble())

public typealias MapFeature = Feature<Gmc>

public fun FeatureGroup<Gmc>.circle(
public fun FeatureBuilder<Gmc>.circle(
centerCoordinates: Pair<Number, Number>,
size: Dp = 5.dp,
id: String? = null,
): FeatureRef<Gmc, CircleFeature<Gmc>> = feature(
id, CircleFeature(space, coordinatesOf(centerCoordinates), size)
)

public fun FeatureGroup<Gmc>.rectangle(
public fun FeatureBuilder<Gmc>.rectangle(
centerCoordinates: Pair<Number, Number>,
size: DpSize = DpSize(5.dp, 5.dp),
id: String? = null,
Expand All @@ -35,7 +35,7 @@ public fun FeatureGroup<Gmc>.rectangle(
)


public fun FeatureGroup<Gmc>.draw(
public fun FeatureBuilder<Gmc>.draw(
position: Pair<Number, Number>,
id: String? = null,
draw: DrawScope.() -> Unit,
Expand All @@ -45,7 +45,7 @@ public fun FeatureGroup<Gmc>.draw(
)


public fun FeatureGroup<Gmc>.line(
public fun FeatureBuilder<Gmc>.line(
curve: GmcCurve,
id: String? = null,
): FeatureRef<Gmc, LineFeature<Gmc>> = feature(
Expand All @@ -56,7 +56,7 @@ public fun FeatureGroup<Gmc>.line(
/**
* A segmented geodetic curve
*/
public fun FeatureGroup<Gmc>.geodeticLine(
public fun FeatureBuilder<Gmc>.geodeticLine(
curve: GmcCurve,
ellipsoid: GeoEllipsoid = GeoEllipsoid.WGS84,
maxLineDistance: Distance = 100.kilometers,
Expand All @@ -79,15 +79,15 @@ public fun FeatureGroup<Gmc>.geodeticLine(
multiLine(points.map { it.coordinates }, id = id)
}

public fun FeatureGroup<Gmc>.geodeticLine(
public fun FeatureBuilder<Gmc>.geodeticLine(
from: Gmc,
to: Gmc,
ellipsoid: GeoEllipsoid = GeoEllipsoid.WGS84,
maxLineDistance: Distance = 100.kilometers,
id: String? = null,
): FeatureRef<Gmc, Feature<Gmc>> = geodeticLine(ellipsoid.curveBetween(from, to), ellipsoid, maxLineDistance, id)

public fun FeatureGroup<Gmc>.line(
public fun FeatureBuilder<Gmc>.line(
aCoordinates: Pair<Double, Double>,
bCoordinates: Pair<Double, Double>,
id: String? = null,
Expand All @@ -96,7 +96,7 @@ public fun FeatureGroup<Gmc>.line(
LineFeature(space, coordinatesOf(aCoordinates), coordinatesOf(bCoordinates))
)

public fun FeatureGroup<Gmc>.arc(
public fun FeatureBuilder<Gmc>.arc(
center: Pair<Double, Double>,
radius: Distance,
startAngle: Angle,
Expand All @@ -112,17 +112,17 @@ public fun FeatureGroup<Gmc>.arc(
)
)

public fun FeatureGroup<Gmc>.points(
public fun FeatureBuilder<Gmc>.points(
points: List<Pair<Double, Double>>,
id: String? = null,
): FeatureRef<Gmc, PointsFeature<Gmc>> = feature(id, PointsFeature(space, points.map(::coordinatesOf)))

public fun FeatureGroup<Gmc>.multiLine(
public fun FeatureBuilder<Gmc>.multiLine(
points: List<Pair<Double, Double>>,
id: String? = null,
): FeatureRef<Gmc, MultiLineFeature<Gmc>> = feature(id, MultiLineFeature(space, points.map(::coordinatesOf)))

public fun FeatureGroup<Gmc>.icon(
public fun FeatureBuilder<Gmc>.icon(
position: Pair<Double, Double>,
image: ImageVector,
size: DpSize = DpSize(20.dp, 20.dp),
Expand All @@ -137,7 +137,7 @@ public fun FeatureGroup<Gmc>.icon(
)
)

public fun FeatureGroup<Gmc>.text(
public fun FeatureBuilder<Gmc>.text(
position: Pair<Double, Double>,
text: String,
font: Font.() -> Unit = { size = 16f },
Expand All @@ -147,7 +147,7 @@ public fun FeatureGroup<Gmc>.text(
TextFeature(space, coordinatesOf(position), text, fontConfig = font)
)

public fun FeatureGroup<Gmc>.pixelMap(
public fun FeatureBuilder<Gmc>.pixelMap(
rectangle: Rectangle<Gmc>,
latitudeDelta: Angle,
longitudeDelta: Angle,
Expand Down
1 change: 1 addition & 0 deletions maps-kt-features/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ kscience {
api(compose.material)
api(compose.ui)
api("io.github.oshai:kotlin-logging:6.0.3")
api("com.benasher44:uuid:0.8.4")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlin.math.min
*/
public fun <T : Any> Modifier.canvasControls(
state: CanvasState<T>,
features: FeatureGroup<T>,
features: FeatureStore<T>,
): Modifier = with(state) {

// //selecting all tapabales ahead of time
Expand Down Expand Up @@ -67,7 +67,7 @@ public fun <T : Any> Modifier.canvasControls(
point
)

features.forEachWithAttributeUntil(ClickListenerAttribute) { _, feature, listeners ->
features.forEachWithAttributeUntil(ClickListenerAttribute) {_, feature, listeners ->
if (point in (feature as DomainFeature)) {
listeners.forEach { it.handle(event, point) }
false
Expand Down
Loading

0 comments on commit e913874

Please sign in to comment.