GeoSoft API

## no.geosoft.cc.geometry Class Matrix4x4

```Object
|
+--Matrix4x4
```

public class Matrix4x4
extends Object

Implementation of a 4x4 matrix suited for use in a 2D and 3D graphics rendering engine.

Author:
GeoSoft

 Constructor Summary `Matrix4x4()`           Construct a 4x4 identity matrix. `Matrix4x4(double[] m)`           Construct a 4x4 matrix with the specified element values. ```Matrix4x4(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)```           Construct a 4x4 matrix with the specified values. `Matrix4x4(Matrix4x4 matrix)`           Constrauct a 4x4 matrix as a copy of the specified matrix.

 Method Summary ` void` `add(Matrix4x4 matrix)`           Add the specified 4x4 matrix to this matrix. `static Matrix4x4` ```add(Matrix4x4 m1, Matrix4x4 m2)```           Add two matrices and return the result matrix. ` boolean` `equals(Object object)`           Check if this 4x4 matrix equals the specified object. ` double[]` `get()`           Return the values of this 4x4 matrix. ` double` ```getElement(int i, int j)```           Return matrix element [i,j]. `static Matrix4x4` `inverse(Matrix4x4 matrix)`           Return the inverse of the specified matrix. ` void` `invert()`           Invert this 4x4 matrix. ` void` `multiply(Matrix4x4 matrix)`           Multiply this 4x4 matrix with the specified matrix and store the result in this 4x4 matrix. `static Matrix4x4` ```multiply(Matrix4x4 m1, Matrix4x4 m2)```           Multiply two matrices and return the result matrix. ` Vector4` `multiply(Vector4 vector4)`           Multiply this 4x4 matrix with the specified vector. ` void` ```rotate(double angle, double[] p0, double[] p1)```           Apply rotation around an arbitrary axis. ` void` `rotateX(double angle)`           Apply rotation around X axis to this matrix. ` void` `rotateY(double angle)`           Apply rotation around Y axis to this matrix. ` void` `rotateZ(double angle)`           Apply rotation around z axis to this matrix. ` void` ```scale(double xScale, double yScale, double zScale)```           Apply scaling (relative to origo) to this 4x4 matrix. ` void` ```scale(double xScale, double yScale, double zScale, double[] fixedPoint)```           Apply scaling relative to a fixed point to this 4x4 matrix. ` void` `set(double[] m)`           Set the values of this 4x4 matrix. ` void` ```set(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)```           Set the values of this 4x4 matrix. ` void` `set(Matrix4x4 matrix)`           Set the value of this 4x4matrix according to the specified matrix ` void` ```setElement(int i, int j, double value)```           Set specified matrix element. ` void` `setIdentity()`           Make an identity matrix out of this 4x4 matrix. ` void` ```setWorld2DeviceTransform(double[] w0, double[] w1, double[] w2, int x0, int y0, int width, int height)```           Make this 4x4 matrix a world-2-device transformation matrix. ` Vector4` `solve(Vector4 vector)`           Solve the A x = b equation, where A is this 4x4 matrix, b is the specified result vector and the returned vector is the unknown x. ` String` `toString()`           Create a string representation of this matrix. ` double[]` `transformPoint(double[] point)`           Transform one coordinate using this 4x4 matrix. ` void` `transformPoints(double[] points)`           Transform a set of 3D coordinates using this 4x4 matrix. ` void` `transformPoints(int[] points)`           Transform a set of 3D coordinates using this 4x4 matrix. ` void` `transformXyPoints(double[] points)`           Transform a set of 2D (x,y) coordinates using this 4x4 matrix. ` void` `transformXyPoints(int[] points)`           Transform a set of 2D (x,y) coordinates using this 4x4 matrix. ` void` ```translate(double dx, double dy)```           Apply specified XY translation to this 4x4 matrix. ` void` ```translate(double dx, double dy, double dz)```           Apply specified translation to this 4x4 matrix.

 Methods inherited from class Object `getClass, hashCode, notify, notifyAll, wait, wait, wait`

 Constructor Detail

### Matrix4x4

`public Matrix4x4()`
Construct a 4x4 identity matrix.

### Matrix4x4

`public Matrix4x4(double[] m)`
Construct a 4x4 matrix with the specified element values.

Parameters:
`m` - Array of 16 matrix elements, m00, m01, etc.

### Matrix4x4

`public Matrix4x4(Matrix4x4 matrix)`
Constrauct a 4x4 matrix as a copy of the specified matrix.

Parameters:
`matrix` - Matrix to copy.

### Matrix4x4

