Inline EPSILON and TOLERANCE for better performance in Numerical.

This commit is contained in:
Jürg Lehni 2013-12-17 15:27:55 +01:00
parent 6b4917f4a8
commit 76ea7ef066

View file

@ -56,12 +56,14 @@ var Numerical = new function() {
sqrt = Math.sqrt,
pow = Math.pow,
cos = Math.cos,
PI = Math.PI;
PI = Math.PI,
TOLERANCE = 10e-6,
EPSILON = 10e-12;
return {
TOLERANCE: 10e-6,
TOLERANCE: TOLERANCE,
// Precision when comparing against 0
EPSILON: 10e-12,
EPSILON: EPSILON,
// Kappa, see: http://www.whizkidtech.redprince.net/bezier/circle/kappa/
KAPPA: 4 * (sqrt(2) - 1) / 3,
@ -70,7 +72,7 @@ var Numerical = new function() {
* Numerical.EPSILON.
*/
isZero: function(val) {
return abs(val) <= Numerical.EPSILON;
return abs(val) <= EPSILON;
},
/**
@ -127,10 +129,9 @@ var Numerical = new function() {
* a*x^2 + b*x + c = 0
*/
solveQuadratic: function(a, b, c, roots, min, max) {
var epsilon = Numerical.EPSILON,
unbound = min === undefined,
minE = min - epsilon,
maxE = max + epsilon,
var unbound = min === undefined,
minE = min - EPSILON,
maxE = max + EPSILON,
count = 0;
function add(root) {
@ -141,17 +142,17 @@ var Numerical = new function() {
// Code ported over and adapted from Uintah library (MIT license).
// If a is 0, equation is actually linear, return 0 or 1 easy roots.
if (abs(a) < epsilon) {
if (abs(b) >= epsilon)
if (abs(a) < EPSILON) {
if (abs(b) >= EPSILON)
return add(-c / b);
// If all the coefficients are 0, we have infinite solutions!
return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions
return abs(c) < EPSILON ? -1 : 0; // Infinite or 0 solutions
}
// Convert to normal form: x^2 + px + q = 0
var p = b / (2 * a);
var q = c / a;
var p2 = p * p;
if (p2 < q - epsilon)
if (p2 < q - EPSILON)
return 0;
var s = p2 > q ? sqrt(p2 - q) : 0;
add (s - p);
@ -167,14 +168,13 @@ var Numerical = new function() {
* a*x^3 + b*x^2 + c*x + d = 0
*/
solveCubic: function(a, b, c, d, roots, min, max) {
var epsilon = Numerical.EPSILON;
// If a is 0, equation is actually quadratic.
if (abs(a) < epsilon)
if (abs(a) < EPSILON)
return Numerical.solveQuadratic(b, c, d, roots, min, max);
var unbound = min === undefined,
minE = min - epsilon,
maxE = max + epsilon,
minE = min - EPSILON,
maxE = max + EPSILON,
count = 0;
function add(root) {
@ -197,8 +197,8 @@ var Numerical = new function() {
D = q * q - ppp;
// Substitute x = y - b/3 to eliminate quadric term: x^3 +px + q = 0
b /= 3;
if (abs(D) < epsilon) {
if (abs(q) < epsilon) // One triple solution.
if (abs(D) < EPSILON) {
if (abs(q) < EPSILON) // One triple solution.
return add(-b);
// One single and one double solution.
var sqp = sqrt(p),