Unfamiliar syntax
Jun 12, 2014 at 12:04pm UTC
I downloaded some code for triangle-box intersection, and it contains a way of defining functions I haven't really seen used before. Here's an example:
1 2 3 4 5 6 7 8
#define CROSS(dest, v1, v2) \
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
When I try to compile this I get several error messages like:
error: C++ requires a type specifier for all declarations
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
and
error: use of undeclared identifier 'v1'
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
Is this a maybe a C-thing? Does anyone know how I can get past this without rewriting the entire file?
Thanks,
Fafner
Jun 12, 2014 at 12:19pm UTC
remove the empty lines.
> Is this a maybe a C-thing?
that's called a `macro', it would simply do text substitution (use at your own risk)
In C++ you may use templates
Jun 12, 2014 at 12:23pm UTC
Thanks, removing the lines solved it :) I know about macros, I've just never used them and so had no idea about the empty line thing.
Jun 12, 2014 at 7:39pm UTC
Off the top of my head:
1 2 3 4 5 6 7 8
template <typename T>
inline
void CROSS( T& dest[ 3 ], T& v1[ 3 ], T& v2[ 3 ] )
{
dest[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
dest[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
dest[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
}
That'll properly compute the cross product of any two 3D vectors with a (presumably) floating point element type.
Remember that order matters. Mix the two input vectors and your normal (dest) vector will point in the opposite direction.
Hope this helps.
Jun 14, 2014 at 4:21am UTC
vector will point in the opposite
Yep cw vs ccw
Topic archived. No new replies allowed.