Started by Feb 25 2011 12:02 PM

,
10 replies to this topic

Posted 25 February 2011 - 12:02 PM

Hey.

Given the coordinates a, b, c, d, and p, how would I find the normalized UV coordinates of p? (For example, to sample a texture at that point.)

a, b, c, d, and p are 2D (that is, only X,Y coordinates). p will always be inside abcd.

I have no idea where to start. Any ideas?

Given the coordinates a, b, c, d, and p, how would I find the normalized UV coordinates of p? (For example, to sample a texture at that point.)

a, b, c, d, and p are 2D (that is, only X,Y coordinates). p will always be inside abcd.

I have no idea where to start. Any ideas?

Posted 25 February 2011 - 12:15 PM

I don't know if this is the most efficient, but you can determine which of 2 triangles* p is located within. Then calculate the barycentric coordinates of p in that triangle and apply those coords to the texture coordinates of the vertices.

*E.g., adc and acb, etc.

*E.g., adc and acb, etc.

Please don't PM me with questions. Post them in the forums for *everyone's* benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Posted 25 February 2011 - 12:23 PM

Ok, cool, thanks for the information. I'll try it

If anyone has any other ideas, feel free to chip in.

If anyone has any other ideas, feel free to chip in.

Posted 25 February 2011 - 12:25 PM

the issue with buckeye's solution is the uv coordinates won't be continuous except for the special case of a paralellogram/rectangle/square

a continous uv mapping could consist of the solutions to:

p = a + ux + vy + uvz

where the four coordinates of your rectangle in order are a,b,c,d, with x = b-a, y = d-a, z = a+c-d-b

i don't however know of a solution to this, and wolframalpha doesn't give you anything pretty for it!

