Skip to main content
GameDev.net gamedev.net
🔒 Locked

point inside triangle function - in C

Started by voidless Dec 29, 2008 at 5:47 PM 4 replies 3k views
Original Post
voidless
voidless
hello everyone. this is my first post on the gamedev forums. ive recently started to code a smaller 3d application, and looked around the net after collision detection mechanisms. i have found this, and tried to implement the code in C (as the application is written in C. now my problem comes from the checkPointInTriangle functions return part (also here, a bit modified but still C plus plus). ive made a C code but im not sure if it does the same:

#include <stdio.h>
typedef float VECTOR[3];

#define VECTOR_DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])

#define VECTOR_SUB(vector,v1,v2) vector[0]= v1[0]-v2[0]; vector[1]= v1[1]-v2[1]; vector[2]= v1[2]-v2[2];

typedef unsigned int uint32;

#define in(a) (*((uint32*)&a))

int check_point_in_triangle(VECTOR point, VECTOR v0, VECTOR v1, VECTOR v2){
 VECTOR e1;
 VECTOR e2;

 VECTOR_SUB(e1,v1,v0);
 VECTOR_SUB(e2,v2,v0);

 float a= VECTOR_DOT(e1,e1);
 float b= VECTOR_DOT(e1,e2);
 float c= VECTOR_DOT(e2,e2);

 float ac_bb= (a*c)-(b*b);

 VECTOR vp;

 VECTOR_SUB(vp,point,v0);

 float d= VECTOR_DOT(vp,e1);
 float e= VECTOR_DOT(vp,e2);
 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);
}

it sometimes even gives wrong results, for example for the triangle: (1,0,0) (0,1,1) (0,0,1) and point (1,1,1) it does not return 0. thank you
apatriarca
apatriarca
I always use a function like that only after a point in the plane test. I think the point you have tested isn't in the plane defined by the triangle.

[Edited by - apatriarca on December 30, 2008 5:44:48 AM]
voidless
voidless
thanks i was thinking about something like that too, but my main concern is the RETURN line.
if is that equivalent of the C plus plus functions return line (as there is no reference type in C)
samster581
samster581
I would check to see if this is a Point in 3D triangle test, or a Point In 2D triangle. If it's 2D only, then it probably won't work for 3D points.
Halifax2
Halifax2
Quote:
Original post by voidless
thanks i was thinking about something like that too, but my main concern is the RETURN line.
if is that equivalent of the C plus plus functions return line (as there is no reference type in C)


That isn't the reference operator, but rather the bitwise AND operator. It performs as such:
11010010 &01101011----------01000010

It's great for masking out data.
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume
voidless
voidless
Quote:
Original post by Halifax2
Quote:
Original post by voidless
thanks i was thinking about something like that too, but my main concern is the RETURN line.
if is that equivalent of the C plus plus functions return line (as there is no reference type in C)


That isn't the reference operator, but rather the bitwise AND operator. It performs as such:
11010010 &01101011----------01000010

It's great for masking out data.


i know that mainly its the bitwise AND, but in the original C plusplus contect, that line looked like:

typedef unsigned int unit32

#define in(a) ((uint32&)a)

and i rewrote that the way i posted it above in C:

typedef unsigned int unit32

#define in(a) (*((uint32*)&a))

and the return line is common between the C and Cplusplus implementation:

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


my question wast about the and operator, but rather about the park where in that define it casts a to an unsigned int reference type, and if is my code equivalent to that

oh and by the way in a previous function, it checks if the point is in the triangle plane before calling that function

thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.