org.ode4j.math
Class DVector3

java.lang.Object
  extended by org.ode4j.math.DVector3
All Implemented Interfaces:
DVector3C

public class DVector3
extends java.lang.Object
implements DVector3C

This class provides functionality for DVector3 math.

Most methods have a prefix indicating whether the (this) object will be modified or not. All methods starting with "eq" will write the result into (this). All methods starting with "re" return a new Object containing the result. For all other methods the behaviour should be obvious.

Performance considerations: - Each class implements its own strictly typed methods. Using generic methods in super-classes showed to slow down operations like add() or set() by a factor of ~10 (SUN JDK 6 on SuSE 11.1 64bit). - Using complex methods (e.g. v0.sum(v1, v2, s2)) showed to be about 2 times faster than concatenating methods, e.g. v0.set(v2).scale(s2).add(v1).

Author:
Tilmann Zaeschke

Field Summary
static int CURRENT_LENGTH
           
static DVector3C ZERO
           
 
Constructor Summary
DVector3()
           
DVector3(double[] v2)
           
DVector3(double i, double j, double k)
           
DVector3(DVector3C v2)
           
 
Method Summary
 DVector3 add(double a, double b, double c)
           
 DVector3 add(DVector3C v2)
          Adds v2 to the current vector.
 void add(int i, double d)
           
 void add0(double d)
           
 void add1(double d)
           
 void add2(double d)
           
 DVector3 addScaled(DVector3C v2, double d)
          Scales the first parameter with the second and then adds the result to the current vector.
 DVector3 clone()
           
 int dim()
           
 double distance(DVector3C a)
          Distance between this vector and a.
 double dot(DVector3C b)
          Return the 'dot' product of two vectors.
 double dot(DVector3View b)
          Return the 'dot' product of two vectors.
 double dotCol(DMatrix3C m, int col)
          Calculates the dot product of this vector with the specified column of the given Matrix.
 void eqAbs()
           
 void eqCross(DVector3C b, DVector3C c)
          Set this vector = b x c.
 DVector3 eqDiff(DVector3C v2, DVector3C v3)
          Sets the current vector v0 = v2 - v3.
 void eqIdentity()
           
 void eqProd(DMatrix3C m, DVector3C v2)
          Calculates the ordinary matrix product for a 3x3 Matrix and a 3-Vector.
 DVector3 eqSum(DMatrix3.DVector3ColView v2, double s2, DMatrix3.DVector3ColView v3, double s3)
          Convenience function that performs: this = v2*s2 + v3*s3
 DVector3 eqSum(DMatrix3.DVector3ColView v2, double s2, DVector3C v3, double s3)
          Convenience function that performs: this = v2*s2 + v3*s3
 DVector3 eqSum(DVector3C v2, DMatrix3.DVector3ColView v3, double s3)
          Convenience function that performs: this = v2 + v3*s3
 DVector3 eqSum(DVector3C v2, double s2, DVector3C v3, double s3)
          Convenience function that performs: this = v2*s2 + v3*s3
 DVector3 eqSum(DVector3C v2, DVector3C v3)
          Sets current vector = v2 + v3.
 DVector3 eqSum(DVector3C v2, DVector3C v3, double s3)
          Convenience function that performs: this = v2 + v3*s3
 boolean equals(java.lang.Object obj)
          Any implementation of DVector3I will return true if get0(), get1() and get2() return the same values.
 void eqZero()
           
 double get(int i)
           
 double get0()
           
 double get1()
           
 double get2()
           
 int hashCode()
           
 boolean isEq(DVector3 a)
           
 double length()
           
 double lengthSquared()
           
 void normalize()
          this may be called for vectors `a' with extremely small magnitude, for example the result of a cross product on two nearly perpendicular vectors.
 DVector3C reAdd(DVector3C c)
           
 DVector3 reScale(double d)
           
 DVector3 reSub(DVector3C v2)
          Return a new vector v0 = v(this) - v2.
 boolean safeNormalize()
          this may be called for vectors `a' with extremely small magnitude, for example the result of a cross product on two nearly perpendicular vectors.
 DVector3 scale(double s)
           
 DVector3 scale(double a, double b, double c)
           
 DVector3 scale(DVector3C v2)
           
 void scale(int i, double d)
           
 DVector3 set(DMatrix3.DVector3ColView v2)
           
 DVector3 set(double[] v2)
           
 DVector3 set(double x, double y, double z)
           
 DVector3 set(DVector3C v2)
           
 void set(float[] v2)
           
 void set(int i, double d)
           
 void set0(double d)
           
 void set1(double d)
           
 void set2(double d)
           
 void setIdentity()
           
 void setZero()
           
 DVector3 sub(double a, double b, double c)
           
 DVector3 sub(DVector3C v2)
           
 float[] toFloatArray()
           
 float[] toFloatArray4()
           
 java.lang.String toString()
           
 void wrapAdd(double[] array, int pos)
          Adds the content of this vector to the elements of array at position pos.
 void wrapSet(double[] array, int pos)
          Writes the content of this vector into array at position pos.
 void wrapSub(double[] array, int pos)
          Subtracts the content of this vector from the elements of array at position pos.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final DVector3C ZERO

