-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
50 lines (42 loc) · 892 Bytes
/
utils.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
package colidr
import "math"
// gauss computes the gaussian function of variance
func gauss(x, mean, sigma float64) float64 {
return math.Exp((-(x-mean)*(x-mean))/(2*sigma*sigma)) / math.Sqrt(math.Pi*2.0*sigma*sigma)
}
// makeGaussianVector constructs a gaussian vector field of floats
func makeGaussianVector(sigma float64) []float64 {
var (
gau []float64
threshold = 0.001
i int
)
for {
i++
if gauss(float64(i), 0.0, sigma) < threshold {
break
}
}
// clear slice
gau = gau[:0]
// extend slice
gau = append(gau, make([]float64, i+1)...)
gau[0] = gauss(0.0, 0.0, sigma)
for j := 1; j < len(gau); j++ {
gau[j] = gauss(float64(j), 0.0, sigma)
}
return gau
}
func abs(val float32) float32 {
if val < 0.0 {
return -val
}
return val
}
// absInt return the absolute value of x
func absInt(x int) int {
if x < 0 {
return -x
}
return x
}