Skip to content

Commit 2ab269d

Browse files
authored
Merge pull request #449 from wearepal/idw-interpolation
Fixed caching logic for interpolation
2 parents cb82d23 + c0e0fa1 commit 2ab269d

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

app/javascript/projects/modelling/components/interpolation_component.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { ProjectProperties } from "."
55
import { numberSocket, numericDataSocket } from "../socket_types"
66
import { workerPool } from '../../../modelling/workerPool'
77
import { maskFromExtentAndShape } from "../bounding_box"
8-
import { NumericTileGrid } from "../tile_grid"
8+
import { NumericTileGrid, TileGridJSON } from "../tile_grid"
99
import { SelectControl, SelectControlOptions } from "../controls/select"
1010
import { InterpolationType } from "../../../modelling/worker/interpolation"
1111
import { NumericConstant } from "../numeric_constant"
12+
import { isEqual } from "lodash"
1213

1314
interface InterpolationMethodOption extends SelectControlOptions {
1415
value: InterpolationType
@@ -32,16 +33,21 @@ export class InterpolationComponent extends BaseComponent {
3233
maxdist : number
3334
p : number
3435
closest_points : number
35-
cache : Map<number, Map<NumericTileGrid, NumericTileGrid>>
36+
cachedInputs: NumericTileGrid[]
37+
cachedOutputs: Map<string, NumericTileGrid>
38+
3639

3740
constructor(projectProps : ProjectProperties) {
3841
super("Interpolation")
3942
this.category = "Calculations"
4043
this.projectProps = projectProps
44+
this.cachedInputs = []
45+
this.cachedOutputs = new Map()
46+
47+
// default values
4148
this.maxdist = 50
4249
this.p = 2
4350
this.closest_points = 10
44-
this.cache = new Map()
4551
}
4652

4753
async builder(node: Node) {
@@ -76,27 +82,29 @@ export class InterpolationComponent extends BaseComponent {
7682
const maxDist = inputs['maxdist'].length > 0 ? (inputs['maxdist'][0] as NumericConstant).value : this.maxdist
7783
const p = inputs['p'].length > 0 ? (inputs['p'][0] as NumericConstant).value : this.p
7884
const closest_points = inputs['closest_points'].length > 0 ? (inputs['closest_points'][0] as NumericConstant).value : this.closest_points
85+
7986
const input = inputs['input'][0] as NumericTileGrid
8087

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()))
8589

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)
8797
return
8898
}
8999
}
90100

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+
}
100108
}
101109

102110
}

app/javascript/projects/modelling/tile_grid.ts

+4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ export class NumericTileGrid extends TileGrid {
239239
this.minMax = null
240240
}
241241

242+
getData(): Float32Array {
243+
return this.data
244+
}
245+
242246
iterate(callback: (x: number, y: number, value: number) => void) {
243247
const { x, y, width, height } = this
244248
for (let i = x; i < x + width; i++) {

0 commit comments

Comments
 (0)