Skip to content

Commit

Permalink
temp3
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj committed Sep 17, 2024
1 parent a5871c6 commit d8a2502
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/annotation/annotation_layer_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void main() {

export class AnnotationDisplayState extends RefCounted {
annotationProperties = new WatchableValue<
AnnotationPropertySpec[] | undefined
readonly Readonly<AnnotationPropertySpec>[] | undefined
>(undefined);
shader = makeTrackableFragmentMain(DEFAULT_FRAGMENT_MAIN);
shaderControls = new ShaderControlState(
Expand Down
9 changes: 6 additions & 3 deletions src/annotation/frontend_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
SliceViewChunkSource,
} from "#src/sliceview/frontend.js";
import { StatusMessage } from "#src/status.js";
import type { WatchableValue } from "#src/trackable_value.js";
import type { Borrowed, Owned } from "#src/util/disposable.js";
import { ENDIANNESS, Endianness } from "#src/util/endian.js";
import * as matrix from "#src/util/matrix.js";
Expand Down Expand Up @@ -517,7 +518,9 @@ export class MultiscaleAnnotationSource
spatiallyIndexedSources = new Set<Borrowed<AnnotationGeometryChunkSource>>();
rank: number;
readonly relationships: readonly string[];
readonly properties: Readonly<AnnotationPropertySpec>[];
readonly properties: WatchableValue<
readonly Readonly<AnnotationPropertySpec>[]
>;
readonly annotationPropertySerializers: AnnotationPropertySerializer[];
constructor(
public chunkManager: Borrowed<ChunkManager>,
Expand All @@ -529,10 +532,10 @@ export class MultiscaleAnnotationSource
) {
super();
this.rank = options.rank;
this.properties = options.properties;
this.properties.value = options.properties;
this.annotationPropertySerializers = makeAnnotationPropertySerializers(
this.rank,
this.properties,
this.properties.value,
);
const segmentFilteredSources: Owned<AnnotationSubsetGeometryChunkSource>[] =
(this.segmentFilteredSources = []);
Expand Down
58 changes: 45 additions & 13 deletions src/annotation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
CoordinateSpaceTransform,
WatchableCoordinateSpaceTransform,
} from "#src/coordinate_transform.js";
import { WatchableValue } from "#src/trackable_value.js";
import { arraysEqual } from "#src/util/array.js";
import {
packColor,
Expand Down Expand Up @@ -532,7 +533,7 @@ export function formatAnnotationPropertyValue(

export function parseAnnotationPropertyId(obj: unknown) {
const s = verifyString(obj);
if (s.match(/^[a-z][a-zA-Z0-9_ ]*$/) === null) {
if (s.match(/^[a-zA-Z0-9_ -]+$/) === null) {
throw new Error(`Invalid property identifier: ${JSON.stringify(obj)}`);
}
return s;
Expand Down Expand Up @@ -617,13 +618,19 @@ function parseAnnotationPropertySpec(obj: unknown): AnnotationPropertySpec {
}

function annotationPropertySpecToJson(spec: AnnotationPropertySpec) {
console.log("annotationPropertySpecToJson", spec);
const defaultValue = spec.default;
const tag = isAnnotationNumericPropertySpec(spec) ? spec.tag : undefined;
const isNumeric = isAnnotationNumericPropertySpec(spec);
const tag = isNumeric ? spec.tag : undefined;
const enum_values = isNumeric ? spec.enumValues : undefined;
const enum_labels = isNumeric ? spec.enumLabels : undefined;
return {
id: spec.identifier,
description: spec.description,
type: spec.type,
tag,
enum_values,
enum_labels,
default:
defaultValue === 0
? undefined
Expand Down Expand Up @@ -1013,7 +1020,7 @@ export const annotationTypeHandlers: Record<
export interface AnnotationSchema {
rank: number;
relationships: readonly string[];
properties: readonly AnnotationPropertySpec[];
properties: WatchableValue<readonly Readonly<AnnotationPropertySpec>[]>;
}

export function annotationToJson(
Expand All @@ -1033,8 +1040,8 @@ export function annotationToJson(
segments.map((x) => x.toString()),
);
}
if (schema.properties.length !== 0) {
const propertySpecs = schema.properties;
const propertySpecs = schema.properties.value;
if (propertySpecs.length !== 0) {
result.props = annotation.properties.map((prop, i) =>
annotationPropertyTypeHandlers[propertySpecs[i].type].serializeJson(prop),
);
Expand Down Expand Up @@ -1074,9 +1081,9 @@ function restoreAnnotation(
);
});
const properties = verifyObjectProperty(obj, "props", (propsObj) => {
const propSpecs = schema.properties;
const propSpecs = schema.properties.value;
if (propsObj === undefined) return propSpecs.map((x) => x.default);
return parseArray(expectArray(propsObj, schema.properties.length), (x, i) =>
return parseArray(expectArray(propsObj, propSpecs.length), (x, i) =>
annotationPropertyTypeHandlers[propSpecs[i].type].deserializeJson(x),
);
});
Expand Down Expand Up @@ -1121,16 +1128,20 @@ export class AnnotationSource

annotationPropertySerializers: AnnotationPropertySerializer[];

// new WatchableValue([])

constructor(
rank: number,
public readonly relationships: readonly string[] = [],
public readonly properties: Readonly<AnnotationPropertySpec>[] = [],
public readonly properties: WatchableValue<
readonly Readonly<AnnotationPropertySpec>[]
> = new WatchableValue([]),
) {
super();
this.rank_ = rank;
this.annotationPropertySerializers = makeAnnotationPropertySerializers(
rank,
properties,
properties.value,
);
}

Expand Down Expand Up @@ -1274,32 +1285,53 @@ export class LocalAnnotationSource extends AnnotationSource {

constructor(
public watchableTransform: WatchableCoordinateSpaceTransform,
public properties: AnnotationPropertySpec[],
public readonly properties: WatchableValue<
AnnotationPropertySpec[]
> = new WatchableValue([]),
relationships: string[],
) {
super(watchableTransform.value.sourceRank, relationships, properties);
this.curCoordinateTransform = watchableTransform.value;
this.registerDisposer(
watchableTransform.changed.add(() => this.ensureUpdated()),
);

this.registerDisposer(
properties.changed.add(() => {
this.updateAnnotationPropertySerializers();
this.changed.dispatch();
}),
);
}

updateAnnotationPropertySerializers() {
this.annotationPropertySerializers = makeAnnotationPropertySerializers(
this.rank_,
this.properties,
this.properties.value,
);
}

addProperty(property: AnnotationPropertySpec) {
this.properties.push(property);
this.updateAnnotationPropertySerializers();
this.properties.value.push(property);
this.properties.changed.dispatch();
for (const annotation of this) {
annotation.properties.push(property.default);
}
this.changed.dispatch();
}

removeProperty(identifier: string) {
const propertyIndex = this.properties.value.findIndex(
(x) => x.identifier === identifier,
);
this.properties.value.splice(propertyIndex, 1);
this.properties.changed.dispatch();
for (const annotation of this) {
annotation.properties.splice(propertyIndex, 1);
}
this.changed.dispatch();
}

ensureUpdated() {
const transform = this.watchableTransform.value;
const { curCoordinateTransform } = this;
Expand Down
8 changes: 6 additions & 2 deletions src/annotation/renderlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ function AnnotationRenderLayer<
for (const oldHelper of renderHelpers) {
oldHelper.dispose();
}
const { properties } = this.base.source;
const {
properties: { value: properties },
} = this.base.source;
const { displayState } = this.base.state;
for (const annotationType of annotationTypes) {
const handler = getAnnotationTypeRenderHandler(annotationType);
Expand Down Expand Up @@ -780,7 +782,9 @@ function AnnotationRenderLayer<
transformPickedValue(pickState: PickState) {
const { pickedAnnotationBuffer } = pickState;
if (pickedAnnotationBuffer === undefined) return undefined;
const { properties } = this.base.source;
const {
properties: { value: properties },
} = this.base.source;
if (properties.length === 0) return undefined;
const {
pickedAnnotationBufferBaseOffset,
Expand Down
2 changes: 1 addition & 1 deletion src/datasource/graphene/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ function makeColoredAnnotationState(
const { subsourceEntry } = loadedSubsource;
const source = new LocalAnnotationSource(
loadedSubsource.loadedDataSource.transform,
[],
new WatchableValue([]),
["associated segments"],
);

Expand Down
Loading

0 comments on commit d8a2502

Please sign in to comment.