Class Vector3d

  • All Implemented Interfaces:
    Serializable

    public final class Vector3d
    extends Object
    implements Serializable
    Basic class representing an immutable vector containing 3 doubles. All inputs and outputs are in system units.
    Author:
    moskowitz
    See Also:
    Serialized Form
    • Constructor Detail

      • Vector3d

        public Vector3d​(double x,
                        double y,
                        double z)
        Standard constructor.
        Parameters:
        x -
        y -
        z -
      • Vector3d

        public Vector3d()
        Default constructor. Creates zero vector.
    • Method Detail

      • createCylindrical

        public static Vector3d createCylindrical​(double radius,
                                                 double theta,
                                                 double z)
        Create Vector3d from cylindrical coordinates.

        Note: Negative radius is allowed. This reverses the vector direction in x and y.

        Parameters:
        radius - distance from z-axis (XY plane projection radius)
        theta - horizontal azimuth angle (XY plane projection theta)
        z - z-coordinate
        Returns:
        Vector3d
      • getX

        public double getX()
      • getY

        public double getY()
      • getZ

        public double getZ()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • format

        public String format​(String coordFormat)
        Get formatted String representation.
        Parameters:
        coordFormat - format applied to each coordinate (as in String.format)
        Returns:
        formatted string with comma separated coordinates
      • toArray

        public double[] toArray()
      • fromArray

        public static Vector3d fromArray​(double[] coords)
      • scaledBy

        public Vector3d scaledBy​(double scaleFactor)
      • dotProduct

        public double dotProduct​(Vector3d v)
      • normSquared

        public double normSquared()
      • distanceSquared

        public double distanceSquared​(Vector3d v)
      • distance

        public double distance​(Vector3d v)
      • norm

        public double norm()
      • isZero

        public boolean isZero()
      • distanceOnXYPlaneSquared

        public double distanceOnXYPlaneSquared​(Vector2d v)
      • distanceOnXYPlane

        public double distanceOnXYPlane​(Vector2d v)
      • distanceOnXYPlaneSquared

        public double distanceOnXYPlaneSquared​(Vector3d v)
      • distanceOnXYPlane

        public double distanceOnXYPlane​(Vector3d v)
      • isValid

        public boolean isValid()
        False if any of the coordinates is NaN or Infinite.
      • withZ

        public Vector3d withZ​(double z)
      • normalized

        public Vector3d normalized()
        Returns normalized (rescaled to norm = 1) version of this vector.

        Note: "Strict" version: if zero vector, returned vector will contain all NaN values and a warning will be logged.

        Returns:
        normalized version of this vector
      • normalizedLenient

        public Vector3d normalizedLenient()
        Returns normalized (rescaled to norm = 1) version of this vector.

        Note: "Lenient" version: If zero vector, returned vector will be (1, 0, 0).

        Returns:
        normalized version of this vector
      • horizontalAzimuthAngle

        public double horizontalAzimuthAngle()
        Returns the azimuth angle of the projection of this vector on the xy-plane.
      • horizontalAzimuthAngleFast

        public double horizontalAzimuthAngleFast()
        Returns the azimuth angle of the projection of this vector on the xy-plane.

        Note: This version uses a faster, less accurate, calculation.

      • phiAngle

        public double phiAngle()
        Returns the conical angle between this vector and the positive z axis. Zero on the +z axis. Positive elsewhere with max value of PI radians or 180 degrees (in system units).
      • elevationAngle

        public double elevationAngle()
        Returns the conical angle between this vector and the xy-plane. Zero on the xy-plane. Positive above the plane to a max of PI/2 or 90 degrees (in system units), negative below the plane to a min of -PI/2 or -90 degrees (in system units).
      • rotated

        public Vector3d rotated​(double theta,
                                Vector3d axis)
        Parameters:
        theta - rotation angle around the specified axis.
        axis - the axis about which to rotate.
        Returns:
        rotated copy
      • rotatedAboutXAxisBy

        public Vector3d rotatedAboutXAxisBy​(double theta)
        Parameters:
        theta - rotation angle around the x-axis.
        Returns:
        rotated copy
      • rotatedAboutYAxisBy

        public Vector3d rotatedAboutYAxisBy​(double theta)
        Parameters:
        theta - rotation angle around the y-axis.
        Returns:
        rotated copy
      • rotatedAboutZAxisBy

        public Vector3d rotatedAboutZAxisBy​(double theta)
        Parameters:
        theta - rotation angle around the z-axis.
        Returns:
        rotated copy
      • cosAngleWith

        public double cosAngleWith​(Vector3d v)
        Returns cosine of the angle between this vector and vector v.
      • angleWith

        public double angleWith​(Vector3d v)
        Returns the conical (unsigned) angle between this vector and vector v.
      • projectOnto

        public Vector3d projectOnto​(Vector3d v)
        Returns the projection of this vector onto the vector v.
        See Also:
        for lenient handling of v
      • projectionOnto

        public Vector3d projectionOnto​(Vector3d v,
                                       boolean isLenient)
        Returns the projection of this vector onto the vector v. Lenient version treats zero vector for v as (1,0,0) in order to avoid returning vector containing NaN values.
      • reflectionThroughPlaneAt

        public Vector3d reflectionThroughPlaneAt​(Vector3d x0,
                                                 Vector3d normal)
        Returns reflection of this vector through a plane specified by a point on the plane and a vector normal to the plane.
        Parameters:
        x0 - point on the plane
        normal - normal to the plane
        Returns:
        reflected position
      • orthonormalVectors

        public Vector3d[] orthonormalVectors()
        Returns unit vectors w0 and w1 which are orthogonal to this vector and each other.
      • linearCombination

        public static Vector3d linearCombination​(double[] a,
                                                 Vector3d[] w)
      • interpolate

        public static Vector3d interpolate​(double t,
                                           Vector3d v0,
                                           Vector3d v1)
        Linear interpolation between vectors v0 and v1. t = 0 returns v0 and t = 1 returns v1.