@@ -177,45 +177,67 @@ function range(from: number, to: number, step: number) {
177177function 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
193226interface ContoursCalcOptions {
194- boundary : [ number , number ] ;
227+ levels : number [ ] ;
195228 negative ?: boolean ;
196229 timeout ?: number ;
197- nbLevels : number ;
198230 data : NmrData2DFt [ 'rr' ] ;
199231}
200232
201233function 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