@pilotlab/lux-types
Version:
A luxurious user experience framework, developed by your friends at Pilot.
234 lines • 8.97 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var lux_is_1 = require("@pilotlab/lux-is");
var lux_attributes_1 = require("@pilotlab/lux-attributes");
var types_1 = require("./types");
var pointBase_1 = require("./pointBase");
var Vector = (function (_super) {
__extends(Vector, _super);
function Vector(x, y, z, w) {
var _this = _super.call(this, 'Vector', x, y, z) || this;
_this.p_w = _this.children.set('w', lux_is_1.default.notEmpty(w) ? w : 0, lux_attributes_1.DataType.NUMBER_DOUBLE, lux_attributes_1.AttributeChangeOptions.zero).node;
return _this;
}
Object.defineProperty(Vector.prototype, "w", {
get: function () { return this.p_w.value; },
set: function (value) { this.p_w.set(value, lux_attributes_1.AttributeChangeOptions.saveAndSignal.durationZero); },
enumerable: true,
configurable: true
});
Object.defineProperty(Vector.prototype, "length", {
get: function () {
return Math.sqrt(this.dot(this));
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector.prototype, "magnitude", {
get: function () { return this.length; },
enumerable: true,
configurable: true
});
Object.defineProperty(Vector.prototype, "abs", {
get: function () {
return new Vector(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z), Math.abs(this.w));
},
enumerable: true,
configurable: true
});
Vector.prototype.dot = function (v) {
var values = this.children.list;
var valuesIn = v.children.list;
if (values.length != values.length)
throw new Error('Vectors of different dimensions.');
var erg = 0.0;
for (var i = 0; i < values.length; i++)
erg += values.item(i).value * valuesIn.item(i).value;
return erg;
};
Vector.prototype.normalize = function () {
var m = this.magnitude;
var values = this.children.list;
if (m <= types_1.default.tolerance)
m = 1.0;
for (var i = 0; i < values.length; i++) {
values.item(i).value = values.item(i).value / m;
if (Math.abs(values.item(i).value) < types_1.default.tolerance)
values.item(i).value = 0.0;
}
return this;
};
Vector.prototype.multiply = function (v) {
var values = this.children.list;
var valuesIn = v.children.list;
if (valuesIn.length !== values.length)
return null;
for (var i = 0; i < values.length; i++) {
values.item(i).value = values.item(i).value * valuesIn.item(i).value;
}
return this;
};
Vector.prototype.divide = function (v) {
var values = this.children.list;
var valuesIn = v.children.list;
if (valuesIn.length !== values.length)
return null;
for (var i = 0; i < values.length; i++) {
values.item(i).value = values.item(i).value / valuesIn.item(i).value;
}
return this;
};
Object.defineProperty(Vector, "identity", {
get: function () { return new Vector(0, 0); },
enumerable: true,
configurable: true
});
Vector.fromPoint3D = function (pt) { return new Vector(pt.x, pt.y, pt.z, null); };
Object.defineProperty(Vector, "empty", {
get: function () { return new Vector(); },
enumerable: true,
configurable: true
});
Vector.cross = function (v1, v2, dest) {
if (dest === void 0) { dest = null; }
var values1 = v1.children.list;
var values2 = v2.children.list;
if (values1.length != values2.length || values1.length < 3)
return new Vector();
if (!dest)
dest = new Vector(0, 0, 0);
dest.x = v1.y * v2.z - v1.z * v2.y;
dest.y = v1.z * v2.x - v1.x * v2.z;
dest.z = v1.x * v2.y - v1.y * v2.x;
return dest;
};
Vector.dot = function (v1, v2) {
var values1 = v1.children.list;
var values2 = v2.children.list;
if (values1.length !== values2.length)
return -1.0;
var e = 0.0;
var d = 0.0;
for (var i = 0; i < values1.length; i++) {
d = (values1.item(i).value * values2.item(i).value);
e += d;
}
return e;
};
Vector.distance = function (v1, v2) {
var values1 = v1.children.list;
var values2 = v2.children.list;
if (values1.length !== values2.length)
return -1.0;
var e = 0.0;
var d = 0.0;
for (var i = 0; i < values1.length; i++) {
d = (values1.item(i).value - values2.item(i).value);
e += d * d;
}
return Math.sqrt(e);
};
Vector.direction = function (v1, v2, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
var d = 0.0;
var length = 0.0;
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value - values2.item(i).value;
length += d * d;
}
if (length === 0) {
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = 0;
}
return dest;
}
length = 1 / length;
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = valuesDest.item(i).value * length;
}
return dest;
};
Vector.mix = function (v1, v2, time, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value + time * (values2.item(i).value - values1.item(i).value);
}
return dest;
};
Vector.sum = function (v1, v2, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value + values2.item(i).value;
}
return dest;
};
Vector.difference = function (v1, v2, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value - values2.item(i).value;
}
return dest;
};
Vector.product = function (v1, v2, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value * values2.item(i).value;
}
return dest;
};
Vector.quotient = function (v1, v2, dest) {
if (dest === void 0) { dest = new Vector(); }
var values1 = v1.children.list;
var values2 = v2.children.list;
var valuesDest = dest.children.list;
if (values1.length !== values2.length)
return new Vector();
for (var i = 0; i < values1.length; i++) {
valuesDest.item(i).value = values1.item(i).value / values2.item(i).value;
}
return dest;
};
Vector.zero = new Vector(0, 0, 0);
Vector.up = new Vector(0, 1, 0);
Vector.right = new Vector(1, 0, 0);
Vector.forward = new Vector(0, 0, 1);
return Vector;
}(pointBase_1.default));
exports.Vector = Vector;
exports.default = Vector;
//# sourceMappingURL=vector.js.map