@@ -5,10 +5,11 @@ import { ProjectProperties } from "."
5
5
import { numberSocket , numericDataSocket } from "../socket_types"
6
6
import { workerPool } from '../../../modelling/workerPool'
7
7
import { maskFromExtentAndShape } from "../bounding_box"
8
- import { NumericTileGrid } from "../tile_grid"
8
+ import { NumericTileGrid , TileGridJSON } from "../tile_grid"
9
9
import { SelectControl , SelectControlOptions } from "../controls/select"
10
10
import { InterpolationType } from "../../../modelling/worker/interpolation"
11
11
import { NumericConstant } from "../numeric_constant"
12
+ import { isEqual } from "lodash"
12
13
13
14
interface InterpolationMethodOption extends SelectControlOptions {
14
15
value : InterpolationType
@@ -32,16 +33,21 @@ export class InterpolationComponent extends BaseComponent {
32
33
maxdist : number
33
34
p : number
34
35
closest_points : number
35
- cache : Map < number , Map < NumericTileGrid , NumericTileGrid > >
36
+ cachedInputs : NumericTileGrid [ ]
37
+ cachedOutputs : Map < string , NumericTileGrid >
38
+
36
39
37
40
constructor ( projectProps : ProjectProperties ) {
38
41
super ( "Interpolation" )
39
42
this . category = "Calculations"
40
43
this . projectProps = projectProps
44
+ this . cachedInputs = [ ]
45
+ this . cachedOutputs = new Map ( )
46
+
47
+ // default values
41
48
this . maxdist = 50
42
49
this . p = 2
43
50
this . closest_points = 10
44
- this . cache = new Map ( )
45
51
}
46
52
47
53
async builder ( node : Node ) {
@@ -76,27 +82,29 @@ export class InterpolationComponent extends BaseComponent {
76
82
const maxDist = inputs [ 'maxdist' ] . length > 0 ? ( inputs [ 'maxdist' ] [ 0 ] as NumericConstant ) . value : this . maxdist
77
83
const p = inputs [ 'p' ] . length > 0 ? ( inputs [ 'p' ] [ 0 ] as NumericConstant ) . value : this . p
78
84
const closest_points = inputs [ 'closest_points' ] . length > 0 ? ( inputs [ 'closest_points' ] [ 0 ] as NumericConstant ) . value : this . closest_points
85
+
79
86
const input = inputs [ 'input' ] [ 0 ] as NumericTileGrid
80
87
81
- // TODO: Caching doesn't work
82
- if ( this . cache . has ( maxDist ) ) {
83
-
84
- if ( this . cache . get ( maxDist ) ?. has ( input ) ) {
88
+ let cacheIdx = this . cachedInputs . findIndex ( cachedInput => isEqual ( cachedInput . getData ( ) , input . getData ( ) ) )
85
89
86
- outputs [ 'output' ] = this . cache . get ( maxDist ) ?. get ( input )
90
+ if ( cacheIdx === - 1 ) {
91
+ this . cachedInputs . push ( input )
92
+ cacheIdx = this . cachedInputs . length - 1
93
+ } else {
94
+ const code = `${ cacheIdx } _${ method . value } _${ maxDist } _${ p } _${ closest_points } `
95
+ if ( this . cachedOutputs . has ( code ) ) {
96
+ outputs [ 'output' ] = this . cachedOutputs . get ( code )
87
97
return
88
98
}
89
99
}
90
100
91
- const out = await workerPool . queue ( async worker =>
92
- worker . interpolateGrid ( inputs [ 'input' ] [ 0 ] , mask , method . value , maxDist , p , closest_points )
93
- )
94
-
95
- const map = this . cache . get ( maxDist ) || new Map ( )
96
- map . set ( input , out )
97
- this . cache . set ( maxDist , map )
98
-
99
- outputs [ 'output' ] = out
101
+ if ( outputs [ 'output' ] === undefined ) {
102
+ const out = await workerPool . queue ( async worker =>
103
+ worker . interpolateGrid ( input , mask , method . value , maxDist , p , closest_points )
104
+ )
105
+ this . cachedOutputs . set ( `${ cacheIdx } _${ method . value } _${ maxDist } _${ p } _${ closest_points } ` , out )
106
+ outputs [ 'output' ] = out
107
+ }
100
108
}
101
109
102
110
}
0 commit comments