Package com.j3d.engine.math.matrix
Class Vector3
java.lang.Object
com.j3d.engine.math.matrix.Vector3
- All Implemented Interfaces:
MatrixInterface
- Direct Known Subclasses:
Point
Represents an immutable 3D vector or a point in 3D space.
This class also implements the MatrixInterface, treating the vector as a
3x1 column matrix. This allows it to be used directly in matrix operations
defined in MatrixMath.
All vector operations (e.g., add, sub, mult) return a new
Vector3 instance, preserving the immutability of the original.
- Author:
- Lehlogonolo Poole
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic Vector3A static constant representing the unit vector along the negative X-axis (-1, 0, 0).static Vector3A static constant representing the unit vector along the negative Y-axis (0, -1, 0).static Vector3A static constant representing the unit vector along the negative Z-axis (0, 0, -1).static Vector3A static constant representing the unit vector (1, 1, 1).private final doublestatic Vector3A static constant representing the unit vector along the positive X-axis (1, 0, 0).private final doublestatic Vector3A static constant representing the unit vector along the positive Y-axis (0, 1, 0).private final doublestatic Vector3A static constant representing the unit vector along the positive Z-axis (0, 0, 1).static Vector3A static constant representing the zero vector (0, 0, 0). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Vector3acrossX(double x) Creates a vector along the X-axis with the specified magnitude.static Vector3acrossY(double y) Creates a vector along the Y-axis with the specified magnitude.static Vector3acrossZ(double z) Creates a vector along the Z-axis with the specified magnitude.Adds another vector to this one.intcols()Returns the number of columns in the matrix.copy()Creates a deep copy of this vector.Calculates the cross product of this vector and another.doubleCalculates the Euclidean distance between this vector (as a point) and another.doubledistanceSquaredTo(CartesianPoint point) Calculates the squared 2D distance from this vector's X and Y components to aCartesianPoint.div(double scalar) Divides the vector by a scalar value.doubleCalculates the dot product of this vector and another.booleanstatic Vector3fromObject(GettersPack.CallProperties callProperties, EngineObject obj) double[][]get()Returns the underlying data as a 2D array (3x1).doubleget(int row, int col) Gets the value at a specific row in this 3x1 column matrix.doublegetX()doublegetY()doublegetZ()inthashCode()booleanChecks if the vector is not set to the special "empty" value.doubleCalculates the magnitude (length) of the vector.mult(double scalar) Multiplies the vector by a scalar value.Performs component-wise multiplication (Hadamard product) with another vector.Normalizes the vector to have a magnitude of 1.static Vector3Creates aVector3from a 3x1MatrixInterface.static Vector3Generates a randomVector3with components within a specified range.static <K> Kreduce(ArrayList<Vector3> vectors, BiFunction<Vector3, K, K> reducer, K initialValue) A generic reduction function for a list of vectors.static Vector3reduceToVector3(List<Vector3> vectors, BiFunction<Vector3, Vector3, Vector3> reducer) Reduces a list of vectors into a single vector using a binary reducer function.rotateAroundAxis(Vector3 axis, double angleDegrees) Rotates this vector around a given axis by a specified angle using Rodrigues' rotation formula.introws()Returns the number of rows in the matrix.scale(double d) Scales the vector by a scalar value.static Vector3setXComponent(Vector3 original, double xComp) static Vector3setYComponent(Vector3 original, double yComp) static Vector3setZComponent(Vector3 original, double zComp) stream()Subtracts another vector from this one.Converts this Vector3 to command palette syntaxtoObject()Projects this 3D vector onto a 2D Cartesian plane based on the camera's properties.toString()triple()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.j3d.engine.math.matrix.MatrixInterface
toMatrixString
-
Field Details
-
x
private final double x -
y
private final double y -
z
private final double z -
Y
A static constant representing the unit vector along the positive Y-axis (0, 1, 0). -
X
A static constant representing the unit vector along the positive X-axis (1, 0, 0). -
Z
A static constant representing the unit vector along the positive Z-axis (0, 0, 1). -
NY
A static constant representing the unit vector along the negative Y-axis (0, -1, 0). -
NX
A static constant representing the unit vector along the negative X-axis (-1, 0, 0). -
NZ
A static constant representing the unit vector along the negative Z-axis (0, 0, -1). -
ZERO
A static constant representing the zero vector (0, 0, 0). -
UNIT
A static constant representing the unit vector (1, 1, 1).
-
-
Constructor Details
-
Vector3
public Vector3(double[][] m) Constructs a new vector 3 from a 2d matrix double array- Parameters:
m- The 2d matrix double array
-
Vector3
Constructs a vector with the specified x, y, and z components.- Parameters:
x- The X component.y- The Y component.z- The Z component.
-
Vector3
public Vector3()Constructs a zero vector (0, 0, 0). -
Vector3
public Vector3(boolean empty)
-
-
Method Details
-
isNotEmpty
public boolean isNotEmpty()Checks if the vector is not set to the special "empty" value.- Returns:
trueif the vector holds meaningful data,falseotherwise.
-
of
Creates aVector3from a 3x1MatrixInterface.- Parameters:
m- The matrix to convert. Must have 3 rows and 1 column.- Returns:
- A new
Vector3instance. - Throws:
RuntimeException- if the matrix is not 3x1.
-
getX
public double getX()- Returns:
- The X component of the vector.
-
getY
public double getY()- Returns:
- The Y component of the vector.
-
getZ
public double getZ()- Returns:
- The Z component of the vector.
-
dot
Calculates the dot product of this vector and another.- Parameters:
v- The other vector.- Returns:
- The scalar dot product.
-
normalize
Normalizes the vector to have a magnitude of 1.- Returns:
- A new, normalized
Vector3.
-
add
Adds another vector to this one.- Parameters:
v- The vector to add.- Returns:
- A new
Vector3representing the sum.
-
magnitude
public double magnitude()Calculates the magnitude (length) of the vector.- Returns:
- The magnitude of the vector.
-
mult
Multiplies the vector by a scalar value.- Parameters:
scalar- The scalar value to multiply by.- Returns:
- A new, scaled
Vector3.
-
mult
Performs component-wise multiplication (Hadamard product) with another vector.- Parameters:
B- The vector to multiply by.- Returns:
- A new
Vector3with the component-wise product.
-
div
Divides the vector by a scalar value.- Parameters:
scalar- The scalar value to divide by.- Returns:
- A new
Vector3with the divided values.
-
cross
Calculates the cross product of this vector and another.- Parameters:
B- The other vector.- Returns:
- A new
Vector3representing the cross product, which is orthogonal to both vectors.
-
sub
Subtracts another vector from this one.- Parameters:
B- The vector to subtract.- Returns:
- A new
Vector3representing the difference.
-
distance
Calculates the Euclidean distance between this vector (as a point) and another.- Parameters:
B- The other vector.- Returns:
- The distance between the two points.
-
random
Generates a randomVector3with components within a specified range.- Parameters:
low- The lower bound vector for the range.high- The upper bound vector for the range.- Returns:
- A new random
Vector3.
-
copy
Creates a deep copy of this vector.- Specified by:
copyin interfaceMatrixInterface- Returns:
- A new
Vector3instance with the same X, Y, and Z values.
-
toPoint
Projects this 3D vector onto a 2D Cartesian plane based on the camera's properties. This method transforms the world-space vector into camera space and then projects it onto the camera's 2D projection plane.- Parameters:
cam- The camera defining the viewpoint and projection.- Returns:
- The resulting 2D
CartesianPoint.
-
equals
-
hashCode
public int hashCode() -
distanceSquaredTo
Calculates the squared 2D distance from this vector's X and Y components to aCartesianPoint. The Z component is ignored. This is useful for fast 2D distance comparisons.- Parameters:
point- The 2D point.- Returns:
- The squared distance in the XY plane.
-
reduceToVector3
public static Vector3 reduceToVector3(List<Vector3> vectors, BiFunction<Vector3, Vector3, Vector3> reducer) Reduces a list of vectors into a single vector using a binary reducer function.- Parameters:
vectors- The list of vectors to reduce.reducer- ABiFunctionthat combines two vectors into one.- Returns:
- The final reduced
Vector3.
-
reduce
public static <K> K reduce(ArrayList<Vector3> vectors, BiFunction<Vector3, K, K> reducer, K initialValue) A generic reduction function for a list of vectors.- Type Parameters:
K- The type of the accumulated value.- Parameters:
vectors- The list of vectors to process.reducer- The function to apply to each vector and the accumulated value.initialValue- The initial value for the reduction.- Returns:
- The final accumulated value.
-
toString
-
rotateAroundAxis
Rotates this vector around a given axis by a specified angle using Rodrigues' rotation formula.- Parameters:
axis- The axis to rotate around (should be a normalized vector).angleDegrees- The angle in degrees to rotate.- Returns:
- A new
Vector3representing the rotated vector.
-
scale
Scales the vector by a scalar value. Alias formult(double).- Parameters:
d- The scalar value to multiply by.- Returns:
- A new, scaled
Vector3.
-
rows
public int rows()Description copied from interface:MatrixInterfaceReturns the number of rows in the matrix.- Specified by:
rowsin interfaceMatrixInterface- Returns:
- The number of rows, which is always 3 for a Vector3.
-
cols
public int cols()Description copied from interface:MatrixInterfaceReturns the number of columns in the matrix.- Specified by:
colsin interfaceMatrixInterface- Returns:
- The number of columns, which is always 1 for a Vector3.
-
get
public double get(int row, int col) Gets the value at a specific row in this 3x1 column matrix.- Specified by:
getin interfaceMatrixInterface- Parameters:
row- The row index (0 for X, 1 for Y, 2 for Z).col- The column index (must be 0).- Returns:
- The value of the corresponding component.
-
get
public double[][] get()Returns the underlying data as a 2D array (3x1).- Specified by:
getin interfaceMatrixInterface- Returns:
- A new 2D double array
{{X}, {Y}, {Z}}.
-
toCommandPaletteString
Converts this Vector3 to command palette syntax- Returns:
- the string version of this vector3.
-
toObject
-
stream
-
triple
-
acrossX
Creates a vector along the X-axis with the specified magnitude.- Parameters:
x- The magnitude along the X-axis.- Returns:
- A new
Vector3(x, 0, 0).
-
acrossY
Creates a vector along the Y-axis with the specified magnitude.- Parameters:
y- The magnitude along the Y-axis.- Returns:
- A new
Vector3(0, y, 0).
-
acrossZ
Creates a vector along the Z-axis with the specified magnitude.- Parameters:
z- The magnitude along the Z-axis.- Returns:
- A new
Vector3(0, 0, z).
-
setXComponent
-
setYComponent
-
setZComponent
-
fromObject
-