Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic functions for change of basis in 3D #939

Merged
merged 16 commits into from
Jun 30, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addons/vectors/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
class CfgFunctions {
class CBA {
class Vectors {
PATHTO_FNC(matrixProduct3D);
PATHTO_FNC(matrixTranspose);
PATHTO_FNC(polar2vect);
PATHTO_FNC(scaleVect);
PATHTO_FNC(scaleVectTo);
Expand All @@ -16,6 +18,7 @@ class CfgFunctions {
PATHTO_FNC(vectElev);
PATHTO_FNC(vectMagn);
PATHTO_FNC(vectMagn2D);
PATHTO_FNC(vectMap3D);
PATHTO_FNC(vectRotate2D);
PATHTO_FNC(vectRotate3D);
PATHTO_FNC(vectSubtract);
Expand Down
33 changes: 33 additions & 0 deletions addons/vectors/fnc_matrixProduct3D.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_matrixProduct3D

Description:
Returns the resulting matrix from the matrix-matrix product.

Only accepts 3x3 matrices.

Parameters:
_matrixA - first 3x3 matrix. <ARRAY>
_matrixB - second 3x3 matrix. <ARRAY>

Returns:
_returnMatrix - 3x3 matrix returned after matrix multiplication <ARRAY>

Examples:
(begin example)
// point reflection at origin [0,0,0] for every vector in matrix B
[[[-1,0,0],[0,-1,0],[0,0,-1]], [[1,2,3], [3,1,2], [2,3,1]]] call CBA_fnc_matrixProduct3D;
(end)

Author:
Kex
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_matrixA", [], [[]], 3], ["_matrixB", [], [[]], 3]];

_matrixB = [_matrixB] call CBA_fnc_matrixTranspose;
_matrixA apply {
private _rowA = _x;
_matrixB apply {_rowA vectorDotProduct _x}
} // return
34 changes: 34 additions & 0 deletions addons/vectors/fnc_matrixTranspose.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_matrixTranspose

Description:
Returns the transposed matrix.

Accepts any mxn matrix.

Parameters:
_matrix - mxn matrix to transpose. <ARRAY>

Returns:
_returnMatrix - nxm matrix <ARRAY>

Examples:
(begin example)
// returns the transposed matrix [[1,3,2], [2,1,3], [3,2,1]]
[[[1,2,3], [3,1,2], [2,3,1]]] call CBA_fnc_matrixTranspose;
(end)

Author:
Kyle Kotowick, Kex
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_matrix", [], [[]]]];

private _returnMatrix = [];

for "_j" from 0 to (count (_matrix select 0) - 1) do {
_returnMatrix pushBack (_matrix apply {_x select _j});
};

_returnMatrix
29 changes: 29 additions & 0 deletions addons/vectors/fnc_vectMap3D.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_vectMap3D

Description:
Returns the resulting vector of the matrix-vector product.

Only accepts a 3x3 matrix and 3D vector.

Parameters:
_matrix - 3x3 matrix, which serves as a map. <ARRAY>
_vector - 3D vector that is mapped. <ARRAY>

Returns:
_returnVector - 3D vector returned after matrix multiplication <ARRAY>

Examples:
(begin example)
// point reflection at origin [0,0,0]
[[[-1,0,0],[0,-1,0],[0,0,-1]], [1,2,3]] call CBA_fnc_vectMap3D;
(end)

Author:
Kex
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_matrix", [], [[]], 3], ["_vector", [], [[]], 3]];

_matrix apply {_x vectorDotProduct _vector} // return
52 changes: 51 additions & 1 deletion addons/vectors/test_vectors.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ private _fnc_vectorEquals = {
if ((count _vector1) != (count _vector2)) exitWith {false};
private _equal = true;
{
if ((abs (_x - (_vector2 select _forEachIndex))) > 0.00001) then {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replaced by findIf I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findif does not support _forEachIndex

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to, findIf is kind of hard to read.

if ((abs (_x - (_vector2 select _forEachIndex))) > 0.00001) exitWith {
_equal = false;
};
} forEach _vector1;
_equal
};

//Need custom func to compare matrices to handle floating point errors
private _fnc_matrixEquals = {
params ["_matrix1", "_matrix2"];
if ((isNil "_matrix1") || {isNil "_matrix2"}) exitWith {false};
if (!(_matrix1 isEqualTypeArray _matrix2)) exitWith {false};
if ((count _matrix1) != (count _matrix2)) exitWith {false};
private _equal = true;
{
if !([ARR_2(_x, _matrix2 select _forEachIndex)] call _fnc_vectorEquals) exitWith {
_equal = false;
};
} forEach _matrix1;
_equal
};

// ----------------------------------------------------------------------------

LOG('Testing Vectors');
Expand Down Expand Up @@ -296,4 +311,39 @@ _result = _temp call CBA_fnc_polar2vect;
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals, "complex polar 2");


// UNIT TESTS (vectMap3D)
_fn = "CBA_fnc_vectMap3D";
TEST_DEFINED("CBA_fnc_vectMap3D","");

_result = [[[0,1,0],[0,0,1],[1,0,0]], [1,2,3]] call CBA_fnc_vectMap3D;
_expected = [2,3,1];
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals, _fn);

_result = [[[1,2,5],[2,-1,3],[5,6,-1]], [1,2,3]] call CBA_fnc_vectMap3D;
_expected = [20,9,14];
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals, _fn);

// UNIT TESTS (matrixTranspose)
_fn = "CBA_fnc_matrixTranspose";
TEST_DEFINED("CBA_fnc_matrixTranspose","");

_result = [[[1,2,5],[-2,4,3],[5,6,8]]] call CBA_fnc_matrixTranspose;
_expected = [[1,-2,5],[2,4,6],[5,3,8]];
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_matrixEquals, _fn);

// UNIT TESTS (matrixProduct3D)
_fn = "CBA_fnc_matrixProduct3D";
TEST_DEFINED("CBA_fnc_matrixProduct3D","");

_result = [[[1,2,5],[2,-1,3],[5,6,-1]], [[1,2,3],[3,1,2],[2,3,1]]] call CBA_fnc_matrixProduct3D;
_expected = [[17,19,12],[5,12,7],[21,13,26]];
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_matrixEquals, _fn);

private _orthogonalMatrix = [[2/3,-1/3,2/3],[2/3,2/3,-1/3],[-1/3,2/3,2/3]];
private _orthogonalMatrixTransposed = [_orthogonalMatrix] call CBA_fnc_matrixTranspose;
_result = [_orthogonalMatrix, _orthogonalMatrixTransposed] call CBA_fnc_matrixProduct3D;
_expected = [[1,0,0],[0,1,0],[0,0,1]];
TEST_TRUE([ARR_2(_result,_expected)] call _fnc_matrixEquals, _fn);


nil;