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  function clamp(x, lo, hi) {
35    return Math.min(Math.max(x, lo), hi);
36  }
37
38  function lerp(percentage, lo, hi) {
39    var range = hi - lo;
40    return lo + percentage * range;
41  }
42
43  function normalize(value, lo, hi) {
44    return (value - lo) / (hi - lo);
45  }
46
47  function deg2rad(deg) {
48    return (Math.PI * deg) / 180.0;
49  }
50
51  var tmp_vec2 = vec2.create();
52  var tmp_vec2b = vec2.create();
53  var tmp_vec4 = vec4.create();
54  var tmp_mat2d = mat2d.create();
55
56  vec2.createFromArray = function(arr) {
57    if (arr.length != 2)
58      throw new Error('Should be length 2');
59    var v = vec2.create();
60    vec2.set(v, arr[0], arr[1]);
61    return v;
62  };
63
64  vec2.createXY = function(x, y) {
65    var v = vec2.create();
66    vec2.set(v, x, y);
67    return v;
68  };
69
70  vec2.toString = function(a) {
71    return '[' + a[0] + ', ' + a[1] + ']';
72  };
73
74  vec2.addTwoScaledUnitVectors = function(out, u1, scale1, u2, scale2) {
75    // out = u1 * scale1 + u2 * scale2
76    vec2.scale(tmp_vec2, u1, scale1);
77    vec2.scale(tmp_vec2b, u2, scale2);
78    vec2.add(out, tmp_vec2, tmp_vec2b);
79  };
80
81  vec2.interpolatePiecewiseFunction = function(points, x) {
82    if (x < points[0][0])
83      return points[0][1];
84    for (var i = 1; i < points.length; ++i) {
85      if (x < points[i][0]) {
86        var percent = normalize(x, points[i - 1][0], points[i][0]);
87        return lerp(percent, points[i - 1][1], points[i][1]);
88      }
89    }
90    return points[points.length - 1][1];
91  };
92
93  vec3.createXYZ = function(x, y, z) {
94    var v = vec3.create();
95    vec3.set(v, x, y, z);
96    return v;
97  };
98
99  vec3.toString = function(a) {
100    return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
101  };
102
103  mat2d.translateXY = function(out, x, y) {
104    vec2.set(tmp_vec2, x, y);
105    mat2d.translate(out, out, tmp_vec2);
106  };
107
108  mat2d.scaleXY = function(out, x, y) {
109    vec2.set(tmp_vec2, x, y);
110    mat2d.scale(out, out, tmp_vec2);
111  };
112
113  vec4.unitize = function(out, a) {
114    out[0] = a[0] / a[3];
115    out[1] = a[1] / a[3];
116    out[2] = a[2] / a[3];
117    out[3] = 1;
118    return out;
119  };
120
121  vec2.copyFromVec4 = function(out, a) {
122    vec4.unitize(tmp_vec4, a);
123    vec2.copy(out, tmp_vec4);
124  };
125
126  return {
127    clamp: clamp,
128    lerp: lerp,
129    normalize: normalize,
130    deg2rad: deg2rad
131  };
132
133});
134</script>
135