ObjFW  Check-in [91f446cfd8]

Overview
Comment:Add -[rotateWithVector:angle:] to OFFloatMatrix.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 91f446cfd88e0d1988d0c19be7f88280b9d27da141464cbaa77b3567f1596f04
User & Date: js on 2011-06-16 02:14:53
Other Links: manifest | tags
Context
2011-06-16
02:25
Add -[setDimension:] to OFFloatVector. check-in: ef368d182c user: js tags: trunk
02:14
Add -[rotateWithVector:angle:] to OFFloatMatrix. check-in: 91f446cfd8 user: js tags: trunk
01:05
of_asprintf: Work around Win32 breaking C99 standard length modifiers. check-in: 6c20d8bff4 user: js tags: trunk
Changes

Modified src/OFFloatMatrix.h from [8345b9957a] to [ff5f178d3a].

177
178
179
180
181
182
183









184
185
186
187
188
189
190
/**
 * \brief Translates the nxn matrix of the receiver with an n-1 vector.
 *
 * \param vector The vector to translate with
 */
- (void)translateWithVector: (OFFloatVector*)vector;










/**
 * \brief Scales the nxn matrix of the receiver with an n-1 vector.
 *
 * \param scale The vector to scale with
 */
- (void)scaleWithVector: (OFFloatVector*)vector;
@end







>
>
>
>
>
>
>
>
>







177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
 * \brief Translates the nxn matrix of the receiver with an n-1 vector.
 *
 * \param vector The vector to translate with
 */
- (void)translateWithVector: (OFFloatVector*)vector;

/**
 * \brief Rotates the 4x4 matrix of the receiver with a 3D vector and an angle.
 *
 * \param vector The vector to rotate with
 * \param angle The angle to rotate with
 */
- (void)rotateWithVector: (OFFloatVector*)vector
		   angle: (float)angle;

/**
 * \brief Scales the nxn matrix of the receiver with an n-1 vector.
 *
 * \param scale The vector to scale with
 */
- (void)scaleWithVector: (OFFloatVector*)vector;
@end

Modified src/OFFloatMatrix.m from [d37cee00b5] to [2c065c1145].

27
28
29
30
31
32
33


34






35
36
37
38
39
40
41
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"



@implementation OFFloatMatrix






+ matrixWithRows: (size_t)rows
	 columns: (size_t)columns
{
	return [[[self alloc] initWithRows: rows
				   columns: columns] autorelease];
}








>
>

>
>
>
>
>
>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

static Class floatVector = Nil;

@implementation OFFloatMatrix
+ (void)initialize
{
	if (self == [OFFloatMatrix class])
		floatVector = [OFFloatVector class];
}

+ matrixWithRows: (size_t)rows
	 columns: (size_t)columns
{
	return [[[self alloc] initWithRows: rows
				   columns: columns] autorelease];
}

392
393
394
395
396
397
398
399

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

















































416
417
418
419
420
421
422
423

424
425
426
427
428
429
430
}

- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;

	if (rows != columns || [vector dimension] != rows - 1)

		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	translation = [[OFFloatMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(float));

	@try {
		[self multiplyWithMatrix: translation];
	} @finally {
		[translation release];
	}
}


















































- (void)scaleWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *scale;
	float *cArray;
	size_t i, j;

	if (rows != columns || [vector dimension] != rows - 1)

		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	scale = [[OFFloatMatrix alloc] initWithRows: rows
					    columns: columns];








|
>
















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







|
>







400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
}

- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;

	if (rows != columns || vector->isa != floatVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	translation = [[OFFloatMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(float));

	@try {
		[self multiplyWithMatrix: translation];
	} @finally {
		[translation release];
	}
}

- (void)rotateWithVector: (OFFloatVector*)vector
		   angle: (float)angle
{
	OFFloatMatrix *rotation;
	float n[3], m, angleCos, angleSin;

	if (rows != 4 || columns != 4 || vector->isa != floatVector ||
	    vector->dimension != 3)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	n[0] = vector->data[0];
	n[1] = vector->data[1];
	n[2] = vector->data[2];

	m = sqrtf(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);

	if (m != 1.0) {
		n[0] /= m;
		n[1] /= m;
		n[2] /= m;
	}

	angle = (float)(angle * M_PI / 180.0f);
	angleCos = cosf(angle);
	angleSin = sinf(angle);

	rotation = [[OFFloatMatrix alloc] initWithRows: rows
					       columns: columns];

	rotation->data[0] = angleCos + n[0] * n[0] * (1 - angleCos);
	rotation->data[1] = n[1] * n[0] * (1 - angleCos) + n[2] * angleSin;
	rotation->data[2] = n[2] * n[0] * (1 - angleCos) - n[1] * angleSin;

	rotation->data[4] = n[0] * n[1] * (1 - angleCos) - n[2] * angleSin;
	rotation->data[5] = angleCos + n[1] * n[1] * (1 - angleCos);
	rotation->data[6] = n[2] * n[1] * (1 - angleCos) + n[0] * angleSin;

	rotation->data[8] = n[0] * n[2] * (1 - angleCos) + n[1] * angleSin;
	rotation->data[9] = n[1] * n[2] * (1 - angleCos) - n[0] * angleSin;
	rotation->data[10] = angleCos + n[2] * n[2] * (1 - angleCos);

	@try {
		[self multiplyWithMatrix: rotation];
	} @finally {
		[rotation release];
	}
}

- (void)scaleWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *scale;
	float *cArray;
	size_t i, j;

	if (rows != columns || vector->isa != floatVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	scale = [[OFFloatMatrix alloc] initWithRows: rows
					    columns: columns];

Modified src/OFFloatVector.h from [0701029750] to [dc5b2c9055].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
@class OFFloatMatrix;

/**
 * \brief A class for storing and manipulating vectors of floats.
 */
@interface OFFloatVector: OFObject <OFCopying>
{

	size_t dimension;
	float *data;
}

/**
 * \brief Creates a new vector with the specified dimension.
 *







>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@class OFFloatMatrix;

/**
 * \brief A class for storing and manipulating vectors of floats.
 */
@interface OFFloatVector: OFObject <OFCopying>
{
@public
	size_t dimension;
	float *data;
}

/**
 * \brief Creates a new vector with the specified dimension.
 *