Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/GLModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ArrowSpec } from "GLShape";
import { ParserOptionsSpec } from "./parsers/ParserOptionsSpec";
import { LabelSpec } from "Label";
import { assignBonds } from "./parsers/utils/assignBonds";
import { resetIntersectionShapes } from "./intersectionShapes";

/**
* GLModel represents a group of related atoms
Expand Down Expand Up @@ -1666,6 +1667,9 @@ export class GLModel {
for (let ai = 0; ai < atoms.length; ai++) this._coordIndex.set(atoms[ai], ai);
this._coordAtomCount = atoms.length;

// Picking shapes are stored on atoms, so clear stale frame geometry before rebuilding.
resetIntersectionShapes(atoms);

var ret = new Object3D();
var cartoonAtoms = [];
var lineGeometries: Record<number, Geometry> = {};
Expand Down
7 changes: 7 additions & 0 deletions src/intersectionShapes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface InteractiveAtom {
clickable?: boolean;
hoverable?: boolean;
intersectionShape?: any;
}

export function resetIntersectionShapes(atoms: InteractiveAtom[]): void;
10 changes: 10 additions & 0 deletions src/intersectionShapes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function resetIntersectionShapes(atoms) {
for (var i = 0; i < atoms.length; i++) {
var atom = atoms[i];
if (atom && (atom.clickable || atom.hoverable)) {
atom.intersectionShape = { sphere: [], cylinder: [], line: [], triangle: [] };
}
}
}

module.exports = { resetIntersectionShapes: resetIntersectionShapes };
42 changes: 42 additions & 0 deletions tests/jest/intersectionShapes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { resetIntersectionShapes } = require("../../src/intersectionShapes");

function oldShapes() {
return { sphere: ["stale"], cylinder: ["stale"], line: ["stale"], triangle: ["stale"] };
}

describe("picking-shape rebuilds", () => {
test("keeps clickable and hoverable frame shapes bounded on revisits", () => {
const frames = [
[
{ clickable: true, intersectionShape: oldShapes() },
{ hoverable: true, intersectionShape: oldShapes() },
{ intersectionShape: { sphere: ["unchanged"] } },
],
[{ clickable: true, intersectionShape: oldShapes() }],
];
const noninteractiveShapes = frames[0][2].intersectionShape;

for (let rebuild = 0; rebuild < 200; rebuild++) {
const frame = frames[rebuild % frames.length];
resetIntersectionShapes(frame);
for (const atom of frame) {
if (atom.clickable || atom.hoverable) {
atom.intersectionShape.sphere.push(rebuild);
atom.intersectionShape.cylinder.push(rebuild);
}
}
}

for (const frame of frames) {
for (const atom of frame) {
if (atom.clickable || atom.hoverable) {
expect(atom.intersectionShape.sphere).toHaveLength(1);
expect(atom.intersectionShape.cylinder).toHaveLength(1);
expect(atom.intersectionShape.line).toHaveLength(0);
expect(atom.intersectionShape.triangle).toHaveLength(0);
}
}
}
expect(frames[0][2].intersectionShape).toBe(noninteractiveShapes);
});
});
Loading