Jump to content

VEX: Average Neighbouring Points Sample


galagast

Recommended Posts

I'm currently studying VEX and tried to add Iterations to the default "Average Neighbouring Points" sample that comes with the Attribute Wrangle SOP.

post-3610-0-56848300-1447843798_thumb.jp

 

My goal is to write it all in one node, thus I'm running the code over Detail instead of points.

 

So far the code seems to work OK.. but the result seems to be slightly different when I set the Iteration to 1.

I was expecting the result to be the same as the sample VEX code.

 

By Iterations, I mean that instead of copying the sample wrangle node multiple times, I would just have a parameter to control how many times I want to average the neighbouring points.

 

I'm also refraining from using a Foreach SOP, as this is just an exercise for me to learn VEX.

 

H15 file attached below.

average_neighbouring_points.hipnc

Link to comment
Share on other sites

the result is probably different due to the nature of serial evaluation

you are iterating over array of positions, but at the same time updating that array with new values

so if the currently processed point has some neighbor(s) with already updated P values if will of course result in different P than the same point in the example where it's always averaging unchanged P of it's neighbors 

 

so you would need to introduce temporary array with P values from previous iteration to sample from if you want to get the same result

  • Like 2
Link to comment
Share on other sites

Awesome, the results are now the same! Thank you very much for looking into it!

I've now updated the file.

 

old code:

int np = npoints(0);
vector posArr[];

// initialize position data.
for (int i=0; i<np; i++)
{
    vector pos = point(0, "P", i);
    push(posArr, pos);
}

// iterate
int iters = chi("iters");
for (int x=0; x<iters; x++)
{
    for (int i=0; i<np; i++)
    {
        int n[] = neighbours(0, i);
        vector avgP = posArr[i];
        
        foreach(int pt; n)
        {
            avgP += posArr[pt];
        }
        
        avgP /= len(n)+1;
        posArr[i] = avgP;
    }
}

// set positions
for (int i=0; i<np; i++)
{
    setpointattrib(0, "P", i, posArr[i], "set");
}

new code:

int np = npoints(0);
vector posArr[];
vector tempPosArr[]; //*NEW*

// initialize position data.
for (int i=0; i<np; i++)
{
    vector pos = point(0, "P", i);
    push(posArr, pos);
}

// iterate
int iters = chi("iters");
for (int x=0; x<iters; x++)
{
    for (int i=0; i<np; i++)
    {
        int n[] = neighbours(0, i);
        vector avgP = posArr[i];
        
        foreach(int pt; n)
        {
            avgP += posArr[pt];
        }
        
        avgP /= len(n)+1;
        tempPosArr[i] = avgP; //*NEW*
    }
    posArr = tempPosArr; //*NEW*
}

// set positions
for (int i=0; i<np; i++)
{
    setpointattrib(0, "P", i, posArr[i], "set");
}

The attached H15 file below also has the new ForEach method for comparison.

 

I was just wondering, when I try to interactively change the Max Iterations in the ForEach SOP method, I get instant feedback, whilst using the Wrangle method, you would notice that it is kinda sluggish?

I also tried adding $F/10 to the parameters to see how it handles animation. The ForEach method really is much faster. I'm just curious as to what's causing it :)

 

My initial hunch is that the Run Over Detail method is unable to use multi-threading on every single point (as compared to the Point Wrangle inside the ForEach).

 

average_neighbouring_points_2.hipnc

Edited by galagast
Link to comment
Share on other sites

While reading through the docs, I just noticed that part of the update that came with H15 for the Attribute Wrangle was the ability to Run Over Numbers.

 

I got curious as to how it can be used. (Because maybe a could try a variant of the above using this method).

 

Here's what the 'What's New" docs says:

The Attribute VOP SOP, Attribute Wrangle SOP, Geometry VOP DOP, and Geometry Wrangle DOP have a new Run over > Numbers mode, which runs the VEX code a certain number of times. In this mode, @elemnum is the current iteration number (starting at 0), and @numelem is the total number of iterations. This mode only binds detail attributes and binds them as read-only.

 

 

So now I tried doing a simple code to test how it runs through a value:

int count;
if (@elemnum == 0)
    count = 0;
count++;

addattrib(0, "detail", "foo", count);

post-3610-0-86918500-1448033928_thumb.pn

Setting the Number Count to 10, I was expectng to see a detail attribute with the value of 10 as a result.

But oddly enough, the value returned is only 1.. it seems to always reset the count to 0, so running count++ only always adds 1 to 0.

 

If it's not too much to ask.. can anyone provide a simple usage of the Run-Over Numbers example?

Link to comment
Share on other sites

run over numbers runs them in parallel, not one after another

so if you just want add 1 in each step you do:

float value = 1.0;
adddetailattrib(0, "sum", 0.0);
setdetailattrib(0, "sum", value, "add");

so if the value is some expensive calculation, that can run independently for each number, and you need sum of those, then you will get speedup since they will run in parallel

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

you probably also want to consider doing stuff like this:

////////////////////////////

 

int np = npoints();
vector posArr[];
vector tempPosArr[]; //*NEW*

 

resize(posArr,np);

// initialize position data.
for (int i=; i<np; i++)
{
    vector pos = point(, "P", i);
    posArrpos;
}

 

/////////////////////////

 

that will give you a slight performance boost, since you are not re-initializing the array for each point that you add to it.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...