PDA

View Full Version : Which particle id connects to the arrayMapper?


vfortin
08-22-2006, 01:44 PM
Hi eveyone,

I have an arrayMapper providing a particle object with an array of values. Just like it does when you connect a ramp to a PP attribute.

The size of the arrayMapper.outValuePP is equal to the number of particles no matter how many particles die during the simulation.
That means that the array is constantly updated and, more important, the position occuped by a particle in the array will change to fill the space left by a dying one.

As you can imagine, the order of IDs quickly becomes thrown into disarray and almost impossible to follow unless you keep track of all particles died and newborns.
But Maya does it, it seems, under the hood.
For example, when you type this in a particle expression:
particleShape1.radiusPP = particleShape1.myArrayPP
Maya knows right-away which index of the array the current particle ID is reffering to even if the array order has changed during the previous simulation frame. Go figure how, that's what I'm looking for.

I'm asking this because I'm working with MEL commands within particle expressions.
I'm using something like this `getAttr(particleShape1.myArrayPP[$x])`, $x being the index in the array occupied by the currect particle.

Hopefuly someone here has gone throught this before me.
Is there some kind of formula out there to keep track on this without too much calculation overhead?

Thanks

Vincent

JamesPiechota
08-25-2006, 03:09 AM
The particle node's 'idMapping.sortedId' and 'idMapping.idIndex' arrays provide a relatively quick way of going from particleId to pp-attribute index. To go the other direction you can query the pp-attribute index in the particle system's particleId array attribute.

particleId -> ppAttrIndex :

Search sortedId for the particle ID. Plug the index of that particle ID in the sortedId array into the idIndex array to get the index into the pp-attribute arrays. (Confusing, eh?)

// Or, in pseudo-code
int i = index of particleId in sortedId array;
ppAttrIndex = idIndex[i];


Also, since sortedId is sorted you can theoretically find the particleId in O(logn) time.

vfortin
08-28-2006, 08:26 PM
Wow, it took me some time to get my head around this one.
These are great, thanks for pointing them out!

While doing my tests I realized that it also works by finding out the corresponding index in the id array.

In other words...
id -> sortedId -> idIndex
...is an equivalent method to:
id -> id[]
Looks like it's as simple as that :buttrock:

I'm not quite sure what you meant by O(logn) time though. Care to share the details?

Vincent

JamesPiechota
08-28-2006, 11:11 PM
O(log n) is a computer science geek term to mean its "pretty quick" to find the ID using the sortedId's array. Because the Id's are sorted you can, theoretically, find your target id much quicker than if the array was not sorted. If there was no sortedId's array and you had to manually walk through each element in the particleShape's array of particleIds, the time would be O(n^2), or "pretty slow".

For example, say you have 8 particle ids in an unsorted array:

1 2 4 3 0 6 5 7

If you're looking for particleId 7 you might have to check all 8 of the array elements before you find the id: 1, then 2, then 4, then 3, then 0, ....

But if the array is sorted:

0 1 2 3 4 5 6 7

You can jump around in the array and use something called a binary search to find the element in only 3 tries: 4, then 6, then 7. With each jump you know whether to move up or down in the array because you know the ids are sorted (e.g. 4 is less than 7 so my next jump has to move up in the array). As it turns out log(8) is 3.

vfortin
08-29-2006, 06:01 PM
Thanks James.

Wouldn't be simplier if attribute arrays were all sorted?

The way they are ordered is weird and slower to filter.

Cheers!

Vincent

JamesPiechota
08-29-2006, 10:47 PM
I hear you. Life would be easier - unfortunately keeping an array sorted takes some work, and Maya would undoubtably run slower if it had to keep all arrays sorted.

CGTalk Moderation
08-29-2006, 10:47 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.