From 57db9a6a12aa07f896da7f504758c33c941618a6 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 10 Feb 2022 14:24:49 +0000 Subject: [PATCH] Add floor, round and ceil --- src/ol/style/expressions.js | 36 +++++++++++++++++++ .../browser/spec/ol/style/expressions.test.js | 5 +++ 2 files changed, 41 insertions(+) diff --git a/src/ol/style/expressions.js b/src/ol/style/expressions.js index 2fef211625..f4aa6f51ea 100644 --- a/src/ol/style/expressions.js +++ b/src/ol/style/expressions.js @@ -38,6 +38,9 @@ import {log2} from '../math.js'; * * `['%', value1, value2]` returns the result of `value1 % value2` (modulo) * * `['^', value1, value2]` returns the value of `value1` raised to the `value2` power * * `['abs', value1]` returns the absolute value of `value1` + * * `['floor', value1]` returns the nearest integer less than or equal to `value1` + * * `['round', value1]` returns the nearest integer to `value1` + * * `['ceil', value1]` returns the nearest integer greater than or equal to `value1` * * `['sin', value1]` returns the sine of `value1` * * `['cos', value1]` returns the cosine of `value1` * * `['atan', value1, value2]` returns `atan2(value1, value2)`. If `value2` is not provided, returns `atan(value1)` @@ -667,6 +670,39 @@ Operators['abs'] = { }, }; +Operators['floor'] = { + getReturnType: function (args) { + return ValueTypes.NUMBER; + }, + toGlsl: function (context, args) { + assertArgsCount(args, 1); + assertNumbers(args); + return `floor(${expressionToGlsl(context, args[0])})`; + }, +}; + +Operators['round'] = { + getReturnType: function (args) { + return ValueTypes.NUMBER; + }, + toGlsl: function (context, args) { + assertArgsCount(args, 1); + assertNumbers(args); + return `floor(${expressionToGlsl(context, args[0])} + 0.5)`; + }, +}; + +Operators['ceil'] = { + getReturnType: function (args) { + return ValueTypes.NUMBER; + }, + toGlsl: function (context, args) { + assertArgsCount(args, 1); + assertNumbers(args); + return `ceil(${expressionToGlsl(context, args[0])})`; + }, +}; + Operators['sin'] = { getReturnType: function (args) { return ValueTypes.NUMBER; diff --git a/test/browser/spec/ol/style/expressions.test.js b/test/browser/spec/ol/style/expressions.test.js index 1afa4c260a..86acbf145b 100644 --- a/test/browser/spec/ol/style/expressions.test.js +++ b/test/browser/spec/ol/style/expressions.test.js @@ -243,6 +243,11 @@ describe('ol/style/expressions', function () { ['-', ['get', 'attr3'], ['get', 'attr2']], ]) ).to.eql('abs((a_attr3 - a_attr2))'); + expect(expressionToGlsl(context, ['floor', 1])).to.eql('floor(1.0)'); + expect(expressionToGlsl(context, ['round', 1])).to.eql( + 'floor(1.0 + 0.5)' + ); + expect(expressionToGlsl(context, ['ceil', 1])).to.eql('ceil(1.0)'); expect(expressionToGlsl(context, ['sin', 1])).to.eql('sin(1.0)'); expect(expressionToGlsl(context, ['cos', 1])).to.eql('cos(1.0)'); expect(expressionToGlsl(context, ['atan', 1])).to.eql('atan(1.0)');