OLD | NEW |
| (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 <link rel="import" href="/tracing/base/rect.html"> | |
10 | |
11 <script> | |
12 'use strict'; | |
13 | |
14 /** | |
15 * @fileoverview 2D bounding box computations. | |
16 */ | |
17 tr.exportTo('tr.b', function() { | |
18 /** | |
19 * Tracks a 2D bounding box. | |
20 * @constructor | |
21 */ | |
22 function BBox2() { | |
23 this.isEmpty_ = true; | |
24 this.min_ = undefined; | |
25 this.max_ = undefined; | |
26 } | |
27 | |
28 BBox2.prototype = { | |
29 __proto__: Object.prototype, | |
30 | |
31 reset: function() { | |
32 this.isEmpty_ = true; | |
33 this.min_ = undefined; | |
34 this.max_ = undefined; | |
35 }, | |
36 | |
37 get isEmpty() { | |
38 return this.isEmpty_; | |
39 }, | |
40 | |
41 addBBox2: function(bbox2) { | |
42 if (bbox2.isEmpty) | |
43 return; | |
44 this.addVec2(bbox2.min_); | |
45 this.addVec2(bbox2.max_); | |
46 }, | |
47 | |
48 clone: function() { | |
49 var bbox = new BBox2(); | |
50 bbox.addBBox2(this); | |
51 return bbox; | |
52 }, | |
53 | |
54 /** | |
55 * Adds x, y to the range. | |
56 */ | |
57 addXY: function(x, y) { | |
58 if (this.isEmpty_) { | |
59 this.max_ = vec2.create(); | |
60 this.min_ = vec2.create(); | |
61 vec2.set(this.max_, x, y); | |
62 vec2.set(this.min_, x, y); | |
63 this.isEmpty_ = false; | |
64 return; | |
65 } | |
66 this.max_[0] = Math.max(this.max_[0], x); | |
67 this.max_[1] = Math.max(this.max_[1], y); | |
68 this.min_[0] = Math.min(this.min_[0], x); | |
69 this.min_[1] = Math.min(this.min_[1], y); | |
70 }, | |
71 | |
72 /** | |
73 * Adds value_x, value_y in the form [value_x,value_y] to the range. | |
74 */ | |
75 addVec2: function(value) { | |
76 if (this.isEmpty_) { | |
77 this.max_ = vec2.create(); | |
78 this.min_ = vec2.create(); | |
79 vec2.set(this.max_, value[0], value[1]); | |
80 vec2.set(this.min_, value[0], value[1]); | |
81 this.isEmpty_ = false; | |
82 return; | |
83 } | |
84 this.max_[0] = Math.max(this.max_[0], value[0]); | |
85 this.max_[1] = Math.max(this.max_[1], value[1]); | |
86 this.min_[0] = Math.min(this.min_[0], value[0]); | |
87 this.min_[1] = Math.min(this.min_[1], value[1]); | |
88 }, | |
89 | |
90 addQuad: function(quad) { | |
91 this.addVec2(quad.p1); | |
92 this.addVec2(quad.p2); | |
93 this.addVec2(quad.p3); | |
94 this.addVec2(quad.p4); | |
95 }, | |
96 | |
97 get minVec2() { | |
98 if (this.isEmpty_) | |
99 return undefined; | |
100 return this.min_; | |
101 }, | |
102 | |
103 get maxVec2() { | |
104 if (this.isEmpty_) | |
105 return undefined; | |
106 return this.max_; | |
107 }, | |
108 | |
109 get sizeAsVec2() { | |
110 if (this.isEmpty_) | |
111 throw new Error('Empty BBox2 has no size'); | |
112 var size = vec2.create(); | |
113 vec2.subtract(size, this.max_, this.min_); | |
114 return size; | |
115 }, | |
116 | |
117 get size() { | |
118 if (this.isEmpty_) | |
119 throw new Error('Empty BBox2 has no size'); | |
120 return {width: this.max_[0] - this.min_[0], | |
121 height: this.max_[1] - this.min_[1]}; | |
122 }, | |
123 | |
124 get width() { | |
125 if (this.isEmpty_) | |
126 throw new Error('Empty BBox2 has no width'); | |
127 return this.max_[0] - this.min_[0]; | |
128 }, | |
129 | |
130 get height() { | |
131 if (this.isEmpty_) | |
132 throw new Error('Empty BBox2 has no width'); | |
133 return this.max_[1] - this.min_[1]; | |
134 }, | |
135 | |
136 toString: function() { | |
137 if (this.isEmpty_) | |
138 return 'empty'; | |
139 return 'min=(' + this.min_[0] + ',' + this.min_[1] + ') ' + | |
140 'max=(' + this.max_[0] + ',' + this.max_[1] + ')'; | |
141 }, | |
142 | |
143 asRect: function() { | |
144 return tr.b.Rect.fromXYWH( | |
145 this.min_[0], | |
146 this.min_[1], | |
147 this.max_[0] - this.min_[0], | |
148 this.max_[1] - this.min_[1]); | |
149 } | |
150 }; | |
151 | |
152 return { | |
153 BBox2, | |
154 }; | |
155 }); | |
156 </script> | |
OLD | NEW |