Last active
August 29, 2015 14:02
-
-
Save guybrush/7f3de1287f3047d423cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
this an approach to be able to use typedarray.set to set continuous parts | |
of an ndarray: | |
var store = ndarray(new Store(new Uint8Array(32*32*32*3*3*3)),[32*3,32*3,32*3]) | |
var chunks = {} | |
for (var x=0;x<3;x++) | |
for (var y=0;y<3;y++) | |
for (var z=0;z<3;z++) { | |
chunks[x] = chunks[x] || {} | |
chunks[x][y] = chunks[x][y] || {} | |
chunks[x][y][z] = ndarray(new Uint8Array(32*32*32,[32,32,32])) | |
var offset = x*32*32 + y*32 +z | |
store.data.set(chunks[x][y][z], offset) | |
} | |
*/ | |
var range = 32*32*32 | |
var pow32_2 = 32*32 | |
var stride0 = 3*3 | |
var stride1 = 3 | |
var stride2 = 1 | |
function Store(underlyingStorage) { | |
this.data = underlyingStorage | |
this.length = underlyingStorage.length | |
} | |
Store.prototype.get = function(i) { | |
return this.data[calc(i)] | |
} | |
Store.prototype.set = function(i, v) { | |
this.data[calc(i)] = v | |
} | |
function calc(i) { | |
var z = i%32 | |
var y = (i-z)%pow32_2 | |
var izy = i-z-y | |
var x = izy>0 ? izy/pow32_2 : 0 | |
y = y/32 | |
var chunk_index = (z/32)|0 * stride0 + (y/32)|0 * stride1 + (x/32)|0 * stride0 | |
return i%range + (chunk_index*range) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment