• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qmuntal / gltf / 10597453321

28 Aug 2024 01:14PM UTC coverage: 90.815% (-3.3%) from 94.096%
10597453321

push

github

web-flow
Merge pull request #88 from qmuntal/buffers

Improve modeler.ReadAccessor buffer handling

161 of 264 new or added lines in 2 files covered. (60.98%)

2 existing lines in 1 file now uncovered.

2541 of 2798 relevant lines covered (90.81%)

62.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.87
/binary/slice.go
1
package binary
2

3
import (
4
        "fmt"
5
        "image/color"
6
        "reflect"
7
        "unsafe"
8

9
        "github.com/qmuntal/gltf"
10
)
11

12
func castSlice(c gltf.ComponentType, t gltf.AccessorType, v []byte) any {
24✔
13
        var ptr unsafe.Pointer
24✔
14
        if len(v) != 0 {
48✔
15
                ptr = unsafe.Pointer(&v[0])
24✔
16
        }
24✔
17
        switch c {
24✔
18
        case gltf.ComponentUbyte:
24✔
19
                switch t {
24✔
NEW
20
                case gltf.AccessorScalar:
×
NEW
21
                        return v
×
22
                case gltf.AccessorVec2:
24✔
23
                        return unsafe.Slice((*[2]uint8)(ptr), len(v)/2)
24✔
NEW
24
                case gltf.AccessorVec3:
×
NEW
25
                        return unsafe.Slice((*[3]uint8)(ptr), len(v)/3)
×
NEW
26
                case gltf.AccessorVec4:
×
NEW
27
                        return unsafe.Slice((*[4]uint8)(ptr), len(v)/4)
×
NEW
28
                case gltf.AccessorMat2:
×
NEW
29
                        return unsafe.Slice((*[2][2]uint8)(ptr), len(v)/4)
×
NEW
30
                case gltf.AccessorMat3:
×
NEW
31
                        return unsafe.Slice((*[3][3]uint8)(ptr), len(v)/9)
×
NEW
32
                case gltf.AccessorMat4:
×
NEW
33
                        return unsafe.Slice((*[4][4]uint8)(ptr), len(v)/16)
×
34
                }
NEW
35
        case gltf.ComponentByte:
×
NEW
36
                switch t {
×
NEW
37
                case gltf.AccessorScalar:
×
NEW
38
                        return unsafe.Slice((*int8)(ptr), len(v))
×
NEW
39
                case gltf.AccessorVec2:
×
NEW
40
                        return unsafe.Slice((*[2]int8)(ptr), len(v)/2)
×
NEW
41
                case gltf.AccessorVec3:
×
NEW
42
                        return unsafe.Slice((*[3]int8)(ptr), len(v)/3)
×
NEW
43
                case gltf.AccessorVec4:
×
NEW
44
                        return unsafe.Slice((*[4]int8)(ptr), len(v)/4)
×
NEW
45
                case gltf.AccessorMat2:
×
NEW
46
                        return unsafe.Slice((*[2][2]int8)(ptr), len(v)/4)
×
NEW
47
                case gltf.AccessorMat3:
×
NEW
48
                        return unsafe.Slice((*[3][3]int8)(ptr), len(v)/9)
×
NEW
49
                case gltf.AccessorMat4:
×
NEW
50
                        return unsafe.Slice((*[4][4]int8)(ptr), len(v)/16)
×
51
                }
NEW
52
        case gltf.ComponentUshort:
×
NEW
53
                switch t {
×
NEW
54
                case gltf.AccessorScalar:
×
NEW
55
                        return unsafe.Slice((*uint16)(ptr), len(v)/2)
×
NEW
56
                case gltf.AccessorVec2:
×
NEW
57
                        return unsafe.Slice((*[2]uint16)(ptr), len(v)/4)
×
NEW
58
                case gltf.AccessorVec3:
×
NEW
59
                        return unsafe.Slice((*[3]uint16)(ptr), len(v)/6)
×
NEW
60
                case gltf.AccessorVec4:
×
NEW
61
                        return unsafe.Slice((*[4]uint16)(ptr), len(v)/8)
×
NEW
62
                case gltf.AccessorMat2:
×
NEW
63
                        return unsafe.Slice((*[2][2]uint16)(ptr), len(v)/8)
×
NEW
64
                case gltf.AccessorMat3:
×
NEW
65
                        return unsafe.Slice((*[3][3]uint16)(ptr), len(v)/18)
×
NEW
66
                case gltf.AccessorMat4:
×
NEW
67
                        return unsafe.Slice((*[4][4]uint16)(ptr), len(v)/32)
×
68
                }
NEW
69
        case gltf.ComponentShort:
×
NEW
70
                switch t {
×
NEW
71
                case gltf.AccessorScalar:
×
NEW
72
                        return unsafe.Slice((*int16)(ptr), len(v)/2)
×
NEW
73
                case gltf.AccessorVec2:
×
NEW
74
                        return unsafe.Slice((*[2]int16)(ptr), len(v)/4)
×
NEW
75
                case gltf.AccessorVec3:
×
NEW
76
                        return unsafe.Slice((*[3]int16)(ptr), len(v)/6)
×
NEW
77
                case gltf.AccessorVec4:
×
NEW
78
                        return unsafe.Slice((*[4]int16)(ptr), len(v)/8)
×
NEW
79
                case gltf.AccessorMat2:
×
NEW
80
                        return unsafe.Slice((*[2][2]int16)(ptr), len(v)/8)
×
NEW
81
                case gltf.AccessorMat3:
×
NEW
82
                        return unsafe.Slice((*[3][3]int16)(ptr), len(v)/18)
×
NEW
83
                case gltf.AccessorMat4:
×
NEW
84
                        return unsafe.Slice((*[4][4]int16)(ptr), len(v)/32)
×
85
                }
NEW
86
        case gltf.ComponentUint:
×
NEW
87
                switch t {
×
NEW
88
                case gltf.AccessorScalar:
×
NEW
89
                        return unsafe.Slice((*uint32)(ptr), len(v)/4)
×
NEW
90
                case gltf.AccessorVec2:
×
NEW
91
                        return unsafe.Slice((*[2]uint32)(ptr), len(v)/8)
×
NEW
92
                case gltf.AccessorVec3:
×
NEW
93
                        return unsafe.Slice((*[3]uint32)(ptr), len(v)/12)
×
NEW
94
                case gltf.AccessorVec4:
×
NEW
95
                        return unsafe.Slice((*[4]uint32)(ptr), len(v)/16)
×
NEW
96
                case gltf.AccessorMat2:
×
NEW
97
                        return unsafe.Slice((*[2][2]uint32)(ptr), len(v)/16)
×
NEW
98
                case gltf.AccessorMat3:
×
NEW
99
                        return unsafe.Slice((*[3][3]uint32)(ptr), len(v)/36)
×
NEW
100
                case gltf.AccessorMat4:
×
NEW
101
                        return unsafe.Slice((*[4][4]uint32)(ptr), len(v)/64)
×
102
                }
NEW
103
        case gltf.ComponentFloat:
×
NEW
104
                switch t {
×
NEW
105
                case gltf.AccessorScalar:
×
NEW
106
                        return unsafe.Slice((*float32)(ptr), len(v)/4)
×
NEW
107
                case gltf.AccessorVec2:
×
NEW
108
                        return unsafe.Slice((*[2]float32)(ptr), len(v)/8)
×
NEW
109
                case gltf.AccessorVec3:
×
NEW
110
                        return unsafe.Slice((*[3]float32)(ptr), len(v)/12)
×
NEW
111
                case gltf.AccessorVec4:
×
NEW
112
                        return unsafe.Slice((*[4]float32)(ptr), len(v)/16)
×
NEW
113
                case gltf.AccessorMat2:
×
NEW
114
                        return unsafe.Slice((*[2][2]float32)(ptr), len(v)/16)
×
NEW
115
                case gltf.AccessorMat3:
×
NEW
116
                        return unsafe.Slice((*[3][3]float32)(ptr), len(v)/36)
×
NEW
117
                case gltf.AccessorMat4:
×
NEW
118
                        return unsafe.Slice((*[4][4]float32)(ptr), len(v)/64)
×
119
                }
120
        }
NEW
121
        return nil
×
122
}
123

