简体   繁体   中英

np.ndarray is indexed by np.ndarray

def compute_normal(vertices, faces):
    norm = np.zeros(vertices.shape, dtype=vertices.dtype)
    tris = vertices[faces]
    ## skip the rest of the code ##

where vertices is an np.ndarray of shape (A,3), faces is an np.ndarray of shape (B,3), and tris turns to be of shape (B,3,3). (by the way, B>A)

What is tris = vertices[faces] actually doing?

Maybe this example help you:

import numpy as np
np.random.seed(123)

vertices = np.random.rand(3, 3)            # shape -> (3, 3)
faces = np.random.randint(3, size=(4, 3))  # shape -> (4, 3)

print(vertices)
# [[0.69646919 0.28613933 0.22685145]
#  [0.55131477 0.71946897 0.42310646]
#  [0.9807642  0.68482974 0.4809319 ]]

print(faces)
# [[1 0 2]
#  [0 1 2]
#  [1 0 0]
#  [0 0 1]]

print(vertices[faces]) # shape -> (4, 3, 3)
# [[[0.55131477 0.71946897 0.42310646]   # faces==1 -> take second row of vertices
#   [0.69646919 0.28613933 0.22685145]   # faces==0 -> take first  row of vertices
#   [0.9807642  0.68482974 0.4809319 ]]  # faces==2 -> take third  row of vertices

#  [[0.69646919 0.28613933 0.22685145]   # faces==0 -> take first  row of vertices
#   [0.55131477 0.71946897 0.42310646]   # faces==1 -> take second row of vertices
#   [0.9807642  0.68482974 0.4809319 ]]  # faces==2 -> take third  row of vertices

#  [[0.55131477 0.71946897 0.42310646]   # faces==1 -> take second row of vertices
#   [0.69646919 0.28613933 0.22685145]   # faces==0 -> take first  row of vertices
#   [0.69646919 0.28613933 0.22685145]]  # faces==0 -> take first  row of vertices

#  [[0.69646919 0.28613933 0.22685145]   # faces==0 -> take first  row of vertices
#   [0.69646919 0.28613933 0.22685145]   # faces==0 -> take first  row of vertices
#   [0.55131477 0.71946897 0.42310646]]] # faces==1 -> take second row of vertices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM