Orient a bounding box to look-at a target?

   2111   11   0
User Avatar
Member
13 posts
Joined: 5月 2014
Offline
All...

I'm trying to get the bounds of a given object, then orient that bounding box so that the +Z normal looks at a target, but still properly bounds the object.

I can get the bounds to look at (or away from) a target with a Copy To Points node, feeding a point into port0 and a target into port1 of an attribute wrangle with the following VEX code:

vector @N;
vector source = point (0, "P", @ptnum);
vector target = point (1, "P", @ptnum);
@N = normalize(source-target);
v@up = set(0,1,0);

...but of course that invalidates the bounds.

Any tips on how to get the bounds of an object, leverage the code above to look-at a target and maintain proper bounds, all in a single attribute wrangle? I can't figure it out. Neither can ChatGPT.

I'll attach a GIF and a HIP.

Thank you in advance.


Image Not Found
Edited by BrandonRiza - 2023年4月13日 11:19:37

Attachments:
BoundsThatPointTowardsOrigin.gif (69.0 KB)
BoundingBoxIssue.hip (260.9 KB)

User Avatar
Member
7737 posts
Joined: 9月 2011
Online
Get the lookat transform and apply the inverse transform so the bound happens in the space of the lookat. Then apply the bounds and forward transform.
User Avatar
Member
4495 posts
Joined: 2月 2012
Offline
Hi,

Doing all of this inside a single Wrangle node would be very inefficient. So I would recommend to do it in multiple steps like this:





transform_geo (Point Wrangle):

vector p0 = point ( 1, "P", 0 );
vector p1 = point ( 1, "P", 1 );
vector dir = normalize ( p1 - p0 );

vector x = { 1, 0, 0 };
float angle = radians ( ch("angle") );
matrix3 m = dihedral ( dir, x );
rotate ( m, angle, x );
setdetailattrib ( 0, "M", m );

@P *= m;

reverse_xform (Point Wrangle):

matrix3 m = detail ( 1, "M" );
@P *= invert ( m );

Just using a direction, the orientation of the bounding box along the direction will be arbitrary so I added additional rotation parameter to control this. If you want fixed orientation along the direction, then you have to define an up vector instead. If you use the normalized dir vector and the up vector, you can use the maketransform function to create a new transform matrix instead of using the dihedral function.
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 | animatrix2k7.gumroad.com
User Avatar
Member
13 posts
Joined: 5月 2014
Offline
Hi, animatrix...

This is an interesting solution and i've rolled it into my system successfully (thanks!), but i'm unclear on how to implement the up vector. I do indeed want a fixed orientation on each bounds (this is running through on a per-frame basis and creating bounds for many objects.) I've tried adding v@up = set(0,1,0); in various places to no avail.
How would you accomplish this, if you were to remove your float angle = radians ( ch("angle") ); functionality?

End result of the total bounding boxes needs to (essentially) look like the attached image.

jsmack ... thanks for your input; i'm attempting this as well.

Attachments:
OrientedBounds.png (311.1 KB)

User Avatar
Member
4495 posts
Joined: 2月 2012
Offline
For transform_geo wrangle node, you can do something like this:

vector up = normalize ( chv("up_vector") );

vector p0 = point ( 1, "P", 0 );
vector p1 = point ( 1, "P", 1 );
vector dir = normalize ( p1 - p0 );
vector cdir = normalize ( cross ( up, dir ) );
up = cross ( up, cdir );
matrix3 m = maketransform ( dir, up );

setdetailattrib ( 0, "M", m );

@P *= m;
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 | animatrix2k7.gumroad.com
User Avatar
Member
13 posts
Joined: 5月 2014
Offline
Here's a comparison of the end result. No up vector defined (first option) vs defining the up vector (second option).

I'll keep digging; thanks for the tips.

Edited by BrandonRiza - 2023年4月13日 15:58:34

Attachments:
NoUpVectorVsUpVector.jpg (648.1 KB)

