com.threed.jpct
Class SimpleVector

java.lang.Object
  extended by com.threed.jpct.SimpleVector
All Implemented Interfaces:
java.io.Serializable

public class SimpleVector
extends java.lang.Object
implements java.io.Serializable

SimpleVector is a class that represents a basic three-dimensional vector. Almost every passing of vectors is done using SimpleVector or at least a SimpleVector-variant is offered (sometimes (float x, float y, float z) is also offered for historical and/or performance reasons).

See Also:
Serialized Form

Field Summary
static SimpleVector ORIGIN
          A SimpleVector representing the origin (0,0,0)
 float x
          The x component of the vector
 float y
          The y component of the vector
 float z
          The z component of the vector
 
Constructor Summary
SimpleVector()
          Creates a new SimpleVector and sets the x,y and z components to zero
SimpleVector(double x, double y, double z)
          Creates a new SimpleVector by setting the x,y and z components of the vector.
SimpleVector(float[] vector)
          Creates a new SimpleVector from an array[3] of floats
SimpleVector(float x, float y, float z)
          Creates a new SimpleVector by setting the x,y and z components of the vector
SimpleVector(SimpleVector s)
          Creates a new SimpleVector from an existing one
 
Method Summary
 void add(SimpleVector vec)
          Adds another SimpleVector to this SimpleVector without creating a new instance.
 SimpleVector calcAdd(SimpleVector vec)
          Add two SimpleVectors to a new SimpleVector.
 float calcAngle(SimpleVector vec)
          Calculates the angle between this SimpleVector and another one.
 float calcAngleFast(SimpleVector vec)
          Calculates the angle between this SimpleVector and another one.
 SimpleVector calcCross(SimpleVector vec)
          Calculates the cross product of two SimpleVectors.
 float calcDot(SimpleVector vec)
          Calculates the dot product of two SimpleVectors.
 SimpleVector calcSub(SimpleVector vec)
          Calculates the difference vector of two SimpleVectors.
static SimpleVector create()
          Another way to create a SimpleVector.
static SimpleVector create(float x, float y, float z)
          Another way to create a SimpleVector.
static SimpleVector create(SimpleVector sv)
          Another way to create a SimpleVector.
 float distance(SimpleVector pos)
          Returns the distance between this SimpleVector and another position vector.
 boolean equals(java.lang.Object obj)
          Overrides the equals()-method from java.lang.Object.
 Matrix getRotationMatrix()
          Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
 Matrix getRotationMatrix(Matrix mat)
          Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
 Matrix getRotationMatrix(Matrix mat, SimpleVector up)
          Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
 Matrix getRotationMatrix(SimpleVector up)
          Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
 int hashCode()
          Overrides hashCode() from java.lang.Object.
 float length()
          Gives the length of this SimpleVector
 void makeEqualLength(SimpleVector vec)
          Makes this SimpleVector the length of another SimpleVector, but only if its longer than this.
 void matMul(Matrix mat)
          Multiplies the SimpleVector with a matrix, i.e. it applies the matrix's transformation to the SimpleVector.
 SimpleVector normalize()
          Normalizes a SimpleVector and returns a normalized vector as a new SimpleVector.
 SimpleVector normalize(SimpleVector sv)
          Normalizes a SimpleVector and returns a normalized vector as a new SimpleVector.
 SimpleVector reflect(SimpleVector faceNormal)
          Reflects this vector on a plane with the given normal.
 void rotate(Matrix mat)
          Similar to matMul(), but uses only the rotational part of the matrix.
 SimpleVector rotate(SimpleVector rotVector)
          Rotates this SimpleVector using the angles (x,y,z) of rotVector.
 void rotateAxis(SimpleVector axis, float angle)
          Rotates the vector around an arbitrary axis.
 void rotateX(float angle)
          Rotates the SimpleVector around the x-axis.
 void rotateY(float angle)
          Rotates the SimpleVector around the y-axis.
 void rotateZ(float angle)
          Rotates the SimpleVector around the z-axis.
 void scalarMul(float scalar)
          Multiplies the SimpleVector with a scalar.
 void set(float x, float y, float z)
          Sets the x,y and z values of the SimpleVector to the given floats.
 void set(SimpleVector s)
          Sets the x,y and z values of this SimpleVector to the ones of the given SimpleVector.
 void sub(SimpleVector vec)
          Subtracts another SimpleVector from this SimpleVector without creating a new instance.
 float[] toArray()
          Returns an float[3]-array containing the components of the vector so that [0]=x, [1]=y and [2]=z.
 java.lang.String toString()
          Returns this SimpleVector as a String.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ORIGIN

