Skip to content

Commit

Permalink
chore: Rename polygon items to vertices. #1597
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Nov 3, 2022
1 parent 040f324 commit 6c07a49
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
18 changes: 9 additions & 9 deletions py/h2o_wave/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6485,27 +6485,27 @@ class ImageAnnotatorPolygon:
"""
def __init__(
self,
items: List[ImageAnnotatorPoint],
vertices: List[ImageAnnotatorPoint],
):
_guard_vector('ImageAnnotatorPolygon.items', items, (ImageAnnotatorPoint,), False, False, False)
self.items = items
_guard_vector('ImageAnnotatorPolygon.vertices', vertices, (ImageAnnotatorPoint,), False, False, False)
self.vertices = vertices
"""List of points of the polygon."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
_guard_vector('ImageAnnotatorPolygon.items', self.items, (ImageAnnotatorPoint,), False, False, False)
_guard_vector('ImageAnnotatorPolygon.vertices', self.vertices, (ImageAnnotatorPoint,), False, False, False)
return _dump(
items=[__e.dump() for __e in self.items],
vertices=[__e.dump() for __e in self.vertices],
)

@staticmethod
def load(__d: Dict) -> 'ImageAnnotatorPolygon':
"""Creates an instance of this class using the contents of a dict."""
__d_items: Any = __d.get('items')
_guard_vector('ImageAnnotatorPolygon.items', __d_items, (dict,), False, False, False)
items: List[ImageAnnotatorPoint] = [ImageAnnotatorPoint.load(__e) for __e in __d_items]
__d_vertices: Any = __d.get('vertices')
_guard_vector('ImageAnnotatorPolygon.vertices', __d_vertices, (dict,), False, False, False)
vertices: List[ImageAnnotatorPoint] = [ImageAnnotatorPoint.load(__e) for __e in __d_vertices]
return ImageAnnotatorPolygon(
items,
vertices,
)


