fork to bluetooth account.
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/types/Matrix4.h@1:8aa5cdb4ab67, 2016-04-07 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Thu Apr 07 01:33:22 2016 +0100
- Revision:
- 1:8aa5cdb4ab67
Synchronized with git rev 55cb9199
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MICROBIT_MATRIX4_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_MATRIX4_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | * Class definition for a simple matrix, that is optimised for nx4 or 4xn matrices. |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | * This class is heavily optimised for these commonly used matrices as used in 3D geometry. |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | * Whilst this class does support basic operations on matrices of any dimension, it is not intended as a |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | * general purpose matrix class as inversion operations are only provided for 4x4 matrices. |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | * For programmers needing more flexible Matrix support, the Matrix and MatrixMath classes from |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | * Ernsesto Palacios provide a good basis: |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | * https://developer.mbed.org/cookbook/MatrixClass |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * https://developer.mbed.org/users/Yo_Robot/code/MatrixMath/ |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | class Matrix4 |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | float *data; // Linear buffer representing the matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | int rows; // The number of rows in the matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | int cols; // The number of columns in the matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | * Create a matrix of the given size. |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | * @param rows the number of rows in the matrix to be created. |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | * @param cols the number of columns in the matrix to be created. |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | * Matrix4(10, 4); // Creates a Matrix with 10 rows and 4 columns. |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | Matrix4(int rows, int cols); |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * Create a matrix that is an identical copy of the given matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | * @param matrix The matrix to copy. |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | * Matrix newMatrix(matrix); . |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | Matrix4(const Matrix4 &matrix); |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | * Determines the number of columns in this matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * @return The number of columns in the matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * int c = matrix.width(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | int width(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | * Determines the number of rows in this matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | * @return The number of rows in the matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | * int r = matrix.height(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | int height(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | * Reads the matrix element at the given position. |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | * @param row The row of the element to read. |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * @param col The column of the element to read. |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | * @return The value of the matrix element at the given position. 0 is returned if the given index is out of range. |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | * float v = matrix.get(1,2); |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | float get(int row, int col); |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | * Writes the matrix element at the given position. |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | * @param row The row of the element to write. |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | * @param col The column of the element to write. |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | * @param v The new value of the element. |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | * matrix.set(1,2,42.0); |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | void set(int row, int col, float v); |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | * Transposes this matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | * @return the resultant matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | * matrix.transpose(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | Matrix4 transpose(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | * Multiplies this matrix with the given matrix (if possible). |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | * @param matrix the matrix to multiply this matrix's values against. |
Jonathan Austin |
1:8aa5cdb4ab67 | 144 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 145 | * @param transpose Transpose the matrices before multiplication. Defaults to false. |
Jonathan Austin |
1:8aa5cdb4ab67 | 146 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 147 | * @return the resultant matrix. An empty matrix is returned if the operation canot be completed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 148 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 149 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 150 | * Matrix result = matrixA.multiply(matrixB); |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | Matrix4 multiply(Matrix4 &matrix, bool transpose = false); |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | * Multiplies the transpose of this matrix with the given matrix (if possible). |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | * @param matrix the matrix to multiply this matrix's values against. |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | * @return the resultant matrix. An empty matrix is returned if the operation canot be completed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | * Matrix result = matrixA.multiplyT(matrixB); |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 165 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 166 | Matrix4 multiplyT(Matrix4 &matrix); |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 168 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 169 | * Performs an optimised inversion of a 4x4 matrix. |
Jonathan Austin |
1:8aa5cdb4ab67 | 170 | * Only 4x4 matrices are supported by this operation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 171 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 172 | * @return the resultant matrix. An empty matrix is returned if the operation canot be completed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 173 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 174 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 175 | * Matrix result = matrixA.invert(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 176 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 177 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 178 | Matrix4 invert(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 180 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | * Destructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | * Frees any memory consumed by this Matrix4 instance. |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | ~Matrix4(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 189 | * Multiplies the transpose of this matrix with the given matrix (if possible). |
Jonathan Austin |
1:8aa5cdb4ab67 | 190 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 191 | * @return the resultant matrix. An empty matrix is returned if the operation canot be completed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 192 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 193 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 194 | * Matrix result = matrixA.multiplyT(matrixB); |
Jonathan Austin |
1:8aa5cdb4ab67 | 195 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 196 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 197 | inline Matrix4 Matrix4::multiplyT(Matrix4 &matrix) |
Jonathan Austin |
1:8aa5cdb4ab67 | 198 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 199 | return multiply(matrix, true); |
Jonathan Austin |
1:8aa5cdb4ab67 | 200 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 201 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 202 | #endif |