Add quaternion class.
This commit is contained in:
parent
2815c27a32
commit
0394d60581
@ -14,6 +14,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
@ -117,3 +117,8 @@ OV.VectorLength3D = function (x, y, z)
|
||||
{
|
||||
return Math.sqrt (x * x + y * y + z * z);
|
||||
};
|
||||
|
||||
OV.ArrayToCoord3D = function (arr)
|
||||
{
|
||||
return new OV.Coord3D (arr[0], arr[1], arr[2]);
|
||||
};
|
||||
|
||||
@ -104,16 +104,16 @@ OV.Matrix = class
|
||||
|
||||
ComposeTRS (translation, rotation, scale)
|
||||
{
|
||||
let tx = translation[0];
|
||||
let ty = translation[1];
|
||||
let tz = translation[2];
|
||||
let qx = rotation[0];
|
||||
let qy = rotation[1];
|
||||
let qz = rotation[2];
|
||||
let qw = rotation[3];
|
||||
let sx = scale[0];
|
||||
let sy = scale[1];
|
||||
let sz = scale[2];
|
||||
let tx = translation.x;
|
||||
let ty = translation.y;
|
||||
let tz = translation.z;
|
||||
let qx = rotation.x;
|
||||
let qy = rotation.y;
|
||||
let qz = rotation.z;
|
||||
let qw = rotation.w;
|
||||
let sx = scale.x;
|
||||
let sy = scale.y;
|
||||
let sz = scale.z;
|
||||
|
||||
let x2 = qx + qx;
|
||||
let y2 = qy + qy;
|
||||
@ -139,11 +139,11 @@ OV.Matrix = class
|
||||
|
||||
DecomposeTRS ()
|
||||
{
|
||||
let translation = [
|
||||
let translation = new OV.Coord3D (
|
||||
this.matrix[12],
|
||||
this.matrix[13],
|
||||
this.matrix[14]
|
||||
];
|
||||
);
|
||||
|
||||
let sx = OV.VectorLength3D (this.matrix[0], this.matrix[1], this.matrix[2]);
|
||||
let sy = OV.VectorLength3D (this.matrix[4], this.matrix[5], this.matrix[6]);
|
||||
@ -152,7 +152,7 @@ OV.Matrix = class
|
||||
if (OV.IsNegative (determinant)) {
|
||||
sx *= -1.0;
|
||||
}
|
||||
let scale = [sx, sy, sz];
|
||||
let scale = new OV.Coord3D (sx, sy, sz);
|
||||
|
||||
let m00 = this.matrix[0] / sx;
|
||||
let m01 = this.matrix[4] / sy;
|
||||
@ -169,36 +169,36 @@ OV.Matrix = class
|
||||
let tr = m00 + m11 + m22;
|
||||
if (tr > 0.0) {
|
||||
let s = Math.sqrt (tr + 1.0) * 2.0;
|
||||
rotation = [
|
||||
rotation = new OV.Quaternion (
|
||||
(m21 - m12) / s,
|
||||
(m02 - m20) / s,
|
||||
(m10 - m01) / s,
|
||||
0.25 * s
|
||||
];
|
||||
);
|
||||
} else if ((m00 > m11) && (m00 > m22)) {
|
||||
let s = Math.sqrt (1.0 + m00 - m11 - m22) * 2.0;
|
||||
rotation = [
|
||||
rotation = new OV.Quaternion (
|
||||
0.25 * s,
|
||||
(m01 + m10) / s,
|
||||
(m02 + m20) / s,
|
||||
(m21 - m12) / s
|
||||
];
|
||||
);
|
||||
} else if (m11 > m22) {
|
||||
let s = Math.sqrt (1.0 + m11 - m00 - m22) * 2.0;
|
||||
rotation = [
|
||||
rotation = new OV.Quaternion (
|
||||
(m01 + m10) / s,
|
||||
0.25 * s,
|
||||
(m12 + m21) / s,
|
||||
(m02 - m20) / s
|
||||
];
|
||||
);
|
||||
} else {
|
||||
let s = Math.sqrt (1.0 + m22 - m00 - m11) * 2.0;
|
||||
rotation = [
|
||||
rotation = new OV.Quaternion (
|
||||
(m02 + m20) / s,
|
||||
(m12 + m21) / s,
|
||||
0.25 * s,
|
||||
(m10 - m01) / s
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
15
source/geometry/quaternion.js
Normal file
15
source/geometry/quaternion.js
Normal file
@ -0,0 +1,15 @@
|
||||
OV.Quaternion = class
|
||||
{
|
||||
constructor (x, y, z, w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
};
|
||||
|
||||
OV.ArrayToQuaternion = function (arr)
|
||||
{
|
||||
return new OV.Quaternion (arr[0], arr[1], arr[2], arr[3]);
|
||||
};
|
||||
@ -468,9 +468,9 @@ OV.Importer3ds = class extends OV.ImporterBase
|
||||
|
||||
let matrix = new OV.Matrix ();
|
||||
matrix.ComposeTRS (
|
||||
GetNodePosition (node),
|
||||
GetNodeRotation (node),
|
||||
GetNodeScale (node)
|
||||
OV.ArrayToCoord3D (GetNodePosition (node)),
|
||||
OV.ArrayToQuaternion (GetNodeRotation (node)),
|
||||
OV.ArrayToCoord3D (GetNodeScale (node))
|
||||
);
|
||||
|
||||
if (node.userId !== 65535) {
|
||||
|
||||
@ -700,7 +700,11 @@ OV.ImporterGltf = class extends OV.ImporterBase
|
||||
}
|
||||
|
||||
if (hasTransformation) {
|
||||
matrix.ComposeTRS (translation, rotation, scale);
|
||||
matrix.ComposeTRS (
|
||||
OV.ArrayToCoord3D (translation),
|
||||
OV.ArrayToQuaternion (rotation),
|
||||
OV.ArrayToCoord3D (scale)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ OV.TransformMesh = function (mesh, transformation)
|
||||
|
||||
if (mesh.NormalCount () > 0) {
|
||||
let trs = transformation.GetMatrix ().DecomposeTRS ();
|
||||
let normalMatrix = new OV.Matrix ().ComposeTRS ([0.0, 0.0, 0.0], trs.rotation, [1.0, 1.0, 1.0]);
|
||||
let normalMatrix = new OV.Matrix ().ComposeTRS (new OV.Coord3D (0.0, 0.0, 0.0), trs.rotation, new OV.Coord3D (1.0, 1.0, 1.0));
|
||||
let normalTransformation = new OV.Transformation (normalMatrix);
|
||||
for (let i = 0; i < mesh.NormalCount (); i++) {
|
||||
let normal = mesh.GetNormal (i);
|
||||
|
||||
@ -12,12 +12,12 @@ function CreateYRot90Quaternion ()
|
||||
let rotX = 0.0;
|
||||
let rotY = 1.0;
|
||||
let rotZ = 0.0;
|
||||
let quaternion = [
|
||||
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;
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ describe ('Transformation', function() {
|
||||
assert (!tr.IsIdentity ());
|
||||
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), new OV.Coord3D (3.0, 8.0, 15.0)));
|
||||
|
||||
tr.AppendMatrix (new OV.Matrix ().CreateRotation (rotation[0], rotation[1], rotation[2], rotation[3]));
|
||||
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)));
|
||||
|
||||
@ -111,7 +111,7 @@ describe ('Transformation', function() {
|
||||
assert (OV.CoordIsEqual3D (tr.TransformCoord3D (coord), new OV.Coord3D (19.0, 13.0, 3.0)));
|
||||
|
||||
let tr2 = new OV.Transformation ();
|
||||
tr2.SetMatrix (new OV.Matrix ().ComposeTRS ([4.0, 5.0, 6.0], rotation, [3.0, 4.0, 5.0]));
|
||||
tr2.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 (tr2.TransformCoord3D (coord), new OV.Coord3D (19.0, 13.0, 3.0)));
|
||||
});
|
||||
|
||||
@ -120,7 +120,7 @@ describe ('Transformation', function() {
|
||||
let coord = new OV.Coord3D (1.0, 2.0, 3.0);
|
||||
|
||||
let tr = new OV.Transformation ();
|
||||
tr.SetMatrix (new OV.Matrix ().ComposeTRS ([4.0, 5.0, 6.0], rotation, [3.0, 4.0, 5.0]));
|
||||
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)));
|
||||
});
|
||||
|
||||
@ -129,7 +129,7 @@ describe ('Transformation', function() {
|
||||
let coord = new OV.Coord3D (1.0, 2.0, 3.0);
|
||||
|
||||
let tr = new OV.Transformation ();
|
||||
tr.SetMatrix (new OV.Matrix ().ComposeTRS ([4.0, 5.0, 6.0], rotation, [3.0, 4.0, 5.0]));
|
||||
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)));
|
||||
|
||||
let trs = tr.GetMatrix ().DecomposeTRS ();
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
"source/geometry/geometry.js",
|
||||
"source/geometry/coord2d.js",
|
||||
"source/geometry/coord3d.js",
|
||||
"source/geometry/quaternion.js",
|
||||
"source/geometry/box3d.js",
|
||||
"source/geometry/octree.js",
|
||||
"source/geometry/matrix.js",
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
<script type="text/javascript" src="../source/geometry/geometry.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord2d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/coord3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/quaternion.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/box3d.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/octree.js"></script>
|
||||
<script type="text/javascript" src="../source/geometry/matrix.js"></script>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user