import {
Vector3,
Vector4,
Matrix3,
Matrix4,
Quaternion
} from "three";
/**
* Add a method to Three.js Vector3.
* Return a human-readable version of the vector (for debugging purpose)
* @return {string}
*/
Vector3.prototype.toLog = function () {
return `[${this.x}, ${this.y}, ${this.z}]`
}
/**
* Add a method to Three.js Vector4.
* Return a human-readable version of the vector (for debugging purpose)
* @return {string}
*/
Vector4.prototype.toLog = function () {
return `[${this.x}, ${this.y}, ${this.z}, ${this.w}]`
}
/**
* Add a method to Three.js Matrix3.
* Return a human-readable version of the matrix (for debugging purpose)
* @return {string}
*/
Matrix3.prototype.toLog = function () {
let res = '\r\n';
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j !== 0) {
res = res + ",\t";
}
res = res + this.elements[i + 3 * j];
}
res = res + "\r\n";
}
return res;
}
/**
* Return the given power of the current matrix
* @param {number} n - the exponent. It should be an integer (non necessarily positive)
* @return {Matrix3} - the power of the matrix
*/
Matrix3.prototype.power = function (n) {
if (n < 0) {
return this.invert().power(-n);
}
if (n === 0) {
return this.identity();
}
if (n === 1) {
return this;
}
if (n % 2 === 0) {
this.power(n / 2);
return this.multiply(this);
} else {
const aux = this.clone();
this.power(n - 1);
return this.multiply(aux);
}
}
/**
* Add a method to Three.js Matrix4.
* Return a human-readable version of the matrix (for debugging purpose)
* @return {string}
*/
Matrix4.prototype.toLog = function () {
let res = '\r\n';
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (j !== 0) {
res = res + ",\t";
}
res = res + this.elements[i + 4 * j];
}
res = res + "\r\n";
}
return res;
}
/**
* A a method to Three.js Matrix4
* Addition of matrices
* @param {Matrix4} matrix - the matrix to add
* @returns {Matrix4} - the current matrix
*/
Matrix4.prototype.add = function (matrix) {
// addition of tow 4x4 matrices
this.set.apply(this, [].map.call(this.elements, function (c, i) {
return c + matrix.elements[i];
}));
return this;
};
/**
* Add a method to Three.js Quaternion.
* Return a human-readable version of the vector (for debugging purpose)
* @return {string}
*/
Quaternion.prototype.toLog = function () {
return `[${this.x}, ${this.y}, ${this.z}, ${this.w}]`
}
/**
* Multiply a quaternion by the given scalar
* @param {number} c - a scalar
* @return {Quaternion} - the current quaternion
*/
Quaternion.prototype.multiplyScalar = function (c) {
this.set(c * this.x, c * this.y, c * this.z, c * this.w);
return this;
}
/**
* Add two quaternions
* @param {Quaternion} q - the quaternion to add
* @return {Quaternion} - the current quaternion
*/
Quaternion.prototype.add = function (q) {
this.set(this.x + q.x, this.y + q.y, this.z + q.z, this.w + q.w);
return this;
}
/**
* Transform a method attached to an object into a function.
* @param {Object} scope - the object on which the method is called
* @param {function} fn - the method to call
* @return {function(): *}
*/
export function bind(scope, fn) {
return function () {
return fn.apply(scope, arguments);
};
}
/**
* Replace all the special characters in the string by an underscore
* @param {string} str
* @return {string}
*/
export function safeString(str) {
return str.replace(/\W/g, '_');
}