Make vector_product() aliasing-safe
This commit is contained in:
parent
7587b8058a
commit
1df3195f37
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "err.h"
|
#include "err.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include "vec_math.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -25,7 +26,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the vector product of a and b
|
* Calculate the vector product of a and b
|
||||||
* and store it in c.
|
* and store it in c. This function is aliasing safe.
|
||||||
*
|
*
|
||||||
* @param a vector
|
* @param a vector
|
||||||
* @param b vector
|
* @param b vector
|
||||||
@ -34,12 +35,37 @@
|
|||||||
*/
|
*/
|
||||||
bool vector_product(vector *a, vector *b, vector *c)
|
bool vector_product(vector *a, vector *b, vector *c)
|
||||||
{
|
{
|
||||||
|
vector a_tmp,
|
||||||
|
b_tmp;
|
||||||
|
|
||||||
if (!a || !b)
|
if (!a || !b)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
c->x = a->y * b->z - a->z * b->y;
|
copy_vector(a, &a_tmp);
|
||||||
c->y = a->z * b->x - a->x * b->z;
|
copy_vector(b, &b_tmp);
|
||||||
c->z = a->x * b->y - a->y * b->x;
|
|
||||||
|
c->x = a_tmp.y * b_tmp.z - a_tmp.z * b_tmp.y;
|
||||||
|
c->y = a_tmp.z * b_tmp.x - a_tmp.x * b_tmp.z;
|
||||||
|
c->z = a_tmp.x * b_tmp.y - a_tmp.y * b_tmp.x;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy coordinates from vector a to vector b.
|
||||||
|
*
|
||||||
|
* @param a vector
|
||||||
|
* @param b vector [out]
|
||||||
|
* @return true/false for success/failure
|
||||||
|
*/
|
||||||
|
bool copy_vector(vector *a, vector *b)
|
||||||
|
{
|
||||||
|
if (!a || !b)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
b->x = a->x;
|
||||||
|
b->y = a->y;
|
||||||
|
b->z = a->z;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
|
|
||||||
bool vector_product(vector *a, vector *b, vector *c);
|
bool vector_product(vector *a, vector *b, vector *c);
|
||||||
|
bool copy_vector(vector *a, vector *b);
|
||||||
|
|
||||||
|
|
||||||
#endif /* _DROW_ENGINE_VEC_MATH_H */
|
#endif /* _DROW_ENGINE_VEC_MATH_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user