Python / Numpy: Triangle mask from point mask -
i'm working triangulated mesh consisting of points 3 x n , triangles specified point indices 3 x m. can plot e.g. using mlab
mesh = mlab.triangular_mesh(p[0,:],p[1,:],p[2,:],t.t
i generating mask masking points out of bounds or nan
, have mask size of n. want mask triangles have masked point. solutions far:
1: use mask turn masked points nan
, e.g.
p[mask] = nan
mlab
still shows nan
(i need include threshold filter...) , don't want mess data
2: generating triangle mask, started this
def trianglemask(triangles, pointmask): maskedtris = np.zeros((triangles.shape[1]), dtype=np.bool) maskedidx = np.nonzero(pointmask)[0] i,t in enumerate(triangles.t): if (i%5000) == 0: print('working it.:', i) p in t: if p in maskedidx: maskedtris[i] = true break return maskedtris
this works, not fast. , in case, n = 250.000 , m = 500.000, "not fast" quite problem.
i know there's mask keyword in mlab
, cant work. masking points in triangular_mesh call yields , error since t refers indices larger size of p.
so have points
array of shape (3, n)
, triangles
array of shape (3, m)
, point_mask
boolean array of shape (n,)
, , create triangle_mask
of shape (m,)
holding true
@ position j
if of indices in triangles[:, j]
corresponds true
in point_mask
. can little bit of fancy indexing:
triangle_mask = np.any(point_mask[triangles], axis=0)
to understand what's going on, point_mask[triangles]
creates boolean array of shape (3, m)
, value @ position (i, j)
being point_mask[triangles[i, j]]
.
Comments
Post a Comment