restructure code, internalize devices

This commit is contained in:
Stewart Allen 2020-06-03 22:39:30 -04:00
commit 02feb524e6
110 changed files with 592 additions and 681 deletions

139
src/add/array.js Normal file
View file

@ -0,0 +1,139 @@
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
(function() {
var AP = Array.prototype;
/** ******************************************************************
* array prototype helpers
******************************************************************* */
if (!AP.flat) {
AP.flat = function() {
return [].concat.apply([], this);
};
}
AP.equals = function(arr) {
if (!arr) return false;
if (arr.length !== this.length) return false;
for (let i=0; i<this.length; i++) {
if (arr[i] !== this[i]) return false;
}
return true;
};
AP.peek = function() {
return this[this.length-1];
};
/**
* allow chaining of push() calls
*
* @param v
* @returns {Array}
*/
AP.append = function(v) {
this.push(v);
return this;
};
/**
* append all array elements to this array
*
* @param {Array} arr
* @returns {Array}
*/
AP.appendAll = function(arr) {
if (arr && arr.length > 0) this.push.apply(this,arr);
return this;
};
/**
* shallow cloning with clone(arg) call on each new element
*
* @param {Array} [arg]
* @returns {Array}
*/
AP.clone = function(arg) {
var na = this.slice(),
ln = na.length,
i = 0;
while (i < ln) na[i] = na[i++].clone(arg);
return na;
};
/**
* remove and return element from array, if present
*
* @param val
* @returns {*}
*/
AP.remove = function(val) {
var idx = this.indexOf(val);
if (idx >= 0) return this.splice(idx,1);
return null;
};
/**
* return last array element if array length > 0
*
* @returns {*}
*/
AP.last = function() {
if (this.length === 0) return null;
return this[this.length-1];
};
AP.contains = function(val) {
return this.indexOf(val) >= 0;
};
AP.toFloat32 = function() {
var i = 0, f32 = new Float32Array(this.length);
while (i < this.length) {
f32[i] = this[i++];
}
return f32;
};
AP.forEachPair = function(fn, incr) {
var scope = this,
idx = 0,
inc = incr || 2,
len = scope.length;
while (idx < len) {
fn(scope[idx], scope[(idx+1)%len], idx);
idx += inc;
}
};
AP.show = function(fn,f) {
f = f || function(v) { console.log(v) };
this.forEach(function(av) {
f(av[fn]());
})
};
/** ******************************************************************
* string prototype helpers
******************************************************************* */
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
/** ******************************************************************
* Object static helpers
******************************************************************* */
Object.clone = function(o) {
return o ? JSON.parse(JSON.stringify(o)) : o;
};
Math.bound = function(val,min,max) {
return Math.max(min,Math.min(max,val));
};
})();

116
src/add/class.js Normal file
View file

@ -0,0 +1,116 @@
class DataWriter {
constructor(view, pos) {
this.view = view;
this.pos = pos || 0;
}
skip(v) {
let p = this.pos;
this.pos += v;
return p;
}
writeU8(v) {
try {
this.view.setUint8(this.pos, v);
return this.skip(1);
} catch (err) {
console.log({pos:this.pos, err})
throw err;
}
}
writeU16(v,le) {
this.view.setUint16(this.pos, v, le);
return this.skip(2);
}
writeU32(v,le) {
this.view.setUint32(this.pos, v, le);
return this.skip(4);
}
writeI32(v,le) {
this.view.setInt32(this.pos, v, le);
return this.skip(4);
}
writeUB64(v,le) {
this.view.setBigUint64(this.pos, v, le);
return this.skip(8);
}
writeB64(v,le) {
this.view.setBigInt64(this.pos, v, le);
return this.skip(8);
}
writeF32(v,le) {
this.view.setFloat32(this.pos, v, le);
return this.skip(4);
}
writeF64(v,le) {
this.view.setFloat64(this.pos, v, le);
return this.skip(8);
}
}
class DataReader {
constructor(view, pos) {
this.view = view;
this.pos = pos || 0;
}
skip(v) {
let p = this.pos;
this.pos += v;
return p;
}
readU8() {
return this.view.getUint8(this.pos++);
}
readU16(le) {
let v = this.view.getUint16(this.pos, le);
this.pos += 2;
return v;
}
readU32(le) {
let v = this.view.getUint32(this.pos, le);
this.pos += 4;
return v;
}
readI32(le) {
let v = this.view.getInt32(this.pos, le);
this.pos += 4;
return v;
}
readB64(le) {
let v = this.view.getBigInt64(this.pos, le);
this.pos += 8;
return v;
}
readUB64(le) {
let v = this.view.getBigUint64(this.pos, le);
this.pos += 8;
return v;
}
readF32(le) {
let v = this.view.getFloat32(this.pos, le);
this.pos += 4;
return v;
}
readF64(le) {
let v = this.view.getFloat64(this.pos, le);
this.pos += 8;
return v;
}
}

