true
if this vector has the same components as the argument vector
* v
, false otherwise
*/
public boolean equals(Vector3f v)
{
if (v == null)
return false;
return (this.x == v.x && this.y == v.y && this.z == v.z);
}
/**
* Compares this vector with the given vector.
* Note: tolerance is applied per component, thus all vectors * which equal this vector will form a box.
* @param v the vector to be used for compare * @param tolerance accepted tolerance * @returntrue
if this the components of this vector are equal to the argument vector
* v
with a tolerance of argument tolerance
, false otherwise
*/
public boolean equals(Vector3f v, float tolerance)
{
if (v == null)
return false;
return (Math.abs(this.x-v.x)true
if vector is {@link Float#NEGATIVE_INFINITY},
* {@link Float#POSITIVE_INFINITY} or {@link Float#NaN}
* @return true
if vector is {@link Float#NEGATIVE_INFINITY},
* {@link Float#POSITIVE_INFINITY} or {@link Float#NaN}.
*/
public boolean isInvalid()
{
return Float.isInfinite(x) || Float.isInfinite(y) || Float.isInfinite(z)
|| Float.isNaN(x) || Float.isNaN(y) || Float.isNaN(z);
}
/**
* Returns true
iff x exactly zero (0,0,0).
* @return true
iff x exactly zero (0,0,0).
*/
public boolean isZero()
{
return x==0f && y==0f && z==0f;
}
/**
* Returns true
if vector is nearly zero (0,0,0).
* @param tolerance accepted tolerance
* @return true
if vector is nearly zero (0,0,0).
*/
public boolean isZero(float tolerance)
{
return Math.abs(this.x)array
.
* @param array array which contains the new values
* @return this
*/
public Vector3f set(final float[] array)
{
this.x = array[0];
this.y = array[1];
this.z = array[2];
return this;
}
/**
* Negates all coordinates of this vector.
*
* @return this
*/
public Vector3f negate()
{
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
/**
* Returns the squared length of this vector.
*
* @return the squared length of this vector
*/
public float lengthSquared()
{
return (this.x * this.x + this.y * this.y + this.z * this.z);
}
/**
* Returns the length of this vector.
*
* @return the length of this vector
*/
public float length()
{
return (float)Math.sqrt(this.lengthSquared());
}
/**
* Calculates the vector cross product of this vector and the given vector.
*
* @param v the second vector
* @return this
*/
public Vector3f cross(final Vector3f v)
{
final float a = this.y * v.z - this.z * v.y;
final float b = this.z * v.x - this.x * v.z;
final float c = this.x * v.y - this.y * v.x;
this.x = a;
this.y = b;
this.z = c;
return this;
}
/**
* Calculates the vector cross product of the two given vectors.
* a x b = c
c is returned.
* @param a The first vector.
* @param b The second vector.
* @return A new vector, holding the cross product of a x b.
*/
public static Vector3f cross(final Vector3f a, final Vector3f b)
{
return new Vector3f(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
/**
* {@inheritDoc}
*/
public Object clone()
{
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
throw new RuntimeException("the roof is on fire", e);
}
}
/**
* {@inheritDoc}
*/
public String toString()
{
return "[" + this.x + "," + this.y + "," + this.z + "]";
}
/**
* Copies all components of this vector into the first 3 array components.
* @param dest array to be filled - may be null
* @return argument dest
or a new array filled with this
* instances components.
*/
public float[] toArray(float[] dest)
{
if (dest==null)
dest = new float[3];
dest[0] = this.x;
dest[1] = this.y;
dest[2] = this.z;
return dest;
}
/**
* Returns true
if this vector is normalized. If it's length is 1
.
* @return true
if this vector is normalized.
*/
public boolean isNormalized()
{
return this.lengthSquared()==1;
}
/**
* Returns true
if this vector is normalized.
* If it's length is 1
within a tolerance of argument tolerance
.
* @param tolerance amount of error to be ignores - must be positive!
* Note: the normal is computed by using {@link #lengthSquared()}, thus
* the following equation approximated.
* For a Vector3f v
:
* Correct:
return {@link Math#sqrt(double) sqrt}({@link #lengthSquared() * v.lengthSquared()})>1f-tolerance;* * Steps to a more numerical stable and faster test: *
* v.lengthSquared()>(1f-tolerance)^2 * <=> v.lengthSquared()>1f-2f*tolerance+tolerance^2;* Optimized and approximated result: *
return v.lengthSquared()>1f-2f*tolerance;* This test is infact less precise than the original test, * but seems to be reasonable, as a tolerance is assumed to be pretty small! * @return
true
if this vector is normalized.
*/
public boolean isNormalized(float tolerance)
{
assert tolerance>=0:"tolerance must be positive or 0";
float tmp1 = this.lengthSquared() - 1f;
float tmp2 = 2f * tolerance + tolerance * tolerance;
return tmp1 < tmp2 && -tmp1 < tmp2;
}
/**
* Returns true
if this vector is orthogonal to argument other
.
* @param other other vector
* @return true
if this vector is orthogonal to argument other
.
*/
public boolean isOrthogonal(Vector3f other)
{
return this.dot(other)==0;
}
/**
* Returns true
if this vector is orthogonal to argument other
.
* @param other other vector
* @param tolerance amount of error to be ignores - must be positive!
* @return true
if this vector is orthogonal to argument other
.
*/
public boolean isOrthogonal(Vector3f other, float tolerance)
{
assert tolerance>=0:"tolerance must be positive or 0";
return this.dot(other)>-tolerance;
}
}