(I come up with that by linearly interpolating accross top and bottom lengths with 'u', then linearly interpolating between those two points with 'v' which gives a unique, continous, but very complex mapping of p to (u,v) coordinates.

a continous uv mapping could consist of the solutions to:

p = a + ux + vy + uvz

where the four coordinates of your rectangle in order are a,b,c,d, with x = b-a, y = d-a, z = a+c-d-b

i don't however know of a solution to this, and wolframalpha doesn't give you anything pretty for it!

(I come up with that by linearly interpolating accross top and bottom lengths with 'u', then linearly interpolating between those two points with 'v' which gives a unique, continous, but very complex mapping of p to (u,v) coordinates.

Posted 25 February 2011 - 12:30 PM

If you use two triangles to stretch a rectangle image like that, you might need to consider the problem described on the following page: http://home.xyzw.us/~cass/qcoord/

Posted 25 February 2011 - 12:53 PM

Hm, you're right. Any other ideas? I'm basically trying to map out a virtual 2D quadrilateral onto a physical, rectangular screen. (Basically, texture mapping it.)

Posted 25 February 2011 - 01:10 PM

Don't know of a closed solution, but of course is it possible to "bilateral binary search" over (u,v) and checking each step in which quadrant p lies. It would even be sufficient to iterate over u only, until the resulting line will be close enough to p, and then solving for v analytically.

Posted 25 February 2011 - 01:16 PM

there is a closed solution, but it's very long!

ultimately you can reduce the problem to:

which has a very complex closed solution of: http://www.wolframal...,+solve+for+u,v

of which only one of the two top solutions should be the correct one.

a,b,c,d,e being rather complex equatinos involving your coordinate p, and the coordinates of the 4 rectangle points starting with:

with rectangle being represented with top-left a, top-right b, bottom-right c, bottom-left d. u running left to right, v running top to bottom

ultimately you can reduce the problem to:

( u ) ( 1 a b c )( uv ) = ( 0 ) ( 0 1 d e )( v ) ( 0 ) ( 1 )

which has a very complex closed solution of: http://www.wolframal...,+solve+for+u,v

of which only one of the two top solutions should be the correct one.

a,b,c,d,e being rather complex equatinos involving your coordinate p, and the coordinates of the 4 rectangle points starting with:

( u ) ( bx-ax cx-bx dx-ax ax-px )( uv ) = ( 0 ) ( by-ay cy-by dy-ay ay-py )( v ) ( 0 ) ( 1 )

with rectangle being represented with top-left a, top-right b, bottom-right c, bottom-left d. u running left to right, v running top to bottom

Posted 25 February 2011 - 01:32 PM

There are lots of reasonable answers here.

We've had this discussion here in the past; if you search the old forums you can probably find those threads. The solutions always boiled down to one of,

1.) Split into triangles and do barycentric interpolation on each (buckeye's answer)

2.) Bilinear interpolation (luca-deltodesco's answer)

Details were given in those threads for both of these approaches.

Here's a third: [EDIT: Removed; was wrong.]

We've had this discussion here in the past; if you search the old forums you can probably find those threads. The solutions always boiled down to one of,

1.) Split into triangles and do barycentric interpolation on each (buckeye's answer)

2.) Bilinear interpolation (luca-deltodesco's answer)

Details were given in those threads for both of these approaches.

Here's a third: [EDIT: Removed; was wrong.]

Posted 25 February 2011 - 01:45 PM

Yeah, I though of looking at it the following way:

The approach of the bilinear interpolation

**p**_{1} := **a** + ( **b** - **a** ) * u

**p**_{2} := **d** + ( **c** - **d** ) * u

**p** := **p**_{1} + ( **p**_{2} - **p**_{1} ) * v

means that there is a line between**p**_{1} and **p**_{2} that passes through **p**. Expressing this line the other way, namely

**p** + **l** = **p**_{1}

**p** + k * **l** = **p**_{2}

so that**l** + k * **l** (where k is obviously need to be a negative number) is the said line. Considering that this is done in 2D, we have 4 equations with 4 unkowns (k, l_{x}, l_{y}, and u) where k * l_{x} and k * l_{y} are the problems. Fortunately, from the lower of the both equations, we can isolate

k * l_{x} = d_{x} - p_{x} + ( c_{x} - d_{x} ) * u

k * l_{y} = d_{y} - p_{y} + ( c_{y} - d_{y} ) * u

and divide both of these, so that

l_{x} / l_{y }= [ d_{x} - p_{x} + ( c_{x} - d_{x} ) * u ] / [ d_{y} - p_{y} + ( c_{y} - d_{y} ) * u ]

Now, using the upper of the equations to get

l_{x} = a_{x} - p_{x} + ( b_{x} - a_{x} ) * u

l_{y} = a_{y} - p_{y} + ( b_{y} - a_{y} ) * u

and setting these into the above l_{x} / l_{y}, we get a quadratic equation solely in u which can be solved using the famous p,q-formula.

This gives, as luca-deltodesco has mentioned, 0 or 1 real solutions in general. But due to the fact that**p** always lies inside the quad, I expect 1 real solution. With that, v can be determined easily.

The approach of the bilinear interpolation

means that there is a line between

so that

k * l

k * l

and divide both of these, so that

l

Now, using the upper of the equations to get

l

l

and setting these into the above l

This gives, as luca-deltodesco has mentioned, 0 or 1 real solutions in general. But due to the fact that

Posted 25 February 2011 - 08:31 PM

Yeah, I though of looking at it the following way:

The approach of the bilinear interpolationp_{1}:=a+ (b-a) * up_{2}:=d+ (c-d) * up:=p_{1}+ (p_{2}-p_{1}) * v

means that there is a line betweenp_{1}andp_{2}that passes throughp. Expressing this line the other way, namelyp+l=p_{1}p+ k *l=p_{2}

so thatl+ k *l(where k is obviously need to be a negative number) is the said line. Considering that this is done in 2D, we have 4 equations with 4 unkowns (k, l_{x}, l_{y}, and u) where k * l_{x}and k * l_{y}are the problems. Fortunately, from the lower of the both equations, we can isolate

k * l_{x}= d_{x}- p_{x}+ ( c_{x}- d_{x}) * u

k * l_{y}= d_{y}- p_{y}+ ( c_{y}- d_{y}) * u

and divide both of these, so that

l_{x}/ l_{y }= [ d_{x}- p_{x}+ ( c_{x}- d_{x}) * u ] / [ d_{y}- p_{y}+ ( c_{y}- d_{y}) * u ]

Now, using the upper of the equations to get

l_{x}= a_{x}- p_{x}+ ( b_{x}- a_{x}) * u

l_{y}= a_{y}- p_{y}+ ( b_{y}- a_{y}) * u

and setting these into the above l_{x}/ l_{y}, we get a quadratic equation solely in u which can be solved using the famous p,q-formula.

This gives, as luca-deltodesco has mentioned, 0 or 1 real solutions in general. But due to the fact thatpalways lies inside the quad, I expect 1 real solution. With that, v can be determined easily.

Thank you very, very much! This came out to be the easiest solution to implement and it works great!

In case anyone is interested in the same problem, here's some code:

double C = (double)(a.Y - p.Y) * (d.X - p.X) - (double)(a.X - p.X) * (d.Y - p.Y); double B = (double)(a.Y - p.Y) * (c.X - d.X) + (double)(b.Y - a.Y) * (d.X - p.X) - (double)(a.X - p.X) * (c.Y - d.Y) - (double)(b.X - a.X) * (d.Y - p.Y); double A = (double)(b.Y - a.Y) * (c.X - d.X) - (double)(b.X - a.X) * (c.Y - d.Y); double D = B * B - 4 * A * C; double u = (-B - Math.Sqrt(D)) / (2 * A); double p1x = a.X + (b.X - a.X) * u; double p2x = d.X + (c.X - d.X) * u; double px = p.X; double v = (px - p1x) / (p2x - p1x);u and v are then [0,1] coordinates you can sample a texture from, etc.

Thanks again!