Given two points and a control point, one can easily draw a bezier path between the two points. What I would like to do use a bezier curve to draw a path that with changing width, by a assigning a "weight" to a the points of the curve which will determine its width. For example, if I give weight=0 to the first point of the curve and weight = 1 to the second point of the curve then something like the following path should be generated (the curve in the picture is cubic, but I am working with quadratic bezier curves):
In order to do this I would need to find the control points of the "edge" curves that determine the shape and then fill the shape that is found between the two new curves. However, I am quite unsure on how this can be done. One thing I thought about was to determine the starting and ending points of the new curves by simple drawing perpendicular segments to the line connecting the original control point and the original end points, but this still doesn't solve the problem of finding the new control points for the new curves.
I would use cubics instead of quadratics.
Yes you offset the control points perpendicularly by your weight but not the control points of BEZIER but control points of interpolation cubic (or catmull-rom) and then just convert that into Bezier control points. See related QAs:
How can i produce multi point linear interpolation?
How to create bezier curves for an arc with different start and end tangent slopes
draw outline for some connected lines
However much easier would be to directly render curve using Shaders and (perpendicular) distance. See:
Draw Quadratic Curve on GPU
That way you would not need to offset anything just interpolate the width of your curve ...
Maybe this could help, also there is an example on variable offseting
https://microbians.com/mathcode
Related
I have a list of cubic bezier curves in 3D, such that the curves are connected to each other and closes a cycle.
I am looking for a way to create a surface from the bezier curves. Eventually i want to triangulate the surface and present it in a graphic application.
Is there an algorithm for surfacing a closed path of cubic bezier segments?
It looks like you only know part of the details of the surface (given by the Bezier curves) and you've to extrapolate the surface out of it. As a simple example I'm imagining a bunch of circles in 3D with the center and radius that will be reconstructed into a sphere.
If this is the case you can use level sets. With level sets, you define a bunch of input parameters that defines the force exerted by the external factors on your surface and the 'tension' of the surface.
Crudely put, level sets define the behaviour of surface as they expand(or contract ) over time. As it expands it tries to maintain it's smoothness while meeting other boundary conditions - like 'sticking' to the circles in this case. So if you want a sphere from bunch of circles, the circles will exert a great force, while the surface will also be very tense.
Physbam has an open source implementation of level sets.
CGAL and PCL also provide a host of methods that generate surfaces from things such as points sets and implicit surface. You may be able to adapt one of them for your use.
You can look into the algorithms they use if you want to implement one on your own. I think at least one of them use the Poisson Surface Reconstruction algorithm.
I know how to describe curved line of constant thickness (with Bezier or similar models).
Are there any common models of curved line with variable thickness?
I am imagining some similar things like in Bezier. For example, each node can contain thickness value and it's weight, so renderer would interpolate thickness along curve.
Is there some implementations and/or descriptions?
UPDATE
More precisely the question is follows.
Suppose we have cubic Bezier segment, controlled by 4 points ABCD
In Bezier, the longer we have vector, say AB, then the longer curve follows AB direction. On the picture above, we have raltively long following.
So, I want thikness behave synchronously with control nodes B and C. If AB and CD is long, then thinkness should follow end nodes thinkness long and change to another thickness fast, like below
and if control vectors are short, then thinkness should smoothly change from one to another, like below
Metafont and its successor MetaPost
support variable thickness in the form of shaped pens.
See also
L.M. Mestetskii, Fat curves and representation of planar figures, Computers & Graphics, 24:1 (2000) 9-21 doi: 10.1016/S0097-8493(99)00133-8
If you want to use a "disc based" approach, you need to draw circles around every control point, then find the points on those circles that represent the "offset" (normal to the tangent, for on-curve points, normal to the tangent of the projection for off-curve points). You then plug those new points into the Bezier functions to get your "offset curve".
Curve offsetting, in your case with variable width, is essentially the trick of finding an outline rather than a single curve. For Bezier curves you can find a full explanation over at http://pomax.github.io/bezierinfo/#offsetting, with the variable width explanation over at http://pomax.github.io/bezierinfo/#offsetting (you're interested in the latter, but you need to understand the basics before you look at the special case =)
I have two objects: A sphere and an object. Its an object that I created using surface reconstruction - so we do not know the equation of the object. I want to know the intersecting points on the sphere when the object and the sphere intersect. If we had a sphere and a cylinder, we could solve for the equation and figure out the area and all that but the problem here is that the object is not uniform.
Is there a way to find out the intersecting points or area on the sphere?
I'd start by finding the intersection of triangles with the sphere. First find the intersection of each triangle's plane and the sphere, which gives a circle. Then find the circle's intersection/s with the triangle edges in 2D using line/circle tests. The result will be many arcs which I guess you could approximate with lines. I'm not really sure where to go from here without knowing the end goal.
If it's surface area you're after, maybe a numerical approach would be better. I'd cover the sphere in points and count the number inside the non-uniform object. To find if a point is inside, maybe trace outwards and count the intersections with the surface (if it's odd, the point is inside). You could use the stencil buffer for this if you wanted (similar to stencil shadows).
If you want the volume of intersection a quick google search gives "carve", a mesh based CSG library.
Starting with triangles versus the sphere will give you the points of intersection.
You can take the arcs of intersection with each surface and combine them to make fences around the sphere. Ideally your reconstructed object will be in winged-edge format so you could just step from one fence segment to the next, but with reconstructed surfaces I guess you might need to apply some slightly fuzzy logic.
You can determine which side of each fence is inside the reconstructed object and which side is out by factoring in the surface normals along the fence.
You can then cut the sphere along the fences and add the internal bits to the display.
For the other side of things you could remove any triangle completely inside the sphere and cut those that intersect.
Given an ordered list of points, I want to draw a smooth curve that passes through all of them. Each part of the curve can either be horizontal, vertical, or an arc with given radius r (all arcs will have the same radius). The transitions should be smooth, i.e., the heading at the end of one part should be the same as the heading at the beginning of the next part. There can be any number of arcs or straight line segments between any two consecutive input points.
It's sort of like a train track that should run orthogonally or along sections with fixed curvature.
Is there a good algorithm to construct such a curve? (or, in cases where such a line is not possible, I would like to know that.)
I looked into Bezier curves, but that seems like overkill and I couldn't find a good way to enforce my constraints.
What you are asking for above implies to me that you seek tangent continuity of your curve across points (similar to a spline with tangent continuity at knots). The train track analogy at least conveys this requirement. Given the strict limitations of straight lines, and fixed radius circular arcs I am fairly certain that you will not be able to do this. Why not consider a spline interpolation of your points if you require such smoothness instead?
To see why consider the following image:
Consider replacing the line segment between B and C with a circular arc. You can do it to make the join continuous, but to make it tangent continuous, you would need a great deal of good fortune as there is only one circle that is tangent continuous to the line segment AB that also touches point C. The chances of that circle having tangent at C matching the tangent of line CD is remote. It is possible that your data will line up like this but you cannot rely on it.
If I have misunderstood your question please let me know and I will adjust the answer.
Looking at Convert a quadratic bezier to a cubic?, I can finally understand why programming teachers always told me that math was so important. Sadly, I didn't listen.
Can anyone provide a more concrete - e.g., computer-language-y - formula for converting a quadratic curve to a cubic? Understanding that there's some rounding errors possible, which is fine.
Given a quad curve represented by variables:
StartX, StartY
ControlX, ControlY
EndX, EndY
And desiring StartX, StartY and EndX, EndY to remain the same, but to now have Control1X, Control1Y and Control2X, Control2Y of a cubic curve.
Is it...
Control1X = StartX + (.66 * (ControlX - StartX))
Control2X = EndX + (.66 * (ControlX - EndX))
With the same essential functions used to calculate Control1Y and Control2Y?
Your code is right except that you should use 2.0/3.0 instead of 0.66.
You avoid most rounding errors by using
Control1 = (Start + 2 * Control) / 3
Control2 = (End + 2 * Control) / 3
Note that line segments are also convertible to cubic Bezier curves using:
Control1 = Start
Control2 = End
This can be handy when converting a complex path mixing various types of curves (linear, quadratic, cubic).
There's also a basic transform for converting elliptic arcs to cubic (with some minor unnoticeable errors): you just have to split at least the arc on elliptic quadrans (cutting the ellipse first on the two orthogonal axis of symetries, or on arbitrary orthogonal axis passing through the center if the ellipse is a circle, then representing each arc; when the ellipse is a circle, the two focal points are confused on the same point, the center of the circle, so you can use any direction for one of the orthogonal axis).
Many SVG renderers do that by adding an additional split on octants (so that you get also precise position not only for points where the two main axis are passing through, but also for two diagonal axis which are bissecting (when the ellipse is a circle) each quadrant (when the ellipse is not a circle, assimilate it as a circle flattened with a linear transform along the small axis only, you do the same computation), because octants are also quite precisely positioned:
cos(pi/4) = sin(pi/4) = sqrt(2)/2 ≈ 0.71, and because this additional splitting will allow precise rendering of tangents on points crossing the diagonals at 45 degrees of the circle.
A full ellipse is then converted to 8 cubic arcs (i.e. 8 points on ellipse and 16 control points): you'll almost not notice the difference between elliptical arcs and these generated cubic arcs
You can create an algorithm that uses the same "flattening error" computed when splitting a Bezier to a list of linear segments, which are then drawn using the classic fast Bresenham algo for line segments; a "flattenning" algorithm just has to measure the relative deviation of the sum of lengths of the two straight segments joining the two focal points of the ellipse to any point of the generated cubic arcs, as this sum is constant on any true ellipse: if you make this measurement on the generated control points for the cubic arcs, the difference should be below a given percentage of the expected sum, or within an absolute distance precision, and can be used to create better approximation of control points with a simple linear formula so that these added points will be on the real ellipse.
Such transform of arbitrary paths is useful when you want to derive other curves from the path, notably the curves of "buffers" at a given distance, notably when these paths must be converted to "strokes" with a defined "stroke width": you need to compute two "inner" and "outer" curves and then concentrate on how to converting the miters/buts/squares/rounded corners, and then to cut long miters at a convenient distance (matching the "miter limit" factor times the "stroke width").
More advanced renderers will also use miters represented by tangent circles when there's a corner between two arcs instead of two segments (this is useful for drawing cute geographic maps)...
Converting an arbitrary path mixing segments, elliptic and bezier arcs to only cubic arcs is a necessary step to compute precise images without excessive defects visible when zooming in. This is then necessary when your "stroke" buffers have to take some effects (such as computing dashes), and then enhancing the result with semi-transparent pixels or subpixels to smooth the rendered strokes (smoothing is easy to computez only when everything has been flattened to line segments, and alsos may be simpler to develop if it only has to manage paths containing only cubic beziers: it can easily be parallelized if needed and accelerated by hardware). Bezier arcs are always interesting because drawing them is fast and requires only basic arithmetics, and the time needed to draw them is proportional to the length of the curve with every point drawn with the same accuracy level.
In summary, all curves are representable by cubic Bezier arcs with a maximum measurable deviation allowed (you can set this maximum deviation to one half pixel, or one subpixel if you first scale up the measurement grid for half-toning or subpixel shading, and then represent accurately every curve with a reasonnaly fast rendering, and get accurate rendering at any zoom level with curves smoothed everywhere, including with half-toning or transparency levels when finally drawing the linear strokes with the classic Bresenham algorithm using fast integer-only arithmetics). These rendered curve will all have the correct tangeants everywhere, without any unexpected angles visible on approximation points, and the remaining control points in the approximation will make also a good smooth rendering of the curvature everywhere (i.e. radius of the tangeant circle), so you can use this approximation as well to derive other measurements such as acceleration, inertial forces, or magnetic effects of paths of charged particles).
If you ever need higher precision, use Bezier arcs with degree 4 (i.e. with 3 control points between points on curve) to get smoothed derivation at a supplementary degree (e.g. gradients of forces), or just split the cubic arcs with additional steps further, until the derivation is smooth enough (but using degree-4 Bezier arcs requires much less points curves and less control points for the same accuracy tolerances, than when using cubic Bezier only).