houdini oriented bounding box

basic process

  • caculate covariance matrix.
  • caculate eigenvalue and eigenvector using convaruance (we don't use eigenvalue in this case).
  • create bounding box.
    • under eigenvector or transform positions by eigenvector create AABB then transform bouding box using reverse eigenvector.

houdini

inside houdini we can use numpy to caculate eigenvector

import numpy as np
node = hou.pwd()
geo = node.geometry()

# using pointFloatAttribValues is much faster then using point.position()
# get all positions
points = np.array(node.geometry().pointFloatAttribValues("P")).reshape((-1,3))

eigenvalues, eigenvectors = np.linalg.eig(np.cov(points,rowvar=False))
eigenvectors = np.matrix(eigenvectors)*-1
geo.addAttrib(hou.attribType.Global, "__eigenvectors", eigenvectors.A1)

example
example

hip file: oob.hip

formula and ref

numpy https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html
covariance matrix https://en.wikipedia.org/wiki/Covariance_matrix
principal component analysis https://en.wikipedia.org/wiki/Principal_component_analysis

covariance matrix: $ Cov(x_i,y_i) = E[(x_i- \mu_i)(y_j-\mu_j)] $

todo

  • basic numpy implementation
  • jacobi using vex or hdk
  • 2d shape