Overview
Comment: | Add OFMatrix4x4 and OFVector3D
These will be useful for 3D graphics. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
44eb35b2124e981ae43c81d90d867810 |
User & Date: | js on 2023-02-12 22:20:39 |
Other Links: | manifest | tags |
Context
2023-02-13
| ||
20:32 | Support for storing OFColors in tagged pointers check-in: 51c8047121 user: js tags: trunk | |
2023-02-12
| ||
22:20 | Add OFMatrix4x4 and OFVector3D check-in: 44eb35b212 user: js tags: trunk | |
2023-02-11
| ||
23:49 | OFStreamSocket: Require open socket in -[accept] check-in: c02886307e user: js tags: trunk | |
2022-11-07
| ||
00:24 | Merge trunk into branch "3d-affine-transforms" Closed-Leaf check-in: fd7d3894ff user: js tags: 3d-affine-transforms | |
Changes
Modified src/Makefile from [40cede6562] to [cf49f1b080].
︙ | ︙ | |||
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | OFInvocation.m \ OFLHAArchive.m \ OFLHAArchiveEntry.m \ OFList.m \ OFLocale.m \ OFMD5Hash.m \ OFMapTable.m \ OFMemoryStream.m \ OFMessagePackExtension.m \ OFMethodSignature.m \ OFMutableArray.m \ OFMutableData.m \ OFMutableDictionary.m \ OFMutableIRI.m \ | > | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | OFInvocation.m \ OFLHAArchive.m \ OFLHAArchiveEntry.m \ OFList.m \ OFLocale.m \ OFMD5Hash.m \ OFMapTable.m \ OFMatrix4x4.m \ OFMemoryStream.m \ OFMessagePackExtension.m \ OFMethodSignature.m \ OFMutableArray.m \ OFMutableData.m \ OFMutableDictionary.m \ OFMutableIRI.m \ |
︙ | ︙ |
Added src/OFMatrix4x4.h version [f8bbebd161].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" OF_ASSUME_NONNULL_BEGIN /** * @brief A 4x4 matrix of floats. */ OF_SUBCLASSING_RESTRICTED @interface OFMatrix4x4: OFObject <OFCopying> { float _values[16]; } #ifdef OF_HAVE_CLASS_PROPERTIES @property (readonly, class) OFMatrix4x4 *identityMatrix; #endif /** * @brief An array of the 16 floats of the 4x4 matrix in column-major format. * * These may be modified directly. */ @property (readonly, nonatomic) float *values; /** * @brief Returns the 4x4 identity matrix. */ + (OFMatrix4x4 *)identityMatrix; /** * @brief Creates a new 4x4 matrix with the specified values. * * @param values An array of 16 floats in column-major format * @return A new, autoreleased OFMatrix4x4 */ + (instancetype)matrixWithValues: (const float [_Nonnull 16])values; /** * @brief Initializes an already allocated 4x4 matrix with the specified values. * * @param values An array of 16 floats in column-major format * @return An initialized OFMatrix4x4 */ - (instancetype)initWithValues: (const float [_Nonnull 16])values; /** * @brief Transposes the matrix. */ - (void)transpose; /** * @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 */ - (void)multiplyWithMatrix: (OFMatrix4x4 *)matrix; @end OF_ASSUME_NONNULL_END |
Added src/OFMatrix4x4.m version [8c09d10573].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFMatrix4x4.h" #import "OFString.h" #import "OFOnce.h" static const float identityValues[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; static OFMatrix4x4 *identityMatrix; static void initIdentityMatrix(void) { identityMatrix = [[OFMatrix4x4 alloc] initWithValues: identityValues]; } @implementation OFMatrix4x4 + (void)initialize { if (self != [OFMatrix4x4 class]) return; } + (OFMatrix4x4 *)identityMatrix { static OFOnceControl onceControl = OFOnceControlInitValue; OFOnce(&onceControl, initIdentityMatrix); return identityMatrix; } + (instancetype)matrixWithValues: (const float [16])values { return [[[self alloc] initWithValues: values] autorelease]; } - (instancetype)initWithValues: (const float [16])values { self = [super init]; memcpy(_values, values, 16 * sizeof(float)); return self; } - (float *)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, 16 * sizeof(float)) == 0); } - (unsigned long)hash { unsigned long hash; OFHashInit(&hash); for (size_t i = 0; i < 16; i++) OFHashAddHash(&hash, OFFloatToRawUInt32(_values[i])); OFHashFinalize(&hash); return hash; } - (void)transpose { float copy[16]; memcpy(copy, _values, 16 * sizeof(float)); _values[1] = copy[4]; _values[2] = copy[8]; _values[3] = copy[12]; _values[4] = copy[1]; _values[6] = copy[9]; _values[7] = copy[13]; _values[8] = copy[2]; _values[9] = copy[6]; _values[11] = copy[14]; _values[12] = copy[3]; _values[13] = copy[7]; _values[14] = copy[11]; } - (void)multiplyWithMatrix: (OFMatrix4x4 *)matrix { float copy[16]; memcpy(copy, _values, 16 * sizeof(float)); _values[0] = matrix->_values[0] * copy[0] + matrix->_values[4] * copy[1] + matrix->_values[8] * copy[2] + matrix->_values[12] * copy[3]; _values[1] = matrix->_values[1] * copy[0] + matrix->_values[5] * copy[1] + matrix->_values[9] * copy[2] + matrix->_values[13] * copy[3]; _values[2] = matrix->_values[2] * copy[0] + matrix->_values[6] * copy[1] + matrix->_values[10] * copy[2] + matrix->_values[14] * copy[3]; _values[3] = matrix->_values[3] * copy[0] + matrix->_values[7] * copy[1] + matrix->_values[11] * copy[2] + matrix->_values[15] * copy[3]; _values[4] = matrix->_values[0] * copy[4] + matrix->_values[4] * copy[5] + matrix->_values[8] * copy[6] + matrix->_values[12] * copy[7]; _values[5] = matrix->_values[1] * copy[4] + matrix->_values[5] * copy[5] + matrix->_values[9] * copy[6] + matrix->_values[13] * copy[7]; _values[6] = matrix->_values[2] * copy[4] + matrix->_values[6] * copy[5] + matrix->_values[10] * copy[6] + matrix->_values[14] * copy[7]; _values[7] = matrix->_values[3] * copy[4] + matrix->_values[7] * copy[5] + matrix->_values[11] * copy[6] + matrix->_values[15] * copy[7]; _values[8] = matrix->_values[0] * copy[8] + matrix->_values[4] * copy[9] + matrix->_values[8] * copy[10] + matrix->_values[12] * copy[11]; _values[9] = matrix->_values[1] * copy[8] + matrix->_values[5] * copy[9] + matrix->_values[9] * copy[10] + matrix->_values[13] * copy[11]; _values[10] = matrix->_values[2] * copy[8] + matrix->_values[6] * copy[9] + matrix->_values[10] * copy[10] + matrix->_values[14] * copy[11]; _values[11] = matrix->_values[3] * copy[8] + matrix->_values[7] * copy[9] + matrix->_values[11] * copy[10] + matrix->_values[15] * copy[11]; _values[12] = matrix->_values[0] * copy[12] + matrix->_values[4] * copy[13] + matrix->_values[8] * copy[14] + matrix->_values[12] * copy[15]; _values[13] = matrix->_values[1] * copy[12] + matrix->_values[5] * copy[13] + matrix->_values[9] * copy[14] + matrix->_values[13] * copy[15]; _values[14] = matrix->_values[2] * copy[12] + matrix->_values[6] * copy[13] + matrix->_values[10] * copy[14] + matrix->_values[14] * copy[15]; _values[15] = matrix->_values[3] * copy[12] + matrix->_values[7] * copy[13] + matrix->_values[11] * copy[14] + matrix->_values[15] * copy[15]; } - (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], _values[4], _values[8], _values[12], _values[1], _values[5], _values[9], _values[13], _values[2], _values[6], _values[10], _values[14], _values[3], _values[7], _values[11], _values[15]]; } @end |
Modified src/OFObject.h from [797f554cc7] to [06e4bde8d0].
︙ | ︙ | |||
288 289 290 291 292 293 294 295 296 297 298 299 300 301 | { if (!OFEqualPoints(rect1.origin, rect2.origin)) return false; if (!OFEqualSizes(rect1.size, rect2.size)) return false; return true; } /** * @brief Adds the specified byte to the hash. * * @param hash A pointer to a hash to add the byte to | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | { if (!OFEqualPoints(rect1.origin, rect2.origin)) return false; if (!OFEqualSizes(rect1.size, rect2.size)) return false; return true; } /** * @struct OFVector3D OFObject.h ObjFW/OFObject.h * * @brief A vector in 3D space. */ typedef struct OF_BOXABLE { /** The x coordinate of the vector */ float x; /** The y coordinate of the vector */ float y; /** The z coordinate of the vector */ float z; } OFVector3D; /** * @brief Creates a new OFVector3D. * * @param x The x coordinate of the vector * @param y The x coordinate of the vector * @param z The z coordinate of the vector * @return An OFVector3D with the specified coordinates */ static OF_INLINE OFVector3D OF_CONST_FUNC OFMakeVector3D(float x, float y, float z) { OFVector3D vector = { x, y, z }; return vector; } /** * @brief Returns whether the two vectors are equal. * * @param vector1 The first vector for the comparison * @param vector2 The second vector for the comparison * @return Whether the two vectors are equal */ static OF_INLINE bool OFEqualVectors3D(OFVector3D vector1, OFVector3D vector2) { if (vector1.x != vector2.x) return false; if (vector1.y != vector2.y) return false; if (vector1.z != vector2.z) return false; return true; } /** * @brief Adds the specified byte to the hash. * * @param hash A pointer to a hash to add the byte to |
︙ | ︙ |
Modified src/ObjFW.h from [91747980ba] to [1712ca6be5].
︙ | ︙ | |||
133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #import "OFApplication.h" #import "OFSystemInfo.h" #import "OFLocale.h" #import "OFOptionsParser.h" #import "OFTimer.h" #import "OFRunLoop.h" #ifdef OF_WINDOWS # import "OFWindowsRegistryKey.h" #endif #import "OFAllocFailedException.h" #import "OFAlreadyOpenException.h" | > > | 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #import "OFApplication.h" #import "OFSystemInfo.h" #import "OFLocale.h" #import "OFOptionsParser.h" #import "OFTimer.h" #import "OFRunLoop.h" #import "OFMatrix4x4.h" #ifdef OF_WINDOWS # import "OFWindowsRegistryKey.h" #endif #import "OFAllocFailedException.h" #import "OFAlreadyOpenException.h" |
︙ | ︙ |
Modified tests/Makefile from [517e3febc0] to [950b9c643e].
︙ | ︙ | |||
28 29 30 31 32 33 34 35 36 37 38 39 40 41 | OFINIFileTests.m \ OFIRITests.m \ OFInvocationTests.m \ OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMD5HashTests.m \ OFMemoryStreamTests.m \ OFMethodSignatureTests.m \ OFNotificationCenterTests.m \ OFNumberTests.m \ OFObjectTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ | > | 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | OFINIFileTests.m \ OFIRITests.m \ OFInvocationTests.m \ OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMD5HashTests.m \ OFMatrix4x4Tests.m \ OFMemoryStreamTests.m \ OFMethodSignatureTests.m \ OFNotificationCenterTests.m \ OFNumberTests.m \ OFObjectTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ |
︙ | ︙ |
Added tests/OFMatrix4x4Tests.m version [9a374ca140].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "TestsAppDelegate.h" static OFString *const module = @"OFMatrix4x4Tests"; @implementation TestsAppDelegate (OFMatrix4x4Tests) - (void)matrix4x4Tests { void *pool = objc_autoreleasePoolPush(); OFMatrix4x4 *matrix, *matrix2; TEST(@"+[identityMatrix]", memcmp([[OFMatrix4x4 identityMatrix] values], (float [16]){ 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 [16]){ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }])) TEST(@"-[description]", [matrix.description isEqual: @"<OFMatrix4x4: {\n" @"\t1 5 9 13\n" @"\t2 6 10 14\n" @"\t3 7 11 15\n" @"\t4 8 12 16\n" @"}>"]) TEST(@"-[transpose]", R([matrix transpose]) && memcmp(matrix.values, (float [16]){ 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16 }, 16 * sizeof(float)) == 0) TEST(@"-[isEqual:]", [[OFMatrix4x4 identityMatrix] isEqual: [OFMatrix4x4 matrixWithValues: (float [16]){ 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 [16]){ 100, 500, 900, 1300, 200, 600, 1000, 1400, 300, 700, 1100, 1500, 400, 800, 1200, 1600 }]; TEST(@"-[multiplyWithMatrix:] #2", R([matrix2 multiplyWithMatrix: matrix]) && [matrix2 isEqual: [OFMatrix4x4 matrixWithValues: (float [16]){ 9000, 20200, 31400, 42600, 10000, 22800, 35600, 48400, 11000, 25400, 39800, 54200, 12000, 28000, 44000, 60000 }]]) objc_autoreleasePoolPop(pool); } @end |
Modified tests/TestsAppDelegate.h from [310deacabd] to [6965bfbc0f].
︙ | ︙ | |||
142 143 144 145 146 147 148 149 150 151 152 153 154 155 | @interface TestsAppDelegate (OFLocaleTests) - (void)localeTests; @end @interface TestsAppDelegate (OFMD5HashTests) - (void)MD5HashTests; @end @interface TestsAppDelegate (OFMemoryStreamTests) - (void)memoryStreamTests; @end @interface TestsAppDelegate (OFMethodSignatureTests) - (void)methodSignatureTests; | > > > > | 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | @interface TestsAppDelegate (OFLocaleTests) - (void)localeTests; @end @interface TestsAppDelegate (OFMD5HashTests) - (void)MD5HashTests; @end @interface TestsAppDelegate (OFMatrix4x4Tests) - (void)matrix4x4Tests; @end @interface TestsAppDelegate (OFMemoryStreamTests) - (void)memoryStreamTests; @end @interface TestsAppDelegate (OFMethodSignatureTests) - (void)methodSignatureTests; |
︙ | ︙ |
Modified tests/TestsAppDelegate.m from [b2830d46cd] to [7b3ff2f71c].
︙ | ︙ | |||
438 439 440 441 442 443 444 445 446 447 448 449 450 451 | #endif [self XMLParserTests]; [self XMLNodeTests]; [self XMLElementBuilderTests]; [self serializationTests]; [self JSONTests]; [self propertyListTests]; #if defined(OF_HAVE_PLUGINS) [self pluginTests]; #endif #ifdef OF_WINDOWS [self windowsRegistryKeyTests]; #endif | > > | 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | #endif [self XMLParserTests]; [self XMLNodeTests]; [self XMLElementBuilderTests]; [self serializationTests]; [self JSONTests]; [self propertyListTests]; [self matrix4x4Tests]; #if defined(OF_HAVE_PLUGINS) [self pluginTests]; #endif #ifdef OF_WINDOWS [self windowsRegistryKeyTests]; #endif |
︙ | ︙ |