• Create Account

# Bezier Triangles

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1cephalo  Members   -  Reputation: 739

Like
0Likes
Like

Posted 01 February 2013 - 02:35 PM

Original post-------------------------------

I was reading up about Bezier triangles in the hopes of running some experiments, but my understanding of math terminology is not keeping up. According to this article:

http://www.gamasutra.com/view/feature/131389/b%C3%A9zier_triangles_and_npatches.php?print=1

You can interpolate positions on a triangular surface with some number of control points with this formula

You can also determine the tangent frame at that point with the two partial derivatives:

The problem I am having is that I just can't figure out how to write this out in long form. The sigma operator always defeats any chance I have of math comprehension. I have seen 3rd order triangles written out in the case of PN-triangles etc. but I can't make out the pattern of iteration enough to expand it to higher orders. I don't even understand how and why the big 'B' is different than the bold 'p'. My experiment is going to be using a 6th order Bezier triangle (7 control points on each side) So if I have all the 28 control points set and I want to know the position at the barycentric coords p(u,v) how to I calculate that with code? I need the same help with the two partial derivatives as well.

-------------------------------------------------

Ok, here are the results that I calculated. Hopefully there aren't too many mistakes. First I will calculate w so that the patterns can be more easily seen for calculating the position at (u,v):

w = 1 - u - v;

p(u,v) =
p600 * u^6 +
p060 * v^6 +
p006 * w^6 +
p501 * 6 * u^5 * w +
p510 * 6 * u^5 * v +
p150 * 6 * u * v^5 +
p051 * 6 * v^5 * w +
p105 * 6 * u * w^5 +
p015 * 6 * v * w^5 +
p402 * 15 * u^4 * w^2 +
p411 * 30 * u^4 * v * w +
p420 * 15 * u^4 * w^2 +
p240 * 15 * u^2 * v^4 +
p141 * 30 * u * v^4 * w +
p042 * 15 * v^4 * w^2 +
p024 * 15 * v^2 * w^4 +
p114 * 30 * u * v * w^4 +
p204 * 15 * u^2 * w^4 +
p303 * 20 * u^3 * w^3 +
p312 * 60 * u^3 * v * w^2 +
p321 * 60 * u^3 * v^2 * w +
p330 * 20 * u^3 * v^3 +
p231 * 60 * u^2 * v^3 * w +
p132 * 60 * u * v^3 * w^2 +
p033 * 20 * v^3 * w^3 +
p213 * 60 * u^2 * v * w^3 +
p123 * 60 * u * v^2 * w^3 +
p222 * 90 * u^2 * v^2 * w^2;



Now for the partial derivatives. This is a little more complicated to understand, because you calculate these with a subset of control points that is basically a bezier triangle at an order that is one smaller, such that you are leaving off the opposite edge of the original triangle. Mapping this smaller triangle onto the original control points looks a bit strange, hopefully I did it correctly:

partial derivative for u
dp(u,v)/du =
p600 * u^5 +
p501 * 5 * u^4 * w +
p510 * 5 * u^4 * v +
p402 * 10 * u^3 * w^2 +
p411 * 20 * u^3 * v * w +
p420 * 10 * u^3 * v^2 +
p303 * 10 * u^2 * w^3 +
p312 * 30 * u^2 * v * w^2 +
p321 * 30 * u^2 * v^2 * w +
p330 * 10 * u^2 * v^3 +
p204 * 5 * u * w^4 +
p213 * 20 * u * v * w^3 +
p222 * 30 * u * v^2 * w^2 +
p231 * 20 * u * v^3 * w +
p240 * 5 * u * v^4 +
p105 * w^5 +
p114 * 5 * v * w^4 +
p123 * 10 * v^2 * w^3 +
p132 * 10 * v^3 * w^2 +
p141 * 5 * v^4 * w +
p150 * v^5;

partial derivative for v
dp(u,v)/dv =
p060 * v^5 +
p150 * 5 * u * v^4 +
p051 * 5 * v^4 * w +
p240 * 10 * u^2 * v^3 +
p141 * 20 * u * v^3 * w +
p042 * 10 * v^3 * w^2 +
p330 * 10 * u^3 * v^2 +
p231 * 30 * u^2 * v^2 * w +
p132 * 30 * u * v^2 * w^2 +
p033 * 10 * v^2 * w^3 +
p420 * 5 * u^4 * v +
p321 * 20 * u^3 * v * w +
p222 * 30 * u^2 * v * w^2 +
p123 * 20 * u * v * w^3 +
p024 * 5 * v * w^4 +
p510 * u^5 +
p411 * 5 * u^4 * w +
p312 * 10 * u^3 * w^2 +
p213 * 10 * u^2 * w^3 +
p114 * 5 * u * w^4 +
p015 * w^5; 

I would guess if you were going to use these tangents for a tangent frame in a shader, you might have to fiddle with them a bit since they are calculated from barycentric coordinates and are probably not perpendicular on the plane. Perhaps you could calculate the normal, then create a new bitangent from the normal and tangent, and then rotate it to your texture edge somehow.

### #2greggles  Members   -  Reputation: 347

Like
1Likes
Like

Posted 02 February 2013 - 12:04 PM

First of all, the bold vs. not bold: Anything bold is a point, anything not bold is a number. So when you see p(u, v), it means the Point at the barycentric coordinates u, v.

Secondly, the sigma notation used in the article means "sum together all of the values where this equation is true." So, for example, the equation i+j+k=n could be true when 1+0+6=7 or when 0+3+4=7, and so on.

Let's look at the first equation you have posted:

When determining how to create the loops, I found the picture of the triangle very helpful. Lets assume that each digit in the point corresponds to the variables i, j, and k in order. Thus, the lower left point of the triangle p003 means i=0, j=0, and k=3.

The first thing that I notice is that i increases from 0 to 3 from bottom to top of the image. This is a basic for loop that we can easily handle.

Next, I notice that for any i, the j value starts at 0 on the left and increases until i+j=n on the right. This is another loop we can handle.

Finally, the original equation gives us k via algebra: i+j+k=n can be rearranged to k = n - (i+j). That is the final value we need to know.

This gives us our final form:

Point p;
for (int i = 0; i <= n; ++i) {
for (int j = 0; i+j <= n; ++j) {
int k = n - (i + j);
p += B(i, j, k, u, v, n) * p(i, j, k); //Probably not quite what you want here, but this is an example of what this might look like.
}
}


Now, the B() function refers to the function on the Gamasutra page and the p function here means "give me the point with i, j, k index." For example, p(0, 6, 1) means p061.

Does this help? The partial derivatives would then be created in a very similar way.

### #3cephalo  Members   -  Reputation: 739

Like
0Likes
Like

Posted 02 February 2013 - 04:39 PM

Thanks for the help! That gives me a bit more confidence in figuring it out. I need to better understand the big 'B' I think. The examples I've seen with the 3 order PN-Triangles seem a lot simpler than that crazy 'B' formula. There must be a way to simplify it. For example, check this out.

I guess that B number also has a i,j,k index, I guess that is what is creating the decreasing exponent on these terms... somehow. How do you know which exponent goes with which point?

Edited by cephalo, 02 February 2013 - 04:41 PM.

### #4greggles  Members   -  Reputation: 347

Like
1Likes
Like

Posted 02 February 2013 - 09:32 PM

Thanks for the help! That gives me a bit more confidence in figuring it out. I need to better understand the big 'B' I think. The examples I've seen with the 3 order PN-Triangles seem a lot simpler than that crazy 'B' formula. There must be a way to simplify it. For example, check this out.

I guess that B number also has a i,j,k index, I guess that is what is creating the decreasing exponent on these terms... somehow. How do you know which exponent goes with which point?

The first thing to understand is that B (a bernstein polynomial) determines how much each point contributes to the final p(u, v) point. The value it returns is between 0 and 1, with 0 meaning that the point won't affect the final output and 1 meaning that it completely determines the output.

The "simplified" equation you give is just that... a simplified version of B when n = 3. You can do the math yourself if you want to take the time. Just take the equation for B, make n always 3, and compute the result for every valid i, j, k combination. Then with a bit of algebraic manipulation you could get the same equation. But the way this equation is written is only true for n=3. Breaking down the case of seven control points into a single equation is going to be too unwieldy. Instead, I would suggest trying to understand how to use:

which works for any n, before trying to simplify it.

Let's look at how this will be used:

Let's say that we have a triangle patch with n = 5 and are trying to determine how much point p301 will contribute to the output point at (u,v). We would use the "big B" to determine this. B of 3,1,1 at u,v. The equation would be:

( 5! / (3! * 1! * 1!) )   * (ui) * (vj) * (1 - u - v)k =

(5*4 *3*2*1) / (3 * 2 * 1 * 1 * 1)  * u3 * v * (1 - u -v) =

20*(u3) * v * (1 - u - v)

This means that when u = 0.1 and v = 0.5), the point p301 would contribute to the point:

20*(0.001) *0.1 * 1 * (1 - 0.1 - 0.5) =

20*(0.0001)*(0.4) =

0.0008

Then back in the for-loop I had talked about before, p(3, 0, 1) would be multiplied by 0.0008 and added to the output point.

Note that B can be created as a function in code somewhat easily once you have a factorial function (something that can also be created easily) and a power function (should be available in your favorite programming language's standard library). All that it needs to do is take in multiple parameters (n, u, v, i, j, and k) and output a double value.

...well, that was a long explanation. I probably got something in there wrong

Let me know if something needs to be clarified.

### #5cephalo  Members   -  Reputation: 739

Like
0Likes
Like

Posted 02 February 2013 - 09:40 PM

Wow, thanks. I completely understand now. Once you know your n and that its not going to change, all that stuff can be pre-calculated. As soon as I have time I'm going to post my results here for future reference.

### #6cephalo  Members   -  Reputation: 739

Like
0Likes
Like

Posted 06 February 2013 - 04:37 PM

Ok, I posted my results to the OP. Hopefully everything is correct.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS