mirror of
https://github.com/nophead/NopSCADlib.git
synced 2025-01-16 21:18:15 +01:00
Added bezier_join().
Moved path_length() from sweep.scad to maths.scad.
This commit is contained in:
parent
76aa613093
commit
42fccc1afb
@ -6130,6 +6130,10 @@ Annotation used in this documentation
|
||||
## Bezier
|
||||
Bezier curves and function to get and adjust the length or minimum z point.
|
||||
|
||||
`bezier_join()` joins two paths with a Bezier curve that starts tangential to the end of `path1` and ends tangential to the end of `path2`.
|
||||
To do this the outer control points are the path ends and the inner two control points are along the tangents to the path ends.
|
||||
The only degree of freedom is how far along those tangents, which are the `d` and optional `d2` parameters.
|
||||
|
||||
[utils/bezier.scad](utils/bezier.scad) Implementation.
|
||||
|
||||
[tests/bezier.scad](tests/bezier.scad) Code for this example.
|
||||
@ -6140,6 +6144,7 @@ Bezier curves and function to get and adjust the length or minimum z point.
|
||||
| `adjust_bezier_length(v, l, eps = 0.001, r1 = 1.0, r2 = 1.5, l1, l2)` | Adjust Bezier control points `v` to get the required curve length `l` |
|
||||
| `adjust_bezier_z(v, z, eps = 0.001, r1 = 1, r2 = 1.5, z1, z2)` | Adjust Bezier control points `v` to get the required minimum `z` |
|
||||
| `bezier(t, v)` | Returns a point at distance `t` [0 - 1] along the curve with control points `v` |
|
||||
| `bezier_join(path1, path2, d, d2 = undef)` | Join two paths with a Bezier curve, control points are the path ends are `d` and `d2` from the ends in the same direction. |
|
||||
| `bezier_length(v, delta = 0.01, t = 0, length = 0)` | Calculate the length of a Bezier curve from control points `v` |
|
||||
| `bezier_min_z(v, steps = 100, z = inf, i = 0)` | Calculate the minimum z coordinate of a Bezier curve from control points `v` |
|
||||
| `bezier_path(v, steps = 100)` | Returns a Bezier path from control points `v` with `steps` segments |
|
||||
@ -6430,6 +6435,7 @@ Maths utilities for manipulating vectors and matrices.
|
||||
| `map(v, func)` | make a new vector where the func function argument is applied to each element of the vector v |
|
||||
| `mapi(v, func)` | make a new vector where the func function argument is applied to each element of the vector v. The func will get the index number as first argument, and the element as second argument. |
|
||||
| `nearly_zero(x)` | True if x is close to zero |
|
||||
| `path_length(path, i = 0, length = 0)` | Calculated the length along a path |
|
||||
| `quadratic_real_roots(a, b, c)` | Returns real roots of a quadratic equation, biggest first. Returns empty list if no real roots |
|
||||
| `radians(degrees)` | Convert radians to degrees |
|
||||
| `reduce(v, func, unity)` | reduce a vector v to a single entity by applying the func function recursively to the reduced value so far and the next element, starting with unity as the initial reduced value |
|
||||
@ -6650,7 +6656,6 @@ Each vertex, apart from the first and the last, has an associated radius and the
|
||||
| `cap(facets, segment = 0, end)` | Create the mesh for an end cap |
|
||||
| `circle_points(r = 1, z = 0, dir = -1)` | Generate the points of a circle, setting z makes a single turn spiral |
|
||||
| `helical_twist_per_segment(r, pitch, sides)` | Calculate the twist around Z that rotate_from_to() introduces |
|
||||
| `path_length(path, i = 0, length = 0)` | Calculated the length along a path |
|
||||
| `rectangle_points(w, h)` | Generate the points of a rectangle |
|
||||
| `rounded_path(path)` | Convert a rounded_path, consisting of a start coordinate, vertex / radius pairs and then an end coordinate, to a path of points for sweep. |
|
||||
| `rounded_path_vertices(path)` | Show the unrounded version of a rounded_path for debug |
|
||||
|
@ -77,6 +77,17 @@ module beziers() {
|
||||
|
||||
translate(control_points[1] - [0, 0, 2])
|
||||
label(str("bezier_length = ", length, ", bezier_min_z = ", bezier_min_z(curve)), valign = "top");
|
||||
|
||||
path1 = [[20, 20, 0], [40, 20, 0]];
|
||||
path2 = [[70, 40, -5], [60, 40, 0]];
|
||||
|
||||
color("green")
|
||||
for(p = concat(path1, path2))
|
||||
translate(p)
|
||||
sphere(1);
|
||||
|
||||
color("orange")
|
||||
sweep(bezier_join(path1, path2, 10), circle_points(0.5, $fn = 64));
|
||||
}
|
||||
|
||||
if($preview)
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 85 KiB |
@ -19,8 +19,13 @@
|
||||
|
||||
//
|
||||
//! Bezier curves and function to get and adjust the length or minimum z point.
|
||||
//!
|
||||
//! `bezier_join()` joins two paths with a Bezier curve that starts tangential to the end of `path1` and ends tangential to the end of `path2`.
|
||||
//! To do this the outer control points are the path ends and the inner two control points are along the tangents to the path ends.
|
||||
//! The only degree of freedom is how far along those tangents, which are the `d` and optional `d2` parameters.
|
||||
//
|
||||
include <../global_defs.scad>
|
||||
include <maths.scad>
|
||||
|
||||
function bezier(t, v) = //! Returns a point at distance `t` [0 - 1] along the curve with control points `v`
|
||||
(len(v) > 2) ? bezier(t, [for (i = [0 : len(v) - 2]) v[i] * (1 - t) + v[i + 1] * (t)])
|
||||
@ -55,3 +60,16 @@ function adjust_bezier_z(v, z, eps = 0.001, r1 = 1, r2 = 1.5, z1, z2) = //! Adju
|
||||
: let(r = r1 + (z - z1) * (r2 - r1) / (z2 - z1))
|
||||
abs(r - r1) < abs(r - r2) ? adjust_bezier_z(v, z, eps, r, r1, undef, z1)
|
||||
: adjust_bezier_z(v, z, eps, r, r2, undef, z2);
|
||||
|
||||
function bezier_join(path1, path2, d, d2 = undef) = let( //! Join two paths with a Bezier curve, control points are the path ends are `d` and `d2` from the ends in the same direction.
|
||||
d2 = is_undef(d2) ? d : d2,
|
||||
l = len(path1),
|
||||
p0 = path1[l - 1],
|
||||
p1 = p0 + unit(p0 - path1[l - 2]) * d,
|
||||
p3 = path2[0],
|
||||
p2 = p3 + unit(path2[0] - path2[1]) * d2,
|
||||
v = [p0, p1, p2, p3],
|
||||
segs = path_length(v) / $fs,
|
||||
path = [for(i = [1 : segs - 1], t = i / segs) bezier(t, v)],
|
||||
len = len(path)
|
||||
) concat(path1, path, path2);
|
||||
|
@ -187,3 +187,7 @@ function cubic_real_roots(a, b, c, d) = //! Returns real roots of cubic equation
|
||||
) roots == 1 ? [x] :
|
||||
roots == 2 ? [3 * q /p + inflection, -3 * q / p / 2 + inflection] :
|
||||
[for(i = [0 : roots - 1]) 2 * sqrt(-p / 3) * cos(acos(3 * q * sqrt(-3 / p) / p / 2) - i * 120) + inflection];
|
||||
|
||||
function path_length(path, i = 0, length = 0) = //! Calculated the length along a path
|
||||
i >= len(path) - 1 ? length
|
||||
: path_length(path, i + 1, length + norm(path[i + 1] - path[i]));
|
||||
|
@ -177,10 +177,6 @@ module sweep(path, profile, loop = false, twist = 0, convexity = 1) { //! Draw a
|
||||
polyhedron(points = mesh[0], faces = mesh[1], convexity = convexity);
|
||||
}
|
||||
|
||||
function path_length(path, i = 0, length = 0) = //! Calculated the length along a path
|
||||
i >= len(path) - 1 ? length
|
||||
: path_length(path, i + 1, length + norm(path[i + 1] - path[i]));
|
||||
|
||||
function circle_points(r = 1, z = 0, dir = -1) = //! Generate the points of a circle, setting z makes a single turn spiral
|
||||
let(sides = r2sides(r))
|
||||
[for(i = [0 : sides - 1]) let(a = dir * i * 360 / sides) [r * cos(a), r * sin(a), z * i / sides]];
|
||||
|
Loading…
x
Reference in New Issue
Block a user