deCasteljau algorithm on bezier surfaces - algorithm

Hi people i have 2 question related to Decasteljau algorithm,they are more of a general questions,but if im right it could help solving many problems.Here it is:
We have some sufrace: Ʃ(i=0,n) Ʃ(j=o,m) Bi,n(U),Bi,m(v) Pi,j analysis that i have found says that first we take some value for one parameter u=uo,then we itterate other parametar v -> 1 get a set of points,then increment u by one etc....for loop inside for loop in code language.My question is can we fix one parameter U=Uo for what ever value,and then just compute points on for parameter v?Because all points that are on one curve are also on the surface,and if distance between curves approaches to zero (which itteration really is) we can apply DeCasteljau algorithm only to one set of curves itterating only one parameter.Or i got something wrong?:)
Second question is i havent really figured out what do we really need DeCasteljau algorithm for,unless we are drawing curves by hand?If we know order of the curve we can easily form Bernstain polynoms for that curve order and compute point for given value of parametar.Because when you unwrap Decasteljau what you get is Bernstain polynom?
So like i said,please help have i got i wrong?

Yes you can fix one parameter (say U) and change the other (V) to generate an iso-U curve.
You can see the things as if you had an NxM array of control points. If you perform a first interpolation on U (actually M interpolations involving N control points), you get M new control points that define a Bezier curve. and by varying U, the curve moves in space.
The De Casteljau's algorithm is used for convenience: it computes the interpolant by using a cascade of linear interpolations between the control points. Direct evaluation of the Bernstein polynomials would require the precomputation of the coefficients, and would not be faster, even when implemented by Horner's scheme, and can be numerically less stable.
The De Casteljau's algorithm is also appreciated for its geometrical interpretation, and for its connection with the subdivision process: if you want to build the control points for just a part of a Bezier curve, De Calsteljau's provides them.

Related

How to find the intersection point of a 3D curve and a 3D surface?

