Seeking your kind help on a VEX question...
I have a vector, and I'd like to find the ortho axis which is nearest to it, and whether it faces in the positive or negative direction on that axis - ie: one of six results.
Can anyone suggest a VEX solution, or point me in the right direction? (pun happily accepted : )
With thanks.
Which ortho axis is closest to this vector?
412 4 2- Mike_A
- Member
- 274 posts
- Joined: Aug. 2018
- Offline
- viklc
- Member
- 218 posts
- Joined: May 2017
- Offline
You could simply compare the distance to each ortho axis:
printf('------------ \n'); vector p = point(1, 'P', 0); addpoint(0, 0); addpoint(0, p); addprim(0, 'polyline', 0, 1); vector ort_axis = 0; float min_dist = 0; for (int i = 0; i < 6; ++i) { vector tmp = 0; tmp[i / 2] = (i % 2 * 2) - 1; float tmp_dist = distance(p, tmp); if (min_dist < tmp_dist) { min_dist = tmp_dist; ort_axis = floor(tmp * -1); } } printf('%d \n', ort_axis );
- animatrix_
- Member
- 4703 posts
- Joined: Feb. 2012
- Online
Hi,
You can use the dot product to do this:
You can use the dot product to do this:
vector v = normalize ( chv("vec") ); vector dirs [ ] = array ( {1, 0, 0}, {0, 1, 0 }, {0, 0, 1} ); float maxdv = -1; vector nearestdir = 0; for ( int i = 0; i < len ( dirs ); ++i ) { float dv = dot ( v, dirs[ i ] ); float adv = abs ( dv ); if ( adv > maxdv ) { maxdv = adv; nearestdir = dv > 0 ? dirs [ i ] : -dirs [ i ]; } } int pt = addpoint ( 0, nearestdir ); setpointattrib ( 0, "N", pt, nearestdir );
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]
youtube.com/@pragmaticvfx | patreon.com/animatrix | pragmaticvfx.gumroad.com
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]
youtube.com/@pragmaticvfx | patreon.com/animatrix | pragmaticvfx.gumroad.com
- tamte
- Member
- 8786 posts
- Joined: July 2007
- Offline
you can also try checking the longest value and that will be your axis
(I know that == with floats is questionable, but seems to work in VEX at least in this case)
(I know that == with floats is questionable, but seems to work in VEX at least in this case)
vector v = chv("vector"); float longest = max( abs(v) ); vector axis = {0,0,0}; for ( int i=0; i<3; i++){ if ( abs(v[i]) == longest ) { axis[i] = sign( v[i] ); break; } }
Edited by tamte - Nov. 24, 2024 11:21:22
Tomas Slancik
FX Supervisor
Method Studios, NY
FX Supervisor
Method Studios, NY
- Mike_A
- Member
- 274 posts
- Joined: Aug. 2018
- Offline
-
- Quick Links