124
// MakeSliceBuffer returns the slice type associated with c and t and with the given element count.
125
// If the buffer is an slice which type matches with the expected by the acr then it will
126
// be used as backing slice.
127
func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count int, buffer []byte) (any, error) {
36✔
128
        if len(buffer) == 0 {
48✔
129
                return MakeSlice(c, t, count)
12✔
130
        }
12✔
131
        v := castSlice(c, t, buffer)
24✔
132
        if v == nil {
24✔
UNCOV
133
                return MakeSlice(c, t, count)
×
UNCOV
134
        }
×
135
        count1 := reflect.ValueOf(v).Len()
24✔
136
        if count1 < count {
30✔
137
                tmpSlice, _ := MakeSlice(c, t, count-count1)
6✔
138
                return reflect.AppendSlice(reflect.ValueOf(v), reflect.ValueOf(tmpSlice)).Interface(), nil
6✔
139
        }
6✔
140
        if count1 > count {
30✔
141
                return reflect.ValueOf(v).Slice(0, int(count)).Interface(), nil
12✔
142
        }
12✔
143
        return v, nil
6✔
144
}
145

146
// MakeSlice returns the slice type associated with c and t and with the given element count.
147
// For example, if c is gltf.ComponentFloat and t is gltf.AccessorVec3
148
// then MakeSlice(c, t, 5) is equivalent to make([][3]float32, 5).
149
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count int) (any, error) {
270✔
150
        var tp reflect.Type
270✔
151
        switch c {
270✔
152
        case gltf.ComponentUbyte:
60✔
153
                tp = reflect.TypeOf((*uint8)(nil))
60✔
154
        case gltf.ComponentByte:
42✔
155
                tp = reflect.TypeOf((*int8)(nil))
42✔
156
        case gltf.ComponentUshort:
42✔
157
                tp = reflect.TypeOf((*uint16)(nil))
42✔
158
        case gltf.ComponentShort:
42✔
159
                tp = reflect.TypeOf((*int16)(nil))
42✔
160
        case gltf.ComponentUint:
42✔
161
                tp = reflect.TypeOf((*uint32)(nil))
42✔
162
        case gltf.ComponentFloat:
42✔
163
                tp = reflect.TypeOf((*float32)(nil))
42✔
NEW
164
        default:
×
NEW
165
                return nil, fmt.Errorf("gltf: unsupported component type %d", c)
×
166
        }
167
        tp = tp.Elem()
270✔
168
        switch t {
270✔
169
        case gltf.AccessorScalar:
36✔
170
                // Nothing to do.
171
        case gltf.AccessorVec2:
54✔
172
                tp = reflect.ArrayOf(2, tp)
54✔
173
        case gltf.AccessorVec3:
36✔
174
                tp = reflect.ArrayOf(3, tp)
36✔
175
        case gltf.AccessorVec4:
36✔
176
                tp = reflect.ArrayOf(4, tp)
36✔
177
        case gltf.AccessorMat2:
36✔
178
                tp = reflect.ArrayOf(2, reflect.ArrayOf(2, tp))
36✔
179
        case gltf.AccessorMat3:
36✔
180
                tp = reflect.ArrayOf(3, reflect.ArrayOf(3, tp))
36✔
181
        case gltf.AccessorMat4:
36✔
182
                tp = reflect.ArrayOf(4, reflect.ArrayOf(4, tp))
36✔
NEW
183
        default:
×
NEW
184
                return nil, fmt.Errorf("gltf: unsupported accessor type %d", t)
×
185
        }
186
        return reflect.MakeSlice(reflect.SliceOf(tp), count, count).Interface(), nil
270✔
187
}
188

