-
Notifications
You must be signed in to change notification settings - Fork 21
/
buffer.R
541 lines (502 loc) · 16.8 KB
/
buffer.R
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
typeSignedByte <- 5120
typeUnsignedByte <- 5121
typeSignedShort <- 5122
typeUnsignedShort <- 5123
typeSignedInt <- 5124 # Not supported in glTF
typeUnsignedInt <- 5125
typeFloat <- 5126
typeDouble <- 5130 # Not supported in glTF
gltfTypes <- c(byte = 5120, ubyte = 5121,
short = 5122, ushort = 5123,
uint = 5125, float = 5126, int = 5124, double = 5130)
getType <- function(x, types = "anyGLTF") {
types <- match.arg(types,
c(names(gltfTypes), c("anyGLTF", "any")),
several.ok = TRUE)
if ("anyGLTF" %in% types)
types <- c(types, names(gltfTypes)[1:6])
if ("any" %in% types)
types <- c(types, names(gltfTypes))
types <- unique(setdiff(types, c("anyGLTF", "any")))
r <- suppressWarnings(range(x, na.rm = TRUE))
if (is.integer(x) &&
!any(is.na(x)) &&
(r[1] >= 0 &&
any(c("byte", "short", "int", "ubyte", "ushort", "uint") %in% types) ||
(r[1] < 0 &&
any(c("byte", "short", "int") %in% types))))
{
if (r[1] < 0 && ("byte" %in% types)) {
if (-128 <= r[1] && r[2] <= 127)
"byte"
else if (-32768 <= r[1] && r[2] <= 32767 && ("short" %in% types))
"short"
else
"int"
} else {
if (r[2] <= 255 && ("ubyte" %in% types))
"ubyte"
else if (r[2] <= 65535 && ("ushort" %in% types))
"ushort"
else
"uint"
}
} else if (is.numeric(x)) {
if ((-32768 <= r[1] && r[2] <= 32767 ||
0 <= r[1] && r[2] <= 65535) &&
isTRUE(all(x == as.integer(x))) &&
any(c("byte", "short", "ubyte", "ushort") %in% types))
getType(as.integer(x), types)
else if ("float" %in% types)
"float"
else if ("double" %in% types)
"double"
} else
stop('Unrecognized or disallowed type')
}
#' @title R6 Class for binary buffers in glTF files.
#'
#' @description
#' These files typically have one buffer holding all the
#' binary data for a scene.
Buffer <- R6Class("Buffer",
public = list(
#' @param json
#' list read from glTF file.
#' @param binfile
#' optional External binary filename, or raw vector
#'
initialize = function(json = NULL, binfile = NULL) {
if (!is.null(json)) {
private$buffers <- json$buffers
private$bufferViews <- json$bufferViews
private$accessors <- json$accessors
}
buffer <- self$getBuffer(0)
if (is.null(buffer$uri)) {
if (is.character(binfile))
buffer$uri <- binfile
else if (is.raw(binfile))
buffer$bytes <- binfile
}
self$setBuffer(0, buffer)
},
#' @description
#' Load from file.
#'
#' @param uri Which file to load.
#' @param buf Which buffer number to load.
#'
load = function(uri, buf = 0) {
buffer <- self$getBuffer(buf)
if (is.null(buffer))
buffer <- list(byteLength = 0)
self$closeBuffer(buf)
if (is.character(uri)) {
bytes <- readBin(uri, "raw", n = file.size(uri))
buffer$uri <- uri
} else if (is.raw(uri))
bytes <- uri
buffer$byteLength <- length(bytes)
buffer$con <- rawConnection(bytes, open = "r b")
self$setBuffer(buf, buffer)
},
#' @description
#' Write open buffer to connection.
#'
#' @param con
#' Output connection.
#' @param buf
#' Buffer number.
#'
saveOpenBuffer = function(con, buf = 0) {
buffer <- self$getBuffer(buf)
if (is.null(buffer) ||
is.null(con0 <- buffer$con) ||
!inherits(con0, "connection") ||
!isOpen(con0))
stop("buffer ", buf, " is not open.")
bytes <- rawConnectionValue(con0)
writeBin(bytes, con)
},
#' @description
#' Get buffer object.
#'
#' @param buf Buffer number.
#' @param default Default buffer object if `buf` not found.
#'
#' @return A list containing components described here:
#' \url{https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-buffer}.
#'
getBuffer = function(buf, default = list(byteLength = 0)) {
buffer <- if (buf 1 <= length(private$buffers))
private$buffers[[buf 1]]
if (is.null(buffer))
default
else
structure(buffer, class = "gltfBuffer")
},
#' @description
#' Set buffer object.
#'
#' @param buf Buffer number.
#' @param buffer New value to insert.
#'
setBuffer = function(buf, buffer)
private$buffers[[buf 1]] <- unclass(buffer),
#' @description
#' Open a connection for the data in a buffer.
#'
#' @param buf Buffer number.
#'
#' @return An open binary connection.
#'
openBuffer = function(buf) {
buffer <- self$getBuffer(buf)
if (is.null(buffer))
stop("no such buffer")
if (is.null(buffer$con)) {
if (!is.null(bytes <- buffer$bytes)) {
buffer$con <- rawConnection(bytes, open = "r b")
buffer$bytes <- NULL
self$setBuffer(buf, buffer)
} else if (is.null(buffer$uri)) {
buffer$con <- rawConnection(raw(0), open = "r b")
self$setBuffer(buf, buffer)
} else
self$load(buffer$uri, buf = buf)
}
self$getBuffer(buf)$con
},
#' @description
#' Write data to buffer.
#'
#' @param values Values to write.
#' @param type Type to write.
#' @param size Byte size of each value.
#' @param buf Which buffer to write to.
#'
#' @return Byte offset of start of bytes written.
#'
writeBuffer = function(values, type, size, buf = 0) {
if (is.null(buffer <- self$getBuffer(buf)))
self$setBuffer(buf, buffer <- list(byteLength = 0))
byteLength <- buffer$byteLength
byteOffset <- byteLength
con <- self$openBuffer(buf)
seek(con, byteOffset)
byteOffset <- bitwAnd(byteOffset size - 1, bitwNot(size - 1))
if (is.null(byteLength))
browser()
if (byteOffset > byteLength) {
writeBin(raw(byteOffset - byteLength), con)
}
if (type %in% c(typeFloat, typeDouble))
values <- as.numeric(values)
else
values <- as.integer(values)
writeBin(values, con, size = size, endian = "little")
buffer <- self$getBuffer(buf)
buffer$byteLength <- byteOffset length(values)*size
self$setBuffer(buf, buffer)
byteOffset
},
#' @description
#' Close the connection in a buffer.
#'
#' If there was a connection open, this will save the
#' contents in the raw vector `bytes` within the buffer object.
#'
#' @param buf The buffer number.
#'
closeBuffer = function(buf) {
buffer <- self$getBuffer(buf)
if (!is.null(buffer) &&
!is.null(buffer$con)) {
buffer$bytes <- rawConnectionValue(buffer$con)
close(buffer$con)
buffer$con <- NULL
self$setBuffer(buf, buffer)
}
},
#' @description
#' Close any open buffers.
#'
#' Call this after working with a GLTF file to avoid warnings
#' from R about closing unused connections.
#'
closeBuffers = function() {
for (i in seq_along(private$buffers)) {
self$closeBuffer(i - 1)
}
},
#' @description
#' Get `bufferView` object.
#'
#' @param bufv `bufferView` number.
#'
#' @return A list containing components described here:
#' \url{https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-bufferview}.
#'
getBufferview = function(bufv) {
bufferview <- private$bufferViews[[bufv 1]]
if (is.null(bufferview))
stop("bufferView ", bufv, " not found.")
structure(bufferview, class = "gltfBufferview")
},
#' @description
#' Add a new buffer view.
#'
#' @param values Values to put in the view.
#' @param type Type of values.
#' @param size Size of values in bytes.
#' @param target Optional target use for values.
#' @param buf Which buffer to write to.
#'
#' @return New `bufferView` number.
#'
addBufferView = function(values, type, size, target = NULL, buf = 0) {
bufferview <- list()
bufferview$buffer <- buf
bufferview$byteLength <- size*length(values)
buffer <- self$getBuffer(buf)
bufferview$byteOffset <- self$writeBuffer(values, type, size, buf)
if (!is.null(target))
bufferview$target <- target
self$setBufferview(length(private$bufferViews), bufferview)
length(private$bufferViews) - 1
},
#' @description
#' Open a connection to a buffer view.
#'
#' @param bufv Which `bufferView`.
#'
#' @return A connection.
openBufferview = function(bufv) {
bufferview <- self$getBufferview(bufv)
con <- self$openBuffer(bufferview$buffer)
seek(con, bufferview$byteOffset)
con
},
#' @description
#' Set `bufferView` object.
#'
#' @param bufv `bufferView` number.
#' @param bufferView New value to insert.
setBufferview = function(bufv, bufferView)
private$bufferViews[[bufv 1]] <- unclass(bufferView),
#' @description
#' Get accessor object
#'
#' @param acc Accessor number
#'
#' @return A list containing components described here:
#' \url{https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-accessor}
#'
getAccessor = function(acc)
structure(private$accessors[[acc 1]], class = "gltfAccessor"),
#' @description
#' Set accessor object.
#'
#' @param acc Accessor number.
#' @param accessor New value to insert.
#'
setAccessor = function(acc, accessor)
private$accessors[[acc 1]] <- unclass(accessor),
#' @description
#' Read data given by accessor number.
#'
#' @param acc Accessor number.
#'
#' @return A vector or array as specified in the accessor. For the `MATn` types, the 3rd index
#' indexes the element.
#'
readAccessor = function(acc) {
if (acc 1 > length(private$accessors))
stop("No such accessor")
accessor <- self$getAccessor(acc)
self$readAccessor0(accessor)
},
#' @description
#' Read data given by accessor object.
#'
#' @param accessor Accessor object
#'
#' @return A vector or array as specified in the accessor. For the `MATn` types, the 3rd index
#' indexes the element.
#'
readAccessor0 = function(accessor) {
typenames <- c("5120" = "byte", "5121" = "unsigned_byte",
"5122" = "short", "5123" = "unsigned_short",
"5125" = "unsigned_int", "5126" = "float")
types <- c("5120" = "int", "5121" = "int",
"5122" = "int", "5123" = "int",
"5125" = "int", "5126" = "double")
sizes <- c("5120" = 1, "5121" = 1,
"5122" = 2, "5123" = 2,
"5125" = 4, "5126" = 4)
signeds <- c("5120" = TRUE, "5121" = FALSE,
"5122" = TRUE, "5123" = FALSE,
"5125" = TRUE, # not really, but make readBin happy
"5126" = TRUE)
lens <- c(SCALAR = 1, VEC2 = 2, VEC3 = 3, VEC4 = 4,
MAT2 = 4, MAT3 = 9, MAT4 = 16)
ctype <- as.character(accessor$componentType)
atype <- accessor$type
type <- types[ctype]
len <- lens[atype]
size <- sizes[ctype]
signed <- signeds[ctype]
count <- accessor$count
if (is.null(accessor$bufferView)) {
values <- numeric(count*len) # initialized to zero
} else {
view <- self$getBufferview(accessor$bufferView)
con <- self$openBufferview(accessor$bufferView)
if (is.null(view$byteStride)) {
skip <- 0
} else
skip <- len*size - view$byteStride
if (is.null(byteOffset <- accessor$byteOffset))
byteOffset <- 0
start <- seek(con) byteOffset
if (skip == 0) {
seek(con, start)
values <- readBin(con, type, n = len*count, size = size,
signed = signed, endian = "little")
} else {
values <- numeric(count*len)
for (i in seq_len(count)) {
seek(con, start (i-1)*view$byteStride)
values[(i-1)*len seq_len(len)] <-
readBin(con, type, n = len, size = size,
signed = signed, endian = "little")
}
}
if (ctype == "5125") { # fix up unsigned integers
values[is.na(values)] <- 2^31
values[values < 0] <- values[values < 0] 2^32
}
}
if (!is.null(sparse <- accessor$sparse)) {
indexobj <- sparse$indices
indexobj$type <- "SCALAR"
indexobj$count <- sparse$count
index <- self$readAccessor0(indexobj)
valueobj <- sparse$values
valueobj$type <- "SCALAR"
valueobj$componentType <- accessor$componentType
valueobj$count <- len*sparse$count
newvalues <- self$readAccessor0(valueobj)
for (i in seq_len(sparse$count))
for (j in seq_len(len))
values[len*index[i] j] <-
newvalues[len*(i-1) j]
}
if (!is.null(accessor$normalized) && accessor$normalized)
values <- switch(ctype,
"5120" = (values 128)/255 - 1, # byte
"5121" = values/255, # u byte
"5122" = (values 2^15)/65535 - 1, # short
"5123" = values/65535, # u short
values) # default
if (len > 1)
if (grepl("MAT", atype)) {
values <- array(values, dim=c(sqrt(len), sqrt(len), count))
} else
values <- matrix(values, ncol = len, byrow = TRUE)
values
},
#' @description
#' Write values to accessor, not including `min` and `max`.
#'
#' @param values Values to write.
#' @param target Optional target use for values.
#' @param types Which types can be used?
#' @param normalized Are normalized integers allowed?
#' @param useDouble Whether to write doubles or singles.
#'
#' @return New accessor number
addAccessor = function(values, target = NULL, types = "anyGLTF", normalized = FALSE) {
componentTypeName <- getType(values, types)
size <- switch(componentTypeName,
"byte" =, # typeSignedByte
"ubyte" = 1, # typeUnsignedByte = 1,
"short" =, # typeSignedShort =,
"ushort" = 2, # typeUnsignedShort = 2,
"uint" =, # typeUnsignedInt =,
"int" =, # typeSignedInt =,
"float" = 4, # typeFloat = 4,
"double" = 8) # typeDouble = 8)
bufferView <- self$addBufferView(c(values), gltfTypes[componentTypeName],
size = size, target = target)
if (is.matrix(values) && nrow(values) > 1) {
count <- ncol(values)
type <- paste0("VEC", nrow(values))
} else {
count <- length(values)
type <- "SCALAR"
}
if (normalized &&
!(componentTypeName %in%
c("byte", "ubyte", "short", "ushort")))
stop("Only bytes and short values can be normalized")
accessor <- list(bufferView = bufferView,
componentType = gltfTypes[[componentTypeName]], # double to drop name
count = count,
type = type)
if (normalized)
accessor$normalized <- TRUE
private$accessors <- c(private$accessors, list(accessor))
length(private$accessors) - 1
},
#' @description
#' Convert buffer to data URI.
#'
#' @param buf Buffer to convert.
#'
#' @return String containing data URI.
dataURI = function(buf = 0) {
self$closeBuffer(buf)
buffer <- self$getBuffer(buf)
if (is.null(buffer))
stop("Buffer ", buf, " does not exist.")
bytes <- buffer$bytes
if (is.null(bytes)) {
if (is.null(buffer$uri))
return(base64enc::dataURI(raw(0), mime = "application/octet-stream"))
else {
self$load(buffer$uri, buf)
self$closeBuffer(buf)
buffer <- self$getBuffer(buf)
bytes <- buffer$bytes
}
}
base64enc::dataURI(bytes, mime = "application/octet-stream")
},
#' @description Convert to list.
#' @return List suitable for writing using JSON.
as.list = function() {
result <- list()
for (n in names(private)) {
thelist <- private[[n]]
if (is.list(thelist) && length(thelist)) {
for (i in seq_along(thelist))
thelist[[i]] <- unclass(thelist[[i]])
result[[n]] <- thelist
}
}
result
}
),
private = list(
buffers = list(),
bufferViews = list(),
accessors = list()
# ,
#
# finalize = function() {
# self$closeBuffers()
# }
)
)