Closed vorg closed 4 years ago
Hey @vorg - with the above commits I've just added some new functions which should address most of this issue, mainly: openCubicFromControlPoints()
and sampleCubicArray
(via this higher-order helper). I will try to do a new release later today...
There should also be an openCubicFromBreakPoints()
version at some point, but don't have time for that right now... Also, am sure some of this can/should be refactored/merged...
The use of the main @thi.ng/geom package in the example below is just for demo/convenience and the SVG output here. You can also just use the resample()
function from the @thi.ng/geom-resample pkg to work with the raw point array...
import { openCubicFromControlPoints, sampleCubicArray } from "@thi.ng/geom-splines";
import { polyline, resample, circle, svgDoc, asSvg } from "@thi.ng/geom";
// input control points
const src = [[0,0], [100,50], [200,0], [150,100], [200,200], [100,150], [0,200]];
// cubic segments
const segments = openCubicFromControlPoints(src, 2/3);
// sampled curve points (w/ default sample config)
// the `false` arg indicates a non-closed curve
const pts = sampleCubicArray(segments, false);
// uniformly resample polyline
const line = resample(polyline(pts), { dist: 10 });
fs.writeFileSync(
"test.svg",
asSvg(
svgDoc(
{ stroke:"black" },
line, ...line.points.map((p) => circle(p, 1))
)
)
);
Just for future reference, here's how you'd use the aforementioned resample()
function to resample the raw point array (without @thi.ng/geom shape wrappers):
resample(
sampleCubicArray(
openCubicFromControlPoints(src, 2 / 3),
// open (non-closed) curve
false,
// optional config (here default aka 20 points per cubic segment)
{ num: 20 }
),
// uniform distance resampling
{ dist: 5 }
)
1) The resample
step is obviously optional and you could also ask for a fixed number of points (aka num
option) instead of uniform distance.
2) Do NOT use the dist
sampling option for the first stage, since that only applies to single segments, rather than the complete polyline. Also, as you can see, there's no need to call simplify()
, since sampleCubicArray
doesn't create duplicated intermediate vertices...
@vorg - openCubicFromBreakPoints()
is now available too...
Hi @vorg - this is all released now as "@thi.ng/geom-splines": "0.5.2"
. Let me know if that works for you and we can close this issue... thx! :)
Works great! My strange attractors thank you...
@vorg Nice one! :) + great to hear!
I'm trying to create smooth curve from a polyline and hit a roadblock when realized that cubic-from-controlpoints assumes that curve is closed. Turns of that
buildNonUniform
is all I needed. Is there an official way to convert list of points to list of Cubics? Currently i'm doing:assuming following buildNonUniform from cubic-from-controlpoints
and modified segment generation
Note:
vec3.lerp
comes from pex-math/vec3.js#L150 and is similar tomixN
.