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 - Ternary Functions #4709

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion Source/Core/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ define([
};

/**
* Constraint a value to lie between two values.
* Constrains a value to lie between two values.
*
* @param {Number} value The value to constrain.
* @param {Number} min The minimum value.
Expand Down
41 changes: 32 additions & 9 deletions Source/Scene/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ define([
var unaryFunctions = {
abs : Math.abs,
sqrt : Math.sqrt,
cos : Math.cos,
sin : Math.sin,
cos : Math.cos,
tan : Math.tan,
acos : Math.acos,
asin : Math.asin,
atan : Math.atan,
radians : CesiumMath.toRadians,
degrees : CesiumMath.toDegrees
degrees : CesiumMath.toDegrees,
radians : CesiumMath.toRadians
};

var ternaryFunctions = {
clamp : CesiumMath.clamp,
mix: CesiumMath.lerp
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to mix : CesiumMath.lerp.

};

/**
Expand Down Expand Up @@ -370,6 +375,16 @@ define([
//>>includeEnd('debug');
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (defined(ternaryFunctions[call])) {
//>>includeStart('debug', pragmas.debug);
if (args.length < 3 || args.length > 3) {
throw new DeveloperError('Error: ' + call + ' requires exactly three arguments.');
}
//>>includeEnd('debug');
left = createRuntimeAst(expression, args[0]);
right = createRuntimeAst(expression, args[1]);
var test = createRuntimeAst(expression, args[2]);
return new Node(ExpressionNodeType.TERNARY, call, left, right, test);
} else if (defined(binaryFunctions[call])) {
//>>includeStart('debug', pragmas.debug);
if (args.length < 2 || args.length > 2) {
Expand Down Expand Up @@ -647,6 +662,8 @@ define([
if (node._value === 'TILES3D_TILESET_TIME') {
node.evaluate = evaluateTime;
}
} else if (node._type === ExpressionNodeType.TERNARY) {
node.evaluate = getEvaluateTernaryFunction(node._value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Place after binary. Also can you put the unary before binary in this function - not caused by you but it's good for consistency.

} else {
node.evaluate = node._evaluateLiteral;
}
Expand All @@ -670,6 +687,13 @@ define([
};
}

function getEvaluateTernaryFunction(call) {
var evaluate = ternaryFunctions[call];
return function(feature) {
return evaluate(this._left.evaluate(feature), this._right.evaluate(feature));
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be:
return evaluate(this._left.evaluate(feature), this._right.evaluate(feature), this._test.evaluate(feature));

Copy link
Contributor

Choose a reason for hiding this comment

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

};
}

Node.prototype._evaluateLiteral = function(frameState, feature) {
return this._value;
};
Expand Down Expand Up @@ -1186,12 +1210,6 @@ define([
return 'float(' + left + ')';
} else if (defined(unaryFunctions[value])) {
return value + '(' + left + ')';
} else if (value === 'abs') {
return 'abs(' + left + ')';
} else if (value === 'cos') {
return 'cos(' + left + ')';
} else if (value === 'sqrt') {
return 'sqrt(' + left + ')';
}
//>>includeStart('debug', pragmas.debug);
else if ((value === 'isNaN') || (value === 'isFinite') || (value === 'String')) {
Expand Down Expand Up @@ -1327,6 +1345,11 @@ define([
if (value === 'TILES3D_TILESET_TIME') {
return 'u_tilesetTime';
}
break;
case ExpressionNodeType.TERNARY:
if (defined(ternaryFunctions[value])) {
return value + '(' + left + ', ' + right + ', ' + test + ')';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Place this near the unary functions.

Copy link
Contributor Author

@Dylan-Brown Dylan-Brown Dec 7, 2016

Choose a reason for hiding this comment

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

@lilleyse So, now the shader expression test cases are passing, but the test cases for 'evaluates clamp function' and 'evaluates mix function' don't make sense; for mix, "Expected NaN to equal 1." is the error message, and "DeveloperError: max is required." for clamp. These don't make sense to me, since three arguments are passed for clamp and mix doesn't seem to be getting any arguments, given that the result is NaN.

Copy link
Contributor Author

@Dylan-Brown Dylan-Brown Dec 15, 2016

Choose a reason for hiding this comment

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

@lilleyse isExactClass, isClass, getExactClassName are functions that are also now failing in my code. The only other place I see this referernced is in PR #4625
Actually, nevermind, build is passing. Should be done!

}
};

Expand Down
31 changes: 16 additions & 15 deletions Source/Scene/ExpressionNodeType.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ define([
var ExpressionNodeType = {
VARIABLE : 0,
UNARY : 1,
BINARY : 2,
CONDITIONAL : 3,
MEMBER : 4,
FUNCTION_CALL : 5,
ARRAY : 6,
REGEX: 7,
VARIABLE_IN_STRING : 8,
LITERAL_NULL : 9,
LITERAL_BOOLEAN : 10,
LITERAL_NUMBER : 11,
LITERAL_STRING : 12,
LITERAL_COLOR : 13,
LITERAL_REGEX : 14,
LITERAL_UNDEFINED : 15,
LITERAL_GLOBAL : 16
TERNARY : 2,
BINARY : 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Place ternary after binary.

CONDITIONAL : 4,
MEMBER : 5,
FUNCTION_CALL : 6,
ARRAY : 7,
REGEX: 8,
VARIABLE_IN_STRING : 9,
LITERAL_NULL : 10,
LITERAL_BOOLEAN : 11,
LITERAL_NUMBER : 12,
LITERAL_STRING : 13,
LITERAL_COLOR : 14,
LITERAL_REGEX : 15,
LITERAL_UNDEFINED : 16,
LITERAL_GLOBAL : 17
};

return freezeObject(ExpressionNodeType);
Expand Down
66 changes: 66 additions & 0 deletions Specs/Scene/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,58 @@ defineSuite([
}).toThrowDeveloperError();
});

it('evaluates clamp function', function() {
var expression = new Expression('clamp(50.0, 0.0, 100.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(50.0);

expression = new Expression('clamp(50.0, 0.0, 25.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(25.0);

expression = new Expression('clamp(50.0, 75.0, 100.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(75.0);
});

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

expect(function() {
return new Expression('clamp(1)');
}).toThrowDeveloperError();

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

expect(function() {
return new Expression('clamp(1, 2, 3, 4)');
}).toThrowDeveloperError();
});

it('evaluates mix function', function() {
var expression = new Expression('mix(0.0, 2.0, 0.5)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);
});

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

expect(function() {
return new Expression('mix(1)');
}).toThrowDeveloperError();

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

expect(function() {
return new Expression('mix(1, 2, 3, 4)');
}).toThrowDeveloperError();
});

it('evaluates atan2 function', function() {
var expression = new Expression('atan2(0,1)');
expect(expression.evaluate(frameState, undefined)).toEqualEpsilon(0.0, CesiumMath.EPSILON10);
Expand Down Expand Up @@ -1879,6 +1931,20 @@ defineSuite([
expect(shaderExpression).toEqual(expected);
});

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

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

it('gets shader expression for atan2', function() {
var expression = new Expression('atan2(0.0,1.0)');
var shaderExpression = expression.getShaderExpression('', {});
Expand Down