Skip to content

Commit

Permalink
CELE-32 feat: Connect split with new enhanced model
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Aug 1, 2024
1 parent 5c5707f commit fd626a3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type React from "react";
import {useMemo} from "react";
import {Menu, MenuItem} from "@mui/material";
import {type NeuronGroup} from "../../../models";
import {isNeuronClass} from "../../../helpers/twoD/twoDHelpers.ts";
import {type NeuronGroup, ViewerType} from "../../../models";
import {calculateSplitPositions, isNeuronClass} from "../../../helpers/twoD/twoDHelpers.ts";
import {useSelectedWorkspace} from "../../../hooks/useSelectedWorkspace.ts";
import {Position} from "cytoscape";


interface ContextMenuProps {
open: boolean;
Expand Down Expand Up @@ -93,17 +95,29 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
const newJoin = new Set(prevState.join);

const newSelectedNeurons = new Set(workspace.selectedNeurons);
const positionUpdates: Record<string, { position?: Position | null, visibility: boolean }> = {};


workspace.selectedNeurons.forEach(neuronId => {
if (isNeuronClass(neuronId, workspace)) {
newSplit.add(neuronId);
newSelectedNeurons.delete(neuronId);

// Add the individual neurons to the selected neurons
Object.values(workspace.availableNeurons).forEach(neuron => {
if (neuron.nclass === neuronId && neuron.nclass !== neuron.name) {
newSelectedNeurons.add(neuron.name);
}
const individualNeurons = Object.values(workspace.availableNeurons).filter(neuron => {
return neuron.nclass === neuronId && neuron.nclass !== neuron.name;
}).map(neuron => neuron.name);

// Calculate the positions for the individual neurons
const basePosition = workspace.availableNeurons[neuronId].viewerData[ViewerType.Graph]?.position || {
x: 0,
y: 0
};
const positions = calculateSplitPositions(individualNeurons, basePosition);

// Update the selected neurons with individual neurons
individualNeurons.forEach(neuronName => {
newSelectedNeurons.add(neuronName);
positionUpdates[neuronName] = {position: positions[neuronName], visibility: true};
});

// Remove the corresponding class from the toJoin set
Expand All @@ -112,12 +126,24 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
newJoin.delete(joinNeuronId);
}
});

positionUpdates[neuronId] = {visibility: false};
}
});

// Update the selected neurons in the workspace
workspace.customUpdate(draft => {
// Update the selected neurons
draft.selectedNeurons = newSelectedNeurons;

// Update the positions and visibility for the individual neurons and class neuron
Object.entries(positionUpdates).forEach(([neuronName, update]) => {
if (draft.availableNeurons[neuronName]) {
if (update.position !== undefined) {
draft.availableNeurons[neuronName].viewerData[ViewerType.Graph].position = update.position;
}
draft.availableNeurons[neuronName].viewerData[ViewerType.Graph].visibility = update.visibility;
}
});
});

return {split: newSplit, join: newJoin};
Expand Down
46 changes: 46 additions & 0 deletions applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,52 @@ export const calculateMeanPosition = (nodeIds: string[], workspace: Workspace):
};


// This functions is a complete copy from the original nemanode code
export const calculateSplitPositions = (nodes, basePosition) => {
let { x: offsetX, y: offsetY } = basePosition;
let positions = {};
let n = nodes.length;

if (n == 1) {
positions[nodes[0]] = { x: offsetX, y: offsetY };
return positions;
} else if (n == 2) {
positions[nodes[0]] = { x: offsetX - 35, y: offsetY };
positions[nodes[1]] = { x: offsetX + 35, y: offsetY };
} else if (n == 3) {
positions[nodes[0]] = { x: offsetX, y: offsetY - 35 };
positions[nodes[1]] = { x: offsetX - 35, y: offsetY + 35 };
positions[nodes[2]] = { x: offsetX + 35, y: offsetY + 35 };
} else if (n == 4 && nodes[0] == 'RMED') {
positions[nodes[0]] = { x: offsetX, y: offsetY - 50 };
positions[nodes[1]] = { x: offsetX - 50, y: offsetY };
positions[nodes[2]] = { x: offsetX + 50, y: offsetY };
positions[nodes[3]] = { x: offsetX, y: offsetY + 50 };
} else if (n == 4) {
positions[nodes[0]] = { x: offsetX - 35, y: offsetY - 35 };
positions[nodes[1]] = { x: offsetX + 35, y: offsetY - 35 };
positions[nodes[2]] = { x: offsetX - 35, y: offsetY + 35 };
positions[nodes[3]] = { x: offsetX + 35, y: offsetY + 35 };
} else if (n == 6) {
positions[nodes[0]] = { x: offsetX - 35, y: offsetY - 60 };
positions[nodes[1]] = { x: offsetX + 35, y: offsetY - 60 };
positions[nodes[2]] = { x: offsetX - 70, y: offsetY };
positions[nodes[3]] = { x: offsetX + 70, y: offsetY };
positions[nodes[4]] = { x: offsetX - 35, y: offsetY + 60 };
positions[nodes[5]] = { x: offsetX + 35, y: offsetY + 60 };
} else {
let r = 70;
for (let i = 0; i < n; i++) {
let theta = (-i * 2 * Math.PI) / n;
positions[nodes[i]] = {
x: offsetX - r * Math.sin(theta),
y: offsetY - r * Math.cos(theta)
};
}
}
return positions;
};

export const updateWorkspaceNeurons2DViewerData = (workspace: Workspace, cy: Core) => {
// Update the workspace availableNeurons with the positions and visibility
workspace.customUpdate(draft => {
Expand Down

0 comments on commit fd626a3

Please sign in to comment.