-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
550 lines (469 loc) · 19.3 KB
/
functions.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import os, glob
import numpy as np
import vtk
import math
import classes
# intrinsic calibration
class renderer:
@classmethod
def draw_camera(cls, vtk_renderer, camera, color_str):
t = camera.SE3[0:3, 3].reshape(3,)
R = camera.SE3[0:3, 0:3].reshape(3, 3)
wh = camera.sensor_size[0] * 0.5
hh = camera.sensor_size[1] * 0.5
focal_length = (camera.focal_lengths[0]+camera.focal_lengths[1]) / 2.
size_scaler = 20.
wh *= size_scaler
hh *= size_scaler
focal_length *= size_scaler
p0 = np.array([-wh, hh, focal_length])
p1 = np.array([wh, hh, focal_length])
p2 = np.array([wh, -hh, focal_length])
p3 = np.array([-wh, -hh, focal_length])
p4 = np.array([0, 0, 0])
p0 = R.dot(p0) + t
p1 = R.dot(p1) + t
p2 = R.dot(p2) + t
p3 = R.dot(p3) + t
p4 = R.dot(p4) + t
points = vtk.vtkPoints()
points.InsertNextPoint(p0)
points.InsertNextPoint(p1)
points.InsertNextPoint(p2)
points.InsertNextPoint(p3)
points.InsertNextPoint(p4)
# camera face1
tri1 = vtk.vtkTriangle()
tri1.GetPointIds().SetId(0, 0)
tri1.GetPointIds().SetId(1, 1)
tri1.GetPointIds().SetId(2, 2)
# camera face2
tri2 = vtk.vtkTriangle()
tri2.GetPointIds().SetId(0, 0)
tri2.GetPointIds().SetId(1, 2)
tri2.GetPointIds().SetId(2, 3)
# camera side right
tri3 = vtk.vtkTriangle()
tri3.GetPointIds().SetId(0, 0)
tri3.GetPointIds().SetId(1, 4)
tri3.GetPointIds().SetId(2, 3)
# camera side left
tri4 = vtk.vtkTriangle()
tri4.GetPointIds().SetId(0, 1)
tri4.GetPointIds().SetId(1, 4)
tri4.GetPointIds().SetId(2, 2)
# camera side top
tri5 = vtk.vtkTriangle()
tri5.GetPointIds().SetId(0, 0)
tri5.GetPointIds().SetId(1, 1)
tri5.GetPointIds().SetId(2, 4)
# camera side bottom
tri6 = vtk.vtkTriangle()
tri6.GetPointIds().SetId(0, 2)
tri6.GetPointIds().SetId(1, 3)
tri6.GetPointIds().SetId(2, 4)
triangles = vtk.vtkCellArray()
triangles.InsertNextCell(tri1)
triangles.InsertNextCell(tri2)
triangles.InsertNextCell(tri3)
triangles.InsertNextCell(tri4)
triangles.InsertNextCell(tri5)
triangles.InsertNextCell(tri6)
# setup colors (setting the name to "Colors" is nice but not necessary)
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
colors.SetName("Colors")
namedColors = vtk.vtkNamedColors()
try:
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color_str))
except AttributeError:
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color_str))
# polydata object
trianglePolyData = vtk.vtkPolyData()
trianglePolyData.SetPoints(points)
trianglePolyData.SetPolys(triangles)
trianglePolyData.GetCellData().SetScalars(colors)
# mapper
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(trianglePolyData)
else:
mapper.SetInputData(trianglePolyData)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# assign actor to the renderer
vtk_renderer.AddActor(actor)
renderer.draw_axis(vtk_renderer, R, t, 2, 500)
# draw ray in +z direction of camera
# ray_length = 3000
# r = np.array([0, 0, ray_length])
# r = R.dot(r) + t
# renderer.draw_ray(vtk_renderer, p4, r, 1, 'Blue')
return actor
@classmethod
def draw_floor(cls, vtk_renderer, w, h, color):
p0 = np.array([-w, h, 0])
p1 = np.array([w, h, 0])
p2 = np.array([w, -h, 0])
p3 = np.array([-w, -h, 0])
points = vtk.vtkPoints()
points.InsertNextPoint(p0)
points.InsertNextPoint(p1)
points.InsertNextPoint(p2)
points.InsertNextPoint(p3)
# checkboard triangle 1
tri1 = vtk.vtkTriangle()
tri1.GetPointIds().SetId(0, 0)
tri1.GetPointIds().SetId(1, 1)
tri1.GetPointIds().SetId(2, 2)
tri2 = vtk.vtkTriangle()
tri2.GetPointIds().SetId(0, 0)
tri2.GetPointIds().SetId(1, 2)
tri2.GetPointIds().SetId(2, 3)
triangles = vtk.vtkCellArray()
triangles.InsertNextCell(tri1)
triangles.InsertNextCell(tri2)
# polydata object
trianglePolyData = vtk.vtkPolyData()
trianglePolyData.SetPoints(points)
trianglePolyData.SetPolys(triangles)
# Create a vtkUnsignedCharArray container and store the colors in it
namedColors = vtk.vtkNamedColors()
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
try:
colors.InsertNextTupleValue(namedColors.GetColor3ub(color))
colors.InsertNextTupleValue(namedColors.GetColor3ub(color))
except AttributeError:
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color))
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color))
trianglePolyData.GetCellData().SetScalars(colors)
# mapper
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(trianglePolyData)
else:
mapper.SetInputData(trianglePolyData)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetPosition(0, 0, 0)
vtk_renderer.AddActor(actor)
return actor
@classmethod
def edit_ray_mapper(cls, vtk_actor, p0s, p1s, width, colors_in):
linesPolyData = vtk.vtkPolyData()
# Create a vtkCellArray container and store the lines in it
lines = vtk.vtkCellArray()
for i in range(len(p0s)):
pts = vtk.vtkPoints()
pts.InsertNextPoint(p0s[i])
pts.InsertNextPoint(p1s[i])
# Add the points to the polydata container
linesPolyData.SetPoints(pts)
# Create the line between p0 and p1
line = vtk.vtkLine()
line.GetPointIds().SetId(0, 0)
line.GetPointIds().SetId(1, 1)
lines.InsertNextCell(line)
# Add the lines to the polydata container
linesPolyData.SetLines(lines)
# Create a vtkUnsignedCharArray container and store the colors in it
namedColors = vtk.vtkNamedColors()
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
try:
for i in range(len(colors_in)):
colors.InsertNextTupleValue(namedColors.GetColor3ub(colors_in[i]))
except AttributeError:
for i in range(len(colors_in)):
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub(colors_in[i]))
linesPolyData.GetCellData().SetScalars(colors)
# Setup the visualization pipeline
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(linesPolyData)
vtk_actor.GetProperty().SetLineWidth(width)
vtk_actor.SetMapper(mapper)
@classmethod
def draw_ray(cls, vtk_renderer, p0, p1, width, color):
# Create the polydata where we will store all the geometric data
linesPolyData = vtk.vtkPolyData()
pts = vtk.vtkPoints()
pts.InsertNextPoint(p0)
pts.InsertNextPoint(p1)
# Add the points to the polydata container
linesPolyData.SetPoints(pts)
# Create the line between p0 and p1
line = vtk.vtkLine()
line.GetPointIds().SetId(0, 0)
line.GetPointIds().SetId(1, 1)
# Create a vtkCellArray container and store the lines in it
lines = vtk.vtkCellArray()
lines.InsertNextCell(line)
# Add the lines to the polydata container
linesPolyData.SetLines(lines)
# Create a vtkUnsignedCharArray container and store the colors in it
namedColors = vtk.vtkNamedColors()
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
try:
colors.InsertNextTupleValue(namedColors.GetColor3ub(color))
except AttributeError:
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub(color))
linesPolyData.GetCellData().SetScalars(colors)
# Setup the visualization pipeline
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(linesPolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetLineWidth(width)
vtk_renderer.AddActor(actor)
return actor
@classmethod
def draw_checkboard(cls, vtk_renderer, chb):
w = chb.dim[0]
h = chb.dim[1]
p0 = np.array([0, 0, 0])
p1 = np.array([-w, 0, 0])
p2 = np.array([-w, h, 0])
p3 = np.array([0, h, 0])
R = chb.SE3[0:3, 0:3].reshape(3, 3)
t = chb.SE3[0:3, 3].reshape(3, )
#
# p0 = R.dot(p0) + t
# p1 = R.dot(p1) + t
# p2 = R.dot(p2) + t
# p3 = R.dot(p3) + t
points = vtk.vtkPoints()
points.InsertNextPoint(p0)
points.InsertNextPoint(p1)
points.InsertNextPoint(p2)
points.InsertNextPoint(p3)
# checkboard triangle 1
tri1 = vtk.vtkTriangle()
tri1.GetPointIds().SetId(0, 0)
tri1.GetPointIds().SetId(1, 1)
tri1.GetPointIds().SetId(2, 2)
tri2 = vtk.vtkTriangle()
tri2.GetPointIds().SetId(0, 0)
tri2.GetPointIds().SetId(1, 2)
tri2.GetPointIds().SetId(2, 3)
triangles = vtk.vtkCellArray()
triangles.InsertNextCell(tri1)
triangles.InsertNextCell(tri2)
# polydata object
trianglePolyData = vtk.vtkPolyData()
trianglePolyData.SetPoints(points)
trianglePolyData.SetPolys(triangles)
# Create a vtkUnsignedCharArray container and store the colors in it
namedColors = vtk.vtkNamedColors()
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
try:
colors.InsertNextTupleValue(namedColors.GetColor3ub('Black'))
colors.InsertNextTupleValue(namedColors.GetColor3ub('Black'))
except AttributeError:
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub('Black'))
colors.InsertNextTypedTuple(namedColors.GetColor3ub('Black'))
trianglePolyData.GetCellData().SetScalars(colors)
# mapper
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(trianglePolyData)
else:
mapper.SetInputData(trianglePolyData)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetPosition(t[0], t[1], t[2])
r = tool.rodrgiue([0, 1, 0], np.pi/2)
axis, ang = tool.Rot2Axisangle(r)
actor.RotateWXYZ(ang, axis[0], axis[1], axis[2])
# assign actor to the renderer
vtk_renderer.AddActor(actor)
renderer.draw_axis(vtk_renderer, R, t, 2, 10)
return actor
@classmethod
def draw_axis(cls, vtk_renderer, R, t, width, length):
x = length*np.array([1, 0, 0])
y = length*np.array([0, 1, 0])
z = length*np.array([0, 0, 1])
x = R.dot(x) + t
y = R.dot(y) + t
z = R.dot(z) + t
# Create the polydata where we will store all the geometric data
linesPolyData = vtk.vtkPolyData()
# Create a vtkPoints container and store the points in it
pts = vtk.vtkPoints()
pts.InsertNextPoint(t)
pts.InsertNextPoint(x)
pts.InsertNextPoint(y)
pts.InsertNextPoint(z)
# Add the points to the polydata container
linesPolyData.SetPoints(pts)
# Create the first line (between Origin and x)
line_x = vtk.vtkLine()
line_x.GetPointIds().SetId(0, 0) # the second 0 is the index of the Origin in linesPolyData's points
line_x.GetPointIds().SetId(1, 1) # the second 1 is the index of x in linesPolyData's points
# Create the second line (between Origin and y)
line_y = vtk.vtkLine()
line_y.GetPointIds().SetId(0, 0) # the second 0 is the index of the Origin in linesPolyData's points
line_y.GetPointIds().SetId(1, 2) # 2 is the index of y in linesPolyData's points
# Create the second line (between Origin and z)
line_z = vtk.vtkLine()
line_z.GetPointIds().SetId(0, 0) # the second 0 is the index of the Origin in linesPolyData's points
line_z.GetPointIds().SetId(1, 3) # 2 is the index of z in linesPolyData's points
# Create a vtkCellArray container and store the lines in it
lines = vtk.vtkCellArray()
lines.InsertNextCell(line_x)
lines.InsertNextCell(line_y)
lines.InsertNextCell(line_z)
# Add the lines to the polydata container
linesPolyData.SetLines(lines)
namedColors = vtk.vtkNamedColors()
# Create a vtkUnsignedCharArray container and store the colors in it
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
try:
colors.InsertNextTupleValue(namedColors.GetColor3ub("Red"))
colors.InsertNextTupleValue(namedColors.GetColor3ub("Green"))
colors.InsertNextTupleValue(namedColors.GetColor3ub("Blue"))
except AttributeError:
# For compatibility with new VTK generic data arrays.
colors.InsertNextTypedTuple(namedColors.GetColor3ub("Red"))
colors.InsertNextTypedTuple(namedColors.GetColor3ub("Green"))
colors.InsertNextTypedTuple(namedColors.GetColor3ub("Blue"))
# Color the lines.
# SetScalars() automatically associates the values in the data array passed as parameter
# to the elements in the same indices of the cell data array on which it is called.
# This means the first component (red) of the colors array
# is matched with the first component of the cell array (line 0)
# and the second component (green) of the colors array
# is matched with the second component of the cell array (line 1)
linesPolyData.GetCellData().SetScalars(colors)
# Setup the visualization pipeline
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(linesPolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetLineWidth(width)
vtk_renderer.AddActor(actor)
return actor
@classmethod
def render_scene(cls, vtk_renderer, cameras):
# cameras
colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Navy', 'Purple', 'Cyan']
cam_actors = []
for camera in cameras:
if camera.is_available:
actor = cls.draw_camera(vtk_renderer, camera, colors[camera.index])
if camera.render:
actor.GetProperty().SetOpacity(1.0)
else:
actor.GetProperty().SetOpacity(0.1)
cam_actors.append(actor)
# floor
floor_w = 3500
floor_h = 2500
color = 'azure'
cls.draw_floor(vtk_renderer, floor_w, floor_h, color)
# global axis
axis_length = 1000 # 1m
cls.draw_axis(vtk_renderer, np.identity(3), [0, 0, 0], 3, axis_length)
vtk_renderer.SetBackground(1, 1, 1)
return cam_actors
@classmethod
def draw_3d_point(cls, vtk_renderer, point, radius, color):
# create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(radius)
# mapper
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(source.GetOutput())
else:
mapper.SetInputConnection(source.GetOutputPort())
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(color[0], color[1], color[2])
actor.SetPosition(point[0], point[1], point[2])
# assign actor to the renderer
vtk_renderer.AddActor(actor)
return actor
class tool:
@classmethod
def fetch_files_with_extension(cls, path, ext, order=1):
# order=1: increasing, -1: deceasing
os.chdir(path)
fetched_list = sorted(glob.glob("*." + ext))
f0 = os.path.splitext(fetched_list[0])[0]
f1 = os.path.splitext(fetched_list[1])[0]
if (order == -1 and int(f0) < int(f1)) or (order == 1 and int(f0) > int(f1)):
fetched_list.reverse()
return fetched_list
@classmethod
def rodrgiue(cls, axis, rad):
X = tool.skew_sym(axis)
R = np.identity(3) + np.sin(rad)*(X) + (1.0 - np.cos(rad))*X.dot(X)
return R.astype('float32')
@classmethod
def skew_sym(cls, axis):
x = axis[0]
y = axis[1]
z = axis[2]
return np.array([[0, -z, y], [z, 0, -x], [-y, x, 0]]).astype('float32')
@classmethod
def unskew_sym(cls, V):
v = np.array([V[2,1]-V[1, 2], V[0,2]-V[2,0], V[1,0]-V[0,1]]).astype('float32')
return v
@classmethod
def to_SE3(cls, R, t):
H = np.array([R[0,0], R[0,1], R[0,2], t[0], R[1,0], R[1,1], R[1,2], t[1], R[2,0], R[2,1], R[2,2], t[2], 0, 0, 0, 1]).reshape((4, 4))
return H.astype('float32')
@classmethod
def to_R_t(cls, H):
R = np.array([H[0,0], H[0,1], H[0,2], H[1,0], H[1,1], H[1,2], H[2,0], H[2,1], H[2,2]]).reshape((3, 3))
t = np.array([H[0,3], H[1,3], H[2,3]]).reshape((3, ))
return R.astype('float32'), t.astype('float32')
@classmethod
def inv_SE3(cls, H):
R, t = tool.to_R_t(H)
Rinv = R.T
tinv = R.T.dot(-t)
return tool.to_SE3(Rinv, tinv)
@classmethod
def R2Quat(cls, R):
qw = np.sqrt(1 + R[0,0] + R[1,1] + R[2,2]) / 2.
qx = (R[2,1] - R[1,2]) / (4 * qw)
qy = (R[0,2] - R[2,0]) / (4 * qw)
qz = (R[1,0] - R[0,1]) / (4 * qw)
return [qx, qy, qz, qw]
@classmethod
def Rot2Axisangle(cls, R):
# R is SO(3), 3x3 numpy matrix
u_skew_sym = R-R.transpose()
# IF R IS IDENTITY, u IS ZERO VECTOR
u = cls.unskew_sym(u_skew_sym)
if np.linalg.norm(u) < 0.0001:
print('[WARNING] Rot2Axisangle -> R is almost identity')
u = u / np.linalg.norm(u)
rad = math.acos((R.trace()-1.0)/2.0)
return u, rad