public static final SimpleVector ORIGIN
A SimpleVector representing the origin (0,0,0)


x

public float x
The x component of the vector


y

public float y
The y component of the vector


z

public float z
The z component of the vector

Constructor Detail

SimpleVector

public SimpleVector(float x,
                    float y,
                    float z)
Creates a new SimpleVector by setting the x,y and z components of the vector

Parameters:
x - the x component
y - the y component
z - the z component

SimpleVector

public SimpleVector(double x,
                    double y,
                    double z)
Creates a new SimpleVector by setting the x,y and z components of the vector.
This method takes doubles as arguments which are internally casted into floats.

Parameters:
x - the x component
y - the y component
z - the z component

SimpleVector

public SimpleVector(SimpleVector s)
Creates a new SimpleVector from an existing one

Parameters:
s - the SimpleVector to "copy"

SimpleVector

public SimpleVector()
Creates a new SimpleVector and sets the x,y and z components to zero


SimpleVector

public SimpleVector(float[] vector)
Creates a new SimpleVector from an array[3] of floats

Parameters:
vector - the array containing the x, y and z components of the vector in [0],[1] and [2].
Method Detail

create

public static SimpleVector create()
Another way to create a SimpleVector.

Returns:
a SimpleVector

create

public static SimpleVector create(SimpleVector sv)
Another way to create a SimpleVector.

Parameters:
sv - the source vector
Returns:
a SimpleVector

create

public static SimpleVector create(float x,
                                  float y,
                                  float z)
Another way to create a SimpleVector.

Parameters:
x - the x component
y - the y component
z - the z component
Returns:
a SimpleVector

set

public void set(float x,
                float y,
                float z)
Sets the x,y and z values of the SimpleVector to the given floats.


set

public void set(SimpleVector s)
Sets the x,y and z values of this SimpleVector to the ones of the given SimpleVector.


toArray

public float[] toArray()
Returns an float[3]-array containing the components of the vector so that [0]=x, [1]=y and [2]=z. This method can be used to use SimpleVectors for methods that requires float[3]-arrays. The returned array is allowed to be the same instance over different calls.

Returns:
a float[3]-array

toString

public java.lang.String toString()
Returns this SimpleVector as a String. Useful for debugging.

Overrides:
toString in class java.lang.Object
Returns:
the vector data as String

equals

public boolean equals(java.lang.Object obj)
Overrides the equals()-method from java.lang.Object. Two SimpleVectors are equal, if their x,y and z components are equal.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the SimpleVector that should be compared to this one
Returns:
equal or not

hashCode

public int hashCode()
Overrides hashCode() from java.lang.Object. This is required when overriding equals(), which this implementation does, so...

Overrides:
hashCode in class java.lang.Object
Returns:
the hashcode

rotate

public SimpleVector rotate(SimpleVector rotVector)
Rotates this SimpleVector using the angles (x,y,z) of rotVector. The result is a new SimpleVector. If you want to rotate the SimpleVector itself without creating a new one, use rotateX(), rotateY() and rotateZ() instead.

Parameters:
rotVector - the rotation vector containing the angles for the rotations around x,y and z axis.
Returns:
the rotated vector

rotate

public void rotate(Matrix mat)
Similar to matMul(), but uses only the rotational part of the matrix. Useful, if your matrix isn't a pure rotation- but a combined transformation matrix of some kind.

Parameters:
mat - Matrix the rotation matrix
See Also:
matMul(com.threed.jpct.Matrix)

rotateAxis

public void rotateAxis(SimpleVector axis,
                       float angle)
Rotates the vector around an arbitrary axis. The method is more powerful than the normal rotate-around-an-axis methods, but also a bit slower.

Parameters:
axis - a direction-vector pointing into the axis direction
angle - the angle of the rotation

reflect

public SimpleVector reflect(SimpleVector faceNormal)
Reflects this vector on a plane with the given normal.

Parameters:
faceNormal - the face normal of the reflective plane
Returns:
the reflected vector

rotateX

public void rotateX(float angle)
Rotates the SimpleVector around the x-axis. This method modifies the SimpleVector itself (unlike rotate()).