CURRENT_LENGTH

public static final int CURRENT_LENGTH
See Also:
Constant Field Values
Constructor Detail

DVector3

public DVector3()

DVector3

public DVector3(DVector3C v2)

DVector3

public DVector3(double[] v2)

DVector3

public DVector3(double i,
                double j,
                double k)
Method Detail

set

public final DVector3 set(double[] v2)

set

public void set(float[] v2)

set

public final DVector3 set(double x,
                          double y,
                          double z)

set

public final DVector3 set(DVector3C v2)

set

public final DVector3 set(DMatrix3.DVector3ColView v2)

clone

public DVector3 clone()
Specified by:
clone in interface DVector3C
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

set0

public final void set0(double d)

set1

public final void set1(double d)

set2

public final void set2(double d)

get0

public final double get0()
Specified by:
get0 in interface DVector3C

get1

public final double get1()
Specified by:
get1 in interface DVector3C

get2

public final double get2()
Specified by:
get2 in interface DVector3C

add

public final DVector3 add(double a,
                          double b,
                          double c)

add

public final DVector3 add(DVector3C v2)
Adds v2 to the current vector.

Parameters:
v2 -
Returns:
Current vector.

eqSum

public final DVector3 eqSum(DVector3C v2,
                            DVector3C v3)
Sets current vector = v2 + v3.

Parameters:
v2 -
v3 -
Returns:
Current vector.

eqSum

public final DVector3 eqSum(DVector3C v2,
                            double s2,
                            DVector3C v3,
                            double s3)
Convenience function that performs: this = v2*s2 + v3*s3

Returns:
this

eqSum

public final DVector3 eqSum(DMatrix3.DVector3ColView v2,
                            double s2,
                            DVector3C v3,
                            double s3)
Convenience function that performs: this = v2*s2 + v3*s3

Returns:
this

eqSum

public final DVector3 eqSum(DMatrix3.DVector3ColView v2,
                            double s2,
                            DMatrix3.DVector3ColView v3,
                            double s3)
Convenience function that performs: this = v2*s2 + v3*s3

Returns:
this

eqSum

public final DVector3 eqSum(DVector3C v2,
                            DVector3C v3,
                            double s3)
Convenience function that performs: this = v2 + v3*s3

Returns:
this

eqSum

public final DVector3 eqSum(DVector3C v2,
                            DMatrix3.DVector3ColView v3,
                            double s3)
Convenience function that performs: this = v2 + v3*s3

Returns:
this

sub

public final DVector3 sub(double a,
                          double b,
                          double c)

sub

public final DVector3 sub(DVector3C v2)

scale

public final DVector3 scale(double a,
                            double b,
                            double c)

scale

public final DVector3 scale(double s)

scale

public final DVector3 scale(DVector3C v2)

dot

public final double dot(DVector3C b)
Return the 'dot' product of two vectors. r = a0*b0 + a1*b1 + a2*b2;

Specified by:
dot in interface DVector3C
Parameters:
b -
Returns:
(this) * b
See Also:
dot(DVector3C)

dot

public final double dot(DVector3View b)
Return the 'dot' product of two vectors. r = a0*b0 + a1*b1 + a2*b2;

Specified by:
dot in interface DVector3C
Parameters:
b -
Returns:
(this) * b
See Also:
dot(DVector3C)

