Skip to content

Commit 0c4ee0f

Browse files
Fix hover bug
1 parent 4d628db commit 0c4ee0f

5 files changed

Lines changed: 157 additions & 15 deletions

File tree

‎.github/tasks.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,40 @@ both loaded together with class identity intact.
9898
classes together in one subtask. Lint clean, 166 jest tests pass.
9999

100100

101+
102+
- [x] 10. Show every diff outcome at once, and keep the hover card off the annotation
103+
- (model-registry) Dropped the TP/FP/FN layer picker: diff mode now calls
104+
`set_active_class_layer(key, null, 1)` so all three outcome groups stay at
105+
full opacity. A null active layer is also what makes them all hover
106+
targets, since `get_edit_candidates` skips groups that aren't active.
107+
Passing the opacity explicitly matters: with no active class every group
108+
takes the `inactive_opacity` branch, so the default would dim all of them.
109+
The sidebar "Diff Colors" rows are now a legend plus recolor.
110+
- Added `annotation_display_name_resolver` to ULabel, alongside the existing
111+
color and canvas-group resolvers, so the hover card can name the diff
112+
outcome instead of the class. Every diff annotation carries the same class,
113+
which made the old class name useless there.
114+
- The hover card was anchored at the containing box's centre, so it covered
115+
whatever was under the cursor. It now clears the box by half its on-screen
116+
height plus a gap, flipping below only when there isn't room above.
117+
Offsets are divided by the dialog container's CSS scale (0.5 / 0.66666
118+
from `.global_edit_suggestion`), which otherwise halves them.
119+
- Verified in browser on eval run #3 item 503: `canvasses__diff` holds `fn`,
120+
`fp` and `tp` all at opacity 1, each is hover-targetable, the card reads
121+
"True Positive" / "False Negative", and it sits a 10 px gap above the
122+
hovered box in every sampled position. Lint clean in both repos.
123+
124+
- [x] 11. Hover on the annotation boundary, not its containing box
125+
- `get_edit_candidates` already hit-tests exactly (`get_pixel` for bitmasks,
126+
point-in-polygon for polygons), so this cost nothing extra. The stray
127+
hovers came from the fallback underneath: when nothing contains the
128+
cursor, it still picked the smallest annotation whose *containing box*
129+
was within `dst_thresh`. That fallback exists so you can grab an
130+
annotation to edit it, which a read-only subtask never needs.
131+
- Now skipped when the subtask is read-only and the spatial type has an
132+
exact test. Types without one (polyline, tbar, contour) keep the box
133+
fallback, so they stay hoverable.
134+
- Verified on run #3 item 503: across six probes the hover card appeared if
135+
and only if the cursor was over a painted mask pixel, comparing against
136+
the coordinates ULabel itself received. GT polylines still hover and read
137+
"Row". 166 unit tests pass.

‎index.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ export type ULabelConstructorArgs = {
328328
* can dim or raise.
329329
*/
330330
annotation_canvas_group_resolver?: (annotation: ULabelAnnotation) => string | null;
331+
/**
332+
* Override the name the hover card shows for an annotation. Return null to
333+
* fall back to its class name.
334+
*/
335+
annotation_display_name_resolver?: (annotation: ULabelAnnotation) => string | null;
331336
/** @deprecated Use top-level properties instead. */
332337
config_data?: object;
333338
};

