File: src/math/Transform.js
var Vec3 = require('./Vec3');
var Quaternion = require('./Quaternion');
module.exports = Transform;
/**
* @class Transform
* @constructor
*/
function Transform(options) {
options = options || {};
/**
* @property {Vec3} position
*/
this.position = new Vec3();
if(options.position){
this.position.copy(options.position);
}
/**
* @property {Quaternion} quaternion
*/
this.quaternion = new Quaternion();
if(options.quaternion){
this.quaternion.copy(options.quaternion);
}
}
var tmpQuat = new Quaternion();
/**
* @static
* @method pointToLocaFrame
* @param {Vec3} position
* @param {Quaternion} quaternion
* @param {Vec3} worldPoint
* @param {Vec3} result
*/
Transform.pointToLocalFrame = function(position, quaternion, worldPoint, result){
var result = result || new Vec3();
worldPoint.vsub(position, result);
quaternion.conjugate(tmpQuat);
tmpQuat.vmult(result, result);
return result;
};
/**
* Get a global point in local transform coordinates.
* @method pointToLocal
* @param {Vec3} point
* @param {Vec3} result
* @return {Vec3} The "result" vector object
*/
Transform.prototype.pointToLocal = function(worldPoint, result){
return Transform.pointToLocalFrame(this.position, this.quaternion, worldPoint, result);
};
/**
* @static
* @method pointToWorldFrame
* @param {Vec3} position
* @param {Vec3} quaternion
* @param {Vec3} localPoint
* @param {Vec3} result
*/
Transform.pointToWorldFrame = function(position, quaternion, localPoint, result){
var result = result || new Vec3();
quaternion.vmult(localPoint, result);
result.vadd(position, result);
return result;
};
/**
* Get a local point in global transform coordinates.
* @method pointToWorld
* @param {Vec3} point
* @param {Vec3} result
* @return {Vec3} The "result" vector object
*/
Transform.prototype.pointToWorld = function(localPoint, result){
return Transform.pointToWorldFrame(this.position, this.quaternion, localPoint, result);
};
Transform.prototype.vectorToWorldFrame = function(localVector, result){
var result = result || new Vec3();
this.quaternion.vmult(localVector, result);
return result;
};
Transform.vectorToWorldFrame = function(quaternion, localVector, result){
quaternion.vmult(localVector, result);
return result;
};
Transform.vectorToLocalFrame = function(position, quaternion, worldVector, result){
var result = result || new Vec3();
quaternion.w *= -1;
quaternion.vmult(worldVector, result);
quaternion.w *= -1;
return result;
};