-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpb_arrutil.lua
More file actions
executable file
·159 lines (124 loc) · 4.77 KB
/
pb_arrutil.lua
File metadata and controls
executable file
·159 lines (124 loc) · 4.77 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
return {init=function(box,module,api,share,api_init,load_flags)
local function create_multilayer_list(n,tbl)
tbl = tbl or {}
if n == 0 then return tbl end
return setmetatable(tbl, {__index = function(t, k)
local new = create_multilayer_list(n-1)
--if k == nil then error("Table index is nil",2) end
t[k] = new
return new
end})
end
local function metatable_remap(tbl,mappings)
local remapped_list = setmetatable({},{__index=function(self,key)
local remapped_val = mappings[key]
if remapped_val then
return rawget(tbl,remapped_val)
else
return rawget(tbl,key)
end
end})
return remapped_list
end
local function indexer_2d_remap_yx(tbl,key,width)
local y_index = key/(width+1)
y_index = (y_index - y_index%1) + 1
local x_index = (key-1)%width+1
return tbl[y_index][x_index]
end
local function indexer_2d_remap_pnglua(tbl,key,width,height)
local y_index = key/(width+1)
y_index = (y_index - y_index%1) + 1
local x_index = (key-1)%width+1
return tbl[y_index].pixels[x_index]
end
local function metatable_value_remap(tbl,remapping_func)
local remapped_list = setmetatable({},{
__index = function(self,key)
local value = tbl[key]
if value then
return remapping_func(value,key,self)
end
end,
__len = function() return #tbl end
})
return remapped_list
end
local function metatable_value_remap_memoized(tbl,remapping_func)
local remapped_list = setmetatable({},{
__index = function(self,key)
if rawget(self,key) ~= nil then
return rawget(self,key)
end
local value = tbl[key]
if value then
local remapped_value = remapping_func(value,key,self)
rawset(self,key,remapped_value)
return remapped_value
end
end,
__len = function() return #tbl end
})
return remapped_list
end
local function clone_value_remap(tbl,remapping_func)
local remapped_list = {}
for i=1,#tbl do
remapped_list[i] = remapping_func(tbl[i])
end
return remapped_list
end
local function metatable_1d_scale_remap(tbl,original_width,original_height,new_width,new_height)
local inverse_width = 1/new_width
local inverse_small_width = 1/(new_width - 1)
local inverse_small_height = 1/(new_height - 1)
local remapped_list = setmetatable({},{
__index = function(_, index)
local decoded_x = (index - 1) % new_width
local decoded_y = (index - 1) * inverse_width
decoded_y = decoded_y - decoded_y%1
local normalized_x = decoded_x * inverse_small_width
local normalized_y = decoded_y * inverse_small_height
local original_x = normalized_x * (original_width - 1)
local original_y = normalized_y * (original_height - 1)
original_x = original_x - original_x%1
original_y = original_y - original_y%1
local mapped_index = original_y * original_width + original_x + 1
return tbl[mapped_index]
end,
__len = function()
return new_width*new_height
end
})
return remapped_list
end
local function metatable_remap_2d_to_1d(tbl,width,height,indexer)
indexer = indexer or indexer_2d_remap_yx
local size = width*height
local remapped_list = setmetatable({},{__index=function(self,key)
return indexer(tbl,key,width,height)
end,__len=function()
return size
end})
return remapped_list
end
return {arrutil = {
create_multilayer_list = create_multilayer_list,
mt_key_remap = metatable_remap,
mt_val_remap = metatable_value_remap,
mt_val_remap_mem = metatable_value_remap_memoized,
mt_scale_remap = metatable_1d_scale_remap,
mt_remap_2d_to_1d = metatable_remap_2d_to_1d,
cl_val_remap = clone_value_remap,
map = {
pnglua = indexer_2d_remap_pnglua,
yx = indexer_2d_remap_yx,
}
}},{}
end,
id = "PB_MODULE:arrutil",
name = "PB_ArrayUtil",
author = "9551",
contact = "https://devvie.cc/contact",
report_msg = "\n__name: module error, report issues at\n-> __contact"
}