@@ -10,6 +10,7 @@ import colors from "../../colors"
10
10
import fixTwisted from "./fix-twisted"
11
11
import convertExpandingLineToPolygon from "./convert-expanding-line-to-polygon"
12
12
import clamp from "clamp"
13
+ import getLandmarksWithTransform from "../../utils/get-landmarks-with-transform"
13
14
14
15
const getRandomId = ( ) => Math . random ( ) . toString ( ) . split ( "." ) [ 1 ]
15
16
@@ -206,6 +207,16 @@ export default (state: MainLayoutState, action: Action) => {
206
207
pointIndex,
207
208
} )
208
209
}
210
+ case "BEGIN_MOVE_KEYPOINT" : {
211
+ const { region, keypointId } = action
212
+ state = closeEditors ( state )
213
+ state = saveToHistory ( state , "Move Keypoint" )
214
+ return setIn ( state , [ "mode" ] , {
215
+ mode : "MOVE_KEYPOINT" ,
216
+ regionId : region . id ,
217
+ keypointId,
218
+ } )
219
+ }
209
220
case "ADD_POLYGON_POINT" : {
210
221
const { polygon, point, pointIndex } = action
211
222
const regionIndex = getRegionIndex ( polygon )
@@ -240,6 +251,22 @@ export default (state: MainLayoutState, action: Action) => {
240
251
[ x , y ]
241
252
)
242
253
}
254
+ case "MOVE_KEYPOINT" : {
255
+ const { keypointId, regionId } = state . mode
256
+ const [ region , regionIndex ] = getRegion ( regionId )
257
+ if ( regionIndex === null ) return state
258
+ return setIn (
259
+ state ,
260
+ [
261
+ ...pathToActiveImage ,
262
+ "regions" ,
263
+ regionIndex ,
264
+ "points" ,
265
+ keypointId ,
266
+ ] ,
267
+ { ...( region : any ) . points [ keypointId ] , x, y }
268
+ )
269
+ }
243
270
case "MOVE_REGION" : {
244
271
const { regionId } = state . mode
245
272
if ( regionId === "$$allowed_area" ) {
@@ -312,6 +339,20 @@ export default (state: MainLayoutState, action: Action) => {
312
339
h : dh ,
313
340
} )
314
341
}
342
+ case "RESIZE_KEYPOINTS" : {
343
+ const { regionId, landmarks, centerX, centerY } = state . mode
344
+ const distFromCenter = Math . sqrt (
345
+ ( centerX - x ) ** 2 + ( centerY - y ) ** 2
346
+ )
347
+ const scale = distFromCenter / 0.15
348
+ return modifyRegion ( regionId , {
349
+ points : getLandmarksWithTransform ( {
350
+ landmarks,
351
+ center : { x : centerX , y : centerY } ,
352
+ scale,
353
+ } ) ,
354
+ } )
355
+ }
315
356
case "DRAW_POLYGON" : {
316
357
const { regionId } = state . mode
317
358
const [ region , regionIndex ] = getRegion ( regionId )
@@ -541,6 +582,34 @@ export default (state: MainLayoutState, action: Action) => {
541
582
} )
542
583
break
543
584
}
585
+ case "create-keypoints" : {
586
+ state = saveToHistory ( state , "Create Keypoints" )
587
+ const [
588
+ [ keypointsDefinitionId , { landmarks, connections } ] ,
589
+ ] = ( Object . entries ( state . keypointDefinitions ) : any )
590
+
591
+ newRegion = {
592
+ type : "keypoints" ,
593
+ keypointsDefinitionId,
594
+ points : getLandmarksWithTransform ( {
595
+ landmarks,
596
+ center : { x, y } ,
597
+ scale : 1 ,
598
+ } ) ,
599
+ highlighted : true ,
600
+ editingLabels : false ,
601
+ id : getRandomId ( ) ,
602
+ }
603
+ state = setIn ( state , [ "mode" ] , {
604
+ mode : "RESIZE_KEYPOINTS" ,
605
+ landmarks,
606
+ centerX : x ,
607
+ centerY : y ,
608
+ regionId : newRegion . id ,
609
+ isNew : true ,
610
+ } )
611
+ break
612
+ }
544
613
default :
545
614
break
546
615
}
@@ -581,9 +650,13 @@ export default (state: MainLayoutState, action: Action) => {
581
650
}
582
651
}
583
652
case "MOVE_REGION" :
653
+ case "RESIZE_KEYPOINTS" :
584
654
case "MOVE_POLYGON_POINT" : {
585
655
return { ...state , mode : null }
586
656
}
657
+ case "MOVE_KEYPOINT" : {
658
+ return { ...state , mode : null }
659
+ }
587
660
case "CREATE_POINT_LINE" : {
588
661
return state
589
662
}
0 commit comments