Jump to content

[SOLVED] Polylines from near points


Recommended Posts

Hi,

I'm trying to create polylines between two groups of points, but not use the same 'destination' point twice.

 

The points are divided into two groups (upper and lower) and I'm using a point wrangle (with Group set to 'upper') to create the polylines:

int nrpt = nearpoint(0, 'lower', @P);
addprim(0, 'polyline', @ptnum, nrpt);
setpointgroup(0, 'lower', nrpt, 0);

By removing the destination point (nrpt) from the 'lower' point group I was hoping that nearpoint() wouldn't return that point again. It creates the polylines and removes the destination point from the point group, but it still uses that point again in the next iteration.

I thought this might be because the wrangle is multi-threaded and so the call to setpointgroup() is too late (the other threads have already done their thing) so I tried using a Detail wrangle instead:

int npts = npoints(0);

for(int i = 0; i < npts; i++) {
    if (inpointgroup(0, 'upper', i))
    {
        int nrpt = nearpoint(0, 'lower', point(0, 'P', i));
        addprim(0, 'polyline', i, nrpt);
        setpointgroup(0, 'lower', nrpt, 0);
    }
}

Unfortunately I get the same result :(

Anyone got any ideas?

test .hip attached...

polyline_connect.hipnc

Capture.PNG

Link to comment
Share on other sites

Just play with 1 inp and 2 inp on Wrangle ( code found here on Odforce).:wub:

int drawLine(vector f; vector t){
    int base=addpoint(0,f);
    int to=addpoint(0,t);
    int line=addprim(0,"polyline",base,to);
    return line;
}
//-----------------------------

int read=0;
if(npoints(1)>0){
    read=1;
}

int maxLines=chi("maxLineCount");
float minLen=chf("minLineLength");

int pts[]=pcfind(read,"P",v@P,chf("maxLineLength"),chi("maxFindCount"));
int randomConnect=chi("randomConnect");
int keepPointCount=min(1, max(0,chi("keepPointCount")));
int runner=0;
vector curPos;
int pt;
if(randomConnect == 0){
    for(int x=0; x<len(pts);++x){
        pt=pts[x];
        
        if(runner > maxLines){ break; }
        
        curPos=attrib(read,"point", "P", pt);
        if(length(curPos-v@P)>minLen && (@ptnum<pt || read)){
            if(keepPointCount){
                int to=pt;
                if(read){
                    to=addpoint(0,curPos);
                }
                addprim(0,"polyline",@ptnum,to);
            }else{
                drawLine(v@P,curPos);
            }
            runner++;
        }
    }
}else{
    int l=len(pts);
    float fl=float(l);
    int rander=pts[int(random(@ptnum*fl+randomConnect)*fl*fl) % l];
    curPos=attrib(read,"point", "P", rander);
    if(keepPointCount){
        int to=rander;
        if(read){
            to=addpoint(0,curPos);
        }
        addprim(0,"polyline",@ptnum,to);
    }else{
        drawLine(v@P,curPos);
    }
}

 

Link to comment
Share on other sites

Thanks @Librarian, but this seems to give me the same results :(

For each point in the upper group, I want to draw a line to the nearest point in the lower group that doesn't already have a line connecting it.

I'm probably missing something, but I can't see an 'already connected' check in the code you posted?

Capture.PNG

Link to comment
Share on other sites

Fixed it! 

In a Detail wrangle:

int npts = npoints(0);
int connected[];
//for each point
for(int i = 0; i < npts; i++) {
    if (inpointgroup(0, 'upper', i))
    {
        //find all nearest lower pts
        int nrpts[] = nearpoints(0, 'lower', point(0, 'P', i), 1000, 1000);

        //loop through lower points
        for(int j = 0; j < len(nrpts); j++) {
            //if not already connected
            if(connected[nrpts[j]] != 1){
                addprim(0, 'polyline', i, nrpts[j]);
                connected[nrpts[j]]=1;
                break;
            }
        }
    }
}

By creating a new array to store the 'connectedness' of each point I could remove the setpointgroup() logic.

Makes sense now I think about it - the wrangle was pulling in the nearpoints (and their group data) from upstream each iteration!

 

Link to comment
Share on other sites

  • josfella changed the title to [SOLVED] Polylines from near points

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...