Permalink
Browse files

16-bit quantizer

  • Loading branch information...
1 parent 71531e5 commit a85dbe6a48fdb662e419395305d2fb2222f7e331 @kig committed May 8, 2012
Showing with 90 additions and 2 deletions.
  1. +22 −1 index.html
  2. +68 −1 shp.js
View
@@ -4,18 +4,38 @@
<script src="http://mrdoob.github.com/three.js/build/Three.js"></script>
<script src="shp.js"></script>
<script>
+ function save(buffer, name, mimetype) {
+ var bb = new WebKitBlobBuilder();
+ bb.append(buffer);
+ var b = bb.getBlob();
+ var url = webkitURL.createObjectURL(b);
+ var a = document.createElement('a');
+ a.download = name;
+ a.mimetype = mimetype;
+ a.href = url;
+ a.click();
+ webkitURL.revokeObjectURL(url);
+ }
+ </script>
+ <script>
parsedShp = null;
SHPParser.load('110m_land.shp',
function(res) {
console.log('ok', res);
parsedShp = res;
- var m = (new THREE.SHPLoader()).createModel(parsedShp, true);
+ var loader = new THREE.SHPLoader();
+ var m = loader.createModel(parsedShp, true);
console.log('created model', m);
+ var compressed = loader.compress(parsedShp);
+ //save(compressed.buffer, '110m_land.i16a')
+ console.log('16-bit quantized size', compressed.buffer.byteLength);
+ var n = loader.loadCompressed(compressed, true);
var r = new THREE.WebGLRenderer();
r.setSize(500,500);
document.body.appendChild(r.domElement);
var s = new THREE.Scene();
s.add(m);
+ s.add(n);
var c = new THREE.PerspectiveCamera(45, 1, 1, 10000);
c.position.z = 500;
c.position.y = 200;
@@ -25,6 +45,7 @@
var dt = t-t0;
t0 = t;
m.rotation.y += 0.002 * dt;
+ n.rotation.y += 0.002 * dt;
m.position.y = Math.abs(Math.sin(t / 500)) * 200 - 100;
c.lookAt(new THREE.Vector3(0,0,0));
r.render(s, c);
View
69 shp.js
@@ -145,7 +145,6 @@ p.createModel = function(shp, spherize) {
var points = r.content.points;
var parts = r.content.parts;
var poly = [];
- poly.push(new THREE.Vector2(x, y));
for (var k=0; k<parts.length; k++) {
poly = [];
for (var j=parts[k], last=parts[k+1]||(points.length/2); j<last; j++) {
@@ -188,5 +187,73 @@ p.createModel = function(shp, spherize) {
new THREE.MeshBasicMaterial({color: 0x88ff44, wireframe: true})
));
}
+ console.log('parsed', polys.length, lines.length);
return model;
};
+
+p.loadCompressed = function(compressed, spherize) {
+ var polys = [];
+ var lines = [];
+ var poly = [];
+ for (var i=0; i<compressed.length; i++) {
+ if (compressed[i] === -32768) {
+ console.log('new polyline', poly.length);
+ var geo = new THREE.Geometry();
+ geo.vertices = poly;
+ lines.push(geo);
+ poly = [];
+ continue;
+ }
+ var x = compressed[i] * 180 / 32767;
+ var y = compressed[i+1] * 180 / 32767;
+ i++;
+ if (spherize) {
+ var a = -x/180*Math.PI;
+ var t = y/180*Math.PI;
+ y = Math.sin(t) * 90;
+ x = Math.cos(a) * 90 * Math.cos(t);
+ var z = Math.sin(a) * 90 * Math.cos(t);
+ poly.push(new THREE.Vector3(x, y, z));
+ } else {
+ poly.push(new THREE.Vector3(x, y, 0));
+ }
+ }
+ var model = new THREE.Object3D();
+ for (var i=0; i<lines.length; i++) {
+ model.add(new THREE.Line(
+ lines[i],
+ new THREE.LineBasicMaterial({color: 'black', lineWidth: 2}),
+ THREE.LineStrip
+ ));
+ }
+ for (var i=0; i<polys.length; i++) {
+ model.add(new THREE.Mesh(
+ polys[i],
+ new THREE.MeshBasicMaterial({color: 0x88ff44, wireframe: true})
+ ));
+ }
+ console.log('parsed compressed', polys.length, lines.length);
+ return model;
+};
+
+p.compress = function(shp) {
+ var polys = [];
+ for (var i=0; i<shp.records.length; i++) {
+ var r = shp.records[i].shape;
+ if (r.type === SHP.POLYGON) {
+ var points = r.content.points;
+ var parts = r.content.parts;
+ for (var k=0; k<parts.length; k++) {
+ for (var j=parts[k], last=parts[k+1]||(points.length/2); j<last; j++) {
+ var x = points[j*2];
+ var y = points[j*2+1];
+ polys.push(x / 180 * 32767, y / 180 * 32767);
+ }
+ polys.push(-32768);
+ }
+ }
+ }
+ var i16a = new Int16Array(polys);
+ console.log(polys, i16a);
+ return i16a;
+};

0 comments on commit a85dbe6

Please sign in to comment.