Skip to content

Commit 786f883

Browse files
committed
fix incorrect source/target access and properly update nd coords in edge if position changes
1 parent a16210b commit 786f883

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

frontend/javascripts/viewer/geometries/skeleton.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,14 @@ const EdgeBufferHelperType = {
111111
class Skeleton {
112112
rootGroup: Group;
113113
pickingNode: Object3D;
114-
// @ts-expect-error ts-migrate(2564) FIXME: Property 'prevTracing' has no initializer and is n... Remove this comment to see the full error message
115-
prevTracing: SkeletonTracing;
116-
// @ts-expect-error ts-migrate(2564) FIXME: Property 'nodes' has no initializer and is not def... Remove this comment to see the full error message
117-
nodes: BufferCollection;
118-
// @ts-expect-error ts-migrate(2564) FIXME: Property 'edges' has no initializer and is not def... Remove this comment to see the full error message
119-
edges: BufferCollection;
120-
// @ts-expect-error ts-migrate(2564) FIXME: Property 'treeColorTexture' has no initializer and... Remove this comment to see the full error message
121-
treeColorTexture: DataTexture;
122114
supportsPicking: boolean;
123115
stopStoreListening: () => void;
116+
// The following properties are set in reset() which is called from the constructor.
117+
// Therefore, we use ! tell TS it's safe to assume non-null.
118+
prevTracing!: SkeletonTracing;
119+
nodes!: BufferCollection;
120+
edges!: BufferCollection;
121+
treeColorTexture!: DataTexture;
124122

125123
nodeShader: NodeShader | undefined;
126124
edgeShader: EdgeShader | undefined;
@@ -642,6 +640,7 @@ class Skeleton {
642640
this.update(bufferNodeId, this.nodes, ({ buffer, index }) => {
643641
const attribute = buffer.geometry.attributes.position;
644642
attribute.set(position, index * 3);
643+
const updatedAttributes = [attribute];
645644

646645
if (flycamAdditionalCoordinateNames.size > 0) {
647646
const nodeCoords = additionalCoordinates ?? [];
@@ -651,21 +650,44 @@ class Skeleton {
651650
const attribute = buffer.geometry.attributes[`additionalCoord_${name}`];
652651
const value = nodeCoordMap.get(name) ?? NaN;
653652
attribute.set([value], index);
653+
updatedAttributes.push(attribute);
654654
}
655655
}
656656

657-
return [attribute];
657+
return updatedAttributes;
658658
});
659659

660660
const edgePositionUpdater = (edge: Edge, isIngoingEdge: boolean) => {
661-
// The changed node is the target node of the edge which is saved
662-
// after the source node in the buffer. Thus we need an offset.
661+
// If isIngoingEdge is true, the changed node is the target node of the
662+
// (source, target) edge. Thus, we need an offset.
663663
const indexOffset = isIngoingEdge ? 3 : 0;
664664
const bufferEdgeId = this.combineIds(treeId, edge.source, edge.target);
665665
this.update(bufferEdgeId, this.edges, ({ buffer, index }) => {
666-
const positionAttribute = buffer.geometry.attributes.position;
666+
const { attributes } = buffer.geometry;
667+
const positionAttribute = attributes.position;
667668
positionAttribute.set(position, index * 6 + indexOffset);
668-
return [positionAttribute];
669+
const updatedAttributes = [positionAttribute];
670+
671+
if (flycamAdditionalCoordinateNames.size > 0) {
672+
const source = tree.nodes.getOrThrow(edge.source);
673+
const target = tree.nodes.getOrThrow(edge.target);
674+
const sourceCoordMap = new Map(
675+
(source.additionalCoordinates ?? []).map((c) => [c.name, c.value]),
676+
);
677+
const targetCoordMap = new Map(
678+
(target.additionalCoordinates ?? []).map((c) => [c.name, c.value]),
679+
);
680+
681+
for (const name of flycamAdditionalCoordinateNames) {
682+
const additionalCoordAttribute = attributes[`additionalCoord_${name}`];
683+
684+
additionalCoordAttribute.set([sourceCoordMap.get(name) ?? NaN], 2 * index);
685+
additionalCoordAttribute.set([targetCoordMap.get(name) ?? NaN], 2 * index + 1);
686+
updatedAttributes.push(additionalCoordAttribute);
687+
}
688+
}
689+
690+
return updatedAttributes;
669691
});
670692
};
671693

@@ -717,6 +739,8 @@ class Skeleton {
717739
const positionAttribute = attributes.position;
718740
const treeIdAttribute = attributes.treeId;
719741

742+
// Each position needs 3 items (x, y, z). Per edge, there are two
743+
// positions, which explains the ... * 6 + 3 calculation.
720744
positionAttribute.set(source.untransformedPosition, index * 6);
721745
positionAttribute.set(target.untransformedPosition, index * 6 + 3);
722746
treeIdAttribute.set([treeId, treeId], index * 2);
@@ -727,7 +751,7 @@ class Skeleton {
727751
(source.additionalCoordinates ?? []).map((c) => [c.name, c.value]),
728752
);
729753
const targetCoordMap = new Map(
730-
(source.additionalCoordinates ?? []).map((c) => [c.name, c.value]),
754+
(target.additionalCoordinates ?? []).map((c) => [c.name, c.value]),
731755
);
732756

733757
for (const name of flycamAdditionalCoordinateNames) {

0 commit comments

Comments
 (0)