Class Vector4

java.lang.Object
civitas.celestis.util.tuple.Double4
civitas.celestis.math.vector.Vector4
All Implemented Interfaces:
Vector<Vector4>, BaseTuple<Double>, DoubleTuple, Serializable

public class Vector4 extends Double4 implements Vector<Vector4>
An immutable four-dimensional vector which uses the type double.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector4
    The negative W unit vector.
    static final Vector4
    The negative X unit vector.
    static final Vector4
    The negative Y unit vector.
    static final Vector4
    The negative Z unit vector.
    static final Vector4
    The positive W unit vector.
    static final Vector4
    The positive X unit vector.
    static final Vector4
    The positive Y unit vector.
    static final Vector4
    The positive Z unit vector.
    static final Vector4
    A vector of no direction of magnitude.

    Fields inherited from class civitas.celestis.util.tuple.Double4

    w, x, y, z
  • Constructor Summary

    Constructors
    Constructor
    Description
    Vector4(double[] components)
    Creates a new vector.
    Vector4(double w, double x, double y, double z)
    Creates a new vector.
    Creates a new vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(double s)
    Adds a scalar to this vector component-wise, then returns the resulting vector.
    Adds another vector to this vector, then returns the resulting vector.
    clamp(Vector4 min, Vector4 max)
    Between this vector and the provided boundary vectors min and max, this returns a new vector whose components are clamped to respect the range of [min, max].
    double
    Returns the Euclidean distance between this vector and the provided vector v.
    double
    Returns the squared Euclidean distance between this vector and the provided vector v.
    double
    Returns the Manhattan distance between this vector and the provided vector v.
    divide(double s)
    Divides this vector by a scalar component-wise, then returns the resulting vector.
    double
    Returns the dot product between this vector and the provided vector v.
    boolean
    Checks for equality between this vector and the provided vector v.
    Applies the provided mapper function f to each component of this tuple, then returns a new tuple containing the return values of the function f.
    Between this vector and the provided boundary vector v, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.
    Between this vector and the provided vector v, this applies the merger function f to each corresponding pair of components, then returns a new vector whose components are populated from the return values of the function f.
    Between this vector and the provided boundary vector v, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.
    multiply(double s)
    Multiplies this vector by a scalar component-wise, then returns the resulting vector.
    Returns the quaternion left-product between this vector and the provided vector v.
    Negates all components of this vector, then returns the resulting vector.
    double
    Returns the Euclidean norm (the magnitude) of this vector.
    double
    Returns the squared Euclidean norm (the squared magnitude) of this vector.
    Normalizes this vector to have a Euclidean norm (magnitude) of 1.
    Normalizes this vector to have a Euclidean norm (magnitude) of 1.
    Normalizes this vector ot have a Euclidean norm (magnitude) of 1.
    double
    Returns the Manhattan norm of this vector.
    subtract(double s)
    Subtracts this vector by a scalar component-wise, then returns the resulting vector.
    Subtracts another vector from this vector, then returns the resulting vector.

    Methods inherited from class civitas.celestis.util.tuple.Double4

    array, boxed, contains, containsAll, equals, get, hashCode, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString, w, x, y, z

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface civitas.celestis.util.tuple.DoubleTuple

    array, boxed, contains, containsAll, doubleArray, equals, get, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString
  • Field Details

    • ZERO

      public static final Vector4 ZERO
      A vector of no direction of magnitude. Represents origin.
    • POSITIVE_W

      public static final Vector4 POSITIVE_W
      The positive W unit vector.
    • POSITIVE_X

      public static final Vector4 POSITIVE_X
      The positive X unit vector.
    • POSITIVE_Y

      public static final Vector4 POSITIVE_Y
      The positive Y unit vector.
    • POSITIVE_Z

      public static final Vector4 POSITIVE_Z
      The positive Z unit vector.
    • NEGATIVE_W

      public static final Vector4 NEGATIVE_W
      The negative W unit vector.
    • NEGATIVE_X

      public static final Vector4 NEGATIVE_X
      The negative X unit vector.
    • NEGATIVE_Y

      public static final Vector4 NEGATIVE_Y
      The negative Y unit vector.
    • NEGATIVE_Z

      public static final Vector4 NEGATIVE_Z
      The negative Z unit vector.
  • Constructor Details

    • Vector4

      public Vector4(double w, double x, double y, double z)
      Creates a new vector.
      Parameters:
      w - The W component of this vector
      x - The X component of this vector
      y - The Y component of this vector
      z - The Z component of this vector
    • Vector4

      public Vector4(@Nonnull double[] components)
      Creates a new vector.
      Parameters:
      components - An array containing the components of this vector in WXYZ order
      Throws:
      IllegalArgumentException - When the provided array's length is not 4
    • Vector4

      public Vector4(@Nonnull DoubleTuple t)
      Creates a new vector.
      Parameters:
      t - The tuple of which to copy component values from
      Throws:
      IllegalArgumentException - When the provided tuple t's size is not 4
  • Method Details

    • norm

      public double norm()
      Returns the Euclidean norm (the magnitude) of this vector.
      Specified by:
      norm in interface Vector<Vector4>
      Returns:
      The Euclidean norm of this vector
    • norm2

      public double norm2()
      Returns the squared Euclidean norm (the squared magnitude) of this vector.
      Specified by:
      norm2 in interface Vector<Vector4>
      Returns:
      The squared Euclidean norm of this vector
    • normManhattan

      public double normManhattan()
      Returns the Manhattan norm of this vector.
      Specified by:
      normManhattan in interface Vector<Vector4>
      Returns:
      The Manhattan norm of this vector
    • add

      @Nonnull public Vector4 add(double s)
      Adds a scalar to this vector component-wise, then returns the resulting vector.
      Specified by:
      add in interface Vector<Vector4>
      Parameters:
      s - The scalar of which to add to each component of this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public Vector4 subtract(double s)
      Subtracts this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<Vector4>
      Parameters:
      s - The scalar of which to subtract from each component of this vector
      Returns:
      The resulting vector
    • multiply

      @Nonnull public Vector4 multiply(double s)
      Multiplies this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      multiply in interface Vector<Vector4>
      Parameters:
      s - The scalar of which to multiply each component of this vector by
      Returns:
      The resulting vector
    • divide

      @Nonnull public Vector4 divide(double s) throws ArithmeticException
      Divides this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      divide in interface Vector<Vector4>
      Parameters:
      s - The scalar of which to divide each component of this vector by
      Returns:
      The resulting vector
      Throws:
      ArithmeticException - When the denominator s is zero
    • add

      @Nonnull public Vector4 add(@Nonnull Vector4 v)
      Adds another vector to this vector, then returns the resulting vector.
      Specified by:
      add in interface Vector<Vector4>
      Parameters:
      v - The vector of which to add to this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public Vector4 subtract(@Nonnull Vector4 v)
      Subtracts another vector from this vector, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<Vector4>
      Parameters:
      v - The vector of which to subtract from this vector
      Returns:
      The resulting vector
    • multiply

      @Nonnull public Vector4 multiply(@Nonnull Vector4 v)
      Returns the quaternion left-product between this vector and the provided vector v.
      Parameters:
      v - The vector of which to get the quaternion left-product between
      Returns:
      The quaternion left-product between the two vectors
    • dot

      public double dot(@Nonnull Vector4 v)
      Returns the dot product between this vector and the provided vector v.
      Specified by:
      dot in interface Vector<Vector4>
      Parameters:
      v - The vector of which to get the dot product between
      Returns:
      The dot product between the two vectors
    • normalize

      @Nonnull public Vector4 normalize() throws ArithmeticException
      Normalizes this vector to have a Euclidean norm (magnitude) of 1.
      Specified by:
      normalize in interface Vector<Vector4>
      Returns:
      The normalized vector of this vector
      Throws:
      ArithmeticException - When the Euclidean norm of this vector is zero
    • normalizeOrZero

      @Nonnull public Vector4 normalizeOrZero()
      Normalizes this vector ot have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return itself. (which is guaranteed to be a vector where all components are zero, as only zero vectors have a Euclidean norm of zero)
      Specified by:
      normalizeOrZero in interface Vector<Vector4>
      Returns:
      The normalized vector of this vector if successful, zero otherwise
    • normalizeOrDefault

      @Nonnull public Vector4 normalizeOrDefault(@Nonnull Vector4 v)
      Normalizes this vector to have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return the provided fallback value v.
      Specified by:
      normalizeOrDefault in interface Vector<Vector4>
      Parameters:
      v - The value of which to return in case of division by zero
      Returns:
      The normalized vector of this vector if successful, the fallback value v otherwise
    • negate

      @Nonnull public Vector4 negate()
      Negates all components of this vector, then returns the resulting vector.
      Specified by:
      negate in interface Vector<Vector4>
      Returns:
      The negation of this vector
    • min

      @Nonnull public Vector4 min(@Nonnull Vector4 v)
      Between this vector and the provided boundary vector v, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.
      Specified by:
      min in interface Vector<Vector4>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The minimum vector of the two vectors
    • max

      @Nonnull public Vector4 max(@Nonnull Vector4 v)
      Between this vector and the provided boundary vector v, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.
      Specified by:
      max in interface Vector<Vector4>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The maximum vector of the two vectors
    • clamp

      @Nonnull public Vector4 clamp(@Nonnull Vector4 min, @Nonnull Vector4 max)
      Between this vector and the provided boundary vectors min and max, this returns a new vector whose components are clamped to respect the range of [min, max]. This is the equivalent of clamping each component to the corresponding pair of minimum and maximum values.
      Specified by:
      clamp in interface Vector<Vector4>
      Parameters:
      min - The minimum boundary vector to compare to
      max - The maximum boundary vector to compare to
      Returns:
      The clamped vector which respects the boundaries of [min, max]
    • distance

      public double distance(@Nonnull Vector4 v)
      Returns the Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance in interface Vector<Vector4>
      Parameters:
      v - The vector of which get the Euclidean distance between
      Returns:
      The Euclidean distance between the two vectors
    • distance2

      public double distance2(@Nonnull Vector4 v)
      Returns the squared Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance2 in interface Vector<Vector4>
      Parameters:
      v - The vector of which to get the squared Euclidean distance between
      Returns:
      The squared Euclidean distance between the two vectors
    • distanceManhattan

      public double distanceManhattan(@Nonnull Vector4 v)
      Returns the Manhattan distance between this vector and the provided vector v.
      Specified by:
      distanceManhattan in interface Vector<Vector4>
      Parameters:
      v - The vector of which to get the Manhattan distance between
      Returns:
      The Manhattan distance between the two vectors
    • map

      @Nonnull public Vector4 map(@Nonnull DoubleUnaryOperator f)
      Applies the provided mapper function f to each component of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      map in interface DoubleTuple
      Specified by:
      map in interface Vector<Vector4>
      Overrides:
      map in class Double4
      Parameters:
      f - The function of which to apply to each component of this tuple
      Returns:
      The resulting tuple
    • merge

      @Nonnull public Vector4 merge(@Nonnull Vector4 v, @Nonnull BinaryOperator<Double> f)
      Between this vector and the provided vector v, this applies the merger function f to each corresponding pair of components, then returns a new vector whose components are populated from the return values of the function f.
      Specified by:
      merge in interface Vector<Vector4>
      Parameters:
      v - The vector of which to merge this vector with
      f - The merger function to handle the merging of the two vectors
      Returns:
      The resulting vector
    • equals

      public boolean equals(@Nullable Vector4 v)
      Checks for equality between this vector and the provided vector v.
      Specified by:
      equals in interface Vector<Vector4>
      Parameters:
      v - The vector to compare to
      Returns:
      true if the component values are equal