Package toxi.geom

Class GVector

java.lang.Object
toxi.geom.GVector
All Implemented Interfaces:
Serializable, Cloneable

public class GVector extends Object implements Serializable, Cloneable
A double precision, general, dynamically-resizable, one-dimensional vector class.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GVector(double[] vector)
    Constructs a new GVector from the specified array elements.
    GVector(double[] vector, int length)
    Constructs a new GVector of the specified length and initializes it by copying the specified number of elements from the specified array.
    GVector(int length)
    Constructs a new GVector of the specified length with all vector elements initialized to 0.
    GVector(GVector vector)
    Constructs a new GVector from the specified vector.
    Constructs a new GVector and copies the initial values from the specified tuple.
    Constructs a new GVector and copies the initial values from the specified tuple.
    Constructs a new GVector and copies the initial values from the specified tuple.
  • Method Summary

    Modifier and Type
    Method
    Description
    final GVector
    Creates the vector sum of this vector and the given one (must be equal sized).
    final GVector
    addSelf(GVector vector)
    Sets the value of this vector to sum of itself and the specified vector
    final double
    Returns the (n-space) angle in radians between this vector and the vector parameter; the return value is constrained to the range [0,PI].
    final void
    backSolveLUD(GMatrix LU, GVector b, GVector permutation)
    LU Decomposition Back Solve; this method takes the LU matrix and the permutation vector produced by the GMatrix method LUD and solves the equation (LU)*x = b by placing the solution vector x into this vector.
    final void
    Solves for x in Ax = b, where x is this vector (nx1), A is mxn, b is mx1, and A = U*W*transpose(V); U,W,V must be precomputed and can be found by taking the singular value decomposition (SVD) of A using the method SVD found in the GMatrix class.
    Creates a new object of the same class as this object.
    final double
    Returns the dot product of this vector and vector v.
    boolean
    Returns true if the Object o1 is of type GMatrix and all of the data members of o1 are equal to the corresponding data members in this GMatrix.
    boolean
    equals(GVector vector1)
    Returns true if all of the data members of GVector vector1 are equal to the corresponding data members in this GVector.
    boolean
    equalsWithTolerance(GVector v, double tolerance)
    Returns true if the L-infinite distance between this vector and vector v is less than or equal to the tolerance parameter, otherwise returns false.
    final double
    get(int index)
    Retrieves the value at the specified index value of this vector.
    int
    Returns a hash code value based on the data values in this object.
    final GVector
    interpolateTo(GVector v, double alpha)
    Linearly interpolates this vector to the target vector and places the result into a new instance: result = this + (target-this)*alpha.
    final GVector
    interpolateTo(GVector v, double alpha, InterpolateStrategy strategy)
    Interpolates the vector towards the given target vector, using the given InterpolateStrategy.
    final GVector
    interpolateToSelf(GVector v, double alpha)
    Linearly interpolates this vector to the target vector and places result in this vector.
    final GVector
    interpolateToSelf(GVector v, double alpha, InterpolateStrategy strategy)
    Interpolates the vector towards the given target vector, using the given InterpolateStrategy.
    final void
    Negates the value of this vector: this = -this.
    final double
    Returns the square root of the sum of the squares of this vector (its length in n-dimensional space).
    final double
    Returns the sum of the squares of this vector (its length squared in n-dimensional space).
    final void
    mul(GMatrix m1, GVector v1)
    Multiplies matrix m1 times Vector v1 and places the result into this vector (this = m1*v1).
    final void
    mul(GVector v1, GMatrix m1)
    Multiplies the transpose of vector v1 (ie, v1 becomes a row vector with respect to the multiplication) times matrix m1 and places the result into this vector (this = transpose(v1)*m1).
    final void
    Normalizes this vector in place.
    final GVector
    scale(double s)
    Scales this vector by the scale factor s and returns result as new vector.
    final GVector
    Scales the values of this vector with the values of the given vector vector (this = this * vector).
    final GVector
    scaleSelf(double s)
    Scales this vector by the scale factor s.
    final GVector
    Scales the values of this vector with the values of the given vector vector (this = this * vector).
    final GVector
    set(double[] vector)
    Sets the values of this vector to the values found in the array parameter.
    final GVector
    set(GVector vector)
    Sets the value of this vector to the values found in vector vector.
    final GVector
    Sets the value of this vector to the values in tuple
    final GVector
    Sets the value of this vector to the values in tuple
    final GVector
    Sets the value of this vector to the values in tuple
    final GVector
    setElement(int index, double value)
    Modifies the value at the specified index of this vector.
    final GVector
    setSize(int length)
    Changes the size of this vector dynamically.
    final int
    Returns the number of elements in this vector.
    final GVector
    Creates the vector difference of this vector and the given one (must be equal sized).
    final GVector
    subSelf(GVector vector)
    Sets the value of this vector to the vector difference of itself and vector (this = this - vector).
    Returns a string that contains the values of this GVector.
    final GVector
    Sets all the values in this vector to zero.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • values

      public double[] values
  • Constructor Details

    • GVector

      public GVector(double[] vector, int length)
      Constructs a new GVector of the specified length and initializes it by copying the specified number of elements from the specified array. The array must contain at least length elements (i.e., vector.length >= length. The length of this new GVector is set to the specified length.
      Parameters:
      vector - The array from which the values will be copied.
      length - The number of values copied from the array.
    • GVector

      public GVector(double[] vector)
      Constructs a new GVector from the specified array elements. The length of this GVector is set to the length of the specified array. The array elements are copied into this new GVector.
      Parameters:
      vector - the values for the new GVector.
    • GVector

      public GVector(GVector vector)
      Constructs a new GVector from the specified vector. The vector elements are copied into this new GVector.
      Parameters:
      vector - the source GVector for this new GVector.
    • GVector

      public GVector(int length)
      Constructs a new GVector of the specified length with all vector elements initialized to 0.
      Parameters:
      length - the number of elements in this GVector.
    • GVector

      public GVector(ReadonlyVec2D v)
      Constructs a new GVector and copies the initial values from the specified tuple.
      Parameters:
      v - the source for the new GVector's initial values
    • GVector

      public GVector(ReadonlyVec3D v)
      Constructs a new GVector and copies the initial values from the specified tuple.
      Parameters:
      v - the source for the new GVector's initial values
    • GVector

      public GVector(ReadonlyVec4D v)
      Constructs a new GVector and copies the initial values from the specified tuple.
      Parameters:
      v - the source for the new GVector's initial values
  • Method Details

    • add

      public final GVector add(GVector v)
      Creates the vector sum of this vector and the given one (must be equal sized). Returns result as new vector.
      Parameters:
      v -
      Returns:
      new vector
    • addSelf

      public final GVector addSelf(GVector vector)
      Sets the value of this vector to sum of itself and the specified vector
      Parameters:
      vector - the second vector
      Returns:
      itself
    • angleBetween

      public final double angleBetween(GVector v)
      Returns the (n-space) angle in radians between this vector and the vector parameter; the return value is constrained to the range [0,PI].
      Parameters:
      v - The other vector
      Returns:
      The angle in radians in the range [0,PI]
    • backSolveLUD

      public final void backSolveLUD(GMatrix LU, GVector b, GVector permutation)
      LU Decomposition Back Solve; this method takes the LU matrix and the permutation vector produced by the GMatrix method LUD and solves the equation (LU)*x = b by placing the solution vector x into this vector. This vector should be the same length or longer than b.
      Parameters:
      LU - The matrix into which the lower and upper decompostions have been placed
      b - The b vector in the equation (LU)*x = b
      permutation - The row permuations that were necessary to produce the LU matrix parameter
    • backSolveSVD

      public final void backSolveSVD(GMatrix U, GMatrix W, GMatrix V, GVector b)
      Solves for x in Ax = b, where x is this vector (nx1), A is mxn, b is mx1, and A = U*W*transpose(V); U,W,V must be precomputed and can be found by taking the singular value decomposition (SVD) of A using the method SVD found in the GMatrix class.
      Parameters:
      U - The U matrix produced by the GMatrix method SVD
      W - The W matrix produced by the GMatrix method SVD
      V - The V matrix produced by the GMatrix method SVD
      b - The b vector in the linear equation Ax = b
    • clone

      public Object clone()
      Creates a new object of the same class as this object.
      Returns:
      a clone of this instance.
      Throws:
      OutOfMemoryError - if there is not enough memory.
      Since:
      vecmath 1.3
      See Also:
    • dot

      public final double dot(GVector v)
      Returns the dot product of this vector and vector v.
      Parameters:
      v - the other vector
      Returns:
      the dot product of this and v
    • equals

      public boolean equals(GVector vector1)
      Returns true if all of the data members of GVector vector1 are equal to the corresponding data members in this GVector.
      Parameters:
      vector1 - The vector with which the comparison is made.
      Returns:
      true or false
    • equals

      public boolean equals(Object o1)
      Returns true if the Object o1 is of type GMatrix and all of the data members of o1 are equal to the corresponding data members in this GMatrix.
      Overrides:
      equals in class Object
      Parameters:
      o1 - The object with which the comparison is made.
      Returns:
      true or false
    • equalsWithTolerance

      public boolean equalsWithTolerance(GVector v, double tolerance)
      Returns true if the L-infinite distance between this vector and vector v is less than or equal to the tolerance parameter, otherwise returns false. The L-infinite distance is equal to MAX[abs(x1-x2), abs(y1-y2), . . . ].
      Parameters:
      v - The vector to be compared to this vector
      tolerance - the threshold value
    • get

      public final double get(int index)
      Retrieves the value at the specified index value of this vector.
      Parameters:
      index - the index of the element to retrieve (zero indexed)
      Returns:
      the value at the indexed element
    • hashCode

      public int hashCode()
      Returns a hash code value based on the data values in this object. Two different GVector objects with identical data values (i.e., GVector.equals returns true) will return the same hash number. Two GVector objects with different data members may return the same hash value, although this is not likely.
      Overrides:
      hashCode in class Object
      Returns:
      the integer hash code value
    • interpolateTo

      public final GVector interpolateTo(GVector v, double alpha)
      Linearly interpolates this vector to the target vector and places the result into a new instance: result = this + (target-this)*alpha. The target vector needs to be equal sized.
      Parameters:
      v - the target vector
      alpha - the alpha interpolation parameter
      Returns:
      result as new vector
    • interpolateTo

      public final GVector interpolateTo(GVector v, double alpha, InterpolateStrategy strategy)
      Interpolates the vector towards the given target vector, using the given InterpolateStrategy. The target vector needs to be equal sized.
      Parameters:
      v - target vector
      alpha - interpolation factor (should be in the range 0..1)
      strategy - InterpolateStrategy instance
      Returns:
      result as new vector
    • interpolateToSelf

      public final GVector interpolateToSelf(GVector v, double alpha)
      Linearly interpolates this vector to the target vector and places result in this vector. result = this + (target-this)*alpha. The target vector needs to be equal sized.
      Parameters:
      v - the target vector
      alpha - the alpha interpolation parameter
    • interpolateToSelf

      public final GVector interpolateToSelf(GVector v, double alpha, InterpolateStrategy strategy)
      Interpolates the vector towards the given target vector, using the given InterpolateStrategy. The target vector needs to be equal sized.
      Parameters:
      v - target vector
      alpha - interpolation factor (should be in the range 0..1)
      strategy - InterpolateStrategy instance
      Returns:
      itself, result overrides current vector
    • invert

      public final void invert()
      Negates the value of this vector: this = -this.
    • magnitude

      public final double magnitude()
      Returns the square root of the sum of the squares of this vector (its length in n-dimensional space).
      Returns:
      length of this vector
    • magSquared

      public final double magSquared()
      Returns the sum of the squares of this vector (its length squared in n-dimensional space).
      Returns:
      length squared of this vector
    • mul

      public final void mul(GMatrix m1, GVector v1)
      Multiplies matrix m1 times Vector v1 and places the result into this vector (this = m1*v1).
      Parameters:
      m1 - The matrix in the multiplication
      v1 - The vector that is multiplied
    • mul

      public final void mul(GVector v1, GMatrix m1)
      Multiplies the transpose of vector v1 (ie, v1 becomes a row vector with respect to the multiplication) times matrix m1 and places the result into this vector (this = transpose(v1)*m1). The result is technically a row vector, but the GVector class only knows about column vectors, and so the result is stored as a column vector.
      Parameters:
      m1 - The matrix in the multiplication
      v1 - The vector that is temporarily transposed
    • normalize

      public final void normalize()
      Normalizes this vector in place.
    • scale

      public final GVector scale(double s)
      Scales this vector by the scale factor s and returns result as new vector.
      Parameters:
      s - the scalar value
      Returns:
      new vector
    • scale

      public final GVector scale(GVector v)
      Scales the values of this vector with the values of the given vector vector (this = this * vector). Returns result as new vector.
      Parameters:
      v - scale vector
      Returns:
      new vector
    • scaleSelf

      public final GVector scaleSelf(double s)
      Scales this vector by the scale factor s.
      Parameters:
      s - the scalar value
      Returns:
      itself
    • scaleSelf

      public final GVector scaleSelf(GVector v)
      Scales the values of this vector with the values of the given vector vector (this = this * vector).
      Parameters:
      v - scale vector
      Returns:
      itself
    • set

      public final GVector set(double[] vector)
      Sets the values of this vector to the values found in the array parameter. If the array is shorter than the number of values in this vector the remaining values are zeroed. If the array is longer, only the first values up to to the vector length are copied.
      Parameters:
      vector - the source array
    • set

      public final GVector set(GVector vector)
      Sets the value of this vector to the values found in vector vector.
      Parameters:
      vector - the source vector
    • set

      public final GVector set(ReadonlyVec2D tuple)
      Sets the value of this vector to the values in tuple
      Parameters:
      tuple - the source for the new GVector's new values
    • set

      public final GVector set(ReadonlyVec3D tuple)
      Sets the value of this vector to the values in tuple
      Parameters:
      tuple - the source for the new GVector's new values
    • set

      public final GVector set(ReadonlyVec4D tuple)
      Sets the value of this vector to the values in tuple
      Parameters:
      tuple - the source for the new GVector's new values
      Returns:
      itself
    • setElement

      public final GVector setElement(int index, double value)
      Modifies the value at the specified index of this vector.
      Parameters:
      index - the index if the element to modify (zero indexed)
      value - the new vector element value
    • setSize

      public final GVector setSize(int length)
      Changes the size of this vector dynamically. If the size is increased no data values will be lost. If the size is decreased, only those data values whose vector positions were eliminated will be lost.
      Parameters:
      length - number of desired elements in this vector
    • size

      public final int size()
      Returns the number of elements in this vector.
      Returns:
      number of elements in this vector
    • sub

      public final GVector sub(GVector v)
      Creates the vector difference of this vector and the given one (must be equal sized). Returns result as new vector.
      Parameters:
      v -
      Returns:
      new vector
    • subSelf

      public final GVector subSelf(GVector vector)
      Sets the value of this vector to the vector difference of itself and vector (this = this - vector).
      Parameters:
      vector - the other vector
    • toString

      public String toString()
      Returns a string that contains the values of this GVector.
      Overrides:
      toString in class Object
      Returns:
      the String representation
    • zero

      public final GVector zero()
      Sets all the values in this vector to zero.