-
Notifications
You must be signed in to change notification settings - Fork 317
feat(annotation) Add default annotation properties #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
47f8d34
356c42c
03eaf93
324342f
43c3e4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,9 @@ import { | |
import { parseDataTypeValue } from "#src/util/lerp.js"; | ||
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; | ||
|
||
|
@@ -106,6 +108,7 @@ export interface AnnotationNumericPropertySpec | |
min?: number; | ||
max?: number; | ||
step?: number; | ||
format?: (x: number) => string; | ||
} | ||
|
||
export const propertyTypeDataType: Record< | ||
|
@@ -496,6 +499,9 @@ export function formatNumericProperty( | |
property: AnnotationNumericPropertySpec, | ||
value: number, | ||
): string { | ||
if (property.format) { | ||
return property.format(value); | ||
} | ||
const formattedValue = | ||
property.type === "float32" ? value.toPrecision(6) : value.toString(); | ||
const { enumValues, enumLabels } = property; | ||
|
@@ -695,6 +701,15 @@ export interface AnnotationTypeHandler<T extends Annotation = Annotation> { | |
annotation: T, | ||
callback: (vec: Float32Array, isVector: boolean) => void, | ||
) => void; | ||
defaultProperties: ( | ||
annotation: T, | ||
layerPosition: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: readonly string[], | ||
) => { | ||
properties: AnnotationNumericPropertySpec[]; | ||
values: number[]; | ||
}; | ||
} | ||
|
||
function serializeFloatVector( | ||
|
@@ -751,6 +766,32 @@ function deserializeTwoFloatVectors( | |
return offset; | ||
} | ||
|
||
function lineLength( | ||
annotationLayerPositions: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: readonly string[], | ||
) { | ||
if (annotationLayerPositions.length !== 2) { | ||
return; | ||
} | ||
const [pointA, pointB] = annotationLayerPositions; | ||
const scalesRank = scales.length; | ||
const lineRank = pointA.length; | ||
if (scalesRank < lineRank) { | ||
return; | ||
} | ||
let lengthSquared = 0; | ||
for (let dim = 0; dim < lineRank; dim++) { | ||
const unitInfo = ALLOWED_UNITS.find((x) => x.unit === units[dim]); | ||
if (!unitInfo) { | ||
return; | ||
} | ||
const voxelToNanometers = scales[dim] * unitInfo.lengthInNanometers; | ||
lengthSquared += ((pointA[dim] - pointB[dim]) * voxelToNanometers) ** 2; | ||
} | ||
return Math.sqrt(lengthSquared); | ||
} | ||
|
||
export const annotationTypeHandlers: Record< | ||
AnnotationType, | ||
AnnotationTypeHandler | ||
|
@@ -814,6 +855,28 @@ export const annotationTypeHandlers: Record< | |
callback(annotation.pointA, false); | ||
callback(annotation.pointB, false); | ||
}, | ||
defaultProperties( | ||
annotation: Line, | ||
annotationLayerPositions: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: readonly string[], | ||
) { | ||
annotation; | ||
const properties: AnnotationNumericPropertySpec[] = []; | ||
const values: number[] = []; | ||
const length = lineLength(annotationLayerPositions, scales, units); | ||
if (length) { | ||
properties.push({ | ||
type: "float32", | ||
identifier: "Length", | ||
default: 0, | ||
description: "Length of the line annotation in nanometers", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably just say "length of the line annotation", with a type of string, and then the value can include the appropriate unit suffix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Jeremy, getting back to this now, are you suggesting adding "string" as a type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I forgot that string properties aren't supported for annotations --- only for segment properties. They would be challenging to support within a shader, and some changes to the precomputed annotation encoding would be needed to support strings in that format, but there is no fundamental difficulty with supporting them solely for display in the "Selection details" panel. However, I realize that with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, would you suggest skipping the "X meters + Y seconds" idea and instead only calculate length if all the non-zeros are the same unit? |
||
format: formatLength, | ||
}); | ||
values.push(length); | ||
} | ||
return { properties, values }; | ||
}, | ||
}, | ||
[AnnotationType.POINT]: { | ||
icon: "⚬", | ||
|
@@ -858,6 +921,18 @@ export const annotationTypeHandlers: Record< | |
visitGeometry(annotation: Point, callback) { | ||
callback(annotation.point, false); | ||
}, | ||
defaultProperties( | ||
annotation: Point, | ||
layerPosition: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: string[], | ||
) { | ||
annotation; | ||
layerPosition; | ||
scales; | ||
units; | ||
return { properties: [], values: [] }; | ||
}, | ||
}, | ||
[AnnotationType.AXIS_ALIGNED_BOUNDING_BOX]: { | ||
icon: "❑", | ||
|
@@ -926,6 +1001,18 @@ export const annotationTypeHandlers: Record< | |
callback(annotation.pointA, false); | ||
callback(annotation.pointB, false); | ||
}, | ||
defaultProperties( | ||
annotation: AxisAlignedBoundingBox, | ||
layerPosition: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: string[], | ||
) { | ||
annotation; | ||
layerPosition; | ||
scales; | ||
units; | ||
return { properties: [], values: [] }; | ||
}, | ||
}, | ||
[AnnotationType.ELLIPSOID]: { | ||
icon: "◎", | ||
|
@@ -994,6 +1081,18 @@ export const annotationTypeHandlers: Record< | |
callback(annotation.center, false); | ||
callback(annotation.radii, true); | ||
}, | ||
defaultProperties( | ||
annotation: Ellipsoid, | ||
layerPosition: Float32Array<ArrayBufferLike>[], | ||
scales: Float64Array, | ||
units: string[], | ||
) { | ||
annotation; | ||
layerPosition; | ||
scales; | ||
units; | ||
return { properties: [], values: [] }; | ||
}, | ||
}, | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
units
will always be normalized to "m" if it is some multiple of meters, so there is no need to check ALLOWED_UNITS.Note that ALLOWED_UNITS is actually from some now-dead code that was originally used to show annotation properties just like you are adding with this PR.
However, you may as well support any unit for length --- just as long as all units for which there is a non-zero delta are the same.
Or perhaps it could be represented as X meters + Y seconds, if for example there are both meters and seconds units. You would compute the length separately for each unit.
The actual SI prefix can be selected using pickSiPrefix, as for the scale bar.