商域无疆 (http://blog.youkuaiyun.com/omni360/)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:商域无疆 - 本博客专注于 敏捷开发及移动和物联设备研究:数据可视化、GOLANG、Html5、WEBGL、THREE.JS,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
以下代码是THREE.JS 源码文件中Math/Vector4.js文件的注释.
更多更新在 : https://github.com/omni360/three.js.sourcecode/blob/master/Three.js
// File:src/math/Vector4.js
/**
* @author supereggbert / http://www.paulbrunt.co.uk/
* @author philogb / http://blog.thejit.org/
* @author mikael emtinger / http://gomo.se/
* @author egraether / http://egraether.com/
* @author WestLangley / http://github.com/WestLangley
*/
/*
///Vector4对象的构造函数.用来创建一个四维向量的对象.Vector4对象的功能函数采用
///定义构造的函数原型对象来实现.
///
/// 用法: var p4d = new Vector4(5,3,2,1)
/// 创建一个x坐标为5,y坐标为3,z坐标为2,齐次坐标w为1的向量
/// NOTE: 参数(x,y,z,w)坐标为可选参数,如果不指定参数(x,y,z,w),将创建一个坐标为(0,0,0,1)的向量.
/// NOTE: 齐次坐标,是一种用来解决坐标变换等操作的快捷方法.w称为齐次坐标。三维空间的点(x,y,z),用四维向量表示成(x,y,z,1)和(x,y,z,0)是不一样的,前者可以用变换矩阵实现平移等操作,后者不能。
/// NOTE: 在进行坐标和向量计算中,为了不至于混淆点和向量,另外,在进行几何变换时,为了加快运算速度,简化计算,往往使用矩阵,而在使用矩阵运算时,矩阵的乘积只能表示旋转、比例和剪切等等变换,而不能表示平移变换。因此为统一计算(使用齐次坐标在数学中的意义还要广),引入了第四个分量w,这使得原本二维坐标变成三维坐标,同理三维坐标变为四维坐标,而w称为比例因子,当w不为0时(一般设1),表示一个坐标,一个三维坐标的三个分量x,y,z用齐次坐标表示为变为x,y,z,w的四维空间,变换成三维坐标是方式是x/w,y/w,z/w,当w为0时,在数学上代表无穷远点,即并非一个具体的坐标位置,而是一个具有大小和方向的向量。从而,通过w我们就可以用同一系统表示两种不同的量
*/
///<summary>Vector4</summary>
///<param name ="x" type="number">x坐标</param>
///<param name ="y" type="number">y坐标</param>
///<param name ="z" type="number">z坐标</param>
///<param name ="w" type="number">w齐次坐标</param>
THREE.Vector4 = function ( x, y, z, w ) {
this.x = x || 0; //如果参数x值没有定义初始化为0.
this.y = y || 0; //如果参数y值没有定义初始化为0.
this.z = z || 0; //如果参数z值没有定义初始化为0.
this.w = ( w !== undefined ) ? w : 1; //如果参数w值没有定义初始化为1.
};
/****************************************
****下面是Vector4对象提供的功能函数.
****************************************/
THREE.Vector4.prototype = {
constructor: THREE.Vector4, //构造器
/*
///set方法用来从新设置四维向量的x,y,z,w坐标值.并返回新的坐标值的四维向量.
*/
///<summary>set</summary>
///<param name ="x" type="number">x坐标</param>
///<param name ="y" type="number">y坐标</param>
///<param name ="z" type="number">y坐标</param>
///<param name ="w" type="number">w齐次坐标</param>
///<returns type="Vector4">返回新坐标值的四维向量</returns>
set: function ( x, y, z, w ) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this; //返回新坐标值的四维向量
},
/*
///setX方法用来从新设置四维向量的x坐标值.并返回新的坐标值的四维向量.
*/
///<summary>setX</summary>
///<param name ="x" type="number">x坐标</param>
///<returns type="Vector4">返回新坐标值的四维向量</returns>
setX: function ( x ) {
this.x = x;
return this; //返回新坐标值的四维向量
},
/*
///setY方法用来从新设置四维向量的y坐标值.并返回新的坐标值的四维向量.
*/
///<summary>setY</summary>
///<param name ="y" type="number">y坐标</param>
///<returns type="Vector4">返回新坐标值的四维向量</returns>
setY: function ( y ) {
this.y = y;
return this; //返回新坐标值的四维向量
},
/*
///setZ方法用来从新设置四维向量的z坐标值.并返回新的坐标值的四维向量.
*/
///<summary>setZ</summary>
///<param name ="z" type="number">z坐标</param>
///<returns type="Vector4">返回新坐标值的四维向量</returns>
setZ: function ( z ) {
this.z = z;
return this; //返回新坐标值的四维向量
},
/*
///setW方法用来从新设置四维向量的w坐标值.并返回新的坐标值的四维向量.
/// NOTE: 齐次坐标,是一种用来解决坐标变换等操作的快捷方法.
*/
///<summary>setW</summary>
///<param name ="w" type="number">w齐次坐标</param>
///<returns type="Vector4">返回新坐标值的四维向量</returns>
setW: function ( w ) {
this.w = w;
return this; //返回新坐标值的四维向量
},
/*
///setComponent方法用来从新设置四维向量的(x,y,z,w)坐标值.并返回新的坐标值的四维向量.
///参数index取值为0,1,2 或者 3,取值为0,参数value设置x的坐标值,取值为1,参数value设置y的坐标,
///取值为2,参数value设置z的坐标,取值为3,参数value设置w的坐标.
*/
///<summary>setComponent</summary>
///<param name ="index" type="number">0,1,2或3</param>
///<param name ="value" type="number">x, y,z 或 w坐标</param>
setComponent: function ( index, value ) {
switch ( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
case 3: this.w = value; break;
default: throw new Error( 'index is out of range: ' + index );
}
},
/*
///getComponent方法用获得四维向量的(x,y,z,w)坐标值.
///参数index取值为0,1,2或者 3,取值为0,获得x的坐标值,取值为1,获得y的坐标,
///取值为2,获得z的坐标,取值为3,获得w的坐标.
*/
///<summary>getComponent</summary>
///<param name =