-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_kernel.lua
76 lines (60 loc) · 2.07 KB
/
filter_kernel.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
local maf = require("lib.maf")
local math_helper = require("lib.math_helper")
local lume = require("lib.lume")
local vector_sequence = require("vector_sequence")
local filter_kernel = {
_class = "filter_kernel",
fallback = function(self)
self.list = lume.clone(self._original_list)
self._center = self._original_center
end,
set_center = function(self, center)
center = center or {}
if not center.x or not center.y then return end
self._center = center
end,
get_center = function(self) return self._center end,
displace = function(self, bias)
bias = bias or {}
if not bias.x or not bias.y then return end
self:displace_center(bias)
for index, component in pairs (self.list) do
self.list[index] = component + bias
end
end,
displace_center = function(self, bias)
bias = bias or {}
if not bias.x or not bias.y then return end
self:set_center(self:get_center() + bias)
end,
displace_to_point = function(self, point)
point = point or {}
if not point.x or not point.y then return end
local center = self:get_center()
self:displace(point - center)
end,
-- угол в радианах
rotate = function(self, angle)
local center = self:get_center()
for index, component in pairs (self.list) do
local x = center.x + (component.x-center.x)*math.cos(angle) - (component.y-center.y)*math.sin(angle)
local y = center.y + (component.y-center.y)*math.cos(angle) + (component.x-center.x)*math.sin(angle)
self.list[index] = maf.vector(x,y)
end
end,
}
setmetatable(filter_kernel,{
__index = vector_sequence,
__call = function(self, list, center)
local object = {
list = {}
}
setmetatable(object, { __index = filter_kernel })
object:push(list or {})
object._original_list = lume.clone(object.list)
object:set_center(center)
object._original_center = center
return object
end
})
return filter_kernel