189
// Type returns the associated glTF type data.
190
// It panics if data is not an slice.
191
func Type(data any) (c gltf.ComponentType, t gltf.AccessorType, count int) {
648✔
192
        v := reflect.ValueOf(data)
648✔
193
        if v.Kind() != reflect.Slice {
648✔
194
                panic(fmt.Sprintf("gltf: binary.Type expecting a slice but got %s", v.Kind()))
×
195
        }
196
        count = v.Len()
648✔
197
        switch data.(type) {
648✔
198
        case []int8:
48✔
199
                c, t = gltf.ComponentByte, gltf.AccessorScalar
48✔
200
        case [][2]int8:
12✔
201
                c, t = gltf.ComponentByte, gltf.AccessorVec2
12✔
202
        case [][3]int8:
12✔
203
                c, t = gltf.ComponentByte, gltf.AccessorVec3
12✔
204
        case [][4]int8:
12✔
205
                c, t = gltf.ComponentByte, gltf.AccessorVec4
12✔
206
        case [][2][2]int8:
12✔
207
                c, t = gltf.ComponentByte, gltf.AccessorMat2
12✔
208
        case [][3][3]int8:
12✔
209
                c, t = gltf.ComponentByte, gltf.AccessorMat3
12✔
210
        case [][4][4]int8:
12✔
211
                c, t = gltf.ComponentByte, gltf.AccessorMat4
12✔
212
        case []uint8:
36✔
213
                c, t = gltf.ComponentUbyte, gltf.AccessorScalar
36✔
214
        case [][2]uint8:
12✔
215
                c, t = gltf.ComponentUbyte, gltf.AccessorVec2
12✔
216
        case [][3]uint8:
12✔
217
                c, t = gltf.ComponentUbyte, gltf.AccessorVec3
12✔
218
        case []color.RGBA, [][4]uint8:
24✔
219
                c, t = gltf.ComponentUbyte, gltf.AccessorVec4
24✔
220
        case [][2][2]uint8:
12✔
221
                c, t = gltf.ComponentUbyte, gltf.AccessorMat2
12✔
222
        case [][3][3]uint8:
12✔
223
                c, t = gltf.ComponentUbyte, gltf.AccessorMat3
12✔
224
        case [][4][4]uint8:
12✔
225
                c, t = gltf.ComponentUbyte, gltf.AccessorMat4
12✔
226
        case []int16:
24✔
227
                c, t = gltf.ComponentShort, gltf.AccessorScalar
24✔
228
        case [][2]int16:
12✔
229
                c, t = gltf.ComponentShort, gltf.AccessorVec2
12✔
230
        case [][3]int16:
12✔
231
                c, t = gltf.ComponentShort, gltf.AccessorVec3
12✔
232
        case [][4]int16:
12✔
233
                c, t = gltf.ComponentShort, gltf.AccessorVec4
12✔
234
        case [][2][2]int16:
12✔
235
                c, t = gltf.ComponentShort, gltf.AccessorMat2
12✔
236
        case [][3][3]int16:
12✔
237
                c, t = gltf.ComponentShort, gltf.AccessorMat3
12✔
238
        case [][4][4]int16:
12✔
239
                c, t = gltf.ComponentShort, gltf.AccessorMat4
12✔
240
        case []uint16:
24✔
241
                c, t = gltf.ComponentUshort, gltf.AccessorScalar
24✔
242
        case [][2]uint16:
12✔
243
                c, t = gltf.ComponentUshort, gltf.AccessorVec2
12✔
244
        case [][3]uint16:
12✔
245
                c, t = gltf.ComponentUshort, gltf.AccessorVec3
12✔
246
        case []color.RGBA64, [][4]uint16:
24✔
247
                c, t = gltf.ComponentUshort, gltf.AccessorVec4
24✔
248
        case [][2][2]uint16:
12✔
249
                c, t = gltf.ComponentUshort, gltf.AccessorMat2
12✔
250
        case [][3][3]uint16:
12✔
251
                c, t = gltf.ComponentUshort, gltf.AccessorMat3
12✔
252
        case [][4][4]uint16:
12✔
253
                c, t = gltf.ComponentUshort, gltf.AccessorMat4
12✔
254
        case []uint32:
24✔
255
                c, t = gltf.ComponentUint, gltf.AccessorScalar
24✔
256
        case [][2]uint32:
12✔
257
                c, t = gltf.ComponentUint, gltf.AccessorVec2
12✔
258
        case [][3]uint32:
12✔
259
                c, t = gltf.ComponentUint, gltf.AccessorVec3
12✔
260
        case [][4]uint32:
12✔
261
                c, t = gltf.ComponentUint, gltf.AccessorVec4
12✔
262
        case [][2][2]uint32:
12✔
263
                c, t = gltf.ComponentUint, gltf.AccessorMat2
12✔
264
        case [][3][3]uint32:
12✔
265
                c, t = gltf.ComponentUint, gltf.AccessorMat3
12✔
266
        case [][4][4]uint32:
12✔
267
                c, t = gltf.ComponentUint, gltf.AccessorMat4
12✔
268
        case []float32:
24✔
269
                c, t = gltf.ComponentFloat, gltf.AccessorScalar
24✔
270
        case [][2]float32:
12✔
271
                c, t = gltf.ComponentFloat, gltf.AccessorVec2
12✔
272
        case [][3]float32:
24✔
273
                c, t = gltf.ComponentFloat, gltf.AccessorVec3
24✔
274
        case [][4]float32:
12✔
275
                c, t = gltf.ComponentFloat, gltf.AccessorVec4
12✔
276
        case [][2][2]float32:
12✔
277
                c, t = gltf.ComponentFloat, gltf.AccessorMat2
12✔
278
        case [][3][3]float32:
12✔
279
                c, t = gltf.ComponentFloat, gltf.AccessorMat3
12✔
280
        case [][4][4]float32:
12✔
281
                c, t = gltf.ComponentFloat, gltf.AccessorMat4
12✔
282
        default:
×
283
                panic(fmt.Sprintf("gltf: binary.Type expecting a glTF supported type but got %s", v.Kind()))
×
284
        }
285
        return
648✔
286
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc