laslaz.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // laslaz.js
  2. // LAS/LAZ loading
  3. //
  4. //var common = require("./common"),
  5. // Promise = require("bluebird");
  6. (function(scope) {
  7. "use strict";
  8. var pointFormatReaders = {
  9. 0: function(dv) {
  10. return {
  11. "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
  12. "intensity": dv.getUint16(12, true),
  13. "classification": dv.getUint8(16, true)
  14. };
  15. },
  16. 1: function(dv) {
  17. return {
  18. "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
  19. "intensity": dv.getUint16(12, true),
  20. "classification": dv.getUint8(16, true)
  21. };
  22. },
  23. 2: function(dv) {
  24. return {
  25. "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
  26. "intensity": dv.getUint16(12, true),
  27. "classification": dv.getUint8(16, true),
  28. "color": [dv.getUint16(20, true), dv.getUint16(22, true), dv.getUint16(24, true)]
  29. };
  30. },
  31. 3: function(dv) {
  32. return {
  33. "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
  34. "intensity": dv.getUint16(12, true),
  35. "classification": dv.getUint8(16, true),
  36. "color": [dv.getUint16(28, true), dv.getUint16(30, true), dv.getUint16(32, true)]
  37. };
  38. }
  39. };
  40. function readAs(buf, Type, offset, count) {
  41. count = (count === undefined || count === 0 ? 1 : count);
  42. var sub = buf.slice(offset, offset + Type.BYTES_PER_ELEMENT * count);
  43. var r = new Type(sub);
  44. if (count === undefined || count === 1)
  45. return r[0];
  46. var ret = [];
  47. for (var i = 0 ; i < count ; i ++) {
  48. ret.push(r[i]);
  49. }
  50. return ret;
  51. }
  52. function parseLASHeader(arraybuffer) {
  53. var o = {};
  54. o.pointsOffset = readAs(arraybuffer, Uint32Array, 32*3);
  55. o.pointsFormatId = readAs(arraybuffer, Uint8Array, 32*3+8);
  56. o.pointsStructSize = readAs(arraybuffer, Uint16Array, 32*3+8+1);
  57. o.pointsCount = readAs(arraybuffer, Uint32Array, 32*3 + 11);
  58. var start = 32*3 + 35;
  59. o.scale = readAs(arraybuffer, Float64Array, start, 3); start += 24; // 8*3
  60. o.offset = readAs(arraybuffer, Float64Array, start, 3); start += 24;
  61. var bounds = readAs(arraybuffer, Float64Array, start, 6); start += 48; // 8*6;
  62. o.maxs = [bounds[0], bounds[2], bounds[4]];
  63. o.mins = [bounds[1], bounds[3], bounds[5]];
  64. return o;
  65. }
  66. var msgIndex = 0;
  67. var waitHandlers = {};
  68. // This method is scope-wide since the nacl module uses this fuction to notify
  69. // us of events
  70. scope.handleMessage = function(message_event) {
  71. var msg = message_event.data;
  72. var resolver = waitHandlers[msg.id];
  73. delete waitHandlers[msg.id];
  74. // call the callback in a separate context, make sure we've cleaned our
  75. // state out before the callback is invoked since it may queue more doExchanges
  76. setTimeout(function() {
  77. if (msg.error)
  78. return resolver.reject(new Error(msg.message || "Unknown Error"));
  79. if (msg.hasOwnProperty('count') && msg.hasOwnProperty('hasMoreData')) {
  80. return resolver.resolve({
  81. buffer: msg.result,
  82. count: msg.count,
  83. hasMoreData: msg.hasMoreData});
  84. }
  85. resolver.resolve(msg.result);
  86. }, 0);
  87. };
  88. var doDataExchange = function(cmd, callback) {
  89. cmd.id = msgIndex.toString();
  90. msgIndex ++;
  91. var resolver = Promise.defer();
  92. waitHandlers[cmd.id] = resolver;
  93. nacl_module.postMessage(cmd);
  94. return resolver.promise.cancellable();
  95. };
  96. // LAS Loader
  97. // Loads uncompressed files
  98. //
  99. var LASLoader = function(arraybuffer) {
  100. this.arraybuffer = arraybuffer;
  101. };
  102. LASLoader.prototype.open = function() {
  103. // nothing needs to be done to open this file
  104. //
  105. this.readOffset = 0;
  106. return new Promise(function(res, rej) {
  107. setTimeout(res, 0);
  108. });
  109. };
  110. LASLoader.prototype.getHeader = function() {
  111. var o = this;
  112. return new Promise(function(res, rej) {
  113. setTimeout(function() {
  114. o.header = parseLASHeader(o.arraybuffer);
  115. res(o.header);
  116. }, 0);
  117. });
  118. };
  119. LASLoader.prototype.readData = function(count, offset, skip) {
  120. var o = this;
  121. return new Promise(function(res, rej) {
  122. setTimeout(function() {
  123. if (!o.header)
  124. return rej(new Error("Cannot start reading data till a header request is issued"));
  125. var start;
  126. if (skip <= 1) {
  127. count = Math.min(count, o.header.pointsCount - o.readOffset);
  128. start = o.header.pointsOffset + o.readOffset * o.header.pointsStructSize;
  129. var end = start + count * o.header.pointsStructSize;
  130. res({
  131. buffer: o.arraybuffer.slice(start, end),
  132. count: count,
  133. hasMoreData: o.readOffset + count < o.header.pointsCount});
  134. o.readOffset += count;
  135. }
  136. else {
  137. var pointsToRead = Math.min(count * skip, o.header.pointsCount - o.readOffset);
  138. var bufferSize = Math.ceil(pointsToRead / skip);
  139. var pointsRead = 0;
  140. var buf = new Uint8Array(bufferSize * o.header.pointsStructSize);
  141. for (var i = 0 ; i < pointsToRead ; i ++) {
  142. if (i % skip === 0) {
  143. start = o.header.pointsOffset + o.readOffset * o.header.pointsStructSize;
  144. var src = new Uint8Array(o.arraybuffer, start, o.header.pointsStructSize);
  145. buf.set(src, pointsRead * o.header.pointsStructSize);
  146. pointsRead ++;
  147. }
  148. o.readOffset ++;
  149. }
  150. res({
  151. buffer: buf.buffer,
  152. count: pointsRead,
  153. hasMoreData: o.readOffset < o.header.pointsCount
  154. });
  155. }
  156. }, 0);
  157. });
  158. };
  159. LASLoader.prototype.close = function() {
  160. var o = this;
  161. return new Promise(function(res, rej) {
  162. o.arraybuffer = null;
  163. setTimeout(res, 0);
  164. });
  165. };
  166. // LAZ Loader
  167. // Uses NaCL module to load LAZ files
  168. //
  169. var LAZLoader = function(arraybuffer) {
  170. this.arraybuffer = arraybuffer;
  171. let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
  172. this.ww = Potree.workerPool.getWorker(workerPath);
  173. this.nextCB = null;
  174. var o = this;
  175. this.ww.onmessage = function(e) {
  176. if (o.nextCB !== null) {
  177. o.nextCB(e.data);
  178. o.nextCB = null;
  179. }
  180. };
  181. this.dorr = function(req, cb) {
  182. o.nextCB = cb;
  183. o.ww.postMessage(req);
  184. };
  185. };
  186. LAZLoader.prototype.open = function() {
  187. // nothing needs to be done to open this file
  188. //
  189. var o = this;
  190. return new Promise(function(res, rej) {
  191. o.dorr({type:"open", arraybuffer: o.arraybuffer}, function(r) {
  192. if (r.status !== 1)
  193. return rej(new Error("Failed to open file"));
  194. res(true);
  195. });
  196. });
  197. };
  198. LAZLoader.prototype.getHeader = function() {
  199. var o = this;
  200. return new Promise(function(res, rej) {
  201. o.dorr({type:'header'}, function(r) {
  202. if (r.status !== 1)
  203. return rej(new Error("Failed to get header"));
  204. res(r.header);
  205. });
  206. });
  207. };
  208. LAZLoader.prototype.readData = function(count, offset, skip) {
  209. var o = this;
  210. return new Promise(function(res, rej) {
  211. o.dorr({type:'read', count: count, offset: offset, skip: skip}, function(r) {
  212. if (r.status !== 1)
  213. return rej(new Error("Failed to read data"));
  214. res({
  215. buffer: r.buffer,
  216. count: r.count,
  217. hasMoreData: r.hasMoreData
  218. });
  219. });
  220. });
  221. };
  222. LAZLoader.prototype.close = function() {
  223. var o = this;
  224. return new Promise(function(res, rej) {
  225. o.dorr({type:'close'}, function(r) {
  226. let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
  227. Potree.workerPool.returnWorker(workerPath, o.ww);
  228. if (r.status !== 1)
  229. return rej(new Error("Failed to close file"));
  230. res(true);
  231. });
  232. });
  233. };
  234. // A single consistent interface for loading LAS/LAZ files
  235. var LASFile = function(arraybuffer) {
  236. this.arraybuffer = arraybuffer;
  237. this.determineVersion();
  238. if (this.version > 12)
  239. throw new Error("Only file versions <= 1.2 are supported at this time");
  240. this.determineFormat();
  241. if (pointFormatReaders[this.formatId] === undefined)
  242. throw new Error("The point format ID is not supported");
  243. this.loader = this.isCompressed ?
  244. new LAZLoader(this.arraybuffer) :
  245. new LASLoader(this.arraybuffer);
  246. };
  247. LASFile.prototype.determineFormat = function() {
  248. var formatId = readAs(this.arraybuffer, Uint8Array, 32*3+8);
  249. var bit_7 = (formatId & 0x80) >> 7;
  250. var bit_6 = (formatId & 0x40) >> 6;
  251. if (bit_7 === 1 && bit_6 === 1)
  252. throw new Error("Old style compression not supported");
  253. this.formatId = formatId & 0x3f;
  254. this.isCompressed = (bit_7 === 1 || bit_6 === 1);
  255. };
  256. LASFile.prototype.determineVersion = function() {
  257. var ver = new Int8Array(this.arraybuffer, 24, 2);
  258. this.version = ver[0] * 10 + ver[1];
  259. this.versionAsString = ver[0] + "." + ver[1];
  260. };
  261. LASFile.prototype.open = function() {
  262. return this.loader.open();
  263. };
  264. LASFile.prototype.getHeader = function() {
  265. return this.loader.getHeader();
  266. };
  267. LASFile.prototype.readData = function(count, start, skip) {
  268. return this.loader.readData(count, start, skip);
  269. };
  270. LASFile.prototype.close = function() {
  271. return this.loader.close();
  272. };
  273. // Decodes LAS records into points
  274. //
  275. var LASDecoder = function(buffer, pointFormatID, pointSize, pointsCount, scale, offset, mins, maxs) {
  276. this.arrayb = buffer;
  277. this.decoder = pointFormatReaders[pointFormatID];
  278. this.pointsCount = pointsCount;
  279. this.pointSize = pointSize;
  280. this.scale = scale;
  281. this.offset = offset;
  282. this.mins = mins;
  283. this.maxs = maxs;
  284. };
  285. LASDecoder.prototype.getPoint = function(index) {
  286. if (index < 0 || index >= this.pointsCount)
  287. throw new Error("Point index out of range");
  288. var dv = new DataView(this.arrayb, index * this.pointSize, this.pointSize);
  289. return this.decoder(dv);
  290. };
  291. // NACL Module support
  292. // Called by the common.js module.
  293. //
  294. //window.startNaCl = function(name, tc, config, width, height) {
  295. // // check browser support for nacl
  296. // //
  297. // if(!common.browserSupportsNaCl()) {
  298. // return $.event.trigger({
  299. // type: "plasio.nacl.error",
  300. // message: "NaCl support is not available"
  301. // });
  302. // }
  303. // navigator.webkitPersistentStorage.requestQuota(2048 * 2048, function(bytes) {
  304. // common.updateStatus(
  305. // 'Allocated ' + bytes + ' bytes of persistant storage.');
  306. // common.attachDefaultListeners();
  307. // common.createNaClModule(name, tc, config, width, height);
  308. // },
  309. // function(e) {
  310. // $.event.trigger({
  311. // type: "plasio.nacl.error",
  312. // message: "Could not allocate persistant storage"
  313. // });
  314. // });
  315. // $(document).on("plasio.nacl.available", function() {
  316. // scope.LASModuleWasLoaded = true;
  317. // });
  318. //};
  319. scope.LAZLoader = LAZLoader;
  320. scope.LASLoader = LASLoader;
  321. scope.LASFile = LASFile;
  322. scope.LASDecoder = LASDecoder;
  323. scope.LASModuleWasLoaded = false;
  324. //})(module.exports);
  325. })(this);