1<!DOCTYPE html> 2<!-- 3Copyright (c) 2014 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7<link rel="import" href="/tracing/base/base.html"> 8<script src="/gl-matrix-min.js"></script> 9 10<script> 11'use strict'; 12 13// In node, the script-src for gl-matrix-min above brings in glmatrix into 14// a module, instead of into the global scope. Whereas, Tracing code 15// assumes that glMatrix is in the global scope. So, in Node only, we 16// require() it in, and then take all its exports and shove them into the 17// global scope by hand. 18(function(global) { 19 if (tr.isNode) { 20 var glMatrixAbsPath = HTMLImportsLoader.hrefToAbsolutePath( 21 '/gl-matrix-min.js'); 22 var glMatrixModule = require(glMatrixAbsPath); 23 for (var exportName in glMatrixModule) { 24 global[exportName] = glMatrixModule[exportName]; 25 } 26 } 27})(this); 28</script> 29 30<script> 31'use strict'; 32 33tr.exportTo('tr.b', function() { 34 /* Returns true when x and y are within delta of each other. */ 35 function approximately(x, y, delta) { 36 if (delta === undefined) 37 delta = 1e-9; 38 return Math.abs(x - y) < delta; 39 } 40 41 function clamp(x, lo, hi) { 42 return Math.min(Math.max(x, lo), hi); 43 } 44 45 function lerp(percentage, lo, hi) { 46 var range = hi - lo; 47 return lo + percentage * range; 48 } 49 50 function normalize(value, lo, hi) { 51 return (value - lo) / (hi - lo); 52 } 53 54 function deg2rad(deg) { 55 return (Math.PI * deg) / 180.0; 56 } 57 58 /* The Gauss error function gives the probability that a measurement (which is 59 * under the influence of normally distributed errors with standard deviation 60 * sigma = 1) is less than x from the mean value of the standard normal 61 * distribution. 62 * https://www.desmos.com/calculator/t1v4bdpske 63 * 64 * @param {number} x A tolerance for error. 65 * @return {number} The probability that a measurement is less than |x| from 66 * the mean value of the standard normal distribution. 67 */ 68 function erf(x) { 69 // save the sign of x 70 // erf(-x) = -erf(x); 71 var sign = (x >= 0) ? 1 : -1; 72 x = Math.abs(x); 73 74 // constants 75 var a1 = 0.254829592; 76 var a2 = -0.284496736; 77 var a3 = 1.421413741; 78 var a4 = -1.453152027; 79 var a5 = 1.061405429; 80 var p = 0.3275911; 81 82 // Abramowitz and Stegun formula 7.1.26 83 // maximum error: 1.5e-7 84 var t = 1.0 / (1.0 + p * x); 85 var y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * 86 Math.exp(-x * x); 87 return sign * y; 88 } 89 90 var tmp_vec2 = vec2.create(); 91 var tmp_vec2b = vec2.create(); 92 var tmp_vec4 = vec4.create(); 93 var tmp_mat2d = mat2d.create(); 94 95 vec2.createFromArray = function(arr) { 96 if (arr.length != 2) 97 throw new Error('Should be length 2'); 98 var v = vec2.create(); 99 vec2.set(v, arr[0], arr[1]); 100 return v; 101 }; 102 103 vec2.createXY = function(x, y) { 104 var v = vec2.create(); 105 vec2.set(v, x, y); 106 return v; 107 }; 108 109 vec2.toString = function(a) { 110 return '[' + a[0] + ', ' + a[1] + ']'; 111 }; 112 113 vec2.addTwoScaledUnitVectors = function(out, u1, scale1, u2, scale2) { 114 // out = u1 * scale1 + u2 * scale2 115 vec2.scale(tmp_vec2, u1, scale1); 116 vec2.scale(tmp_vec2b, u2, scale2); 117 vec2.add(out, tmp_vec2, tmp_vec2b); 118 }; 119 120 vec2.interpolatePiecewiseFunction = function(points, x) { 121 if (x < points[0][0]) 122 return points[0][1]; 123 for (var i = 1; i < points.length; ++i) { 124 if (x < points[i][0]) { 125 var percent = normalize(x, points[i - 1][0], points[i][0]); 126 return lerp(percent, points[i - 1][1], points[i][1]); 127 } 128 } 129 return points[points.length - 1][1]; 130 }; 131 132 vec3.createXYZ = function(x, y, z) { 133 var v = vec3.create(); 134 vec3.set(v, x, y, z); 135 return v; 136 }; 137 138 vec3.toString = function(a) { 139 return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; 140 }; 141 142 mat2d.translateXY = function(out, x, y) { 143 vec2.set(tmp_vec2, x, y); 144 mat2d.translate(out, out, tmp_vec2); 145 }; 146 147 mat2d.scaleXY = function(out, x, y) { 148 vec2.set(tmp_vec2, x, y); 149 mat2d.scale(out, out, tmp_vec2); 150 }; 151 152 vec4.unitize = function(out, a) { 153 out[0] = a[0] / a[3]; 154 out[1] = a[1] / a[3]; 155 out[2] = a[2] / a[3]; 156 out[3] = 1; 157 return out; 158 }; 159 160 vec2.copyFromVec4 = function(out, a) { 161 vec4.unitize(tmp_vec4, a); 162 vec2.copy(out, tmp_vec4); 163 }; 164 165 return { 166 approximately: approximately, 167 clamp: clamp, 168 lerp: lerp, 169 normalize: normalize, 170 deg2rad: deg2rad, 171 erf: erf 172 }; 173}); 174</script> 175