UNPKG

vislite

Version:

灵活、快速、简单的数据可视化交互式跨端前端库

135 lines (129 loc) 4.65 kB
function _move (d, a, b, c) { if (c === void 0) { c = 0; } var sqrt = Math.sqrt(a * a + b * b + c * c); return [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, a * d / sqrt, b * d / sqrt, c * d / sqrt, 1 ]; } function _rotate (deg) { var sin = Math.sin(deg), cos = Math.cos(deg); return [ cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]; } function _scale (xTimes, yTimes, zTimes, cx, cy, cz) { if (cx === void 0) { cx = 0; } if (cy === void 0) { cy = 0; } if (cz === void 0) { cz = 0; } return [ xTimes, 0, 0, 0, 0, yTimes, 0, 0, 0, 0, zTimes, 0, cx - cx * xTimes, cy - cy * yTimes, cz - cz * zTimes, 1 ]; } function _transform (a1, b1, c1, a2, b2, c2) { if (typeof a1 === 'number' && typeof b1 === 'number') { if (typeof c1 !== 'number') { c1 = 0; a2 = a1; b2 = b1; c2 = 1; } else if (typeof a2 !== 'number' || typeof b2 !== 'number' || typeof c2 !== 'number') { a2 = a1; b2 = b1; c2 = c1; a1 = 0; b1 = 0; c1 = 0; } if (a1 == a2 && b1 == b2 && c1 == c2) throw new Error('It\'s not a legitimate ray!'); var sqrt1 = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1)), cos1 = sqrt1 != 0 ? (b2 - b1) / sqrt1 : 1, sin1 = sqrt1 != 0 ? (a2 - a1) / sqrt1 : 0, b = (a2 - a1) * sin1 + (b2 - b1) * cos1, c = c2 - c1, sqrt2 = Math.sqrt(b * b + c * c), cos2 = sqrt2 != 0 ? c / sqrt2 : 1, sin2 = sqrt2 != 0 ? b / sqrt2 : 0; return [ [ cos1, cos2 * sin1, sin1 * sin2, 0, -sin1, cos1 * cos2, cos1 * sin2, 0, 0, -sin2, cos2, 0, b1 * sin1 - a1 * cos1, c1 * sin2 - a1 * sin1 * cos2 - b1 * cos1 * cos2, -a1 * sin1 * sin2 - b1 * cos1 * sin2 - c1 * cos2, 1 ], [ cos1, -sin1, 0, 0, cos2 * sin1, cos2 * cos1, -sin2, 0, sin1 * sin2, cos1 * sin2, cos2, 0, a1, b1, c1, 1 ] ]; } else { throw new Error('a1 and b1 is required!'); } } var _multiply = function (matrix4, param) { var newParam = []; for (var i = 0; i < 4; i++) for (var j = 0; j < param.length / 4; j++) newParam[j * 4 + i] = matrix4[i] * param[j * 4] + matrix4[i + 4] * param[j * 4 + 1] + matrix4[i + 8] * param[j * 4 + 2] + matrix4[i + 12] * param[j * 4 + 3]; return newParam; }; var __initMatrix4 = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]; var Matrix4 = (function () { function Matrix4(initMatrix4) { if (initMatrix4 === void 0) { initMatrix4 = __initMatrix4; } this.name = 'Matrix4'; this.__matrix4 = initMatrix4; } Matrix4.prototype.setValue = function (initMatrix4) { if (initMatrix4 === void 0) { initMatrix4 = __initMatrix4; } this.__matrix4 = initMatrix4; return this; }; Matrix4.prototype.move = function (dis, a, b, c) { if (c === void 0) { c = 0; } this.__matrix4 = _multiply(_move(dis, a, b, c), this.__matrix4); return this; }; Matrix4.prototype.rotate = function (deg, a1, b1, c1, a2, b2, c2) { var matrix4s = _transform(a1, b1, c1, a2, b2, c2); this.__matrix4 = _multiply(_multiply(_multiply(matrix4s[1], _rotate(deg)), matrix4s[0]), this.__matrix4); return this; }; Matrix4.prototype.scale = function (xTimes, yTimes, zTimes, cx, cy, cz) { if (cx === void 0) { cx = 0; } if (cy === void 0) { cy = 0; } if (cz === void 0) { cz = 0; } this.__matrix4 = _multiply(_scale(xTimes, yTimes, zTimes, cx, cy, cz), this.__matrix4); return this; }; Matrix4.prototype.multiply = function (newMatrix4, flag) { if (flag === void 0) { flag = false; } this.__matrix4 = flag ? _multiply(this.__matrix4, newMatrix4) : _multiply(newMatrix4, this.__matrix4); return this; }; Matrix4.prototype.use = function (x, y, z, w) { if (z === void 0) { z = 0; } if (w === void 0) { w = 1; } return _multiply(this.__matrix4, [x, y, z, w]); }; Matrix4.prototype.value = function () { return this.__matrix4; }; return Matrix4; }()); export { Matrix4 as default };