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.
7 <link rel="import" href="/tvcm/gl_matrix.html">
11 tvcm.exportTo('tvcm', function() {
13 for (var i = 0; i < 8; i++)
14 tmpVec2s[i] = vec2.create();
16 var tmpVec2a = vec4.create();
17 var tmpVec4a = vec4.create();
18 var tmpVec4b = vec4.create();
19 var tmpMat4 = mat4.create();
20 var tmpMat4b = mat4.create();
22 var p00 = vec2.createXY(0, 0);
23 var p10 = vec2.createXY(1, 0);
24 var p01 = vec2.createXY(0, 1);
25 var p11 = vec2.createXY(1, 1);
27 var lerpingVecA = vec2.create();
28 var lerpingVecB = vec2.create();
29 function lerpVec2(out, a, b, amt) {
30 vec2.scale(lerpingVecA, a, amt);
31 vec2.scale(lerpingVecB, b, 1 - amt);
32 vec2.add(out, lerpingVecA, lerpingVecB);
33 vec2.normalize(out, out);
41 this.p1 = vec2.create();
42 this.p2 = vec2.create();
43 this.p3 = vec2.create();
44 this.p4 = vec2.create();
47 Quad.fromXYWH = function(x, y, w, h) {
50 vec2.set(q.p2, x + w, y);
51 vec2.set(q.p3, x + w, y + h);
52 vec2.set(q.p4, x, y + h);
56 Quad.fromRect = function(r) {
57 return new Quad.fromXYWH(
62 Quad.from4Vecs = function(p1, p2, p3, p4) {
64 vec2.set(q.p1, p1[0], p1[1]);
65 vec2.set(q.p2, p2[0], p2[1]);
66 vec2.set(q.p3, p3[0], p3[1]);
67 vec2.set(q.p4, p4[0], p4[1]);
71 Quad.from8Array = function(arr) {
73 throw new Error('Array must be 8 long');
87 pointInside: function(point) {
88 return pointInImplicitQuad(point,
89 this.p1, this.p2, this.p3, this.p4);
92 boundingRect: function() {
93 var x0 = Math.min(this.p1[0], this.p2[0], this.p3[0], this.p4[0]);
94 var y0 = Math.min(this.p1[1], this.p2[1], this.p3[1], this.p4[1]);
96 var x1 = Math.max(this.p1[0], this.p2[0], this.p3[0], this.p4[0]);
97 var y1 = Math.max(this.p1[1], this.p2[1], this.p3[1], this.p4[1]);
99 return new tvcm.Rect.fromXYWH(x0, y0, x1 - x0, y1 - y0);
104 vec2.copy(q.p1, this.p1);
105 vec2.copy(q.p2, this.p2);
106 vec2.copy(q.p3, this.p3);
107 vec2.copy(q.p4, this.p4);
113 this.scaleFast(q, s);
117 scaleFast: function(dstQuad, s) {
118 vec2.copy(dstQuad.p1, this.p1, s);
119 vec2.copy(dstQuad.p2, this.p2, s);
120 vec2.copy(dstQuad.p3, this.p3, s);
121 vec2.copy(dstQuad.p3, this.p3, s);
124 isRectangle: function() {
125 // Simple rectangle check. Note: will not handle out-of-order components.
126 var bounds = this.boundingRect();
128 bounds.x == this.p1[0] &&
129 bounds.y == this.p1[1] &&
130 bounds.width == this.p2[0] - this.p1[0] &&
131 bounds.y == this.p2[1] &&
132 bounds.width == this.p3[0] - this.p1[0] &&
133 bounds.height == this.p3[1] - this.p2[1] &&
134 bounds.x == this.p4[0] &&
135 bounds.height == this.p4[1] - this.p2[1]
139 projectUnitRect: function(rect) {
141 this.projectUnitRectFast(q, rect);
145 projectUnitRectFast: function(dstQuad, rect) {
146 var v12 = tmpVec2s[0];
147 var v14 = tmpVec2s[1];
148 var v23 = tmpVec2s[2];
149 var v43 = tmpVec2s[3];
150 var l12, l14, l23, l43;
152 vec2.sub(v12, this.p2, this.p1);
153 l12 = vec2.length(v12);
154 vec2.scale(v12, v12, 1 / l12);
156 vec2.sub(v14, this.p4, this.p1);
157 l14 = vec2.length(v14);
158 vec2.scale(v14, v14, 1 / l14);
160 vec2.sub(v23, this.p3, this.p2);
161 l23 = vec2.length(v23);
162 vec2.scale(v23, v23, 1 / l23);
164 vec2.sub(v43, this.p3, this.p4);
165 l43 = vec2.length(v43);
166 vec2.scale(v43, v43, 1 / l43);
168 var b12 = tmpVec2s[0];
169 var b14 = tmpVec2s[1];
170 var b23 = tmpVec2s[2];
171 var b43 = tmpVec2s[3];
172 lerpVec2(b12, v12, v43, rect.y);
173 lerpVec2(b43, v12, v43, 1 - rect.bottom);
174 lerpVec2(b14, v14, v23, rect.x);
175 lerpVec2(b23, v14, v23, 1 - rect.right);
177 vec2.addTwoScaledUnitVectors(tmpVec2a,
180 vec2.add(dstQuad.p1, this.p1, tmpVec2a);
182 vec2.addTwoScaledUnitVectors(tmpVec2a,
183 b12, l12 * -(1.0 - rect.right),
185 vec2.add(dstQuad.p2, this.p2, tmpVec2a);
188 vec2.addTwoScaledUnitVectors(tmpVec2a,
189 b43, l43 * -(1.0 - rect.right),
190 b23, l23 * -(1.0 - rect.bottom));
191 vec2.add(dstQuad.p3, this.p3, tmpVec2a);
193 vec2.addTwoScaledUnitVectors(tmpVec2a,
194 b43, l43 * rect.left,
195 b14, l14 * -(1.0 - rect.bottom));
196 vec2.add(dstQuad.p4, this.p4, tmpVec2a);
199 toString: function() {
201 vec2.toString(this.p1) + ', ' +
202 vec2.toString(this.p2) + ', ' +
203 vec2.toString(this.p3) + ', ' +
204 vec2.toString(this.p4) + ')';
208 function sign(p1, p2, p3) {
209 return (p1[0] - p3[0]) * (p2[1] - p3[1]) -
210 (p2[0] - p3[0]) * (p1[1] - p3[1]);
213 function pointInTriangle2(pt, p1, p2, p3) {
214 var b1 = sign(pt, p1, p2) < 0.0;
215 var b2 = sign(pt, p2, p3) < 0.0;
216 var b3 = sign(pt, p3, p1) < 0.0;
217 return ((b1 == b2) && (b2 == b3));
220 function pointInImplicitQuad(point, p1, p2, p3, p4) {
221 return pointInTriangle2(point, p1, p2, p3) ||
222 pointInTriangle2(point, p1, p3, p4);
226 pointInTriangle2: pointInTriangle2,
227 pointInImplicitQuad: pointInImplicitQuad,