-
Notifications
You must be signed in to change notification settings - Fork 304
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
feat(annotation) Add default annontation properties #694
Open
chrisj
wants to merge
5
commits into
google:master
Choose a base branch
from
seung-lab:cj-annotation-default-properties
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47f8d34
feat(annotation) Add default annontation properties
chrisj 356c42c
added optional format override property to AnnotationNumericPropertySpec
chrisj 03eaf93
no longer assume three dimensions when calculating default annotation…
chrisj 324342f
use globalCoordinateSpace.units in defaultProperties rather than assu…
chrisj 43c3e4a
fix default annotation properties to use transformed coordinates
chrisj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
||
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: [] }; | ||
}, | ||
}, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.