Skip to content

Commit 4cc2778

Browse files
committed
chore: approach of context to keep the calculated contours
1 parent 2e8c154 commit 4cc2778

3 files changed

Lines changed: 59 additions & 22 deletions

File tree

src/component/2d/ft/Contours.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import debounce from 'lodash/debounce';
22
import get from 'lodash/get';
33
import { Spectrum2D } from 'nmr-load-save';
4-
import { memo, useMemo, useRef } from 'react';
4+
import { memo, useEffect, useMemo, useRef } from 'react';
55

66
import {
77
drawContours,
88
getDefaultContoursLevel,
99
LevelSign,
1010
} from '../../../data/data2d/Spectrum2D/contours';
1111
import { useChartData } from '../../context/ChartContext';
12+
import { useContourCache } from '../../context/ContourCacheContext';
1213
import { usePreferences } from '../../context/PreferencesContext';
1314
import { useToaster } from '../../context/ToasterContext';
1415
import { useActiveSpectrum } from '../../hooks/useActiveSpectrum';
@@ -76,20 +77,25 @@ function ContoursPaths({
7677
const activeSpectrum = useActiveSpectrum();
7778
const preferences = usePreferences();
7879
const level = useContoursLevel(spectrum, sign);
80+
const [cache, setCache] = useContourCache();
7981

8082
const contours = useMemo(() => {
8183
const { contours, timeout } = drawContours(
8284
level,
8385
spectrum,
86+
cache,
8487
sign === 'negative',
8588
);
8689
if (timeout) {
8790
onTimeout();
8891
}
89-
return contours;
90-
}, [spectrum, level, onTimeout, sign]);
92+
setCache(cache);
93+
return { contours, cache };
94+
}, [spectrum, level, onTimeout, sign, cache, setCache]);
9195

92-
const path = usePath(spectrum, contours);
96+
// useEffect(() => {}, [setCache, contours]);
97+
98+
const path = usePath(spectrum, contours.contours);
9399

94100
const opacity =
95101
activeSpectrum === null || spectrumID === activeSpectrum.id
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, { createContext, useState } from 'react';
2+
3+
const initialContourCache: Record<string, any> = {};
4+
const ContourCacheContext = createContext(null);
5+
export const ContourCacheProvider = ContourCacheContext.Provider;
6+
export function useContourCache() {
7+
React.useContext(ContourCacheContext);
8+
return useState(initialContourCache);
9+
}

src/data/data2d/Spectrum2D/contours.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,45 +177,67 @@ function range(from: number, to: number, step: number) {
177177
function drawContours(
178178
level: ContourItem,
179179
spectrum: Spectrum2D,
180+
contourCache: Record<string, any>,
180181
negative = false,
181182
quadrant = 'rr',
182183
) {
183184
const { contourLevels, numberOfLayers } = level;
185+
const key = negative ? 'negative' : 'positive';
184186

185-
return getContours({
187+
const nbLevels = Math.min(
188+
numberOfLayers,
189+
contourLevels[1] - contourLevels[0],
190+
);
191+
192+
const { id, data } = spectrum;
193+
if (!contourCache[id]) {
194+
contourCache[id] = {};
195+
}
196+
197+
if (!(key in contourCache[id])) {
198+
contourCache[id][key] = { contours: [], timeout: false };
199+
}
200+
201+
const oneSenseContours = contourCache[id][key].contours;
202+
const selectedLevels = getRange(
203+
Math.max(0, contourLevels[0]),
204+
contourLevels[1],
205+
nbLevels,
206+
).map((e) => Math.round(e));
207+
208+
const levels = selectedLevels.filter((level) => !oneSenseContours[level]);
209+
const { contours, timeout } = getContours({
210+
levels,
186211
negative,
187-
boundary: contourLevels,
188-
nbLevels: numberOfLayers,
189-
data: spectrum.data[quadrant],
212+
data: data[quadrant],
190213
});
214+
215+
for (const [i, level] of contours.entries()) {
216+
oneSenseContours[levels[i]] = level;
217+
}
218+
contourCache[id][key] = { contours: oneSenseContours, timeout };
219+
220+
return {
221+
contours: selectedLevels.map((level) => oneSenseContours[level]),
222+
timeout,
223+
};
191224
}
192225

193226
interface ContoursCalcOptions {
194-
boundary: [number, number];
227+
levels: number[];
195228
negative?: boolean;
196229
timeout?: number;
197-
nbLevels: number;
198230
data: NmrData2DFt['rr'];
199231
}
200232

201233
function getContours(options: ContoursCalcOptions) {
202-
const {
203-
boundary,
204-
negative = false,
205-
timeout = 2000,
206-
nbLevels,
207-
data,
208-
} = options;
234+
const { levels, negative = false, timeout = 2000, data } = options;
209235
const xs = getRange(data.minX, data.maxX, data.z[0].length);
210236
const ys = getRange(data.minY, data.maxY, data.z.length);
211237
const conrec = new Conrec(data.z, { xs, ys, swapAxes: false });
212238
const max = Math.max(Math.abs(data.minZ), Math.abs(data.maxZ));
213-
const minLevel = calculateValueOfLevel(boundary[0], max);
214-
const maxLevel = calculateValueOfLevel(boundary[1], max);
215-
216-
const diffRange = boundary[1] - boundary[0];
217239

218-
let _range = getRange(minLevel, maxLevel, Math.min(nbLevels, diffRange), 2);
240+
let _range = levels.map((level) => calculateValueOfLevel(level, max));
219241
if (negative) {
220242
_range = _range.map((value) => -value);
221243
}

0 commit comments

Comments
 (0)