Skip to content

Commit

Permalink
use globalCoordinateSpace.units in defaultProperties rather than assu…
Browse files Browse the repository at this point in the history
…ming meters
  • Loading branch information
chrisj committed Jan 17, 2025
1 parent 03eaf93 commit c45bba8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
55 changes: 41 additions & 14 deletions src/annotation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { getRandomHexString } from "#src/util/random.js";
import { NullarySignal, Signal } from "#src/util/signal.js";
import { formatLength } from "#src/util/spatial_units.js";
import { Uint64 } from "#src/util/uint64.js";
import { ALLOWED_UNITS } from "#src/widget/scale_bar.js";

export type AnnotationId = string;

Expand Down Expand Up @@ -703,6 +704,7 @@ export interface AnnotationTypeHandler<T extends Annotation = Annotation> {
defaultProperties: (
annotation: T,
scales: Float64Array,
units: readonly string[],
) => {
properties: AnnotationNumericPropertySpec[];
values: number[];
Expand Down Expand Up @@ -826,30 +828,43 @@ export const annotationTypeHandlers: Record<
callback(annotation.pointA, false);
callback(annotation.pointB, false);
},
defaultProperties(annotation: Line, scales: Float64Array) {
defaultProperties(
annotation: Line,
scales: Float64Array,
units: readonly string[],
) {
const properties: AnnotationNumericPropertySpec[] = [];
const values: number[] = [];
const rank = scales.length;
if (
rank === annotation.pointA.length &&
rank === annotation.pointB.length
) {
properties.push({
type: "float32",
identifier: "Length",
default: 0,
description: "Length of the line annotation in nanometers",
format: formatLength,
});
let validLength = true;
let length = 0;
for (let dim = 0; dim < rank; dim++) {
const unitInfo = ALLOWED_UNITS.find((x) => x.unit === units[dim]);
if (!unitInfo) {
validLength = false;
break;
}
const voxelToNanometers = scales[dim] * unitInfo.lengthInNanometers;
length +=
(((annotation.pointA[dim] - annotation.pointB[dim]) / 1e-9) *
scales[dim]) **
((annotation.pointA[dim] - annotation.pointB[dim]) *
voxelToNanometers) **
2;
}
length = Math.sqrt(length);
values.push(length);
if (validLength) {
properties.push({
type: "float32",
identifier: "Length",
default: 0,
description: "Length of the line annotation in nanometers",
format: formatLength,
});
length = Math.sqrt(length);
values.push(length);
}
}
return { properties, values };
},
Expand Down Expand Up @@ -897,9 +912,14 @@ export const annotationTypeHandlers: Record<
visitGeometry(annotation: Point, callback) {
callback(annotation.point, false);
},
defaultProperties(annotation: Point, scales: Float64Array) {
defaultProperties(
annotation: Point,
scales: Float64Array,
units: string[],
) {
annotation;
scales;
units;
return { properties: [], values: [] };
},
},
Expand Down Expand Up @@ -973,9 +993,11 @@ export const annotationTypeHandlers: Record<
defaultProperties(
annotation: AxisAlignedBoundingBox,
scales: Float64Array,
units: string[],
) {
annotation;
scales;
units;
return { properties: [], values: [] };
},
},
Expand Down Expand Up @@ -1046,9 +1068,14 @@ export const annotationTypeHandlers: Record<
callback(annotation.center, false);
callback(annotation.radii, true);
},
defaultProperties(annotation: Ellipsoid, scales: Float64Array) {
defaultProperties(
annotation: Ellipsoid,
scales: Float64Array,
units: string[],
) {
annotation;
scales;
units;
return { properties: [], values: [] };
},
},
Expand Down
6 changes: 5 additions & 1 deletion src/ui/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,11 @@ export function UserLayerWithAnnotationsMixin<
this.manager.root.coordinateSpace.value;
const defaultProperties = annotationTypeHandlers[
annotation.type
].defaultProperties(annotation, globalCoordinateSpace.scales);
].defaultProperties(
annotation,
globalCoordinateSpace.scales,
globalCoordinateSpace.units,
);
const allProperties = [
...defaultProperties.properties,
...properties,
Expand Down

0 comments on commit c45bba8

Please sign in to comment.