Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3D Tiles binary functions #4688

Merged
merged 18 commits into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Apps/Sandcastle/gallery/3D Tiles Point Cloud Styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,23 @@
});

addStyle('Arc Trigonometric Functions', {
color : "color() * acos(degrees(${temperature})) + color() * asin(${temperature}) + color() * atan(${temperature})"
color : "color() * acos(degrees(${temperature})) + color() * asin(${temperature}) + color() * atan(${temperature}) + color() * atan2(${POSITION}[0],${temperature})",
pointSize : "5"
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #4628 in just incorporate atan2 to the Trigonometric Functions style.


addStyle('Sqrt', {
color : "color() * sqrt(${temperature})",
pointSize : "10"
pointSize : "5"
});

addStyle('Pow', {
color : "color() * pow(${temperature}, 3)",
pointSize : "5"
});

addStyle('Min and Max', {
color : "rgb(min(${POSITION}[0], 0.75) * 255, max(${POSITION}[2], 0.25) * 255, 255)",
pointSize : "5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min and max could be combined into one style, min could affect the red channel and max could affect the green channel.

});

addStyle('Secondary Color', {
Expand Down
35 changes: 32 additions & 3 deletions Source/Scene/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ define([
}
};

var binaryFunctions = {
atan2 : Math.atan2,
pow : Math.pow,
min : Math.min,
max : Math.max
};

var unaryFunctions = {
abs : Math.abs,
sqrt : Math.sqrt,
Expand Down Expand Up @@ -275,7 +282,7 @@ define([
function parseCall(expression, ast) {
var args = ast.arguments;
var call;
var val;
var val, left, right;

// Member function calls
if (ast.callee.type === 'MemberExpression') {
Expand All @@ -295,8 +302,8 @@ define([
return new Node(ExpressionNodeType.LITERAL_NULL, null);
}
}
var left = createRuntimeAst(expression, object);
var right = createRuntimeAst(expression, args[0]);
left = createRuntimeAst(expression, object);
right = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.FUNCTION_CALL, call, left, right);
} else if (call === 'toString') {
val = createRuntimeAst(expression, object);
Expand Down Expand Up @@ -363,6 +370,15 @@ define([
//>>includeEnd('debug');
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (defined(binaryFunctions[call])) {
//>>includeStart('debug', pragmas.debug);
if (args.length < 2 || args.length > 2) {
throw new DeveloperError('Error: ' + call + ' requires exactly two arguments.');
}
//>>includeEnd('debug');
left = createRuntimeAst(expression, args[0]);
right = createRuntimeAst(expression, args[1]);
return new Node(ExpressionNodeType.BINARY, call, left, right);
} else if (call === 'Boolean') {
if (args.length === 0) {
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
Expand Down Expand Up @@ -586,6 +602,8 @@ define([
node.evaluate = node._evaluateRegExpMatch;
} else if (node._value === '!~') {
node.evaluate = node._evaluateRegExpNotMatch;
} else if (defined(binaryFunctions[node._value])) {
node.evaluate = getEvaluateBinaryFunction(node._value);
}
} else if (node._type === ExpressionNodeType.UNARY) {
if (node._value === '!') {
Expand Down Expand Up @@ -638,6 +656,13 @@ define([
return feature._content._tileset.timeSinceLoad;
}

function getEvaluateBinaryFunction(call) {
var evaluate = binaryFunctions[call];
return function(feature) {
return evaluate(this._left.evaluate(feature), this._right.evaluate(feature));
};
}

function getEvaluateUnaryFunction(call) {
var evaluate = unaryFunctions[call];
return function(feature) {
Expand Down Expand Up @@ -1182,6 +1207,10 @@ define([
return '(' + left + ' == ' + right + ')';
} else if (value === '!==') {
return '(' + left + ' != ' + right + ')';
} else if (value === 'atan2') {
return 'atan(' + left + ', ' + right + ')';
} else if (defined(binaryFunctions[value])) {
return value + '(' + left + ', ' + right + ')';
}
return '(' + left + ' ' + value + ' ' + right + ')';
case ExpressionNodeType.CONDITIONAL:
Expand Down
100 changes: 100 additions & 0 deletions Specs/Scene/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,78 @@ defineSuite([
}).toThrowDeveloperError();
});

it('evaluates atan2 function', function() {
var expression = new Expression('atan2(0,1)');
expect(expression.evaluate(frameState, undefined)).toEqualEpsilon(0.0, CesiumMath.EPSILON10);

expression = new Expression('atan2(1,0)');
expect(expression.evaluate(frameState, undefined)).toEqualEpsilon(0.5*Math.PI, CesiumMath.EPSILON10);
});

it('throws if atan2 function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('atan2(0.0)');
}).toThrowDeveloperError();

expect(function() {
return new Expression('atan2(1, 2, 0)');
}).toThrowDeveloperError();
});

it('evaluates pow function', function() {
var expression = new Expression('pow(5,0)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);

expression = new Expression('pow(4,2)');
expect(expression.evaluate(frameState, undefined)).toEqual(16.0);
});

it('throws if pow function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('pow(0.0)');
}).toThrowDeveloperError();

expect(function() {
return new Expression('pow(1, 2, 0)');
}).toThrowDeveloperError();
});

it('evaluates min function', function() {
var expression = new Expression('min(0,1)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('min(-1,0)');
expect(expression.evaluate(frameState, undefined)).toEqual(-1.0);
});

it('throws if min function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('min(0.0)');
}).toThrowDeveloperError();

expect(function() {
return new Expression('min(1, 2, 0)');
}).toThrowDeveloperError();
});

it('evaluates max function', function() {
var expression = new Expression('max(0,1)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);

expression = new Expression('max(-1,0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);
});

it('throws if max function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('max(0.0)');
}).toThrowDeveloperError();

expect(function() {
return new Expression('max(1, 2, 0)');
}).toThrowDeveloperError();
});

it('evaluates ternary conditional', function() {
var expression = new Expression('true ? "first" : "second"');
expect(expression.evaluate(frameState, undefined)).toEqual('first');
Expand Down Expand Up @@ -1807,6 +1879,34 @@ defineSuite([
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for atan2', function() {
var expression = new Expression('atan2(0.0,1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'atan(0.0, 1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for pow', function() {
var expression = new Expression('pow(2.0,2.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'pow(2.0, 2.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for min', function() {
var expression = new Expression('min(3.0,5.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'min(3.0, 5.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for max', function() {
var expression = new Expression('max(3.0,5.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'max(3.0, 5.0)';
expect(shaderExpression).toEqual(expected);
});

it('throws when getting shader expression for regex', function() {
var expression = new Expression('regExp("a").test("abc")');
expect(function() {
Expand Down