@stringsync/vexml
Version:
MusicXML to Vexflow
88 lines (87 loc) • 3.66 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Locator = void 0;
const spatial = __importStar(require("../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. */
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;
});
}
}
exports.Locator = Locator;