Closed lijon closed 7 months ago
Your step size when you trim (last step) is too small to produce a valid polyhedron. The geometry is correct, I think, but because of the rotation it self-intersects.
To elaborate a bit, here's the result when I use p1_trimmed = path_trim(p1, 6, 5.5);
It still looks OK, but that face on the left edge is getting very skinny.
With p1_trimmed = path_trim(p1, 6, 6);
the face has crossed over zero length and is going backwards.
So the problem is that this is simply an invalid input. Why does it work with twist=0? It's because the profile is much longer in one direction than the other, and the twist changes which direction is aligned with the curvature of the path. You can make it fail without any twist if you rotate the profile 90 deg.
So the problem is in my path_trim()?
Well, indirectly. I thought a bit more about what exactly what's going on. If you sample your path finely then there is no problem. The problem seems to arise specifically as a result of the tangent at the ends. I don't know if there's a way to do the last point tangent that is better. The tangent that is computed doesn't seem to be consistent with the rest of the shape, though.
I took a look a bit more at this. It doesn't seem like anything is wrong anywhere that can be fixed. Here's the path with tangents computed with uniform=false. They look reasonable:
The problem as I said before has to do with how the profile rotates into itself when you have the small step size and a small error in the tangent calculated at the ends. So possible solutions: avoid small stepsize. The best way to do that would be to sample the correct arc from the beginning. But if that's not convenient, then rewrite path_trim to omit the penultimate point at the ends if it's too close to the final point. I did that directly with list_remove for the example above and the result then looks fine:
Or alternatively but less accurately for a case like this you could use resample_path() after trimming. (You could also generate the original path at high resolution, trim, and then resample, which would be more accurate.)
Another option would be to make the tangent more accurate---so compute your own tangent vector from the formula for the arc at every point instead of doing this numerically from sampled data. I haven't tried this, but I believe it would fix the problem. It might be possible to get a more accurate numerical estimate of the tangent by using more points, but that could be bad for data which is less smooth, so it's not clear it's a good idea as a general code modification.
I'm thinking this issue can be closed.
Another option that might also work would be to compute the tangent numerically from the full path and interpolate it in the same way to get a tangent at the ends. That tangent should be more accurate, and consist with the other tangents, because it will be a 2-sided estimate instead of a 1-sided estimate of the derivative.
Code To Reproduce Bug
The path is trimmed at the ends, so it's not uniform anymore. However this breaks with twist. See artifact at the end:
Screenshots