Saturday, September 12, 2009

NURBS Knot Vectors In Perl 6 (part 2)

This is the sort of frustrating post you occasionally get when working with a compiler in the process of development like Rakudo. Because the cool thing I want to do is legal Perl 6 (I'm pretty sure), but it doesn't come close to working in Rakudo.

When we last left knot vectors, we had defined a recursive function for calculating the knot vector basis. The problem with that implementation is each level calls the prior level twice. In practice, this means most of values are calculated repeatedly. For higher order NURBS objects, this is massively inefficient. So ideally we'd like to calculate each value of N exactly once. This can be done by calculating all the degree 0 values of N, then use those results to calculate all the degree 1 values, then use those to calculate the degree 2 values, and so on. My big idea here was that with Perl 6's hyper operators, each level can be calculated without even using a loop! So given an array of knots named @.knots, the prior degree's array of N values called @N_prev, and $end = @.knots.end, the calculation looks like this:


I think that is correct. But it makes Rakudo very unhappy: "Non-dwimmy hyperoperator cannot be used on arrays of different sizes or dimensions." Mind you, that's happening in $u «-« @.knots[0..($end - $p - 1)], which is very definitely a dwimmy hyperoperator. And that's just the tip of the iceberg -- in fact, I have yet to simplify this calculation enough to make Rakudo happy with it. It seems Rakudo is really not ready to handle deeply nested hyperoperators yet. You can work around it in this case by using the Texas version of the operator. It also seems there is some odd glitch the second time you call »O/«: you get the error message "ResizablePMCArray: Can't pop from an empty array!" even simplifying the use until it is very obviously right.

I believe both bugs have been reported already, so there's nothing to do but dumb down the code so that it will work with the current Rakudo. Sigh.

No comments:

Post a Comment