Created
July 18, 2012 14:53
-
-
Save saitoha/3136671 to your computer and use it in GitHub Desktop.
css transform 3D / get transfrom matrix from 2 vectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @class Vector3d | |
| */ | |
| function Vector3d() | |
| { | |
| return this.initialize.apply(this, arguments); | |
| }; | |
| Vector3d.prototype = { | |
| x: 0, | |
| y: 0, | |
| z: 0, | |
| /** constructor */ | |
| initialize: function initialize(x, y, z) | |
| { | |
| this.x = x; | |
| this.y = y; | |
| this.z = z; | |
| }, | |
| /** get absolute value */ | |
| abs: function abs() | |
| { | |
| // (x^2 + y^2 + z^2) ^ (1/2) | |
| return Math.sqrt(this.x * this.x | |
| + this.y * this.y | |
| + this.z * this.z); | |
| }, | |
| /** get normalized vector */ | |
| norm: function nrom() | |
| { | |
| var abs = this.abs(); | |
| return new Vector3d(this.x / abs, | |
| this.y / abs, | |
| this.z / abs); | |
| }, | |
| /** get dot product for this and other */ | |
| dot: function dot(/* Vector3d */ other) | |
| { | |
| return this.x * other.x + this.y * other.y + this.z * other.z; | |
| }, | |
| /** get cross product for this and other */ | |
| cross: function cross(/* Vector3d */ other) | |
| { | |
| return new Vector3d( | |
| this.y * other.z - this.z * other.y, | |
| this.z * other.x - this.x * other.z, | |
| this.x * other.y - this.y * other.x); | |
| }, | |
| }; | |
| /** | |
| * | |
| * @class TransformMatrix | |
| * | |
| */ | |
| function TransformMatrix() | |
| { | |
| return this.initialize.apply(this, arguments); | |
| }; | |
| TransformMatrix.prototype = { | |
| _m00: 1, | |
| _m01: 0, | |
| _m02: 0, | |
| _m03: 0, | |
| _m10: 0, | |
| _m11: 1, | |
| _m12: 0, | |
| _m13: 0, | |
| _m20: 0, | |
| _m21: 0, | |
| _m22: 1, | |
| _m23: 0, | |
| _m30: 0, | |
| _m31: 0, | |
| _m32: 0, | |
| _m33: 1, | |
| /** constructor */ | |
| initialize: function initialize(m00, m01, m02, m03, | |
| m10, m11, m12, m13, | |
| m20, m21, m22, m23, | |
| m30, m31, m32, m33) | |
| { | |
| this._m00 = m00; | |
| this._m01 = m01; | |
| this._m02 = m02; | |
| this._m03 = m03; | |
| this._m10 = m10; | |
| this._m11 = m11; | |
| this._m12 = m12; | |
| this._m13 = m13; | |
| this._m20 = m20; | |
| this._m21 = m21; | |
| this._m22 = m22; | |
| this._m23 = m23; | |
| this._m30 = m30; | |
| this._m31 = m31; | |
| this._m32 = m32; | |
| this._m33 = m33; | |
| }, | |
| apply: function apply(other) | |
| { | |
| var a = this, | |
| b = other; | |
| return new TransformMatrix( | |
| a._m00 * b._m00 + a._m01 * b._m10 + a._m02 * b._m20 + a._m03 * b._m30, | |
| a._m00 * b._m01 + a._m01 * b._m11 + a._m02 * b._m21 + a._m03 * b._m31, | |
| a._m00 * b._m02 + a._m01 * b._m12 + a._m02 * b._m22 + a._m03 * b._m32, | |
| a._m00 * b._m03 + a._m01 * b._m13 + a._m02 * b._m23 + a._m03 * b._m33, | |
| a._m10 * b._m00 + a._m11 * b._m10 + a._m12 * b._m20 + a._m13 * b._m30, | |
| a._m10 * b._m01 + a._m11 * b._m11 + a._m12 * b._m21 + a._m13 * b._m31, | |
| a._m10 * b._m02 + a._m11 * b._m12 + a._m12 * b._m22 + a._m13 * b._m32, | |
| a._m10 * b._m03 + a._m11 * b._m13 + a._m12 * b._m23 + a._m13 * b._m33, | |
| a._m20 * b._m00 + a._m21 * b._m10 + a._m22 * b._m20 + a._m23 * b._m30, | |
| a._m20 * b._m01 + a._m21 * b._m11 + a._m22 * b._m21 + a._m23 * b._m31, | |
| a._m20 * b._m02 + a._m21 * b._m12 + a._m22 * b._m22 + a._m23 * b._m32, | |
| a._m20 * b._m03 + a._m21 * b._m13 + a._m22 * b._m23 + a._m23 * b._m33, | |
| a._m30 * b._m00 + a._m31 * b._m10 + a._m32 * b._m20 + a._m33 * b._m30, | |
| a._m30 * b._m01 + a._m31 * b._m11 + a._m32 * b._m21 + a._m33 * b._m31, | |
| a._m30 * b._m02 + a._m31 * b._m12 + a._m32 * b._m22 + a._m33 * b._m32, | |
| a._m30 * b._m03 + a._m31 * b._m13 + a._m32 * b._m23 + a._m33 * b._m33); | |
| }, | |
| toString: function toString() | |
| { | |
| return "matrix3d(" | |
| + [ | |
| this._m00, this._m01, this._m02, this._m03, | |
| this._m10, this._m11, this._m12, this._m13, | |
| this._m20, this._m21, this._m22, this._m23, | |
| this._m30, this._m31, this._m32, this._m33 | |
| ].join(",") | |
| + ")" | |
| }, | |
| }; | |
| /** | |
| * @fn getMatrixFrom2Vecters | |
| */ | |
| function getMatrixFrom2Vectors(a, b) | |
| { | |
| var cross = b.cross(a), | |
| cos_angle = b.dot(a), | |
| sin_angle = cross.abs(), | |
| normalized_axis = cross.norm(), | |
| x = normalized_axis.x, | |
| y = normalized_axis.y, | |
| z = normalized_axis.z, | |
| matrix = new TransformMatrix( | |
| 1 + (1 - cos_angle) * (x * x - 1), | |
| -z * sin_angle + (1 - cos_angle) * x * y, | |
| y * sin_angle + (1 - cos_angle) * x * z, | |
| 0, | |
| z * sin_angle + (1 - cos_angle) * x * y, | |
| 1 + (1 - cos_angle) * (y * y - 1), | |
| -x * sin_angle + (1 - cos_angle) * y * z, | |
| 0, | |
| -y * sin_angle + (1- cos_angle) * x * z, | |
| x * sin_angle + (1 - cos_angle) * y * z, | |
| 1 + (1 - cos_angle) * (z * z - 1), | |
| 0, | |
| 0, 0, 0, 1); | |
| return matrix; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment