-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.go
269 lines (228 loc) · 4.17 KB
/
kernel.go
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
package rendr
import (
"fmt"
"math"
"strings"
)
func ZeroEval(xx float64) float64 {
return 0
}
func Zero1Apply(ww []float64, xa float64) {
ww[0] = 0
}
func Zero2Apply(ww []float64, xa float64) {
ww[0] = 0
ww[1] = 0
}
func Zero3Apply(ww []float64, xa float64) {
ww[0] = 0
ww[1] = 0
ww[2] = 0
}
func Zero4Apply(ww []float64, xa float64) {
ww[0] = 0
ww[1] = 0
ww[2] = 0
ww[3] = 0
}
func Zero5Apply(ww []float64, xa float64) {
ww[0] = 0
ww[1] = 0
ww[2] = 0
ww[3] = 0
ww[4] = 0
}
func Zero6Apply(ww []float64, xa float64) {
ww[0] = 0
ww[1] = 0
ww[2] = 0
ww[3] = 0
ww[4] = 0
ww[5] = 0
}
// ============ Ctmr to the third derivative ============
func ctmrEval(x float64) float64 {
var ret float64
x = math.Abs(x)
if x<1 {
ret = 1+x*x*(-2.5 + x*1.5);
} else if x<2 {
x -= 1;
ret = x*(-0.5 + x*(1-x/2))
} else {
ret = 0
}
return ret
}
func ctmrApply(ww []float64, xa float64) {
ww[0] = -((xa-1)*(xa-1)*xa)/2
ww[1] = (2 + xa*xa*(-5 + 3*xa))/2
ww[2] = (xa + xa*xa*(4 - 3*xa))/2
ww[3] = ((-1 + xa)*xa*xa)/2
}
func dctmrEval(_x float64) float64 {
var ret float64
x := math.Abs(_x)
if x<1 {
ret = x*(-5 + x*4.5)
} else if x<2 {
x -= 1
ret = -0.5 + x * (2-x*1.5)
} else {
ret = 0
}
if _x<0 {
return -ret
} else {
return ret
}
}
func dctmrApply(ww []float64, xa float64) {
ww[0] = (-1 + (4 - 3*xa)*xa)/2
ww[1] = (xa*(-10 + 9*xa))/2
ww[2] = -((-1 + xa)*(1 + 9*xa))/2
ww[3] = (xa*(-2 + 3*xa))/2
}
func ddctmrEval(x float64) float64 {
var ret float64
x = math.Abs(x)
if x<1 {
ret = -5 + 9 * x
} else if x<2 {
x -= 1
ret = 2 - 3*x
} else {
ret = 0
}
return ret
}
func ddctmrApply(ww []float64, xa float64) {
ww[0] = 2 - 3*xa;
ww[1] = -5 + 9*xa;
ww[2] = 4 - 9*xa;
ww[3] = -1 + 3*xa;
}
func dddctmrEval(_x float64) float64 {
var ret float64
x := math.Abs(_x)
if x<1 {
ret = 9
} else if x<2 {
ret = -3
} else {
ret = 0
}
if _x<0 {
return -ret
} else {
return ret
}
}
func dddctmrApply(ww []float64, xa float64) {
ww[0] = -3
ww[1] = 9
ww[2] = -9
ww[3] = 3
}
var zero1Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 1)",
Support: 1,
Eval: ZeroEval,
Apply: Zero1Apply,
}
var zero2Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 2)",
Support: 2,
Eval: ZeroEval,
Apply: Zero2Apply,
}
var zero3Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 3)",
Support: 3,
Eval: ZeroEval,
Apply: Zero3Apply,
}
var zero4Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 4)",
Support: 4,
Eval: ZeroEval,
Apply: Zero4Apply,
}
var zero5Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 5)",
Support: 5,
Eval: ZeroEval,
Apply: Zero5Apply,
}
var zero6Kernel = rndKernel{
Name: "Zero",
Desc: "Returns zero everywhere (support 6)",
Support: 6,
Eval: ZeroEval,
Apply: Zero6Apply,
}
var dddCtmr = rndKernel{
Name: "dddCtmr",
Desc: "3rd deriv of Catmull-Rom",
Support: 4,
Eval: dddctmrEval,
Apply: dddctmrApply,
Deriv: &zero4Kernel,
}
var ddCtmr = rndKernel{
Name: "dddCtmr",
Desc: "2nd deriv of Catmull-Rom",
Support: 4,
Eval: ddctmrEval,
Apply: ddctmrApply,
Deriv: &dddCtmr,
}
var dCtmr = rndKernel{
Name: "dddCtmr",
Desc: "1st deriv of Catmull-Rom",
Support: 4,
Eval: dctmrEval,
Apply: dctmrApply,
Deriv: &ddCtmr,
}
var ctmr = rndKernel{
Name: "Ctmr",
Desc: "Catmull-Rom spline (C1, reconstructs quadratic)",
Support: 4,
Eval: ctmrEval,
Apply: ctmrApply,
Deriv: &dCtmr,
}
var rndKernelZero *rndKernel = &zero1Kernel
var rndKernelCtmr *rndKernel = &ctmr
// C way, we can use Map in Go
//var RndKernelAll = [](*rndKernel){
// &zero1Kernel,
// &ctmr,
//}
var rndKernelMap = map[string]*rndKernel{
"ctmr": rndKernelCtmr,
"zero": rndKernelZero,
}
func InitKernels() {
zero1Kernel.Deriv = &zero1Kernel
zero2Kernel.Deriv = &zero2Kernel
zero3Kernel.Deriv = &zero3Kernel
zero4Kernel.Deriv = &zero4Kernel
zero5Kernel.Deriv = &zero5Kernel
zero6Kernel.Deriv = &zero6Kernel
}
func rndKernelParse(_kstr string) *rndKernel{
InitKernels()
kstr := strings.ToLower(_kstr)
if ret, ok := rndKernelMap[kstr]; ok {
return ret
}
fmt.Printf("The kernel [%s] does not exist\n", _kstr)
return nil
}