[Numpy-discussion] Advanced selection, duplicate indices, and augmented assignment
Sean R. Lynch
seanl at literati.org
Sat Jan 6 12:38:03 CST 2007
I have an array of vertices and an array of indices that make a list of
triangles. I'd like to calculate the normals for each vertex as a
(pseudo-)average of the normals of each triangle that contains it, so I
get smooth lighting in OpenGL. I'd also like to avoid looping in Python
because the vertex positions need to be able to be updated interactively.
I tried using the following code:
def calculate_normals(self):
i = self.indices
faces = self.vertices[i].reshape((i.shape[0] / 3, 3, 3))
face_normals = cross(faces[:,1] - faces[:,0], faces[:,2] -
faces[:,0])
normals = zeros(self.vertices.shape, float32)
normals[i] += face_normals.repeat(3, axis=0)
normals /= sqrt(sum(normals*normals,
axis=1)).reshape(normals.shape[0], 1)
self.normals = normals
but this appears to only add the face normal of the last face that
contains each vertex, so I don't get averaged normals but the normals of
a single face each. A test in the interactive interpreter shows why:
>>> x = zeros((3,))
>>> x[array([0, 1, 1])] += array([1, 1, 1])
>>> x
array([ 1., 1., 0.])
If this worked the way I was hoping, the output would be [1 2 0] because
it would add to element 1 twice due to its duplication in the advanced
selection array.
Is the current behavior intentional or is it an accident of
implementation? I think the multiple operations on elements with
duplicated indices is more intuitive and it makes it possible to
"invert" the expansion due to duplicate indices, whereas dropping the
duplicate indices throws away information. Are people relying on the
current behavior with duplicate indices and augmented assignment?
I'd be happy with another way to do this that doesn't require a loop in
Python or lots of temporary arrays, as well. Does anybody know of one?
-Sean
P.S. Could somebody please update http://numpy.scipy.org/ to point to
the new list? I wasted a lot of time subscribing and posting to the
wrong list.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070106/6f822ed2/attachment.bin
More information about the Numpy-discussion
mailing list