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

Fix opacity reset #7186

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions changelog.d/20231127_123423_klakhov_fix_opacity_reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- Resetting appearance opacity on each `change:frame`
klakhov marked this conversation as resolved.
Show resolved Hide resolved
(<https://github.com/opencv/cvat/pull/7186>)
44 changes: 17 additions & 27 deletions cvat-ui/src/reducers/settings-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AnnotationActionTypes } from 'actions/annotation-actions';
import {
SettingsState, GridColor, FrameSpeed, ColorBy,
} from 'reducers';
import { ObjectState, ShapeType, DimensionType } from 'cvat-core-wrapper';
import { clampOpacity } from 'utils/clamp-opacity';

const defaultState: SettingsState = {
shapes: {
Expand Down Expand Up @@ -439,39 +439,29 @@ export default (state = defaultState, action: AnyAction): SettingsState => {
};
}
case AnnotationActionTypes.UPLOAD_JOB_ANNOTATIONS_SUCCESS:
case AnnotationActionTypes.CREATE_ANNOTATIONS_SUCCESS:
case AnnotationActionTypes.CHANGE_FRAME_SUCCESS: {
{
const MIN_OPACITY = 30;
const { shapes: { opacity } } = state;
if (opacity < MIN_OPACITY) {
return {
...state,
shapes: {
...state.shapes,
opacity: MIN_OPACITY,
selectedOpacity: MIN_OPACITY * 2,
},
};
}
}

return state;
case AnnotationActionTypes.CREATE_ANNOTATIONS_SUCCESS: {
const { states } = action.payload;
const { shapes } = state;
const [clampedOpacity, clampedSelectedOpacity] = clampOpacity(states, shapes);
return {
...state,
shapes: {
...state.shapes,
opacity: clampedOpacity,
selectedOpacity: clampedSelectedOpacity,
},
};
}
case BoundariesActionTypes.RESET_AFTER_ERROR:
case AnnotationActionTypes.GET_JOB_SUCCESS: {
const { job, states } = action.payload;
const { shapes } = state;
const filters = [...state.imageFilters];
filters.forEach((imageFilter) => {
imageFilter.modifier.currentProcessedImage = null;
});

const withMasks = states
.some((_state: ObjectState): boolean => _state.shapeType === ShapeType.MASK);
const opacity = withMasks || job.dimension === DimensionType.DIMENSION_3D ?
Math.max(state.shapes.opacity, 30) : state.shapes.opacity;
const selectedOpacity = withMasks || job.dimension === DimensionType.DIMENSION_3D ?
Math.max(state.shapes.selectedOpacity, 60) : state.shapes.selectedOpacity;
const [clampedOpacity, clampedSelectedOpacity] = clampOpacity(states, shapes, job);

return {
...state,
Expand All @@ -480,8 +470,8 @@ export default (state = defaultState, action: AnyAction): SettingsState => {
},
shapes: {
...defaultState.shapes,
opacity,
selectedOpacity,
opacity: clampedOpacity,
selectedOpacity: clampedSelectedOpacity,
},
imageFilters: filters,
};
Expand Down
26 changes: 26 additions & 0 deletions cvat-ui/src/utils/clamp-opacity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2023 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import { Job } from 'cvat-core-wrapper';
import { ShapeType, DimensionType } from 'cvat-core/src/enums';
import ObjectState from 'cvat-core/src/object-state';
import { SettingsState } from 'reducers';

export function clampOpacity(
states: ObjectState[],
shapes: SettingsState['shapes'],
job?: Job,
): [number, number] {
const ENHANCED_DEFAULT_OPACITY = 30;
const ENHANCED_DEFAULT_SELECTED_OPACITY = 60;

const withMasks = states
.some((_state: ObjectState): boolean => _state.shapeType === ShapeType.MASK);
const opacity = withMasks || job?.dimension === DimensionType.DIMENSION_3D ?
Math.max(shapes.opacity, ENHANCED_DEFAULT_OPACITY) : shapes.opacity;
const selectedOpacity = withMasks || job?.dimension === DimensionType.DIMENSION_3D ?
Math.max(shapes.selectedOpacity, ENHANCED_DEFAULT_SELECTED_OPACITY) : shapes.selectedOpacity;
Copy link
Member

Choose a reason for hiding this comment

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

As an optimization, I would suggest first to check if current opacity/selected opacity already more or equal than enhanced level, just skip the algorithm. Also checking job?.dimension is quicker, so, it is a thing we should probably do first


return [opacity, selectedOpacity];
}
Loading