@threepipe/plugin-gaussian-splatting 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/assets/worker-dK7pvmNj.js +1569 -0
  2. package/dist/assets/worker-dK7pvmNj.js.map +1 -0
  3. package/dist/dependencies.txt +212 -0
  4. package/dist/gaussian-splats-3d/GaussianSplatsPlugin.d.ts +1 -0
  5. package/dist/gaussian-splats-3d/GaussianSplatsPlugin.d.ts.map +1 -0
  6. package/dist/index.d.ts +5 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +879 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/index.mjs +876 -0
  11. package/dist/index.mjs.map +1 -0
  12. package/dist/three-gaussian-splat/ThreeGaussianSplatPlugin.d.ts +45 -0
  13. package/dist/three-gaussian-splat/ThreeGaussianSplatPlugin.d.ts.map +1 -0
  14. package/dist/three-gaussian-splat/cpp-sorter/BufferPool.d.ts +9 -0
  15. package/dist/three-gaussian-splat/cpp-sorter/BufferPool.d.ts.map +1 -0
  16. package/dist/three-gaussian-splat/cpp-sorter/SortWorkerManager.d.ts +13 -0
  17. package/dist/three-gaussian-splat/cpp-sorter/SortWorkerManager.d.ts.map +1 -0
  18. package/dist/three-gaussian-splat/cpp-sorter/worker.d.ts +18 -0
  19. package/dist/three-gaussian-splat/cpp-sorter/worker.d.ts.map +1 -0
  20. package/dist/three-gaussian-splat/geometry/GaussianSplatGeometry.d.ts +25 -0
  21. package/dist/three-gaussian-splat/geometry/GaussianSplatGeometry.d.ts.map +1 -0
  22. package/dist/three-gaussian-splat/index.d.ts +22 -0
  23. package/dist/three-gaussian-splat/index.d.ts.map +1 -0
  24. package/dist/three-gaussian-splat/loaders/SplatLoader.d.ts +13 -0
  25. package/dist/three-gaussian-splat/loaders/SplatLoader.d.ts.map +1 -0
  26. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialExtension.d.ts +28 -0
  27. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialExtension.d.ts.map +1 -0
  28. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialPhysical.d.ts +9 -0
  29. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialPhysical.d.ts.map +1 -0
  30. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialRaw.d.ts +12 -0
  31. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialRaw.d.ts.map +1 -0
  32. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialUnlit.d.ts +9 -0
  33. package/dist/three-gaussian-splat/materials/GaussianSplatMaterialUnlit.d.ts.map +1 -0
  34. package/dist/three-gaussian-splat/materials/util.d.ts +3 -0
  35. package/dist/three-gaussian-splat/materials/util.d.ts.map +1 -0
  36. package/dist/three-gaussian-splat/mesh/GaussianSplatMesh.d.ts +10 -0
  37. package/dist/three-gaussian-splat/mesh/GaussianSplatMesh.d.ts.map +1 -0
  38. package/dist/three-gaussian-splat/shaders/index.d.ts +7 -0
  39. package/dist/three-gaussian-splat/shaders/index.d.ts.map +1 -0
  40. package/package.json +34 -0
  41. package/src/gaussian-splats-3d/GaussianSplatsPlugin.ts +50 -0
  42. package/src/global.d.ts +40 -0
  43. package/src/index.ts +5 -0
  44. package/src/three-gaussian-splat/ThreeGaussianSplatPlugin.css +3 -0
  45. package/src/three-gaussian-splat/ThreeGaussianSplatPlugin.ts +74 -0
  46. package/src/three-gaussian-splat/cpp-sorter/BufferPool.ts +30 -0
  47. package/src/three-gaussian-splat/cpp-sorter/ISort.d.ts +3 -0
  48. package/src/three-gaussian-splat/cpp-sorter/SortWorkerManager.ts +53 -0
  49. package/src/three-gaussian-splat/cpp-sorter/build-in-docker.sh +4 -0
  50. package/src/three-gaussian-splat/cpp-sorter/build.sh +7 -0
  51. package/src/three-gaussian-splat/cpp-sorter/sort.cpp +107 -0
  52. package/src/three-gaussian-splat/cpp-sorter/sort.js +16 -0
  53. package/src/three-gaussian-splat/cpp-sorter/sort.wasm +0 -0
  54. package/src/three-gaussian-splat/cpp-sorter/watch.sh +9 -0
  55. package/src/three-gaussian-splat/cpp-sorter/worker.ts +59 -0
  56. package/src/three-gaussian-splat/geometry/GaussianSplatGeometry.ts +182 -0
  57. package/src/three-gaussian-splat/index.ts +22 -0
  58. package/src/three-gaussian-splat/loaders/SplatLoader.ts +51 -0
  59. package/src/three-gaussian-splat/materials/GaussianSplatMaterialExtension.ts +105 -0
  60. package/src/three-gaussian-splat/materials/GaussianSplatMaterialPhysical.ts +25 -0
  61. package/src/three-gaussian-splat/materials/GaussianSplatMaterialRaw.ts +73 -0
  62. package/src/three-gaussian-splat/materials/GaussianSplatMaterialUnlit.ts +26 -0
  63. package/src/three-gaussian-splat/materials/util.ts +9 -0
  64. package/src/three-gaussian-splat/mesh/GaussianSplatMesh.ts +96 -0
  65. package/src/three-gaussian-splat/shaders/gsplat.main.frag.glsl +18 -0
  66. package/src/three-gaussian-splat/shaders/gsplat.main.vert.glsl +15 -0
  67. package/src/three-gaussian-splat/shaders/gsplat.pars.frag.glsl +10 -0
  68. package/src/three-gaussian-splat/shaders/gsplat.pars.vert.glsl +58 -0
  69. package/src/three-gaussian-splat/shaders/index.ts +11 -0