‎src/configuration.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export class Configuration {
155155
// fall back to its class id. Grouping decides what `set_active_class_layer`
156156
// can dim or raise, so a diff subtask can layer by outcome instead of class.
157157
public annotation_canvas_group_resolver: ((annotation: ULabelAnnotation) => string | null) | null = null;
158+
// Override the name the hover card shows for an annotation. Return null to
159+
// fall back to its class name, so a diff subtask can label by outcome.
160+
public annotation_display_name_resolver: ((annotation: ULabelAnnotation) => string | null) | null = null;
158161
public username: string = "ULabelUser";
159162
public initial_line_size: number = 5;
160163

‎src/geometric_utils.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,48 @@ export class GeometricUtils {
543543
return false;
544544
}
545545

546+
/** Squared distance from a point to a line segment, endpoints included. */
547+
public static point_segment_distance_squared(
548+
point: Point2D,
549+
kp1: Point2D,
550+
kp2: Point2D,
551+
): number {
552+
const dx: number = kp2[0] - kp1[0];
553+
const dy: number = kp2[1] - kp1[1];
554+
const len_sq: number = dx * dx + dy * dy;
555+
let t: number = 0;
556+
if (len_sq > 0) {
557+
t = ((point[0] - kp1[0]) * dx + (point[1] - kp1[1]) * dy) / len_sq;
558+
t = Math.max(0, Math.min(1, t));
559+
}
560+
const nx: number = kp1[0] + t * dx - point[0];
561+
const ny: number = kp1[1] + t * dy - point[1];
562+
return nx * nx + ny * ny;
563+
}
564+
565+
/** Whether a point falls within `threshold` of a polyline's path. */
566+
public static point_is_near_polyline(
567+
point: Point2D,
568+
polyline: ULabelSpatialPayload2D,
569+
threshold: number,
570+
): boolean {
571+
if (polyline.length === 0) return false;
572+
const threshold_sq: number = threshold * threshold;
573+
if (polyline.length === 1) {
574+
const dx: number = polyline[0][0] - point[0];
575+
const dy: number = polyline[0][1] - point[1];
576+
return dx * dx + dy * dy <= threshold_sq;
577+
}
578+
for (let i = 0; i < polyline.length - 1; i++) {
579+
if (
580+
GeometricUtils.point_segment_distance_squared(point, polyline[i], polyline[i + 1]) <= threshold_sq
581+
) {
582+
return true;
583+
}
584+
}
585+
return false;
586+
}
587+
546588
// Convert a bbox to a simple polygon by adding the last point
547589
public static bbox_to_simple_polygon(
548590
bbox: ULabelSpatialPayload2D,

‎src/index.js‎

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,11 +3658,29 @@ export class ULabel {
36583658
current_subtask["state"]["visible_dialogs"][esid]["top"] = new_top;
36593659
// Decide confidence card position from the un-offset cbox so it stays stable during moves.
36603660
// Account for annbox scroll: what matters is the visible position, not the image-space position.
3661-
const cbox_center_y_imwrap = ((cbox["tly"] + cbox["bry"]) / 2) * this.state["zoom_val"];
36623661
const scroll_top = $("#" + this.config["annbox_id"]).scrollTop() || 0;
36633662
const conf_id = `global_annotation_confidence__${subtask_key}`;
3664-
const flip_below = (cbox_center_y_imwrap - scroll_top) < 100;
3665-
$(`#${conf_id}`).css("margin-top", flip_below ? "-1em" : "-9.5em");
3663+
const conf_jq = $(`#${conf_id}`);
3664+
// The dialog container is CSS-scaled about the anchor, so a margin
3665+
// set here lands `scale` times as far. `outerHeight` is already in
3666+
// that local space; the cbox measurements are in screen px.
3667+
const es_el = esjq[0];
3668+
const scale = es_el.offsetWidth > 0 ?
3669+
es_el.getBoundingClientRect().width / es_el.offsetWidth :
3670+
1;
3671+
const card_height = conf_jq.outerHeight() || 0;
3672+
const gap = 10;
3673+
// The anchor is the box centre, so clear it by half the on-screen
3674+
// box height or the card covers whatever is being hovered.
3675+
const half_height = Math.abs(cbox["bry"] - cbox["tly"]) / 2 * this.state["zoom_val"];
3676+
const box_top_visible = (cbox["tly"] * this.state["zoom_val"]) - scroll_top;
3677+
const flip_below = box_top_visible - (card_height * scale) - gap < 0;
3678+
conf_jq.css(
3679+
"margin-top",
3680+
flip_below ?
3681+
`${(half_height + gap) / scale}px` :
3682+
`${-((half_height + gap) / scale + card_height)}px`,
3683+
);
36663684
this.reposition_dialogs();
36673685
idd_x = (cbox["tlx"] + cbox["brx"] + 2 * diffX) / 2;
36683686
idd_y = (cbox["tly"] + cbox["bry"] + 2 * diffY) / 2;
@@ -6236,6 +6254,14 @@ export class ULabel {
62366254
let minsize = Infinity;
62376255
let found_containing_annotation = false;
62386256
const active_class_layer = this.get_current_subtask()["state"]["active_class_layer"] ?? null;
6257+
// A read-only subtask has nothing to grab, so the near-miss fallback
6258+
// below is just noise: require a real hit wherever one can be tested.
6259+
const require_exact_hit = this.is_current_subtask_read_only();
6260+
// One cursor pixel spans several image pixels when zoomed out, so the
6261+
// exact tests get that much slack. Without it a thin mask or the edge
6262+
// of a large one is unhittable: the pixel under the cursor is empty
6263+
// even though the downscaled render looks filled there.
6264+
const slack = 2 / this.get_empirical_scale();
62396265
// TODO(3d)
62406266
for (let edi = 0; edi < this.get_current_subtask()["annotations"]["ordering"].length; edi++) {
62416267
const annotation_id = this.get_current_subtask()["annotations"]["ordering"][edi];
@@ -6273,6 +6299,9 @@ export class ULabel {
62736299
(this.state["current_frame"] <= cbox["brz"])
62746300
) {
62756301
let is_a_containing_annotation = false;
6302+
// Whether this spatial type can be hit-tested against its real
6303+
// boundary rather than just its containing box.
6304+
let has_exact_test = true;
62766305
let boxsize = (cbox["brx"] - cbox["tlx"]) * (cbox["bry"] - cbox["tly"]);
62776306
switch (spatial_type) {
62786307
case "polygon":
@@ -6292,14 +6321,28 @@ export class ULabel {
62926321
is_a_containing_annotation = true;
62936322
}
62946323
break;
6324+
case "polyline":
6325+
// Within the drawn stroke of the line itself
6326+
if (GeometricUtils.point_is_near_polyline(
6327+
[gblx, gbly],
6328+
annotation["spatial_payload"],
6329+
(annotation["line_size"] ?? this.get_subtask_line_size()) / 2 + slack,
6330+
)) {
6331+
is_a_containing_annotation = true;
6332+
}
6333+
break;
62956334
case "bitmask":
62966335
// The mouse must be over a painted pixel of the mask
6297-
if (this.get_bitmask(annotation).get_pixel(Math.round(gblx), Math.round(gbly))) {
6336+
if (this.get_bitmask(annotation).has_foreground_in_circle(
6337+
Math.round(gblx),
6338+
Math.round(gbly),
6339+
slack,
6340+
)) {
62986341
is_a_containing_annotation = true;
62996342
}
63006343
break;
63016344
default:
6302-
6345+
has_exact_test = false;
63036346
break;
63046347
}
63056348

@@ -6321,7 +6364,7 @@ export class ULabel {
63216364
};
63226365
}
63236366
}
6324-
} else if (boxsize < minsize) {
6367+
} else if (!(require_exact_hit && has_exact_test) && boxsize < minsize) {
63256368
ret["candidate_ids"].push(annotation_id);
63266369
minsize = boxsize;
63276370
ret["best"] = {
@@ -6452,18 +6495,31 @@ export class ULabel {
64526495

64536496
// ================= Mouse event interpreters =================
64546497

6498+
/**
6499+
* Page coordinates of the annbox's content origin, which is where the image
6500+
* starts. `offset()` gives the border box, so any border on the annbox would
6501+
* otherwise shift every position by its width.
6502+
*/
6503+
get_annbox_content_origin() {
6504+
const annbox = $("#" + this.config["annbox_id"]);
6505+
const offset = annbox.offset();
6506+
const el = annbox[0];
6507+
return {
6508+
left: offset.left + (el?.clientLeft ?? 0) - annbox.scrollLeft(),
6509+
top: offset.top + (el?.clientTop ?? 0) - annbox.scrollTop(),
6510+
};
6511+
}
6512+
64556513
// Get the mouse position on the screen
64566514
get_global_mouse_x(mouse_event) {
64576515
const scale = this.get_empirical_scale();
6458-
const annbox = $("#" + this.config["annbox_id"]);
6459-
const raw = (mouse_event.pageX - annbox.offset().left + annbox.scrollLeft()) / scale;
6516+
const raw = (mouse_event.pageX - this.get_annbox_content_origin().left) / scale;
64606517
return raw;
64616518
}
64626519

64636520
get_global_mouse_y(mouse_event) {
64646521
const scale = this.get_empirical_scale();
6465-
const annbox = $("#" + this.config["annbox_id"]);
6466-
const raw = (mouse_event.pageY - annbox.offset().top + annbox.scrollTop()) / scale;
6522+
const raw = (mouse_event.pageY - this.get_annbox_content_origin().top) / scale;
64676523
return raw;
64686524
}
64696525

@@ -6491,16 +6547,14 @@ export class ULabel {
64916547

64926548
get_global_element_center_x(jqel) {
64936549
const scale = this.get_empirical_scale();
6494-
const annbox = $("#" + this.config["annbox_id"]);
6495-
const raw = (jqel.offset().left + jqel.width() / 2 - annbox.offset().left + annbox.scrollLeft()) / scale;
6550+
const raw = (jqel.offset().left + jqel.width() / 2 - this.get_annbox_content_origin().left) / scale;
64966551
// return Math.round(raw);
64976552
return raw;
64986553
}
64996554

65006555
get_global_element_center_y(jqel) {
65016556
const scale = this.get_empirical_scale();
6502-
const annbox = $("#" + this.config["annbox_id"]);
6503-
const raw = (jqel.offset().top + jqel.height() / 2 - annbox.offset().top + annbox.scrollTop()) / scale;
6557+
const raw = (jqel.offset().top + jqel.height() / 2 - this.get_annbox_content_origin().top) / scale;
65046558
// return Math.round();
65056559
return raw;
65066560
}
@@ -6829,10 +6883,11 @@ export class ULabel {
68296883
class_name = class_def.name;
68306884
}
68316885
}
6886+
const display_name = this.config.annotation_display_name_resolver?.(active_annotation) ?? class_name;
68326887

68336888
// Update the display dialog
68346889
const global_id = `global_annotation_confidence__${subtask_key}`;
6835-
$(`#${global_id} .annotation-confidence-classname`).text(class_name);
6890+
$(`#${global_id} .annotation-confidence-classname`).text(display_name);
68366891
$(`#${global_id} .annotation-confidence-value`).text(`Confidence: ${confidence.toFixed(2)}`);
68376892
}
68386893

0 commit comments

Comments
 (0)