-
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy path__init__.py
More file actions
202 lines (168 loc) · 6.47 KB
/
__init__.py
File metadata and controls
202 lines (168 loc) · 6.47 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import os
from glumpy.ext.six.moves.urllib import request
import numpy as np
try:
from PIL import Image
except:
Image = None
try:
from urllib.parse import urljoin # Python 3
except ImportError:
from urlparse import urljoin # Python 2
from glumpy import gloo
from glumpy.log import log
def _fetch_file(filename):
"""
Fetch a font file from a remote data server
Available servers:
* https://github.com/glumpy/glumpy-font/raw/master/Fonts
* https://github.com/glumpy/glumpy-data/raw/master/Data
"""
local_directory = os.path.dirname(__file__) or '.'
local_file = os.path.join(local_directory, filename)
if os.path.isfile(local_file):
return local_file
extension = os.path.basename(filename).split('.')[-1]
# Font server
if extension in ['ttf', 'otf']:
server = "https://github.com/glumpy/glumpy-font/raw/master/Fonts/"
# Data server
else:
server = "https://github.com/glumpy/glumpy-data/raw/master/Data/"
filename = os.path.basename(filename)
remote = urljoin(server, filename)
# Build url request
log.info('Requesting "%s" from remote server' % filename)
try:
response = request.urlopen(remote)
except:
log.warning('Data not available on remote server')
return None
# Fetch symlink data (font location)
symlink = response.read().decode()
remote = urljoin(server, symlink)
response = request.urlopen(remote)
# Fetch data
size = response.headers['Content-Length'].strip()
log.info('Fetching data (%s bytes) to "%s"' % (size, local_file))
with open(local_file, 'wb') as fp:
fp.write(response.read())
return local_file
def objload(filename, rescale=True):
""" """
V = [] # vertex
T = [] # texcoords
N = [] # normals
F = [] # face indices
F_V = [] # face indices
F_T = [] # face indices
F_N = [] # face indices
for lineno,line in enumerate(open(filename)):
if line[0] == '#':
continue
values = line.strip().split(' ')
code = values[0]
values = values[1:]
# vertex (v)
if code == 'v':
V.append([float(x) for x in values])
# tex-coord (vt)
elif code == 'vt' :
T.append([float(x) for x in values])
# normal (n)
elif code == 'vn' :
N.append([float(x) for x in values])
# face (f)
elif code == 'f' :
if len(values) != 3:
raise ValueError('not a triangle at line' % lineno)
for v in values:
for j,index in enumerate(v.split('/')):
if len(index):
if j==0: F_V.append(int(index)-1)
elif j==1: F_T.append(int(index)-1)
elif j==2: F_N.append(int(index)-1)
# Building the vertices
V = np.array(V)
F_V = np.array(F_V)
vtype = [('position', np.float32, 3)]
if len(T):
T = np.array(T)
F_T = np.array(F_T)
vtype.append(('texcoord', np.float32, 2))
if len(N):
N = np.array(N)
F_N = np.array(F_N)
vtype.append(('normal', np.float32, 3))
vertices = np.empty(len(F_V),vtype)
vertices["position"] = V[F_V,:3]
if len(T):
vertices["texcoord"] = T[F_T,:2]
if len(N):
vertices["normal"] = N[F_N]
vertices = vertices.view(gloo.VertexBuffer)
if rescale:
# Centering and scaling to fit the unit box
xmin,xmax = vertices["position"][:,0].min(), vertices["position"][:,0].max()
ymin,ymax = vertices["position"][:,1].min(), vertices["position"][:,1].max()
zmin,zmax = vertices["position"][:,2].min(), vertices["position"][:,2].max()
vertices["position"][:,0] -= (xmax+xmin)/2.0
vertices["position"][:,1] -= (ymax+ymin)/2.0
vertices["position"][:,2] -= (zmax+zmin)/2.0
scale = max(max(xmax-xmin,ymax-ymin), zmax-zmin) / 2.0
vertices["position"] /= scale
# xmin,xmax = vertices["position"][:,0].min(), vertices["position"][:,0].max()
# ymin,ymax = vertices["position"][:,1].min(), vertices["position"][:,1].max()
# zmin,zmax = vertices["position"][:,2].min(), vertices["position"][:,2].max()
# print("xmin: %g, xmax: %g" % (xmin,xmax))
# print("ymin: %g, xmax: %g" % (ymin,ymax))
# print("zmin: %g, zmax: %g" % (zmin,zmax))
itype = np.uint32
indices = np.arange(len(vertices), dtype=np.uint32)
indices = indices.view(gloo.IndexBuffer)
return vertices, indices
def checkerboard(grid_num=8, grid_size=32):
row_even = grid_num // 2 * [0, 1]
row_odd = grid_num // 2 * [1, 0]
Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)
def get(name, *args, **kwargs): #, depth=0):
""" Retrieve data content from a name """
if name == "checkerboard":
return checkerboard(8,16)
filename = _fetch_file(name)
return load(filename, *args, **kwargs)
def load(filename, *args, **kwargs):
""" Load data content from a filename """
extension = os.path.basename(filename).split('.')[-1]
if extension == 'npy':
return np.load(filename, *args, **kwargs)
elif extension in ['ttf', 'otf']:
if filename is not None:
return filename
if depth == 0:
log.warning("Falling back to default font")
return get("SourceSansPro-Regular.otf", 1)
else:
log.critical("Default font not available")
raise RuntimeError
elif extension == 'obj':
return objload(filename, *args, **kwargs)
elif extension in ['svg', 'json', 'csv', 'tsv']:
return filename
elif extension in ('png', 'jpg', 'jpeg', 'tif', 'tiff', 'tga'):
if Image is not None:
if filename is not None:
return np.array(Image.open(filename))
log.warning("File not found")
return checkerboard(16,32)
else:
log.warning("PIL/Pillow not installed, cannot load image")
return checkerboard(16,32)
log.warning("Data not found (%s)" % name)
raise RuntimeError
return None