@@ -0,0 +1,74 @@
1
+ import {AViewerPluginSync, createStyles, IGeometryEvent, ILoader, Importer, ThreeViewer} from 'threepipe'
2
+ import styles from './ThreeGaussianSplatPlugin.css?inline'
3
+ import {GaussianSplatMesh} from './index'
4
+ import {AnyOptions} from 'ts-browser-helpers'
5
+ import {SplatLoader} from './loaders/SplatLoader'
6
+ import {SortWorkerManager} from './cpp-sorter/SortWorkerManager'
7
+ import {GaussianSplatGeometry} from './geometry/GaussianSplatGeometry'
8
+
9
+ export class ThreeGaussianSplatPlugin extends AViewerPluginSync<string> {
10
+ public static readonly PluginType: string = 'ThreeGaussianSplatPlugin'
11
+ enabled = true
12
+ dependencies = []
13
+ toJSON: any = null
14
+
15
+ constructor() {
16
+ super()
17
+ }
18
+
19
+ splats: GaussianSplatMesh[] = []
20
+ private _ready = false
21
+ onAdded(viewer: ThreeViewer) {
22
+ super.onAdded(viewer)
23
+ createStyles(styles)
24
+ viewer.assetManager.importer.addImporter(this._importer)
25
+ viewer.scene.addEventListener('mainCameraUpdate', this._activeCameraUpdate)
26
+ viewer.scene.addEventListener('geometryUpdate', this._geometryUpdate)
27
+ this._ready = true
28
+ }
29
+
30
+ onRemove(viewer: ThreeViewer) {
31
+ viewer.assetManager.importer.removeImporter(this._importer)
32
+ }
33
+
34
+ private _activeCameraUpdate = () => {
35
+ if (!this._ready || this.isDisabled()) return
36
+ this.splats.forEach(async splat=>splat.update(this._viewer!.scene.mainCamera, this._viewer!.renderManager.webglRenderer))
37
+ }
38
+
39
+ private _geometryUpdate = (event: IGeometryEvent) => {
40
+ if (!this._ready || this.isDisabled() || !(event.geometry as GaussianSplatGeometry)?.isGaussianSplatGeometry) return
41
+ event.geometry!.appliedMeshes.forEach(async(splat: GaussianSplatMesh)=>splat.update ? splat.update(this._viewer!.scene.mainCamera, this._viewer!.renderManager.webglRenderer) : undefined)
42
+ }
43
+
44
+ private _sortWorkerManager = new SortWorkerManager() // todo: dispose?
45
+ protected _importer = new Importer(class extends SplatLoader implements ILoader {
46
+ onDispose: (mesh: GaussianSplatMesh)=>void = ()=>{return}
47
+ onCreate: (mesh: GaussianSplatMesh)=>void = ()=>{return}
48
+ transform(res: GaussianSplatMesh, _: AnyOptions): any {
49
+ res.addEventListener('dispose', ()=>this.onDispose(res))
50
+ this.onCreate(res)
51
+ return res
52
+ }
53
+ }, ['splat'], [], true, (l)=>{
54
+ if (!l) return l
55
+ l.sortWorkerManager = this._sortWorkerManager
56
+ l.onDispose = (mesh: GaussianSplatMesh)=>{ // todo: dispose should only remove from GPU?
57
+ this.splats = this.splats.filter(splat=>splat !== mesh)
58
+ }
59
+ l.onCreate = (mesh: GaussianSplatMesh)=>{
60
+ this.splats.push(mesh)
61
+ }
62
+ l.onGeometryLoad = (_)=>{
63
+ // console.log('geometry loaded')
64
+ // console.log(geometry.boundingBox)
65
+ this._viewer?.setDirty()
66
+ }
67
+ return l
68
+ })
69
+
70
+ // protected _viewerListeners = {
71
+ // preRender: (_: IViewerEvent) => {
72
+ // },
73
+ // }
74
+ }
@@ -0,0 +1,30 @@
1
+ export class BufferPool {
2
+ private pool: ArrayBuffer[];
3
+ private bufferSize: number;
4
+
5
+ constructor(bufferSize: number, initialPoolSize: number = 0) {
6
+ this.bufferSize = bufferSize;
7
+ this.pool = [];
8
+ this.initPool(initialPoolSize);
9
+ }
10
+
11
+ private initPool(initialPoolSize: number): void {
12
+ for (let i = 0; i < initialPoolSize; i++) {
13
+ this.pool.push(new ArrayBuffer(this.bufferSize));
14
+ }
15
+ }
16
+
17
+ public getBuffer(): ArrayBuffer {
18
+ if (this.pool.length > 0) {
19
+ return this.pool.pop()!;
20
+ } else {
21
+ return new ArrayBuffer(this.bufferSize);
22
+ }
23
+ }
24
+
25
+ public returnBuffer(buffer: ArrayBuffer): void {
26
+ if (buffer.byteLength === this.bufferSize) {
27
+ this.pool.push(buffer);
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,3 @@
1
+ export interface MainModule {
2
+ runSort(_0: number, _1: number, _2: number, _3: number): void;
3
+ }
@@ -0,0 +1,53 @@
1
+ import {Remote, transfer, wrap} from 'comlink'
2
+ import type {WasmSorter} from './worker'
3
+
4
+ export const SPLAT_ROW_LENGTH = 3 * 4 + 3 * 4 + 4 + 4
5
+
6
+ function trimBuffer(_buffer: Uint8Array, _maxSplats: number, _vertexCount: number): {buffer: Uint8Array; vertexCount: number} {
7
+ const actualVertexCount = Math.min(_vertexCount, _maxSplats)
8
+ const actualBufferSize = SPLAT_ROW_LENGTH * actualVertexCount
9
+ const buffer = _buffer.slice(0, actualBufferSize)
10
+ return {buffer, vertexCount: actualVertexCount}
11
+ }
12
+
13
+ export class SortWorkerManager {
14
+ private _workers: Remote<WasmSorter>[] = []
15
+ private _maxWorkers = 8
16
+
17
+ private _workerCtor: new (vertexCount: number, globalBuffer: Uint8Array) => Remote<WasmSorter>
18
+
19
+ onError = (e: any) => {
20
+ console.error(e)
21
+ console.error(
22
+ 'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message
23
+ )
24
+ }
25
+
26
+ constructor() {
27
+ const worker = new Worker(new URL('../cpp-sorter/worker', import.meta.url), {type: 'module'})
28
+ worker.addEventListener('error', this.onError, false)
29
+ this._workerCtor = wrap(worker) as any
30
+ }
31
+
32
+ async createWorker(data: Uint8Array, maxSplats = 1000000) {
33
+ if (this._workers.length < this._maxWorkers) {
34
+ const vertexCount = Math.floor(data.length / SPLAT_ROW_LENGTH)
35
+ const bufferInfo = trimBuffer(data, maxSplats, vertexCount)
36
+ const globalBuffer = transfer(bufferInfo.buffer, [bufferInfo.buffer.buffer])
37
+ const worker = await new this._workerCtor(vertexCount, globalBuffer)
38
+ await worker.load()
39
+ this._workers.push(worker)
40
+ return worker
41
+ }
42
+ console.error('Max workers reached')
43
+ throw new Error('Max workers reached')
44
+ }
45
+
46
+ async disposeWorker(worker: Remote<WasmSorter>) {
47
+ const index = this._workers.indexOf(worker)
48
+ if (index !== -1) {
49
+ this._workers.splice(index, 1)
50
+ await worker.dispose()
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+ set -e
3
+ DOCKER_IMAGE="emscripten/emsdk:3.1.50"
4
+ docker run --rm -v "$(pwd)":/src -w /src $DOCKER_IMAGE bash build.sh
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ rm -rf sort.wasm sort.js sort.d.ts
5
+ rm -rf pthread_sort.wasm pthread_sort.js pthread_sort.worker.js
6
+ em++ -pthread -O3 -s EXPORT_ES6=1 -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s ENVIRONMENT=web,worker -flto --embind-emit-tsd=ISort.d.ts sort.cpp -o pthread_sort.js -s WASM=1 -lembind -s EXPORTED_FUNCTIONS='["_malloc", "_free"]' -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1
7
+ em++ -O3 -s EXPORT_ES6=1 -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s ENVIRONMENT=web,worker -flto --embind-emit-tsd=ISort.d.ts sort.cpp -o sort.js -s WASM=1 -lembind -s EXPORTED_FUNCTIONS='["_malloc", "_free"]' -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1
@@ -0,0 +1,107 @@
1
+ #include <emscripten/bind.h>
2
+ #include <algorithm>
3
+ #include <cstdint>
4
+ #include <cmath>
5
+ #include <vector>
6
+ #include <numeric>
7
+ #include <stdio.h>
8
+ #include <cstring>
9
+
10
+
11
+ union DepthIndex {
12
+ struct {
13
+ float depth;
14
+ uint32_t index;
15
+ };
16
+
17
+ bool operator < (const DepthIndex& other) const {
18
+ return depth < other.depth;
19
+ }
20
+ };
21
+
22
+ uint32_t floatToUInt(float f) {
23
+ uint32_t u;
24
+ std::memcpy(&u, &f, sizeof(float));
25
+ uint32_t mask = -int32_t(u >> 31) | 0x80000000;
26
+ return u ^ mask;
27
+ }
28
+
29
+ void radixSort(std::vector<DepthIndex>& depthIndices) {
30
+ const int n = depthIndices.size();
31
+ std::vector<DepthIndex> temp(n);
32
+
33
+ for (int shift = 0; shift < 32; shift += 8) {
34
+ size_t count[256] = {};
35
+ for (auto& di : depthIndices) {
36
+ count[(floatToUInt(di.depth) >> shift) & 0xFF]++;
37
+ }
38
+ size_t pos[256];
39
+ pos[0] = 0;
40
+ for (int i = 1; i < 256; i++) {
41
+ pos[i] = pos[i - 1] + count[i - 1];
42
+ }
43
+ for (auto& di : depthIndices) {
44
+ int index = (floatToUInt(di.depth) >> shift) & 0xFF;
45
+ temp[pos[index]++] = di;
46
+ }
47
+ depthIndices.swap(temp);
48
+ }
49
+ }
50
+
51
+
52
+ using namespace emscripten;
53
+
54
+
55
+ void runSort(int viewProjPtr, int bufferPtr, int vertexCount, int combinedPtr) {
56
+ auto *viewProj = reinterpret_cast<float *>(viewProjPtr);
57
+ uint8_t *buffer = reinterpret_cast<uint8_t *>(bufferPtr);
58
+ auto *combined = reinterpret_cast<float *>(combinedPtr);
59
+
60
+ // Calculate lengths based on vertexCount
61
+ int quatLength = 4 * vertexCount;
62
+ int scaleLength = 3 * vertexCount;
63
+ int centerLength = 3 * vertexCount;
64
+ int colorLength = 4 * vertexCount;
65
+
66
+ // Calculate offsets for each array within the combined array
67
+ int quatOffset = 0;
68
+ int scaleOffset = quatOffset + quatLength;
69
+ int centerOffset = scaleOffset + scaleLength;
70
+ int colorOffset = centerOffset + centerLength;
71
+
72
+ std::vector<DepthIndex> depthIndices(vertexCount);
73
+ for (int i = 0; i < vertexCount; ++i) {
74
+ auto *f_buffer = reinterpret_cast<float *>(buffer + 32 * i);
75
+ depthIndices[i].depth = 10000 - (viewProj[2] * f_buffer[0] + viewProj[6] * f_buffer[1] + viewProj[10] * f_buffer[2]);
76
+ depthIndices[i].index = i;
77
+ }
78
+
79
+ radixSort(depthIndices);
80
+
81
+ for (int i = 0; i < vertexCount; ++i) {
82
+ uint32_t index = depthIndices[i].index;
83
+ auto *f_buffer = reinterpret_cast<float *>(buffer + 32 * index);
84
+
85
+ combined[quatOffset + 4 * i + 0] = (buffer[32 * index + 28 + 0] - 128) / 128.0f;
86
+ combined[quatOffset + 4 * i + 1] = (buffer[32 * index + 28 + 1] - 128) / 128.0f;
87
+ combined[quatOffset + 4 * i + 2] = (buffer[32 * index + 28 + 2] - 128) / 128.0f;
88
+ combined[quatOffset + 4 * i + 3] = (buffer[32 * index + 28 + 3] - 128) / 128.0f;
89
+
90
+ combined[centerOffset + 3 * i + 0] = f_buffer[0];
91
+ combined[centerOffset + 3 * i + 1] = f_buffer[1];
92
+ combined[centerOffset + 3 * i + 2] = f_buffer[2];
93
+
94
+ combined[colorOffset + 4 * i + 0] = buffer[32 * index + 24 + 0] / 255.0f;
95
+ combined[colorOffset + 4 * i + 1] = buffer[32 * index + 24 + 1] / 255.0f;
96
+ combined[colorOffset + 4 * i + 2] = buffer[32 * index + 24 + 2] / 255.0f;
97
+ combined[colorOffset + 4 * i + 3] = buffer[32 * index + 24 + 3] / 255.0f;
98
+
99
+ combined[scaleOffset + 3 * i + 0] = f_buffer[3];
100
+ combined[scaleOffset + 3 * i + 1] = f_buffer[4];
101
+ combined[scaleOffset + 3 * i + 2] = f_buffer[5];
102
+ }
103
+ }
104
+
105
+ EMSCRIPTEN_BINDINGS(my_module) {
106
+ function("runSort", &runSort);
107
+ }
@@ -0,0 +1,16 @@
1
+
2
+ var Module = (() => {
3
+ var _scriptDir = import.meta.url;
4
+
5
+ return (
6
+ function(moduleArg = {}) {
7
+
8
+ var Module=moduleArg;var readyPromiseResolve,readyPromiseReject;Module["ready"]=new Promise((resolve,reject)=>{readyPromiseResolve=resolve;readyPromiseReject=reject});var moduleOverrides=Object.assign({},Module);var arguments_=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var ENVIRONMENT_IS_WEB=typeof window=="object";var ENVIRONMENT_IS_WORKER=typeof importScripts=="function";var ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var read_,readAsync,readBinary;if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){if(ENVIRONMENT_IS_WORKER){scriptDirectory=self.location.href}else if(typeof document!="undefined"&&document.currentScript){scriptDirectory=document.currentScript.src}if(_scriptDir){scriptDirectory=_scriptDir}if(scriptDirectory.indexOf("blob:")!==0){scriptDirectory=scriptDirectory.substr(0,scriptDirectory.replace(/[?#].*/,"").lastIndexOf("/")+1)}else{scriptDirectory=""}{read_=url=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.send(null);return xhr.responseText};if(ENVIRONMENT_IS_WORKER){readBinary=url=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}}readAsync=(url,onload,onerror)=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,true);xhr.responseType="arraybuffer";xhr.onload=()=>{if(xhr.status==200||xhr.status==0&&xhr.response){onload(xhr.response);return}onerror()};xhr.onerror=onerror;xhr.send(null)}}}else{}var out=Module["print"]||console.log.bind(console);var err=Module["printErr"]||console.error.bind(console);Object.assign(Module,moduleOverrides);moduleOverrides=null;if(Module["arguments"])arguments_=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];if(Module["quit"])quit_=Module["quit"];var wasmBinary;if(Module["wasmBinary"])wasmBinary=Module["wasmBinary"];if(typeof WebAssembly!="object"){abort("no native wasm support detected")}var wasmMemory;var ABORT=false;var EXITSTATUS;var HEAP8,HEAPU8,HEAP16,HEAPU16,HEAP32,HEAPU32,HEAPF32,HEAPF64;function updateMemoryViews(){var b=wasmMemory.buffer;Module["HEAP8"]=HEAP8=new Int8Array(b);Module["HEAP16"]=HEAP16=new Int16Array(b);Module["HEAPU8"]=HEAPU8=new Uint8Array(b);Module["HEAPU16"]=HEAPU16=new Uint16Array(b);Module["HEAP32"]=HEAP32=new Int32Array(b);Module["HEAPU32"]=HEAPU32=new Uint32Array(b);Module["HEAPF32"]=HEAPF32=new Float32Array(b);Module["HEAPF64"]=HEAPF64=new Float64Array(b)}var __ATPRERUN__=[];var __ATINIT__=[];var __ATPOSTRUN__=[];var runtimeInitialized=false;function preRun(){if(Module["preRun"]){if(typeof Module["preRun"]=="function")Module["preRun"]=[Module["preRun"]];while(Module["preRun"].length){addOnPreRun(Module["preRun"].shift())}}callRuntimeCallbacks(__ATPRERUN__)}function initRuntime(){runtimeInitialized=true;callRuntimeCallbacks(__ATINIT__)}function postRun(){if(Module["postRun"]){if(typeof Module["postRun"]=="function")Module["postRun"]=[Module["postRun"]];while(Module["postRun"].length){addOnPostRun(Module["postRun"].shift())}}callRuntimeCallbacks(__ATPOSTRUN__)}function addOnPreRun(cb){__ATPRERUN__.unshift(cb)}function addOnInit(cb){__ATINIT__.unshift(cb)}function addOnPostRun(cb){__ATPOSTRUN__.unshift(cb)}var runDependencies=0;var runDependencyWatcher=null;var dependenciesFulfilled=null;function addRunDependency(id){runDependencies++;if(Module["monitorRunDependencies"]){Module["monitorRunDependencies"](runDependencies)}}function removeRunDependency(id){runDependencies--;if(Module["monitorRunDependencies"]){Module["monitorRunDependencies"](runDependencies)}if(runDependencies==0){if(runDependencyWatcher!==null){clearInterval(runDependencyWatcher);runDependencyWatcher=null}if(dependenciesFulfilled){var callback=dependenciesFulfilled;dependenciesFulfilled=null;callback()}}}function abort(what){if(Module["onAbort"]){Module["onAbort"](what)}what="Aborted("+what+")";err(what);ABORT=true;EXITSTATUS=1;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);readyPromiseReject(e);throw e}var dataURIPrefix="data:application/octet-stream;base64,";var isDataURI=filename=>filename.startsWith(dataURIPrefix);var wasmBinaryFile;if(Module["locateFile"]){wasmBinaryFile="sort.wasm";if(!isDataURI(wasmBinaryFile)){wasmBinaryFile=locateFile(wasmBinaryFile)}}else{wasmBinaryFile=new URL("sort.wasm",import.meta.url).href}function getBinarySync(file){if(file==wasmBinaryFile&&wasmBinary){return new Uint8Array(wasmBinary)}if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}function getBinaryPromise(binaryFile){if(!wasmBinary&&(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER)){if(typeof fetch=="function"){return fetch(binaryFile,{credentials:"same-origin"}).then(response=>{if(!response["ok"]){throw"failed to load wasm binary file at '"+binaryFile+"'"}return response["arrayBuffer"]()}).catch(()=>getBinarySync(binaryFile))}}return Promise.resolve().then(()=>getBinarySync(binaryFile))}function instantiateArrayBuffer(binaryFile,imports,receiver){return getBinaryPromise(binaryFile).then(binary=>WebAssembly.instantiate(binary,imports)).then(instance=>instance).then(receiver,reason=>{err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)})}function instantiateAsync(binary,binaryFile,imports,callback){if(!binary&&typeof WebAssembly.instantiateStreaming=="function"&&!isDataURI(binaryFile)&&typeof fetch=="function"){return fetch(binaryFile,{credentials:"same-origin"}).then(response=>{var result=WebAssembly.instantiateStreaming(response,imports);return result.then(callback,function(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation");return instantiateArrayBuffer(binaryFile,imports,callback)})})}return instantiateArrayBuffer(binaryFile,imports,callback)}function createWasm(){var info={"a":wasmImports};function receiveInstance(instance,module){wasmExports=instance.exports;wasmMemory=wasmExports["o"];updateMemoryViews();wasmTable=wasmExports["s"];addOnInit(wasmExports["p"]);removeRunDependency("wasm-instantiate");return wasmExports}addRunDependency("wasm-instantiate");function receiveInstantiationResult(result){receiveInstance(result["instance"])}if(Module["instantiateWasm"]){try{return Module["instantiateWasm"](info,receiveInstance)}catch(e){err(`Module.instantiateWasm callback failed with error: ${e}`);readyPromiseReject(e)}}instantiateAsync(wasmBinary,wasmBinaryFile,info,receiveInstantiationResult).catch(readyPromiseReject);return{}}var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var noExitRuntime=Module["noExitRuntime"]||true;function ExceptionInfo(excPtr){this.excPtr=excPtr;this.ptr=excPtr-24;this.set_type=function(type){HEAPU32[this.ptr+4>>2]=type};this.get_type=function(){return HEAPU32[this.ptr+4>>2]};this.set_destructor=function(destructor){HEAPU32[this.ptr+8>>2]=destructor};this.get_destructor=function(){return HEAPU32[this.ptr+8>>2]};this.set_caught=function(caught){caught=caught?1:0;HEAP8[this.ptr+12>>0]=caught};this.get_caught=function(){return HEAP8[this.ptr+12>>0]!=0};this.set_rethrown=function(rethrown){rethrown=rethrown?1:0;HEAP8[this.ptr+13>>0]=rethrown};this.get_rethrown=function(){return HEAP8[this.ptr+13>>0]!=0};this.init=function(type,destructor){this.set_adjusted_ptr(0);this.set_type(type);this.set_destructor(destructor)};this.set_adjusted_ptr=function(adjustedPtr){HEAPU32[this.ptr+16>>2]=adjustedPtr};this.get_adjusted_ptr=function(){return HEAPU32[this.ptr+16>>2]};this.get_exception_ptr=function(){var isPointer=___cxa_is_pointer_type(this.get_type());if(isPointer){return HEAPU32[this.excPtr>>2]}var adjusted=this.get_adjusted_ptr();if(adjusted!==0)return adjusted;return this.excPtr}}var exceptionLast=0;var uncaughtExceptionCount=0;var ___cxa_throw=(ptr,type,destructor)=>{var info=new ExceptionInfo(ptr);info.init(type,destructor);exceptionLast=ptr;uncaughtExceptionCount++;throw exceptionLast};var __embind_register_bigint=(primitiveType,name,size,minRange,maxRange)=>{};var embind_init_charCodes=()=>{var codes=new Array(256);for(var i=0;i<256;++i){codes[i]=String.fromCharCode(i)}embind_charCodes=codes};var embind_charCodes;var readLatin1String=ptr=>{var ret="";var c=ptr;while(HEAPU8[c]){ret+=embind_charCodes[HEAPU8[c++]]}return ret};var awaitingDependencies={};var registeredTypes={};var typeDependencies={};var BindingError;var throwBindingError=message=>{throw new BindingError(message)};var InternalError;var throwInternalError=message=>{throw new InternalError(message)};var whenDependentTypesAreResolved=(myTypes,dependentTypes,getTypeConverters)=>{myTypes.forEach(function(type){typeDependencies[type]=dependentTypes});function onComplete(typeConverters){var myTypeConverters=getTypeConverters(typeConverters);if(myTypeConverters.length!==myTypes.length){throwInternalError("Mismatched type converter count")}for(var i=0;i<myTypes.length;++i){registerType(myTypes[i],myTypeConverters[i])}}var typeConverters=new Array(dependentTypes.length);var unregisteredTypes=[];var registered=0;dependentTypes.forEach((dt,i)=>{if(registeredTypes.hasOwnProperty(dt)){typeConverters[i]=registeredTypes[dt]}else{unregisteredTypes.push(dt);if(!awaitingDependencies.hasOwnProperty(dt)){awaitingDependencies[dt]=[]}awaitingDependencies[dt].push(()=>{typeConverters[i]=registeredTypes[dt];++registered;if(registered===unregisteredTypes.length){onComplete(typeConverters)}})}});if(0===unregisteredTypes.length){onComplete(typeConverters)}};function sharedRegisterType(rawType,registeredInstance,options={}){var name=registeredInstance.name;if(!rawType){throwBindingError(`type "${name}" must have a positive integer typeid pointer`)}if(registeredTypes.hasOwnProperty(rawType)){if(options.ignoreDuplicateRegistrations){return}else{throwBindingError(`Cannot register type '${name}' twice`)}}registeredTypes[rawType]=registeredInstance;delete typeDependencies[rawType];if(awaitingDependencies.hasOwnProperty(rawType)){var callbacks=awaitingDependencies[rawType];delete awaitingDependencies[rawType];callbacks.forEach(cb=>cb())}}function registerType(rawType,registeredInstance,options={}){if(!("argPackAdvance"in registeredInstance)){throw new TypeError("registerType registeredInstance requires argPackAdvance")}return sharedRegisterType(rawType,registeredInstance,options)}var GenericWireTypeSize=8;var __embind_register_bool=(rawType,name,trueValue,falseValue)=>{name=readLatin1String(name);registerType(rawType,{name:name,"fromWireType":function(wt){return!!wt},"toWireType":function(destructors,o){return o?trueValue:falseValue},"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":function(pointer){return this["fromWireType"](HEAPU8[pointer])},destructorFunction:null})};function handleAllocatorInit(){Object.assign(HandleAllocator.prototype,{get(id){return this.allocated[id]},has(id){return this.allocated[id]!==undefined},allocate(handle){var id=this.freelist.pop()||this.allocated.length;this.allocated[id]=handle;return id},free(id){this.allocated[id]=undefined;this.freelist.push(id)}})}function HandleAllocator(){this.allocated=[undefined];this.freelist=[]}var emval_handles=new HandleAllocator;var __emval_decref=handle=>{if(handle>=emval_handles.reserved&&0===--emval_handles.get(handle).refcount){emval_handles.free(handle)}};var count_emval_handles=()=>{var count=0;for(var i=emval_handles.reserved;i<emval_handles.allocated.length;++i){if(emval_handles.allocated[i]!==undefined){++count}}return count};var init_emval=()=>{emval_handles.allocated.push({value:undefined},{value:null},{value:true},{value:false});emval_handles.reserved=emval_handles.allocated.length;Module["count_emval_handles"]=count_emval_handles};var Emval={toValue:handle=>{if(!handle){throwBindingError("Cannot use deleted val. handle = "+handle)}return emval_handles.get(handle).value},toHandle:value=>{switch(value){case undefined:return 1;case null:return 2;case true:return 3;case false:return 4;default:{return emval_handles.allocate({refcount:1,value:value})}}}};function simpleReadValueFromPointer(pointer){return this["fromWireType"](HEAP32[pointer>>2])}var __embind_register_emval=(rawType,name)=>{name=readLatin1String(name);registerType(rawType,{name:name,"fromWireType":handle=>{var rv=Emval.toValue(handle);__emval_decref(handle);return rv},"toWireType":(destructors,value)=>Emval.toHandle(value),"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":simpleReadValueFromPointer,destructorFunction:null})};var floatReadValueFromPointer=(name,width)=>{switch(width){case 4:return function(pointer){return this["fromWireType"](HEAPF32[pointer>>2])};case 8:return function(pointer){return this["fromWireType"](HEAPF64[pointer>>3])};default:throw new TypeError(`invalid float width (${width}): ${name}`)}};var __embind_register_float=(rawType,name,size)=>{name=readLatin1String(name);registerType(rawType,{name:name,"fromWireType":value=>value,"toWireType":(destructors,value)=>value,"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":floatReadValueFromPointer(name,size),destructorFunction:null})};var createNamedFunction=(name,body)=>Object.defineProperty(body,"name",{value:name});var runDestructors=destructors=>{while(destructors.length){var ptr=destructors.pop();var del=destructors.pop();del(ptr)}};function newFunc(constructor,argumentList){if(!(constructor instanceof Function)){throw new TypeError(`new_ called with constructor type ${typeof constructor} which is not a function`)}var dummy=createNamedFunction(constructor.name||"unknownFunctionName",function(){});dummy.prototype=constructor.prototype;var obj=new dummy;var r=constructor.apply(obj,argumentList);return r instanceof Object?r:obj}function craftInvokerFunction(humanName,argTypes,classType,cppInvokerFunc,cppTargetFunc,isAsync){var argCount=argTypes.length;if(argCount<2){throwBindingError("argTypes array size mismatch! Must at least get return value and 'this' types!")}var isClassMethodFunc=argTypes[1]!==null&&classType!==null;var needsDestructorStack=false;for(var i=1;i<argTypes.length;++i){if(argTypes[i]!==null&&argTypes[i].destructorFunction===undefined){needsDestructorStack=true;break}}var returns=argTypes[0].name!=="void";var argsList="";var argsListWired="";for(var i=0;i<argCount-2;++i){argsList+=(i!==0?", ":"")+"arg"+i;argsListWired+=(i!==0?", ":"")+"arg"+i+"Wired"}var invokerFnBody=`\n return function (${argsList}) {\n if (arguments.length !== ${argCount-2}) {\n throwBindingError('function ${humanName} called with ' + arguments.length + ' arguments, expected ${argCount-2}');\n }`;if(needsDestructorStack){invokerFnBody+="var destructors = [];\n"}var dtorStack=needsDestructorStack?"destructors":"null";var args1=["throwBindingError","invoker","fn","runDestructors","retType","classParam"];var args2=[throwBindingError,cppInvokerFunc,cppTargetFunc,runDestructors,argTypes[0],argTypes[1]];if(isClassMethodFunc){invokerFnBody+="var thisWired = classParam.toWireType("+dtorStack+", this);\n"}for(var i=0;i<argCount-2;++i){invokerFnBody+="var arg"+i+"Wired = argType"+i+".toWireType("+dtorStack+", arg"+i+"); // "+argTypes[i+2].name+"\n";args1.push("argType"+i);args2.push(argTypes[i+2])}if(isClassMethodFunc){argsListWired="thisWired"+(argsListWired.length>0?", ":"")+argsListWired}invokerFnBody+=(returns||isAsync?"var rv = ":"")+"invoker(fn"+(argsListWired.length>0?", ":"")+argsListWired+");\n";if(needsDestructorStack){invokerFnBody+="runDestructors(destructors);\n"}else{for(var i=isClassMethodFunc?1:2;i<argTypes.length;++i){var paramName=i===1?"thisWired":"arg"+(i-2)+"Wired";if(argTypes[i].destructorFunction!==null){invokerFnBody+=paramName+"_dtor("+paramName+"); // "+argTypes[i].name+"\n";args1.push(paramName+"_dtor");args2.push(argTypes[i].destructorFunction)}}}if(returns){invokerFnBody+="var ret = retType.fromWireType(rv);\n"+"return ret;\n"}else{}invokerFnBody+="}\n";args1.push(invokerFnBody);var invokerFn=newFunc(Function,args1).apply(null,args2);return createNamedFunction(humanName,invokerFn)}var ensureOverloadTable=(proto,methodName,humanName)=>{if(undefined===proto[methodName].overloadTable){var prevFunc=proto[methodName];proto[methodName]=function(){if(!proto[methodName].overloadTable.hasOwnProperty(arguments.length)){throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${arguments.length}) - expects one of (${proto[methodName].overloadTable})!`)}return proto[methodName].overloadTable[arguments.length].apply(this,arguments)};proto[methodName].overloadTable=[];proto[methodName].overloadTable[prevFunc.argCount]=prevFunc}};var exposePublicSymbol=(name,value,numArguments)=>{if(Module.hasOwnProperty(name)){if(undefined===numArguments||undefined!==Module[name].overloadTable&&undefined!==Module[name].overloadTable[numArguments]){throwBindingError(`Cannot register public name '${name}' twice`)}ensureOverloadTable(Module,name,name);if(Module.hasOwnProperty(numArguments)){throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`)}Module[name].overloadTable[numArguments]=value}else{Module[name]=value;if(undefined!==numArguments){Module[name].numArguments=numArguments}}};var heap32VectorToArray=(count,firstElement)=>{var array=[];for(var i=0;i<count;i++){array.push(HEAPU32[firstElement+i*4>>2])}return array};var replacePublicSymbol=(name,value,numArguments)=>{if(!Module.hasOwnProperty(name)){throwInternalError("Replacing nonexistant public symbol")}if(undefined!==Module[name].overloadTable&&undefined!==numArguments){Module[name].overloadTable[numArguments]=value}else{Module[name]=value;Module[name].argCount=numArguments}};var dynCallLegacy=(sig,ptr,args)=>{var f=Module["dynCall_"+sig];return args&&args.length?f.apply(null,[ptr].concat(args)):f.call(null,ptr)};var wasmTableMirror=[];var wasmTable;var getWasmTableEntry=funcPtr=>{var func=wasmTableMirror[funcPtr];if(!func){if(funcPtr>=wasmTableMirror.length)wasmTableMirror.length=funcPtr+1;wasmTableMirror[funcPtr]=func=wasmTable.get(funcPtr)}return func};var dynCall=(sig,ptr,args)=>{if(sig.includes("j")){return dynCallLegacy(sig,ptr,args)}var rtn=getWasmTableEntry(ptr).apply(null,args);return rtn};var getDynCaller=(sig,ptr)=>{var argCache=[];return function(){argCache.length=0;Object.assign(argCache,arguments);return dynCall(sig,ptr,argCache)}};var embind__requireFunction=(signature,rawFunction)=>{signature=readLatin1String(signature);function makeDynCaller(){if(signature.includes("j")){return getDynCaller(signature,rawFunction)}return getWasmTableEntry(rawFunction)}var fp=makeDynCaller();if(typeof fp!="function"){throwBindingError(`unknown function pointer with signature ${signature}: ${rawFunction}`)}return fp};var extendError=(baseErrorType,errorName)=>{var errorClass=createNamedFunction(errorName,function(message){this.name=errorName;this.message=message;var stack=new Error(message).stack;if(stack!==undefined){this.stack=this.toString()+"\n"+stack.replace(/^Error(:[^\n]*)?\n/,"")}});errorClass.prototype=Object.create(baseErrorType.prototype);errorClass.prototype.constructor=errorClass;errorClass.prototype.toString=function(){if(this.message===undefined){return this.name}else{return`${this.name}: ${this.message}`}};return errorClass};var UnboundTypeError;var getTypeName=type=>{var ptr=___getTypeName(type);var rv=readLatin1String(ptr);_free(ptr);return rv};var throwUnboundTypeError=(message,types)=>{var unboundTypes=[];var seen={};function visit(type){if(seen[type]){return}if(registeredTypes[type]){return}if(typeDependencies[type]){typeDependencies[type].forEach(visit);return}unboundTypes.push(type);seen[type]=true}types.forEach(visit);throw new UnboundTypeError(`${message}: `+unboundTypes.map(getTypeName).join([", "]))};var getFunctionName=signature=>{signature=signature.trim();const argsIndex=signature.indexOf("(");if(argsIndex!==-1){return signature.substr(0,argsIndex)}else{return signature}};var __embind_register_function=(name,argCount,rawArgTypesAddr,signature,rawInvoker,fn,isAsync)=>{var argTypes=heap32VectorToArray(argCount,rawArgTypesAddr);name=readLatin1String(name);name=getFunctionName(name);rawInvoker=embind__requireFunction(signature,rawInvoker);exposePublicSymbol(name,function(){throwUnboundTypeError(`Cannot call ${name} due to unbound types`,argTypes)},argCount-1);whenDependentTypesAreResolved([],argTypes,function(argTypes){var invokerArgsArray=[argTypes[0],null].concat(argTypes.slice(1));replacePublicSymbol(name,craftInvokerFunction(name,invokerArgsArray,null,rawInvoker,fn,isAsync),argCount-1);return[]})};var integerReadValueFromPointer=(name,width,signed)=>{switch(width){case 1:return signed?pointer=>HEAP8[pointer>>0]:pointer=>HEAPU8[pointer>>0];case 2:return signed?pointer=>HEAP16[pointer>>1]:pointer=>HEAPU16[pointer>>1];case 4:return signed?pointer=>HEAP32[pointer>>2]:pointer=>HEAPU32[pointer>>2];default:throw new TypeError(`invalid integer width (${width}): ${name}`)}};var __embind_register_integer=(primitiveType,name,size,minRange,maxRange)=>{name=readLatin1String(name);if(maxRange===-1){maxRange=4294967295}var fromWireType=value=>value;if(minRange===0){var bitshift=32-8*size;fromWireType=value=>value<<bitshift>>>bitshift}var isUnsignedType=name.includes("unsigned");var checkAssertions=(value,toTypeName)=>{};var toWireType;if(isUnsignedType){toWireType=function(destructors,value){checkAssertions(value,this.name);return value>>>0}}else{toWireType=function(destructors,value){checkAssertions(value,this.name);return value}}registerType(primitiveType,{name:name,"fromWireType":fromWireType,"toWireType":toWireType,"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":integerReadValueFromPointer(name,size,minRange!==0),destructorFunction:null})};var __embind_register_memory_view=(rawType,dataTypeIndex,name)=>{var typeMapping=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];var TA=typeMapping[dataTypeIndex];function decodeMemoryView(handle){var size=HEAPU32[handle>>2];var data=HEAPU32[handle+4>>2];return new TA(HEAP8.buffer,data,size)}name=readLatin1String(name);registerType(rawType,{name:name,"fromWireType":decodeMemoryView,"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":decodeMemoryView},{ignoreDuplicateRegistrations:true})};function readPointer(pointer){return this["fromWireType"](HEAPU32[pointer>>2])}var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i<str.length;++i){var u=str.charCodeAt(i);if(u>=55296&&u<=57343){var u1=str.charCodeAt(++i);u=65536+((u&1023)<<10)|u1&1023}if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}}heap[outIdx]=0;return outIdx-startIdx};var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);var lengthBytesUTF8=str=>{var len=0;for(var i=0;i<str.length;++i){var c=str.charCodeAt(i);if(c<=127){len++}else if(c<=2047){len+=2}else if(c>=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var UTF8Decoder=typeof TextDecoder!="undefined"?new TextDecoder("utf8"):undefined;var UTF8ArrayToString=(heapOrArray,idx,maxBytesToRead)=>{var endIdx=idx+maxBytesToRead;var endPtr=idx;while(heapOrArray[endPtr]&&!(endPtr>=endIdx))++endPtr;if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx<endPtr){var u0=heapOrArray[idx++];if(!(u0&128)){str+=String.fromCharCode(u0);continue}var u1=heapOrArray[idx++]&63;if((u0&224)==192){str+=String.fromCharCode((u0&31)<<6|u1);continue}var u2=heapOrArray[idx++]&63;if((u0&240)==224){u0=(u0&15)<<12|u1<<6|u2}else{u0=(u0&7)<<18|u1<<12|u2<<6|heapOrArray[idx++]&63}if(u0<65536){str+=String.fromCharCode(u0)}else{var ch=u0-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}}return str};var UTF8ToString=(ptr,maxBytesToRead)=>ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead):"";var __embind_register_std_string=(rawType,name)=>{name=readLatin1String(name);var stdStringIsUTF8=name==="std::string";registerType(rawType,{name:name,"fromWireType"(value){var length=HEAPU32[value>>2];var payload=value+4;var str;if(stdStringIsUTF8){var decodeStartPtr=payload;for(var i=0;i<=length;++i){var currentBytePtr=payload+i;if(i==length||HEAPU8[currentBytePtr]==0){var maxRead=currentBytePtr-decodeStartPtr;var stringSegment=UTF8ToString(decodeStartPtr,maxRead);if(str===undefined){str=stringSegment}else{str+=String.fromCharCode(0);str+=stringSegment}decodeStartPtr=currentBytePtr+1}}}else{var a=new Array(length);for(var i=0;i<length;++i){a[i]=String.fromCharCode(HEAPU8[payload+i])}str=a.join("")}_free(value);return str},"toWireType"(destructors,value){if(value instanceof ArrayBuffer){value=new Uint8Array(value)}var length;var valueIsOfTypeString=typeof value=="string";if(!(valueIsOfTypeString||value instanceof Uint8Array||value instanceof Uint8ClampedArray||value instanceof Int8Array)){throwBindingError("Cannot pass non-string to std::string")}if(stdStringIsUTF8&&valueIsOfTypeString){length=lengthBytesUTF8(value)}else{length=value.length}var base=_malloc(4+length+1);var ptr=base+4;HEAPU32[base>>2]=length;if(stdStringIsUTF8&&valueIsOfTypeString){stringToUTF8(value,ptr,length+1)}else{if(valueIsOfTypeString){for(var i=0;i<length;++i){var charCode=value.charCodeAt(i);if(charCode>255){_free(ptr);throwBindingError("String has UTF-16 code units that do not fit in 8 bits")}HEAPU8[ptr+i]=charCode}}else{for(var i=0;i<length;++i){HEAPU8[ptr+i]=value[i]}}}if(destructors!==null){destructors.push(_free,base)}return base},"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":readPointer,destructorFunction(ptr){_free(ptr)}})};var UTF16Decoder=typeof TextDecoder!="undefined"?new TextDecoder("utf-16le"):undefined;var UTF16ToString=(ptr,maxBytesToRead)=>{var endPtr=ptr;var idx=endPtr>>1;var maxIdx=idx+maxBytesToRead/2;while(!(idx>=maxIdx)&&HEAPU16[idx])++idx;endPtr=idx<<1;if(endPtr-ptr>32&&UTF16Decoder)return UTF16Decoder.decode(HEAPU8.subarray(ptr,endPtr));var str="";for(var i=0;!(i>=maxBytesToRead/2);++i){var codeUnit=HEAP16[ptr+i*2>>1];if(codeUnit==0)break;str+=String.fromCharCode(codeUnit)}return str};var stringToUTF16=(str,outPtr,maxBytesToWrite)=>{if(maxBytesToWrite===undefined){maxBytesToWrite=2147483647}if(maxBytesToWrite<2)return 0;maxBytesToWrite-=2;var startPtr=outPtr;var numCharsToWrite=maxBytesToWrite<str.length*2?maxBytesToWrite/2:str.length;for(var i=0;i<numCharsToWrite;++i){var codeUnit=str.charCodeAt(i);HEAP16[outPtr>>1]=codeUnit;outPtr+=2}HEAP16[outPtr>>1]=0;return outPtr-startPtr};var lengthBytesUTF16=str=>str.length*2;var UTF32ToString=(ptr,maxBytesToRead)=>{var i=0;var str="";while(!(i>=maxBytesToRead/4)){var utf32=HEAP32[ptr+i*4>>2];if(utf32==0)break;++i;if(utf32>=65536){var ch=utf32-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}else{str+=String.fromCharCode(utf32)}}return str};var stringToUTF32=(str,outPtr,maxBytesToWrite)=>{if(maxBytesToWrite===undefined){maxBytesToWrite=2147483647}if(maxBytesToWrite<4)return 0;var startPtr=outPtr;var endPtr=startPtr+maxBytesToWrite-4;for(var i=0;i<str.length;++i){var codeUnit=str.charCodeAt(i);if(codeUnit>=55296&&codeUnit<=57343){var trailSurrogate=str.charCodeAt(++i);codeUnit=65536+((codeUnit&1023)<<10)|trailSurrogate&1023}HEAP32[outPtr>>2]=codeUnit;outPtr+=4;if(outPtr+4>endPtr)break}HEAP32[outPtr>>2]=0;return outPtr-startPtr};var lengthBytesUTF32=str=>{var len=0;for(var i=0;i<str.length;++i){var codeUnit=str.charCodeAt(i);if(codeUnit>=55296&&codeUnit<=57343)++i;len+=4}return len};var __embind_register_std_wstring=(rawType,charSize,name)=>{name=readLatin1String(name);var decodeString,encodeString,getHeap,lengthBytesUTF,shift;if(charSize===2){decodeString=UTF16ToString;encodeString=stringToUTF16;lengthBytesUTF=lengthBytesUTF16;getHeap=()=>HEAPU16;shift=1}else if(charSize===4){decodeString=UTF32ToString;encodeString=stringToUTF32;lengthBytesUTF=lengthBytesUTF32;getHeap=()=>HEAPU32;shift=2}registerType(rawType,{name:name,"fromWireType":value=>{var length=HEAPU32[value>>2];var HEAP=getHeap();var str;var decodeStartPtr=value+4;for(var i=0;i<=length;++i){var currentBytePtr=value+4+i*charSize;if(i==length||HEAP[currentBytePtr>>shift]==0){var maxReadBytes=currentBytePtr-decodeStartPtr;var stringSegment=decodeString(decodeStartPtr,maxReadBytes);if(str===undefined){str=stringSegment}else{str+=String.fromCharCode(0);str+=stringSegment}decodeStartPtr=currentBytePtr+charSize}}_free(value);return str},"toWireType":(destructors,value)=>{if(!(typeof value=="string")){throwBindingError(`Cannot pass non-string to C++ string type ${name}`)}var length=lengthBytesUTF(value);var ptr=_malloc(4+length+charSize);HEAPU32[ptr>>2]=length>>shift;encodeString(value,ptr+4,length+charSize);if(destructors!==null){destructors.push(_free,ptr)}return ptr},"argPackAdvance":GenericWireTypeSize,"readValueFromPointer":simpleReadValueFromPointer,destructorFunction(ptr){_free(ptr)}})};var __embind_register_void=(rawType,name)=>{name=readLatin1String(name);registerType(rawType,{isVoid:true,name:name,"argPackAdvance":0,"fromWireType":()=>undefined,"toWireType":(destructors,o)=>undefined})};var _abort=()=>{abort("")};var _emscripten_memcpy_js=(dest,src,num)=>HEAPU8.copyWithin(dest,src,src+num);var getHeapMax=()=>2147483648;var growMemory=size=>{var b=wasmMemory.buffer;var pages=(size-b.byteLength+65535)/65536;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};var _emscripten_resize_heap=requestedSize=>{var oldSize=HEAPU8.length;requestedSize>>>=0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}var alignUp=(x,multiple)=>x+(multiple-x%multiple)%multiple;for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignUp(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}return false};embind_init_charCodes();BindingError=Module["BindingError"]=class BindingError extends Error{constructor(message){super(message);this.name="BindingError"}};InternalError=Module["InternalError"]=class InternalError extends Error{constructor(message){super(message);this.name="InternalError"}};handleAllocatorInit();init_emval();UnboundTypeError=Module["UnboundTypeError"]=extendError(Error,"UnboundTypeError");var wasmImports={n:___cxa_throw,k:__embind_register_bigint,i:__embind_register_bool,h:__embind_register_emval,d:__embind_register_float,g:__embind_register_function,b:__embind_register_integer,a:__embind_register_memory_view,e:__embind_register_std_string,c:__embind_register_std_wstring,j:__embind_register_void,f:_abort,m:_emscripten_memcpy_js,l:_emscripten_resize_heap};var wasmExports=createWasm();var ___wasm_call_ctors=()=>(___wasm_call_ctors=wasmExports["p"])();var ___getTypeName=a0=>(___getTypeName=wasmExports["q"])(a0);var ___errno_location=()=>(___errno_location=wasmExports["__errno_location"])();var _malloc=Module["_malloc"]=a0=>(_malloc=Module["_malloc"]=wasmExports["r"])(a0);var _free=Module["_free"]=a0=>(_free=Module["_free"]=wasmExports["t"])(a0);var ___cxa_increment_exception_refcount=a0=>(___cxa_increment_exception_refcount=wasmExports["__cxa_increment_exception_refcount"])(a0);var ___cxa_is_pointer_type=a0=>(___cxa_is_pointer_type=wasmExports["u"])(a0);var calledRun;dependenciesFulfilled=function runCaller(){if(!calledRun)run();if(!calledRun)dependenciesFulfilled=runCaller};function run(){if(runDependencies>0){return}preRun();if(runDependencies>0){return}function doRun(){if(calledRun)return;calledRun=true;Module["calledRun"]=true;if(ABORT)return;initRuntime();readyPromiseResolve(Module);if(Module["onRuntimeInitialized"])Module["onRuntimeInitialized"]();postRun()}if(Module["setStatus"]){Module["setStatus"]("Running...");setTimeout(function(){setTimeout(function(){Module["setStatus"]("")},1);doRun()},1)}else{doRun()}}if(Module["preInit"]){if(typeof Module["preInit"]=="function")Module["preInit"]=[Module["preInit"]];while(Module["preInit"].length>0){Module["preInit"].pop()()}}run();
9
+
10
+
11
+ return moduleArg.ready
12
+ }
13
+ );
14
+ })();
15
+ ;
16
+ export default Module;
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+ set -e
3
+ ./build.sh
4
+ fswatch -o sort.cpp | while read line
5
+ do
6
+ echo "File changed, rebuilding..."
7
+ ./build.sh
8
+ echo "Done."
9
+ done
@@ -0,0 +1,59 @@
1
+ import {expose, transfer} from 'comlink'
2
+ import type {MainModule} from './ISort'
3
+ // @ts-expect-error no types
4
+ import workerPromise from './sort'
5
+ // import sharedArrayBufferWorkerPromise from './sort'
6
+ import {BufferPool} from './BufferPool'
7
+
8
+ export class WasmSorter {
9
+ public module: EmscriptenModule & MainModule
10
+ private _viewProjPtr: number
11
+ private _globalBufferPtr: number
12
+ private _combinedPtr: number
13
+ private _bufferPool: BufferPool
14
+
15
+ constructor(private _vertexCount: number, private _globalBuffer: Uint8Array) {
16
+ const combinedLength = this._calculateCombinedLength()
17
+ // we're double buffering, so 2 is the magic number here
18
+ this._bufferPool = new BufferPool(combinedLength, 2)
19
+ }
20
+
21
+ public async load() {
22
+ // const sharedABSupported = typeof SharedArrayBuffer !== 'undefined'
23
+ // this.module = await (sharedABSupported ? sharedArrayBufferWorkerPromise() : workerPromise())
24
+ this.module = await workerPromise()
25
+ this._viewProjPtr = this.module._malloc(16 * Float32Array.BYTES_PER_ELEMENT)
26
+ this._globalBufferPtr = this.module._malloc(this._vertexCount * 32)
27
+ this._combinedPtr = this.module._malloc(this._calculateCombinedLength())
28
+ this.module.HEAPU8.set(this._globalBuffer, this._globalBufferPtr)
29
+ }
30
+
31
+ public runSort(viewProj: Float32Array): ArrayBuffer {
32
+ this.module.HEAPF32.set(viewProj, this._viewProjPtr / Float32Array.BYTES_PER_ELEMENT)
33
+ this.module.runSort(this._viewProjPtr, this._globalBufferPtr, this._vertexCount, this._combinedPtr)
34
+
35
+ const byteStart = this._combinedPtr
36
+ const byteLength = this._calculateCombinedLength()
37
+ const bufferToTransfer = this._bufferPool.getBuffer()
38
+ new Uint8Array(bufferToTransfer).set(new Uint8Array(this.module.HEAPU8.buffer, byteStart, byteLength))
39
+ return transfer(bufferToTransfer, [bufferToTransfer])
40
+ }
41
+
42
+ public returnBuffer(buffer: ArrayBuffer): void {
43
+ this._bufferPool.returnBuffer(buffer)
44
+ }
45
+
46
+ private _calculateCombinedLength(): number {
47
+ return 4 * 4 * this._vertexCount + 3 * 4 * this._vertexCount + 3 * 4 * this._vertexCount + 4 * 4 * this._vertexCount * Float32Array.BYTES_PER_ELEMENT
48
+ }
49
+
50
+ public dispose(): void {
51
+ if (this.module) {
52
+ this.module._free(this._viewProjPtr)
53
+ this.module._free(this._globalBufferPtr)
54
+ this.module._free(this._combinedPtr)
55
+ }
56
+ }
57
+ }
58
+
59
+ expose(WasmSorter)
@@ -0,0 +1,182 @@
1
+ import type {WasmSorter} from '../cpp-sorter/worker'
2
+ import {Remote, transfer} from 'comlink'
3
+ import {
4
+ Box3,
5
+ BufferAttribute,
6
+ Camera,
7
+ IGeometry,
8
+ iGeometryCommons,
9
+ InstancedBufferAttribute,
10
+ InstancedBufferGeometry,
11
+ type IObject3D,
12
+ Matrix4,
13
+ PerspectiveCamera,
14
+ Sphere,
15
+ Vector3,
16
+ } from 'threepipe'
17
+
18
+ export class GaussianSplatGeometry extends InstancedBufferGeometry implements IGeometry {
19
+ constructor(
20
+ private _worker: Remote<WasmSorter>,
21
+ private _vertexCount: number,
22
+ private _maxSplats: number,
23
+ private _onLoad?: (geometry: GaussianSplatGeometry) => void,
24
+ ) {
25
+ super()
26
+ iGeometryCommons.upgradeGeometry.call(this)
27
+ this.initAttributes()
28
+ }
29
+
30
+ readonly isGaussianSplatGeometry = true
31
+
32
+ assetType: 'geometry' // dont set the value here since its checked in upgradeGeometry
33
+ setDirty = iGeometryCommons.setDirty
34
+ refreshUi = iGeometryCommons.refreshUi
35
+ appliedMeshes = new Set<IObject3D>()
36
+
37
+ private _viewProj: number[] = []
38
+ private _sortRunning = false
39
+
40
+ private _initialized = false
41
+
42
+ private _centersBuffer: Float32Array = new Float32Array(0)
43
+
44
+ public async update(camera: PerspectiveCamera | Camera, meshMatrixWorld: Matrix4) {
45
+ if (this._sortRunning || !this._initialized || !this._worker) {
46
+ return
47
+ }
48
+
49
+ camera.updateMatrixWorld(true)
50
+
51
+ this._viewProj = new Matrix4().multiply(camera.projectionMatrix).multiply(camera.matrixWorldInverse).multiply(meshMatrixWorld).elements
52
+
53
+ this._sortRunning = true
54
+ const viewProj = new Float32Array(this._viewProj)
55
+ const result = await this._worker.runSort(viewProj)
56
+
57
+ const {quat, scale, center, color} = this._extractViews(result)
58
+
59
+ if (this._centersBuffer.length !== center.length) this._centersBuffer = new Float32Array(center)
60
+ else this._centersBuffer.set(center)
61
+
62
+ ;(this.attributes.color as InstancedBufferAttribute).array = color;
63
+ (this.attributes.quat as InstancedBufferAttribute).array = quat;
64
+ (this.attributes.scale as InstancedBufferAttribute).array = scale;
65
+ (this.attributes.center as InstancedBufferAttribute).array = this._centersBuffer
66
+ // (this.attributes.center as InstancedBufferAttribute).array = center
67
+
68
+ const pms = Promise.all([
69
+ new Promise<void>(resolve => (this.attributes.color as InstancedBufferAttribute).onUpload(resolve)),
70
+ new Promise<void>(resolve => (this.attributes.quat as InstancedBufferAttribute).onUpload(resolve)),
71
+ new Promise<void>(resolve => (this.attributes.scale as InstancedBufferAttribute).onUpload(resolve)),
72
+ // new Promise<void>(resolve => (this.attributes.center as InstancedBufferAttribute).onUpload(resolve)),
73
+ ])
74
+
75
+ this.attributes.color.needsUpdate = true
76
+ this.attributes.quat.needsUpdate = true
77
+ this.attributes.scale.needsUpdate = true
78
+ this.attributes.center.needsUpdate = true
79
+
80
+ this.setDirty()
81
+
82
+ await pms
83
+
84
+ await this._worker.returnBuffer(transfer(result, [result]))
85
+
86
+ this._sortRunning = false
87
+ }
88
+
89
+ async initAttributes() {
90
+ const viewProj = new Float32Array(this._viewProj)
91
+ const result = await this._worker.runSort(viewProj)
92
+ const {quat, scale, center, color} = this._extractViews(result)
93
+
94
+ this.setAttribute('color', new InstancedBufferAttribute(color, 4, true))
95
+ this.setAttribute('quat', new InstancedBufferAttribute(quat, 4, true))
96
+ this.setAttribute('scale', new InstancedBufferAttribute(scale, 3, true))
97
+ this.setAttribute('center', new InstancedBufferAttribute(center, 3, true))
98
+ this.setAttribute('position', new BufferAttribute(new Float32Array([1, -1, 0, 1, 1, 0, -1, -1, 0, -1, 1, 0]), 3, true))
99
+
100
+ this.attributes.position.needsUpdate = true
101
+ this.setIndex(new BufferAttribute(new Uint16Array([0, 1, 2, 2, 3, 0]), 1, true))
102
+ this.instanceCount = Math.min(quat.length / 4, this._maxSplats)
103
+ this.computeBoundingBox()
104
+ this.computeBoundingSphere()
105
+
106
+ this._initialized = true
107
+ this.setDirty()
108
+ this._onLoad && this._onLoad(this)
109
+ }
110
+
111
+ private _extractViews(receivedBuffer: ArrayBuffer): {quat: Float32Array; scale: Float32Array; center: Float32Array; color: Float32Array} {
112
+ const combined = new Float32Array(receivedBuffer)
113
+
114
+ const quatLength = 4 * this._vertexCount
115
+ const scaleLength = 3 * this._vertexCount
116
+ const centerLength = 3 * this._vertexCount
117
+ const colorLength = 4 * this._vertexCount
118
+
119
+ const quatOffset = 0
120
+ const scaleOffset = quatOffset + quatLength
121
+ const centerOffset = scaleOffset + scaleLength
122
+ const colorOffset = centerOffset + centerLength
123
+
124
+ const quat = combined.subarray(quatOffset, quatOffset + quatLength)
125
+ const scale = combined.subarray(scaleOffset, scaleOffset + scaleLength)
126
+ const center = combined.subarray(centerOffset, centerOffset + centerLength)
127
+ const color = combined.subarray(colorOffset, colorOffset + colorLength)
128
+
129
+ return {quat, scale, center, color}
130
+ }
131
+
132
+ computeBoundingBox() {
133
+ if (!this.getAttribute('center')) return super.computeBoundingBox()
134
+
135
+ const box = this.boundingBox ?? (this.boundingBox = new Box3())
136
+ box.setFromBufferAttribute(this.getAttribute('center') as InstancedBufferAttribute)
137
+
138
+ if (isNaN(this.boundingBox!.min.x) || isNaN(this.boundingBox!.min.y) || isNaN(this.boundingBox!.min.z)) {
139
+ console.error('GaussianSplatGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this)
140
+ }
141
+ }
142
+
143
+ computeBoundingSphere() {
144
+
145
+ if (this.boundingSphere === null) this.boundingSphere = new Sphere()
146
+
147
+ const position = this.attributes.center
148
+
149
+ if (position && (position as any).isGLBufferAttribute) {
150
+ console.error('THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere. Alternatively set "mesh.frustumCulled" to "false".', this)
151
+ this.boundingSphere.set(new Vector3(), Infinity)
152
+ return
153
+ }
154
+
155
+ if (position) {
156
+ // first, find the center of the bounding sphere
157
+ const center = this.boundingSphere.center
158
+ if (!this.boundingBox) this.computeBoundingBox()
159
+ this.boundingBox!.getCenter(center)
160
+
161
+ // second, try to find a boundingSphere with a radius smaller than the
162
+ // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
163
+
164
+ let maxRadiusSq = 0
165
+ const vector = new Vector3()
166
+
167
+ for (let i = 0, il = position.count; i < il; i++) {
168
+ vector.fromBufferAttribute(position, i)
169
+ maxRadiusSq = Math.max(maxRadiusSq, center.distanceToSquared(vector))
170
+ }
171
+
172
+ this.boundingSphere.radius = Math.sqrt(maxRadiusSq)
173
+
174
+ if (isNaN(this.boundingSphere.radius)) {
175
+ console.error('THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this)
176
+ }
177
+
178
+ }
179
+
180
+ }
181
+
182
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * ThreeGaussianSplatPlugin: Threejs utilities and threepipe plugin and material extension for Gaussian Splatting
3
+ *
4
+ * @license
5
+ * A port of -
6
+ * https://github.com/zappar-xr/three-gaussian-splat
7
+ * MIT License (c) 2023 Zappar Limited
8
+ * Which is based on -
9
+ * gsplat.js, MIT License (c) 2023 Dylan Ebert
10
+ * antimatter15/splat, MIT License (c) 2023 Kevin Kwok
11
+ */
12
+
13
+ export {GaussianSplatMaterialExtension} from './materials/GaussianSplatMaterialExtension'
14
+ export {GaussianSplatMaterialPhysical} from './materials/GaussianSplatMaterialPhysical'
15
+ export {GaussianSplatMaterialRaw} from './materials/GaussianSplatMaterialRaw'
16
+ export {GaussianSplatMaterialUnlit} from './materials/GaussianSplatMaterialUnlit'
17
+ export {computeFocalLengths} from './materials/util'
18
+ export {GaussianSplatGeometry} from './geometry/GaussianSplatGeometry'
19
+ export {GaussianSplatMesh} from './mesh/GaussianSplatMesh'
20
+ export {ThreeGaussianSplatPlugin} from './ThreeGaussianSplatPlugin'
21
+ export {SplatLoader} from './loaders/SplatLoader'
22
+ export {gaussianSplatShaders} from './shaders'