jon3de Posted March 13, 2016 Share Posted March 13, 2016 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 Quote Link to comment Share on other sites More sharing options...
Atom Posted March 13, 2016 Share Posted March 13, 2016 (edited) 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 March 13, 2016 by Atom 1 Quote Link to comment Share on other sites More sharing options...
mawi Posted March 13, 2016 Share Posted March 13, 2016 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. 1 Quote Link to comment Share on other sites More sharing options...
jon3de Posted March 13, 2016 Author Share Posted March 13, 2016 @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 Quote Link to comment Share on other sites More sharing options...
anim Posted March 13, 2016 Share Posted March 13, 2016 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 2 Quote Link to comment Share on other sites More sharing options...
Atom Posted March 13, 2016 Share Posted March 13, 2016 (edited) 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); } } ap_colorParticlesAccordingToDistance_fix.hipnc Edited March 13, 2016 by Atom 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.