Sign in to follow this  
phb5000

C++ Expression

Recommended Posts

How can this be translated into C#? Its supposed to return a bool, and I know its some kind of bitwise operation, but what exactly does "in" mean? return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);

Share this post


Link to post
Share on other sites
Quote:
Original post by phb5000
but what exactly does "in" mean?


It could mean a number of things, it definitely isn't a keyword. It could be a call to a macro, call to a function, call to a user-defined type with the function-call operator overloaded, a call to a constructor of user-defined type called in, etc, etc.

Where does it come from and can't you need to find the definition (or at the very least declaration) of in.

Share this post


Link to post
Share on other sites
I still dont understand how it works. How does it become a bool? :S
How can I convert the return expression to C#?


typedef unsigned int uint32;
#define in(a) ((uint32&) a)

bool checkPointInTriangle(const VECTOR& point, const VECTOR& pa,const VECTOR& pb, const VECTOR& pc)
{
VECTOR e10=pb-pa;
VECTOR e20=pc-pa;

float a = e10.dot(e10);
float b = e10.dot(e20);
float c = e20.dot(e20);
float ac_bb=(a*c)-(b*b);

VECTOR vp(point.x-pa.x, point.y-pa.y, point.z-pa.z);
float d = vp.dot(e10);
float e = vp.dot(e20);
float x = (d*c)-(e*b);
float y = (e*a)-(d*b);
float z = x+y-ac_bb;

return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);
}

Share this post


Link to post
Share on other sites
Hey,

If you have the floats x,y and z you should use ((System.UInt32)x) to make the floats into a unsigned int. Thats what de in(a) does in your C code.

Next your function returns an unsigned int, which is either 0 (false in C) or not 0 (true in C).

To make it a bool you could simply add != 0 to the expression.

Your return statement should look something like:

//for readability:
System.UInt32 ix = (System.UInt32) x;
System.UInt32 iy = (System.UInt32) y;
System.Uint32 iz = (System.UInt32) z;

return ((iz & ~(ix | iy)) & 0x80000000) != 0; //which is type 'bool'

Share this post


Link to post
Share on other sites
Quote:
Original post by phb5000
I still dont understand how it works. How does it become a bool? :S
How can I convert the return expression to C#?

*** Source Snippet Removed ***


OK, 'in' here is a macro. It means that every time preprocessor encounters 'in(something)', it replaces it by ((uint32&) something).

So the return statement becomes:
 return (( ((uint32&) z)& ~(((uint32&) x)|((uint32&) y)) ) & 0x80000000); 


How does it translate into a bool? Well, it's an integer. In C++, when an integer is converted to a bool, it takes the value "false" if the integer value is zero, and "true" in all other cases.

One word about the construct ((uint32&) z). Here, the programmer is telling the compiler to use a reference of an object which must be considered as a "uint32" but which is a float variable called 'z'. This way, it converts the float variable to an integer one which has the same internal representation. It means that the mantissa, exponent and sign bit of the float are considered as normal bits of an integer. That's a dirty trick done purposely for optimization. I don't know C# so I don't know if this can be converted easily.

Share this post


Link to post
Share on other sites
This dirty trick relies on floats being 4 bytes in length and the MSB of a float being its sign (+/-).

It simplifies to


[pseudocode]
if z is negative and x is positive and y is positive
return true
else
return false
[/pseudocode]


also this trick is flawed in C++ as 0x80000000 is truncated to false as bool is usually 8 bits in length. Adding !! before the first bracket works.

Share this post


Link to post
Share on other sites
The trick is not flawed in C++ (although it is still evil), since the value will undergo the well-defined boolean conversion:
Quote:
C++ Final Draft Standard, Section 4.12, Paragraph 1
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this