```public Matrix4x4(double m00,
double m01,
double m02,
double m03,
double m10,
double m11,
double m12,
double m13,
double m20,
double m21,
double m22,
double m23,
double m30,
double m31,
double m32,
double m33)```
Construct a 4x4 matrix with the specified values.

Parameters:
`m00` - Value of element m[0,0].
`m01` - Value of element m[0,1].
`m02` - Value of element m[0,2].
`m03` - Value of element m[0,3].
`m10` - Value of element m[1,0].
`m11` - Value of element m[1,1].
`m12` - Value of element m[1,2].
`m13` - Value of element m[1,3].
`m20` - Value of element m[2,0].
`m21` - Value of element m[2,1].
`m22` - Value of element m[2,2].
`m23` - Value of element m[2,3].
`m30` - Value of element m[3,0].
`m31` - Value of element m[3,1].
`m32` - Value of element m[3,2].
`m33` - Value of element m[3,3].
 Method Detail

### setIdentity

`public void setIdentity()`
Make an identity matrix out of this 4x4 matrix.

### set

`public void set(Matrix4x4 matrix)`
Set the value of this 4x4matrix according to the specified matrix

Parameters:
`matrix` - Matrix to copy.

### set

`public void set(double[] m)`
Set the values of this 4x4 matrix.

Parameters:
`m` - Array of 16 matrix elements, m00, m01, etc.

### set

```public void set(double m00,
double m01,
double m02,
double m03,
double m10,
double m11,
double m12,
double m13,
double m20,
double m21,
double m22,
double m23,
double m30,
double m31,
double m32,
double m33)```
Set the values of this 4x4 matrix.

Parameters:
`m00` - Value of element m[0,0].
`m01` - Value of element m[0,1].
`m02` - Value of element m[0,2].
`m03` - Value of element m[0,3].
`m10` - Value of element m[1,0].
`m11` - Value of element m[1,1].
`m12` - Value of element m[1,2].
`m13` - Value of element m[1,3].
`m20` - Value of element m[2,0].
`m21` - Value of element m[2,1].
`m22` - Value of element m[2,2].
`m23` - Value of element m[2,3].
`m30` - Value of element m[3,0].
`m31` - Value of element m[3,1].
`m32` - Value of element m[3,2].
`m33` - Value of element m[3,3].

### get

`public double[] get()`
Return the values of this 4x4 matrix.

Returns:
Array ov values: m00, m01, etc.

### equals

`public boolean equals(Object object)`
Check if this 4x4 matrix equals the specified object.

Overrides:
`equals` in class `Object`
Parameters:
`object` - Object to check.
Returns:
True if the two are equal, false otherwise.
Throws:
`ClassCastException` - if object is not of type Matrix4x4.

### getElement

```public double getElement(int i,
int j)```
Return matrix element [i,j].

Parameters:
`i` - Row of element to get (first row is 0).
`j` - Column of element to get (first column is 0).
Returns:
Element at specified position.
Throws:
`ArrayOutOfBoundsException`

### setElement

```public void setElement(int i,
int j,
double value)```
Set specified matrix element.

Parameters:
`i` - Row of element to set (first row is 0).
`j` - Column of element to set (first column is 0).
`value` - New element value.
Throws:
`ArrayOutOfBoundsException`

`public void add(Matrix4x4 matrix)`
Add the specified 4x4 matrix to this matrix.

Parameters:
`matrix` - Matrix to add.

```public static Matrix4x4 add(Matrix4x4 m1,
Matrix4x4 m2)```
Add two matrices and return the result matrix.

Parameters:
`m1` - First matrix to add.
`m2` - Second matrix to add.
Returns:
Sum m1 + m2.

### multiply

`public void multiply(Matrix4x4 matrix)`
Multiply this 4x4 matrix with the specified matrix and store the result in this 4x4 matrix.

Parameters:
`matrix` - Matrix to multiply with.

### multiply

```public static Matrix4x4 multiply(Matrix4x4 m1,
Matrix4x4 m2)```
Multiply two matrices and return the result matrix.

Parameters:
`m1` - First matrix to multiply.
`m2` - Second matrix to multiply.
Returns:
Product m1 * m2.

### multiply

`public Vector4 multiply(Vector4 vector4)`
Multiply this 4x4 matrix with the specified vector.

Parameters:
`vector4` - Vector to multiply with.
Returns:
Result of operation.

### transformPoint

`public double[] transformPoint(double[] point)`
Transform one coordinate using this 4x4 matrix.

