ObjFW  Check-in [42b5eee531]

Overview
Comment:OF4x4Matrix: Add -[multiplyWithMatrix:]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 3d-affine-transforms
Files: files | file ages | folders
SHA3-256: 42b5eee5312c3d60568047572ee18fc585352836e23c7073e5b990cc3243adc7
User & Date: js on 2021-09-26 20:48:56
Other Links: branch diff | manifest | tags
Context
2021-10-14
23:45
Add OF3DVector check-in: e5f7c958ea user: js tags: 3d-affine-transforms
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
Changes

Modified src/OF4x4Matrix.h from [f31b2162fa] to [fdc5246dcd].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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







|







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief A 4x4 matrix of floats.
 */
OF_SUBCLASSING_RESTRICTED
@interface OF4x4Matrix: OFObject <OFCopying>
{
	float _values[16];
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (readonly, class) OF4x4Matrix *identity;
#endif
58
59
60
61
62
63
64








65
66
67
 */
- (instancetype)initWithValues: (const float [_Nonnull 16])values;

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








@end

OF_ASSUME_NONNULL_END







>
>
>
>
>
>
>
>



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 */
- (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: (OF4x4Matrix *)matrix;
@end

OF_ASSUME_NONNULL_END

Modified src/OF4x4Matrix.m from [070da82233] to [cd962a5d85].

54
55
56
57
58
59
60





61
62
63
64
65
66
67
	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);







>
>
>
>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	return self;
}

- (float *)values
{
	return _values;
}

- (instancetype)copy
{
	return [[OF4x4Matrix alloc] initWithValues: _values];
}

- (bool)isEqual: (OF4x4Matrix *)matrix
{
	if (![matrix isKindOfClass: [OF4x4Matrix class]])
		return false;

	return (memcmp(_values, matrix->_values, 16 * sizeof(float)) == 0);
95
96
97
98
99
100
101







































































102
103
104
105
106
107
108
	_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"







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	_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: (OF4x4Matrix *)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:
	    @"<OF4x4Matrix: {\n"
	    @"\t%g %g %g %g\n"
	    @"\t%g %g %g %g\n"

Modified tests/OF4x4MatrixTests.m from [e6c6082b2d] to [84ce4ae574].

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

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







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

static OFString *const module = @"OF4x4MatrixTests";

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

	TEST(@"+[identity]",
	    memcmp([[OF4x4Matrix identity] values], (float [16]){
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
60
61
62
63
64
65
66





















67
68
69
70
	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







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




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
	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
	    }]])

	TEST(@"-[copy]", (matrix2 = [matrix copy]) && [matrix2 isEqual: matrix])

	TEST(@"-[multiplyWithMatrix:] #1",
	    R([matrix2 multiplyWithMatrix: [OF4x4Matrix identity]]) &&
	    [matrix2 isEqual: matrix])

	matrix2 = [OF4x4Matrix 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: [OF4x4Matrix matrixWithValues: (float [16]){
		 9000, 20200, 31400, 42600,
		10000, 22800, 35600, 48400,
		11000, 25400, 39800, 54200,
		12000, 28000, 44000, 60000
	    }]])

	objc_autoreleasePoolPop(pool);
}
@end