From cd2adbeab3891c2bcccace54a9dfdee812eedbd3 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Fri, 22 Oct 2021 15:55:38 +0200 Subject: [PATCH] Store transformation in node. --- source/import/importero3dv.js | 45 ++++++++++++++++++++++++++++++ source/model/node.js | 21 ++++++++++---- test/testfiles/o3dv/hierarchy.o3dv | 2 ++ test/tests/node_test.js | 8 ++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/source/import/importero3dv.js b/source/import/importero3dv.js index 17aab26..ac21685 100644 --- a/source/import/importero3dv.js +++ b/source/import/importero3dv.js @@ -41,6 +41,9 @@ OV.ImporterO3dv = class extends OV.ImporterBase this.ImportMesh (meshContent); } } + if (content.root !== undefined) { + this.ImportNode (content.root, this.model.GetRootNode ()); + } onFinish (); } @@ -68,6 +71,7 @@ OV.ImporterO3dv = class extends OV.ImporterBase if (meshContent.material !== undefined) { genParams.SetMaterial (meshContent.material); } + // TODO: remove transformation from mesh, and from generator if (meshContent.transformation !== undefined) { let translation = new OV.Coord3D (0.0, 0.0, 0.0); let rotation = new OV.Quaternion (0.0, 0.0, 0.0, 1.0); @@ -120,4 +124,45 @@ OV.ImporterO3dv = class extends OV.ImporterBase this.model.AddMesh (mesh); } } + + ImportNode (node, meshNode) + { + if (node.name !== undefined) { + meshNode.SetName (node.name); + } + if (node.transformation !== undefined) { + const nodeTransformation = this.GetTransformation (node.transformation); + meshNode.SetTransformation (nodeTransformation); + } + if (node.meshes !== undefined) { + for (const meshIndex of node.meshes) { + meshNode.AddMeshIndex (meshIndex); + } + } + if (node.children !== undefined) { + for (const child of node.children) { + let childMeshNode = new OV.Node (); + meshNode.AddChildNode (childMeshNode); + this.ImportNode (child, childMeshNode); + } + } + } + + GetTransformation (contentTransformation) + { + let translation = new OV.Coord3D (0.0, 0.0, 0.0); + let rotation = new OV.Quaternion (0.0, 0.0, 0.0, 1.0); + let scale = new OV.Coord3D (1.0, 1.0, 1.0); + if (contentTransformation.translation !== undefined) { + translation = OV.ArrayToCoord3D (contentTransformation.translation); + } + if (contentTransformation.rotation !== undefined) { + rotation = OV.ArrayToQuaternion (contentTransformation.rotation); + } + if (contentTransformation.scale !== undefined) { + scale = OV.ArrayToCoord3D (contentTransformation.scale); + } + const matrix = new OV.Matrix ().ComposeTRS (translation, rotation, scale); + return new OV.Transformation (matrix); + } }; diff --git a/source/model/node.js b/source/model/node.js index c08e04d..ad029a8 100644 --- a/source/model/node.js +++ b/source/model/node.js @@ -4,10 +4,21 @@ OV.Node = class { this.name = ''; this.parent = null; + this.transformation = new OV.Transformation (); this.childNodes = []; this.meshIndices = []; } + GetName () + { + return this.name; + } + + SetName (name) + { + this.name = name; + } + HasParent () { return this.parent !== null; @@ -18,14 +29,14 @@ OV.Node = class return this.parent; } - GetName () + GetTransformation () { - return this.name; + return this.transformation; } - - SetName (name) + + SetTransformation (transformation) { - this.name = name; + this.transformation = transformation; } AddChildNode (node) diff --git a/test/testfiles/o3dv/hierarchy.o3dv b/test/testfiles/o3dv/hierarchy.o3dv index b502c41..8174992 100644 --- a/test/testfiles/o3dv/hierarchy.o3dv +++ b/test/testfiles/o3dv/hierarchy.o3dv @@ -100,6 +100,7 @@ }, "meshes" : [ { + "name" : "Cube", "type" : "cuboid", "parameters" : { "size_x" : 1.0, @@ -108,6 +109,7 @@ } }, { + "name" : "Sphere", "type" : "sphere", "parameters" : { "radius" : 0.5 diff --git a/test/tests/node_test.js b/test/tests/node_test.js index 9064642..2bb02a0 100644 --- a/test/tests/node_test.js +++ b/test/tests/node_test.js @@ -14,6 +14,14 @@ describe ('Node', function() { assert.strictEqual (node.GetName (), 'New Name'); }); + it ('Set Transformation', function () { + let node = new OV.Node (); + assert (node.GetTransformation ().IsIdentity ()); + let tr = new OV.Transformation (new OV.Matrix ().CreateScale (3.0, 4.0, 5.0)); + node.SetTransformation (tr); + assert (!node.GetTransformation ().IsIdentity ()); + }); + it ('Add Mesh Indices', function () { let node = new OV.Node (); node.AddMeshIndex (0);