Expand Down
6 changes: 3 additions & 3 deletions py/h2o_wave/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,17 +2417,17 @@ def image_annotator_point(


def image_annotator_polygon(
items: List[ImageAnnotatorPoint],
vertices: List[ImageAnnotatorPoint],
) -> ImageAnnotatorShape:
"""Create a polygon annotation shape.
Args:
items: List of points of the polygon.
vertices: List of points of the polygon.
Returns:
A `h2o_wave.types.ImageAnnotatorPolygon` instance.
"""
return ImageAnnotatorShape(polygon=ImageAnnotatorPolygon(
items,
vertices,
))


Expand Down
8 changes: 4 additions & 4 deletions r/R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -2817,14 +2817,14 @@ ui_image_annotator_point <- function(

#' Create a polygon annotation shape.
#'
#' @param items List of points of the polygon.
#' @param vertices List of points of the polygon.
#' @return A ImageAnnotatorPolygon instance.
#' @export
ui_image_annotator_polygon <- function(
items) {
.guard_vector("items", "WaveImageAnnotatorPoint", items)
vertices) {
.guard_vector("vertices", "WaveImageAnnotatorPoint", vertices)
.o <- list(polygon=list(
items=items))
vertices=vertices))
class(.o) <- append(class(.o), c(.wave_obj, "WaveImageAnnotatorShape"))
return(.o)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_image_annotator_polygon" value="ui.image_annotator_polygon(items=[&#10; $items$ &#10;]),$END$" description="Create a minimal Wave ImageAnnotatorPolygon." toReformat="true" toShortenFQNames="true">
<variable name="items" expression="" defaultValue="" alwaysStopAt="true"/>
<template name="w_image_annotator_polygon" value="ui.image_annotator_polygon(vertices=[&#10; $vertices$ &#10;]),$END$" description="Create a minimal Wave ImageAnnotatorPolygon." toReformat="true" toShortenFQNames="true">
<variable name="vertices" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
Expand Down Expand Up @@ -1416,8 +1416,8 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_image_annotator_polygon" value="ui.image_annotator_polygon(items=[&#10; $items$ &#10;]),$END$" description="Create Wave ImageAnnotatorPolygon with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="items" expression="" defaultValue="" alwaysStopAt="true"/>
<template name="w_full_image_annotator_polygon" value="ui.image_annotator_polygon(vertices=[&#10; $vertices$ &#10;]),$END$" description="Create Wave ImageAnnotatorPolygon with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="vertices" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
Expand Down
4 changes: 2 additions & 2 deletions tools/vscode-extension/component-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
"Wave ImageAnnotatorPolygon": {
"prefix": "w_image_annotator_polygon",
"body": [
"ui.image_annotator_polygon(items=[\n\t\t$1\t\t\n]),$0"
"ui.image_annotator_polygon(vertices=[\n\t\t$1\t\t\n]),$0"
],
"description": "Create a minimal Wave ImageAnnotatorPolygon."
},
Expand Down Expand Up @@ -1185,7 +1185,7 @@
"Wave Full ImageAnnotatorPolygon": {
"prefix": "w_full_image_annotator_polygon",
"body": [
"ui.image_annotator_polygon(items=[\n\t\t$1\t\t\n]),$0"
"ui.image_annotator_polygon(vertices=[\n\t\t$1\t\t\n]),$0"
],
"description": "Create a full Wave ImageAnnotatorPolygon."
},
Expand Down
18 changes: 9 additions & 9 deletions ui/src/image_annotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ImageAnnotatorPoint {
/** Create a polygon annotation shape. */
export interface ImageAnnotatorPolygon {
/** List of points of the polygon. */
items: ImageAnnotatorPoint[]
vertices: ImageAnnotatorPoint[]
}

/** Create a rectangular annotation shape. */
Expand Down Expand Up @@ -113,7 +113,7 @@ const
eventToCursor = (event: React.MouseEvent, rect: DOMRect) => ({ cursor_x: event.clientX - rect.left, cursor_y: event.clientY - rect.top }),
getIntersectedShape = (shapes: DrawnShape[], cursor_x: F, cursor_y: F) => shapes.find(({ shape, isFocused }) => {
if (shape.rect) return isIntersectingRect(cursor_x, cursor_y, shape.rect, isFocused)
if (shape.polygon) return isIntersectingPolygon({ x: cursor_x, y: cursor_y }, shape.polygon.items, isFocused)
if (shape.polygon) return isIntersectingPolygon({ x: cursor_x, y: cursor_y }, shape.polygon.vertices, isFocused)
}),
getCorrectCursor = (cursor_x: U, cursor_y: U, focused?: DrawnShape, intersected?: DrawnShape, isSelect = false) => {
let cursor = intersected
Expand All @@ -123,8 +123,8 @@ const
: 'crosshair'
if (intersected?.isFocused && intersected.shape.rect) cursor = getRectCornerCursor(intersected.shape.rect, cursor_x, cursor_y) || 'move'
else if (focused?.shape.rect) cursor = getRectCornerCursor(focused.shape.rect, cursor_x, cursor_y) || cursor
else if (intersected?.isFocused && intersected.shape.polygon) cursor = getPolygonPointCursor(intersected.shape.polygon.items, cursor_x, cursor_y) || 'move'
else if (focused?.shape.polygon) cursor = getPolygonPointCursor(focused.shape.polygon.items, cursor_x, cursor_y) || cursor
else if (intersected?.isFocused && intersected.shape.polygon) cursor = getPolygonPointCursor(intersected.shape.polygon.vertices, cursor_x, cursor_y) || 'move'
else if (focused?.shape.polygon) cursor = getPolygonPointCursor(focused.shape.polygon.vertices, cursor_x, cursor_y) || cursor

return cursor
},
Expand All @@ -140,7 +140,7 @@ const
}
}
}
else if (shape.polygon) return { tag, shape: { polygon: { items: shape.polygon.items.map(i => ({ x: i.x * aspectRatio, y: i.y * aspectRatio })) } } }
else if (shape.polygon) return { tag, shape: { polygon: { vertices: shape.polygon.vertices.map(i => ({ x: i.x * aspectRatio, y: i.y * aspectRatio })) } } }
return { tag, shape }
})

Expand Down Expand Up @@ -172,7 +172,7 @@ export const XImageAnnotator = ({ model }: { model: ImageAnnotator }) => {
setDrawnShapes(shapes => {
shapes.forEach(({ shape, isFocused, tag }) => {
if (shape.rect) rectRef.current?.drawRect(shape.rect, getCurrentTagColor(tag), isFocused)
else if (shape.polygon) polygonRef.current?.drawPolygon(shape.polygon.items, getCurrentTagColor(tag), true, isFocused)
else if (shape.polygon) polygonRef.current?.drawPolygon(shape.polygon.vertices, getCurrentTagColor(tag), true, isFocused)
})
return shapes
})
Expand Down Expand Up @@ -260,13 +260,13 @@ export const XImageAnnotator = ({ model }: { model: ImageAnnotator }) => {
}
case 'select': {
if (intersected) setActiveTag(intersected.tag)
if (intersected?.shape.polygon) polygonRef.current?.addAuxPoint(cursor_x, cursor_y, intersected.shape.polygon.items)
if (intersected?.shape.polygon) polygonRef.current?.addAuxPoint(cursor_x, cursor_y, intersected.shape.polygon.vertices)
polygonRef.current?.resetDragging()

setDrawnShapes(drawnShapes => drawnShapes.map(s => {
s.isFocused = s === intersected
if (s.isFocused && s.shape.polygon && polygonRef.current) {
s.shape.polygon.items = polygonRef.current.getPolygonPointsWithAux(s.shape.polygon.items)
s.shape.polygon.vertices = polygonRef.current.getPolygonPointsWithAux(s.shape.polygon.vertices)
}
return s
}))
Expand Down Expand Up @@ -342,7 +342,7 @@ export const XImageAnnotator = ({ model }: { model: ImageAnnotator }) => {
tag,
shape: {
polygon: {
items: shape.polygon.items
vertices: shape.polygon.vertices
.filter((i: DrawnPoint) => !i.isAux)
.map(i => ({ x: i.x / aspectRatio, y: i.y / aspectRatio }))
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/image_annotator_polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ export class PolygonAnnotator {
return
}

const clickedPolygonPoint = focused.shape.polygon.items.find(p => isIntersectingPoint(p, cursor_x, cursor_y))
const clickedPolygonPoint = focused.shape.polygon.vertices.find(p => isIntersectingPoint(p, cursor_x, cursor_y))
this.draggedPoint = clickedPolygonPoint || this.draggedPoint
if (this.draggedPoint) {
this.draggedPoint.x += cursor_x - this.draggedPoint.x
this.draggedPoint.y += cursor_y - this.draggedPoint.y
}
else if (intersected == focused || this.draggedShape) {
this.draggedShape = intersected?.shape.polygon && intersected.isFocused ? intersected : this.draggedShape
this.draggedShape?.shape.polygon?.items.forEach(p => {
this.draggedShape?.shape.polygon?.vertices.forEach(p => {
p.x += cursor_x - clickStartPosition!.x
p.y += cursor_y - clickStartPosition!.y
})
Expand Down

0 comments on commit 6c07a49

Please sign in to comment.