Calculate world transformation from node.

This commit is contained in:
kovacsv 2021-10-26 07:53:25 +02:00
parent 034c4079c9
commit 92eac095e1
4 changed files with 64 additions and 7 deletions

View File

@ -394,3 +394,15 @@ OV.Matrix = class
return new OV.Matrix (result); return new OV.Matrix (result);
} }
}; };
OV.MatrixIsEqual = function (a, b)
{
const aMatrix = a.Get ();
const bMatrix = b.Get ();
for (let i = 0; i < 16; i++) {
if (!OV.IsEqual (aMatrix[i], bMatrix[i])) {
return false;
}
}
return true;
};

View File

@ -51,3 +51,8 @@ OV.Transformation = class
return new OV.Transformation (clonedMatrix); return new OV.Transformation (clonedMatrix);
} }
}; };
OV.TransformationIsEqual = function (a, b)
{
return OV.MatrixIsEqual (a.GetMatrix (), b.GetMatrix ());
};

View File

@ -63,6 +63,18 @@ OV.Node = class
return this.transformation; return this.transformation;
} }
GetWorldTransformation ()
{
let transformation = this.transformation.Clone ();
let parent = this.parent;
while (parent !== null) {
const parentTransformation = parent.transformation.Clone ();
transformation = parentTransformation.Append (transformation);
parent = parent.parent;
}
return transformation;
}
SetTransformation (transformation) SetTransformation (transformation)
{ {
this.transformation = transformation; this.transformation = transformation;

View File

@ -111,6 +111,34 @@ describe ('Node', function() {
assert.deepStrictEqual (enumerated, [1, 2, 3, 5, 6, 4]); assert.deepStrictEqual (enumerated, [1, 2, 3, 5, 6, 4]);
}); });
it ('World Transformation', function () {
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 0.0, 1.0), Math.PI / 2.0);
let tr1 = new OV.Transformation (new OV.Matrix ().CreateTranslation (2.0, 0.0, 0.0));
let tr2 = new OV.Transformation (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w));
let tr3 = new OV.Transformation (new OV.Matrix ().CreateTranslation (0.0, 0.0, 2.0));
let refTr = new OV.Transformation ().Append (tr1).Append (tr2).Append (tr3);
let node1 = new OV.Node ();
node1.SetTransformation (tr1);
let node2 = new OV.Node ();
node2.SetTransformation (tr2);
let node3 = new OV.Node ();
node3.SetTransformation (tr3);
node1.AddChildNode (node2);
node2.AddChildNode (node3);
let nodeTr = node3.GetWorldTransformation ();
assert (OV.TransformationIsEqual (node1.GetTransformation (), tr1));
assert (OV.TransformationIsEqual (node2.GetTransformation (), tr2));
assert (OV.TransformationIsEqual (node3.GetTransformation (), tr3));
assert (OV.TransformationIsEqual (nodeTr, refTr));
});
it ('Id Generator', function () { it ('Id Generator', function () {
let node1 = new OV.Node (); let node1 = new OV.Node ();
let node2 = new OV.Node (); let node2 = new OV.Node ();