File size: 7,208 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
/*
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.
*/
class cGSData {
static ROW_LENGTH_IN_BITES = 32; // Bytes per splat
static FLOAT_SIZE = 4; // Bytes per float
static ROW_LENGTH_IN_FLOATS = cGSData.ROW_LENGTH_IN_BITES / cGSData.FLOAT_SIZE;
static POSITION = 0;
static SCALE = 1;
static QUATERNION = 2;
static COLOR = 3;
static MASTER_BUFFER = 4;
constructor(splatData){
this.uBuffer = new Uint8Array(splatData);
this.fBuffer = new Float32Array(this.uBuffer.buffer);
if(!this.uBuffer){
console.error("invalid splatData")
}
}
getBuffer(typeData) {
switch(typeData){
case cGSData.POSITION:
case cGSData.SCALE:
return this.fBuffer;
case cGSData.QUATERNION:
case cGSData.COLOR:
case cGSData.MASTER_BUFFER:
return this.uBuffer;
}
console.log(typeData, " returning NULL");
return null;
}
getBufferInd(typeData, splatInd){
switch(typeData){
case cGSData.POSITION:
return cGSData.ROW_LENGTH_IN_FLOATS * splatInd;
case cGSData.SCALE:
return cGSData.ROW_LENGTH_IN_FLOATS * splatInd + 3;
case cGSData.QUATERNION:
return cGSData.ROW_LENGTH_IN_BITES * splatInd + 28;
case cGSData.COLOR:
return cGSData.ROW_LENGTH_IN_BITES * splatInd + 24;
}
return -1;
}
print(ind) {
const pBuffer = this.getBuffer(cGSData.POSITION)
const sBuffer = this.getBuffer(cGSData.SCALE)
const qBuffer = this.getBuffer(cGSData.QUATERNION)
const cBuffer = this.getBuffer(cGSData.COLOR)
let bufferInd = this.getBufferInd(cGSData.POSITION, ind);
console.log("pos ", pBuffer[bufferInd], pBuffer[bufferInd+1],pBuffer[bufferInd+2])
bufferInd = this.getBufferInd(cGSData.SCALE, ind);
console.log("scl ", sBuffer[bufferInd], sBuffer[bufferInd+1], sBuffer[bufferInd+2])
bufferInd = this.getBufferInd(cGSData.QUATERNION, ind);
let quat = [];
for(let i=0; i<4; i++) {
quat[i] = (qBuffer[bufferInd + i] - 128) / 128;
}
console.log("quat ", quat[0], quat[1], quat[2], quat[3])
}
getNumberOfSplats() {
return this.fBuffer.length / cGSData.ROW_LENGTH_IN_FLOATS;
}
getPositionBounds() {
let min = [100000, 100000, 100000];
let max = [-100000, -100000, -100000];
for(let i=0; i<this.getNumberOfSplats(); i++){
let bufferInd = this.getBufferInd(cGSData.POSITION, i);
for(let j=0; j<3; j++){
if(i===0 || this.fBuffer[bufferInd + j] < min[j]){
min[j] = this.fBuffer[bufferInd + j];
}
if(i===0 || this.fBuffer[bufferInd + j] > max[j]){
max[j] = this.fBuffer[bufferInd + j];
}
}
}
let eps = 0.0001;
return ensureDifference(min, max, eps);
}
getScaleBounds() {
let min = [100000, 100000, 100000];
let max = [-100000, -100000, -100000];
for(let i=0; i<this.getNumberOfSplats(); i++){
let bufferInd = this.getBufferInd(cGSData.SCALE, i);
for(let j=0; j<3; j++){
if(i===0 || this.fBuffer[bufferInd + j] < min[j]){
min[j] = this.fBuffer[bufferInd + j];
}
if(i===0 || this.fBuffer[bufferInd + j] > max[j]){
max[j] = this.fBuffer[bufferInd + j];
}
}
}
let eps = 0.0001;
return ensureDifference(min, max, eps);
}
async getUniqueColors(colorTolerance, maxIndexCount, updateCB) {
let n = this.getNumberOfSplats();
let cBuffer = this.getBuffer(cGSData.COLOR);
let splatIndToCInd = new Array(n).fill(-1);
let uniqueColors = [];
for(let i=0; i<n; i++){
if (i % 1000 === 0) {
await new Promise(resolve => {
requestAnimationFrame(() => {
if(updateCB){
updateCB((i + 1.0) / n);
}
let progress = Math.floor((i + 1) / n * 100);
console.log("progress", progress);
resolve();
});
});
} // end if
let bufferInd = this.getBufferInd(cGSData.COLOR, i);
// check if the color is already in the set
let isUnique = true;
let uniqueInd = 0;
for(let uniqueColor of uniqueColors){
let sumSq = 0;
// compare uniqueColor with the i-th color
for(let j=0; j<4; j++){
sumSq += Math.pow(uniqueColor[j] - cBuffer[bufferInd+j], 2);
}
if(sumSq < colorTolerance){
isUnique = false;
splatIndToCInd[i] = uniqueInd;
break;
}
uniqueInd++;
if(uniqueInd > maxIndexCount){
console.error("Too many unique colors. Max number allowed is ", maxIndexCount);
return null;
}
}
if(isUnique){
splatIndToCInd[i] = uniqueColors.length;
let color = [cBuffer[bufferInd], cBuffer[bufferInd+1], cBuffer[bufferInd+2], cBuffer[bufferInd+3]];
uniqueColors.push(color);
}
}
return [splatIndToCInd, uniqueColors];
}
}
function ensureDifference(min, max, eps) {
if(Math.abs(min[0] - max[0]) < eps){
min[0] -= eps;
max[0] += eps;
}
if(Math.abs(min[1] - max[1]) < eps){
min[1] -= eps;
max[1] += eps;
}
if(Math.abs(min[2] - max[2]) < eps){
min[2] -= eps;
max[2] += eps;
}
return [min[0], max[0], min[1], max[1], min[2], max[2]];
}
|