Skip to content

Commit 589669e

Browse files
committed
cleanup
1 parent 87d3923 commit 589669e

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed

src/layer/segmentation/index.ts

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,18 @@ import {
141141
import type { DependentViewContext } from "#src/widget/dependent_view_widget.js";
142142
import { registerLayerShaderControlsTool } from "#src/widget/shader_controls.js";
143143
import { ShaderControlState } from "#src/webgl/shader_ui_controls.js";
144+
import { computeInvlerp } from "#src/util/lerp.js";
144145

145146
const MAX_LAYER_BAR_UI_INDICATOR_COLORS = 6;
146147

147148
export interface SegmentPropertyColor {
148149
type: "tag" | "numeric";
149-
property: string;
150150
active: boolean;
151+
property?: string;
151152
map?: Map<string, string>; // TODO is there a color type?
152153
options?: {
153-
min: number;
154-
max: number;
154+
min?: number;
155+
max?: number;
155156
minColor: string;
156157
maxColor: string;
157158
};
@@ -368,33 +369,32 @@ export class SegmentationUserLayerColorGroupState
368369
}
369370
}
370371
} else if (propertyColor.type === "numeric") {
371-
372372
const options = propertyColor.options!;
373-
374-
375-
const {numericalProperties} = segmentPropertyMap;
376-
377-
const numericalProperty = numericalProperties.find(x => x.id === propertyColor.property);
378-
379-
const {min, max} = options;
380-
381-
const minColor = parseRGBColorSpecification(options.minColor);
382-
const maxColor = parseRGBColorSpecification(options.maxColor);
383-
373+
const { numericalProperties } = segmentPropertyMap;
374+
const numericalProperty = numericalProperties.find(
375+
(x) => x.id === propertyColor.property,
376+
);
384377
if (numericalProperty) {
385-
const {values} = numericalProperty;
386-
for (const [index, id] of (
387-
segmentPropertyMap.segmentPropertyMap.inlineProperties?.ids ||
388-
[]
389-
).entries()) {
390-
const value = values[index];
391-
if (Number.isNaN(value)) continue;
392-
const valueClamped = Math.min(Math.max(value, min), max);
393-
const valueNormalized = (valueClamped - min) / (max - min);
378+
const minColor = parseRGBColorSpecification(options.minColor);
379+
const maxColor = parseRGBColorSpecification(options.maxColor);
394380
const color = vec3.create();
395-
vec3.lerp(color, minColor, maxColor, valueNormalized);
396-
map.set(id, BigInt(packColor(color)));
397-
}
381+
const { values, bounds } = numericalProperty;
382+
const min = Number(options.min ?? bounds[0]);
383+
const max = Number(options.max ?? bounds[1]);
384+
385+
for (const [index, id] of (
386+
segmentPropertyMap.segmentPropertyMap.inlineProperties?.ids ||
387+
[]
388+
).entries()) {
389+
const value = values[index];
390+
if (Number.isNaN(value)) continue;
391+
const normalized = Math.min(
392+
1,
393+
Math.max(0, computeInvlerp([min, max], value)),
394+
);
395+
vec3.lerp(color, minColor, maxColor, normalized);
396+
map.set(id, BigInt(packColor(color)));
397+
}
398398
}
399399
}
400400
}
@@ -439,14 +439,12 @@ export class SegmentationUserLayerColorGroupState
439439
parseRGBColorSpecification(String(x)),
440440
);
441441
for (const [idStr, colorVec] of result) {
442-
console.log("stated color", idStr, colorVec);
443442
const id = parseUint64(idStr);
444443
const color = BigInt(packColor(colorVec));
445444
this.segmentStatedColors.set(id, color);
446445
}
447446
},
448447
);
449-
450448
verifyOptionalObjectProperty(
451449
specification,
452450
json_keys.SEGMENT_PROPERTY_COLORS_JSON_KEY,
@@ -459,11 +457,6 @@ export class SegmentationUserLayerColorGroupState
459457
"type",
460458
verifyString,
461459
);
462-
const property = verifyObjectProperty(
463-
propertyColor,
464-
"property",
465-
verifyString,
466-
);
467460
const active = verifyOptionalObjectProperty(
468461
propertyColor,
469462
"active",
@@ -486,18 +479,21 @@ export class SegmentationUserLayerColorGroupState
486479
this.segmentPropertyColors.value.push({
487480
type,
488481
active: active ?? true,
489-
property,
490482
map,
491483
});
492-
continue;
493484
} else if (type === "numeric") {
485+
const property = verifyObjectProperty(
486+
propertyColor,
487+
"property",
488+
verifyString,
489+
);
494490
const options = verifyObjectProperty(
495491
propertyColor,
496492
"options",
497493
(x) => {
498494
verifyObject(x);
499-
const min = verifyObjectProperty(x, "min", verifyFloat);
500-
const max = verifyObjectProperty(x, "max", verifyFloat);
495+
const min = verifyOptionalObjectProperty(x, "min", verifyFloat);
496+
const max = verifyOptionalObjectProperty(x, "max", verifyFloat);
501497
const minColor = verifyObjectProperty(
502498
x,
503499
"minColor",
@@ -518,8 +514,8 @@ export class SegmentationUserLayerColorGroupState
518514
);
519515
this.segmentPropertyColors.value.push({
520516
type,
521-
active: active ?? true,
522517
property,
518+
active: active ?? true,
523519
options,
524520
});
525521
} else {
@@ -552,14 +548,13 @@ export class SegmentationUserLayerColorGroupState
552548
const p: any = {};
553549
p["type"] = propertyColor.type;
554550
p["property"] = propertyColor.property;
555-
p["active"] = propertyColor.active;
551+
p["active"] = propertyColor.active ? undefined : false;
556552
if (propertyColor.type === "tag") {
557553
const map: any = (p[json_keys.MAP_JSON_KEY] = {});
558554
for (const [key, value] of propertyColor.map!.entries()) {
559555
map[key.toString()] = value;
560556
}
561557
} else {
562-
// TODO numeric
563558
p["options"] = propertyColor.options;
564559
}
565560
j.push(p);

0 commit comments

Comments
 (0)