Skip to content

Commit 324342f

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

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/annotation/index.ts

Lines changed: 46 additions & 16 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[];
@@ -763,6 +765,29 @@ function deserializeTwoFloatVectors(
763765
return offset;
764766
}
765767

768+
function lineLength(
769+
line: Line,
770+
scales: Float64Array,
771+
units: readonly string[],
772+
) {
773+
const scalesRank = scales.length;
774+
const lineRank = line.pointA.length;
775+
if (scalesRank < lineRank) {
776+
return;
777+
}
778+
let lengthSquared = 0;
779+
for (let dim = 0; dim < lineRank; dim++) {
780+
const unitInfo = ALLOWED_UNITS.find((x) => x.unit === units[dim]);
781+
if (!unitInfo) {
782+
return;
783+
}
784+
const voxelToNanometers = scales[dim] * unitInfo.lengthInNanometers;
785+
lengthSquared +=
786+
((line.pointA[dim] - line.pointB[dim]) * voxelToNanometers) ** 2;
787+
}
788+
return Math.sqrt(lengthSquared);
789+
}
790+
766791
export const annotationTypeHandlers: Record<
767792
AnnotationType,
768793
AnnotationTypeHandler
@@ -826,29 +851,22 @@ export const annotationTypeHandlers: Record<
826851
callback(annotation.pointA, false);
827852
callback(annotation.pointB, false);
828853
},
829-
defaultProperties(annotation: Line, scales: Float64Array) {
854+
defaultProperties(
855+
annotation: Line,
856+
scales: Float64Array,
857+
units: readonly string[],
858+
) {
830859
const properties: AnnotationNumericPropertySpec[] = [];
831860
const values: number[] = [];
832-
const rank = scales.length;
833-
if (
834-
rank === annotation.pointA.length &&
835-
rank === annotation.pointB.length
836-
) {
861+
const length = lineLength(annotation, scales, units);
862+
if (length) {
837863
properties.push({
838864
type: "float32",
839865
identifier: "Length",
840866
default: 0,
841867
description: "Length of the line annotation in nanometers",
842868
format: formatLength,
843869
});
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);
852870
values.push(length);
853871
}
854872
return { properties, values };
@@ -897,9 +915,14 @@ export const annotationTypeHandlers: Record<
897915
visitGeometry(annotation: Point, callback) {
898916
callback(annotation.point, false);
899917
},
900-
defaultProperties(annotation: Point, scales: Float64Array) {
918+
defaultProperties(
919+
annotation: Point,
920+
scales: Float64Array,
921+
units: string[],
922+
) {
901923
annotation;
902924
scales;
925+
units;
903926
return { properties: [], values: [] };
904927
},
905928
},
@@ -973,9 +996,11 @@ export const annotationTypeHandlers: Record<
973996
defaultProperties(
974997
annotation: AxisAlignedBoundingBox,
975998
scales: Float64Array,
999+
units: string[],
9761000
) {
9771001
annotation;
9781002
scales;
1003+
units;
9791004
return { properties: [], values: [] };
9801005
},
9811006
},
@@ -1046,9 +1071,14 @@ export const annotationTypeHandlers: Record<
10461071
callback(annotation.center, false);
10471072
callback(annotation.radii, true);
10481073
},
1049-
defaultProperties(annotation: Ellipsoid, scales: Float64Array) {
1074+
defaultProperties(
1075+
annotation: Ellipsoid,
1076+
scales: Float64Array,
1077+
units: string[],
1078+
) {
10501079
annotation;
10511080
scales;
1081+
units;
10521082
return { properties: [], values: [] };
10531083
},
10541084
},

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)