-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.go
120 lines (113 loc) · 2.36 KB
/
conversions.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
package conversions
import (
"fmt"
"strconv"
"strings"
)
func IntForValue(value interface{}) (out int, err error) {
switch value := value.(type) { // shadow variable
case string:
i, err := strconv.ParseFloat(value, 32)
if err == nil {
out = int(i)
} else {
fmt.Println(err)
out = -1
}
case float32:
out = int(value)
case float64:
out = int(value)
case int:
out = int(value)
case bool:
out = 0
if value {
out = 1
}
default:
return 0, fmt.Errorf("float32ForValue(): %v is not a "+
"number or Boolean", value)
}
return out, nil
}
func Float32ForValue(value interface{}) (out float32, err error) {
switch value := value.(type) { // shadow variable
case string:
i, err := strconv.ParseFloat(value, 32)
if err == nil {
out = float32(i)
} else {
fmt.Println(err)
out = -1
}
case float32:
out = value
case float64:
out = float32(value)
case int:
out = float32(value)
case bool:
out = 0
if value {
out = 1
}
default:
return 0, fmt.Errorf("float32ForValue(): %v is not a "+"number or Boolean", value)
}
return out, nil
}
func Float64ForValue(value interface{}) (out float64, err error) {
switch value := value.(type) { // shadow variable
case string:
i, err := strconv.ParseFloat(value, 32)
if err == nil {
out = float64(i)
} else {
fmt.Println(err)
out = -1
}
case float32:
out = float64(value)
case float64:
out = value
case int:
out = float64(value)
case bool:
out = 0
if value {
out = 1
}
default:
return 0, fmt.Errorf("float32ForValue(): %v is not a "+"number or Boolean", value)
}
return out, nil
}
func String2Array16(value string, atype string, out []int16) (err error) {
separator := string(",")
str := strings.Replace(value, " ", "", -1)
str = strings.Replace(str, "\n", "", -1)
str = strings.Replace(str, "\t", "", -1)
imageParts := strings.Split(str, separator)
i := 0
for _, voxel := range imageParts {
tmp, _ := strconv.Atoi(voxel)
out[i] = int16(tmp)
i++
}
return
}
func String2Array32(value string, atype string, out []int32) (err error) {
separator := string(",")
str := strings.Replace(value, " ", "", -1)
str = strings.Replace(str, "\n", "", -1)
str = strings.Replace(str, "\t", "", -1)
imageParts := strings.Split(str, separator)
i := 0
for _, voxel := range imageParts {
tmp, _ := strconv.Atoi(voxel)
out[i] = int32(tmp)
i++
}
return
}