This repository was archived by the owner on Nov 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.py
More file actions
98 lines (82 loc) · 2.85 KB
/
mesh.py
File metadata and controls
98 lines (82 loc) · 2.85 KB
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
import numpy as np
class Mesh():
def __init__(self, filename):
# parse the .obj file
V, T = [], []
with open(filename) as f:
for line in f.readlines():
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
V.append([float(x) for x in values[1:4]])
elif values[0] == 'f':
T.append([int(x) for x in values[1:4]])
self.V, self.T = np.array(V), np.array(T) - 1
# compute the adjacency
self.v2c = np.array([-1] * self.nverts)
self.c2c = np.array([-1] * self.ncorners)
for c in range(self.ncorners):
v = self.T[c // 3][c % 3]
self.v2c[v] = c
for c in range(self.ncorners):
v = self.T[c // 3][c % 3]
self.c2c[c] = self.v2c[v]
self.v2c[v] = c
# speed up the computations
self.opp = np.array([-1] * self.ncorners)
for c in range(self.ncorners):
c_org = self.T[c // 3][c % 3]
c_dst = self.T[c // 3][(c + 1) % 3]
cir = c
opp = -1
while True:
cand = (cir // 3) * 3 + (cir + 2) % 3
cand_org = self.T[cand // 3][cand % 3]
cand_dst = self.T[cand // 3][(cand + 1) % 3]
if (cand_org == c_dst and cand_dst == c_org):
opp = cand # we suppose manifold input
cir = self.c2c[cir]
if (cir == c): break
self.opp[c] = opp
self.boundary = np.array([False] * self.nverts)
for v in range(self.nverts):
cir = self.v2c[v]
if cir < 0: continue
while (True):
if self.opp[cir] < 0:
self.boundary[v] = True
break
cir = self.c2c[cir]
if (cir == self.v2c[v]): break
@property
def nverts(self):
return len(self.V)
@property
def ntriangles(self):
return len(self.T)
@property
def ncorners(self):
return len(self.T) * 3
def org(self, c):
return self.T[c // 3][c % 3]
def dst(self, c):
return self.T[c // 3][(c + 1) % 3]
def prev(self, c):
return (c // 3) * 3 + (c + 2) % 3
def next(self, c):
return (c // 3) * 3 + 1
def opposite(self, c):
return self.opp[c]
def on_border(self, v):
return self.boundary[v]
def __str__(self):
ret = ""
for v in self.V:
ret = ret + ("v %f %f %f\n" % (v[0], v[1], v[2]))
for t in self.T:
ret = ret + ("f %d %d %d\n" % (t[0] + 1, t[1] + 1, t[2] + 1))
return ret
def save(self, path):
with open(path, "w") as out:
out.write(self.__str__())