-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharrayUtilities.js
74 lines (69 loc) · 2.09 KB
/
arrayUtilities.js
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
/**
* Created by Johnson on 12/2/2016.
*/
/**
* Create an unsigned integer array from 0 to n
* @param n
* @returns {Uint32Array}
*/
function createIndicesArray(n) {
var array = new Uint32Array(n);
for (var i = 0; i<n; i++) {
array[0] = i;
}
return array;
}
/**
* Create a 2D Float32Array where each element is made of n components (a 3D vector for example)
* @param n_rows number of rows = height
* @param n_cols number of columns = width
* @param n_components number of components
* @returns {Float32Array} returned array
*/
function create2DArray(n_rows, n_cols, n_components) {
var array = new Float32Array(n_rows*n_cols*n_components);
array.height = n_rows;
array.width = n_cols;
array.comp = n_components;
return array;
}
/**
* Sets the value of each element to 2D indices. An MXN matrix of 2D Vectors will be filled as follow:
* (0,0) (0,1) ..... (0,N-1)
* (1,0) (1,1) ..... (1,N-1)
* ...
* (M,0) (0,1) ..... (M,N-1)
*/
Float32Array.prototype.fill2DArrayWithOrderedIndices = function() {
if (this.comp < 2) // not possible for 1D arrays
return;
for (var i = 0; i< this.height; i++) {
for (var j = 0; j < this.width; j++) {
this.setTo(i,j,0,i);
this.setTo(i,j,1,j);
}
}
};
/**
* Get element (r,c) of a 2D array at components k. Components are used for vectors: ex. for an RGBA 2D vector, to
* address R, we use (r,c,0); to address B, we use (r,c,2). Row major.
* @param r row number
* @param c column number
* @param k strand value
* @returns {*}
*/
Float32Array.prototype.get = function(r,c,k) {
return this[(r*this.width)*this.comp + c*this.comp + k];
};
/**
* Set element (r,c) of a 2D array at component k. Components are used for vectors: ex. for an RGBA 2D vector, to
* address R, we use (r,c,0); to address B, we use (r,c,2). Row major.
* @param r row number
* @param c column number
* @param k component value
* @param val value to be set
* @returns {*}
*/
Float32Array.prototype.setTo = function(r,c,k, val) {
this[(r*this.width)*this.comp + c*this.comp + k] = val;
};