-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add layer click handlers and coordinates convertion logic
- Loading branch information
Showing
10 changed files
with
161 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/solve/rendering/engine/core/input/LayerClickHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package solve.rendering.engine.core.input | ||
|
||
import org.joml.Vector2i | ||
import solve.scene.model.Landmark | ||
|
||
abstract class LayerClickHandler<T: Landmark> { | ||
// Returns the index of the clicked landmark, if there is one. | ||
// Otherwise returns -1. | ||
abstract fun indexOfClickedLandmark(landmarks: List<T>, clickedPixelCoordinate: Vector2i): Int | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/solve/rendering/engine/core/input/LineLayerClickHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package solve.rendering.engine.core.input | ||
|
||
import org.joml.Vector2i | ||
import solve.rendering.engine.utils.pointToSegmentDistance | ||
import solve.rendering.engine.utils.toFloatVector | ||
import solve.rendering.engine.utils.toVector2i | ||
import solve.scene.model.Landmark.Line | ||
|
||
class LineLayerClickHandler : LayerClickHandler<Line>() { | ||
override fun indexOfClickedLandmark(landmarks: List<Line>, clickedPixelCoordinate: Vector2i): Int { | ||
var minLineDistanceIndex = -1 | ||
var minLineDistance = Float.MAX_VALUE | ||
|
||
landmarks.forEachIndexed { index, line -> | ||
val lineStartCoordinate = line.startCoordinate.toVector2i() | ||
val lineFinishCoordinate = line.finishCoordinate.toVector2i() | ||
val lineDistance = pointToSegmentDistance( | ||
clickedPixelCoordinate.toFloatVector(), | ||
lineStartCoordinate.toFloatVector(), | ||
lineFinishCoordinate.toFloatVector() | ||
) | ||
if (lineDistance < ClickedLandmarkMaxAllowedDistance && lineDistance < minLineDistance) { | ||
minLineDistanceIndex = index | ||
minLineDistance = lineDistance | ||
} | ||
} | ||
|
||
return minLineDistanceIndex | ||
} | ||
|
||
companion object { | ||
private const val ClickedLandmarkMaxAllowedDistance = 3 | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/solve/rendering/engine/core/input/MouseInputHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package solve.rendering.engine.core.input | ||
|
||
object MouseInputHandler { | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/solve/rendering/engine/core/input/PointLayerClickHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package solve.rendering.engine.core.input | ||
|
||
import org.joml.Vector2i | ||
import solve.rendering.engine.utils.minus | ||
import solve.rendering.engine.utils.toVector2i | ||
import solve.scene.model.Landmark.Keypoint | ||
|
||
class PointLayerClickHandler : LayerClickHandler<Keypoint>() { | ||
override fun indexOfClickedLandmark(landmarks: List<Keypoint>, clickedPixelCoordinate: Vector2i): Int { | ||
var minKeypointDistanceIndex = -1 | ||
var minKeypointDistance = Double.MAX_VALUE | ||
|
||
landmarks.forEachIndexed { index, keypoint -> | ||
val keypointCoordinate = keypoint.coordinate.toVector2i() | ||
val keypointDistance = (keypointCoordinate - clickedPixelCoordinate).length() | ||
if (keypointDistance < ClickedLandmarkMaxAllowedDistance && keypointDistance < minKeypointDistance) { | ||
minKeypointDistanceIndex = index | ||
minKeypointDistance = keypointDistance | ||
} | ||
} | ||
|
||
return minKeypointDistanceIndex | ||
} | ||
|
||
companion object { | ||
private const val ClickedLandmarkMaxAllowedDistance = 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/solve/rendering/engine/utils/CalculationUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package solve.rendering.engine.utils | ||
|
||
import org.joml.Vector2f | ||
import kotlin.math.abs | ||
import kotlin.math.pow | ||
import kotlin.math.sqrt | ||
|
||
fun pointToSegmentDistance( | ||
pointPosition: Vector2f, | ||
segmentStartPosition: Vector2f, | ||
segmentEndPosition: Vector2f | ||
) : Float { | ||
return abs((segmentEndPosition.x - segmentStartPosition.x) * (pointPosition.y - segmentStartPosition.y) - | ||
(pointPosition.x - segmentStartPosition.x) * (segmentEndPosition.y - segmentStartPosition.y)) / | ||
sqrt((segmentEndPosition.x - segmentStartPosition.x).pow(2) + | ||
(segmentEndPosition.y - segmentStartPosition.y).pow(2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters