Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicating Blender bezier curves in a C++ program

I'm trying to export (3D) bezier curves from Blender to my C++ program. I asked a related question a while back, where I was successfully directed to use De Casteljau's Algorithm to evaluate points (and tangents to these points) along a bezier curve. This works well. In fact, perfectly. I can export the curves and evaluate points along the curve, as well as the tangent to these points, all within my program using De Casteljau's Algorithm.

However, in 3D space a point along a bezier curve and the tangent to this point is not enough to define a "frame" that a camera can lock into, if that makes sense. To put it another way, there is no "up vector" which is required for a camera's orientation to be properly specified at any point along the curve. Mathematically speaking, there are an infinite amount of normal vectors at any point along a 3D bezier curve.

I've noticed when constructing curves in Blender that they aren't merely infinitely thin lines, they actually appear to have a proper 3D orientation defined at any point along them (as shown by the offshooting "arrow lines" in the screenshot below). I'd like to replicate what blender does here as closely as possible in my program. That is, I'd like to be able to form a matrix that represents an orientation at any point along a 3D bezier curve (almost exactly as it would in Blender itself).

enter image description here

Can anyone lend further guidance here, perhaps someone with an intimate knowledge of Blender's source code? (But any advice is welcome, Blender background or not.) I know it's open source, but I'm having a lot of troubles isolating the code responsible for these curve calculations due to the vastness of the program.

like image 241
Alex Z Avatar asked Apr 01 '12 10:04

Alex Z


2 Answers

If I understand you question correcly, what you want is to get 3 orientation vectors (left, front, up) for any point of the curve.

Here is a simple method ( there is a limitation, (*) see below ) :

1) Front vector :

Calculate a 3d point on bezier curve for a given position (t). This is the point for which we will calculate front, left, up vectors. We will call it current_point.

Calculate another 3d point on the curve, next to first one (t + 0.01), let's call it next_point.

Note : i don't write formula here, because i believe you already how to do that.

Then, to calculate front vector, just substract the two points calculated previously :

vector front = next_point - current_point

Don't forget to normalize the result.

2) Left vector

Define a temporary "up" vector

vector up = vector(0.0f, 1.0f, 0.0f);  

Now you can calculate left easily, using front and up :

vector left = CrossProduct(front, up);

3) Up vector

vector up = CrossProduct(left, front);

Using this method you can always calculate a front, left, up for any point along the curve.


(*) NOTE : this wont work in all cases. Imagine you have a loop in you curve, just like a rollercoaster loop. On the top of the loop your calculated up vector will be (0, 1, 0), while you maybe want it to be (0, -1, 0). Only way to solve that is to have two curves : one for points and one for up vectors (from which left and front can be calculated easily).

like image 177
tigrou Avatar answered Nov 16 '22 17:11

tigrou


Primarily, we know, that the normal vector you're searching for lies on the plane "locally perpendicular" to the curve on the specific point. So the real problem is to choose a single vector on this plane.

I've made an empty object to track the curve and noticed, that it behave similarly to the cart of a rollercoaster: its "up" vector was correlated to the centrifugal force while it was moving along the curve. This one can be uniquely evaluated from the local shape of the curve.

I'm not very good at physics, but I would try to estimate that vector by evaluating two planes: the first is previously mentioned perpendicular plane and the second is a plane made of three neighboring points of a curve segment (if the curve is not straight, these will form a triangle, which describes exactly one plane). Intersection of these two planes will give you an axis and you'll only have to choose a direction of such calculated normal vector.

like image 42
Spook Avatar answered Nov 16 '22 16:11

Spook