-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTriSoupMesh.py
65 lines (51 loc) · 2.29 KB
/
TriSoupMesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 2 14:27:39 2018
@author: lukemcculloch
"""
import numpy as np
from Utilities import normalize, norm
# A "triangle soup" mesh, with an array of vertices and list of face indices
# Generally, this class exists only for interfacing with external components, such as IO and visualization.
# We encourage all mesh computation to be performed on a halfedge mesh.
class TriSoupMesh(object):
### Construct a new mesh froms a vert array and a tri list
def __init__(self, verts, tris,
faceAttr=dict(), vertAttr=dict()):
# A Nx3 list of vertex positions
self.verts = np.array(verts)
self.tris = np.array(tris, dtype=np.uint32)
# A Mx3 list of tuples, where each gives an (i,j,k) CCW triangular face
self.nVerts = len(verts)
self.nTris = len(tris)
# Dictionaries to hold various data on the faces and vertices
# (as indexed lists): vertAttr['value'] = [val1, val2, val3...]
self.faceAttr = faceAttr
self.vertAttr = vertAttr
# Numpy-ify all the attr data
# TODO really need some kind of more general solution here... this
# will fail on lots of things
for key in self.faceAttr:
self.faceAttr[key] = np.array(self.faceAttr[key])
for key in self.vertAttr:
self.vertAttr[key] = np.array(self.vertAttr[key])
# Compute face and vertex normals,
# using numpy operations for efficiency
# This will generally be much faster
# than computing normals on the halfedge
# mesh.
# 'useExisting' will cause this method
# to exit immediately if there is already
# a 'normal' attribute defined on the vertices
def computeNormals(self, useExisting=True):
if useExisting and 'normal' in self.vertAttr:
#print("Skipping normal computation, normals are already defined")
return
# Not implemented.
# Students SHOULD NOT touch this version of the function,
# they should implement it in HalfEdgeMesh.py.
# This alternate implementations
# exists for efficiency reasons
# in certain IO scenarios.
self.vertAttr['normal'] = np.zeros( self.verts.shape, dtype=self.verts.dtype)