Skip to content

Commit e211578

Browse files
committed
implemented stage resizing
1 parent f174d06 commit e211578

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

src/main/kotlin/ch/bildspur/annotator/geometry/Polygon2.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class Polygon2() : Serializable {
1313
this.points.addAll(points)
1414
}
1515

16+
operator fun get(i: Int): Vector2 {
17+
return points[i]
18+
}
19+
20+
operator fun set(i: Int, v: Vector2) {
21+
points[i] = v
22+
}
23+
1624
/**
1725
* PNPoly Method:
1826
* http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html#The%20C%20Code

src/main/kotlin/ch/bildspur/annotator/ui/MainViewController.kt

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ch.bildspur.annotator.ui
22

3+
import ch.bildspur.annotator.geometry.Polygon2
34
import ch.bildspur.annotator.geometry.Vector2
45
import ch.bildspur.annotator.model.AnnotationImage
56
import javafx.event.ActionEvent
@@ -15,10 +16,13 @@ import javafx.scene.input.KeyCode
1516
import javafx.scene.input.KeyEvent
1617
import javafx.scene.input.MouseEvent
1718
import javafx.scene.paint.Color
19+
import javafx.scene.shape.Rectangle
1820
import javafx.stage.Modality
1921
import javafx.stage.Stage
2022
import java.io.File
2123
import kotlin.properties.Delegates
24+
import kotlin.system.exitProcess
25+
2226

2327
/**
2428
* Created by cansik on 29.11.16.
@@ -50,14 +54,20 @@ class MainViewController {
5054
val root = loader.load<Parent>(javaClass.classLoader.getResourceAsStream("view/SettingsView.fxml"))
5155
val controller = loader.getController<SettingsViewController>()
5256

53-
val stage = Stage()
54-
stage.initModality(Modality.WINDOW_MODAL)
55-
stage.title = "OpenCV Sample Annotator Settings"
56-
stage.scene = Scene(root)
57-
stage.showAndWait()
57+
val settingStage = Stage()
58+
settingStage.initModality(Modality.WINDOW_MODAL)
59+
settingStage.title = "OpenCV Sample Annotator Settings"
60+
settingStage.scene = Scene(root)
61+
settingStage.showAndWait()
62+
63+
if (!controller.isStart) {
64+
settingStage.close()
65+
exitProcess(0)
66+
}
5867

59-
if (!controller.isStart)
60-
stage.close()
68+
// listeners for stage resize
69+
stage.widthProperty().addListener({ observableValue, oldSceneWidth, newSceneWidth -> stageResized() })
70+
stage.heightProperty().addListener({ observableValue, oldSceneHeight, newSceneHeight -> stageResized() })
6171

6272
positivesFile = controller.positivesFile!!
6373
annotationImages = controller.datasetFiles!!.map(::AnnotationImage)
@@ -66,7 +76,8 @@ class MainViewController {
6676
// setup canvas
6777
canvas!!.onKeyPressed = EventHandler<KeyEvent> { handleKeyEvent(it) }
6878
canvas!!.onMouseDragged = EventHandler<MouseEvent> { handleMouseDragged(it) }
69-
canvas!!.onMouseClicked = EventHandler<MouseEvent> { handleMouseClicked(it) }
79+
canvas!!.onMousePressed = EventHandler<MouseEvent> { handleMousePressed(it) }
80+
canvas!!.onMouseReleased = EventHandler<MouseEvent> { handleMouseReleased(it) }
7081

7182
canvas!!.isFocusTraversable = true
7283

@@ -76,10 +87,15 @@ class MainViewController {
7687
loadNextImage()
7788
}
7889

90+
fun stageResized() {
91+
resizeCanvas()
92+
initImage()
93+
}
94+
7995
fun loadNextImage() {
8096
if (!imageIterator.hasNext()) {
8197
saveAndClose()
82-
return
98+
exitProcess(0)
8399
}
84100

85101
activeImage = imageIterator.next()
@@ -104,7 +120,7 @@ class MainViewController {
104120

105121
fun initImage() {
106122
calculateScaleFactor()
107-
drawInfo()
123+
redrawCanvas()
108124
}
109125

110126
fun calculateScaleFactor() {
@@ -120,20 +136,18 @@ class MainViewController {
120136
scaleFactor = screenSize.y / imageSize.y
121137
}
122138

123-
fun drawInfo() {
139+
fun redrawCanvas() {
124140
gc.clearRect(0.0, 0.0, canvas!!.width, canvas!!.height)
125141
gc.drawImage(activeImage.image, 0.0, 0.0,
126142
activeImage.image.width * scaleFactor,
127143
activeImage.image.height * scaleFactor)
128144

129-
gc.stroke = Color.GREEN
145+
gc.stroke = Color.LIGHTGREEN
130146

131147
// draw polygons
132148
for (polygon in activeImage.polygons) {
133-
gc.strokePolygon(
134-
polygon.points.map { vectorToScreen(it).x }.toDoubleArray(),
135-
polygon.points.map { vectorToScreen(it).y }.toDoubleArray(),
136-
polygon.points.size)
149+
val rect = getRectData(polygon, scaleFactor)
150+
gc.strokeRect(rect.x, rect.y, rect.width, rect.height)
137151
}
138152
}
139153

@@ -144,16 +158,43 @@ class MainViewController {
144158
}
145159
}
146160

161+
var dragged = false
162+
var dragStartPoint = Vector2.NULL
163+
var activePolygon = Polygon2()
164+
147165
fun handleMouseDragged(e: MouseEvent) {
148-
println("Dragged: ${e.x} | ${e.y}")
166+
val m = Vector2(e.x, e.y)
167+
168+
activePolygon.points[1] = vectorToImage(m)
169+
dragged = true
170+
171+
redrawCanvas()
149172
}
150173

151-
fun handleMouseClicked(e: MouseEvent) {
152-
println("Clicked: ${e.x} | ${e.y}")
174+
fun handleMousePressed(e: MouseEvent) {
175+
val m = Vector2(e.x, e.y)
176+
177+
activePolygon = Polygon2(vectorToImage(m), vectorToImage(m))
178+
activeImage.polygons.add(activePolygon)
179+
180+
dragged = false
181+
dragStartPoint = m
182+
}
183+
184+
fun handleMouseReleased(e: MouseEvent) {
185+
val m = Vector2(e.x, e.y)
186+
187+
if (dragged) {
188+
println("dragged!")
189+
} else {
190+
activeImage.polygons.remove(activePolygon)
191+
println("clicked!")
192+
}
153193
}
154194

155195
fun clearClicked(e: ActionEvent) {
156196
activeImage.polygons.clear()
197+
redrawCanvas()
157198
}
158199

159200
fun nextClicked(e: ActionEvent) {
@@ -168,4 +209,16 @@ class MainViewController {
168209
return v.scale(1.0 / scaleFactor)
169210
}
170211

212+
fun getRectData(poly: Polygon2, scale: Double = 1.0): Rectangle {
213+
val x1 = if (poly[0].x < poly[1].x) poly[0].x else poly[1].x
214+
val y1 = if (poly[0].y < poly[1].y) poly[0].y else poly[1].y
215+
216+
val x2 = if (poly[0].x > poly[1].x) poly[0].x else poly[1].x
217+
val y2 = if (poly[0].y > poly[1].y) poly[0].y else poly[1].y
218+
219+
val width = x2 - x1
220+
val height = y2 - y1
221+
222+
return Rectangle(x1 * scale, y1 * scale, width * scale, height * scale)
223+
}
171224
}

0 commit comments

Comments
 (0)