# How to smooth jagged extrusion

Sure!

The sliding average thing I did was just a naive variant on a low-pass filter, which is just a fancy formalization of a blur. We perceive rapid, dramatic shifts in value a â€śjagged,â€ť whereas gradual shifts are â€śsmooth,â€ť so a low-pass filter just works to â€śsmooth outâ€ť dramatic value shifts by distributing them across multiple samples. For example, consider the array `[0, 0, 0, 0, 3, 0, 0, 0, 0]`. This array would be perceived as â€śjaggedâ€ť because it contains a sudden, dramatic shift from 0 to 3, then another dramatic shift from 3 back to 0. To remove this, we can run this array through a low-pass filter â€“ for the purposes of discussion, letâ€™s say with a sliding window size of 3 â€“ which will essentially just replace each value with an average of itself and the values around it, making our new array

`[avg(0, 0), avg(0, 0, 0), avg(0, 0, 0), avg(0, 0, 3), avg(0, 3, 0), avg(3, 0, 0), avg(0, 0, 0), avg(0, 0, 0), avg(0, 0)]`

which in turn becomes

`[0, 0, 0, 1, 1, 1, 0, 0, 0]`

(Note that the first and last values donâ€™t have full size-3 windows available because they run up against the ends of the array. There are several ways to handle that kind of edge case; I just chose one that was easy. )

After filtering, the array doesnâ€™t contain any shifts as dramatic as 0 to 3, and what little shifts it does have are further apart in the array, not on opposite sides of a single number. Consequently, we perceive this new array as much more â€śsmoothâ€ť than the original array, just by taking the average of a sliding window.

Low-pass filters are easy to use and theyâ€™re good for a lot of things, but unfortunately they have quite a few limitations. Theyâ€™re good for smoothing noisy data, but they do so by throwing away precision. I used them to test my hypothesis because I wanted something self-contained, so that I could be sure I wasnâ€™t introducing new problems in my test; but for actually generating smooth paths, I think there are better tools.

Have you heard of Hermite splines? Theyâ€™re a really cool family of math functions that can generate smooth, graceful paths from just a small number of control points. The math can get a little hairy, but fortunately for us we donâ€™t have to deal with that: Babylon.js already supports a bunch of built-in fancy curves, including my personal favorite Catmull-Rom splines!

2 Likes