I am trying to find the intersection point of a curve and a 3D surface with no luck. The surface is in the shape of a cone, and the curve is hyperbolic, as are shown in the figure.
CONE AND THE CURVE
This simulates a ray hits a certain surface. I tried to use bisection method, but it doesn't seem to work. then I tried newton's algorithm, but the results are still not good.
Is there any other good algorithms out there which are suitable for solving this kind of problem?
With the curve given in parametric form
x = fx(t)
y = fy(t)
z = fz(t)
and the surface by one equation of the form
g(x,y,z) = 0
just plug in the curve functions and bisection should work:
g(fx(t), fy(t), fz(t)) = 0
The only problem is to find suitable starting points t1 and t2 where g has opposite sign.
Problem
You are searching for a curve-surface intersection algorithm. Note that both curves and surfaces can be represented in either implicit form or in parametric form. Surface in implicit form is defined by equation F(x, y, z) = 0, which is a quadratic polynomial of x, y, z in case of conic surface. Surface in parametric form is defined by point-valued function S(u, v) of its parameters (e.g. you can use distance along cone axis and polar angle as parameters of conical surface). Curve is usually described only in parametric form, as a function C(t) with parameter t, which could be quadratic for a hyperbolic curve.
Implicit surface
The simplest cases of all is to treat your problem as an intersection of parametric curve against implicit surface. In this case you can write down a single equation q(t) = F(C(t)) = 0 with single variable t. Of course, Newton's iteration is not guaranteed to find all solutions in general case, bisection can only surely find one solution if you find two points with different sign of q(t).
In your case q(t) is a quartic polynomial (after putting quadratic curve parametrization into quadratic surface equation). It can be theoretically solved with Ferrari's analytic formula, but I strongly advise against it, because it is quite unstable numerically. You can apply any popular polynomial solver here, like Jenkins-Traub algorithm or eigenvalues algorithm for companion matrix (also see this question). You can also use methods of interval mathematics: for example, you can recursively subdivide the domain interval of parameter t into smaller pieces, while pruning all the pieces that surely do not contain zeros (interval arithmetic would help you to detect such pieces).
Parametric surface
Now we can move on to the case when both the curve and the surface are represented parametrically. I do not know any solutions that could benefit from the fact the your surface is conical and your curve is hyperbolic, so you have to apply the general curve-surface intersection algorithm. Alternatively, you can fit an implicitly-defined cone into your parametric surface, then use the solution above for quartic polynomial roots.
A lot of reliable general intersection algorithms are based on the subdivision method (which is actually interval mathematics again). The general idea is to continiously divide the curve and the surface into smaller and smaller pieces. The pairs of pieces which surely do not intersect are dropped as soon as possible. At the end you'll have a set of small piece pairs, tightly bounding your intersection points. Yoy might want to run Newton's iteration from them in order to make intersection points precise.
Here is the outline of a sample algorithm:
Start with a single curve piece (the whole input curve) and a single surface
piece (whole surface), and one potentially intersection pair (PIP) of these pieces.
Subdivide each curve piece into two halves (by parameter), subdivide each surface piece into four quadrants (by both parameters).
For each old PIP check all 8 pairs of curve half vs surface quadrant. If they surely do not intersect, forget them. If they can intersect, save them as a new PIP.
Unless all pieces are small enough, repeat from step 2 with new pieces and PIP-s.
For each pair of curve piece and surface piece, you have to check whether they can potentially intersect, which can be easily done by checking their axis-aligned bounding boxes. Also, you can represent your curves and surfaces as NURBS, in which case you can use convex hulls as tighter bounding volumes.
Generally, there are tons of variations and improvements of this algorithms. I advise the following literature for deeper knowledge:
Shape interrogation for computer-aided design and manufacturing.
chapter 4: for root solvers
section 5.7: for curve-surface intersection
PhD of Michael Hohmeyer.
section 4.5: for curve-surface intersection
sections 4.1 and 4.2: for convex hulls intersection (if you are brave enough).
Bottom line
If you are seeking for a simple and working solution, and you are sure that hyperbolas and cones are the only things you have to worry about, then you'd better use implicit definition of cone and solve quartic equation with some standard numerical algorithm from a good library available to you.

Algorithm for making straight lines out of bumpy pixels

The Problem:
I have an image that I downloaded from google's static map api. I use this image to basically create a "magic wand" type feature where a user clicks. For those interested I am using the graph cut algorithm to find the shape that the user clicked. I then find all the points that represent the border of this shape (borderPoints) using contour tracing.
My Goal:
Straighten out the lines (if possible) and minimize the amount of borderPoints (as much as possible). My current use case are the roofs of houses so in the majority of cases I would hope that I could find the corners and just use those as the borderPoints instead of all the varying points in between. But I am having trouble figuring out how to find those corners because of the bumpy pixel lines.
My Attempts at a Solution:
One simple technique is to loop over the points checking the point before, the current point, and the point after. If the point before and the point after have the same x or the same y then the current point can be removed. This trims the number of points down a little but not as much as I would like.
I also tried looking at the before and after point to see if the current point could be removed if it wasn't within a certain slope range but had little success because occasionally a key corner point was removed because the image was kind of fuzzy and the corner had slightly rounded points.
My Question:
Are there any algorithms for doing this type of thing? If so, what is it (they) called? If not, any thoughts on how to progamatically approach this problem?
This sounds similar to the Ramer–Douglas–Peucker algorithm. You may be able to do better by exploiting the fact that all your points lie on a grid.
Seems to me like you are looking for a polynomial approximation of degree 1.
For a quick answer to your question, you may want to read this: http://en.wikipedia.org/wiki/Simple_regression. The Numerical example section shows you concretely how the equation for your line can be computed.
Polynomial approximations allow you to approach a function, curve, group of point, however you want to call it with a polynomial function of the form an.x^n + ... + a1.x^1 + a0
In your case, you want a line, so you want a function a1.x + a0 where a1 and a0 will be calculated to minimize the error with the set of points you have.
There are various ways of computing your error (called a norm) and minimizing it. You may be interested for example in finding the line that minimizes the distance to any of the points you have (minimizing the max), or in minimizing the distance to the set of points as a whole (minimizing the sum of absolute differences, or the sum of squares of differences, etc.)
In terms of algorithms, you may want to look at Chebyshev approximations and Remez algorithms specifically. All of these solve the approximation of a function with a polynomial of any degree but in your case you will only care about degree 1.

