Skip to content

Commit 8767f43

Browse files
committed
fix(vtkviewnode): fix addMissingNodes with enforceOrder
enforceOrder was forcing the dataObjs nodes to be the first children. That had the disadvantage to make the OpenGLCamera the last item, and slow to be found. Instead, enforceOrder now simply enforce that the created missing nodes are following each others. fix #3497
1 parent 0c3f7cb commit 8767f43

File tree

1 file changed

+28
-11
lines changed
  • Sources/Rendering/SceneGraph/ViewNode

1 file changed

+28
-11
lines changed

Sources/Rendering/SceneGraph/ViewNode/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ function vtkViewNode(publicAPI, model) {
1212
// Set our className
1313
model.classHierarchy.push('vtkViewNode');
1414

15+
/**
16+
* Convenient method to move a child to a specific index.
17+
* @param {*} child the child to move
18+
* @param {*} toIndex the index to move the child to
19+
* @returns true if the child was moved, false otherwise
20+
*/
21+
function moveChild(child, toIndex) {
22+
for (let i = 0; i < model.children.length; ++i) {
23+
// Start browsing from toIndex + 1
24+
const childIndex = (toIndex + 1 + i) % model.children.length;
25+
if (model.children[childIndex] === child) {
26+
model.children[childIndex] = model.children[toIndex];
27+
model.children[toIndex] = child;
28+
return true;
29+
}
30+
}
31+
return false;
32+
}
33+
1534
// Builds myself.
1635
publicAPI.build = (prepass) => {};
1736

@@ -122,21 +141,19 @@ function vtkViewNode(publicAPI, model) {
122141
return;
123142
}
124143

144+
let nextIndex;
125145
for (let index = 0; index < dataObjs.length; ++index) {
126146
const dobj = dataObjs[index];
127147
const node = publicAPI.addMissingNode(dobj);
128-
if (
129-
enforceOrder &&
130-
node !== undefined &&
131-
model.children[index] !== node
132-
) {
133-
for (let i = index + 1; i < model.children.length; ++i) {
134-
if (model.children[i] === node) {
135-
model.children.splice(i, 1);
136-
model.children.splice(index, 0, node);
137-
break;
138-
}
148+
if (enforceOrder && node !== undefined) {
149+
if (nextIndex === undefined) {
150+
// First node can be anywhere
151+
nextIndex = model.children.lastIndexOf(node); // node is likely the list child
152+
} else if (model.children[nextIndex] !== node) {
153+
moveChild(model.children, node, nextIndex);
139154
}
155+
// Next node must follow current node
156+
nextIndex++;
140157
}
141158
}
142159
};

0 commit comments

Comments
 (0)