Bounding Box Orientation on Arbitrary Point Clouds with PCA
The most straight forward utility tool you can build using PCA is an automatic bbox generator. Even though the bound SOP can do the exact same thing, it’s still a nice exercise that helps you wrap your head around what principal components are and how you can use them.
I recommend reading the article on Principal Component Analysis first!
This works because the first PCA component gives you the direction, where the most variance occurs in your data. You could think of this as the longest distance through your point cloud. The components are ordered by importance and a perpendicular to each other. So the second one is the direction that has the second most variance and so on.
If you visualize those, you get something that already looks like a transform.
The first one (blue) is essentially our Z vector, the second one our X and we can just construct Y using the cross product.
- The first thing we need to do is center the incoming points on the origin using a
matchsize SOP
. - We stash the transform for later use and call it offset.
- Then we run PCA on it and compute 3 components (2 are enough but 3 already looks like the axis we will create).
- Make sure to set points per sample to 1.
- We can then split point 0 out (this is our origin) and construct the transform matrix on it
// point wrangle “build_matrix”
|
|
- we can then invert that matrix and lock the point cloud to the world space axis in the origin
// point wrangle “invert_tranform”
|
|
- In the end we just need to fit a box around it and move everything back to the original position
// point wrangle “transform_back”
|
|
Download: File
An inherent problem with this approach is that the axis flip a lot. The same thing happens in the bound SOP
and other implementations. You could counter that by solving the orient forward through time and comparing the orientation to the frame before and flip it back when a big jump in orientation happens.
sources / further reading: