PtNum Shuffle?

   9719   4   0
User Avatar
Member
49 posts
Joined: Jan. 2016
Offline
Hey All,

Simple question here. Id like to randomly reorder the points on a line WITHOUT using the sort node. I dont want positions to change (@P) just the ids (ptnums)– self imposed technical challenge.
Example start:
ptnum 1 @P {0,1,0};
ptnum 2 @P {0,2,0};
ptnum 3 @P {0,3,0};
ptnum 4 @P {0,4,0};
ptnum 5 @P {0,5,0};
ptnum 6 @P {0,6,0};

Example Desired result

ptnum 3 @P {0,1,0};
ptnum 2 @P {0,2,0};
ptnum 1 @P {0,3,0};
ptnum 5 @P {0,4,0};
ptnum 6 @P {0,5,0};
ptnum 4 @P {0,6,0};



I know you can do this with a Sort Sop (random) but I'd like to do it in a Vex Sop Specifically, and then in a wrangle afterwards–its a tech challenge thing. I feel like this is simple and Im missing something pretty basic.

This is what it says about the Random method for Sort
Random
Using the random seed, each point or primitive is assigned a random priority. They are then reordered according to that priority. This has the effect of generating a random ordering of the points or primitives.

Helpful info or not in this case?
If so, how can I apply this in a Vop Sop, and then later on if possible an Attribute Wrangle. All the random operators within the point vop give arbitrary new positions, not random new ones using only the old. A pointer in the right direction would be appreciated.


Thanks

Also, bonus question, what is the difference between ptnum and id in vop globals? They are behaving as if they were the same thing when I am running orders (say random) across points within the point Vop.

Thanks!
User Avatar
Member
1737 posts
Joined: May 2006
Offline
Here's one way, I'm sure there are better methods!

Create an array as a detail attribute as , shuffle it, then in a second point wrangle lookup @P based on that shuffled array.

Attachments:
sort_random_vex.hipnc (67.5 KB)

http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
1737 posts
Joined: May 2006
Offline
Re @id vs @ptnum;

ptnum is handled by houdini, you can't write to it, only read it. its always in order from 0 to the number of points you have, so if you start with a grid of 100 points, then randomly delete 50 of them, you end up with ptnums going from 0 to 50, regardless of what their original ptnum's were.

This would make things difficult if you were interested in a certain point, but were doing operations that added and removed points, because its ptnum would keep getting shuffled around.

A way around it is to store the original ptnum on another attribute, the convention is @id. You'll often see in existing setups a wrangle or similar near the start of a workflow with something like

@id = @ptnum;

That way if you were interested in point 6, no matter how many points are deleted, or how you mangle the geometry, you can do something like

if (@id == 6) {
   // do a cool thing here
}
There's caveats of course, but that's the idea. This comes up a lot with particle sims, where points are being created and destroyed all the time, if you want to track a specific particle and do things, @id is the way.
http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
459 posts
Joined: Oct. 2011
Offline
Based on Matt's example you could also just do it all in one detail wrangle by setting position from a random array index like this:
i[]@index;
int rand;
vector newpos;
int succ;

// create a simple ordered array
for (int i=0; i<@numpt;i++){
    @index[i] = i;
}

// set new point position from a random array index and shrinking the array for each iteration so we only select unused indexes.
for (int i = 0; i < @numpt; i++){
    rand = int(fit01(rand(i + chf('seed')),0 ,len(@index)));
    newpos = pointattrib( 0, 'P', pop(@index, rand), succ );
    setpointattrib(0,'P', i, newpos);
}

-b
Edited by bonsak - Nov. 11, 2017 10:15:07
http://www.racecar.no [www.racecar.no]
User Avatar
Member
8532 posts
Joined: July 2007
Online
it is not an easy task for VOPs or wrangle

since when reordering you can't just swap positions of the points, you have to swap all the attribute values and rewire all the vertices to other points, so in the end to be efficient, you'll additionally to VEX end up using Attribute Interpolate SOP and Rewire Vertices SOP, in which case I'd rather recommend using Sort SOP
Edited by tamte - Nov. 23, 2017 09:13:19
Tomas Slancik
FX Supervisor
Method Studios, NY
  • Quick Links