-
/*
-
* Copyright (c) 2003-2009 jMonkeyEngine
-
* All rights reserved.
-
*
-
* Redistribution and use in source and binary forms, with or without
-
* modification, are permitted provided that the following conditions are
-
* met:
-
*
-
* * Redistributions of source code must retain the above copyright
-
* notice, this list of conditions and the following disclaimer.
-
*
-
* * Redistributions in binary form must reproduce the above copyright
-
* notice, this list of conditions and the following disclaimer in the
-
* documentation and/or other materials provided with the distribution.
-
*
-
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
-
* may be used to endorse or promote products derived from this software
-
* without specific prior written permission.
-
*
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
-
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
*/
-
-
package com.jme.math;
-
-
import java.io.Externalizable;
-
import java.io.IOException;
-
import java.io.ObjectInput;
-
import java.io.ObjectOutput;
-
import java.util.logging.Logger;
-
-
import com.jme.util.export.InputCapsule;
-
import com.jme.util.export.JMEExporter;
-
import com.jme.util.export.JMEImporter;
-
import com.jme.util.export.OutputCapsule;
-
import com.jme.util.export.Savable;
-
-
/*
-
* -- Added *Local methods to cut down on object creation - JS
-
*/
-
-
/**
-
* <code>Vector3f</code> defines a Vector for a three float value tuple.
-
* <code>Vector3f</code> can represent any three dimensional value, such as a
-
* vertex, a normal, etc. Utility methods are also included to aid in
-
* mathematical calculations.<br/>
-
* 3维向量类
-
*
-
* @author Mark Powell
-
* @author Joshua Slack
-
*/
-
public class Vector3f implements Externalizable, Savable, Cloneable {
-
private static final Logger logger = Logger.getLogger(Vector3f.class
-
.getName());
-
-
private static final long serialVersionUID = 1L;
-
-
public final static Vector3f ZERO = new Vector3f(0, 0, 0);
-
-
public final static Vector3f UNIT_X = new Vector3f(1, 0, 0);
-
public final static Vector3f UNIT_Y = new Vector3f(0, 1, 0);
-
public final static Vector3f UNIT_Z = new Vector3f(0, 0, 1);
-
public final static Vector3f UNIT_XYZ = new Vector3f(1, 1, 1);
-
-
/**
-
* the x value of the vector.
-
*/
-
public float x;
-
-
/**
-
* the y value of the vector.
-
*/
-
public float y;
-
-
/**
-
* the z value of the vector.
-
*/
-
public float z;
-
-
/**
-
* Constructor instantiates a new <code>Vector3f</code> with default values
-
* of (0,0,0).
-
*
-
*/
-
public Vector3f() {
-
x = y = z = 0;
-
}
-
-
/**
-
* Constructor instantiates a new <code>Vector3f</code> with provides
-
* values.
-
*
-
* @param x
-
* the x value of the vector.
-
* @param y
-
* the y value of the vector.
-
* @param z
-
* the z value of the vector.
-
*/
-
public Vector3f(float x, float y, float z) {
-
this.x = x;
-
this.y = y;
-
this.z = z;
-
}
-
-
/**
-
* Constructor instantiates a new <code>Vector3f</code> that is a copy of
-
* the provided vector
-
*
-
* @param copy
-
* The Vector3f to copy
-
*/
-
public Vector3f(Vector3f copy) {
-
this.set(copy);
-
}
-
-
/**
-
* <code>set</code> sets the x,y,z values of the vector based on passed
-
* parameters.
-
*
-
* @param x
-
* the x value of the vector.
-
* @param y
-
* the y value of the vector.
-
* @param z
-
* the z value of the vector.
-
* @return this vector
-
*/
-
public Vector3f set(float x, float y, float z) {
-
this.x = x;
-
this.y = y;
-
this.z = z;
-
return this;
-
}
-
-
/**
-
* <code>set</code> sets the x,y,z values of the vector by copying the
-
* supplied vector.
-
*
-
* @param vect
-
* the vector to copy.
-
* @return this vector
-
*/
-
public Vector3f set(Vector3f vect) {
-
this.x = vect.x;
-
this.y = vect.y;
-
this.z = vect.z;
-
return this;
-
}
-
-
/**
-
*
-
* <code>add</code> adds a provided vector to this vector creating a
-
* resultant vector which is returned. If the provided vector is null, null
-
* is returned.<br/>
-
* 向量加法(不改变自身和参数)
-
*
-
* Neither 'this' nor 'vec' are modified.
-
*
-
* @param vec
-
* the vector to add to this.
-
* @return the resultant vector.
-
*/
-
public Vector3f add(Vector3f vec) {
-
if (null == vec) {
-
logger.warning("Provided vector is null, null returned.");
-
return null;
-
}
-
return new Vector3f(x + vec.x, y + vec.y, z + vec.z);
-
}
-
-
/**
-
*
-
* <code>add</code> adds the values of a provided vector storing the values
-
* in the supplied vector.<br/>
-
* 向量加法
-
*
-
* @param vec
-
* the vector to add to this
-
* @param result
-
* the vector to store the result in
-
* @return result returns the supplied result vector.
-
*/
-
public Vector3f add(Vector3f vec, Vector3f result) {
-
result.x = x + vec.x;
-
result.y = y + vec.y;
-
result.z = z + vec.z;
-
return result;
-
}
-
-
/**
-
* <code>addLocal</code> adds a provided vector to this vector internally,
-
* and returns a handle to this vector for easy chaining of calls. If the
-
* provided vector is null, null is returned.<br/>
-
* 向量加法(自加)
-
*
-
* @param vec
-
* the vector to add to this vector.
-
* @return this
-
*/
-
public Vector3f addLocal(Vector3f vec) {
-
if (null == vec) {
-
logger.warning("Provided vector is null, null returned.");
-
return null;
-
}
-
x += vec.x;
-
y += vec.y;
-
z += vec.z;
-
return this;
-
}
-
-
/**
-
*
-
* <code>add</code> adds the provided values to this vector, creating a new
-
* vector that is then returned.<br/>
-
* 向量加法
-
*
-
* @param addX
-
* the x value to add.
-
* @param addY
-
* the y value to add.
-
* @param addZ
-
* the z value to add.
-
* @return the result vector.
-
*/
-
public Vector3f add(float addX, float addY, float addZ) {
-
return new Vector3f(x + addX, y + addY, z + addZ);
-
}
-
-
/**
-
* <code>addLocal</code> adds the provided values to this vector internally,
-
* and returns a handle to this vector for easy chaining of calls.<br/>
-
* 向量加法(自加)
-
*
-
* @param addX
-
* value to add to x
-
* @param addY
-
* value to add to y
-
* @param addZ
-
* value to add to z
-
* @return this
-
*/
-
public Vector3f addLocal(float addX, float addY, float addZ) {
-
x += addX;
-
y += addY;
-
z += addZ;
-
return this;
-
}
-
-
/**
-
*
-
* <code>scaleAdd</code> multiplies this vector by a scalar then adds the
-
* given Vector3f.<br/>
-
* 向量加乘(先乘一个标量,再加一个向量)
-
*
-
* @param scalar
-
* the value to multiply this vector by.
-
* @param add
-
* the value to add
-
*/
-
public void scaleAdd(float scalar, Vector3f add) {
-
x = x * scalar + add.x;
-
y = y * scalar + add.y;
-
z = z * scalar + add.z;
-
}
-
-
/**
-
*
-
* <code>scaleAdd</code> multiplies the given vector by a scalar then adds
-
* the given vector.<br/>
-
* 向量加乘(将mult乘以标量scalar,再加上向量add,返回this)
-
*
-
* @param scalar
-
* the value to multiply this vector by.
-
* @param mult
-
* the value to multiply the scalar by
-
* @param add
-
* the value to add
-
*/
-
public void scaleAdd(float scalar, Vector3f mult, Vector3f add) {
-
this.x = mult.x * scalar + add.x;
-
this.y = mult.y * scalar + add.y;
-
this.z = mult.z * scalar + add.z;
-
}
-
-
/**
-
*
-
* <code>dot</code> calculates the dot product of this vector with a
-
* provided vector. If the provided vector is null, 0 is returned.<br/>
-
* 向量点乘
-
*
-
* @param vec
-
* the vector to dot with this vector.
-
* @return the resultant dot product of this vector and a given vector.
-
*/
-
public float dot(Vector3f vec) {
-
if (null == vec) {
-
logger.warning("Provided vector is null, 0 returned.");
-
return 0;
-
}
-
return x * vec.x + y * vec.y + z * vec.z;
-
}
-
-
/**
-
* Returns a new vector which is the cross product of this vector with the
-
* specified vector.<br/>
-
* 叉乘(不改变自身和参数)
-
* <P>
-
* Neither 'this' nor v are modified. The starting value of 'result'
-
* </P>
-
*
-
* @param v
-
* the vector to take the cross product of with this.
-
* @return the cross product vector.
-
*/
-
public Vector3f cross(Vector3f v) {
-
return cross(v, null);
-
}
-
-
/**
-
* <code>cross</code> calculates the cross product of this vector with a
-
* parameter vector v. The result is stored in <code>result</code><br/>
-
* 叉乘(this与v叉乘,结果存入result中。不改变自身和参数)
-
* <P>
-
* Neither 'this' nor v are modified. The starting value of 'result' (if
-
* any) is ignored.
-
* </P>
-
*
-
* @param v
-
* the vector to take the cross product of with this.
-
* @param result
-
* the vector to store the cross product result.
-
* @return result, after recieving the cross product vector.
-
*/
-
public Vector3f cross(Vector3f v, Vector3f result) {
-
return cross(v.x, v.y, v.z, result);
-
}
-
-
/**
-
* <code>cross</code> calculates the cross product of this vector with a
-
* Vector comprised of the specified other* elements. The result is stored
-
* in <code>result</code>, without modifying either 'this' or the 'other*'
-
* values.<br/>
-
* 叉乘(this与参数叉乘,结果存在参数result中)
-
*
-
* @param otherX
-
* x component of the vector to take the cross product of with
-
* this.
-
* @param otherY
-
* y component of the vector to take the cross product of with
-
* this.
-
* @param otherZ
-
* z component of the vector to take the cross product of with
-
* this.
-
* @param result
-
* the vector to store the cross product result.
-
* @return result, after recieving the cross product vector.
-
*/
-
public Vector3f cross(float otherX, float otherY, float otherZ,
-
Vector3f result) {
-
if (result == null)
-
result = new Vector3f();
-
float resX = ((y * otherZ) - (z * otherY));
-
float resY = ((z * otherX) - (x * otherZ));
-
float resZ = ((x * otherY) - (y * otherX));
-
result.set(resX, resY, resZ);
-
return result;
-
}
-
-
/**
-
* <code>crossLocal</code> calculates the cross product of this vector with
-
* a parameter vector v.<br/>
-
* 自身叉乘(this与参数叉乘,结果存入自身)
-
*
-
* @param v
-
* the vector to take the cross product of with this.
-
* @return this.
-
*/
-
public Vector3f crossLocal(Vector3f v) {
-
return crossLocal(v.x, v.y, v.z);
-
}
-
-
/**
-
* <code>crossLocal</code> calculates the cross product of this vector with
-
* a parameter vector v.<br/>
-
* 自身叉乘(this与参数叉乘,结果存入自身)
-
*
-
* @param otherX
-
* x component of the vector to take the cross product of with
-
* this.
-
* @param otherY
-
* y component of the vector to take the cross product of with
-
* this.
-
* @param otherZ
-
* z component of the vector to take the cross product of with
-
* this.
-
* @return this.
-
*/
-
public Vector3f crossLocal(float otherX, float otherY, float otherZ) {
-
float tempx = (y * otherZ) - (z * otherY);
-
float tempy = (z * otherX) - (x * otherZ);
-
z = (x * otherY) - (y * otherX);
-
x = tempx;
-
y = tempy;
-
return this;
-
}
-
-
/**
-
* <code>length</code> calculates the magnitude of this vector.<br/>
-
* 向量长度
-
*
-
* @return the length or magnitude of the vector.
-
*/
-
public float length() {
-
return FastMath.sqrt(lengthSquared());
-
}
-
-
/**
-
* <code>lengthSquared</code> calculates the squared value of the magnitude
-
* of the vector.<br/>
-
* 向量长度平方
-
*
-
* @return the magnitude squared of the vector.
-
*/
-
public float lengthSquared() {
-
return x * x + y * y + z * z;
-
}
-
-
/**
-
* <code>distanceSquared</code> calculates the distance squared between this
-
* vector and vector v.<br/>
-
* 向量this与参赛v间的距离平方
-
*
-
* @param v
-
* the second vector to determine the distance squared.
-
* @return the distance squared between the two vectors.
-
*/
-
public float distanceSquared(Vector3f v) {
-
double dx = x - v.x;
-
double dy = y - v.y;
-
double dz = z - v.z;
-
return (float) (dx * dx + dy * dy + dz * dz);
-
}
-
-
/**
-
* <code>distance</code> calculates the distance between this vector and
-
* vector v.<br/>
-
* 向量this与参赛v间的距离
-
*
-
* @param v
-
* the second vector to determine the distance.
-
* @return the distance between the two vectors.
-
*/
-
public float distance(Vector3f v) {
-
return FastMath.sqrt(distanceSquared(v));
-
}
-
-
/**
-
* <code>mult</code> multiplies this vector by a scalar. The resultant
-
* vector is returned. "this" is not modified.<br/>
-
* 向量标量乘
-
*
-
* @param scalar
-
* the value to multiply this vector by.
-
* @return the new vector.
-
*/
-
public Vector3f mult(float scalar) {
-
return new Vector3f(x * scalar, y * scalar, z * scalar);
-
}
-
-
/**
-
*
-
* <code>mult</code> multiplies this vector by a scalar. The resultant
-
* vector is supplied as the second parameter and returned. "this" is not
-
* modified.<br/>
-
* 向量标量乘,结果存入product。
-
*
-
* @param scalar
-
* the scalar to multiply this vector by.
-
* @param product
-
* the product to store the result in.
-
* @return product
-
*/
-
public Vector3f mult(float scalar, Vector3f product) {
-
if (null == product) {
-
product = new Vector3f();
-
}
-
-
product.x = x * scalar;
-
product.y = y * scalar;
-
product.z = z * scalar;
-
return product;
-
}
-
-
/**
-
* <code>multLocal</code> multiplies this vector by a scalar internally, and
-
* returns a handle to this vector for easy chaining of calls.<br/>
-
* 向量标量乘this
-
*
-
* @param scalar
-
* the value to multiply this vector by.
-
* @return this
-
*/
-
public Vector3f multLocal(float scalar) {
-
x *= scalar;
-
y *= scalar;
-
z *= scalar;
-
return this;
-
}
-
-
/**
-
* <code>multLocal</code> multiplies a provided vector to this vector
-
* internally, and returns a handle to this vector for easy chaining of
-
* calls. If the provided vector is null, null is returned. The provided
-
* 'vec' is not modified.<br/>
-
* 向量标量乘一个向量(对应的x、y、z分量分别相乘),结果改变this
-
*
-
* @param vec
-
* the vector to mult to this vector.
-
评论