Package pamMaths

Class PamVector

java.lang.Object
pamMaths.PamVector
All Implemented Interfaces:
Serializable, Cloneable, ManagedParameters, PamCoordinate

public class PamVector extends Object implements Serializable, Cloneable, PamCoordinate, ManagedParameters
Author:
Doug Gillespie Vector calculations for spatial orientation.
All vectors have three elements for x,y,z.
Package includes basic vector calculations, mostly copied from Stephenson 1961
See Also:
  • Field Details

    • xAxis

      public static final PamVector xAxis
    • yAxis

      public static final PamVector yAxis
    • zAxis

      public static final PamVector zAxis
    • cartesianAxes

      public static final PamVector[] cartesianAxes
  • Constructor Details

    • PamVector

      public PamVector(double[] vector)
    • PamVector

      public PamVector(double x, double y, double z)
    • PamVector

      public PamVector(PamVector vector)
    • PamVector

      public PamVector()
  • Method Details

    • clone

      public PamVector clone()
    • normSquared

      public double normSquared()
      Returns:
      The magnitude squared of the vector
    • normSquared

      public double normSquared(int nDim)
      Get the squared magnitude in a limited number of dimensions.
      Parameters:
      nDim - number of dimensions
      Returns:
      The magnitude squared of the vector
    • fromHeadAndSlant

      public static PamVector fromHeadAndSlant(double heading, double slantAngle)
      Create a unit vector based on a heading (degrees from North) and a slant angle 90 up up, -90 = down
      Parameters:
      heading - heading from North in degrees
      slantAngle - slant angle from the horizontal.
      Returns:
      Unit vector.
    • fromHeadAndSlantR

      public static PamVector fromHeadAndSlantR(double heading, double slantAngle)
      Create a unit vector based on a heading (radians) and a slant angle Pi/2 up to -Pi/2 = down
      Parameters:
      heading - heading from North in radians
      slantAngle - slant angle from the horizontal.
      Returns:
      Unit vector.
    • toHeadAndSlantR

      public double[] toHeadAndSlantR()
      Opposite of the fromHeadAndSlantR() function. returning an angle pair for the vector. First angle is heading, second is slant angle
      Returns:
      angles in radians.
    • equals

      public boolean equals(PamVector vec)
      Parameters:
      vec - other vector to compare.
      Returns:
      true if the vectors are equal in all dimensions
    • sub

      public PamVector sub(PamVector vec)
      Subtract another vector from this vector and return the result in a new Vector
      Parameters:
      vec - vector to subtract
      Returns:
      new vector
    • add

      public PamVector add(PamVector vec)
      Add another vector to this vector and return the result in a new Vector
      Parameters:
      vec - vector to add
      Returns:
      new vector
    • add

      public static PamVector add(PamVector... pamVectors)
      Add any number of vectors together.
      Parameters:
      pamVectors - list of vectors
      Returns:
      sum of all vectors.
    • addQuadrature

      public static PamVector addQuadrature(PamVector... pamVectors)
      Add any number of vectors together in quadrature.
      Parameters:
      pamVectors - list of vectors
      Returns:
      sum of all vectors.
    • arrayToMatrix

      public static Jama.Matrix arrayToMatrix(PamVector[] pamVectors)
      convert a series of vectors into a Matrix
      Parameters:
      pamVectors - array of vectors
      Returns:
      Jama Matrix
    • rotate

      public PamVector rotate(Jama.Matrix rotationMatrix)
      Rotate the vector by a matrix.
      Parameters:
      rotationMatrix - rotation matrix
      Returns:
      rotated vector.
    • rotate

      public PamVector rotate(double rotationAngle)
      Rotate a vector anti-clockwise by an angle.
      the third dimension of the matrix remains untouched. The first two dimensions will rotate anti clockwise by the given angle.
      Parameters:
      rotationAngle - rotation angle in radians.
      Returns:
      new rotated vector.
    • rotate

      public PamVector rotate(PamVector rotationVector)
      Rotate a vector using another vector. This is basically a surface rotation in the x,y plane using the same functionality as the rotate(double rotationAngle) function, but the input are already in vector form
      Parameters:
      rotationVector -
      Returns:
    • times

      public PamVector times(double scalar)
      Multiply a vector by a scalar
      Parameters:
      scalar - scalar value
      Returns:
      new vector
    • getUnitVector

      public PamVector getUnitVector()
      Get the unit vector, or if the vector is zero, return the zero vector (0,0,0).
      Returns:
      the unit vector
    • angle

      public double angle(PamVector vec)
      Calculate the angle between two vectors
      Parameters:
      vec - other vector
      Returns:
      angle in radians between 0 and pi
    • absAngle

      public double absAngle(PamVector vec)
      Calculates the smallest angle between two vectors (0 invalid input: '<'= angle invalid input: '<' pi/2);
      Parameters:
      vec - other vector
      Returns:
      angle between two vectors (0 invalid input: '<' angle invalid input: '<' pi/2)
    • distSquared

      public double distSquared(PamVector vec)
      Parameters:
      vec - other vector
      Returns:
      square of the distance between two vectors
    • dist

      public double dist(PamVector vec)
      Parameters:
      vec - other vector
      Returns:
      the distance between two vectors
    • norm

      public double norm()
      Returns:
      the magnitude of the vector
    • norm

      public double norm(int nDim)
      Get the magnitude of the vector in a limited number of dimensions
      Parameters:
      nDim - number of dimensions
      Returns:
      magnitude of those dimensions only.
    • dotProd

      public double dotProd(PamVector vec)
      Parameters:
      vec - a PamVector
      Returns:
      the dot product of this and vec
    • sumComponentsSquared

      public double sumComponentsSquared(PamVector vec)
      A bit like a dot product, but the three components are added in quadrature. This is used to estimate errors along a particular vector component.
      Parameters:
      vec - a Pam Vector
      Returns:
      sum of components in each direction, added in quadrature.
    • vecProd

      public PamVector vecProd(PamVector vec)
      Vector or cross product of two vectors.
      Parameters:
      vec -
      Returns:
      vector product of this and vec.
    • tripleDotProduct

      public double tripleDotProduct(PamVector v1, PamVector v2)
      Calculate the triple product of this with v1 and v2
      Parameters:
      v1 - other vector
      v2 - other vector
      Returns:
      triple produce this.(v1^v2);
    • isParallel

      public boolean isParallel(PamVector vec)
      Tests to see whether two vectors are parallel
      Parameters:
      vec - other vector
      Returns:
      true if parallel to within 1/1000 radian.
    • isInLine

      public boolean isInLine(PamVector vec)
      Test to see whether two vectors lie in a perfect line
      Parameters:
      vec - other vector
      Returns:
      true if they line up perfectly
    • xyDistance

      public double xyDistance()
      Returns:
      the magnitude of the xy components of the vector - generally the magnitude across the sea surface if the vector is in normal coordinates.
    • getVector

      public double[] getVector()
    • setVector

      public void setVector(double[] vector)
    • getElement

      public double getElement(int iElement)
      Get a single vector element
      Parameters:
      iElement - element index
      Returns:
      value
    • setElement

      public void setElement(int iElement, double val)
      Set a single vector element
      Parameters:
      iElement - element index
      val - element value
    • getXAxis

      public static PamVector getXAxis()
    • getYAxis

      public static PamVector getYAxis()
    • getZAxis

      public static PamVector getZAxis()
    • getCartesianAxes

      public static PamVector getCartesianAxes(int iAxis)
      Parameters:
      iAxis -
      Returns:
      a unit vector along a Cartesian axis (x,y,z)
    • getVectorPairs

      public static PamVector[] getVectorPairs(PamVector[] vectors)
      Create a set of vector pairs, which are vectors between all the vectors in the input argument
      Parameters:
      vectors - array of vectors
      Returns:
      array of vectors between all the input vectors (creates n*(n-1)/2 new vectors)
    • getPrincipleAxis

      public int getPrincipleAxis()
      Returns:
      the axis to which the vector is closest.
    • getPerpendicularVector

      public PamVector getPerpendicularVector()
      Get a vector that's perpendicular to the current one, in the xy plane if at all possible.
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • vectorsToSurfaceBearings

      public static double[] vectorsToSurfaceBearings(PamVector[] vectors)
      Convert an array of Cartesian vectors into bearings from North. Only the x and y parts of the vector are used in this and to be a pain, the bearing is left in radians !
      Parameters:
      vectors - Cartesian vector (need not be a unit vector)
      Returns:
      clockwise bearings from north in radians
    • vectorToSurfaceBearing

      public static double vectorToSurfaceBearing(PamVector vector)
      Convert a Cartesian vector into a bearing from North. Only the x and y parts of the vector are used in this and to be a pain, the bearing is left in radians !
      Parameters:
      vector - Cartesian vector (need not be a unit vector)
      Returns:
      clockwise bearing from north in radians
    • normalise

      public double normalise()
      Normalise a vector so it has a magnitude of 1. if the vector has magnitude 0, it is left with this magnitude and 0 is returned.
      Returns:
      0 if the vector had zero magnitude, a otherwise.
    • absElements

      public PamVector absElements()
      Make all elements within the vector positive. This can be used to simplify symmetric problems.
      Returns:
      The vector but with all elements made positive.
    • getRotMatrixX

      public static Jama.Matrix getRotMatrixX(double angle)
      Get a matrix which rotates a vector around the x axis.
      Parameters:
      angle - - angle to rotate around the x axis in RADIANS.
      Returns:
      x axis rotation matrix.
    • getRotMatrixY

      public static Jama.Matrix getRotMatrixY(double angle)
      Get a matrix which rotates a vector around the y axis.
      Parameters:
      angle - - angle to rotate around the y axis in RADIANS.
      Returns:
      y axis rotation matrix.
    • getRotMatrixZ

      public static Jama.Matrix getRotMatrixZ(double angle)
      Get a matrix which rotates a vector around the z axis.
      Parameters:
      angle - - angle to rotate around the z axis in RADIANS.
      Returns:
      z axis rotation matrix.
    • rotateVector

      public static PamVector rotateVector(PamVector vector, PamQuaternion orientation)
      Rotates a vector by a Quaternion .
      Parameters:
      PamQuaternion - - orientation
      Returns:
      vector rotated by the quaternion.
    • rotateVector

      public static PamVector rotateVector(PamVector vector, double[] origin, PamQuaternion orientation)
      Rotates a vector about an origin point
      Parameters:
      vector - - the vector to rotate
      origin - - the origin
      orientation - - the quaternion to rotate the vector by
      Returns:
      the rotated vector.
    • rotateVector

      public static PamVector rotateVector(PamVector vector, double heading, double pitch)
      Rotates a vector by the Euler angles.
      Parameters:
      heading - - heading in RADIANS. Here we use 0 to 360 for heading.
      pitch - - pitch in RADIANS. Use the standard PAMGUARD convention 90=-g; 0=0 -90=g (g =direction of gravitational field)
      Returns:
      vector rotated by the Euler angles.
    • rotateVector

      public static PamVector rotateVector(PamVector vector, double[] angle)
      Rotates a vector by the Euler angles.
      Parameters:
      angle - - the Euler angles. angle[0] is heading in RADIANS. Here we use 0 to 360 for heading angle[1] pitch in RADIANS. Use the standard PAMGUARD convention 90=-g; 0=0 -90=g (g =direction of gravitational field) angle[2] is tilt in RADIANS. Use the standard PAMGUARD convention.
      Returns:
      vector rotated by the Euler angles.
    • isCone

      public boolean isCone()
      Check whether the vector represents a surface, rather than a pointing vector. PamVectors may represent the surface of a cone rather than a 3D vector.
      Returns:
      true if the PamVector represents a surface.
    • setCone

      public void setCone(boolean isCone)
      Set whether the vector represents a surface, rather than a pointing vector. PamVectors may represent the surface of a cone rather than a 3D vector. If this is the case then isCone is true.
      Parameters:
      isCone - - true if the PamVector represents a surface.
    • getCoordinate

      public double getCoordinate(int iCoordinate)
      Description copied from interface: PamCoordinate
      Get an indexed coordinate.
      Specified by:
      getCoordinate in interface PamCoordinate
      Parameters:
      iCoordinate - coordinate index.
      Returns:
      coordinate value.
    • setCoordinate

      public void setCoordinate(int iCoordinate, double value)
      Description copied from interface: PamCoordinate
      Set a coordinate value
      Specified by:
      setCoordinate in interface PamCoordinate
      Parameters:
      iCoordinate - index of coordinate (0,1,2)
      value - value to set.
    • getNumCoordinates

      public int getNumCoordinates()
      Specified by:
      getNumCoordinates in interface PamCoordinate
      Returns:
      the number of coordinates.
    • mean

      public static PamVector mean(PamVector[] vectors)
      Calculate the mean position of a set of vectors.
      Parameters:
      vectors -
      Returns:
      the vector mean
    • getParameterSet

      public PamParameterSet getParameterSet()
      Description copied from interface: ManagedParameters
      Get a set of data that describes all of the parameters in a class
      Specified by:
      getParameterSet in interface ManagedParameters
      Returns:
      description of the parameters in a class.
    • getHeading

      public double getHeading()
      Returns:
      The heading of a vector, angles clockwise from N in radians.
    • getPitch

      public double getPitch()
      Returns:
      The pitch or slant angle of a vector.
    • getHeadingPitchRoll

      public static double[] getHeadingPitchRoll(PamVector[] vectors)
      Convert orthogonal vectors to heading pitch and roll.
      Parameters:
      vectors -
      Returns:
      array of heading pith and roll in radians
    • getMinimalHeadingPitchRoll

      public static double[] getMinimalHeadingPitchRoll(PamVector[] vectors)
      Get minimum heading pitch and roll information, throwing away any data which are zeros. Primarily used for saving to file where we normally won't need to write p and r if they are zero
      Parameters:
      vectors - orthogonal vectors describing array
      Returns:
      angles in radians (angle clockwise from N, elevation and roll)