Generate unique id for nodes.

This commit is contained in:
kovacsv 2021-10-23 11:16:32 +02:00
parent d3c5a1436b
commit 24e53e242a
3 changed files with 57 additions and 7 deletions

View File

@ -1,3 +1,18 @@
OV.NodeIdGenerator = class
{
constructor ()
{
this.nextId = 0;
}
GenerateId ()
{
const id = this.nextId;
this.nextId += 1;
return id;
}
};
OV.Node = class
{
constructor ()
@ -5,8 +20,12 @@ OV.Node = class
this.name = '';
this.parent = null;
this.transformation = new OV.Transformation ();
this.childNodes = [];
this.meshIndices = [];
this.idGenerator = new OV.NodeIdGenerator ();
this.id = this.idGenerator.GenerateId ();
}
IsEmpty ()
@ -14,6 +33,11 @@ OV.Node = class
return this.childNodes.length === 0 && this.meshIndices.length === 0;
}
GetId ()
{
return this.id;
}
GetName ()
{
return this.name;
@ -47,6 +71,8 @@ OV.Node = class
AddChildNode (node)
{
node.parent = this;
node.idGenerator = this.idGenerator;
node.id = node.idGenerator.GenerateId ();
this.childNodes.push (node);
return this.childNodes.length - 1;
}

View File

@ -184,7 +184,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
return threeMaterial;
}
function CreateThreeMesh (model, meshIndex, modelThreeMaterials)
function CreateThreeMesh (model, nodeId, meshIndex, modelThreeMaterials)
{
let mesh = model.GetMesh (meshIndex);
let triangleCount = mesh.TriangleCount ();
@ -272,6 +272,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
let threeMesh = new THREE.Mesh (threeGeometry, meshThreeMaterials);
threeMesh.userData = {
originalNodeId : nodeId,
originalMeshIndex : meshIndex,
originalMaterials : meshOriginalMaterials,
threeMaterials : null
@ -280,11 +281,11 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
return threeMesh;
}
function ConvertMesh (threeObject, model, meshIndex, modelThreeMaterials)
function ConvertMesh (threeObject, model, nodeId, meshIndex, modelThreeMaterials)
{
let mesh = model.GetMesh (meshIndex);
if (mesh.TriangleCount () > 0) {
let threeMesh = CreateThreeMesh (model, meshIndex, modelThreeMaterials);
let threeMesh = CreateThreeMesh (model, nodeId, meshIndex, modelThreeMaterials);
threeObject.add (threeMesh);
}
}
@ -302,8 +303,9 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
OV.RunTasksBatch (node.MeshIndexCount (), 100, {
runTask : (firstNodeMeshIndex, lastNodeMeshIndex, ready) => {
for (let nodeMeshIndex = firstNodeMeshIndex; nodeMeshIndex <= lastNodeMeshIndex; nodeMeshIndex++) {
let meshIndex = node.GetMeshIndex (nodeMeshIndex);
ConvertMesh (threeNode, model, meshIndex, modelThreeMaterials);
const nodeId = node.GetId ();
const meshIndex = node.GetMeshIndex (nodeMeshIndex);
ConvertMesh (threeNode, model, nodeId, meshIndex, modelThreeMaterials);
}
ready ();
},
@ -323,7 +325,8 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
OV.RunTasksBatch (model.MeshCount (), 100, {
runTask : (firstIndex, lastIndex, ready) => {
for (let meshIndex = firstIndex; meshIndex <= lastIndex; meshIndex++) {
ConvertMesh (threeObject, model, meshIndex, modelThreeMaterials);
let nodeId = -1;
ConvertMesh (threeObject, model, nodeId, meshIndex, modelThreeMaterials);
}
ready ();
},

View File

@ -110,4 +110,25 @@ describe ('Node', function() {
assert.deepStrictEqual (enumerated, [1, 2, 3, 5, 6, 4]);
});
it ('Id Generator', function () {
let node1 = new OV.Node ();
let node2 = new OV.Node ();
let node3 = new OV.Node ();
let node4 = new OV.Node ();
assert.strictEqual (node1.GetId (), 0);
assert.strictEqual (node2.GetId (), 0);
assert.strictEqual (node3.GetId (), 0);
assert.strictEqual (node4.GetId (), 0);
node1.AddChildNode (node2);
assert.strictEqual (node2.GetId (), 1);
node2.AddChildNode (node3);
assert.strictEqual (node3.GetId (), 2);
node1.AddChildNode (node4);
assert.strictEqual (node4.GetId (), 3);
});
});