File size: 6,408 Bytes
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright (c) 2025 Very 360 VR. DBA Hyper Interactive

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

function twoBytesToInt(byte1, byte2) {
    if (byte1 < 0 || byte1 > 255 || byte2 < 0 || byte2 > 255) {
      throw new Error('Input bytes must be between 0 and 255');
    }
  
    const combined = byte1 << 8 | byte2;  // Shift the first byte left by 8 bits and combine with the second byte
    return combined;
}

function extractArrays(combinedArray) {

    // print length
    console.log("combinedArray.length: ", combinedArray.length);
    
    // read the version
    const floatSize = Float32Array.BYTES_PER_ELEMENT;
    let tmpBuffer  = new Float32Array(combinedArray.slice(0, floatSize).buffer);
    const version = Math.round(tmpBuffer[0]);

    const numFloats = version === 1 ?  14 : // version (1), position bounds (2*3), scale bounds (2*3), num splats (1)
                      version === 2 ?  16 : // version (1), position bounds (2*3), scale bounds (2*3), num splats (1), num rotated scales (1), num unique colors (1)
                      version === 3 ?  15 : // version (1), position bounds (2*3), scale bounds (2*3), num splats (1), num unique colors (1)
                        0;

    if(numFloats === 0) {
        console.error("Invalid version: ", version);
        return null;
    }

    const floatArraySize = numFloats * floatSize;
    //console.log("floatArraySize: ", floatArraySize, numFloats, " x ", floatSize);

    // Extract the float data
    const floatBuffer = combinedArray.slice(0, floatArraySize).buffer;
    const floatView = new Float32Array(floatBuffer);

    // Extract the uint8 data
    const uintBuffer = combinedArray.slice(floatArraySize).buffer;
    const uintView = new Uint8Array(uintBuffer);

    //console.log("floatView: ", floatView[0], floatView[1], floatView[2], floatView[3]);
    //console.log("uintView: ", uintView[0], uintView[1], uintView[2], uintView[3]);
    //console.log("combinedArray at ", floatArraySize, " is ", combinedArray[floatArraySize], combinedArray[floatArraySize +1]);
    return [floatView, uintView ];

}

/* 
* Desc: Converts Gaussian Splatting from hyp data format to GaussianSplattingMesh
*
* Input: hypData: Uint8Array
*        scene: BABYLON.Scene
*
* Output: GaussianSplattingMesh if succeed, null otherwise
*
* Example:
*     let fileReader = new FileReader();
*     fileReader.onload = function() {
*         const arrayBuffer = this.result;
*         const hypData = new Uint8Array(arrayBuffer);  
*         let newGS = fromHyp(hypData, scene)
*         if(gaussianSplattingsMesh)
*             gaussianSplattingsMesh.dispose();
*         gaussianSplattingsMesh = newGS;   
*     };
*     fileReader.readAsArrayBuffer(blob);
*/
function fromHyp(hypData, scene) {

    let [floatData, uint8Data] = extractArrays(hypData);
    
    // check if length is correct
    if(floatData.length != 15) {
        console.error("Invalid float data size: expected 15, got ", floatData.length);
        return null;
    }
    //let version = this.getVersion(floatData[0]);

    let posBounds = floatData.slice(1, 7);
    let scaleBounds = floatData.slice(7, 13);
    let nSplats = floatData[13];
    let nUniqueColors = floatData[14];

    // each row contains pos (2*3 byte), scale (3 byte), quaternion (4 byte), color ind (1 byte)
    const hypRowLength = 2*3 + 3 + 4 + 1;
    const colDataRowLength = 4;
    const posOffset = 0;
    const scaleOffset = 2 * 3 * nSplats + posOffset;
    const quatOffset = 3 * nSplats + scaleOffset;
    const colIndOffset = 4 * nSplats + quatOffset;
    const colDataOffset = 2 * nSplats + colIndOffset;

    if(colDataOffset + colDataRowLength * nUniqueColors != uint8Data.length) {
        console.error("Invalid data size for ", nSplats, " splats: expected ", nSplats * 17 + 2, " got ", uint8Data.length);
        return null;
    }

    // allocate GS buffers
    const uBuffer = new Uint8Array(nSplats * cGSData.ROW_LENGTH_IN_BITES);
    const fBuffer = new Float32Array(uBuffer.buffer);
        
    let gsData = new cGSData(uBuffer);
    for(let i=0; i<nSplats; i++) {

        let posInd = gsData.getBufferInd(cGSData.POSITION, i);
        let scaleInd = gsData.getBufferInd(cGSData.SCALE, i);
        let quatInd = gsData.getBufferInd(cGSData.QUATERNION, i);
        let colorInd = gsData.getBufferInd(cGSData.COLOR, i);

        for(let j=0; j<3; j++) {
            
            //fBuffer[posInd + j] = posBounds[2*j] + (uint8Data[posOffset + i * 3 + j] / 255) * (posBounds[2*j+1] - posBounds[2*j]);
            let value = twoBytesToInt(uint8Data[posOffset + i * 6 + 2*j], uint8Data[posOffset + i * 6 + 2*j + 1]);
            fBuffer[posInd + j] = posBounds[2*j] + (value / 65535) * (posBounds[2*j+1] - posBounds[2*j]);

            fBuffer[scaleInd + j] = scaleBounds[2*j] + (uint8Data[scaleOffset + i * 3 + j] / 255) * (scaleBounds[2*j+1] - scaleBounds[2*j]);
        }

            for(let j=0; j<4; j++) {
                uBuffer[quatInd + j] = uint8Data[quatOffset + i * 4 + j];
        }
    
        // record the colors
        let uniqueColorInd = twoBytesToInt(uint8Data[colIndOffset + i*2], uint8Data[colIndOffset + i*2 + 1]);
        for(let j=0; j<4; j++) {
            uBuffer[colorInd + j] = uint8Data[colDataOffset + uniqueColorInd * 4 + j];
        }
    }

    let gsMesh = new BABYLON.GaussianSplattingMesh("gs", undefined, scene, true);
    gsMesh.updateData(uBuffer);
    return gsMesh;
}