-
Notifications
You must be signed in to change notification settings - Fork 22
/
mesh_viewer.py
292 lines (269 loc) · 9 KB
/
mesh_viewer.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""Visualize mesh files. Use the `--help` argument for information on how to
use the script."""
import argparse
from functools import reduce
from os import path
from tempfile import gettempdir
import numpy as np
from pyrr import Matrix44
from .. import Mesh, Lines, Scene, TexturedMesh
from ..behaviours import SceneInit
from ..behaviours.keyboard import SnapshotOnKey
from ..behaviours.misc import LightToCamera
from ..utils import save_frame
try:
from ..window import show
no_gui = False
except ImportError:
no_gui = True
# The tab20 colormap from matplotlib
tab20 = [
(0.12156862745098039, 0.4666666666666667, 0.7058823529411765 ),
(0.6823529411764706, 0.7803921568627451, 0.9098039215686274 ),
(1.0, 0.4980392156862745, 0.054901960784313725),
(1.0, 0.7333333333333333, 0.47058823529411764 ),
(0.17254901960784313, 0.6274509803921569, 0.17254901960784313 ),
(0.596078431372549, 0.8745098039215686, 0.5411764705882353 ),
(0.8392156862745098, 0.15294117647058825, 0.1568627450980392 ),
(1.0, 0.596078431372549, 0.5882352941176471 ),
(0.5803921568627451, 0.403921568627451, 0.7411764705882353 ),
(0.7725490196078432, 0.6901960784313725, 0.8352941176470589 ),
(0.5490196078431373, 0.33725490196078434, 0.29411764705882354 ),
(0.7686274509803922, 0.611764705882353, 0.5803921568627451 ),
(0.8901960784313725, 0.4666666666666667, 0.7607843137254902 ),
(0.9686274509803922, 0.7137254901960784, 0.8235294117647058 ),
(0.4980392156862745, 0.4980392156862745, 0.4980392156862745 ),
(0.7803921568627451, 0.7803921568627451, 0.7803921568627451 ),
(0.7372549019607844, 0.7411764705882353, 0.13333333333333333 ),
(0.8588235294117647, 0.8588235294117647, 0.5529411764705883 ),
(0.09019607843137255, 0.7450980392156863, 0.8117647058823529 ),
(0.6196078431372549, 0.8549019607843137, 0.8980392156862745 )
]
def list_of(f):
def inner(x):
try:
return [f(xi) for xi in x.split(";")]
except ValueError as e:
raise ValueError("Error when parsing the list of elements") from e
return inner
def or_type(f1, f2):
def inner(x):
try:
return f1(x)
except ValueError:
return f2(x)
return inner
def int_tuple(n):
def inner(x):
t = tuple(int(xi) for xi in x.split(","))
if len(t) != n:
raise ValueError("Expected a {}-tuple".format(n))
return t
return inner
def f_tuple(n):
def inner(x):
t = tuple(float(xi) for xi in x.split(","))
if len(t) != n:
raise ValueError("Expected a {}-tuple".format(n))
return t
return inner
def scene_init(args):
def inner(scene):
scene.camera_position = args.camera_position
scene.camera_target = args.camera_target
scene.up_vector = args.up
scene.light = args.light or scene.camera_position
if args.depth:
H, W = args.orthographic_bounds
scene.camera_matrix = Matrix44.orthogonal_projection(
left=-W/2, right=W/2,
bottom=H/2, top=-H/2,
near=args.orthographic_near,
far=args.orthographic_far
)
if args.show_axes:
scene.add(Lines.axes())
return inner
def main(argv=None):
parser = argparse.ArgumentParser(
description="Visualize meshes with simple_3dviz"
)
parser.add_argument(
"file",
default="The mesh to be viewed",
nargs="+"
)
parser.add_argument(
"--size",
type=int_tuple(2),
default=(512, 512),
help="The size of the window"
)
parser.add_argument(
"--background", "-b",
type=or_type(f_tuple(4), f_tuple(3)),
default=(0.7, 0.7, 0.7, 1),
help="The rgba background color"
)
parser.add_argument(
"--camera_position", "-c",
type=f_tuple(3),
default=(-2, -2, -2),
help="The position of the camera"
)
parser.add_argument(
"--camera_target", "-t",
type=f_tuple(3),
default=(0, 0, 0),
help="The target of the camera"
)
parser.add_argument(
"--up",
type=f_tuple(3),
default=(0, 0, 1),
help="The up vector"
)
parser.add_argument(
"--light",
type=f_tuple(3),
default=None
)
parser.add_argument(
"--color",
type=list_of(or_type(f_tuple(4), f_tuple(3))),
default=[(0.3, 0.3, 0.3)],
help="Choose a color for the mesh"
)
parser.add_argument(
"--use_tab20",
action="store_true",
help="Use matplotlib's tab20 color map"
)
parser.add_argument(
"--manual",
action="store_false",
dest="auto",
help="Auto determine the camera position and target"
)
parser.add_argument(
"--save_frame",
default=path.join(gettempdir(), "frame_{:03d}.png"),
help="The location to save the snapshot frame"
)
parser.add_argument(
"--direct_render",
help="If provided render to this file and exit"
)
parser.add_argument(
"--with_texture",
action="store_true",
help="Load mesh with texture"
)
parser.add_argument(
"--depth",
action="store_true",
help="Use an orthographic camera and render the depth of the scene"
)
parser.add_argument(
"--orthographic_bounds",
type=f_tuple(2),
help=("When an orthographic projection is used, these define the "
"width and height of the image plane in the world.")
)
parser.add_argument(
"--orthographic_near",
type=float,
default=0.1,
help="Set nearest visible point to the camera"
)
parser.add_argument(
"--orthographic_far",
type=float,
default=10,
help="Set farthest visible point to the camera"
)
parser.add_argument(
"--show_axes",
action="store_true",
help="Draw the axes at (0, 0, 0)"
)
args = parser.parse_args(argv)
MeshClass = TexturedMesh if args.with_texture else Mesh
colors = args.color*len(args.file) if len(args.color) == 1 else args.color
if args.use_tab20:
colors = [tab20[i % 20] for i in range(len(args.file))]
meshes = [
MeshClass.from_file(f, color=c)
for f, c in zip(args.file, colors)
]
# If manual is not set then move the camera far enough so that it can see
# the whole scene
if args.auto:
bbox_min = reduce(
np.minimum,
(m.bbox[0] for m in meshes),
meshes[0].bbox[0]
)
bbox_max = reduce(
np.maximum,
(m.bbox[1] for m in meshes),
meshes[0].bbox[1]
)
bbox = [bbox_min, bbox_max]
center = (bbox[1]-bbox[0])/2 + bbox[0]
args.camera_target = center
args.camera_position = center + (bbox[1]-center)*2
# In case the camera is too far, then scale the objects so that the
# camera distance is not very large.
# NOTE: This probably only works with a single model
D = np.sqrt(np.sum((args.camera_position - args.camera_target)**2))
if D > 100:
s = 100. / D
args.camera_target *= s
args.camera_position *= s
for m in meshes:
m.scale(s)
# Fill the orthographic_bounds if they are not given by computing the scene
# bounding box and setting the camera plane to be a square with a side
# equal to the diagonal of the bbox of the scene.
if args.orthographic_bounds is None:
bbox_min = reduce(
np.minimum,
(m.bbox[0] for m in meshes),
meshes[0].bbox[0]
)
bbox_max = reduce(
np.maximum,
(m.bbox[1] for m in meshes),
meshes[0].bbox[1]
)
diagonal = np.sqrt(((bbox_max - bbox_min)**2).sum())
args.orthographic_bounds = (diagonal, diagonal)
# Set the meshes to render in depth mode if args.depth is set
if args.depth:
for m in meshes:
if hasattr(m, "mode"):
m.mode = "depth"
behaviours = [
SnapshotOnKey(path=args.save_frame, keys={"<ctrl>", "S"}),
]
if args.light is None:
behaviours.append(LightToCamera())
if args.direct_render:
scene = Scene(size=args.size, background=args.background)
for m in meshes:
m.sort_triangles(args.camera_position)
scene.add(m)
scene_init(args)(scene)
scene.render()
save_frame(args.direct_render, scene.frame)
else:
show(
meshes,
size=args.size, background=args.background, title="Mesh Viewer",
camera_position=args.camera_position,
camera_target=args.camera_target,
up_vector=args.up,
light=args.light,
behaviours=[SceneInit(scene_init(args))] + behaviours
)