ObjFW  Check-in [831ce30929]

Overview
Comment:Add initial OF4x4Matrix
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 3d-affine-transforms
Files: files | file ages | folders
SHA3-256: 831ce309297c376fc85ca6beea929c89451553621becf18e6f908d485f86bfbb
User & Date: js on 2021-09-24 01:35:26
Other Links: branch diff | manifest | tags
Context
2021-09-26
20:48
OF4x4Matrix: Add -[multiplyWithMatrix:] check-in: 42b5eee531 user: js tags: 3d-affine-transforms
2021-09-24
01:35
Add initial OF4x4Matrix check-in: 831ce30929 user: js tags: 3d-affine-transforms
2021-09-05
20:19
Don't automatically run tests check-in: be8865bcd2 user: js tags: trunk
Changes

Modified src/Makefile from [89f588f854] to [c22506806a].

1
2
3
4
5
6
7
8
9
10
11
12

13

14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21












+
-
+







include ../extra.mk

SUBDIRS = ${RUNTIME} exceptions encodings forwarding
SUBDIRS_AFTER = ${BRIDGE}
DISTCLEAN = Info.plist objfw-defs.h

SHARED_LIB = ${OBJFW_SHARED_LIB}
STATIC_LIB = ${OBJFW_STATIC_LIB}
FRAMEWORK = ${OBJFW_FRAMEWORK}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}

SRCS = OF4x4Matrix.m			\
SRCS = OFASPrintF.m			\
       OFASPrintF.m			\
       OFApplication.m			\
       OFArray.m			\
       OFBase64.m			\
       OFBlock.m			\
       OFCRC16.m			\
       OFCRC32.m			\
       OFCharacterSet.m			\

Added src/OF4x4Matrix.h version [f31b2162fa].




































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008-2021 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 OF4x4Matrix: OFObject
{
	float _values[16];
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (readonly, class) OF4x4Matrix *identity;
#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.
 */
+ (OF4x4Matrix *)identity;

/**
 * @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 OF4x4Matrix
 */
+ (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 OF4x4Matrix
 */
- (instancetype)initWithValues: (const float [_Nonnull 16])values;

/**
 * @brief Transposes the matrix.
 */
- (void)transpose;
@end

OF_ASSUME_NONNULL_END

Added src/OF4x4Matrix.m version [070da82233].






















































































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008-2021 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 "OF4x4Matrix.h"
#import "OFString.h"

static const float identityValues[16] = {
	1, 0, 0, 0,
	0, 1, 0, 0,
	0, 0, 1, 0,
	0, 0, 0, 1
};
static OF4x4Matrix *identity;

@implementation OF4x4Matrix
+ (void)initialize
{
	if (self != [OF4x4Matrix class])
		return;

	identity = [[OF4x4Matrix alloc] initWithValues: identityValues];
}

+ (OF4x4Matrix *)identity
{
	return identity;
}

+ (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;
}

- (bool)isEqual: (OF4x4Matrix *)matrix
{
	if (![matrix isKindOfClass: [OF4x4Matrix 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];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"<OF4x4Matrix: {\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/ObjFW.h from [304cf44661] to [090568b11f].

124
125
126
127
128
129
130


131
132
133
134
135
136
137
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139







+
+








#import "OFApplication.h"
#import "OFSystemInfo.h"
#import "OFLocale.h"
#import "OFOptionsParser.h"
#import "OFTimer.h"
#import "OFRunLoop.h"

#import "OF4x4Matrix.h"

#ifdef OF_WINDOWS
# import "OFWindowsRegistryKey.h"
#endif

#import "OFAllocFailedException.h"
#import "OFException.h"

Modified tests/Makefile from [aa8c0b4a05] to [ecc310e44e].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23







+







	${PROG_NOINST}.arm9	\
	${PROG_NOINST}.nds
DISTCLEAN = Info.plist

PROG_NOINST = tests${PROG_SUFFIX}
STATIC_LIB_NOINST = ${TESTS_STATIC_LIB}
SRCS = ForwardingTests.m		\
       OF4x4MatrixTests.m		\
       OFArrayTests.m			\
       ${OF_BLOCK_TESTS_M}		\
       OFCharacterSetTests.m		\
       OFDataTests.m			\
       OFDateTests.m			\
       OFDictionaryTests.m		\
       OFInvocationTests.m		\

Added tests/OF4x4MatrixTests.m version [e6c6082b2d].







































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008-2021 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 = @"OF4x4MatrixTests";

@implementation TestsAppDelegate (OF4x4MatrixTests)
- (void)_4x4MatrixTests
{
	void *pool = objc_autoreleasePoolPush();
	OF4x4Matrix *matrix;

	TEST(@"+[identity]",
	    memcmp([[OF4x4Matrix identity] 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 = [OF4x4Matrix matrixWithValues: (float [16]){
		 1,  2,  3,  4,
		 5,  6,  7,  8,
		 9, 10, 11, 12,
		13, 14, 15, 16
	    }]))

	TEST(@"-[description]",
	    [matrix.description isEqual: @"<OF4x4Matrix: {\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:]", [[OF4x4Matrix identity] isEqual:
	    [OF4x4Matrix matrixWithValues: (float [16]){
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
	    }]])

	objc_autoreleasePoolPop(pool);
}
@end

Modified tests/TestsAppDelegate.h from [36680e2682] to [b159be6a3f].

54
55
56
57
58
59
60




61
62
63
64
65
66
67
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71







+
+
+
+







	int _fails;
}

- (void)outputTesting: (OFString *)test inModule: (OFString *)module;
- (void)outputSuccess: (OFString *)test inModule: (OFString *)module;
- (void)outputFailure: (OFString *)test inModule: (OFString *)module;
@end

@interface TestsAppDelegate (OF4x4MatrixTests)
- (void)_4x4MatrixTests;
@end

@interface TestsAppDelegate (OFArrayTests)
- (void)arrayTests;
@end

@interface TestsAppDelegate (OFBlockTests)
- (void)blockTests;

Modified tests/TestsAppDelegate.m from [1130ab2e6c] to [654f876616].

372
373
374
375
376
377
378


379
380
381
382
383
384
385
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387







+
+







	[self XMLNodeTests];
	[self XMLElementBuilderTests];
#ifdef OF_HAVE_FILES
	[self serializationTests];
#endif
	[self JSONTests];
	[self propertyListTests];
	[self _4x4MatrixTests];

#if defined(OF_HAVE_PLUGINS)
	[self pluginTests];
#endif
#ifdef OF_WINDOWS
	[self windowsRegistryKeyTests];
#endif