Overview
Comment: | OFMatrix4x4: Use 2D arrays in row-major format
This allows writing matrices more naturally, without having to transpose |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
975a812f36966898949ee20d895bc4fc |
User & Date: | js on 2023-02-19 13:37:44 |
Other Links: | manifest | tags |
Context
2023-02-19
| ||
17:40 | Make GCC happy again check-in: d377e8819e user: js tags: trunk | |
13:37 | OFMatrix4x4: Use 2D arrays in row-major format check-in: 975a812f36 user: js tags: trunk | |
2023-02-18
| ||
23:53 | OFMatrix4x4: Don't create a singleton check-in: b924571763 user: js tags: trunk | |
Changes
Modified src/OFMatrix4x4.h from [d21230dbdd] to [817b6aa381].
︙ | ︙ | |||
19 20 21 22 23 24 25 | /** * @brief A 4x4 matrix of floats. */ OF_SUBCLASSING_RESTRICTED @interface OFMatrix4x4: OFObject <OFCopying> { | | | | | | | | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /** * @brief A 4x4 matrix of floats. */ OF_SUBCLASSING_RESTRICTED @interface OFMatrix4x4: OFObject <OFCopying> { float _values[4][4]; } #ifdef OF_HAVE_CLASS_PROPERTIES @property (readonly, class) OFMatrix4x4 *identityMatrix; #endif /** * @brief A 2D array of the 4x4 floats of the matrix in row-major format. * * These may be modified directly. */ @property (readonly, nonatomic) float (*values)[4][4]; /** * @brief Returns the 4x4 identity matrix. */ + (OFMatrix4x4 *)identityMatrix; /** * @brief Creates a new 4x4 matrix with the specified values. * * @param values A 2D array of 4x4 floats in row-major format * @return A new, autoreleased OFMatrix4x4 */ + (instancetype)matrixWithValues: (const float [_Nonnull 4][4])values; /** * @brief Initializes an already allocated 4x4 matrix with the specified values. * * @param values A 2D array of 4x4 floats in row-major format * @return An initialized OFMatrix4x4 */ - (instancetype)initWithValues: (const float [_Nonnull 4][4])values; /** * @brief Mulitplies the receiver with the specified matrix on the left side * and the receiver on the right side. * * @param matrix The matrix to multiply the receiver with */ |
︙ | ︙ |
Modified src/OFMatrix4x4.m from [81834eec17] to [37363ee709].
︙ | ︙ | |||
15 16 17 18 19 20 21 | #include "config.h" #import "OFMatrix4x4.h" #import "OFOnce.h" #import "OFString.h" | | | | | | | | | | | | | > | | | > > | > | > | > | | > | > | > | > | | > | > | < > | > | | > > > > > > > > > | | | | | | | | | | | < < | | > | > | > > | | | | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include "config.h" #import "OFMatrix4x4.h" #import "OFOnce.h" #import "OFString.h" static const float identityValues[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; @implementation OFMatrix4x4 + (OFMatrix4x4 *)identityMatrix { return [[[OFMatrix4x4 alloc] initWithValues: identityValues] autorelease]; } + (instancetype)matrixWithValues: (const float [4][4])values { return [[[self alloc] initWithValues: values] autorelease]; } - (instancetype)initWithValues: (const float [4][4])values { self = [super init]; memcpy(_values, values, sizeof(_values)); return self; } - (float (*)[4][4])values { return &_values; } - (instancetype)copy { return [[OFMatrix4x4 alloc] initWithValues: _values]; } - (bool)isEqual: (OFMatrix4x4 *)matrix { if (![matrix isKindOfClass: [OFMatrix4x4 class]]) return false; return (memcmp(_values, matrix->_values, sizeof(_values)) == 0); } - (unsigned long)hash { unsigned long hash; OFHashInit(&hash); for (uint_fast8_t i = 0; i < 4; i++) for (uint_fast8_t j = 0; j < 4; j++) OFHashAddHash(&hash, OFFloatToRawUInt32(_values[i][j])); OFHashFinalize(&hash); return hash; } - (void)multiplyWithMatrix: (OFMatrix4x4 *)matrix { float right[4][4]; memcpy(right, _values, sizeof(right)); #define left matrix->_values _values[0][0] = left[0][0] * right[0][0] + left[0][1] * right[1][0] + left[0][2] * right[2][0] + left[0][3] * right[3][0]; _values[0][1] = left[0][0] * right[0][1] + left[0][1] * right[1][1] + left[0][2] * right[2][1] + left[0][3] * right[3][1]; _values[0][2] = left[0][0] * right[0][2] + left[0][1] * right[1][2] + left[0][2] * right[2][2] + left[0][3] * right[3][2]; _values[0][3] = left[0][0] * right[0][3] + left[0][1] * right[1][3] + left[0][2] * right[2][3] + left[0][3] * right[3][3]; _values[1][0] = left[1][0] * right[0][0] + left[1][1] * right[1][0] + left[1][2] * right[2][0] + left[1][3] * right[3][0]; _values[1][1] = left[1][0] * right[0][1] + left[1][1] * right[1][1] + left[1][2] * right[2][1] + left[1][3] * right[3][1]; _values[1][2] = left[1][0] * right[0][2] + left[1][1] * right[1][2] + left[1][2] * right[2][2] + left[1][3] * right[3][2]; _values[1][3] = left[1][0] * right[0][3] + left[1][1] * right[1][3] + left[1][2] * right[2][3] + left[1][3] * right[3][3]; _values[2][0] = left[2][0] * right[0][0] + left[2][1] * right[1][0] + left[2][2] * right[2][0] + left[2][3] * right[3][0]; _values[2][1] = left[2][0] * right[0][1] + left[2][1] * right[1][1] + left[2][2] * right[2][1] + left[2][3] * right[3][1]; _values[2][2] = left[2][0] * right[0][2] + left[2][1] * right[1][2] + left[2][2] * right[2][2] + left[2][3] * right[3][2]; _values[2][3] = left[2][0] * right[0][3] + left[2][1] * right[1][3] + left[2][2] * right[2][3] + left[2][3] * right[3][3]; _values[3][0] = left[3][0] * right[0][0] + left[3][1] * right[1][0] + left[3][2] * right[2][0] + left[3][3] * right[3][0]; _values[3][1] = left[3][0] * right[0][1] + left[3][1] * right[1][1] + left[3][2] * right[2][1] + left[3][3] * right[3][1]; _values[3][2] = left[3][0] * right[0][2] + left[3][1] * right[1][2] + left[3][2] * right[2][2] + left[3][3] * right[3][2]; _values[3][3] = left[3][0] * right[0][3] + left[3][1] * right[1][3] + left[3][2] * right[2][3] + left[3][3] * right[3][3]; #undef left } - (void)translateWithVector: (OFVector3D)vector { OFMatrix4x4 *translation = [[OFMatrix4x4 alloc] initWithValues: (float [4][4]){ { 1, 0, 0, vector.x }, { 0, 1, 0, vector.y }, { 0, 0, 1, vector.z }, { 0, 0, 0, 1 } }]; [self multiplyWithMatrix: translation]; [translation release]; } - (void)scaleWithVector: (OFVector3D)vector { OFMatrix4x4 *scale = [[OFMatrix4x4 alloc] initWithValues: (float [4][4]){ { vector.x, 0, 0, 0 }, { 0, vector.y, 0, 0 }, { 0, 0, vector.z, 0 }, { 0, 0, 0, 1 } }]; [self multiplyWithMatrix: scale]; [scale release]; } - (OFVector4D)transformedVector: (OFVector4D)vector { return OFMakeVector4D( _values[0][0] * vector.x + _values[0][1] * vector.y + _values[0][2] * vector.z + _values[0][3] * vector.w, _values[1][0] * vector.x + _values[1][1] * vector.y + _values[1][2] * vector.z + _values[1][3] * vector.w, _values[2][0] * vector.x + _values[2][1] * vector.y + _values[2][2] * vector.z + _values[2][3] * vector.w, _values[3][0] * vector.x + _values[3][1] * vector.y + _values[3][2] * vector.z + _values[3][3] * vector.w); } - (OFString *)description { return [OFString stringWithFormat: @"<OFMatrix4x4: {\n" @"\t%g %g %g %g\n" @"\t%g %g %g %g\n" @"\t%g %g %g %g\n" @"\t%g %g %g %g\n" @"}>", _values[0][0], _values[0][1], _values[0][2], _values[0][3], _values[1][0], _values[1][1], _values[1][2], _values[1][3], _values[2][0], _values[2][1], _values[2][2], _values[2][3], _values[3][0], _values[3][1], _values[3][2], _values[3][3]]; } @end |
Modified tests/OFMatrix4x4Tests.m from [b1e1780492] to [e27fadae34].
︙ | ︙ | |||
23 24 25 26 27 28 29 | - (void)matrix4x4Tests { void *pool = objc_autoreleasePoolPush(); OFMatrix4x4 *matrix, *matrix2; OFVector4D point; TEST(@"+[identityMatrix]", | | | | | | | | | | | | | | | | | > | | | < | | | | | | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | - (void)matrix4x4Tests { void *pool = objc_autoreleasePoolPush(); OFMatrix4x4 *matrix, *matrix2; OFVector4D point; TEST(@"+[identityMatrix]", memcmp([[OFMatrix4x4 identityMatrix] values], (float [4][4]){ { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }, 16 * sizeof(float)) == 0) TEST(@"+[matrixWithValues:]", (matrix = [OFMatrix4x4 matrixWithValues: (float [4][4]){ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }])) TEST(@"-[description]", [matrix.description isEqual: @"<OFMatrix4x4: {\n" @"\t1 2 3 4\n" @"\t5 6 7 8\n" @"\t9 10 11 12\n" @"\t13 14 15 16\n" @"}>"]) TEST(@"-[isEqual:]", [[OFMatrix4x4 identityMatrix] isEqual: [OFMatrix4x4 matrixWithValues: (float [4][4]){ { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }]]) TEST(@"-[copy]", (matrix2 = [matrix copy]) && [matrix2 isEqual: matrix]) TEST(@"-[multiplyWithMatrix:] #1", R([matrix2 multiplyWithMatrix: [OFMatrix4x4 identityMatrix]]) && [matrix2 isEqual: matrix]) matrix2 = [OFMatrix4x4 matrixWithValues: (float [4][4]){ { 100, 200, 300, 400 }, { 500, 600, 700, 800 }, { 900, 1000, 1100, 1200 }, { 1300, 1400, 1500, 1600 } }]; TEST(@"-[multiplyWithMatrix:] #2", R([matrix2 multiplyWithMatrix: matrix]) && [matrix2 isEqual: [OFMatrix4x4 matrixWithValues: (float [4][4]){ { 9000, 10000, 11000, 12000 }, { 20200, 22800, 25400, 28000 }, { 31400, 35600, 39800, 44000 }, { 42600, 48400, 54200, 60000 } }]]) TEST(@"[-translateWithVector:]", R(matrix2 = [OFMatrix4x4 identityMatrix]) && R([matrix2 translateWithVector: OFMakeVector3D(1, 2, 3)]) && R(point = [matrix2 transformedVector: OFMakeVector4D(2, 3, 4, 1)]) && |
︙ | ︙ |