-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.lua
95 lines (83 loc) · 2.01 KB
/
util.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
local util = {}
function util.clamp(x, lo, hi)
if not lo and not hi then
lo, hi = 0, 1
end
return math.min(hi, math.max(lo, x))
end
util.mean = {}
util.mean.arithmetic = {
add = function(accum, value)
return (accum or 0) + value
end,
mean = function(accum, n)
return accum and accum / n
end,
}
util.mean.max = {
add = function(accum, value)
return accum and math.max(accum, value) or value
end,
mean = function(accum, n)
return accum
end,
}
util.mean.min = {
add = function(accum, value)
return accum and math.min(accum, value) or value
end,
mean = function(accum, n)
return accum
end,
}
util.mean.quadratic = {
add = function(accum, value)
return (accum or 0) + value*value
end,
mean = function(accum, n)
return accum and math.sqrt(accum / n)
end,
}
util.mean.harmonic = {
add = function(accum, value)
return (accum or 0) + 1 / value
end,
mean = function(accum, n)
return accum and n / accum
end,
}
function util.nodePathToStr(nodePath)
local str = ""
for _, part in ipairs(nodePath) do
str = str .. "/" .. part[1]
if part[2] > 1 then
str = str .. "[" .. part[2] .. "]"
end
end
return str:len() > 0 and str or "/"
end
-- path should only be the last part of the path
function util.getChildByPath(node, path)
for _, child in ipairs(node.children) do
local childPart = child.path[#child.path]
if path[1] == childPart[1] and path[2] == childPart[2] then
return child
end
end
return nil
end
function util.getNodeByPath(frame, path)
if frame.pathCache[path] then
return frame.pathCache[path]
end
local current = frame
for _, part in ipairs(path) do
current = util.getChildByPath(current, part)
if not current then
return nil
end
end
frame.pathCache[path] = current
return current
end
return util