Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: tracing/tracing/base/quad.html

Issue 2771723003: [tracing] Move math utilities from base into their own subdirectory (attempt 2) (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <!--
3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
6 -->
7 <link rel="import" href="/tracing/base/base.html">
8 <link rel="import" href="/tracing/base/math.html">
9 <script>
10 'use strict';
11
12 tr.exportTo('tr.b', function() {
13 var tmpVec2s = [];
14 for (var i = 0; i < 8; i++)
15 tmpVec2s[i] = vec2.create();
16
17 var tmpVec2a = vec4.create();
18 var tmpVec4a = vec4.create();
19 var tmpVec4b = vec4.create();
20 var tmpMat4 = mat4.create();
21 var tmpMat4b = mat4.create();
22
23 var p00 = vec2.createXY(0, 0);
24 var p10 = vec2.createXY(1, 0);
25 var p01 = vec2.createXY(0, 1);
26 var p11 = vec2.createXY(1, 1);
27
28 var lerpingVecA = vec2.create();
29 var lerpingVecB = vec2.create();
30 function lerpVec2(out, a, b, amt) {
31 vec2.scale(lerpingVecA, a, amt);
32 vec2.scale(lerpingVecB, b, 1 - amt);
33 vec2.add(out, lerpingVecA, lerpingVecB);
34 vec2.normalize(out, out);
35 return out;
36 }
37
38 /**
39 * @constructor
40 */
41 function Quad() {
42 this.p1 = vec2.create();
43 this.p2 = vec2.create();
44 this.p3 = vec2.create();
45 this.p4 = vec2.create();
46 }
47
48 Quad.fromXYWH = function(x, y, w, h) {
49 var q = new Quad();
50 vec2.set(q.p1, x, y);
51 vec2.set(q.p2, x + w, y);
52 vec2.set(q.p3, x + w, y + h);
53 vec2.set(q.p4, x, y + h);
54 return q;
55 };
56
57 Quad.fromRect = function(r) {
58 return new Quad.fromXYWH(
59 r.x, r.y,
60 r.width, r.height);
61 };
62
63 Quad.from4Vecs = function(p1, p2, p3, p4) {
64 var q = new Quad();
65 vec2.set(q.p1, p1[0], p1[1]);
66 vec2.set(q.p2, p2[0], p2[1]);
67 vec2.set(q.p3, p3[0], p3[1]);
68 vec2.set(q.p4, p4[0], p4[1]);
69 return q;
70 };
71
72 Quad.from8Array = function(arr) {
73 if (arr.length !== 8)
74 throw new Error('Array must be 8 long');
75 var q = new Quad();
76 q.p1[0] = arr[0];
77 q.p1[1] = arr[1];
78 q.p2[0] = arr[2];
79 q.p2[1] = arr[3];
80 q.p3[0] = arr[4];
81 q.p3[1] = arr[5];
82 q.p4[0] = arr[6];
83 q.p4[1] = arr[7];
84 return q;
85 };
86
87 Quad.prototype = {
88 pointInside: function(point) {
89 return pointInImplicitQuad(point,
90 this.p1, this.p2, this.p3, this.p4);
91 },
92
93 boundingRect: function() {
94 var x0 = Math.min(this.p1[0], this.p2[0], this.p3[0], this.p4[0]);
95 var y0 = Math.min(this.p1[1], this.p2[1], this.p3[1], this.p4[1]);
96
97 var x1 = Math.max(this.p1[0], this.p2[0], this.p3[0], this.p4[0]);
98 var y1 = Math.max(this.p1[1], this.p2[1], this.p3[1], this.p4[1]);
99
100 return new tr.b.Rect.fromXYWH(x0, y0, x1 - x0, y1 - y0);
101 },
102
103 clone: function() {
104 var q = new Quad();
105 vec2.copy(q.p1, this.p1);
106 vec2.copy(q.p2, this.p2);
107 vec2.copy(q.p3, this.p3);
108 vec2.copy(q.p4, this.p4);
109 return q;
110 },
111
112 scale: function(s) {
113 var q = new Quad();
114 this.scaleFast(q, s);
115 return q;
116 },
117
118 scaleFast: function(dstQuad, s) {
119 vec2.copy(dstQuad.p1, this.p1, s);
120 vec2.copy(dstQuad.p2, this.p2, s);
121 vec2.copy(dstQuad.p3, this.p3, s);
122 vec2.copy(dstQuad.p3, this.p3, s);
123 },
124
125 isRectangle: function() {
126 // Simple rectangle check. Note: will not handle out-of-order components.
127 var bounds = this.boundingRect();
128 return (
129 bounds.x === this.p1[0] &&
130 bounds.y === this.p1[1] &&
131 bounds.width === this.p2[0] - this.p1[0] &&
132 bounds.y === this.p2[1] &&
133 bounds.width === this.p3[0] - this.p1[0] &&
134 bounds.height === this.p3[1] - this.p2[1] &&
135 bounds.x === this.p4[0] &&
136 bounds.height === this.p4[1] - this.p2[1]
137 );
138 },
139
140 projectUnitRect: function(rect) {
141 var q = new Quad();
142 this.projectUnitRectFast(q, rect);
143 return q;
144 },
145
146 projectUnitRectFast: function(dstQuad, rect) {
147 var v12 = tmpVec2s[0];
148 var v14 = tmpVec2s[1];
149 var v23 = tmpVec2s[2];
150 var v43 = tmpVec2s[3];
151 var l12;
152 var l14;
153 var l23;
154 var l43;
155
156 vec2.sub(v12, this.p2, this.p1);
157 l12 = vec2.length(v12);
158 vec2.scale(v12, v12, 1 / l12);
159
160 vec2.sub(v14, this.p4, this.p1);
161 l14 = vec2.length(v14);
162 vec2.scale(v14, v14, 1 / l14);
163
164 vec2.sub(v23, this.p3, this.p2);
165 l23 = vec2.length(v23);
166 vec2.scale(v23, v23, 1 / l23);
167
168 vec2.sub(v43, this.p3, this.p4);
169 l43 = vec2.length(v43);
170 vec2.scale(v43, v43, 1 / l43);
171
172 var b12 = tmpVec2s[0];
173 var b14 = tmpVec2s[1];
174 var b23 = tmpVec2s[2];
175 var b43 = tmpVec2s[3];
176 lerpVec2(b12, v12, v43, rect.y);
177 lerpVec2(b43, v12, v43, 1 - rect.bottom);
178 lerpVec2(b14, v14, v23, rect.x);
179 lerpVec2(b23, v14, v23, 1 - rect.right);
180
181 vec2.addTwoScaledUnitVectors(tmpVec2a,
182 b12, l12 * rect.x,
183 b14, l14 * rect.y);
184 vec2.add(dstQuad.p1, this.p1, tmpVec2a);
185
186 vec2.addTwoScaledUnitVectors(tmpVec2a,
187 b12, l12 * -(1.0 - rect.right),
188 b23, l23 * rect.y);
189 vec2.add(dstQuad.p2, this.p2, tmpVec2a);
190
191
192 vec2.addTwoScaledUnitVectors(tmpVec2a,
193 b43, l43 * -(1.0 - rect.right),
194 b23, l23 * -(1.0 - rect.bottom));
195 vec2.add(dstQuad.p3, this.p3, tmpVec2a);
196
197 vec2.addTwoScaledUnitVectors(tmpVec2a,
198 b43, l43 * rect.left,
199 b14, l14 * -(1.0 - rect.bottom));
200 vec2.add(dstQuad.p4, this.p4, tmpVec2a);
201 },
202
203 toString: function() {
204 return 'Quad(' +
205 vec2.toString(this.p1) + ', ' +
206 vec2.toString(this.p2) + ', ' +
207 vec2.toString(this.p3) + ', ' +
208 vec2.toString(this.p4) + ')';
209 }
210 };
211
212 function sign(p1, p2, p3) {
213 return (p1[0] - p3[0]) * (p2[1] - p3[1]) -
214 (p2[0] - p3[0]) * (p1[1] - p3[1]);
215 }
216
217 function pointInTriangle2(pt, p1, p2, p3) {
218 var b1 = sign(pt, p1, p2) < 0.0;
219 var b2 = sign(pt, p2, p3) < 0.0;
220 var b3 = sign(pt, p3, p1) < 0.0;
221 return ((b1 === b2) && (b2 === b3));
222 }
223
224 function pointInImplicitQuad(point, p1, p2, p3, p4) {
225 return pointInTriangle2(point, p1, p2, p3) ||
226 pointInTriangle2(point, p1, p3, p4);
227 }
228
229 return {
230 pointInTriangle2,
231 pointInImplicitQuad,
232 Quad,
233 };
234 });
235 </script>
OLDNEW
« no previous file with comments | « tracing/tracing/base/piecewise_linear_function_test.html ('k') | tracing/tracing/base/quad_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698