Skip to content

Commit aad7550

Browse files
committed
use globalCoordinateSpace.units in defaultProperties rather than assuming meters
split line length code into a separate function
1 parent 03eaf93 commit aad7550

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/annotation/index.ts

+45-16
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[];
@@ -763,6 +765,28 @@ function deserializeTwoFloatVectors(
763765
return offset;
764766
}
765767

768+
function lineLength(
769+
line: Line,
770+
scales: Float64Array,
771+
units: readonly string[],
772+
) {
773+
const rank = scales.length;
774+
if (rank !== line.pointA.length || rank !== line.pointB.length) {
775+
return;
776+
}
777+
let lengthSquared = 0;
778+
for (let dim = 0; dim < rank; dim++) {
779+
const unitInfo = ALLOWED_UNITS.find((x) => x.unit === units[dim]);
780+
if (!unitInfo) {
781+
return;
782+
}
783+
const voxelToNanometers = scales[dim] * unitInfo.lengthInNanometers;
784+
lengthSquared +=
785+
((line.pointA[dim] - line.pointB[dim]) * voxelToNanometers) ** 2;
786+
}
787+
return Math.sqrt(lengthSquared);
788+
}
789+
766790
export const annotationTypeHandlers: Record<
767791
AnnotationType,
768792
AnnotationTypeHandler
@@ -826,29 +850,22 @@ export const annotationTypeHandlers: Record<
826850
callback(annotation.pointA, false);
827851
callback(annotation.pointB, false);
828852
},
829-
defaultProperties(annotation: Line, scales: Float64Array) {
853+
defaultProperties(
854+
annotation: Line,
855+
scales: Float64Array,
856+
units: readonly string[],
857+
) {
830858
const properties: AnnotationNumericPropertySpec[] = [];
831859
const values: number[] = [];
832-
const rank = scales.length;
833-
if (
834-
rank === annotation.pointA.length &&
835-
rank === annotation.pointB.length
836-
) {
860+
const length = lineLength(annotation, scales, units);
861+
if (length) {
837862
properties.push({
838863
type: "float32",
839864
identifier: "Length",
840865
default: 0,
841866
description: "Length of the line annotation in nanometers",
842867
format: formatLength,
843868
});
844-
let length = 0;
845-
for (let dim = 0; dim < rank; dim++) {
846-
length +=
847-
(((annotation.pointA[dim] - annotation.pointB[dim]) / 1e-9) *
848-
scales[dim]) **
849-
2;
850-
}
851-
length = Math.sqrt(length);
852869
values.push(length);
853870
}
854871
return { properties, values };
@@ -897,9 +914,14 @@ export const annotationTypeHandlers: Record<
897914
visitGeometry(annotation: Point, callback) {
898915
callback(annotation.point, false);
899916
},
900-
defaultProperties(annotation: Point, scales: Float64Array) {
917+
defaultProperties(
918+
annotation: Point,
919+
scales: Float64Array,
920+
units: string[],
921+
) {
901922
annotation;
902923
scales;
924+
units;
903925
return { properties: [], values: [] };
904926
},
905927
},
@@ -973,9 +995,11 @@ export const annotationTypeHandlers: Record<
973995
defaultProperties(
974996
annotation: AxisAlignedBoundingBox,
975997
scales: Float64Array,
998+
units: string[],
976999
) {
9771000
annotation;
9781001
scales;
1002+
units;
9791003
return { properties: [], values: [] };
9801004
},
9811005
},
@@ -1046,9 +1070,14 @@ export const annotationTypeHandlers: Record<
10461070
callback(annotation.center, false);
10471071
callback(annotation.radii, true);
10481072
},
1049-
defaultProperties(annotation: Ellipsoid, scales: Float64Array) {
1073+
defaultProperties(
1074+
annotation: Ellipsoid,
1075+
scales: Float64Array,
1076+
units: string[],
1077+
) {
10501078
annotation;
10511079
scales;
1080+
units;
10521081
return { properties: [], values: [] };
10531082
},
10541083
},

src/ui/annotations.ts

+5-1
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)