Skip to content

Commit

Permalink
Merge pull request #42 from AnalyticalGraphicsInc/convertDagToTree
Browse files Browse the repository at this point in the history
Added convertDagToTree stage
  • Loading branch information
pjcozzi committed Apr 14, 2016
2 parents 4237961 + 68c811a commit 56ea6d3
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 1,921 deletions.
4 changes: 4 additions & 0 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var loadGltfUris = require('../').loadGltfUris;
var writeGltf = require('../').writeGltf;
var parseBinaryGltf = require('../').parseBinaryGltf;
var addPipelineExtras = require('../').addPipelineExtras;
var convertDagToTree = require('../').convertDagToTree;
var OptimizationStatistics = require('../').OptimizationStatistics;
var Cesium = require('cesium');
var defined = Cesium.defined;
Expand Down Expand Up @@ -65,6 +66,9 @@ fs.readFile(gltfPath, function (err, data) {
removeUnused(gltf, stats);
printStats(stats);

addPipelineExtras(gltf);
convertDagToTree(gltf);

gltf = loadGltfUris(gltf, filePath, function(err) {
if (err) {
throw err;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ module.exports = {
writeGltf : require('./lib/writeGltf'),
parseBinaryGltf : require('./lib/parseBinaryGltf'),
writeBinaryGltf : require('./lib/writeBinaryGltf'),
addPipelineExtras : require('./lib/addPipelineExtras'),
convertDagToTree : require('./lib/convertDagToTree'),
OptimizationStatistics : require('./lib/OptimizationStatistics')
};
115 changes: 115 additions & 0 deletions lib/convertDagToTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
'use strict';
var Cesium = require('cesium');
var clone = Cesium.clone;
var defined = Cesium.defined;

module.exports = convertDagToTree;

function convertDagToTree(gltf) {
var scenes = gltf.scenes;
var nodes = gltf.nodes;

if (defined(scenes) && defined(nodes)) {
for (var sceneId in scenes) {
if (scenes.hasOwnProperty(sceneId)) {
var roots = scenes[sceneId].nodes;
if (defined(roots)) {
//For each scene, reinitialize all nodes to unvisited
for (var nodeId in nodes) {
if (nodes.hasOwnProperty(nodeId)) {
nodes[nodeId].extras._pipeline.visited = false;
nodes[nodeId].extras._pipeline.copy = 0;
}
}

//Perform a breadth-first search from each root, searching for previously visited nodes
var nodeStack = [];
var rootsLength = roots.length;
for (var i = 0; i < rootsLength; i++) {
var root = roots[i];
nodeStack.push(root);
nodes[root].extras._pipeline.visited = true;
while (nodeStack.length > 0) {
var currentNode = nodes[nodeStack.shift()];
var children = currentNode.children;
if (defined(children)) {
for (var j = 0; j < children.length; j++) {
var childId = children[j];
var child = nodes[childId];
//If child has already been visited, duplicate and search the subgraph
if (child.extras._pipeline.visited) {
nodeStack.push(duplicateSubgraph(nodes, currentNode, j, childId));
}
else {
child.extras._pipeline.visited = true;
nodeStack.push(childId);
}
}
}
}
}
}
}
}
}

return gltf;
}

//Duplicates and returns the id of the new subgraph root
function duplicateSubgraph(nodes, parent, rootIndex, root) {
var newRootId = duplicateNode(nodes, root);

//Keep track of the subgraph nodes that have already been duplicated
var duplicatedNodes = {};
duplicatedNodes[root] = newRootId;

var nodeStack = [];
nodeStack.push(newRootId);
while (nodeStack.length > 0) {
var currentNode = nodes[nodeStack.shift()];
var children = currentNode.children;
if (defined(children)) {
for (var j = 0; j < children.length; j++) {
//Duplicate the child if it has not been encountered yet
var childId = children[j];
if (Object.keys(duplicatedNodes).indexOf(childId) == -1) {
duplicatedNodes[childId] = duplicateNode(nodes, childId);
}

//Update the list of children with the new child and push it on the node stack
children.splice(j, 1, duplicatedNodes[childId]);
nodeStack.push(duplicatedNodes[childId]);
}
}
}

//Update the parent's children with the new subgraph root id
parent.children.splice(rootIndex, 1, newRootId);
return newRootId;
}

//Duplicate node and return the id of the new node
function duplicateNode(nodes, nodeId) {
var node = nodes[nodeId];
var copyId = node.extras._pipeline.copy + 1;
var newId = nodeId;

//Appends the copy suffix if duplicating the original node
if (copyId == 1) {
newId += '_1';
}

//Increments the copy suffix until the new node has a unique id
var nodeKeys = Object.keys(nodes);
while (nodeKeys.indexOf(newId) != -1) {
copyId++;
newId = newId.slice(0, -1) + copyId;
}

var newNode = clone(node, true);
nodes[newId] = newNode;
newNode.extras._pipeline.visited = false;
newNode.extras._pipeline.copy = copyId;
return newId;
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"gulp-jshint": "2.0.0",
"istanbul": "^0.4.1",
"jasmine": "^2.4.1",
"jasmine-node": "^1.14.5",
"jshint": "2.8.0",
"jshint-stylish": "2.1.0"
}
Expand Down
1 change: 0 additions & 1 deletion specs/data/boxAnimated/README.txt

This file was deleted.

Loading

0 comments on commit 56ea6d3

Please sign in to comment.