From 24e53e242a67571e03d423876b8648b8a43b58db Mon Sep 17 00:00:00 2001 From: kovacsv Date: Sat, 23 Oct 2021 11:16:32 +0200 Subject: [PATCH] Generate unique id for nodes. --- source/model/node.js | 26 ++++++++++++++++++++++++++ source/threejs/threeconverter.js | 15 +++++++++------ test/tests/node_test.js | 23 ++++++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/source/model/node.js b/source/model/node.js index 6f68c92..dcfba7e 100644 --- a/source/model/node.js +++ b/source/model/node.js @@ -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; } diff --git a/source/threejs/threeconverter.js b/source/threejs/threeconverter.js index 7f81600..4ede6db 100644 --- a/source/threejs/threeconverter.js +++ b/source/threejs/threeconverter.js @@ -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 (); }, diff --git a/test/tests/node_test.js b/test/tests/node_test.js index accefed..02e7780 100644 --- a/test/tests/node_test.js +++ b/test/tests/node_test.js @@ -109,5 +109,26 @@ 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); + }); });