# foreach SOP

## Recommended Posts

hello!

help me please to solve some problem. for example, i have two lines - one have 10 points and one have only two. i want somehow query, for example, the nearest points - so i want to cycle every point on one line and another and query the distance between it and those from another, and find minimum of this value.. i think i should use foreach SOP and/or VOP SOP for such task! Could you help me, how should i do - how should i use this powerful nodes?

##### Share on other sites
hello!

help me please to solve some problem. for example, i have two lines - one have 10 points and one have only two. i want somehow query, for example, the nearest points - so i want to cycle every point on one line and another and query the distance between it and those from another, and find minimum of this value.. i think i should use foreach SOP and/or VOP SOP for such task! Could you help me, how should i do - how should i use this powerful nodes?

hi mic, I've written something in vop sop maybe it's what You are looking for.

closest_point.hipnc.hip

cheers

EDIT:

```	vector pts[] = array({0,0,0}, {0,0,0}); // handle information about closest points, pts[0] - input one, pts[1] - input two

int i=0, j=0;
int npt1 = npoints(1); // number of points in input one
int npt2 = npoints(2); // number of points in input two

float distance=100000000000000; // Max distance
float newdistance=0; // float value where we store minimum distance
vector tip={0,0,0}, tsp={0,0,0}; // temp points, where we store current points

// iterate on all points on first geometry
for(; i&lt; npt1; i++){
import("P", tip, 1, i); // import point from input one to tip

// iterate on all points on second gemetry
for(j=0; j&lt;npt2; j++){
import("P", tsp, 2, j);
newdistance = min(distance, distance(tip, tsp));

if(newdistance &lt; distance){ // if we found closest distance
distance = newdistance;
pts[0] = tip;
pts[1] = tsp;
}
}
}

P = pts[ptnum]; // ptnum - current point, global variable, point index on renference points```

Edited by bareja

##### Share on other sites

Hey bareja,

MEL is boring for you?

Query of points in distance you get for free with point cloud methods. No loops whatsoever. Just nice, build-in kdtree structure. Look for pcopen() in help.

pozdro,

skk.

Edited by SYmek

##### Share on other sites

For a lot of the common cases, the AttribTransfer SOP can also be used.

##### Share on other sites

this is little OT but why, in this example, MERGE SOP does points on second line be displayed from number 10 - 11 and not just from 0 -1 ?

##### Share on other sites

Hi skk

yea indeed, i'm bored with mel, MayaAPI and Maya.

I think some of odforce topics are good challenges for noobs, like me.

Really thanks for advice and another challenge, i'll try pcopen tomorrow, i'm too tired today, to think or... whatever

Hey edward! could u upload example scene ? it will be very interesting to do that with Attribute Transfer, I'm curious

Nibbler, sorry but... i don't understand your question, r u asking how megrge sop numerates points indexes after merge?

merge sop is incrementing point index from left to right inputs, try to swap inputs. In this example it will be:

[first line: 10 points, second line: 2 points, result line: 2 points]

indexes:

[first line: 0-9, second line: 10-11, result line: 12-13]

when u connect as first input 'second line' it will be:

[second line: 2 points, first line: 10 points, result line: 2 points]

[second line: 0-1, first line: 2-11, result line: 12-13]

cheers.

Edited by bareja

##### Share on other sites

thnx for answer bareja, I wasn't so clear but this is the answer I looked for

##### Share on other sites

thank you very much, guys, i'll see the scene tomorrow - and, yes - the reason i studied this example are the 'rumors' of the powerful nodes like foreach SOP and point cloud methods - so, it would be very interesting for me to study an example with pcopen() function!

thank you very much!

##### Share on other sites
For a lot of the common cases, the AttribTransfer SOP can also be used.

Good solution Edward. I imagine than you can easily transfer a current point number from the source line to the target line with the attributeTransfer SOP, then you know in your target line which point number from your source line is the nearest.

##### Share on other sites

hi folks,

I've made it with two attribute transfer operators, one delete , please check it, is it correct ?

closest_point.hipnc.hip

Cheers

EDIT:

sorry, it doesn't work :/

Edited by bareja

##### Share on other sites

hmm i don't know how to do this using attribute transfer sop,

How do u wanna get information about closest point, from all points ? using foreach sop ? points will "snap" like on the image:

Edited by bareja

##### Share on other sites

hmm i'm studying point clouds functions for this task. please tell me where i'm wrong: (just simple examples)

```sop
test_point_cloud(string file = ""; export float f = 0.)
{
int handle = pcopen(file, "P", {0, 0, 0}, 0.9, 30);
f = pcfarthest(handle);
pcclose(handle);
}```

- everything is ok.

and

```sop
test_point_cloud(string file = ""; export float temp = 1.2; export int q = 0;)
{
int handle = pcopen(file, "P", {0, 0, 0}, 0.9, 30);
temp = pcfarthest(handle);

while(pciterate(handle))
{
pcimport(handle, "point.distance", q);
if (q &gt; 0)
{
temp = 142.87;
}
}
pcclose(handle);
}```

- everything crashes! fatal error. if comment all except if-construction everything works. importing is ok. but when manipulating imported data, crash happens or smth like that. why?

##### Share on other sites

well, I'm not sure is that what you are after?

PS2.Looking forward for a pointcloud example too

kuba

closest_point2.hipnc

##### Share on other sites
well, I'm not sure is that what you are after?

PS2.Looking forward for a pointcloud example too

kuba

hey dude! thanks for example scene, i've thought that i can do this without foreach thanks again!

mic i didn't try point cloud method yet, to write point maybe try pcexport() ? sounds like method to export point position, but like i said i didn't try, maybe skk will help

EDIT:

maybe your temp variable is reference to value stored somewhere, maybe in kdtree, and u r writing to it something, then h. crashes. and why r u trying write to temp ? in fact i don't know vex

Edited by bareja

##### Share on other sites

maybe this helps.

petz

closest_points.hipnc

##### Share on other sites

thanks! this is it!

cheers

##### Share on other sites

thank you very much, guys!

with your help i solved this example by oneself with two different vex solutions - 'like MEL' as Bareja and using point clouds.

You have already posted the working test scenes, so i won't post mine, the result is simply the same.

But, if someone want to view my vex code for studying vex coding, of course i'll post it, just tell me.

so, thank you very much once more!!

Edited by mic

## Create an account

Register a new account