Skip to content

Commit

Permalink
refactor(paint): consolidate ensure segment group is active if exists…
Browse files Browse the repository at this point in the history
… logic

Rename functions using labelMap to segmentGroup
  • Loading branch information
PaulHax committed Oct 23, 2024
1 parent c8777aa commit 1bc37fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/components/SegmentGroupControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const currentSegmentGroups = computed(() => {
const paintStore = usePaintToolStore();
const currentSegmentGroupID = computed({
get: () => paintStore.activeSegmentGroupID,
set: (id) => paintStore.setActiveLabelmap(id),
set: (id) => paintStore.setActiveSegmentGroup(id),
});
// clear selection if we delete the active segment group
Expand Down
61 changes: 35 additions & 26 deletions src/store/tools/paint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Vector2 } from '@kitware/vtk.js/types';
import { useCurrentImage } from '@/src/composables/useCurrentImage';
import { Manifest, StateFile } from '@/src/io/state-file/schema';
import { computed, ref, watch, watchEffect } from 'vue';
import { computed, ref, watchEffect } from 'vue';
import { vec3 } from 'gl-matrix';
import { defineStore } from 'pinia';
import { Maybe } from '@/src/types';
Expand Down Expand Up @@ -48,24 +48,37 @@ export const usePaintToolStore = defineStore('paint', () => {
/**
* Sets the active labelmap.
*/
function setActiveLabelmap(segmentGroupID: Maybe<string>) {
function setActiveSegmentGroup(segmentGroupID: Maybe<string>) {
activeSegmentGroupID.value = segmentGroupID;
}

/**
* Gets the first segment group ID for a given image.
* @param imageID
*/
function getFirstSegmentGroupID(imageID: Maybe<string>): Maybe<string> {
if (!imageID) return null;
const segmentGroups = segmentGroupStore.orderByParent[imageID];
if (segmentGroups && segmentGroups.length > 0) {
return segmentGroups[0];
}
return null;
}

/**
* Sets the active labelmap from a given image.
*
* If a labelmap exists, pick the first one. If no labelmap exists, create one.
*/
function setActiveLabelmapFromImage(imageID: Maybe<string>) {
function ensureActiveSegmentGroupForImage(imageID: Maybe<string>) {
if (!imageID) {
setActiveLabelmap(null);
setActiveSegmentGroup(null);
return;
}

const labelmaps = segmentGroupStore.orderByParent[imageID];
if (labelmaps?.length) {
activeSegmentGroupID.value = labelmaps[0];
const segmentGroupID = getFirstSegmentGroupID(imageID);
if (segmentGroupID) {
setActiveSegmentGroup(segmentGroupID);
} else {
activeSegmentGroupID.value =
segmentGroupStore.newLabelmapFromImage(imageID);
Expand Down Expand Up @@ -157,7 +170,7 @@ export const usePaintToolStore = defineStore('paint', () => {
if (!imageID) {
return false;
}
setActiveLabelmapFromImage(imageID);
ensureActiveSegmentGroupForImage(imageID);
this.$paint.setBrushSize(this.brushSize);

isActive.value = true;
Expand Down Expand Up @@ -189,28 +202,24 @@ export const usePaintToolStore = defineStore('paint', () => {
if (paint.activeSegmentGroupID !== null) {
activeSegmentGroupID.value =
segmentGroupIDMap[paint.activeSegmentGroupID];
setActiveLabelmap(activeSegmentGroupID.value);
setActiveSegmentGroup(activeSegmentGroupID.value);
setActiveSegment.call(this, paint.activeSegment);
}
}

// --- change labelmap if paint is active --- //
// Create segment group if paint is active and none exist.
// If paint is not active, but there is a segment group for the current image, set it as active.
watchEffect(() => {
const imageID = currentImageID.value;
if (!imageID) return;

watch(
currentImageID,
(imageID) => {
if (isActive.value) {
setActiveLabelmapFromImage(imageID);
if (isActive.value) {
ensureActiveSegmentGroupForImage(imageID);
} else {
const segmentGroupID = getFirstSegmentGroupID(imageID);
if (segmentGroupID) {
setActiveSegmentGroup(segmentGroupID);
}
},
{ immediate: true }
);

watchEffect(() => {
if (!currentImageID.value) return;
const segmentGroups = segmentGroupStore.orderByParent[currentImageID.value];
if (segmentGroups?.length === 1) {
activeSegmentGroupID.value = segmentGroups[0];
}
});

Expand All @@ -229,8 +238,8 @@ export const usePaintToolStore = defineStore('paint', () => {
deactivateTool,

setMode,
setActiveLabelmap,
setActiveLabelmapFromImage,
setActiveSegmentGroup,
ensureActiveSegmentGroupForImage,
setActiveSegment,
setBrushSize,
setSliceAxis,
Expand Down

0 comments on commit 1bc37fe

Please sign in to comment.