com.kuka.nav.geometry
Class Vector2D

java.lang.Object
  extended by com.kuka.nav.geometry.Vector2D
All Implemented Interfaces:
Serializable, Comparable<Vector2D>

public final class Vector2D
extends Object
implements Serializable, Comparable<Vector2D>

Immutable representation of a 2-element vector consisting of x and y elements. The class Vector2D represents a difference vector, which describes the displacement between two 2D points. To quote Murray, R. et al — A mathematical Introduction to Robotic Manipulation, Section 3.1 the following propositions are hold:

  1. Sum and difference of vectors are vectors
  2. The sum of a vector and a point is a point
  3. The difference between two points is a vector
  4. The sum of two points is meaningless

Concerning transformations the following properties are enforced on vectors

To create a new Vector object use the static factory method of(double, double).

See Also:
Serialized Form

Field Summary
static Vector2D UNIT_X
          Immutable Vector2D instance representing the X unit vector.
static Vector2D UNIT_Y
          Immutable Vector2D instance representing the Y unit vector.
static Vector2D ZERO
          Immutable Vector2D instance representing a zero vector.
 
Constructor Summary
Vector2D(double x, double y)
          New instance of this class.
Vector2D(com.kuka.math.geometry.Vector3D v)
          Creates a new 2D vector from a given 3D vector by dropping the third (z) coordinate.
 
Method Summary
 Vector2D add(Vector2D other)
          Adds the given vector to the values of this vector.
 double angleRad(Vector2D other)
          Calculates the smallest angle between this and the other vector.
 boolean approxEquals(Vector2D p, double epsilon)
          Compares for equality within an epsilon envelope.
 int compareTo(Vector2D o)
           
 Vector2D crossProduct(Vector2D other)
          Returns the vector that is the cross product between this vector and the other vector.
 double dotProduct(Vector2D other)
          Returns the dot (inner/scalar) product between this vector and the other vector.
 boolean equals(Object obj)
          Compares the specified object for equality with this vector.
 double get(int index)
          Gets a value of the vector at the specified index, where 0 denotes the x and 1 the y element of the vector.
 double getX()
           
 double getY()
           
 int hashCode()
          Returns the hash code for this vector.
 Vector2D invert()
          Returns an inverted vector, i.e. a vector with element wise negated values.
 double length()
          Returns the length of the vector.
 Vector2D multiply(double factor)
          Returns a vector that has the values of this vector scaled by the given factor.
 Vector2D normalize()
          Returns a vector that points in the same direction as this vector, scaled to unit length (1).
static Vector2D of(double x, double y)
          Returns an immutable Vector2D instance constructed from its Cartesian X and Y elements.
 Vector2D subtract(Vector2D other)
          Subtracts the given vector from the values of this vector.
 double[] toArray()
          Converts the Vector into an array with 3 values.
 String toString()
          Returns the vector value formatted as a column vector in a way compatible with popular scientific numerical computing applications.
 String toString(NumberFormat fmt)
          Returns the vector value formatted as a column vector in a way similar to popular scientific numerical computing applications.
 Vector2D withX(double value)
          Returns a new instance (copy) of the vector, with the x element replaced by the new value.
 Vector2D withY(double value)
          Returns a new instance (copy) of the vector, with the y element replaced by the new value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Vector2D ZERO
Immutable Vector2D instance representing a zero vector.


UNIT_X

public static final Vector2D UNIT_X
Immutable Vector2D instance representing the X unit vector.


UNIT_Y

public static final Vector2D UNIT_Y
Immutable Vector2D instance representing the Y unit vector.

Constructor Detail

Vector2D

public Vector2D(double x,
                double y)
New instance of this class.

Parameters:
x - x element of the vector
y - y element of the vector

Vector2D

public Vector2D(com.kuka.math.geometry.Vector3D v)
Creates a new 2D vector from a given 3D vector by dropping the third (z) coordinate.

Parameters:
v - a 3D vector
Method Detail

of

public static Vector2D of(double x,
                          double y)
Returns an immutable Vector2D instance constructed from its Cartesian X and Y elements.

Parameters:
x - the Cartesian X element of the vector
y - the Cartesian Y element of the vector
Returns:
an immutable Vector2D instance

getX

public double getX()
Returns:
The x element of the vector.
See Also:
getY(), get(int)

getY

public double getY()
Returns:
The y element of the vector.
See Also:
getX(), get(int)

