-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaffine.lua
148 lines (131 loc) · 3.27 KB
/
affine.lua
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
local torch = require 'torch'
local pcl = require 'pcl.PointTypes'
local affine = {}
affine.UnitX = torch.FloatTensor({1,0,0,1})
affine.UnitY = torch.FloatTensor({0,1,0,1})
affine.UnitZ = torch.FloatTensor({0,0,1,1})
function affine.identity()
return torch.eye(4):float()
end
function affine.invert(M)
return torch.inverse(M)
end
function affine.scale(x, y, z)
if type(x) == 'table' or torch.isTensor(x) then
x,y,z = x[1],x[2],x[3]
end
y = y or x
z = z or x
local S = torch.eye(4):float()
S[{1,1}] = x
S[{2,2}] = y
S[{3,3}] = z
return S
end
function affine.translate(x, y, z)
if type(x) == 'table' or torch.isTensor(x) then
x,y,z = x[1],x[2],x[3]
end
local T = torch.eye(4):float()
T[{1,4}] = x
T[{2,4}] = y
T[{3,4}] = z
return T
end
function affine.rotateEuler(x, y, z, deg)
if type(x) == 'table' or torch.isTensor(x) then
x,y,z = x[1],x[2],x[3]
deg = y
end
if deg then
x = math.rad(x)
y = math.rad(y)
z = math.rad(z)
end
local cos,sin = math.cos,math.sin
local X = torch.FloatTensor({
{ 1, 0, 0, 0 },
{ 0, cos(x), -sin(x), 0 },
{ 0, sin(x), cos(x), 0 },
{ 0, 0, 0, 1 }
})
local Y = torch.FloatTensor({
{ cos(y), 0, sin(y), 0 },
{ 0, 1, 0, 0 },
{ -sin(y), 0, cos(y), 0 },
{ 0, 0, 0, 1 }
})
local Z = torch.FloatTensor({
{ cos(z), -sin(z), 0, 0 },
{ sin(z), cos(z), 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
})
return X * Y * Z
end
function affine.rotateAxis(axis, theta, deg)
if not torch.isTensor(axis) then
axis = torch.FloatTensor(axis)
end
if deg then
theta = math.rad(theta)
end
local n = axis:norm()
if n == 0 then
error('axis must not be the null vector')
end
local u = torch.div(axis, n)
local ct, st = math.cos(theta), math.sin(theta)
local d = 1-ct
local R = torch.FloatTensor({
{ ct+u[1]*u[1]*d, u[1]*u[2]*d-u[3]*st, u[1]*u[3]*d+u[2]*st, 0 },
{ u[2]*u[1]*d+u[3]*st, ct+u[2]*u[2]*d, u[2]*u[3]*d-u[1]*st, 0 },
{ u[3]*u[1]*d-u[2]*st, u[3]*u[2]*d+u[1]*st, ct+u[3]*u[3]*d, 0 },
{ 0, 0, 0, 1 },
})
return R
end
local Transform = torch.class('pcl.affine.Transform', affine)
function Transform:__init(M)
self.M = M or affine.identity()
end
function Transform:premul(X)
if torch.isTypeOf(X, affine.Transform) then
X = X:totensor()
end
self.M:set(X * self.M)
end
function Transform:postmul(X)
if torch.isTypeOf(X, affine.Transform) then
X = X:totensor()
end
self.M:set(self.M * X)
end
function Transform:totensor()
return self.M
end
function Transform:__tostring()
local s = ''
for i=1,4 do
s = s .. string.format('%9g %9g %9g %9g\n', self.M[{i,1}], self.M[{i,2}], self.M[{i,3}], self.M[{i,4}])
end
return s
end
local TRANSFORM_NAMES = {
'scale',
'translate',
'rotateEuler',
'rotateAxis'
}
for i,n in ipairs(TRANSFORM_NAMES) do
local f = affine[n]
Transform[n] = function(self, ...)
self:postmul(f(...))
return self
end
Transform['pre' .. n] = function(self, ...)
self:premul(f(...))
return self
end
end
pcl.affine = affine