User Avatar
Member
4495 posts
Joined: 2月 2012
Offline
Oh yes depending on your setup, you can do this instead:

vector up = normalize ( chv("up_vector") );

vector p0 = point ( 1, "P", 0 );
vector p1 = point ( 1, "P", 1 );
vector dir = normalize ( p1 - p0 );
vector cdir = normalize ( cross ( up, dir ) );
dir = normalize ( cross ( cdir, up ) );
matrix3 m = maketransform ( dir, up );

setdetailattrib ( 0, "M", m );

@P *= m;
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 | animatrix2k7.gumroad.com
User Avatar
Member
8525 posts
Joined: 7月 2007
Offline
maketransform doesn't need othonormal axes, just providing normalized is enough to get stable transform
so this should be enough without modifying the original dir or up:
matrix3 m = maketransform ( normalize(dir), normalize(up) );
m's Z axis will point directly in the dir direction
and will use up as up vector for it's Y axis
Edited by tamte - 2023年4月14日 01:22:14
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
13 posts
Joined: 5月 2014
Offline
This definitely fixes the up direction issue, but the described (below) iterations of this code seem to break the look-at:

vector up = set(0,1,0);


vector source = point ( 1, "P", 0 );
vector target = point ( 1, "P", 1 );
vector dir = normalize ( source - target );


vector cdir = normalize ( cross ( up, dir ) );
dir = normalize ( cross ( cdir, up ) );

matrix3 m = maketransform ( dir, up );
//matrix3 m = maketransform ( normalize(dir), normalize(up) );

setdetailattrib ( 0, "M", m );
@P *= m;

Commenting out the "vector cdir" and "dir = normalize" lines yields the same results as leaving them in.
Swapping the commenting status on the matrix lines also yields the same results.

GIF for visual reference.
What am i missing?

Attachments:
Comparison.gif (581.4 KB)

User Avatar
Member
8525 posts
Joined: 7月 2007
Offline
can you post simple hip file? since this wrangle is clearly not what's doing the whole thing

I'd imagine that you are using this for the initial geo inverse alignment before computing bbox and inverse transforming it, in which case I'd expect this snippet to look like this (assuming the dir points from the object centroid to the lookat target)

vector up = set(0,1,0);
vector source = point ( 1, "P", 0 );
vector target = point ( 1, "P", 1 );
vector dir = normalize ( source - target );

matrix3 m = maketransform ( normalize(dir), normalize(up) );
m = invert (m);

setdetailattrib ( 0, "M", m );
@P *= m;
Edited by tamte - 2023年4月14日 16:34:02
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
13 posts
Joined: 5月 2014
Offline
Hi Tomas.

I was missing the "m = invert (m);"
Your edited code works. Thank you! And thank you too, animatrix.

Attached is a simple hip of part of the larger setup i'm working on.
If you open the file and scrub to frame 100, you'll see it gathers up 100 bounding boxes, all looking at the target defined (a point) and still properly bounding the underlying geo (in this sample, randomly-rotated pig heads).
From here i'm proceeding onto the rest of the system, but this was what i was getting hung up on.

Attachments:
BoundingBoxIssueSolved.hip (384.1 KB)

User Avatar
Member
4495 posts
Joined: 2月 2012
Offline
BrandonRiza
Hi Tomas.

I was missing the "m = invert (m);"
Your edited code works. Thank you! And thank you too, animatrix.

Attached is a simple hip of part of the larger setup i'm working on.
If you open the file and scrub to frame 100, you'll see it gathers up 100 bounding boxes, all looking at the target defined (a point) and still properly bounding the underlying geo (in this sample, randomly-rotated pig heads).
From here i'm proceeding onto the rest of the system, but this was what i was getting hung up on.

As you have noticed you were doing double inversion, i.e. storing the inverted matrix and then using the inverted version of that in the end.
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 | animatrix2k7.gumroad.com
  • Quick Links