eqDiff

public final DVector3 eqDiff(DVector3C v2,
                             DVector3C v3)
Sets the current vector v0 = v2 - v3.

Parameters:
v2 -
v3 -

reSub

public final DVector3 reSub(DVector3C v2)
Return a new vector v0 = v(this) - v2.

Specified by:
reSub in interface DVector3C
Parameters:
v2 -
See Also:
reSub(DVector3C)

safeNormalize

public final boolean safeNormalize()
this may be called for vectors `a' with extremely small magnitude, for example the result of a cross product on two nearly perpendicular vectors. we must be robust to these small vectors. to prevent numerical error, first find the component a[i] with the largest magnitude and then scale all the components by 1/a[i]. then we can compute the length of `a' and scale the components by 1/l. this has been verified to work with vectors containing the smallest representable numbers. This method returns (1,0,0) if no normal can be determined.


normalize

public final void normalize()
this may be called for vectors `a' with extremely small magnitude, for example the result of a cross product on two nearly perpendicular vectors. we must be robust to these small vectors. to prevent numerical error, first find the component a[i] with the largest magnitude and then scale all the components by 1/a[i]. then we can compute the length of `a' and scale the components by 1/l. this has been verified to work with vectors containing the smallest representable numbers. This method throws an IllegalArgumentEception if no normal can be determined.


distance

public final double distance(DVector3C a)
Distance between this vector and a.

Specified by:
distance in interface DVector3C
Parameters:
a -
Returns:
distance
See Also:
distance(DVector3C)

isEq

public final boolean isEq(DVector3 a)

eqAbs

public final void eqAbs()

add0

public final void add0(double d)

add1

public final void add1(double d)

add2

public final void add2(double d)

dim

public final int dim()
Returns:
'3'.

reAdd

public final DVector3C reAdd(DVector3C c)

wrapSet

public final void wrapSet(double[] array,
                          int pos)
Writes the content of this vector into array at position pos.

Parameters:
array -
pos -

wrapAdd

public final void wrapAdd(double[] array,
                          int pos)
Adds the content of this vector to the elements of array at position pos.

Parameters:
array -
pos -

wrapSub

public final void wrapSub(double[] array,
                          int pos)
Subtracts the content of this vector from the elements of array at position pos.

Parameters:
array -
pos -

reScale

public final DVector3 reScale(double d)
Specified by:
reScale in interface DVector3C

eqZero

public final void eqZero()

setZero

public final void setZero()

eqIdentity

public final void eqIdentity()

setIdentity

public final void setIdentity()

toFloatArray4

public final float[] toFloatArray4()
Specified by:
toFloatArray4 in interface DVector3C

length

public final double length()
Specified by:
length in interface DVector3C
Returns:
The geometric length of this vector.

lengthSquared

public final double lengthSquared()
Specified by:
lengthSquared in interface DVector3C

get

public final double get(int i)
Specified by:
get in interface DVector3C
Parameters:
i - The row to return [0, 1, 2].

toFloatArray

public final float[] toFloatArray()
Specified by:
toFloatArray in interface DVector3C

set

public final void set(int i,
                      double d)

scale

public final void scale(int i,
                        double d)

add

public final void add(int i,
                      double d)

dotCol

public final double dotCol(DMatrix3C m,
                           int col)
Calculates the dot product of this vector with the specified column of the given Matrix.

Specified by:
dotCol in interface DVector3C
Parameters:
m -
col -

equals

public boolean equals(java.lang.Object obj)
Any implementation of DVector3I will return true if get0(), get1() and get2() return the same values.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

addScaled

public final DVector3 addScaled(DVector3C v2,
                                double d)
Scales the first parameter with the second and then adds the result to the current vector.


eqCross

public final void eqCross(DVector3C b,
                          DVector3C c)
Set this vector = b x c.

Parameters:
b -
c -

eqProd

public final void eqProd(DMatrix3C m,
                         DVector3C v2)
Calculates the ordinary matrix product for a 3x3 Matrix and a 3-Vector.
 a0 = m00*v0 + m01*v1 + m02*v2 
 a1 = m10*v0 + m11*v1 + m12*v2 
 a2 = m20*v0 + m21*v1 + m22*v2