@@ -178,7 +178,7 @@ function range(from: number, to: number, step: number) {
178178interface DrawContoursOptions {
179179 negative ?: boolean ;
180180 quadrant ?: 'rr' | 'ri' | 'ir' | 'ii' ;
181- cache : Map < number , BasicContour > ;
181+ cache : Map < number , BasicContour [ ] > ;
182182}
183183
184184function drawContours (
@@ -204,7 +204,7 @@ interface ContoursCalcOptions {
204204 timeout ?: number ;
205205 nbLevels : number ;
206206 data : NmrData2DFt [ 'rr' ] ;
207- cache : Map < number , BasicContour > ;
207+ cache : Map < number , BasicContour [ ] > ;
208208}
209209
210210function getContours ( options : ContoursCalcOptions ) {
@@ -216,57 +216,31 @@ function getContours(options: ContoursCalcOptions) {
216216 data,
217217 cache,
218218 } = options ;
219- const xs = getRange ( data . minX , data . maxX , data . z [ 0 ] . length ) ;
220- const ys = getRange ( data . minY , data . maxY , data . z . length ) ;
221- const conrec = new Conrec ( data . z , { xs, ys, swapAxes : false } ) ;
222- const max = Math . max ( Math . abs ( data . minZ ) , Math . abs ( data . maxZ ) ) ;
223- const minLevel = calculateValueOfLevel ( boundary [ 0 ] , max ) ;
224- const maxLevel = calculateValueOfLevel ( boundary [ 1 ] , max ) ;
225219
226- const diffRange = boundary [ 1 ] - boundary [ 0 ] ;
220+ const range = calculateRange ( boundary , data , nbLevels , negative ) ;
227221
228- let _range = getRange ( minLevel , maxLevel , Math . min ( nbLevels , diffRange ) , 2 ) ;
229- if ( negative ) {
230- _range = _range . map ( ( value ) => - value ) ;
222+ if ( isZeroRange ( range ) ) {
223+ return createEmptyResult ( range ) ;
231224 }
232225
233- if ( _range . every ( ( r ) => r === 0 ) ) {
234- const emptyLine : number [ ] = [ ] ;
226+ const diffRange = boundary [ 1 ] - boundary [ 0 ] ;
227+
228+ if ( cache . has ( diffRange ) ) {
235229 return {
236- contours : _range . map ( ( r ) => ( { zValue : r , lines : emptyLine } ) ) ,
230+ contours : cache . get ( diffRange ) ?? [ ] ,
237231 timeout : false ,
238232 } ;
239233 }
240234
241- // assuming there is a global variable cache;
242-
243- const contoursInCache : BasicContour [ ] = [ ] ;
244- const levelsToCalculate : number [ ] = [ ] ;
245- for ( const level of _range ) {
246- if ( cache . has ( level ) ) {
247- contoursInCache . push ( cache . get ( level ) as BasicContour ) ;
248- } else {
249- levelsToCalculate . push ( level ) ;
250- }
251- }
252-
253- const conrecResult = conrec . drawContour ( {
235+ const conrec = initializeConrec ( data ) ;
236+ const result = conrec . drawContour ( {
254237 contourDrawer : 'basic' ,
255- levels : levelsToCalculate ,
238+ levels : range ,
256239 timeout,
257240 } ) ;
241+ cache . set ( diffRange , result . contours ) ;
258242
259- // TODO
260- // cache may of course not be Global !
261- // moreover cache must be by spectrum !!!
262- for ( const contour of conrecResult . contours ) {
263- cache . set ( contour . zValue , contour ) ;
264- }
265-
266- return {
267- contours : [ ...contoursInCache , ...conrecResult . contours ] ,
268- timeout : conrecResult . timeout ,
269- } ;
243+ return result ;
270244}
271245
272246/**
@@ -285,6 +259,38 @@ function calculateValueOfLevel(level: number, max: number, invert = false) {
285259 return ( max * ( 2 ** ( level / 10 ) - 1 ) ) / ( 2 ** 10 - 1 ) ;
286260}
287261
262+ function calculateRange (
263+ boundary : [ number , number ] ,
264+ data : ContoursCalcOptions [ 'data' ] ,
265+ nbLevels : number ,
266+ negative : boolean ,
267+ ) : number [ ] {
268+ const max = Math . max ( Math . abs ( data . minZ ) , Math . abs ( data . maxZ ) ) ;
269+ const minLevel = calculateValueOfLevel ( boundary [ 0 ] , max ) ;
270+ const maxLevel = calculateValueOfLevel ( boundary [ 1 ] , max ) ;
271+ const diffRange = boundary [ 1 ] - boundary [ 0 ] ;
272+
273+ const range = getRange ( minLevel , maxLevel , Math . min ( nbLevels , diffRange ) , 2 ) ;
274+ return negative ? range . map ( ( value ) => - value ) : range ;
275+ }
276+
277+ function isZeroRange ( range : number [ ] ) : boolean {
278+ return range . every ( ( r ) => r === 0 ) ;
279+ }
280+
281+ function createEmptyResult ( range : number [ ] ) {
282+ return {
283+ contours : range . map ( ( r ) => ( { zValue : r , lines : [ ] } ) ) ,
284+ timeout : false ,
285+ } ;
286+ }
287+
288+ function initializeConrec ( data : ContoursCalcOptions [ 'data' ] ) : Conrec {
289+ const xs = getRange ( data . minX , data . maxX , data . z [ 0 ] . length ) ;
290+ const ys = getRange ( data . minY , data . maxY , data . z . length ) ;
291+ return new Conrec ( data . z , { xs, ys, swapAxes : false } ) ;
292+ }
293+
288294export {
289295 drawContours ,
290296 contoursManager ,
0 commit comments