-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from midlik/nonoverlapping-perf
NightingaleTrack: Improve `NonOverlappingLayout` performance
- Loading branch information
Showing
5 changed files
with
232 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,96 @@ | ||
import { clamp } from "lodash-es"; | ||
import DefaultLayout, { LayoutOptions } from "./DefaultLayout"; | ||
import { Feature } from "./nightingale-track"; | ||
|
||
const featuresOverlap = (feature1: Feature, feature2: Feature) => | ||
!( | ||
Number(feature2.start) > Number(feature1.end) || | ||
Number(feature2.end) < Number(feature1.start) | ||
); | ||
|
||
const featureOvelapsInRow = (feature: Feature, row: Feature[]) => | ||
row.some((rowFeature) => featuresOverlap(feature, rowFeature)); | ||
|
||
export default class NonOverlappingLayout extends DefaultLayout { | ||
protected rowHeight: number; | ||
featuresMap = new Map(); | ||
#rows: Feature[][]; | ||
/** Height of a row, including gap between rows */ | ||
protected rowHeight: number = 0; | ||
/** Height of a feature, excluding gap between rows */ | ||
protected featureHeight: number = 0; | ||
/** Y coordinate of the top of the first row */ | ||
protected topOffset: number = 0; | ||
|
||
constructor(options: LayoutOptions) { | ||
super(options); | ||
this.rowHeight = 0; | ||
this.minHeight = 15; | ||
this.#rows = []; | ||
} | ||
/** Mapping of feature to row index */ | ||
featuresMap = new Map<Feature, number>(); | ||
|
||
init(features: Feature[]) { | ||
features.forEach((feature) => { | ||
const rowIndex = this.#rows.findIndex( | ||
(row) => !featureOvelapsInRow(feature, row), | ||
); | ||
if (rowIndex >= 0) { | ||
this.#rows[rowIndex].push(feature); | ||
this.featuresMap.set(feature, rowIndex); | ||
} else { | ||
this.#rows.push([feature]); | ||
this.featuresMap.set(feature, this.#rows.length - 1); | ||
} | ||
constructor(options: LayoutOptions) { | ||
super({ | ||
...options, | ||
maxHeight: options.maxHeight ?? 15, | ||
}); | ||
this.rowHeight = Math.min( | ||
this.layoutHeight / this.#rows.length, | ||
this.minHeight, | ||
); | ||
} | ||
|
||
getOffset() { | ||
// this offset is required for centering if the number of rows doesn't | ||
// fill the layout | ||
if (this.#rows.length * this.rowHeight < this.layoutHeight) { | ||
return this.layoutHeight / 2 - (this.#rows.length * this.rowHeight) / 2; | ||
} | ||
return 0; | ||
init(features: Feature[]) { | ||
const { featuresMap, rows } = placeFeaturesIntoRows(features); | ||
this.featuresMap = featuresMap; | ||
const usableHeight = clamp(this.layoutHeight - this.margin.top - this.margin.bottom, 0, rows.length * (this.maxHeight + this.gap)); | ||
this.rowHeight = usableHeight / Math.max(rows.length, 1); | ||
this.featureHeight = clamp(this.rowHeight - this.gap, this.minHeight, this.maxHeight); | ||
const center = 0.5 * (this.margin.top + this.layoutHeight - this.margin.bottom); | ||
this.topOffset = center - 0.5 * usableHeight; | ||
} | ||
|
||
getFeatureYPos(feature: Feature) { | ||
const rowNumber = this.featuresMap.get(feature); | ||
return ( | ||
this.getOffset() + | ||
this.rowHeight * rowNumber + | ||
this.margin.top + | ||
this.margin.bottom | ||
); | ||
const rowIndex = this.featuresMap.get(feature) ?? 0; | ||
const center = this.topOffset + (rowIndex + 0.5) * this.rowHeight; | ||
return center - 0.5 * this.featureHeight; | ||
} | ||
|
||
getFeatureHeight() { | ||
return this.rowHeight - this.margin.top - this.margin.bottom; | ||
return this.featureHeight; | ||
} | ||
} | ||
|
||
|
||
type Range = { start: number, end: number }; | ||
|
||
function overlap(feature1: Partial<Range>, feature2: Partial<Range>): boolean { | ||
return !( | ||
Number(feature2.start) > Number(feature1.end) || | ||
Number(feature2.end) < Number(feature1.start) | ||
); | ||
} | ||
|
||
function overlapInRow(feature: Partial<Range>, row: Partial<Range>[]): boolean { | ||
// Search in reverse order for better performance | ||
return reverseSome(row, rowFeature => overlap(feature, rowFeature)); | ||
} | ||
|
||
/** Equivalent to `array.some(predicate)` but tests elements in reverse order. */ | ||
function reverseSome<T>(array: T[], predicate: (value: T, index: number, array: T[]) => boolean): boolean { | ||
for (let i = array.length - 1; i >= 0; i--) { | ||
if (predicate(array[i], i, array)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
function placeFeaturesIntoRows(features: Feature[]) { | ||
const featuresMap = new Map<Feature, number>(); | ||
const rows: Feature[][] = []; | ||
const rowRanges: Record<number, Range> = {}; // For better performance, would also work without this | ||
for (const feature of features) { | ||
const rowIndex = rows.findIndex( | ||
(row, i) => !overlap(feature, rowRanges[i]) || !overlapInRow(feature, row), | ||
); | ||
if (rowIndex >= 0) { | ||
// Add to existing row | ||
rows[rowIndex].push(feature); | ||
featuresMap.set(feature, rowIndex); | ||
const rowRange = rowRanges[rowIndex]; | ||
rowRanges[rowIndex] = { | ||
start: Math.min(rowRange.start, feature.start ?? -Infinity), | ||
end: Math.max(rowRange.end, feature.end ?? Infinity), | ||
}; | ||
} else { | ||
// Create new row | ||
rows.push([feature]); | ||
featuresMap.set(feature, rows.length - 1); | ||
rowRanges[rows.length - 1] = { | ||
start: feature.start ?? -Infinity, | ||
end: feature.end ?? Infinity, | ||
}; | ||
} | ||
} | ||
return { rows, featuresMap }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.