Linear Least Squares Fit of Sphere to Points

I'm looking for an algorithm to find the best fit between a cloud of points and a sphere.
That is, I want to minimise
where C is the centre of the sphere, r its radius, and each P a point in my set of n points. The variables are obviously Cx, Cy, Cz, and r. In my case, I can obtain a known r beforehand, leaving only the components of C as variables.
I really don't want to have to use any kind of iterative minimisation (e.g. Newton's method, Levenberg-Marquardt, etc) - I'd prefer a set of linear equations or a solution explicitly using SVD.
There are no matrix equations forthcoming. Your choice of E is badly behaved; its partial derivatives are not even continuous, let alone linear. Even with a different objective, this optimization problem seems fundamentally non-convex; with one point P and a nonzero radius r, the set of optimal solutions is the sphere about P.
You should probably reask on an exchange with more optimization knowledge.
You might find the following reference interesting but I would warn you
that you will need to have some familiarity with geometric algebra -
particularly conformal geometric algebra to understand the
mathematics. However, the algorithm is straight forward to implement with
standard linear algebra techniques and is not iterative.
One caveat, the algorithm, at least as presented fits both center and
radius, you may be able to work out a way to constrain the fit so the radius is constrained.
Total Least Squares Fitting of k-Spheres in n-D Euclidean Space Using
an (n+ 2)-D Isometric Representation. L Dorst, Journal of Mathematical Imaging and Vision, 2014 p1-21
Your can pull in a copy from
Leo Dorst's researchgate page
One last thing, I have no connection to the author.
Short description of making matrix equation could be found here.
I've seen that WildMagic Library uses iterative method (at least in version 4)
You may be interested by the best fit d-dimensional sphere, i.e. minimizing the variance of the population of the squared distances to the center; it has a simple analytical solution (matrix calculus): see the appendix of the open access paper of Cerisier et al. in J. Comput. Biol. 24(11), 1134-1137 (2017), https://doi.org/10.1089/cmb.2017.0061
It works when the data points are weighted (it works even for continuous distributions; as a by-product, when d=1, a well-known inequality is retrieved: the kurtosis is always greater than the squared skewness plus 1).
Difficult to do this without iteration.
I would proceed as follows:
find the overall midpoint, by averaging (X,Y,Z) coords for all points
with that result, find the average distance Ravg to the midpoint, decide ok or proceed..
remove points from your set with a distance too far from Ravg found in step 2
go back to step 1 (average points again, yielding a better midpoint)
Of course, this will require some conditions for (2) and (4) that depends on the quality of your points cloud !
Ian Coope has an interesting algorithm in which he linearized the problem using a change of variable. The fit is quite robust, and although it slightly redefines the condition of optimality, I've found it to be generally visually better, especially for noisy data.
A preprint of Coope's paper is available here: https://ir.canterbury.ac.nz/bitstream/handle/10092/11104/coope_report_no69_1992.pdf.
I found the algorithm to be very useful, so I implemented it in scikit-guess as skg.nsphere_fit. Let's say you have an (m, n) array p, consisting of M points of dimension N (here N=3):
r, c = skg.nsphere_fit(p)
The radius, r, is a scalar and c is be an n-vector containing the center.

Looking for a "closing curves connecting with respect to points" algorithm

