Skip to content
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

Allow selective segment visibility regardless of proofreading tool #8281

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released
### Added
- Added the total volume of a dataset to a tooltip in the dataset info tab. [#8229](https://github.com/scalableminds/webknossos/pull/8229)
- Optimized performance of data loading with “fill value“ chunks. [#8271](https://github.com/scalableminds/webknossos/pull/8271)
- Added the option for "Selective Segment Visibility" for segmentation layers. Select this option in the left sidebar to only show segments that are currently active or hovered. [#8281](https://github.com/scalableminds/webknossos/pull/8281)

### Changed
- Renamed "resolution" to "magnification" in more places within the codebase, including local variables. [#8168](https://github.com/scalableminds/webknossos/pull/8168)
Expand Down
1 change: 1 addition & 0 deletions docs/ui/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ In addition to the general layer properties mentioned above, `color` and `segmen

- `Color`: Every `color` layer can be re-colored to make it easily identifiable. By default, all layers have a white overlay, showing the true, raw black & white data. Clicking on the square color indicator brings up your system's color palette to choose from. Note, there is an icon button for inverting all color values in this layer.
- `Pattern Opacity`: Adjust the visibility of the texture/pattern on each segment. To make segments easier to distinguish and more unique, a pattern is applied to each in addition to its base color. 0% hides the pattern. 100% makes the pattern very prominent. Great for increasing the visual contrast between segments.
- `Selective Visibility`: When activated, only segments are shown that are currently active or hovered.
- `ID Mapping`: WEBKNOSSOS supports applying pre-computed agglomerations/groupings of segmentation IDs for a given segmentation layer. This is a very powerful feature to explore and compare different segmentation strategies for a given segmentation layer. Mappings need to be pre-computed and stored together with a dataset for WEBKNOSSOS to download and apply. [Read more about this here](../proofreading/segmentation_mappings.md).


Expand Down
28 changes: 27 additions & 1 deletion frontend/javascripts/libs/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class InputKeyboard {
}

// The mouse module.
// Events: over, out, leftClick, rightClick, leftDownMove
// Events: over, out, {left,right}Click, {left,right}DownMove, {left,right}DoubleClick
class InputMouseButton {
mouse: InputMouse;
name: MouseButtonString;
Expand Down Expand Up @@ -393,6 +393,24 @@ class InputMouseButton {
}
}

handleDoubleClick(event: MouseEvent, triggeredByTouch: boolean): void {
// event.which is 0 on touch devices as there are no mouse buttons, interpret that as the left mouse button
// Safari doesn't support evt.buttons, but only evt.which is non-standardized
const eventWhich = event.which !== 0 ? event.which : 1;

if (eventWhich === this.which) {
if (this.moveDelta <= MOUSE_MOVE_DELTA_THRESHOLD) {
this.mouse.emitter.emit(
`${this.name}DoubleClick`,
this.mouse.lastPosition,
this.id,
event,
triggeredByTouch,
);
}
}
}
Comment on lines +396 to +412
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add null check for lastPosition in handleDoubleClick.

The method emits an event using this.mouse.lastPosition without checking if it's null, which could lead to runtime errors since lastPosition is declared as Point2 | null | undefined.

Apply this diff to add the safety check:

  handleDoubleClick(event: MouseEvent, triggeredByTouch: boolean): void {
    const eventWhich = event.which !== 0 ? event.which : 1;

    if (eventWhich === this.which) {
      if (this.moveDelta <= MOUSE_MOVE_DELTA_THRESHOLD) {
+       if (this.mouse.lastPosition == null) return;
        this.mouse.emitter.emit(
          `${this.name}DoubleClick`,
          this.mouse.lastPosition,
          this.id,
          event,
          triggeredByTouch,
        );
      }
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
handleDoubleClick(event: MouseEvent, triggeredByTouch: boolean): void {
// event.which is 0 on touch devices as there are no mouse buttons, interpret that as the left mouse button
// Safari doesn't support evt.buttons, but only evt.which is non-standardized
const eventWhich = event.which !== 0 ? event.which : 1;
if (eventWhich === this.which) {
if (this.moveDelta <= MOUSE_MOVE_DELTA_THRESHOLD) {
this.mouse.emitter.emit(
`${this.name}DoubleClick`,
this.mouse.lastPosition,
this.id,
event,
triggeredByTouch,
);
}
}
}
handleDoubleClick(event: MouseEvent, triggeredByTouch: boolean): void {
// event.which is 0 on touch devices as there are no mouse buttons, interpret that as the left mouse button
// Safari doesn't support evt.buttons, but only evt.which is non-standardized
const eventWhich = event.which !== 0 ? event.which : 1;
if (eventWhich === this.which) {
if (this.moveDelta <= MOUSE_MOVE_DELTA_THRESHOLD) {
if (this.mouse.lastPosition == null) return;
this.mouse.emitter.emit(
`${this.name}DoubleClick`,
this.mouse.lastPosition,
this.id,
event,
triggeredByTouch,
);
}
}
}


handleMouseMove(event: MouseEvent, delta: Point2): void {
if (this.down) {
this.moveDelta += Math.abs(delta.x) + Math.abs(delta.y);
Expand Down Expand Up @@ -446,6 +464,7 @@ export class InputMouse {
document.addEventListener("mousemove", this.mouseMove);
document.addEventListener("mouseup", this.mouseUp);
document.addEventListener("touchend", this.touchEnd);
document.addEventListener("dblclick", this.doubleClick);

this.delegatedEvents = {
...Utils.addEventListenerWithDelegation(
Expand Down Expand Up @@ -498,6 +517,7 @@ export class InputMouse {
document.removeEventListener("mousemove", this.mouseMove);
document.removeEventListener("mouseup", this.mouseUp);
document.removeEventListener("touchend", this.touchEnd);
document.removeEventListener("dblclick", this.doubleClick);

for (const [eventName, eventHandler] of Object.entries(this.delegatedEvents)) {
document.removeEventListener(eventName, eventHandler);
Expand Down Expand Up @@ -551,6 +571,12 @@ export class InputMouse {
}
};

doubleClick = (event: MouseEvent): void => {
if (this.isHit(event)) {
this.leftMouseButton.handleDoubleClick(event, false);
Copy link
Member

@daniel-wer daniel-wer Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 343 it says {left,right}DoubleClick, but this looks like it only works for the left mouse button?

}
};

touchEnd = (): void => {
// The order of events during a click on a touch enabled device is:
// touch events -> mouse events -> click
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ export class MoveTool {
}
handleClickSegment(pos);
},
leftDoubleClick: (pos: Point2, _plane: OrthoView, _event: MouseEvent, _isTouch: boolean) => {
const { uiInformation } = Store.getState();
const isMoveToolActive = uiInformation.activeTool === AnnotationToolEnum.MOVE;

if (isMoveToolActive) {
// We want to select the clicked segment ID only in the MOVE tool. This method is
// implemented within the Move tool, but other tool controls will fall back to this one
// if they didn't define the double click hook. However, for most other tools, this behavior
// would be suboptimal, because when doing a double click, the first click will also be registered
// as a simple left click. For example, doing a double click with the brush tool would brush something
// and then immediately select the id again which is weird.
VolumeHandlers.handlePickCell(pos);
}
},
middleClick: (pos: Point2, _plane: OrthoView, event: MouseEvent) => {
if (event.shiftKey) {
handleAgglomerateSkeletonAtClick(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class PlaneMaterialFactory {
selectiveVisibilityInProofreading: {
value: true,
},
selectiveSegmentVisibility: {
value: false,
},
is3DViewBeingRendered: {
value: true,
},
Expand Down Expand Up @@ -562,6 +565,17 @@ class PlaneMaterialFactory {
true,
),
);

this.storePropertyUnsubscribers.push(
listenToStoreProperty(
(storeState) => storeState.datasetConfiguration.selectiveSegmentVisibility,
(selectiveSegmentVisibility) => {
this.uniforms.selectiveSegmentVisibility.value = selectiveSegmentVisibility;
},
true,
),
);

this.storePropertyUnsubscribers.push(
listenToStoreProperty(
(storeState) => getMagInfoByLayer(storeState.dataset),
Expand Down
29 changes: 10 additions & 19 deletions frontend/javascripts/oxalis/shaders/main_data_shaders.glsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
convertCellIdToRGB,
getBrushOverlay,
getCrossHairOverlay,
getSegmentationAlphaIncrement,
getSegmentId,
} from "./segmentation.glsl";
import { getMaybeFilteredColorOrFallback } from "./filtering.glsl";
Expand Down Expand Up @@ -110,6 +111,7 @@ uniform highp uint LOOKUP_CUCKOO_TWIDTH;

uniform float sphericalCapRadius;
uniform bool selectiveVisibilityInProofreading;
uniform bool selectiveSegmentVisibility;
uniform float viewMode;
uniform float alpha;
uniform bool renderBucketIndices;
Expand Down Expand Up @@ -178,6 +180,7 @@ ${compileShader(
hasSegmentation ? getBrushOverlay : null,
hasSegmentation ? getSegmentId : null,
hasSegmentation ? getCrossHairOverlay : null,
hasSegmentation ? getSegmentationAlphaIncrement : null,
almostEq,
)}

Expand Down Expand Up @@ -291,25 +294,13 @@ void main() {
&& hoveredUnmappedSegmentIdHigh == <%= segmentationName %>_unmapped_id_high;
bool isActiveCell = activeCellIdLow == <%= segmentationName %>_id_low
&& activeCellIdHigh == <%= segmentationName %>_id_high;
// Highlight cell only if it's hovered or active during proofreading
// and if segmentation opacity is not zero
float alphaIncrement = isProofreading
? (isActiveCell
? (isHoveredUnmappedSegment
? 0.4 // Highlight the hovered super-voxel of the active segment
: (isHoveredSegment
? 0.15 // Highlight the not-hovered super-voxels of the hovered segment
: 0.0
)
)
: (isHoveredSegment
? 0.2
// We are in proofreading mode, but the current voxel neither belongs
// to the active segment nor is it hovered. When selective visibility
// is enabled, lower the opacity.
: (selectiveVisibilityInProofreading ? -<%= segmentationName %>_alpha : 0.0)
)
) : (isHoveredSegment ? 0.2 : 0.0);
float alphaIncrement = getSegmentationAlphaIncrement(
<%= segmentationName %>_alpha,
isHoveredSegment,
isHoveredUnmappedSegment,
isActiveCell
);

gl_FragColor = vec4(mix(
data_color.rgb,
convertCellIdToRGB(<%= segmentationName %>_id_high, <%= segmentationName %>_id_low),
Expand Down
41 changes: 41 additions & 0 deletions frontend/javascripts/oxalis/shaders/segmentation.glsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,44 @@ export const getSegmentId: ShaderModule = {
<% }) %>
`,
};

export const getSegmentationAlphaIncrement: ShaderModule = {
requirements: [],
code: `
float getSegmentationAlphaIncrement(float alpha, bool isHoveredSegment, bool isHoveredUnmappedSegment, bool isActiveCell) {
// Highlight segment only if
// - it's hovered or
// - active during proofreading
// Also, make segments invisible if selective visibility is turned on (unless the segment
// is active or hovered).

if (isProofreading) {
if (isActiveCell) {
return (isHoveredUnmappedSegment
? 0.4 // Highlight the hovered super-voxel of the active segment
: (isHoveredSegment
? 0.15 // Highlight the not-hovered super-voxels of the hovered segment
: 0.0
)
);
} else {
return (isHoveredSegment
? 0.2
// We are in proofreading mode, but the current voxel neither belongs
// to the active segment nor is it hovered. When selective visibility
// is enabled, lower the opacity.
: (selectiveVisibilityInProofreading ? -alpha : 0.0)
);
}
}

if (isHoveredSegment) {
return 0.2;
} else if (selectiveSegmentVisibility) {
return isActiveCell ? 0.15 : -alpha;
} else {
return 0.;
}
}
`,
};
1 change: 1 addition & 0 deletions frontend/javascripts/oxalis/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export type DatasetConfiguration = {
readonly renderMissingDataBlack: boolean;
readonly loadingStrategy: LoadingStrategy;
readonly segmentationPatternOpacity: number;
readonly selectiveSegmentVisibility: boolean;
readonly blendMode: BLEND_MODES;
// If nativelyRenderedLayerName is not-null, the layer with
// that name (or id) should be rendered without any transforms.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import {
reloadHistogramAction,
} from "oxalis/model/actions/settings_actions";
import { userSettings } from "types/schemas/user_settings.schema";
import type { Vector3, ControlMode } from "oxalis/constants";
import type { Vector3 } from "oxalis/constants";
import Constants, { ControlModeEnum, MappingStatusEnum } from "oxalis/constants";
import EditableTextLabel from "oxalis/view/components/editable_text_label";
import LinkButton from "components/link_button";
Expand All @@ -97,9 +97,6 @@ import type {
DatasetLayerConfiguration,
OxalisState,
UserConfiguration,
HistogramDataForAllLayers,
Tracing,
Task,
} from "oxalis/store";
import Store from "oxalis/store";
import Toast from "libs/toast";
Expand Down Expand Up @@ -132,33 +129,8 @@ import {
} from "types/schemas/dataset_view_configuration.schema";
import defaultState from "oxalis/default_state";

type DatasetSettingsProps = {
userConfiguration: UserConfiguration;
datasetConfiguration: DatasetConfiguration;
dataset: APIDataset;
onChange: (propertyName: keyof DatasetConfiguration, value: any) => void;
onChangeLayer: (
layerName: string,
propertyName: keyof DatasetLayerConfiguration,
value: any,
) => void;
onClipHistogram: (layerName: string, shouldAdjustClipRange: boolean) => Promise<void>;
histogramData: HistogramDataForAllLayers;
onChangeRadius: (value: number) => void;
onChangeShowSkeletons: (arg0: boolean) => void;
onSetPosition: (arg0: Vector3) => void;
onZoomToMag: (layerName: string, arg0: Vector3) => number;
onChangeUser: (key: keyof UserConfiguration, value: any) => void;
reloadHistogram: (layerName: string) => void;
tracing: Tracing;
task: Task | null | undefined;
onEditAnnotationLayer: (tracingId: string, layerProperties: EditableLayerProperties) => void;
controlMode: ControlMode;
isArbitraryMode: boolean;
isAdminOrDatasetManager: boolean;
isAdminOrManager: boolean;
isSuperUser: boolean;
};
type DatasetSettingsProps = ReturnType<typeof mapStateToProps> &
ReturnType<typeof mapDispatchToProps>;

type State = {
// If this is set to not-null, the downsampling modal
Expand Down Expand Up @@ -927,9 +899,37 @@ class DatasetSettings extends React.PureComponent<DatasetSettingsProps, State> {
defaultValue={defaultDatasetViewConfigurationWithoutNull.segmentationPatternOpacity}
/>
);

const isProofreadingMode = this.props.activeTool === "PROOFREAD";
const isSelectiveVisibilityDisabled = isProofreadingMode;

const selectiveVisibilitySwitch = (
<FastTooltip
title={
isSelectiveVisibilityDisabled
? "This behavior is overriden by the 'selective segment visibility' button in the toolbar, because the proofreading tool is active."
: "When enabled, only hovered or active segments will be shown."
}
>
<div
style={{
marginBottom: 6,
}}
>
<SwitchSetting
onChange={_.partial(this.props.onChange, "selectiveSegmentVisibility")}
value={this.props.datasetConfiguration.selectiveSegmentVisibility}
label="Selective Visibility"
disabled={isSelectiveVisibilityDisabled}
/>
</div>
</FastTooltip>
);

return (
<div>
{segmentationOpacitySetting}
{selectiveVisibilitySwitch}
<MappingSettingsView layerName={layerName} />
</div>
);
Expand Down Expand Up @@ -1338,6 +1338,7 @@ class DatasetSettings extends React.PureComponent<DatasetSettingsProps, State> {
"loadingStrategy",
"segmentationPatternOpacity",
"blendMode",
"selectiveSegmentVisibility",
] as Array<keyof RecommendedConfiguration>
).map((key) => ({
name: settings[key] as string,
Expand Down Expand Up @@ -1585,6 +1586,7 @@ const mapStateToProps = (state: OxalisState) => ({
state.activeUser != null ? Utils.isUserAdminOrDatasetManager(state.activeUser) : false,
isAdminOrManager: state.activeUser != null ? Utils.isUserAdminOrManager(state.activeUser) : false,
isSuperUser: state.activeUser?.isSuperUser || false,
activeTool: state.uiInformation.activeTool,
});

const mapDispatchToProps = (dispatch: Dispatch<any>) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const defaultDatasetViewConfigurationWithoutNull: DatasetConfiguration =
blendMode: BLEND_MODES.Additive,
colorLayerOrder: [],
nativelyRenderedLayerName: null,
selectiveSegmentVisibility: false,
};
export const defaultDatasetViewConfiguration = {
...defaultDatasetViewConfigurationWithoutNull,
Expand Down