121
src/add/three.js Normal file
View file

@ -0,0 +1,121 @@
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
(function() {
var MP = THREE.Mesh.prototype,
GP = THREE.Geometry.prototype,
BP = THREE.BufferGeometry.prototype;
MP.getBoundingBox = function(update) {
return this.geometry.getBoundingBox(update);
};
MP.center = function() {
this.geometry.center();
return this;
};
MP.mirrorX = function() {
return this.mirror(0);
};
MP.mirrorY = function() {
return this.mirror(1);
};
MP.mirrorZ = function() {
return this.mirror(2);
};
// fast mirror of vertices given axis array offset
// array elements are repeating [x,y,z,x,y,z,...]
MP.mirror = function(start) {
var i,
geo = this.geometry,
at = geo.attributes,
pa = at.position.array,
nm = at.normal.array;
for (i = start || 0 ; i < pa.length; i += 3) {
pa[i] = -pa[i];
nm[i] = -nm[i];
}
geo.computeVertexNormals();
return this;
};
// center geometry on x,y,z coordinates (defaults to 0,0,0)
GP.center = function(x,y,z) {
var box = this.getBoundingBox(),
mid = box.dim.clone().multiplyScalar(0.5),
dif = mid.clone().add(box.min),
pos = this.attributes.position,
arr = pos.array,
maxx = Math.max(mid.x, mid.y, mid.z),
i = 0;
if (x) dif.x -= (maxx - mid.x) * x;
if (y) dif.y -= (maxx - mid.y) * y;
if (z) dif.z -= (maxx - mid.z) * z;
while (i < arr.length) {
arr[i++] -= dif.x;
arr[i++] -= dif.y;
arr[i++] -= dif.z;
}
pos.needsUpdate = true;
// force update of (obsolete) bounding box
this.getBoundingBox(true);
return this;
};
// return cached or refreshed (when update = true) bounding box
GP.getBoundingBox = function(update) {
if (update || !this.boundingBox) this.computeBoundingBox();
this.boundingBox.dim = this.boundingBox.max.clone().sub(this.boundingBox.min);
return this.boundingBox;
};
// uniformly scale any mesh to a max x/y/z dim of 'unit' (defaults to 1)
GP.unitScale = function(unit) {
var bbox = this.getBoundingBox().clone(),
scale = (unit || 1) / Math.max(
bbox.max.x - bbox.min.x,
bbox.max.y - bbox.min.y,
bbox.max.z - bbox.min.z
);
this.applyMatrix4(new THREE.Matrix4().makeScale(scale,scale,scale));
// force update of (obsolete) bounding box
this.getBoundingBox(true);
return this;
};
BP.center = GP.center;
BP.getBoundingBox = GP.getBoundingBox;
BP.unitScale = GP.unitScale;
BP.fixNormals = function() {
this.computeVertexNormals();
return this;
};
THREE.Geometry.fromVertices = function(vertices) {
var geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
return geometry;
};
THREE.Object3D.prototype.newGroup = function() {
var group = new THREE.Group();
this.add(group);
return group;
};
THREE.Object3D.prototype.removeAll = function() {
this.children.slice().forEach(function (c) {
c.parent = undefined;
c.dispatchEvent( { type: 'removed' } );
});
this.children = [];
};
})();