Simple Line sop in VEX - show me a better way to write it

   2231   8   1
User Avatar
Member
38 posts
Joined: May 2016
Offline
Doing a bit of vex learning, goal is to make a simple version of the Line SOP in vex. Nothing fancy just create a polyline with arbitrary step size and point number in any axis. How would you make this code better?

//add points
vector dopoints = @P+chv("gaps");
int pnum = chi("pnum");

for (int i = 0; i < pnum; i++)
{
addpoint(0, set(dopoints*i));
}

//add prim and do vertexes
int prim0 = addprim(0,"polyline");

for (int i=0; i<pnum; i++)
{
addvertex(0,prim0,i);
}
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
Hi,

Here is one way to implement Line SOP in VEX using a Detail Wrangle:

vector origin = chv("origin");
vector dir = normalize ( chv("direction") );
float curvelen = ch("length");
int ptcount = chi("points");
float u = 1.0 / ( ptcount - 1 );

int pts [ ] = { };
for ( int i = 0; i < ptcount; ++i )
{
    vector p = set ( curvelen * i * u, 0, 0 );
    matrix3 m = dihedral ( { 1, 0, 0 }, dir );
    p = p * m + origin;
    
    int pt = addpoint ( 0, p );
    append ( pts, pt );
}

addprim ( 0, "polyline", pts );



Set it to 64-bit precision if you want more precise results.

However for max performance, I would pre-generate the points using a Point Generate SOP, and create the primitive after VEX using an Add SOP.
Edited by animatrix_ - Oct. 31, 2022 08:30:29
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
38 posts
Joined: May 2016
Offline
Thanks! Lots for me to get into there. How come you don't have to create vertices to make the polyline?
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
If you pass points directly to the addprim function, Houdini creates the vertices for you automatically.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
38 posts
Joined: May 2016
Offline
animatrix_
If you pass points directly to the addprim function, Houdini creates the vertices for you automatically.

How would I implement that in the code I wrote - I just want to strip it down to the basics with just number of points and a distance between them so I can understand the grammar better. Looks like you have to create an empty array of points first then do the loop?
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
RT_SD
animatrix_
If you pass points directly to the addprim function, Houdini creates the vertices for you automatically.

How would I implement that in the code I wrote - I just want to strip it down to the basics with just number of points and a distance between them so I can understand the grammar better. Looks like you have to create an empty array of points first then do the loop?

Yes using my approach you have to gather all the points inside an array to pass to addprim.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
38 posts
Joined: May 2016
Offline
animatrix_
RT_SD
animatrix_
If you pass points directly to the addprim function, Houdini creates the vertices for you automatically.

How would I implement that in the code I wrote - I just want to strip it down to the basics with just number of points and a distance between them so I can understand the grammar better. Looks like you have to create an empty array of points first then do the loop?

Yes using my approach you have to gather all the points inside an array to pass to addprim.
OK I edited yours to get this... so append is what's putting the added points into the empty array correct? Or is there another way to do it?
int ptcount = chi("points");
float u = chf("length");

int pts [ ] = { };
for ( int i = 0; i < ptcount; ++i )
{
    vector p = set (i * u, 0, 0 );
    
    int pt = addpoint ( 0, p );
    append ( pts, pt );
}

addprim ( 0, "polyline", pts );
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
RT_SD
animatrix_
RT_SD
animatrix_
If you pass points directly to the addprim function, Houdini creates the vertices for you automatically.

How would I implement that in the code I wrote - I just want to strip it down to the basics with just number of points and a distance between them so I can understand the grammar better. Looks like you have to create an empty array of points first then do the loop?

Yes using my approach you have to gather all the points inside an array to pass to addprim.
OK I edited yours to get this... so append is what's putting the added points into the empty array correct? Or is there another way to do it?
int ptcount = chi("points");
float u = chf("length");

int pts [ ] = { };
for ( int i = 0; i < ptcount; ++i )
{
    vector p = set (i * u, 0, 0 );
    
    int pt = addpoint ( 0, p );
    append ( pts, pt );
}

addprim ( 0, "polyline", pts );

Yes. There is also a push function, but append is newer:
https://www.sidefx.com/docs/houdini/vex/functions/push.html [www.sidefx.com]
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
38 posts
Joined: May 2016
Offline
animatrix_
Yes. There is also a push function, but append is newer:
https://www.sidefx.com/docs/houdini/vex/functions/push.html [www.sidefx.com]

Thanks, I could not find any of this info in the docs or tutorials, it's really helped.
  • Quick Links