-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitive.py
133 lines (111 loc) · 4.69 KB
/
primitive.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Primitive():
def __init__(self):
self.halfedge = None
self.index = -1
def __str__(self) -> str:
return str(self.index)
def __repr__(self) -> str:
return str(self)
class Halfedge(Primitive):
def __init__(self):
# note parent constructor is replaced
self.vertex = None
self.edge = None
self.face = None
# self.corner = None
self.next = None
self.twin = None
self.index = -1 # an ID between 0 and |H| - 1, where |H| is the number of halfedges in a mesh
def prev(self):
# TODO: Q2 -- complete this function
""" Return previous halfedge """
return self.next.next
def tip_vertex(self):
# TODO: Q2 -- complete this function
""" Return vertex on the tip of the halfedge """
return self.next.vertex
def serialize(self):
return (
self.index,
self.vertex.index,
self.edge.index,
self.face.index,
self.next.index,
self.twin.index,
)
class Edge(Primitive):
""" initialization: assign halfedge and index (see Primitive) """
def two_vertices(self):
# TODO: Q2 -- complete this function
"""return the two incident vertices of the edge
note that the incident vertices are ambiguous to ordering
"""
return (self.halfedge.vertex, self.halfedge.tip_vertex())
class Face(Primitive):
""" initialization: assign halfedge and index (see Primitive) """
def adjacentHalfedges(self):
# TODO: Q2 -- complete this function
# Return ONLY the halfedges for which this face is assigned to. Be careful not to return the twins!
""" Return iterator of adjacent halfedges """
return [self.halfedge, self.halfedge.next, self.halfedge.prev()]
# Map is overpowered
def adjacentVertices(self):
# TODO: Q2 -- complete this function
# Return all the vertices which are contained in this face
""" Return iterator of adjacent vertices """
def getVertex(he):
return he.vertex
return list(map(getVertex, self.adjacentHalfedges()))
def adjacentEdges(self):
# TODO: Q2 -- complete this function
# Return all the edges which make up this face
""" Return iterator of adjacent edges """
def getEdge(he):
return he.edge
return list(map(getEdge, self.adjacentHalfedges()))
def adjacentFaces(self):
# TODO: Q2 -- complete this function
# Return all the faces which share an edge with this face
""" Return iterator of adjacent faces """
def getAdjacentFace(he):
return he.twin.face
return list(map(getAdjacentFace, self.adjacentHalfedges()))
class Vertex(Primitive):
""" initialization: assign halfedge and index (see Primitive) """
def degree(self):
# TODO: Q2 -- complete this function
""" Return vertex degree: # of incident edges """
return len(self.adjacentHalfedges())
def isIsolated(self) -> bool:
return self.halfedge is None
def adjacentHalfedges(self):
# TODO: Q2 -- complete this function
# Return ONLY the halfedges for which this vertex is assigned to. Be careful not to return the twins!
""" Return iterator of adjacent halfedges """
halfEdges = [self.halfedge] # Manifold assumption guarantees each vertex has a halfedge
curHalfEdge = self.halfedge.twin.next # Manifold assumption guarantees .twin.next is defined
while curHalfEdge != self.halfedge:
halfEdges.append(curHalfEdge)
curHalfEdge = curHalfEdge.twin.next # Manifold assumption guarantees .twin.next is defined
return halfEdges
def adjacentVertices(self):
# TODO: Q2 -- complete this function
# Return all the vertices which are connected to this vertex by an edge
""" Return iterator of adjacent vertices """
def getAdjacentVertex(he):
return he.tip_vertex()
return list(map(getAdjacentVertex, self.adjacentHalfedges()))
def adjacentEdges(self):
# TODO: Q2 -- complete this function
# Return all the edges which this vertex is contained in
""" Return iterator of adjacent edges """
def getEdge(he):
return he.edge
return list(map(getEdge, self.adjacentHalfedges()))
def adjacentFaces(self):
# TODO: Q2 -- complete this function
# Return all the faces which this vertex is contained in
""" Return iterator of adjacent faces """
def getFace(he):
return he.face
return list(map(getFace, self.adjacentHalfedges()))