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 Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
Calculates the smallest angle between two vectors (0 invalid input: '<'= angle invalid input: '<' pi/2);Make all elements within the vector positive.Add another vector to this vector and return the result in a new Vectorstatic PamVector
Add any number of vectors together.static PamVector
addQuadrature
(PamVector... pamVectors) Add any number of vectors together in quadrature.double
Calculate the angle between two vectorsstatic Jama.Matrix
arrayToMatrix
(PamVector[] pamVectors) convert a series of vectors into a Matrixclone()
double
double
distSquared
(PamVector vec) double
boolean
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 = downstatic 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 = downstatic PamVector
getCartesianAxes
(int iAxis) double
getCoordinate
(int iCoordinate) Get an indexed coordinate.double
getElement
(int iElement) Get a single vector elementdouble
static double[]
getHeadingPitchRoll
(PamVector[] vectors) Convert orthogonal vectors to heading pitch and roll.static double[]
getMinimalHeadingPitchRoll
(PamVector[] vectors) Get minimum heading pitch and roll information, throwing away any data which are zeros.int
Get a set of data that describes all of the parameters in a classGet a vector that's perpendicular to the current one, in the xy plane if at all possible.double
getPitch()
int
static Jama.Matrix
getRotMatrixX
(double angle) Get a matrix which rotates a vector around the x axis.static Jama.Matrix
getRotMatrixY
(double angle) Get a matrix which rotates a vector around the y axis.static Jama.Matrix
getRotMatrixZ
(double angle) Get a matrix which rotates a vector around the z axis.Get the unit vector, or if the vector is zero, return the zero vector (0,0,0).double[]
static PamVector[]
getVectorPairs
(PamVector[] vectors) Create a set of vector pairs, which are vectors between all the vectors in the input argumentstatic PamVector
getXAxis()
static PamVector
getYAxis()
static PamVector
getZAxis()
boolean
isCone()
Check whether the vector represents a surface, rather than a pointing vector.boolean
Test to see whether two vectors lie in a perfect lineboolean
isParallel
(PamVector vec) Tests to see whether two vectors are parallelstatic PamVector
Calculate the mean position of a set of vectors.double
norm()
double
norm
(int nDim) Get the magnitude of the vector in a limited number of dimensionsdouble
Normalise a vector so it has a magnitude of 1.double
double
normSquared
(int nDim) Get the squared magnitude in a limited number of dimensions.rotate
(double rotationAngle) Rotate a vector anti-clockwise by an angle.rotate
(Jama.Matrix rotationMatrix) Rotate the vector by a matrix.Rotate a vector using another vector.static PamVector
rotateVector
(PamVector vector, double[] angle) Rotates a vector by the Euler angles.static PamVector
rotateVector
(PamVector vector, double[] origin, PamQuaternion orientation) Rotates a vector about an origin pointstatic PamVector
rotateVector
(PamVector vector, double heading, double pitch) Rotates a vector by the Euler angles.static PamVector
rotateVector
(PamVector vector, PamQuaternion orientation) Rotates a vector by a Quaternion .void
setCone
(boolean isCone) Set whether the vector represents a surface, rather than a pointing vector.void
setCoordinate
(int iCoordinate, double value) Set a coordinate valuevoid
setElement
(int iElement, double val) Set a single vector elementvoid
setVector
(double[] vector) Subtract another vector from this vector and return the result in a new Vectordouble
A bit like a dot product, but the three components are added in quadrature.times
(double scalar) Multiply a vector by a scalardouble[]
Opposite of the fromHeadAndSlantR() function.toString()
double
tripleDotProduct
(PamVector v1, PamVector v2) Calculate the triple product of this with v1 and v2Vector or cross product of two vectors.static double[]
vectorsToSurfaceBearings
(PamVector[] vectors) Convert an array of Cartesian vectors into bearings from North.static double
vectorToSurfaceBearing
(PamVector vector) Convert a Cartesian vector into a bearing from North.double
-
Field Details
-
xAxis
-
yAxis
-
zAxis
-
cartesianAxes
-
-
Constructor Details
-
PamVector
public PamVector(double[] vector) -
PamVector
public PamVector(double x, double y, double z) -
PamVector
-
PamVector
public PamVector()
-
-
Method Details
-
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
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 degreesslantAngle
- slant angle from the horizontal.- Returns:
- Unit vector.
-
fromHeadAndSlantR
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 radiansslantAngle
- 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
- Parameters:
vec
- other vector to compare.- Returns:
- true if the vectors are equal in all dimensions
-
sub
Subtract another vector from this vector and return the result in a new Vector- Parameters:
vec
- vector to subtract- Returns:
- new vector
-
add
Add another vector to this vector and return the result in a new Vector- Parameters:
vec
- vector to add- Returns:
- new vector
-
add
Add any number of vectors together.- Parameters:
pamVectors
- list of vectors- Returns:
- sum of all vectors.
-
addQuadrature
Add any number of vectors together in quadrature.- Parameters:
pamVectors
- list of vectors- Returns:
- sum of all vectors.
-
arrayToMatrix
convert a series of vectors into a Matrix- Parameters:
pamVectors
- array of vectors- Returns:
- Jama Matrix
-
rotate
Rotate the vector by a matrix.- Parameters:
rotationMatrix
- rotation matrix- Returns:
- rotated vector.
-
rotate
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
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
Multiply a vector by a scalar- Parameters:
scalar
- scalar value- Returns:
- new vector
-
getUnitVector
Get the unit vector, or if the vector is zero, return the zero vector (0,0,0).- Returns:
- the unit vector
-
angle
Calculate the angle between two vectors- Parameters:
vec
- other vector- Returns:
- angle in radians between 0 and pi
-
absAngle
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
- Parameters:
vec
- other vector- Returns:
- square of the distance between two vectors
-
dist
- 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
- Parameters:
vec
- a PamVector- Returns:
- the dot product of this and vec
-
sumComponentsSquared
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
Vector or cross product of two vectors.- Parameters:
vec
-- Returns:
- vector product of this and vec.
-
tripleDotProduct
Calculate the triple product of this with v1 and v2- Parameters:
v1
- other vectorv2
- other vector- Returns:
- triple produce this.(v1^v2);
-
isParallel
Tests to see whether two vectors are parallel- Parameters:
vec
- other vector- Returns:
- true if parallel to within 1/1000 radian.
-
isInLine
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 indexval
- element value
-
getXAxis
-
getYAxis
-
getZAxis
-
getCartesianAxes
- Parameters:
iAxis
-- Returns:
- a unit vector along a Cartesian axis (x,y,z)
-
getVectorPairs
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
Get a vector that's perpendicular to the current one, in the xy plane if at all possible.- Returns:
-
toString
-
vectorsToSurfaceBearings
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
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
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
Rotates a vector by a Quaternion .- Parameters:
PamQuaternion
- - orientation- Returns:
- vector rotated by the quaternion.
-
rotateVector
Rotates a vector about an origin point- Parameters:
vector
- - the vector to rotateorigin
- - the originorientation
- - the quaternion to rotate the vector by- Returns:
- the rotated vector.
-
rotateVector
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
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 interfacePamCoordinate
- 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 interfacePamCoordinate
- Parameters:
iCoordinate
- index of coordinate (0,1,2)value
- value to set.
-
getNumCoordinates
public int getNumCoordinates()- Specified by:
getNumCoordinates
in interfacePamCoordinate
- Returns:
- the number of coordinates.
-
mean
Calculate the mean position of a set of vectors.- Parameters:
vectors
-- Returns:
- the vector mean
-
getParameterSet
Description copied from interface:ManagedParameters
Get a set of data that describes all of the parameters in a class- Specified by:
getParameterSet
in interfaceManagedParameters
- 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
Convert orthogonal vectors to heading pitch and roll.- Parameters:
vectors
-- Returns:
- array of heading pith and roll in radians
-
getMinimalHeadingPitchRoll
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)
-