Create quaternion from axis and angle.

This commit is contained in:
kovacsv 2021-10-26 07:37:06 +02:00
parent 0a5dacd05f
commit 034c4079c9
4 changed files with 49 additions and 60 deletions

View File

@ -13,3 +13,16 @@ OV.ArrayToQuaternion = function (arr)
{
return new OV.Quaternion (arr[0], arr[1], arr[2], arr[3]);
};
OV.QuaternionFromAxisAngle = function (axis, angle)
{
const a = angle / 2.0;
const s = Math.sin (a);
return new OV.Quaternion (
axis.x * s,
axis.y * s,
axis.z * s,
Math.cos (a)
);
};

View File

@ -6,21 +6,6 @@ function SeededRandom (from, to, seed)
return random * (to - from) + from;
}
function CreateYRot90Quaternion ()
{
let angle = Math.PI / 2.0;
let rotX = 0.0;
let rotY = 1.0;
let rotZ = 0.0;
let quaternion = new OV.Quaternion (
Math.sin (angle / 2.0) * rotX,
Math.sin (angle / 2.0) * rotY,
Math.sin (angle / 2.0) * rotZ,
Math.cos (angle / 2.0)
);
return quaternion;
}
describe ('Comparison', function () {
it ('IsEqual', function () {
assert (OV.IsEqual (1.0, 1.0));
@ -32,7 +17,7 @@ describe ('Comparison', function () {
assert (OV.IsGreater (1.0, 0.0));
assert (OV.IsGreater (1.0001, 1.0));
assert (!OV.IsGreater (1.000000001, 1.0));
assert (OV.IsGreaterOrEqual (1.0001, 1.0));
assert (OV.IsGreaterOrEqual (1.000000001, 1.0));
assert (OV.IsGreaterOrEqual (0.999999999, 1.0));
@ -47,7 +32,7 @@ describe ('Comparison', function () {
assert (OV.IsLowerOrEqual (1.0, 1.0001));
assert (OV.IsLowerOrEqual (1.0, 1.000000001));
assert (OV.IsLowerOrEqual (1.0, 0.999999999));
assert (!OV.IsLowerOrEqual (1.0, 0.999));
assert (!OV.IsLowerOrEqual (1.0, 0.999));
});
});
@ -57,22 +42,22 @@ describe ('Coord', function () {
var c = new OV.Coord3D (2.0, 0.0, 0.0);
assert.strictEqual (c.Length (), 2.0);
});
it ('Multiply Scalar', function () {
var c = new OV.Coord3D (2.0, 0.0, 0.0);
c.MultiplyScalar (3.0);
assert.strictEqual (c.x, 6.0);
assert.strictEqual (c.y, 0.0);
assert.strictEqual (c.z, 0.0);
});
});
it ('Normalize', function () {
var c = new OV.Coord3D (2.0, 0.0, 0.0);
c.Normalize ();
assert.strictEqual (c.x, 1.0);
assert.strictEqual (c.y, 0.0);
assert.strictEqual (c.z, 0.0);
});
});
});
describe ('Triangle', function() {
@ -90,7 +75,7 @@ describe ('Triangle', function() {
describe ('Transformation', function() {
it ('Basic Test', function () {
let rotation = CreateYRot90Quaternion ();
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 1.0, 0.0), Math.PI / 2.0);
let coord = new OV.Coord3D (1.0, 2.0, 3.0);
@ -105,7 +90,7 @@ describe ('Transformation', function() {
tr.AppendMatrix (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w));
assert (!tr.IsIdentity ());
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), new OV.Coord3D (15.0, 8.0, -3.0)));
tr.AppendMatrix (new OV.Matrix ().CreateTranslation (4.0, 5.0, 6.0));
assert (!tr.IsIdentity ());
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), new OV.Coord3D (19.0, 13.0, 3.0)));
@ -129,16 +114,16 @@ describe ('Transformation', function() {
});
it ('TRS Compose Test', function () {
let rotation = CreateYRot90Quaternion ();
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 1.0, 0.0), Math.PI / 2.0);
let coord = new OV.Coord3D (1.0, 2.0, 3.0);
let tr = new OV.Transformation ();
tr.SetMatrix (new OV.Matrix ().ComposeTRS (new OV.Coord3D (4.0, 5.0, 6.0), rotation, new OV.Coord3D (3.0, 4.0, 5.0)));
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), new OV.Coord3D (19.0, 13.0, 3.0)));
});
});
it ('TRS Compose Test 2', function () {
let rotation = CreateYRot90Quaternion ();
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 1.0, 0.0), Math.PI / 2.0);
let coord = new OV.Coord3D (1.0, 2.0, 3.0);
let tr = new OV.Transformation ();
@ -158,7 +143,7 @@ describe ('Transformation', function() {
tr.SetMatrix (new OV.Matrix ().CreateRotation (0.0, 0.0, 0.0, 1.0));
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), coord));
});
it ('Clone Test', function () {
let tr = new OV.Transformation ();
let cloned = tr.Clone ();
@ -188,7 +173,7 @@ describe ('Tween', function() {
assert (OV.IsEqual (OV.ParabolicTweenFunction (10.0, 0, 10), 0.0));
assert (OV.IsEqual (OV.ParabolicTweenFunction (10.0, 5, 10), 5.0));
assert (OV.IsEqual (OV.ParabolicTweenFunction (10.0, 10, 10), 10.0));
});
});
it ('Linear Tween Coordinates', function () {
let beg = new OV.Coord3D (0.0, 0.0, 0.0);
@ -276,7 +261,7 @@ describe ('Octree', function() {
new OV.Coord3D (-10.0, -10.0, -10.0),
new OV.Coord3D (10.0, 10.0, 10.0)
);
let octree = new OV.Octree (box);
let count = 1000;

View File

@ -9,7 +9,7 @@ describe ('Mesh', function() {
assert.strictEqual (mesh.TextureUVCount (), 0);
assert.strictEqual (mesh.TriangleCount (), 0);
});
it ('Set Name', function () {
var mesh = new OV.Mesh ();
mesh.SetName ('example');
@ -37,7 +37,7 @@ describe ('Mesh', function() {
assert.strictEqual (normal.y, 2.0);
assert.strictEqual (normal.z, 3.0);
});
it ('Add Texture UV', function () {
var mesh = new OV.Mesh ();
var index = mesh.AddTextureUV (new OV.Coord2D (1.0, 2.0))
@ -47,7 +47,7 @@ describe ('Mesh', function() {
assert.strictEqual (uv.x, 1.0);
assert.strictEqual (uv.y, 2.0);
});
it ('Add Triangle', function () {
var mesh = new OV.Mesh ();
var triangle = new OV.Triangle (1, 2, 3);
@ -84,26 +84,15 @@ describe ('Mesh', function() {
triangle.SetTextureUVs (0, 1, 2);
mesh.AddTriangle (triangle);
let angle = -Math.PI / 2.0;
let rotX = 0.0;
let rotY = 1.0;
let rotZ = 0.0;
let rotation = [
Math.sin (angle / 2.0) * rotX,
Math.sin (angle / 2.0) * rotY,
Math.sin (angle / 2.0) * rotZ,
Math.cos (angle / 2.0)
];
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 1.0, 0.0), -Math.PI / 2.0);
let transformation = new OV.Transformation ();
transformation.AppendMatrix (new OV.Matrix ().CreateScale (2.0, 1.0, 1.0));
transformation.AppendMatrix (new OV.Matrix ().CreateRotation (rotation[0], rotation[1], rotation[2], rotation[3]));
transformation.AppendMatrix (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w));
transformation.AppendMatrix (new OV.Matrix ().CreateTranslation (0.0, 0.0, 1.0));
OV.TransformMesh (mesh, transformation);
assert (OV.CoordIsEqual3D (mesh.GetVertex (0), new OV.Coord3D (0.0, 0.0, 1.0)));
assert (OV.CoordIsEqual3D (mesh.GetVertex (1), new OV.Coord3D (0.0, 0.0, 3.0)));
assert (OV.CoordIsEqual3D (mesh.GetVertex (2), new OV.Coord3D (0.0, 1.0, 3.0)));
assert (OV.CoordIsEqual3D (mesh.GetNormal (0), new OV.Coord3D (-1.0, 0.0, 0.0)));
});
});
});

View File

@ -69,7 +69,7 @@ describe ('Model', function () {
it ('Remove Mesh', function () {
var model = new OV.Model ();
let mesh1 = new OV.Mesh ();
mesh1.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
mesh1.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0));
@ -97,7 +97,7 @@ describe ('Model', function () {
assert.strictEqual (model.MeshCount (), 0);
assert.strictEqual (model.VertexCount (), 0);
assert.strictEqual (model.TriangleCount (), 0);
});
});
});
describe ('Model Finalization', function () {
@ -140,10 +140,10 @@ describe ('Model Finalization', function () {
var meshIndex = model.AddMesh (mesh);
OV.FinalizeModel (model, function () { return new OV.Material (OV.MaterialType.Phong) });
var theMesh = model.GetMesh (meshIndex);
assert.strictEqual (theMesh.NormalCount (), 6);
var normal = theMesh.GetNormal (0);
assert.strictEqual (normal.x, 0.0);
assert.strictEqual (normal.y, -0.7071067811865475);
@ -173,15 +173,15 @@ describe ('Model Finalization', function () {
var meshIndex = model.AddMesh (mesh);
OV.FinalizeModel (model, function () { return new OV.Material (OV.MaterialType.Phong) });
var theMesh = model.GetMesh (meshIndex);
assert.strictEqual (theMesh.NormalCount (), 9);
var normal = theMesh.GetNormal (0);
assert.strictEqual (normal.x, 0.0);
assert.strictEqual (normal.y, -0.7071067811865475);
assert.strictEqual (normal.z, 0.7071067811865475);
});
});
});
describe ('Color Conversion', function () {
@ -307,7 +307,9 @@ function CreateTranslatedRotatedCubesModel ()
let rotatedNode = new OV.Node ();
rotatedNode.SetName ('Rotated');
rotatedNode.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateRotation (0.0, 0.0, 0.7071067811865475, 0.7071067811865476)));
let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 0.0, 1.0), Math.PI / 2.0);
rotatedNode.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w)));
let translatedRotatedNode = new OV.Node ();
translatedRotatedNode.SetName ('Translated and Rotated');
@ -371,7 +373,7 @@ describe ('Node Hierarchy', function () {
name : 'Node 2',
childNodes : [],
meshNames : []
}
}
],
meshNames : ['Mesh 1', 'Mesh 2']
});
@ -404,7 +406,7 @@ describe ('Node Hierarchy', function () {
name : 'Node 2',
childNodes : [],
meshNames : []
}
}
],
meshNames : ['Mesh 1', 'Mesh 2']
});
@ -439,7 +441,7 @@ describe ('Node Hierarchy', function () {
name : 'Node 2',
childNodes : [],
meshNames : []
}
}
],
meshNames : ['Mesh 1', 'Mesh 2']
});
@ -466,7 +468,7 @@ describe ('Node Hierarchy', function () {
}
],
meshNames : []
}
}
],
meshNames : ['Cube']
});
@ -513,7 +515,7 @@ describe ('Node Hierarchy', function () {
}
],
meshNames : []
}
}
],
meshNames : ['Cube']
});
@ -537,5 +539,5 @@ describe ('Node Hierarchy', function () {
assert (OV.CoordIsEqual3D (boundingBox3.min, new OV.Coord3D (0.0, 0.0, 0.0)));
assert (OV.CoordIsEqual3D (boundingBox3.max, new OV.Coord3D (1.0, 1.0, 1.0)));
});
});
});