From 5c12840d8aa60b285303837bffb51518d76cf49d Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Mon, 16 Sep 2024 13:04:38 +0200 Subject: [PATCH] refactor: draw contours --- src/data/data2d/Spectrum2D/contours.ts | 86 ++++++++++++++------------ 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/src/data/data2d/Spectrum2D/contours.ts b/src/data/data2d/Spectrum2D/contours.ts index 54182ea49..b57358f56 100644 --- a/src/data/data2d/Spectrum2D/contours.ts +++ b/src/data/data2d/Spectrum2D/contours.ts @@ -178,7 +178,7 @@ function range(from: number, to: number, step: number) { interface DrawContoursOptions { negative?: boolean; quadrant?: 'rr' | 'ri' | 'ir' | 'ii'; - cache: Map; + cache: Map; } function drawContours( @@ -204,7 +204,7 @@ interface ContoursCalcOptions { timeout?: number; nbLevels: number; data: NmrData2DFt['rr']; - cache: Map; + cache: Map; } function getContours(options: ContoursCalcOptions) { @@ -216,57 +216,31 @@ function getContours(options: ContoursCalcOptions) { data, cache, } = options; - const xs = getRange(data.minX, data.maxX, data.z[0].length); - const ys = getRange(data.minY, data.maxY, data.z.length); - const conrec = new Conrec(data.z, { xs, ys, swapAxes: false }); - const max = Math.max(Math.abs(data.minZ), Math.abs(data.maxZ)); - const minLevel = calculateValueOfLevel(boundary[0], max); - const maxLevel = calculateValueOfLevel(boundary[1], max); - const diffRange = boundary[1] - boundary[0]; + const range = calculateRange(boundary, data, nbLevels, negative); - let _range = getRange(minLevel, maxLevel, Math.min(nbLevels, diffRange), 2); - if (negative) { - _range = _range.map((value) => -value); + if (isZeroRange(range)) { + return createEmptyResult(range); } - if (_range.every((r) => r === 0)) { - const emptyLine: number[] = []; + const diffRange = boundary[1] - boundary[0]; + + if (cache.has(diffRange)) { return { - contours: _range.map((r) => ({ zValue: r, lines: emptyLine })), + contours: cache.get(diffRange) ?? [], timeout: false, }; } - // assuming there is a global variable cache; - - const contoursInCache: BasicContour[] = []; - const levelsToCalculate: number[] = []; - for (const level of _range) { - if (cache.has(level)) { - contoursInCache.push(cache.get(level) as BasicContour); - } else { - levelsToCalculate.push(level); - } - } - - const conrecResult = conrec.drawContour({ + const conrec = initializeConrec(data); + const result = conrec.drawContour({ contourDrawer: 'basic', - levels: levelsToCalculate, + levels: range, timeout, }); + cache.set(diffRange, result.contours); - // TODO - // cache may of course not be Global ! - // moreover cache must be by spectrum !!! - for (const contour of conrecResult.contours) { - cache.set(contour.zValue, contour); - } - - return { - contours: [...contoursInCache, ...conrecResult.contours], - timeout: conrecResult.timeout, - }; + return result; } /** @@ -285,6 +259,38 @@ function calculateValueOfLevel(level: number, max: number, invert = false) { return (max * (2 ** (level / 10) - 1)) / (2 ** 10 - 1); } +function calculateRange( + boundary: [number, number], + data: ContoursCalcOptions['data'], + nbLevels: number, + negative: boolean, +): number[] { + const max = Math.max(Math.abs(data.minZ), Math.abs(data.maxZ)); + const minLevel = calculateValueOfLevel(boundary[0], max); + const maxLevel = calculateValueOfLevel(boundary[1], max); + const diffRange = boundary[1] - boundary[0]; + + const range = getRange(minLevel, maxLevel, Math.min(nbLevels, diffRange), 2); + return negative ? range.map((value) => -value) : range; +} + +function isZeroRange(range: number[]): boolean { + return range.every((r) => r === 0); +} + +function createEmptyResult(range: number[]) { + return { + contours: range.map((r) => ({ zValue: r, lines: [] })), + timeout: false, + }; +} + +function initializeConrec(data: ContoursCalcOptions['data']): Conrec { + const xs = getRange(data.minX, data.maxX, data.z[0].length); + const ys = getRange(data.minY, data.maxY, data.z.length); + return new Conrec(data.z, { xs, ys, swapAxes: false }); +} + export { drawContours, contoursManager,