get

public double get(int index)
Gets a value of the vector at the specified index, where 0 denotes the x and 1 the y element of the vector.

Parameters:
index - The index of the element, must be between 0 and 1.
Returns:
The value at the specified index.
Throws:
IndexOutOfBoundsException - illegal index
See Also:
getX(), getY()

withX

public Vector2D withX(double value)
Returns a new instance (copy) of the vector, with the x element replaced by the new value.

Parameters:
value - The x element of the vector.
Returns:
a new instance of the class

withY

public Vector2D withY(double value)
Returns a new instance (copy) of the vector, with the y element replaced by the new value.

Parameters:
value - The y element of the vector.
Returns:
a new instance of the class

invert

public Vector2D invert()
Returns an inverted vector, i.e. a vector with element wise negated values.

Returns:
The inverted vector.

length

public double length()
Returns the length of the vector.

Returns:
the length of the vector.

normalize

public Vector2D normalize()
Returns a vector that points in the same direction as this vector, scaled to unit length (1).

Returns:
the resulting vector.

dotProduct

public double dotProduct(Vector2D other)
Returns the dot (inner/scalar) product between this vector and the other vector.

Parameters:
other - another vector.
Returns:
the dot (inner/scalar) product.

crossProduct

public Vector2D crossProduct(Vector2D other)
Returns the vector that is the cross product between this vector and the other vector.

Parameters:
other - another vector.
Returns:
the cross product.

angleRad

public double angleRad(Vector2D other)
Calculates the smallest angle between this and the other vector. Angle is in Radiant.

Parameters:
other - a vector
Returns:
The angle between this and the other vector in radian (between 0 and PI).
Throws:
ArithmeticException - if this or the other vector has a approximate zero length

multiply

public Vector2D multiply(double factor)
Returns a vector that has the values of this vector scaled by the given factor.

Parameters:
factor - The factor with which this vector should be multiplied.
Returns:
The resulting vector.
Throws:
ArithmeticException - factor is infinite or NaN

add

public Vector2D add(Vector2D other)
Adds the given vector to the values of this vector.

Parameters:
other - The vector which will be added to the values of this vector.
Returns:
The resulting vector.

subtract

public Vector2D subtract(Vector2D other)
Subtracts the given vector from the values of this vector.

Parameters:
other - The vector which will be subtracted from the values of this vector.
Returns:
The resulting vector.

toString

public String toString()
Returns the vector value formatted as a column vector in a way compatible with popular scientific numerical computing applications. The numerical values will be printed with the default format as returned by MathUtils.getDefaultNumberFormat().

The following string can be regarded as typical:

[1.234; 300.0; -20.2]

Overrides:
toString in class Object
Returns:
a textual representation of the vector

toString

public String toString(NumberFormat fmt)
Returns the vector value formatted as a column vector in a way similar to popular scientific numerical computing applications. The numerical values will be printed using the NumberFormat.format(double) method of the given formatter.

Example: The following code snippet, used on a given Vector3D vec

 NumberFormat fmt = NumberFormat.getNumberInstance(Locale.US);
 fmt.setMinimumFractionDigits(1);
 fmt.setMaximumFractionDigits(3);
 System.out.println(vec.toString(fmt));
 
might give the following output:
 [-1.234; 1.414; 20.0]
 

Parameters:
fmt - the NumberFormat used to format the number values
Returns:
a textual representation of the vector

toArray

public double[] toArray()
Converts the Vector into an array with 3 values.

Returns:
array representation of the vector.

hashCode

public int hashCode()
Returns the hash code for this vector. The hash code is computed by a combination of all the data elements defined by this interface.

Overrides:
hashCode in class Object
Returns:
hash code for this vector.

equals

public boolean equals(Object obj)
Compares the specified object for equality with this vector. Vectors are equal if all data elements returned by this interface are equal.

Overrides:
equals in class Object
Parameters:
obj - Object to be compared with this vector.
Returns:
true if the object is equal to this vector.

compareTo

public int compareTo(Vector2D o)
Specified by:
compareTo in interface Comparable<Vector2D>

approxEquals

public boolean approxEquals(Vector2D p,
                            double epsilon)
Compares for equality within an epsilon envelope.

Parameters:
p - the other point to compare to
epsilon - the tolerance
Returns:
true if the points are within the epsilon tolerance


Copyright © 2019. All rights reserved.