Jump to content

color particles according to their distance to each other


Recommended Posts

Hello,

 

I want to change the color of particles according to their distance to each other via "pop wrangle". This is my approach but its not working as expected. Unfortunately i have no errors or something like that so I cant get any further. If somebody has a hint for me it would be much appreciated.

kind regards 

 

Jon

vector pos;
vector col;

float maxdist = ch("distance");


v@Cd = set(0,0,0);

int handle = pcopen(0,"P",@P,ch("searchDist"),chi("maxPoints"));

while ( pciterate(handle) ){
    pcimport(handle,"P",pos);
    pcimport(handle,"Cd",col);
    
    if( distance(@P,pos)< maxdist) {
        col = set(1,0,0);
    }
}

colorParticlesAccordingToDistance.hipnc

Link to comment
Share on other sites

So where is the reference point? If you just loop through them then the code will just over write color from a previous iteration. Say I have 10 points in a line getting progressively farther apart. How would they be colorized? From which point do you start as a reference?

 

Here is some example code to illustrate the flaw.

vector pos;
vector col;
int result, index;
float maxdist = ch("distance");
float searchdist = ch("searchDist");

v@Cd = set(1,1,1);

string handle_path = "op:/obj/sphere:popobject/Geometry";
int particle_count = npoints(handle_path);
for (int i = 0; i < particle_count; i++) {
    vector my_pos =  point(handle_path,"P",i);
    for (int j = 0; j < particle_count; j++) {
        if (j != i) {
            // Not ourself so check for distance.
            vector neighbor_pos =  point(handle_path,"P",j);
            float neighbor_distance = length(neighbor_pos-my_pos);
            if (neighbor_distance < searchdist) {
                // This point candidate is within our range.
                col = set(1,0,0);
            }
        }
        setpointattrib(geoself(), "Cd", j, col, "set");
    }
   
}

In the end all the points will always be red because each one gets it's turn at being the center of attention. Thus colorizing their neighbor who then get their turn etc...

 

Some kind of reference points are needed to specify a center for the search to take place.

Edited by Atom
  • Like 1
Link to comment
Share on other sites

Unless you are doing this to learn point clouds, there is a pop proximity node that does this for you.

 

This operator sets attributes on the input particles that record which other particles are closest and how far away they are.

 

  • Like 1
Link to comment
Share on other sites

@Atom:

Thank you for looking into the code and your solution. I have to look at it in detail...admittedly this has become a little high for my rudimentary coding skills :) 

 

@mawi:

Thank you very much for this hint. I will look into that too.

 

Jon

Link to comment
Share on other sites

if coloring based on the distance to closest point is what you are after, then no loops are necesary, you can directly query closest point (excluding self) based on distance and if it's found just set the color

 

vector @Cd = 0;
float maxdist = chf("distance");

int pts[] = nearpoints(0, @P, maxdist, 2);
if (len(pts)==2)
{
    vector ptP = point(0, "P", pts[1]);
    v@Cd = {1,0,0};
}
 

colorParticlesAccordingToDistance_fix.hipnc

  • Like 2
Link to comment
Share on other sites

That is cool Tom. You can get a little bit of grey scale action on distance by adding a new control for count and increasing it. I'm not sure if the math is still valid, however, but it presents a nice salt & pepper look.

 

I added a new integer parameter to the interface.

vector @Cd = 0;
float maxdist = chf("distance");

int pts[] = nearpoints(0, @P, maxdist, int(ch("count")));
int l = len(pts);
if (l>1){
    float delta = 1.0/l;
    float v = 0.0;
    for (int i=1; i<l; i++) {
        v = 1.0-(delta*i);
        vector ptP = point(0, "P", pts[i]);
        v@Cd = set(v,v,v);
    }
}

post-12295-0-49127100-1457892871_thumb.j

ap_colorParticlesAccordingToDistance_fix.hipnc

Edited by Atom
  • Like 2
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...