ObjFW  Check-in [1e550fabe3]

Overview
Comment:OFMatrix4x4: Add -[transformedVector3D:]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1e550fabe39dfe9a6abfa878d2cb205c63db30c2bb1e8e47a065d3f22d68a1a4
User & Date: js on 2023-02-15 21:54:40
Other Links: manifest | tags
Context
2023-02-16
19:59
OFVector3D -> OFPoint3D check-in: db7991d07b user: js tags: trunk
2023-02-15
21:54
OFMatrix4x4: Add -[transformedVector3D:] check-in: 1e550fabe3 user: js tags: trunk
2023-02-13
22:14
OFColor: Allow some imprecision for tagged pointer check-in: 061e16d166 user: js tags: trunk
Changes

Modified src/OFMatrix4x4.h from [f8bbebd161] to [6444eb96ee].

66
67
68
69
70
71
72














73
74
75
/**
 * @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







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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * @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;

/**
 * @brief Transforms the specified 3D vector according to the matrix.
 *
 * As multiplying a 4x4 matrix with a 3D vector is not defined, this extends
 * the 3D vector to a 4D vector with its `w` value being set to 0 and just
 * discards the `w` value of the resulting 4D vector for the returned 3D
 * vector. This allows reducing the number number of calculations performed and
 * is mostly useful for 3D graphics.
 *
 * @param vector The 3D vector to transform
 * @return The transformed 3D vector
 */
- (OFVector3D)transformedVector3D: (OFVector3D)vector;
@end

OF_ASSUME_NONNULL_END

Modified src/OFMatrix4x4.m from [61d81a3bf6] to [97bdb88364].

179
180
181
182
183
184
185











186
187
188
189
190
191
192
	    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"







>
>
>
>
>
>
>
>
>
>
>







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	    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];
}

- (OFVector3D)transformedVector3D: (OFVector3D)vector
{
	return OFMakeVector3D(
	    _values[0] * vector.x + _values[4] * vector.y +
	    _values[8] * vector.z,
	    _values[1] * vector.x + _values[5] * vector.y +
	    _values[9] * vector.z,
	    _values[2] * vector.x + _values[6] * vector.y +
	    _values[10] * vector.z);
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"<OFMatrix4x4: {\n"
	    @"\t%g %g %g %g\n"
	    @"\t%g %g %g %g\n"

Modified tests/OFMatrix4x4Tests.m from [9a374ca140] to [9aa8c2d363].

20
21
22
23
24
25
26

27
28
29
30
31
32
33
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







>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
static OFString *const module = @"OFMatrix4x4Tests";

@implementation TestsAppDelegate (OFMatrix4x4Tests)
- (void)matrix4x4Tests
{
	void *pool = objc_autoreleasePoolPush();
	OFMatrix4x4 *matrix, *matrix2;
	OFVector3D vec3;

	TEST(@"+[identityMatrix]",
	    memcmp([[OFMatrix4x4 identityMatrix] values], (float [16]){
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
82
83
84
85
86
87
88




89
90
91
	    [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







>
>
>
>



83
84
85
86
87
88
89
90
91
92
93
94
95
96
	    [matrix2 isEqual: [OFMatrix4x4 matrixWithValues: (float [16]){
		 9000, 20200, 31400, 42600,
		10000, 22800, 35600, 48400,
		11000, 25400, 39800, 54200,
		12000, 28000, 44000, 60000
	    }]])

	TEST(@"-[transformedVector3D:]",
	    R((vec3 = [matrix transformedVector3D: OFMakeVector3D(1, 2, 3)])) &&
	    vec3.x == 14 && vec3.y == 38 && vec3.z == 62)

	objc_autoreleasePoolPop(pool);
}
@end