ObjFW  Check-in [aadec90b2f]

Overview
Comment:Add -[crossProductWithVector:] to OFFloatVector.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: aadec90b2f4110ecbdbe8fadd2503f7819b05784bf5d63be826ddf247d525aa2
User & Date: js on 2011-06-14 19:34:49
Other Links: manifest | tags
Context
2011-06-15
21:38
We need OFObject.h in macros.h now for of_{point,dimension,rectangle}_t. check-in: 674acc5082 user: js tags: trunk
2011-06-14
19:34
Add -[crossProductWithVector:] to OFFloatVector. check-in: aadec90b2f user: js tags: trunk
2011-06-13
23:53
Add of_range(). check-in: a9c38eb137 user: js tags: trunk
Changes

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

96
97
98
99
100
101
102
103
104
105
106












107
108
109
110
111
112
113
- (size_t)dimension;

/**
 * \brief Returns an array of floats with the contents of the vector.
 *
 * Modifying the returned array directly is allowed and will change the vector.
 *
 * \brief An array of floats with the contents of the vector
 */
- (float*)cArray;













/**
 * \brief Adds the specified vector to the receiver.
 *
 * \param vector The vector to add
 */
- (void)addVector: (OFFloatVector*)vector;








|



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







96
97
98
99
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
- (size_t)dimension;

/**
 * \brief Returns an array of floats with the contents of the vector.
 *
 * Modifying the returned array directly is allowed and will change the vector.
 *
 * \return An array of floats with the contents of the vector
 */
- (float*)cArray;

/**
 * \brief Returns the magnitude or length of the vector.
 *
 * \return The magnitude or length of the vector
 */
- (float)magnitude;

/**
 * \brief Normalizes the vector.
 */
- (void)normalize;

/**
 * \brief Adds the specified vector to the receiver.
 *
 * \param vector The vector to add
 */
- (void)addVector: (OFFloatVector*)vector;

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
 * \brief Returns the dot product of the receiver and the specified vector.
 *
 * \return The dot product of the receiver and the specified vector
 */
- (float)dotProductWithVector: (OFFloatVector*)vector;

/**
 * \brief Returns the magnitude or length of the vector.
 *
 * \return The magnitude or length of the vector
 */
- (float)magnitude;

/**
 * \brief Normalizes the vector.

 */
- (void)normalize;


/**
 * \brief Multiplies 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: (OFFloatMatrix*)matrix;
@end







|

|
<
<
|
<
<
>

<
>









164
165
166
167
168
169
170
171
172
173


174


175
176

177
178
179
180
181
182
183
184
185
186
 * \brief Returns the dot product of the receiver and the specified vector.
 *
 * \return The dot product of the receiver and the specified vector
 */
- (float)dotProductWithVector: (OFFloatVector*)vector;

/**
 * \brief Returns the cross product of the receiver and the specified vector.
 *
 * This currently only works for 3D vectors.


 *


 * \return The cross product of the receiver and the specified vector
 */

- (OFFloatVector*)crossProductWithVector: (OFFloatVector*)vector;

/**
 * \brief Multiplies 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: (OFFloatMatrix*)matrix;
@end

Modified src/OFFloatVector.m from [b49ac3419e] to [a58504ec6e].

234
235
236
237
238
239
240































241
242
243
244
245
246
247
	return description;
}

- (float*)cArray
{
	return data;
}
































- (void)addVector: (OFFloatVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa







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







234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	return description;
}

- (float*)cArray
{
	return data;
}

- (float)magnitude
{
	float magnitude;
	size_t i;

	magnitude = 0.0f;

	for (i = 0; i < dimension; i++)
		magnitude += data[i] * data[i];

	magnitude = sqrtf(magnitude);

	return magnitude;
}

- (void)normalize
{
	float magnitude;
	size_t i;

	magnitude = 0.0f;

	for (i = 0; i < dimension; i++)
		magnitude += data[i] * data[i];

	magnitude = sqrtf(magnitude);

	for (i = 0; i < dimension; i++)
		data[i] /= magnitude;
}

- (void)addVector: (OFFloatVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
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
352
353

	for (i = 0; i < dimension; i++)
		dotProduct += data[i] * vector->data[i];

	return dotProduct;
}

- (float)magnitude
{
	float magnitude;
	size_t i;

	magnitude = 0.0f;



	for (i = 0; i < dimension; i++)
		magnitude += data[i] * data[i];

	magnitude = sqrtf(magnitude);

	return magnitude;
}

- (void)normalize
{
	float magnitude;



	size_t i;

	magnitude = [self magnitude];

	for (i = 0; i < dimension; i++)
		data[i] /= magnitude;
}

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	float *newData;
	size_t i, j, k;








|

|
<

|
>
>

|
<
|
<
|
<
|
|
<
|
|
>
>
>
|
|
<

<
|







347
348
349
350
351
352
353
354
355
356

357
358
359
360
361
362

363

364

365
366

367
368
369
370
371
372
373

374

375
376
377
378
379
380
381
382

	for (i = 0; i < dimension; i++)
		dotProduct += data[i] * vector->data[i];

	return dotProduct;
}

- (OFFloatVector*)crossProductWithVector: (OFFloatVector*)vector
{
	OFFloatVector *crossProduct;


	if (dimension != 3)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	if (vector->dimension != dimension)

		@throw [OFInvalidArgumentException newWithClass: isa

						       selector: _cmd];


	crossProduct = [OFFloatVector vectorWithDimension: 3];


	crossProduct->data[0] =
	    data[1] * vector->data[2] - data[2] * vector->data[1];
	crossProduct->data[1] =
	    data[2] * vector->data[0] - data[0] * vector->data[2];
	crossProduct->data[2] =
	    data[0] * vector->data[1] - data[1] * vector->data[0];



	return crossProduct;
}

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	float *newData;
	size_t i, j, k;