How do I compute the control points given a curve in the form of power form? Say I have p(t)=(x(t),y(t)) and 4 control points.
x(t) = 2t
y(t) = (t^3)+3(t^2)
You can always convert from power basis to Bernstein basis. This is always doable and will give you the precise result. Refer to section 3.3 of this link (http://cagd.cs.byu.edu/~557/text/ch3.pdf) for details.
EDIT:
Since the above link is no longer available, I am listing the formula below:
where M is the degree of the Berstein basis, 0 <= k <= M and b_i,k=0 if i < k.
Using the common cubic Berstein basis (where M=3) as an example, we will have
this is pure math question (unless you go for the #3)... I am deducing you need 4 control points for single cubic Bezier curve in 2D.
algebraic approach
try to match your x(t),y(t) polynomials to Bezier polynomials form and extract the coefficients/control points. This is not always doable but most precise... see the link in #2 at the end I do this for my interpolation polynomial to match the Bezier so I get the conversion formula between control points.
interpolation
find extreme points on your curve (to preserve precision as much as possible) if none or not enough extremes found use equally dispersed points along curve for the rest. You need 4 control points on the curve. Now just convert these 4 points to curve for example by this: how to convert interpolation cubic polynmial to cubic Bezier
can use curve fitting
either use approximation search or any other minimization of curves distance ... by fitting Bezier control points but that is 8 parameters to search for which is slow and non precise without additional constrains ..
I am sure there are a lot more (possibly hybrid) methods out there for this problem.
Related
I have points list and control points list to draw a Bezier curve.
Please let me know how to calculate the boundary rectangle of the Bezier curve.
var pointsList = [CGPoint(34, 23), ... , CGPoint(23, 85)]
var controlPoints = [CGPoint(45, 34), ..., CGPoint(55, 99)]
Normally not really a hard problem, covered over on http://pomax.github.io/bezierinfo/#boundingbox:
Compute the x and y derivatives, which is super easy to do,
Find all roots (derivative=0) for both derivatives, let's call those the sets r{x} and r{y}, then
Compute the corresponding value sets bezier{x} and bezier{y} for those roots. Then,
Your bounding box has corners defined by the lowest and highest values min/max values in those sets.
In this, only step 2 might be a bit tricky if you're using high order bezier curves. Once your curve consists of more than four points, you can't use symbolic maths to find the roots and it's far easier to just run through the derivative curve and see where the resulting coordinates have a value close enough to zero to treat them as approximate root.
Your graphic looks like it's simply a series of connected cubic Bezier curves, in which case the root finding is easy (the derivatives will be quadratic curves, you learn how to find the roots for those in high school using the quadratic equation), and the box procedure is simply "compute the bounding box for each cubic curve section, and when you're done, the full bounding box simply uses the min/max values across all individual boxes".
I have a list of (x, y) points. I know how to make a list of Bézier curves which pass through all of those points and have a continuous first (and second, though less important) derivative. However, the list that I end up with is far too long. I would prefer to approximate the points I have if it lets me cut down on the number of curves I have. I would like to be able to pass a parameter of either how close an approximation I get or a maximum number of curves, preferably the former.
The reason I want this is that the end result will have a graphical UI where users can edit the Bézier curves, and it isn't critical that the curves pass through each point exactly, as long as they are close. More curves makes it harder to edit.
EDIT:
Some more information about the purpose of this. I'm trying to make image editing software. When someone loads a bitmap, I want to be able to trace a center line. Potrace is what I would use to trace the outline of a shape, but it won't work for tracing strokes. I've been able to identify lots of points along the center line, and I want to turn this data into a list of connected Bézier curves. The reason I don't want to make a Bézier spline is that there will be too many control points for this to be easy to edit. "Too many" is not an easy-to-define term, but I would like to be able to pass a parameter to limit the number of curves. Either a function that minimizes how far the curves are from the points based on a maximum number of curves or a function that minimizes the number of curves based on a maximum deviation from the points.
Several approaches exist for achieving what you want to do:
1) Use RDP algorithm to reduce the number of points, then create a list of Bezier curves passing thru the remaining points.
2) Use curve fitting algorithms (for example, Schneider algorithm) to produce multiple Bezier curves that are connected with G1 (tangent) continuity. Check out Schneider algorithm implementation in this link.
3) Use least square fitting with B-spline to produce a single B-spline curve.
From implementation point of view, approach 1 is probably the easiest one for you as you already know how to create Bezier curves interpolating a list of points. Approach 3 will be much more difficult to implement and you probably will have to convert the B-spline curve into Bezier curves so as to use them at the UI level. Please refer to this SO article for detailed discussion.
I am developing a 3D graphic application in which the user can draw curves.
I record the curve that is drawn by the user and i would like to create a smooth nurb from the recorded set of points.
I tried using the openNurbs library but i could not find a way to do the fitting using the library.
How can i fit a set of points to a nurb?
First of all, I don't think you need nurbs. Fitting a B-spline curve to your data points should be good enough.
If you only have a few dozen points, then it is likely you would like the B-spline curve to exactly pass thru these data points. In this case, you are looking for spline interpolation algorithms. If this is the case, you can use Catmull Rom spline or Overhauser spline to interpolate your data points. Both will create C1 cubic splines and both are easy to implement without the need to solve a linear equation set.
If you have several hundreds of points, then it is likely that you only want the B-spline curve to lie close to the data points. Then, the algorithm you are looking for is least square fitting. You can find plenty of articles (e.g.: link1 ) in this area online. A typical algorithm for least square fitting with B-spline curve will involve these steps:
1) Choose a parametrization for your data points. Chord length parametrization is typically a good choice for least square fitting.
2) Choose the degree for the B-spline. Typically, we use degree 3, i.e., cubic B-spline.
3) Decide number of control points for your B-spline.
4) Decide the knot vector based on the information in the first 3 steps.
5) Solve a linear equation set to find the control points of the B-spline.
I am wondering if the number of control points will always be 2 more than the number of fit points.
Specially for cubic spline.
Based on your question, I would guess that you're either asking about natural cubic spline curves, or Bezier spline made from cubic Bezier curves. In the case of natural cubic spline curves, the number of control points is exactly the same as the number of fit points.
However, I'm pretty sure you're actually talking about Bezier splines. It's a little trickier here. If you just want a single cubic Bezier curve, then you are correct that 2 fit points (the end points) are needed, as well as two more control points. But if we add a second Bezier curve to form a Bezier spline, we need one more fit point plus another two control points. That's a total of 7 control points, 3 of which are fit points. Adding a third curve gives 10 control points, of which 4 are fit points. If we continue adding curves like this, we can see that the number of control points is actually 3*n-2, where n is the number of fit points.
Given an arbitrary sequence of points in space, how would you produce a smooth continuous interpolation between them?
2D and 3D solutions are welcome. Solutions that produce a list of points at arbitrary granularity and solutions that produce control points for bezier curves are also appreciated.
Also, it would be cool to see an iterative solution that could approximate early sections of the curve as it received the points, so you could draw with it.
The Catmull-Rom spline is guaranteed to pass through all the control points. I find this to be handier than trying to adjust intermediate control points for other types of splines.
This PDF by Christopher Twigg has a nice brief introduction to the mathematics of the spline. The best summary sentence is:
Catmull-Rom splines have C1
continuity, local control, and
interpolation, but do not lie within
the convex hull of their control
points.
Said another way, if the points indicate a sharp bend to the right, the spline will bank left before turning to the right (there's an example picture in that document). The tightness of those turns in controllable, in this case using his tau parameter in the example matrix.
Here is another example with some downloadable DirectX code.
One way is Lagrange polynominal, which is a method for producing a polynominal which will go through all given data points.
During my first year at university, I wrote a little tool to do this in 2D, and you can find it on this page, it is called Lagrange solver. Wikipedia's page also has a sample implementation.
How it works is thus: you have a n-order polynominal, p(x), where n is the number of points you have. It has the form a_n x^n + a_(n-1) x^(n-1) + ...+ a_0, where _ is subscript, ^ is power. You then turn this into a set of simultaneous equations:
p(x_1) = y_1
p(x_2) = y_2
...
p(x_n) = y_n
You convert the above into a augmented matrix, and solve for the coefficients a_0 ... a_n. Then you have a polynomial which goes through all the points, and you can now interpolate between the points.
Note however, this may not suit your purpose as it offers no way to adjust the curvature etc - you are stuck with a single solution that can not be changed.
You should take a look at B-splines. Their advantage over Bezier curves is that each part is only dependent on local points. So moving a point has no effect on parts of the curve that are far away, where "far away" is determined by a parameter of the spline.
The problem with the Langrange polynomial is that adding a point can have extreme effects on seemingly arbitrary parts of the curve; there's no "localness" like described above.
Have you looked at the Unix spline command? Can that be coerced into doing what you want?
There are several algorithms for interpolating (and exrapolating) between an aribtrary (but final) set of points. You should check out numerical recipes, they also include C++ implementations of those algorithms.
Unfortunately the Lagrange or other forms of polynomial interpolation will not work on an arbitrary set of points. They only work on a set where in one dimension e.g. x
xi < xi+1
For an arbitary set of points, e.g. an aeroplane flight path, where each point is a (longitude, latitude) pair, you will be better off simply modelling the aeroplane's journey with current longitude & latitude and velocity. By adjusting the rate at which the aeroplane can turn (its angular velocity) depending on how close it is to the next waypoint, you can achieve a smooth curve.
The resulting curve would not be mathematically significant nor give you bezier control points. However the algorithm would be computationally simple regardless of the number of waypoints and could produce an interpolated list of points at arbitrary granularity. It would also not require you provide the complete set of points up front, you could simply add waypoints to the end of the set as required.
I came up with the same problem and implemented it with some friends the other day. I like to share the example project on github.
https://github.com/johnjohndoe/PathInterpolation
Feel free to fork it.
Google "orthogonal regression".
Whereas least-squares techniques try to minimize vertical distance between the fit line and each f(x), orthogonal regression minimizes the perpendicular distances.
Addendum
In the presence of noisy data, the venerable RANSAC algorithm is worth checking out too.
In the 3D graphics world, NURBS are popular. Further info is easily googled.