Skip to content

Commit c45bba8

Browse files
committed
use globalCoordinateSpace.units in defaultProperties rather than assuming meters
1 parent 03eaf93 commit c45bba8

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/annotation/index.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { getRandomHexString } from "#src/util/random.js";
5656
import { NullarySignal, Signal } from "#src/util/signal.js";
5757
import { formatLength } from "#src/util/spatial_units.js";
5858
import { Uint64 } from "#src/util/uint64.js";
59+
import { ALLOWED_UNITS } from "#src/widget/scale_bar.js";
5960

6061
export type AnnotationId = string;
6162

@@ -703,6 +704,7 @@ export interface AnnotationTypeHandler<T extends Annotation = Annotation> {
703704
defaultProperties: (
704705
annotation: T,
705706
scales: Float64Array,
707+
units: readonly string[],
706708
) => {
707709
properties: AnnotationNumericPropertySpec[];
708710
values: number[];
@@ -826,30 +828,43 @@ export const annotationTypeHandlers: Record<
826828
callback(annotation.pointA, false);
827829
callback(annotation.pointB, false);
828830
},
829-
defaultProperties(annotation: Line, scales: Float64Array) {
831+
defaultProperties(
832+
annotation: Line,
833+
scales: Float64Array,
834+
units: readonly string[],
835+
) {
830836
const properties: AnnotationNumericPropertySpec[] = [];
831837
const values: number[] = [];
832838
const rank = scales.length;
833839
if (
834840
rank === annotation.pointA.length &&
835841
rank === annotation.pointB.length
836842
) {
837-
properties.push({
838-
type: "float32",
839-
identifier: "Length",
840-
default: 0,
841-
description: "Length of the line annotation in nanometers",
842-
format: formatLength,
843-
});
843+
let validLength = true;
844844
let length = 0;
845845
for (let dim = 0; dim < rank; dim++) {
846+
const unitInfo = ALLOWED_UNITS.find((x) => x.unit === units[dim]);
847+
if (!unitInfo) {
848+
validLength = false;
849+
break;
850+
}
851+
const voxelToNanometers = scales[dim] * unitInfo.lengthInNanometers;
846852
length +=
847-
(((annotation.pointA[dim] - annotation.pointB[dim]) / 1e-9) *
848-
scales[dim]) **
853+
((annotation.pointA[dim] - annotation.pointB[dim]) *
854+
voxelToNanometers) **
849855
2;
850856
}
851-
length = Math.sqrt(length);
852-
values.push(length);
857+
if (validLength) {
858+
properties.push({
859+
type: "float32",
860+
identifier: "Length",
861+
default: 0,
862+
description: "Length of the line annotation in nanometers",
863+
format: formatLength,
864+
});
865+
length = Math.sqrt(length);
866+
values.push(length);
867+
}
853868
}
854869
return { properties, values };
855870
},
@@ -897,9 +912,14 @@ export const annotationTypeHandlers: Record<
897912
visitGeometry(annotation: Point, callback) {
898913
callback(annotation.point, false);
899914
},
900-
defaultProperties(annotation: Point, scales: Float64Array) {
915+
defaultProperties(
916+
annotation: Point,
917+
scales: Float64Array,
918+
units: string[],
919+
) {
901920
annotation;
902921
scales;
922+
units;
903923
return { properties: [], values: [] };
904924
},
905925
},
@@ -973,9 +993,11 @@ export const annotationTypeHandlers: Record<
973993
defaultProperties(
974994
annotation: AxisAlignedBoundingBox,
975995
scales: Float64Array,
996+
units: string[],
976997
) {
977998
annotation;
978999
scales;
1000+
units;
9791001
return { properties: [], values: [] };
9801002
},
9811003
},
@@ -1046,9 +1068,14 @@ export const annotationTypeHandlers: Record<
10461068
callback(annotation.center, false);
10471069
callback(annotation.radii, true);
10481070
},
1049-
defaultProperties(annotation: Ellipsoid, scales: Float64Array) {
1071+
defaultProperties(
1072+
annotation: Ellipsoid,
1073+
scales: Float64Array,
1074+
units: string[],
1075+
) {
10501076
annotation;
10511077
scales;
1078+
units;
10521079
return { properties: [], values: [] };
10531080
},
10541081
},

src/ui/annotations.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,11 @@ export function UserLayerWithAnnotationsMixin<
18581858
this.manager.root.coordinateSpace.value;
18591859
const defaultProperties = annotationTypeHandlers[
18601860
annotation.type
1861-
].defaultProperties(annotation, globalCoordinateSpace.scales);
1861+
].defaultProperties(
1862+
annotation,
1863+
globalCoordinateSpace.scales,
1864+
globalCoordinateSpace.units,
1865+
);
18621866
const allProperties = [
18631867
...defaultProperties.properties,
18641868
...properties,

0 commit comments

Comments
 (0)