Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/lib/writers/compressed-chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@ class CompressedChunk {
}

pack() {
// Bounds over the finite values only: a single NaN/Infinity would otherwise
// make the whole chunk's bounds non-finite and corrupt all 256 splats.
const calcMinMax = (data: Float32Array) => {
let min;
let max;
min = max = data[0];
for (let i = 1; i < data.length; ++i) {
let min = Infinity;
let max = -Infinity;
for (let i = 0; i < data.length; ++i) {
const v = data[i];
min = Math.min(min, v);
max = Math.max(max, v);
if (Number.isFinite(v)) {
if (v < min) min = v;
if (v > max) max = v;
}
}
return { min, max };
return min <= max ? { min, max } : { min: 0, max: 0 };
};

const normalize = (x: number, min: number, max: number) => {
if (x <= min) return 0;
if (Number.isNaN(x) || x <= min) return 0;
if (x >= max) return 1;
return max - min < 0.00001 ? 0 : (x - min) / (max - min);
};
Expand Down
22 changes: 22 additions & 0 deletions test/formats.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,28 @@ describe('Compressed PLY Format', () => {
compareSummaries(actualSummary, expectedSummary, { tolerance: 0.1 });
});

it('a non-finite value must not corrupt the rest of its 256-splat chunk', async () => {
const dt = createTestDataTable(256);
dt.transform = Transform.PLY.clone();
dt.getColumnByName('x').data[7] = NaN;
dt.getColumnByName('scale_0').data[9] = NaN;
dt.getColumnByName('f_dc_1').data[11] = NaN;

const writeFs = new MemoryFileSystem();
await writeCompressedPly({ filename: 'nan.compressed.ply', dataTable: dt }, writeFs);

const pool = createChunkDataPool();
const readBack = await materializeToDataTable(
await readPly(new BufferReadSource(writeFs.results.get('nan.compressed.ply')), pool), pool
);

// Before the fix all 256 splats decoded as NaN in each of these columns.
for (const name of ['x', 'scale_0', 'f_dc_1']) {
const nanCount = Array.from(readBack.getColumnByName(name).data).filter(Number.isNaN).length;
assert.strictEqual(nanCount, 0, `${name}: ${nanCount} NaN values after round-trip`);
}
});

it('writeCompressedPlySource (ChunkSource adapter) == writeCompressedPly byte-for-byte', async () => {
const dt = createTestDataTable(300, { includeSH: true, shBands: 1 });
dt.transform = Transform.PLY.clone();
Expand Down