UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

51 lines (50 loc) 2.17 kB
import * as spatial from '../spatial'; /** * How many entries a quad tree node should hold before subdividing. * * This is intentionally not configurable because it is a low level detail that should not be exposed to the caller. */ const QUAD_TREE_THRESHOLD = 10; /** Locates elements using spatial points. */ export class Locator { tree; unorganizedElements; constructor(tree, // The elements that could not be inserted into the tree because they are too large and/or out-of-bounds. unorganizedElements) { this.tree = tree; this.unorganizedElements = unorganizedElements; } static create(score) { const unorganizedElements = new Array(); const systems = score.getSystems(); const measures = systems.flatMap((system) => system.getMeasures()); const fragments = measures.flatMap((measure) => measure.getFragments()); const parts = fragments.flatMap((fragment) => fragment.getParts()); const staves = parts.flatMap((part) => part.getStaves()); const voices = staves.flatMap((stave) => stave.getVoices()); const voiceEntries = voices.flatMap((voice) => voice.getEntries()); const elements = [score, ...systems, ...measures, ...fragments, ...parts, ...staves, ...voices, ...voiceEntries]; const tree = new spatial.QuadTree(score.rect(), QUAD_TREE_THRESHOLD); for (const element of elements) { if (!tree.insert(element.rect(), element)) { unorganizedElements.push(element); } } return new Locator(tree, unorganizedElements); } /** * Locates all the elements that contain the given point, sorted by descending distance to the point based on their * rects' centers. */ locate(point) { return [ ...this.tree.query(point), ...this.unorganizedElements.filter((element) => element.rect().contains(point)), ].sort((a, b) => { const distanceA = a.rect().center().distance(point); const distanceB = b.rect().center().distance(point); return distanceA - distanceB; }); } }