forked from scientific-python/blog.scientific-python.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbunny-1.py
More file actions
30 lines (28 loc) · 917 Bytes
/
bunny-1.py
File metadata and controls
30 lines (28 loc) · 917 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
# Data processing
V, F = [], []
with open("bunny.obj") 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":
F.append([int(x) for x in values[1:4]])
V, F = np.array(V), np.array(F) - 1
V = (V - (V.max(0) + V.min(0)) / 2) / max(V.max(0) - V.min(0))
T = V[F][..., :2]
# Rendering
fig = plt.figure(figsize=(6, 6))
ax = fig.add_axes([0, 0, 1, 1], xlim=[-1, +1], ylim=[-1, +1], aspect=1, frameon=False)
collection = PolyCollection(
T, closed=True, linewidth=0.1, facecolor="None", edgecolor="black"
)
ax.add_collection(collection)
plt.savefig("bunny-1.png", dpi=300)
plt.show()