Parameters:
`point` - [x0,y0,z0]
Returns:
Result of operation: [x0',y0',z0']

### transformPoints

`public void transformPoints(double[] points)`
Transform a set of 3D coordinates using this 4x4 matrix. The result of the operation is put back in the original array.

### transformXyPoints

`public void transformXyPoints(double[] points)`
Transform a set of 2D (x,y) coordinates using this 4x4 matrix. The result of the operation is put back in the original array rounded to the nearest integer.

Parameters:
`points` - Points to transform [x0,y0,x1,y1,...].

### transformPoints

`public void transformPoints(int[] points)`
Transform a set of 3D coordinates using this 4x4 matrix. The result of the operation is put back in the original array.

Parameters:
`points` - Points to transform [x0,y0,z0,x1,y1,z1,...].

### transformXyPoints

`public void transformXyPoints(int[] points)`
Transform a set of 2D (x,y) coordinates using this 4x4 matrix. The result of the operation is put back in the original array rounded to the nearest integer.

Parameters:
`points` - Points to transform [x0,y0,x1,y1,...].

### translate

```public void translate(double dx,
double dy,
double dz)```
Apply specified translation to this 4x4 matrix.

Parameters:
`dx` - x translation.
`dy` - y translation.
`dz` - z translation.

### translate

```public void translate(double dx,
double dy)```
Apply specified XY translation to this 4x4 matrix.

Parameters:
`dx` - x translation.
`dy` - y translation.

### rotateX

`public void rotateX(double angle)`
Apply rotation around X axis to this matrix.

Parameters:
`angle` - Angle to rotate [radians].

### rotateY

`public void rotateY(double angle)`
Apply rotation around Y axis to this matrix.

Parameters:
`angle` - Angle to rotate [radians].

### rotateZ

`public void rotateZ(double angle)`
Apply rotation around z axis to this matrix.

Parameters:
`angle` - Angle to rotate [radians].

### rotate

```public void rotate(double angle,
double[] p0,
double[] p1)```
Apply rotation around an arbitrary axis. Ref: http://www.swin.edu.au/astronomy/pbourke/geometry/rotate/ (but be aware of errors, corrected here)

Parameters:
`angle` - Angle to rotate [radians]
`p0` - First point defining the axis (x,y,z)
`p1` - Second point defining the axis (x,y,z)

### scale

```public void scale(double xScale,
double yScale,
double zScale)```
Apply scaling (relative to origo) to this 4x4 matrix.

Parameters:
`xScale` - Scaling in x direction.
`yScale` - Scaling in y direction.
`zScale` - Scaling in z direction.

### scale

```public void scale(double xScale,
double yScale,
double zScale,
double[] fixedPoint)```
Apply scaling relative to a fixed point to this 4x4 matrix.

Parameters:
`xScale` - Scaling in x direction.
`yScale` - Scaling in y direction.
`zScale` - Scaling in z direction.
`fixedPoint` - Scaling origo.

### invert

`public void invert()`
Invert this 4x4 matrix.

### inverse

`public static Matrix4x4 inverse(Matrix4x4 matrix)`
Return the inverse of the specified matrix.

Parameters:
`matrix` - Matrix to finr the inverse of.
Returns:
Inverse of the specified matrix.

### solve

`public Vector4 solve(Vector4 vector)`
Solve the A x = b equation, where A is this 4x4 matrix, b is the specified result vector and the returned vector is the unknown x.

Parameters:
`vector` - Result vector
Returns:
Unknown vector.

### setWorld2DeviceTransform

```public void setWorld2DeviceTransform(double[] w0,
double[] w1,
double[] w2,
int x0,
int y0,
int width,
int height)```
Make this 4x4 matrix a world-2-device transformation matrix.

The world system is defined as follows:

```        w2 o
|
|
|
w0 o-------o w1

Each point is defined with x,y,z so this system may in effect be
arbitrary oriented in space, and may include sharing.

The device system is defined as follows:

width
x0,y0 o-------o
|
height |
|
o

The matrix maps w2 to (x0,y0), w0 to the lower left corner of the
device rectangle, and w1 to the lower right corner of the device
rectangle.

Parameters:`w0` - x,y,z coordinate of first world position.`w1` - x,y,z coordinate of second world position.`w2` - x,y,z coordinate of third world position.`x0` - X coordinate of upper left corner of device.`y0` - Y coordinate of upper left corner of device.`width` - Width of device`height` - Height of device.

```

### toString

`public String toString()`
Create a string representation of this matrix.

Overrides:
`toString` in class `Object`
Returns:
String representing this matrix.

GeoSoft API