-
Notifications
You must be signed in to change notification settings - Fork 146
[Code health] Don't allow saving a duplicate vertex during polygon drawing session #3750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
e0baf2a
8e2ffa7
95bac36
1ef4ddf
8a409b8
7913d08
ffb8a30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import kotlinx.collections.immutable.toImmutableList | |
| import org.groundplatform.android.ui.datacollection.tasks.polygon.PolygonDrawingSession.Companion.DISTANCE_THRESHOLD_DP | ||
| import org.groundplatform.domain.model.geometry.Coordinates | ||
| import org.groundplatform.domain.model.geometry.LineString | ||
| import org.groundplatform.domain.util.isClosed | ||
| import org.groundplatform.domain.util.isSelfIntersecting | ||
|
|
||
| class PolygonDrawingSessionImpl : PolygonDrawingSession { | ||
|
|
@@ -34,56 +35,60 @@ class PolygonDrawingSessionImpl : PolygonDrawingSession { | |
| override val vertices: List<Coordinates> | ||
| get() = _vertices | ||
|
|
||
| @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
| internal val redoVertexStack = mutableListOf<Coordinates>() | ||
| private var _tentativeVertex: Coordinates? = null | ||
|
|
||
| override val hasSelfIntersection: Boolean | ||
| get() = isSelfIntersecting(_vertices) | ||
| override val tentativeVertex: Coordinates? | ||
| get() = _tentativeVertex | ||
|
|
||
| private fun addVertex(vertex: Coordinates, shouldOverwriteLastVertex: Boolean) { | ||
| val updatedVertices = _vertices.toMutableList() | ||
|
|
||
| if (shouldOverwriteLastVertex && updatedVertices.isNotEmpty()) { | ||
| updatedVertices.removeAt(updatedVertices.lastIndex) | ||
| } | ||
| override val displayVertices: List<Coordinates> | ||
| get() = _tentativeVertex?.let { _vertices + it } ?: _vertices | ||
|
|
||
| updatedVertices.add(vertex) | ||
| @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
| internal val redoVertexStack = mutableListOf<Coordinates>() | ||
|
|
||
| _vertices = updatedVertices.toImmutableList() | ||
| } | ||
| override val hasSelfIntersection: Boolean | ||
| get() = isSelfIntersecting(displayVertices) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't seem to close the polygons. When debugging i noticed that Screen_recording_20260521_143226.webm
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unable to reproduce this on my device. Are you able to consistently reproduce it or is it flaky?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's consistently reproducible. But I've found out it's because I was clicking twice to drop the first point because clicking the first time doesn't make the white circle marker to show up until the camera is dragged. So I believe the problem is that dropping two points in the starting vertex is allowed, but then leads to an invalid polygon |
||
|
|
||
| override fun setVertices(newVertices: List<Coordinates>) { | ||
| _vertices = newVertices.toImmutableList() | ||
| _tentativeVertex = null | ||
| redoVertexStack.clear() | ||
| } | ||
|
|
||
| override fun updateTentativeVertex( | ||
| target: Coordinates, | ||
| calculateDistance: (Coordinates, Coordinates) -> Double, | ||
| ) { | ||
| if (isClosed(_vertices)) { | ||
| _tentativeVertex = null | ||
| _isTooClose = true | ||
| return | ||
| } | ||
|
|
||
| val firstVertex = _vertices.firstOrNull() | ||
| var updatedTarget = target | ||
| if (firstVertex != null && _vertices.size > 2) { | ||
| if (firstVertex != null && _vertices.size >= 3) { | ||
| val distance = calculateDistance(firstVertex, target) | ||
|
|
||
| if (distance <= DISTANCE_THRESHOLD_DP) { | ||
| updatedTarget = firstVertex | ||
| } | ||
| } | ||
|
|
||
| val prev = _vertices.dropLast(1).lastOrNull() | ||
| val lastCommitted = _vertices.lastOrNull() | ||
| _isTooClose = | ||
| _vertices.size > 1 && | ||
| prev?.let { calculateDistance(it, target) <= DISTANCE_THRESHOLD_DP } == true | ||
| lastCommitted?.let { calculateDistance(it, target) <= DISTANCE_THRESHOLD_DP } == true | ||
|
|
||
| addVertex(updatedTarget, true) | ||
| _tentativeVertex = updatedTarget | ||
| } | ||
|
|
||
| override fun commitTentativeVertex(currentCameraTarget: Coordinates?): List<Coordinates>? { | ||
| redoVertexStack.clear() | ||
| val vertex = _vertices.lastOrNull() ?: currentCameraTarget | ||
| val vertex = _tentativeVertex ?: currentCameraTarget | ||
| return vertex?.let { | ||
| _vertices = (_vertices + it).toImmutableList() | ||
| _tentativeVertex = null | ||
| _isTooClose = _vertices.size > 1 | ||
| addVertex(it, false) | ||
| _vertices | ||
| } | ||
| } | ||
|
|
@@ -96,21 +101,30 @@ class PolygonDrawingSessionImpl : PolygonDrawingSession { | |
| return intersected | ||
| } | ||
|
|
||
| override fun isValidPolygon(): Boolean = | ||
| LineString(_vertices).isClosed() && !isSelfIntersecting(_vertices) | ||
| override fun isValidPolygon(): Boolean = LineString(displayVertices).isClosed() && | ||
| !isSelfIntersecting(displayVertices) && | ||
| displayVertices.distinct().size >= 3 | ||
|
|
||
| override fun complete() { | ||
| check(isValidPolygon()) { "Polygon is not valid" } | ||
| check(!_isMarkedComplete) { "Already marked complete" } | ||
| if (_tentativeVertex != null) { | ||
| commitTentativeVertex(null) | ||
| } | ||
| _isMarkedComplete = true | ||
| } | ||
|
|
||
| override fun removeLastVertex(): Boolean { | ||
| if (_vertices.isEmpty()) return false | ||
| if (_tentativeVertex == null && _vertices.isEmpty()) return false | ||
|
|
||
| _isMarkedComplete = false | ||
| redoVertexStack.add(_vertices.last()) | ||
| _vertices = _vertices.dropLast(1).toImmutableList() | ||
| _tentativeVertex = null | ||
|
|
||
| if (_vertices.isNotEmpty()) { | ||
| redoVertexStack.add(_vertices.last()) | ||
| _vertices = _vertices.dropLast(1).toImmutableList() | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
|
|
@@ -120,6 +134,7 @@ class PolygonDrawingSessionImpl : PolygonDrawingSession { | |
| _isMarkedComplete = false | ||
| val redoVertex = redoVertexStack.removeAt(redoVertexStack.lastIndex) | ||
| _vertices = (_vertices + redoVertex).toImmutableList() | ||
| _tentativeVertex = null | ||
| return redoVertex | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.