I am looking for an algorithm that can connect points together with a continuous curve line. Imagine drawing from point a to b to c until the last point, and when you draw from point to point, the line must be a curve and is continuous with respect to the previous point and next point, as if the given points are just samples of a closed loop. Please see figure below for illustration.
Are there such algorithm for something like this?
*The circles in the figure are my list of points.
Given that your points are ordered, spline interpolation is definitely the best way to go here. (As indicated by by bo1024's comment) I highly recommend the following notes:
http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/
And specifically the section here would be most relevant to getting a closed loop like you asked for:
http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve-closed.html
EDIT: If the curve has to pass through the points, then the unique degree n solution is the Lagrange interpolating polynomial. You can just make one polynomial for each component of your points vectors using the formula on the wiki page:
http://en.wikipedia.org/wiki/Lagrange_polynomial
Unfortunately Lagrange interpolation can be pretty noisy if you have too many points. As a result, I would still recommend using some fixed degree spline interpolation. Instead of B-splines, another option are Hermite polynomials:
http://en.wikipedia.org/wiki/Cubic_Hermite_spline
These will guarantee that the curve passes through the points. To get a closed curve, you need to repeat the the first d points of your curve when solving for the coefficients, where d is the degree of the Hermite spline you are using to approximate your points.
The problem is very similar to the travelling salesman problem, you may be able to extend some of the algorithms used to solve it to suit your case.
For instance, evolutionary algorithms are easy to adapt and you will find lot of references about using them to solve the TSP.

Reverse engineering a bezier curve

Given a few sample points on a bézier curve, is it possible to work out the set of possible curves these points may lie on?
In my specific application there is a limited set of endpoints the curve may have, so I want to generate the set of possible curves, enumerate all of them and pick out all the ones which may end on a valid end point.
Some people have asked for more detail.
I have a set of points which I know are on a quadratic bezier curve, I want to calculate the formula of the curve and be able to extrapolate new points on the curve.
Bezier curves will always go through starting and ending control points.
If the degree of the curve is equal to the number of sample points then there will be only one curve that will pass through all your points (in a normal case, where all points are different and they don't lie on a bezier curve of a lesser degree).
If the degree of a curve is less then the number of the sample points then, in general case, you will not be able to make the curve go through all the points (in a normal case).
If the degree of a curve is higher then the number of the sample points then, in general case, you will be able to draw infinite number of curves.
In the wiki article you will find references to control points only, but still I believe that I remember the above properties correctly and that they hold for the points on the curves as well.
I think you need to redefine your question and exactly define what type of curves (and of which degree) do you need. Also as Georg pointed out you might be looking for paths - a combination of curves.
EDIT:
First a correction - curve is defined with degree plus one number of control points points (quadratic need three). Control points are not the same as points on the curve - and for three points on the curve and quadratic curve you could have infinite number of solutions (see this for quadratic curve and four points)
As for the solution (but still under assumption that you are looking at a single curve):
For an equation for single quadratic curve you have
B(t) = (1-t)^2*P0 + 2*(1-t)*t*P1 + t^2*P2
Capital letters above are vectors, and P0 corresponds to starting control point (first point), P2 corresponds to ending control point (last point), so you still need to find P1. The variable t is scalar that ranges from 0 to 1.
If working with 2D curves the above vector equation gives two scalar equations for each point on the curve.
Still there is t as an unknown, so you should take 2 more points (4 in total) which will give you 4 unknowns (t for first point, t for second point, x and y of the P1, middle control point) and 4 equation to solve (2 from each sample point).
Solve that with your favourite numerical method and you will get the original curve on which the points came from.
If you still think that you can get more curves and that you will have to choose something then you are not working with bezier curves, but with bezier splines (in a sense of multiple curves joined together). Still the same principle applies and if you work out a way to solve a single curve from the above equations (and if you have enough points) then you can divide the problem into n-segments of actual bezier curves and solve each as outlined above.
If it turns out that you don't have enough points then look at the linked article again - you are probably looking for the smoothest curve and there are some suggestions in the article on how to get there as looking for the exact solution (shortest curve/smoothest curve) seems to be rather complex.

Resources