Early this week, this ICE “brain teaser” was posted on the XSI mailing list.
Given an array like this:
8,8,8,2,2,2,2,5,5,5,5,9,9,9,9,1,1,1,1,1,1
how can can I –without using any loops–convert it to an array like this
1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,5,5
Martin Chatterjee came up with a nice solution using arrays.
As an exercise, I tried to do it using a [non-simulated] point cloud. Basically, the idea is to use the particle self.ID data set to index into the array without using a Repeat node.
Here’s what I ended up with. First, I set a boolean flag on each point to indicate whether the corresponding array element is the same as the previous, and then I do a cumulative sum of array elements.
My first try at this ended up as an ICE tree version of this algorithm, plugged into the On Creation port of Add Point.
var a = [8,8,8,2,2,2,2,5,5,5,5,9,9,9,9,1,1,1,1,1,1]; var a1 = new Array(a.length); var val = -1; var ix = -1; for (var i=0;i<a.length;i++) { if ( a[i] != val ) { val = a[i]; ix++; } a1[i] = ix; } LogMessage( a ); LogMessage( a1 ); // INFO : 8,8,8,2,2,2,2,5,5,5,5,9,9,9,9,1,1,1,1,1,1 // INFO : 0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,4
But that didn’t work, because as I found out, I wasn’t able to carry attribute values over from one point to the next. In the example below, you can see that self.tmp is local to each point (which means it starts off at zero for each point). Instead of incrementing self.tmp each time, I end up setting it to 1 every time.