Parameters:
angle - the angle

rotateY

public void rotateY(float angle)
Rotates the SimpleVector around the y-axis. This method modifies the SimpleVector itself (unlike rotate()).

Parameters:
angle - the angle

rotateZ

public void rotateZ(float angle)
Rotates the SimpleVector around the z-axis. This method modifies the SimpleVector itself (unlike rotate()).

Parameters:
angle - the angle

normalize

public SimpleVector normalize()
Normalizes a SimpleVector and returns a normalized vector as a new SimpleVector. To reach a higher numerical accuracy, this method works with doubles instead of float internally.

Returns:
the normalized vector

normalize

public SimpleVector normalize(SimpleVector sv)
Normalizes a SimpleVector and returns a normalized vector as a new SimpleVector. To reach a higher numerical accuracy, this method works with doubles instead of float internally.
This variant takes a SimpleVector to fill, i.e. it avoids the creation of a new SimpleVector.

Parameters:
sv - the SimpleVector to fill
Returns:
the normalized vector (i.e. a filled sv)

length

public float length()
Gives the length of this SimpleVector

Returns:
the length

calcCross

public SimpleVector calcCross(SimpleVector vec)
Calculates the cross product of two SimpleVectors.

Parameters:
vec - the second SimpleVector
Returns:
the resulting SimpleVector (the cross product of "this" and "vec")

calcDot

public float calcDot(SimpleVector vec)
Calculates the dot product of two SimpleVectors.

Parameters:
vec - the second SimpleVector
Returns:
the dot product of "this" and "vec"

calcSub

public SimpleVector calcSub(SimpleVector vec)
Calculates the difference vector of two SimpleVectors.

Parameters:
vec - the second SimpleVector
Returns:
the difference vector between "this" and "vec"

calcAdd

public SimpleVector calcAdd(SimpleVector vec)
Add two SimpleVectors to a new SimpleVector.

Parameters:
vec - the second SimpleVector
Returns:
the result

distance

public float distance(SimpleVector pos)
Returns the distance between this SimpleVector and another position vector.

Returns:
the distance

calcAngleFast

public float calcAngleFast(SimpleVector vec)
Calculates the angle between this SimpleVector and another one.
This method is faster than the "normal" method, but less accurate.

Parameters:
vec - the other vector
Returns:
the angle

calcAngle

public float calcAngle(SimpleVector vec)
Calculates the angle between this SimpleVector and another one.

Parameters:
vec - the other vector
Returns:
the angle

scalarMul

public void scalarMul(float scalar)
Multiplies the SimpleVector with a scalar.

Parameters:
scalar - the scalar to multiply with

matMul

public void matMul(Matrix mat)
Multiplies the SimpleVector with a matrix, i.e. it applies the matrix's transformation to the SimpleVector.

Parameters:
mat - Matrix the matrix (usually a rotation or a translation matrix)

add

public void add(SimpleVector vec)
Adds another SimpleVector to this SimpleVector without creating a new instance.

Parameters:
vec - the SimpleVector to add

sub

public void sub(SimpleVector vec)
Subtracts another SimpleVector from this SimpleVector without creating a new instance.

Parameters:
vec - the SimpleVector to subtract

makeEqualLength

public void makeEqualLength(SimpleVector vec)
Makes this SimpleVector the length of another SimpleVector, but only if its longer than this.

Parameters:
vec - the SimpleVector that defines the length

getRotationMatrix

public Matrix getRotationMatrix()
Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.

Returns:
Matrix the rotation matrix

getRotationMatrix

public Matrix getRotationMatrix(SimpleVector up)
Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.

Parameters:
up - the up vector used in the calculation
Returns:
Matrix the rotation matrix

getRotationMatrix

public Matrix getRotationMatrix(Matrix mat)
Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
This method takes a matrix to fill, i.e. it doesn't create a new one for each call.

Parameters:
mat - the Matrix to fill
Returns:
Matrix the rotation matrix (i.e. a filled mat)

getRotationMatrix

public Matrix getRotationMatrix(Matrix mat,
                                SimpleVector up)
Creates a rotation matrix that can be used to rotate a vector pointing to (0,0,1) to match the direction of this vector.
This method takes a matrix to fill, i.e. it doesn't create a new one for each call.

Parameters:
up - the up vector used in the calculation
mat - the Matrix to fill
Returns:
Matrix the rotation matrix (i.e. a filled mat)