ObjFW  Check-in [3e4ed4e59f]

Overview
Comment:OFMatrix4x4: Add translation and scaling
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3e4ed4e59f39319d626bf7abf299bfc10f94366692e7f272afd51a3f35faf5dd
User & Date: js on 2023-02-16 21:37:01
Other Links: manifest | tags
Context
2023-02-18
12:01
OFMatrix4x4: Transform vectors in 4D space check-in: e31a31bdcb user: js tags: trunk
2023-02-16
21:37
OFMatrix4x4: Add translation and scaling check-in: 3e4ed4e59f user: js tags: trunk
19:59
OFVector3D -> OFPoint3D check-in: db7991d07b user: js tags: trunk
Changes

Modified src/OFMatrix4x4.h from [32857d56d5] to [f55c478b84].

67
68
69
70
71
72
73














74
75
76
77
78
79
80
81
82
83
 * @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 point in 3D space according to the matrix.
 *
 * @param point The point to transform
 * @return The transformed point
 */
- (OFPoint3D)transformedPoint3D: (OFPoint3D)point;
@end

OF_ASSUME_NONNULL_END







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






|



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
 * @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 Translates the matrix with the specified 3D vector.
 *
 * @param vector The vector to translate the matrix with
 */
- (void)translateWithVector3D: (OFVector3D)vector;

/**
 * @brief Scales the matrix with the specified 3D vector.
 *
 * @param vector The vector to scale the matrix with
 */
- (void)scaleWithVector3D: (OFVector3D)vector;

/**
 * @brief Transforms the specified point in 3D space according to the matrix.
 *
 * @param point The point to transform
 * @return The transformed point
 */
- (OFVector3D)transformedPoint3D: (OFVector3D)point;
@end

OF_ASSUME_NONNULL_END

Modified src/OFMatrix4x4.m from [630e704e06] to [284898f541].

180
181
182
183
184
185
186


























187
188
189
190
191
192
193
194
195
196
	    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];
}



























- (OFPoint3D)transformedPoint3D: (OFPoint3D)point
{
	return OFMakePoint3D(
	    _values[0] * point.x + _values[4] * point.y +
	    _values[8] * point.z + _values[12],
	    _values[1] * point.x + _values[5] * point.y +
	    _values[9] * point.z + _values[13],
	    _values[2] * point.x + _values[6] * point.y +
	    _values[10] * point.z + _values[14]);
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	    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];
}

- (void)translateWithVector3D: (OFVector3D)vector
{
	OFMatrix4x4 *translation = [[OFMatrix4x4 alloc] initWithValues:
	    (float [16]){
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		vector.x, vector.y, vector.z, 1
	    }];
	[self multiplyWithMatrix: translation];
	[translation release];
}

- (void)scaleWithVector3D: (OFVector3D)vector
{
	OFMatrix4x4 *scale = [[OFMatrix4x4 alloc] initWithValues:
	    (float [16]){
		vector.x, 0, 0, 0,
		0, vector.y, 0, 0,
		0, 0, vector.z, 0,
		0, 0, 0, 1
	    }];
	[self multiplyWithMatrix: scale];
	[scale release];
}

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

Modified src/OFObject.h from [01a23b3a57] to [40bec49225].

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
	if (!OFEqualSizes(rect1.size, rect2.size))
		return false;

	return true;
}

/**
 * @struct OFPoint3D OFObject.h ObjFW/OFObject.h
 *
 * @brief A point in 3D space.
 */
typedef struct OF_BOXABLE {
	/** The x coordinate of the point */
	float x;
	/** The y coordinate of the point */
	float y;
	/** The z coordinate of the point */
	float z;
} OFPoint3D;

/**
 * @brief Creates a new OFPoint3D.
 *
 * @param x The x coordinate of the point
 * @param y The x coordinate of the point
 * @param z The z coordinate of the point
 * @return An OFPoint3D with the specified coordinates
 */
static OF_INLINE OFPoint3D OF_CONST_FUNC
OFMakePoint3D(float x, float y, float z)
{
	OFPoint3D point = { x, y, z };

	return point;
}

/**
 * @brief Returns whether the two points are equal.
 *
 * @param point1 The first point for the comparison
 * @param point2 The second point for the comparison
 * @return Whether the two points are equal
 */
static OF_INLINE bool
OFEqualPoints3D(OFPoint3D point1, OFPoint3D point2)
{
	if (point1.x != point2.x)
		return false;

	if (point1.y != point2.y)
		return false;

	if (point1.z != point2.z)
		return false;

	return true;
}

/**
 * @brief Adds the specified byte to the hash.







|

|


|

|

|

|


|

|
|
|
|

|
|

|

|



|

|
|
|


|

|


|


|







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
	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 vectors 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.

Modified tests/OFMatrix4x4Tests.m from [53376eca89] to [2df72718ee].

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;
	OFPoint3D point;

	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 point;

	TEST(@"+[identityMatrix]",
	    memcmp([[OFMatrix4x4 identityMatrix] values], (float [16]){
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
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(@"-[transformedPoint3D:]",
	    R((point = [matrix transformedPoint3D: OFMakePoint3D(1, 2, 3)])) &&
	    point.x == 18 && point.y == 46 && point.z == 74)

	objc_autoreleasePoolPop(pool);
}
@end







>
>
>
>
>
>
>
>
>
>
>

|





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

	TEST(@"[-translateWithVector3D:]",
	    R(matrix2 = [OFMatrix4x4 identityMatrix]) &&
	    R([matrix2 translateWithVector3D: OFMakeVector3D(1, 2, 3)]) &&
	    R(point = [matrix2 transformedPoint3D: OFMakeVector3D(2, 3, 4)]) &&
	    point.x == 3 && point.y == 5 && point.z == 7)

	TEST(@"-[scaleWithVector3D:]",
	    R([matrix2 scaleWithVector3D: OFMakeVector3D(-1, 0.5, 2)]) &&
	    R(point = [matrix2 transformedPoint3D: OFMakeVector3D(2, 3, 4)]) &&
	    point.x == -3 && point.y == 2.5 && point.z == 14)

	TEST(@"-[transformedPoint3D:]",
	    R((point = [matrix transformedPoint3D: OFMakeVector3D(1, 2, 3)])) &&
	    point.x == 18 && point.y == 46 && point.z == 74)

	objc_autoreleasePoolPop(pool);
}
@end