Skip to content

Instantly share code, notes, and snippets.

@normanrz
Last active September 23, 2024 11:27
Show Gist options
  • Save normanrz/1660164d4e61333d046f8ac2f231389f to your computer and use it in GitHub Desktop.
Save normanrz/1660164d4e61333d046f8ac2f231389f to your computer and use it in GitHub Desktop.
WEBKNOSSOS: mito-segEM mesh rendering
// Adds a keyboard shortcut "S", which collects all segments as marked by a skeleton node of the active tree.
// This allows users to collect segments using the skeleton feature and then render all meshes of these segments.
// The meshes can also be conveniently downloaded.
webknossos.apiReady(3).then((api) => {
api.utils.registerKeyHandler("s", async () => {
const layerName = api.data.getVolumeTracingLayerName();
// Is there a precomputed mesh availble?
let meshFile = api.data.getActiveMeshFile();
if (meshFile == null) {
const availableMeshFileNames = api.data.getAvailableMeshFiles();
if (availableMeshFileNames.length > 0) {
api.data.setActiveMeshFile(availableMeshFileNames[0]);
meshFile = availableMeshFileNames[0];
}
}
// Resolve segment ids for nodes
const activeTree = api.tracing.getAllTrees()[api.tracing.getActiveTreeId()]
const nodes = activeTree.nodes;
const nodeSegmentIds = [];
for (const node of nodes.map(n => n)) {
const position = node.untransformedPosition;
const segmentId = await api.data.getDataValue(layerName, position);
if (segmentId !== 0) {
nodeSegmentIds.push({ position, segmentId });
}
}
// Is there a segment group already?
let groupId = nodeSegmentIds.find(({ segmentId }) => {
try {
return api.tracing.getSegment(segmentId, layerName);
} catch {
return null;
}
});
if (groupId == null) {
groupId = api.tracing.createSegmentGroup(activeTree.name, -1, layerName);
}
// Register all segments with this segment group and render meshes
for (const { position, segmentId } of nodeSegmentIds) {
api.tracing.registerSegment(segmentId, position, null, layerName);
api.tracing.updateSegment(segmentId, { groupId }, layerName);
if (meshFile == null) {
api.data.computeMeshOnDemand(segmentId, position);
} else {
api.data.loadPrecomputedMesh(segmentId, position);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment