Pi4 / viewer /point-sampling.js
yyfz233's picture
Default to 500K random display points and document track sampling
338d434
Raw History Blame Contribute Delete
1.57 kB
export const DEFAULT_POINT_BUDGET = 500_000;
export function linspaceIndices(total, limit) {
if (!Number.isSafeInteger(total) || total < 0 || total > 0xffffffff || !Number.isSafeInteger(limit) || limit < 0) throw new Error('Invalid point sampling budget');
const count = Math.min(total, limit);
const indices = new Uint32Array(count);
for (let index = 0; index < count; index++) indices[index] = count > 1 ? Math.floor(index * (total - 1) / (count - 1)) : 0;
return indices;
}
export class RandomPointSampler {
constructor(capacity, seed = 20261114) {
if (!Number.isSafeInteger(capacity) || capacity < 0 || capacity > 0xffffffff || !Number.isSafeInteger(seed)) throw new Error('Invalid point sampling capacity or seed');
this.order = new Uint32Array(capacity);
this.count = 0;
this.state = seed >>> 0;
}
sample(total, limit) {
if (!Number.isSafeInteger(total) || total < this.count || total > this.order.length || !Number.isSafeInteger(limit) || limit < 0) throw new Error('Invalid point sampling budget');
for (let index = this.count; index < total; index++) {
this.state = (this.state + 0x6d2b79f5) >>> 0;
let value = Math.imul(this.state ^ this.state >>> 15, this.state | 1);
value ^= value + Math.imul(value ^ value >>> 7, value | 61);
const selected = Math.floor(((value ^ value >>> 14) >>> 0) / 2 ** 32 * (index + 1));
this.order[index] = this.order[selected];
this.order[selected] = index;
}
this.count = total;
return this.order.subarray(0, Math.min(total, limit));
}
}