Finding sections of a NURBS curve that has a curvature over a predefined value - math

I am trying to find the the sharp corners of a NURBS curve. For this problem I define a limit curvature. I am trying to find the sections on the curve that has a curvature higher then this value. One option is to interpolate over the curve and calculate curvature for all values but it may take time and some sharp points are likely to be missed. Any ideas about how to find these sections in an effective way?

Computing the derivative of the curvature analytically, I guess that you will find a (terrible) expression with a polynomial at the numerator. A good polynomial solver will allow you to find the roots, hence the extrema, to split the curve in sections with a monotonic curvature, and from there find the precise solutions of k=c by regula falsi or similar.
A much simpler approach is by flattening the curve (convert to a smooth polyline) and estimating the local curvature on all triples of consecutive points (using their circumscribed circle). High curvature sections will probably also be detectable by anomalies in the point density while flattening.
The benefit of flattening over uniform sampling is that it auto-adjusts the point density.
Another idea is to resort to a method of approximation of curves by circular arcs (this can be compared to a second order flattening operation). You will find a few papers on the topic (do not confuse with circle approximation by curves), but usually these methods are complex.
Maybe it is also possible to devise an analytic formula for a lower bound on the NURBS curvature in a given interval and use that to implement a bisection approach.

Related

Why is most software dealing with Bézier curves focusing on the cubic ones, to the detriment of other curve orders?

When you enter the Bézier curve feature of software like Microsoft Office, LibreOffice, and Blender, they let you create and juxtapose cubic, aka fourth-order, aka 4-control-point, Bézier curves. You click-and-drag creating the two points P0 and P3 and interpolate them, and the last two control points of the convex hull P1 and P2, that are not on the curve, are usually hidden or displayed as handles.
Why this focus on cubic (4 points) over quadratic (3 points), quintic (5 points), and higher-order curves?
Why is it considered uninteresting to lower or elevate the curve order?
When you complicate your curve design you usually join cubic Bézier curves together: this is what happens when you click repeatedly to add points, or subdivide. Why is so little software allowing you to define all your N control points at once, and then interpolate those with a Nth-order Bézier curve? This would be a constraint-based approach in opposition to the traditional "editing" approach (not sure how to word it).
Quadratic béziers allow curves to be joined so they share a tangent line. But they won't share the curvature. With unequal curvature, highlights and mirror effects will show an ugly discontinuity. The curvature is even more important when the curve is used to control a camera path or a robot trajectory. Cubic béziers can solve that.
Note that quadratic béziers are used in computer graphics, especially in the early days when calculation speed was more limited. For example TrueType fonts and Adobe Flash (the animation package that powered many websites until about a decade ago) depend on quadratic béziers.
Quartic curves are defined by 5 points; the curve will go through the end points, and its derivatives will be controlled by 3 more points. With cubic curves, one quickly gets an intuitive feeling of the function of the two controlling points; with a quartic the exact consequence of moving one of the inner control points is harder to guess. And when even more points would be involved, deformations would even be harder to control. Also, the computational cost goes up for curves involving more points.
These deformations are also the main reason why one doesn't use fully interpolating curves. Between the control points, undesired bends are hard to avoid.
PS: Did you check out "The Beauty of Bézier Curves"? For example, starting at 6:18, derivatives are explained. 9:07 deals with the curvature.
Probably the reason number one to join cubic splines (or low degree splines in general) is to maintain "locality" of control points i.e., moving a single control point only affects one segment of the curve or two at most if it is the joint point. That locality property is highly desirable on modeling applications. On the other hand high degree curves gives a more "global" effect to each control point.
I believe the cubic spline in particular gives the best compromise between locality and flexibility of the curve sice it can provide C^2 continuity when joining segments. The quadratic spline is also useful and valuable tool for the right problem but it only provides C^1 continuity when joining the segments, which can be a limitation for complex modeling applications.

Getting the boundary of a Bezier curve

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".

Calculate volume from crossections

I have an irregularly shaped 3d object. Of this object I know the areas of the crossections in regular intervals. How can I calculate the volume of this object?
You can only approximate the volume. Just add up all the areas and then multiply by the distance between intervals.
Obviously the smaller the distance between intervals, the more accurate the volume. It is just integration (calculus).
Discretize it using tetrahedra or bricks and add up their volumes, a la finite element methods. Integrate using Gaussian quadrature and sum.
You're estimating a Riemann integral. There are many methods to do this, of varying complexity. Simpson's rule is reasonably straightforward and will be pretty accurate as long as the cross-sectional area varies in a smooth enough fashion, however it requires that the number of intervals be even.
Ed Heal's answer is a Riemann sum that approaches the (volume) integral in the limit. Depending on where the cross-sections are located with respect to the extent of the object, it might be viewed as an application of the midpoint rule.
Assuming the cross-section area varies smoothly with distance (twice continuously differentiable along the axis perpendicular to the cross-sections), the midpoint rule and trapezoid rule have accuracy that improves with the square of the interval width (here assumed regular). Averaging the midpoint and trapezoid rule approximations amounts to an application of Simpson's rule, outlined in Peter Milley's answer, with higher order accuracy (improving with the fourth power of the interval width) provided the integrand is sufficiently smooth (continuous 4th derivative of cross-section area with respect to distance).
Of course many real world figures will not have such smoothness (too many corners, holes, etc.), so it is prudent not to expect exceptional accuracy from making more sophisticated approximations.

Calculating points on the curve

Is it possible to calculate intermediate points of a curve...Here is my mirror image
In the above image.Is it possible to calculate the intermediate points(one side) by knowing starting and ending point
If you know something about the curve it is, and it all depends on what you know about the curve (start and end points, initial slopes, center points, etc). There are generally two approaches:
If you know the equation of the curve, it's possible to do this exactly. Commonly curves like this are either circles or Bezier curves, and if you know it's either of these, you can fit all the other points exactly just given a few.
You can also do a cubic spline fit. This is a standard approach to fitting smooth curves so packages to do this are very common. On a smooth curve like this, give then end points, and, say, the middle point, the fit will be almost exact. (Here, you essentially end up with a Bezier curve, though parametrized a bit differently.)

Point Sequence Interpolation

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.

Resources