'''
-1: colliniar
1: intersection
'''
short IsectLLPt2Df(float x0,float y0,float x1,float y1,
float x2,float y2,float x3,float y3, float *xi,float *yi)
{
# this function computes the intersection of the sent lines
# and returns the intersection point, note that the function assumes
# the lines intersect. the function can handle vertical as well
# as horizontal lines. note the function isn't very clever, it simply
# applies the math, but we don't need speed since this is a
# pre-processing step
float a1,b1,c1, # constants of linear equations
a2,b2,c2,
det_inv, # the inverse of the determinant of the coefficient
m1,m2; # the slopes of each line
# compute slopes, note the cludge for infinity, however, this will
# be close enough
if ( ( x1-x0 ) !=0 )
m1 = ( y1-y0 ) / ( x1-x0 );
else
return -1; '''m1 = ( float ) 1e+10;''' # close enough to infinity
if ( ( x3-x2 ) !=0 )
m2 = ( y3-y2 ) / ( x3-x2 );
else
return -1; '''m2 = ( float ) 1e+10;''' # close enough to infinity
# compute constants
a1 = m1;
a2 = m2;
b1 = -1;
b2 = -1;
c1 = ( y0-m1*x0 );
c2 = ( y2-m2*x2 );
# compute the inverse of the determinate
det_inv = 1/ ( a1*b2 - a2*b1 );
# use Kramers rule to compute xi and yi
*xi= ( ( b1*c2 - b2*c1 ) *det_inv );
*yi= ( ( a2*c1 - a1*c2 ) *det_inv );
return 1;
} # end Intersect_Lines
''' Similar to LineIntersectsTriangleUV, expect it operates on a quad and in 2d, assumes tri is in quad '''
void PointInQuadUV(float v0[2], float v1[2], float v2[2], float v3[2], float pt[2], float *uv)
{
float x0,y0, x1,y1, x2,y2, wtot, v2d[2], w1, w2;
''' used for paralelle lines '''
float pt3d[3], l1[3], l2[3], pt_on_line[3];
''' compute 2 edges of the quad intersection point '''
if (IsectLLPt2Df(v0[0],v0[1],v1[0],v1[1], v2[0],v2[1],v3[0],v3[1], &x0,&y0) == 1) {
printf("\tparalelle1\n");
''' the intersection point between the quad-edge intersection and the point in the quad we want the uv's for '''
''' should never be paralle !! '''
IsectLLPt2Df(pt[0],pt[1],x0,y0, v0[0],v0[1],v3[0],v3[1], &x1,&y1);
''' Get the weights from the new intersection point, to each edge '''
v2d[0] = x1-v0[0];
v2d[1] = y1-v0[1];
w1 = Vec2Length(v2d);
v2d[0] = x1-v3[0]; # some but for the other vert
v2d[1] = y1-v3[1];
w2 = Vec2Length(v2d);
wtot = w1+w2;
#w1 = w1/wtot;
#w2 = w2/wtot;
uv[0] = w1/wtot;
} else {
# lines are paralelle, lambda_cp_line_ex is 3d grrr
printf("\t NOT paralelle1\n");
pt3d[0] = pt[0];
pt3d[1] = pt[1];
pt3d[2] = l1[2] = l2[2] = 0.0f;
l1[0] = v0[0]; l1[1] = v0[1];
l2[0] = v1[0]; l2[1] = v1[1];
lambda_cp_line_ex(pt3d, l1, l2, pt_on_line);
v2d[0] = pt[0]-pt_on_line[0]; # some but for the other vert
v2d[1] = pt[1]-pt_on_line[1];
w1 = Vec2Length(v2d);
l1[0] = v2[0]; l1[1] = v2[1];
l2[0] = v3[0]; l2[1] = v3[1];
lambda_cp_line_ex(pt3d, l1, l2, pt_on_line);
v2d[0] = pt[0]-pt_on_line[0]; # some but for the other vert
v2d[1] = pt[1]-pt_on_line[1];
w2 = Vec2Length(v2d);
wtot = w1+w2;
uv[0] = w1/wtot;
}
# Same as above to calc the uv[1] value, alternate same calculation
if (IsectLLPt2Df(v0[0],v0[1],v3[0],v3[1], v1[0],v1[1],v2[0],v2[1], &x0,&y0) == 1) { # was v0,v1 v2,v3 now v0,v3 v1,v2
printf("\tparalelle2\n");
# never paralle if above was not
IsectLLPt2Df(pt[0],pt[1],x0,y0, v0[0],v0[1],v1[0],v1[1], &x1,&y1);# was v0,v3 now v0,v1
v2d[0] = x1-v0[0];
v2d[1] = y1-v0[1];
w1 = Vec2Length(v2d);
v2d[0] = x1-v1[0];
v2d[1] = y1-v1[1];
w2 = Vec2Length(v2d);
wtot = w1+w2;
uv[1] = w1/wtot;
} else {
# lines are paralelle, lambda_cp_line_ex is 3d grrr
printf("\t NOT paralelle2\n");
pt3d[0] = pt[0];
pt3d[1] = pt[1];
pt3d[2] = l1[2] = l2[2] = 0.0f;
l1[0] = v0[0]; l1[1] = v0[1];
l2[0] = v1[0]; l2[1] = v1[1];
lambda_cp_line_ex(pt3d, l1, l2, pt_on_line);
v2d[0] = pt[0]-pt_on_line[0]; # some but for the other vert
v2d[1] = pt[1]-pt_on_line[1];
w1 = Vec2Length(v2d);
l1[0] = v2[0]; l1[1] = v2[1];
l2[0] = v3[0]; l2[1] = v3[1];
lambda_cp_line_ex(pt3d, l1, l2, pt_on_line);
v2d[0] = pt[0]-pt_on_line[0]; # some but for the other vert
v2d[1] = pt[1]-pt_on_line[1];
w2 = Vec2Length(v2d);
wtot = w1+w2;
uv[1] = w1/wtot;
}
printf("%f %f\n", uv[0], uv[1]);
}