Notifications
Clear all
[Closed] Remove extra spline-knots
Sep 21, 2021 12:28 am
I’m looking for a way to optimize a spline by removing uneccessary knots from a spline (the red knots in the image below):
Does anyone know of a good way to do this?
2 Replies
Sep 21, 2021 12:28 am
If you’re not looking for a code solution specifically, there is a modifier called ‘optimize spline’ which may work for you.
Alternatively, there are a few scripts on www.scriptspot.com that optimize splines in various ways.
For a code sample, here is a very basic example which is neither elegant or very cleverly optimized. I’m sure some of the other folks here could do a much better job. There is zero error checking included.
-- optimize the spines by removing points that lie along a straight line
newspline = $
local epsilonshapepoints = .001
slpinecount = numsplines newspline
for splineindex = 1 to slpinecount do(
knotcount = numknots newspline splineindex
knotindex = 1
--format "knotcount = %\n" knotcount
while knotcount > 2 and knotindex <= knotcount do(
-- get the three points we're going to test
p1index = knotindex - 1
if p1index <= 0 do p1index = knotcount
p2index = knotindex
p3index = knotindex + 1
if p3index > knotcount do p3index = 1
p1 = getKnotPoint newspline splineindex p1index
p2 = getKnotPoint newspline splineindex p2index
p3 = getKnotPoint newspline splineindex p3index
v1 = p2 - p1
v2 = p3 - p1
crossresult = length( cross (normalize v1) (normalize v2))
if crossresult < epsilonshapepoints then(
-- delete the point
deleteknot newspline splineindex knotindex
knotcount = numknots newspline splineindex
)
else knotindex += 1
)
)