8

Given a triangle and a point inside the triangle in Cartesian coordinate system, I compute the Barycentric coordinate of the point like this:

double v2_1[3], v2_3[3], v2_t[3];

double bary[3];
vtkMath::Subtract(p1, p2, v2_1);
vtkMath::Subtract(p3, p2, v2_3);
vtkMath::Subtract(t, p2, v2_t);

float d00 = vtkMath::Dot(v2_1, v2_1);
float d01 = vtkMath::Dot(v2_1, v2_3);
float d11 = vtkMath::Dot(v2_3, v2_3);
float denom = d00 * d11 - d01 * d01;

float d20 = vtkMath::Dot(v2_t, v2_1);
float d21 = vtkMath::Dot(v2_t, v2_3);
bary[0] = (d11 * d20 - d01 * d21) / denom;
bary[1] = (d00 * d21 - d01 * d20) / denom;
bary[2] = 1.0f - bary[0] - bary[1];

return bary;

Now, how can I use the values in bary to interpolate the vertex normal at p1, p2 and p3? Specifically, I'm confused which coefficient belongs to which vertex.

Bla...
  • 481
  • 4
  • 12
  • 1
    try some values, the coefficient that is near 1 when the point inside is close to a vertex belongs to that vertex. – ratchet freak Apr 18 '17 at 08:04
  • 1
    @ratchetfreak I did that, but I'm also curious how to mathematically find the respective coefficient. – Bla... Apr 18 '17 at 09:01

2 Answers2

7

Thats the way your defining the vectors. See your defining vectors $V_1$ and $V_2$ as pointing outwards from the point $P_2$.

enter image description here

Now then $denom$ is the square of the area of the parallelogram of formed by completing the triangle with a duplicate of itself. Another way to look at this is that its the 2 tines the square of the are of the triangle.

enter image description here

Now then the calculation:

(d11 * d20 - d01 * d21)

Is the square area of the parallelogram of $P_2$-$T$-$P_3$

enter image description here

Now if you divide this with denom, then you have calculated the total area relative area of triangle $P_2$-$T$-$P_3$. So the formula (d11 * d20 - d01 * d21) / denom is the triangle size in relation to whole triangle size of:

enter image description here

This is the weight that the opposing corner exerts on the point. which in other words it is the barycenter weight related to point $P_1$.

The next line is simply the relative area of triangle $P_2$-$T$-$P_1$ or:

enter image description here

Which is opposite to $P_3$ being the barycentric coordinate of $P_3$.

Lastly you calculate the rest which is the remaining triangle which is opposite of $P_2$ using 1 - bary[0] - bary[1].

Note: You could have chosen the first vectors differently for a different order.

Bla...
  • 481
  • 4
  • 12
joojaa
  • 8,437
  • 1
  • 24
  • 47
  • Awesome, I'm looking for this kind of explanation. BTW, there is small mistake, the $V_1$ should be $V_0$, $V_2$ should be $V_1$ and $V_3$ should the vector pointing to $T$. Hopefully you have the time to fix that error. Thanks once again, hopefully many people benefit from this. – Bla... Apr 20 '17 at 05:04
  • 1
    Argh, @Bla... yes i will redraw the pictures. Anyway i am feeling that the variables in the code could have more understandable names. $V_0$ - $V_2$ offcourse being reasonable names but would be much better to name them with same nomenclature as your points. – joojaa Apr 20 '17 at 05:34
  • I just changed the variable names, hopefully now it becomes clearer. – Bla... Apr 20 '17 at 06:32
2

You can find which coefficient belongs to which vertex empirically by changing t to be one of the points on the triangle (p1, p2, p3). Then one of the coefficients will be 1 and others are 0. Based on that you know which coefficient belongs to which vertex. Then, interpolating the normal is trivial:

interpolatedNormal = bary[0] * p1_normal + bary[2] * p2_normal + bary[1] * p3_normal;

But I'm still curious the mathematical explanation behind this.

Bla...
  • 481
  • 4
  • 12