I am trying to calculate the axes of largest variances of an object's points. As you might know, the eigenvectors of the covariance matrix of the points should yield the result.
For a cube (which is made out of 8 evenly spread points) I would therefore expect to get the three axes of the cube. However, this is only the case when I did not rotate the cube, but when I do, my method yields axes that are not correct.
Here is some code I use to calculate the axes:
import bpy import numpy from numpy import linalg as LA vertices = bpy.context.active_object.data.vertices matrix_world = bpy.context.active_object.matrix_world verticesArray = numpy.zeros((3,len(vertices))) counter = 0 for v in vertices: world_coord = matrix_world * v.co verticesArray[0,counter]= world_coord verticesArray[1,counter] = world_coord verticesArray[2,counter] = world_coord counter = counter + 1 convMat = numpy.cov(verticesArray) eigenvalues, eigenvectors = LA.eig(convMat) # the eigenvectors determine the axes I want
As you see, I firstly transform all vertices to world coordinates. I guess somewhere there has to be an error that does not account for the rotation or something... Do you have an idea what I am missing?