Skip to content

Commit

Permalink
Merge pull request #233 from wearepal/bugfix
Browse files Browse the repository at this point in the history
Add Merge operation, adjust mask numeric data component
  • Loading branch information
paulthatjazz authored Dec 20, 2023
2 parents 46665af + f58fe31 commit 9c21115
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/javascript/modelling/worker/performOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ operations.set('Intersection', { inputType: BooleanTileGrid, outputType: Boolean
operations.set('Set difference', { inputType: BooleanTileGrid, outputType: BooleanTileGrid, fn: (a, b) => (a && !b) })
operations.set('Symmetric difference', { inputType: BooleanTileGrid, outputType: BooleanTileGrid, fn: (...inputs) => inputs.reduce((a, b) => (a || b) && !(a && b)) })
operations.set('Sum', { inputType: NumericTileGrid, outputType: NumericTileGrid, fn: (...inputs) => inputs.reduce((a, b) => a + b) })
operations.set('Merge', { inputType: NumericTileGrid, outputType: NumericTileGrid, fn: (...inputs) => inputs.reduce((a, b) => isNaN(a) && isNaN(b) ? NaN : (isNaN(a) ? b : (isNaN(b) ? a : (a + b))))})
operations.set('Product', { inputType: NumericTileGrid, outputType: NumericTileGrid, fn: (...inputs) => inputs.reduce((a, b) => a * b) })
operations.set('Add', { inputType: NumericTileGrid, outputType: NumericTileGrid, fn: (a, b) => (a + b) })
operations.set('Subtract', { inputType: NumericTileGrid, outputType: NumericTileGrid, fn: (a, b) => (a - b) })
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/projects/modelling/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: S
new ExpressionComponent(),
new BinaryOpComponent('Min', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Max', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new VariadicOpComponent('Sum', '∑', numericDataSocket, numericDataSocket, 'Arithmetic'),
new VariadicOpComponent('Sum', '∑', numericDataSocket, numericDataSocket, 'Arithmetic', 'Sum all inputs'),
new VariadicOpComponent('Merge', '', numericDataSocket, numericDataSocket, 'Arithmetic', 'Merge all inputs into a single dataset, NaN logic is overridden'),
new VariadicOpComponent('Product', '∏', numericDataSocket, numericDataSocket, 'Arithmetic'),
new BinaryOpComponent('Add', '+', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Subtract', '−', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export class MaskNumericDataComponent extends BaseComponent {

editorNode.meta.previousInputs = [num, mask]

const out = editorNode.meta.output = outputs['out'] = new NumericTileGrid(mask.zoom, mask.x, mask.y, mask.width, mask.height)
const out = editorNode.meta.output = outputs['out'] = new NumericTileGrid(mask.zoom, mask.x, mask.y, mask.width, mask.height, NaN)

mask.iterate((x, y, value) => out.set(x, y, value ? num.get(x, y, mask.zoom) : 0))
mask.iterate((x, y, value) => out.set(x, y, value ? num.get(x, y, mask.zoom) : NaN))

}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ export class VariadicOpComponent extends BaseComponent {
operation: string
inputSocket: Socket
outputSocket: Socket
tooltip: string | undefined

constructor(operation: string, operator: string, inputSocket: Socket, outputSocket: Socket, category: string) {
constructor(operation: string, operator: string, inputSocket: Socket, outputSocket: Socket, category: string, tooltip?: string) {
super(operation)
this.operation = operation
this.operator = operator
this.inputSocket = inputSocket
this.outputSocket = outputSocket
this.category = category
this.contextMenuName = `${operation} (${operator})`
this.contextMenuName = operator !== "" ? `${operation} (${operator})` : operation
this.tooltip = tooltip
}

async builder(node: Node) {
node.meta.toolTip = this.tooltip
node.addControl(new PreviewControl(() =>
node.meta.output as any || new BooleanTileGrid(0, 0, 0, 1, 1)
))
Expand Down
4 changes: 3 additions & 1 deletion app/javascript/projects/modelling/controls/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ const Preview = ({ getTileGrid }: PreviewProps) => {
}
else if (tileGrid instanceof NumericTileGrid) {
let [min, max] = tileGrid.getMinMax()
// if min === max, this returns a blank canvas. Fix by setting min to max - 1
if(min === max) min -= 1

for (let x = 0; x < tileGrid.width; ++x) {
for (let y = 0; y < tileGrid.height; ++y) {
let val = tileGrid.get(tileGrid.x + x, tileGrid.y + y)
ctx.fillStyle = isFinite(val) ? `rgba(255, 255, 255, ${(val - min) / (max - min)})` : 'rgba(255, 255, 255, 0)'
ctx.fillStyle = isFinite(val) ? `rgba(255, 255, 255, ${(val - min) / (max - min)})` : 'rgba(255, 0, 0, 0)'
ctx.fillRect(x, y, 1, 1)
}
}
Expand Down

0 comments on commit 9c21115

Please sign in to comment.