Jump to content



mapping coordinates with barycentric coordinates

  • You cannot reply to this topic
2 replies to this topic

#1 Alessandro   Members   -  Reputation: 164

Like
0Likes
Like

Posted 17 February 2012 - 12:15 PM

Following to this thread (http://www.gamedev.n...uv-coordinates/),

given a triangle ABC and a point P inside it, I'd like to find the coordinates of a point Q inside a DEF triangle, so that ABC are mapped to DEF (see the image).

Posted Image

I'm trying to do this using barycentric coordinates. I was quite sure that my calculations were correct, but the result don't match what expected.
The resulting Q point is not were it should be.

Here is the code I'm using to calculate barycentric coordinates:


// compute the area of a triangle using Heron's formula
float triarea(float a, float b, float c)
{
	float s = (a + b + c)/2.0;
	return sqrt(s*(s-a)*(s-b)*(s-c));		  
}

// compute the distance between two 2d points
float dist(float x0, float y0, float z0, float x1, float y1, float z1)
{
	float a = x1 - x0;	  
	float b = y1 - y0;
	float c = z1 - z0;
	return sqrt(a*a + b*b + c*c);
}

// compute the barycentric coordinates of a 3d point inside a triangle
// (x0, y0, z0) (x1, y1, z1) (x2, y2, z2) are the vertices of a triangle
// (vx, vy, vz) is a point inside the triangle
// u, v, w are the barycentric coordinates of (vx, vy, vz) in the triangle
void barycent(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2,
			  float vx, float vy, float vz,
			  float *u, float *v, float *w)
{
	// compute the area of the big triangle
	float a = dist(x0, y0, z0, x1, y1, z1);
	float b = dist(x1, y1, z1, x2, y2, z2);
	float c = dist(x2, y2, z2, x0, y0, z0);
	float totalarea = triarea(a, b, c);
	
	// compute the distances from the outer vertices to the inner vertex
	float length0 = dist(x0, y0, z0, vx, vy, vz);	  
	float length1 = dist(x1, y1, z2, vx, vy, vz);	  
	float length2 = dist(x2, y2, z2, vx, vy, vz);	  
	
	// divide the area of each small triangle by the area of the big triangle
	*u = triarea(b, length1, length2)/totalarea;
	*v = triarea(c, length0, length2)/totalarea;
	*w = triarea(a, length0, length1)/totalarea;	  
}

And here the main() where I run a test
	float r1,r2,r3;	// barycentric coordinates
	Point3D A(0,0,0);
	Point3D B(2,4,0);
	Point3D C(4,2,0);
	Point3D P(2,3,0);
	
	barycent(A.x,A.y,A.z, B.x,B.y,B.z, C.x,C.y,C.z, P.x,P.y,P.z, &r1, &r2, &r3);
	
	Point3D D(0,0,0);
	Point3D E(3,2,0);
	Point3D F(6,0,0);	
  
	Point3D Q(D.x*r1 + E.x*r1 + F.x*r1, D.y*r2 + E.y*r2 + F.y*r2, D.z*r3 + E.z*r3 + F.z*r3);

	std::cout << "barycentric coordinates: " << r1 << "," << r2 << "," << r3 << std::endl;
	std::cout << "Q: " << Q.x << "," << Q.y << "," << Q.z << std::endl;


After I calculate barycentric coordinates (r1,r2,r3), in order to calculate the Q point I multiply DEF coordinates by that barycentric coordinates.
I know it's an hassle but if you could help me out that would be wonderful. I really suck at math.

Ad:

#2 alvaro   Members   -  Reputation: 1695

Like
1Likes
Like

Posted 17 February 2012 - 12:37 PM

  Point3D Q(D.x*r1 + E.x*r2 + F.x*r3, D.y*r1 + E.y*r2 + F.y*r3, D.z*r1 + E.z*r2 + F.z*r3);


#3 Alessandro   Members   -  Reputation: 164

Like
0Likes
Like

Posted 17 February 2012 - 03:51 PM

Thanks Alvaro, that worked!






We are working on generating results for this topic
PARTNERS