Which ortho axis is closest to this vector?

   155   2   0
User Avatar
Member
272 posts
Joined: 8月 2018
Offline
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.
User Avatar
Member
218 posts
Joined: 5月 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 );
User Avatar
Member
4697 posts
Joined: 2月 2012
Online
Hi,

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 );

Attachments:
dot.png (477.4 KB)
camtasia.gif (818.3 KB)

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
  • Quick Links