Skip to content

Commit 6ea4211

Browse files
authored
fix: ColorModifier bugs - swapped min/max calculation and global wasm reference (#189)
## Summary Fixes #184 Fixes three bugs in the ColorModifier: 1. **Swapped min/max calculation** (lines 164-166): `Math.max` and `Math.min` were swapped, causing incorrect min/max values 2. **Global wasm reference** (line 157): Uses global `wasm` variable instead of `input.wasm` 3. **Debug code in production** (line 162): Removed `window.perAtomArray` assignment ## Changes - Fixed min/max calculation by swapping Math.max/min - Replaced `wasm` with `input.wasm` for proper scoping - Removed debug code `window.perAtomArray` assignment ## Impact The swapped min/max values were causing incorrect color mapping for particle visualization based on compute values. --- This pull request contains changes generated by a Cursor Cloud Agent <a href="https://cursor.com/background-agent?bcId=bc-242f3cde-9d37-4376-92bc-3baf88c02fec"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/open-in-cursor-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/open-in-cursor-light.svg"><img alt="Open in Cursor" src="https://cursor.com/open-in-cursor.svg"></picture></a>&nbsp;<a href="https://cursor.com/agents?id=bc-242f3cde-9d37-4376-92bc-3baf88c02fec"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/open-in-web-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/open-in-web-light.svg"><img alt="Open in Web" src="https://cursor.com/open-in-web.svg"></picture></a>
2 parents c70b2ad + 1eaf9ef commit 6ea4211

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/modifiers/colormodifier.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,18 @@ class ColorModifier extends Modifier {
154154
compute.lmpCompute.sync();
155155
const perAtomDataPtr = compute.lmpCompute.getPerAtomData() / 8;
156156
//@ts-ignore
157-
const perAtomArray = wasm.HEAPF64.subarray(
157+
const perAtomArray = input.wasm.HEAPF64.subarray(
158158
perAtomDataPtr,
159159
perAtomDataPtr + output.particles.count,
160-
) as Float32Array;
161-
//@ts-ignore
162-
window.perAtomArray = perAtomArray;
163-
// @ts-ignore
164-
const minValue = Math.max.apply(null, perAtomArray);
165-
// @ts-ignore
166-
const maxValue = Math.min.apply(null, perAtomArray);
160+
) as Float64Array;
161+
// Calculate min/max using a loop to avoid stack overflow for large arrays
162+
let minValue = Infinity;
163+
let maxValue = -Infinity;
164+
for (let i = 0; i < perAtomArray.length; i++) {
165+
const value = perAtomArray[i];
166+
minValue = Math.min(minValue, value);
167+
maxValue = Math.max(maxValue, value);
168+
}
167169
perAtomArray.forEach((value, index) => {
168170
const realIndex = output.particles.indices[index];
169171
const colorIndex = Math.floor(

0 commit comments

Comments
 (0)