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

Expose setActiveTree and setTreeColorIndex in API #2997

Merged
merged 6 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.md).
- Added placeholders and functionality hints to (nearly) empty lists and tables in the admin views. [#2969](https://github.com/scalableminds/webknossos/pull/2969)
- Added the possibility to import multiple NML files into the active tracing. This can be done by dragging and dropping the files directly into the tracing view. [#2908](https://github.com/scalableminds/webknossos/pull/2908)
- During the import of multiple NML files, the user can select an option to automatically create a group per file so that the imported trees are organized in a hierarchy. [#2908](https://github.com/scalableminds/webknossos/pull/2908)
- Added functions to the front-end API to activate a tree and to change the color of a tree. [#2997](https://github.com/scalableminds/webknossos/pull/2997)

### Changed

Expand Down
27 changes: 27 additions & 0 deletions app/assets/javascripts/oxalis/api/api_latest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
deleteTreeAction,
setNodeRadiusAction,
setTreeNameAction,
setActiveTreeAction,
setTreeColorIndexAction,
} from "oxalis/model/actions/skeletontracing_actions";
import {
findTreeByNodeId,
Expand Down Expand Up @@ -243,6 +245,31 @@ class TracingApi {
});
}

/**
* Makes the specified tree active. Within the tree, the node with the highest ID will be activated.
*
* @example
* api.tracing.setActiveTree(3);
*/
setActiveTree(treeId: number) {
const tracing = Store.getState().tracing;
assertSkeleton(tracing);
Store.dispatch(setActiveTreeAction(treeId));
}

/**
* Changes the color of the referenced tree. Internally, a pre-defined array of colors is used which is
* why this function uses a colorIndex (between 0 and 500) instead of a proper color.
*
* @example
* api.tracing.setTreeColorIndex(3, 10);
*/
setTreeColorIndex(treeId?: number, colorIndex: number) {
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to have the first parameter as optional? Calling the function with only one parameter will produce an incorrect result.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's right. I can add it to treeId: ?number to avoid confusion (even with treeId? one would have to call setTreeColorIndex(undefined, 3) to satisfy the interface).

Copy link
Member

Choose a reason for hiding this comment

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

You could also switch the order of parameters

const tracing = Store.getState().tracing;
assertSkeleton(tracing);
Store.dispatch(setTreeColorIndexAction(treeId, colorIndex));
}

/**
* Returns the name for a given tree id. If none is given, the name of the active tree is returned.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ type SetActiveTreeActionType = { type: "SET_ACTIVE_TREE", treeId: number };
type MergeTreesActionType = { type: "MERGE_TREES", sourceNodeId: number, targetNodeId: number };
type SetTreeNameActionType = { type: "SET_TREE_NAME", name: ?string, treeId: ?number };
type SelectNextTreeActionType = { type: "SELECT_NEXT_TREE", forward: ?boolean };
type SetTreeColorIndexActionType = {
type: "SET_TREE_COLOR_INDEX",
treeId?: number,
colorIndex: number,
};
type ShuffleTreeColorActionType = { type: "SHUFFLE_TREE_COLOR", treeId?: number };
type ShuffleAllTreeColorsActionType = { type: "SHUFFLE_ALL_TREE_COLORS", treeId?: number };
type CreateCommentActionType = {
Expand Down Expand Up @@ -106,6 +111,7 @@ export type SkeletonTracingActionType =
| SelectNextTreeActionType
| ShuffleTreeColorActionType
| ShuffleAllTreeColorsActionType
| SetTreeColorIndexActionType
| CreateCommentActionType
| DeleteCommentActionType
| ToggleTreeActionType
Expand Down Expand Up @@ -310,6 +316,15 @@ export const selectNextTreeAction = (forward: ?boolean = true): SelectNextTreeAc
forward,
});

export const setTreeColorIndexAction = (
treeId?: number,
colorIndex: number,
): SetTreeColorIndexActionType => ({
type: "SET_TREE_COLOR_INDEX",
treeId,
colorIndex,
});

export const shuffleTreeColorAction = (treeId: number): ShuffleTreeColorActionType => ({
type: "SHUFFLE_TREE_COLOR",
treeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
deleteNode,
deleteEdge,
shuffleTreeColor,
setTreeColorIndex,
createComment,
deleteComment,
mergeTrees,
Expand Down Expand Up @@ -376,6 +377,16 @@ function SkeletonTracingReducer(state: OxalisState, action: ActionType): OxalisS
});
}

case "SET_TREE_COLOR_INDEX": {
const { colorIndex } = action;
return getTree(skeletonTracing, action.treeId)
.chain(tree => setTreeColorIndex(skeletonTracing, tree, colorIndex))
.map(([tree, treeId]) =>
update(state, { tracing: { trees: { [treeId]: { $set: tree } } } }),
)
.getOrElse(state);
}

case "SHUFFLE_TREE_COLOR": {
return getTree(skeletonTracing, action.treeId)
.chain(tree => shuffleTreeColor(skeletonTracing, tree))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,15 @@ export function shuffleTreeColor(
tree: TreeType,
): Maybe<[TreeType, number]> {
const randomId = _.random(0, 10000, false);
// ColorGenerator fails to produce distinct color for huge ids (Infinity)
const newTree = update(tree, { color: { $set: ColorGenerator.distinctColorForId(randomId) } });
return setTreeColorIndex(skeletonTracing, tree, randomId);
}

export function setTreeColorIndex(
skeletonTracing: SkeletonTracingType,
tree: TreeType,
colorIndex: number,
): Maybe<[TreeType, number]> {
const newTree = update(tree, { color: { $set: ColorGenerator.distinctColorForId(colorIndex) } });
return Maybe.Just([newTree, tree.treeId]);
}

Expand Down