Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sunray

Interpolation

This topic is 5510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I need some help with intepolation. I have a triangle, which lies inside a bounding box (Actually a lightmap). Each vertex has a value and I need to find the interpolated value for the all pixels of the image. Any help? [image]http://jmb.mine.nu/~sunray/face.png[/image] [edited by - Sunray on August 17, 2003 5:19:58 PM]

Share this post


Link to post
Share on other sites
Advertisement
some code I use. Maybe there is a simpler solution that I don''t know of, so keep posting



//-------------------------------------------------------------------------

// Compute the weighted averages.

// ------------------------------

//

// + A

// e/ \

// / \

// H + \

// / . \

// / + P \

// / . \

// / . \

// / f. \

// / .\

// +-------------------+

// B C

//

// triangle made of points A, B, C and normal D

//

//

// P is the point projected on the triangle plane (N, A).

//

// H is the intersection of ray (C, CP) with edge plane ((AB x N), A)

//

// then,

//

// AH = e.AB

// CP = f.CH

//

// => H = A + e.AB

// => P = C + f.CH

//

// => H = e.A + (1 - e).B

// => P = f.H + (1 - f).C

//

// => P = ef(A) + (f - ef)(B) + (1 - f)(C)

//

// and e = 1.0f - |AH| / |AB|

// and f = 1,0f - |CP| / |CH|

//

// => weight[0] = ef

// weight[1] = (f - ef)

// weight[2] = (1 - f)

//-------------------------------------------------------------------------

void ComputeWeights(const Vector& Point, const Vector& A, const Vector& B, const Vector& C, float* Weights)
{
//-------------------------------------------------------------------------

// compute Triangle Normal

//-------------------------------------------------------------------------

Vector E;
Vector F;
Vector N;
E = B; E -= A;
F = C; F -= B;
N.Cross(E, F);

//-------------------------------------------------------------------------

// project point on triangle plane

//-------------------------------------------------------------------------

Vector P;
P = Point;
float d = P * N - (A * N);
P.AddScaledVector(-d, N);

Vector AB;
Vector ABxN;
Vector CP;

//-------------------------------------------------------------------------

// compute edge plane (AB x N, A)

//-------------------------------------------------------------------------

AB = B; AB -= A;
ABxN.Cross(AB, N);

//-------------------------------------------------------------------------

// compute intersection distance

//-------------------------------------------------------------------------

CP = P; CP -= C;

float det = CP * ABxN;

if (FABS(det) < 0.000001f)
{
Weights[0] = 0.0f;
Weights[1] = 0.0f;
Weights[2] = 1.0f;
return;
}

float t = (ABxN * A - C * ABxN) / det;

//-------------------------------------------------------------------------

// compute intersection point

//-------------------------------------------------------------------------

//Vector H;

H = C;
H.AddScaledVector(t, CP);

// float e;

// float f;

//-------------------------------------------------------------------------

// compute first interpolation factor on edge AB

//-------------------------------------------------------------------------

Vector AH;
AH = H; AH -= A;

float ab = AB.GetLength();
float ah = AH.GetLength();

e = (ah / ab);


//-------------------------------------------------------------------------

// compute second interpolation factor on segment CH

//-------------------------------------------------------------------------

Vector CH;
CH = H; CH -= C;

float ch = CH.GetLength();
float cp = CP.GetLength();

if (ch < 0.00001f)
{
Weights[0] = 0.0f;
Weights[1] = 0.0f;
Weights[2] = 1.0f;
return;
}

f = (cp / ch);


//-------------------------------------------------------------------------

// compute the weights

//-------------------------------------------------------------------------

Weights[0] = f - e * f;
Weights[1] = e * f;
Weights[2] = 1.0f - f;
}

Share this post


Link to post
Share on other sites
I am doing something very similar. In my case, I have a 2d triangle and each corner, has a height, I try to find the height at some point inside of the triangle...

For your situation, do it this way:
1) find a line that foes through the point you are trying to find and 2 edges of the tri
2) find where this new line intersects the triangle
3) you will have 2 intersection points between this line and your triangle; find the value of these points.
4) find the value of the piont by interpolating between the two intersection points

Basically you have 3 linear interpolations. This algorithm can be greatly simplified in most cases. As in my case, where all triangles are right triangles, I use the following code:

void Terrain::GetHeightAtPoint(D3DXVECTOR2 &vec2, float &height)
{
float xd = 1;
float yd = 1;
float z0 = 1;
float z1 = .5;
float z2 = 0;
float w = (xd - ((xd - vec2.x) + (yd - vec2.y))) / xd;
float ah = z0 * (1 - w) + z2 * w;
float bh = z1 * (1 - w) + z2 * w;
height = (bh * (yd - vec2.y) + ah * (xd - vec2.x)) / ((yd - vec2.y) + (xd - vec2.x));
}


in this case, for simplicity, jz0, z1, and z2 are the values you wish to interpolate

This is a little different than u were asking for, but should be close enough to get you going in the right direction

Hope I could help

Dwiel

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!