|
|
## Vec and Mat
|
|
|
|
|
|
The `commons` project contains a set of classes to represent fixed size scalar vectors and matrices. (In [commons/utils](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/simulators/commons/-/tree/master/src/main/java/de/rwth/montisim/commons/utils)).
|
|
|
|
|
|
- `Vec2`, `Vec3`, `Vec4`: 2, 3 or 4 component vector.
|
|
|
- `Mat2`, `Mat3`, `Mat4`: 2x2, 3x3 or 4x4 matrix.
|
|
|
|
|
|
### Available Operations
|
|
|
|
|
|
Operations marked with "(+)" create a new object.
|
|
|
|
|
|
- Single Vector operations
|
|
|
- `set` variants
|
|
|
- `magnitude`
|
|
|
- (+) `normalize`
|
|
|
- (+) `clone`
|
|
|
- Vector-scalar
|
|
|
- (+) `multiply`
|
|
|
- Between two Vectors
|
|
|
- (+) `add`
|
|
|
- (+) `subtract`
|
|
|
- (+) `crossProduct`
|
|
|
- (+) `midpoint`
|
|
|
- `angle`
|
|
|
- `distance`
|
|
|
- `dotProduct`
|
|
|
- `equals` (with a certain precision)
|
|
|
- Single Matrix operations
|
|
|
- `set` variants
|
|
|
- `setZero`
|
|
|
- `setUnit`
|
|
|
- `setDiagonal`
|
|
|
- (+) `unit`
|
|
|
- (+) `fromRows`
|
|
|
- (+) `translationMatrix`
|
|
|
- (+) `rotationMatrix`
|
|
|
- (+) `crossMatrix`
|
|
|
- (+) `scaleMatrix`
|
|
|
- Matrix-Vector
|
|
|
- (+) `multiply`
|
|
|
- Matrix-Scalar
|
|
|
- (+) `multiply`
|
|
|
- Matrix-Matrix
|
|
|
- (+) `multiply`
|
|
|
- (+) `add`
|
|
|
- `equals` (with a certain precision)
|
|
|
|
|
|
## In Place Math (IPM)
|
|
|
|
|
|
Efficient algebra libraries in other languages typically use types that can be passed on the stack.
|
|
|
However in Java this is not possible expect for the existing primitive types (`int`, `float`, ...).
|
|
|
Thus, using traditional functions that return values when performing computations is inefficient on a big scale since a lot of objects have to be allocated and garbage collected.
|
|
|
|
|
|
This is where the `IPM` (*"In Place Math"*) [class](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/simulators/commons/-/blob/master/src/main/java/de/rwth/montisim/commons/utils/IPM.java) comes into play.
|
|
|
This class re-defines all the vector and matrix operations to be *in-place* (memory wise).
|
|
|
All the functions receive pointers for the arguments but also for the result vectors or matrices.
|
|
|
This allows algorithms to re-use allocated vectors or matrices instead of throwing them away.
|
|
|
|
|
|
### Standard syntax
|
|
|
|
|
|
- Functions of the form: **void** *__operator__*(*a*, *b*)
|
|
|
- These will perform the operation *a __operator__ b* and store the result in *a*.
|
|
|
- Example for addition: `IPM.add(a, b)` is equivalent to `a += b`.
|
|
|
- Functions of the form: **void** *__operator__*__To__(*target*, *a*, *b*)
|
|
|
- These will store the result of the operation *a __operator__ b* in the *target* vector or matrix.
|
|
|
- Example for subtraction: `IPM.subtract(t, a, b)` is equivalent to `t = a - b`.
|
|
|
|
|
|
### Available In-Place Operations
|
|
|
|
|
|
- Single Vector operations
|
|
|
- `normalize`
|
|
|
- Vector-Vector
|
|
|
- `add`
|
|
|
- `subtract`
|
|
|
- `multiply` (component-wise)
|
|
|
- `divide` (TODO)
|
|
|
- `dot`
|
|
|
- `cross` (only `Vec3`)
|
|
|
- `midpoint`
|
|
|
- `interpolate` (linear interpolation between two vectors with a factor)
|
|
|
- Vector-scalar
|
|
|
- `multiply`
|
|
|
- `divide` (TODO)
|
|
|
- Matrix-Scalar
|
|
|
- `multiply`
|
|
|
- Matrix-Vector
|
|
|
- `multiply`
|
|
|
- Matrix-Matrix
|
|
|
- `multiply`
|
|
|
- `add`
|
|
|
- `subtract`
|
|
|
- Single Matrix operations
|
|
|
- `orthonormalize`
|
|
|
- `transpose`
|
|
|
- `invert` (`Mat3`)
|
|
|
- Transformation matrix functions
|
|
|
- `cross-matrix`
|
|
|
- `scale` (full matrix and for 2D homogeneous coordinates)
|
|
|
- `translate` (for 2D & 3D homogeneous coordinates)
|
|
|
- `rotation` (for 2D homogeneous coordinates) |