OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 'use strict'; |
| 5 |
| 6 /** |
| 7 * @fileoverview Quick range computations. |
| 8 */ |
| 9 base.require('base.gl_matrix'); |
| 10 base.require('base.rect2'); |
| 11 |
| 12 base.exportTo('base', function() { |
| 13 |
| 14 /** |
| 15 * Tracks a 2D bounding box. |
| 16 * @constructor |
| 17 */ |
| 18 function BBox2() { |
| 19 this.isEmpty_ = true; |
| 20 this.min_ = undefined; |
| 21 this.max_ = undefined; |
| 22 }; |
| 23 |
| 24 BBox2.prototype = { |
| 25 __proto__: Object.prototype, |
| 26 |
| 27 reset: function() { |
| 28 this.isEmpty_ = true; |
| 29 this.min_ = undefined; |
| 30 this.max_ = undefined; |
| 31 }, |
| 32 |
| 33 get isEmpty() { |
| 34 return this.isEmpty_; |
| 35 }, |
| 36 |
| 37 addBBox2: function(bbox2) { |
| 38 if (bbox2.isEmpty) |
| 39 return; |
| 40 this.addVec2(bbox2.min_); |
| 41 this.addVec2(bbox2.max_); |
| 42 }, |
| 43 |
| 44 clone: function() { |
| 45 var bbox = new BBox2(); |
| 46 bbox.addBBox2(this); |
| 47 return bbox; |
| 48 }, |
| 49 |
| 50 /** |
| 51 * Adds x, y to the range. |
| 52 */ |
| 53 addXY: function(x, y) { |
| 54 if (this.isEmpty_) { |
| 55 this.max_ = vec2.create(); |
| 56 this.min_ = vec2.create(); |
| 57 vec2.set(this.max_, x, y); |
| 58 vec2.set(this.min_, x, y); |
| 59 this.isEmpty_ = false; |
| 60 return; |
| 61 } |
| 62 this.max_[0] = Math.max(this.max_[0], x); |
| 63 this.max_[1] = Math.max(this.max_[1], y); |
| 64 this.min_[0] = Math.min(this.min_[0], x); |
| 65 this.min_[1] = Math.min(this.min_[1], y); |
| 66 }, |
| 67 |
| 68 /** |
| 69 * Adds value_x, value_y in the form [value_x,value_y] to the range. |
| 70 */ |
| 71 addVec2: function(value) { |
| 72 if (this.isEmpty_) { |
| 73 this.max_ = vec2.create(); |
| 74 this.min_ = vec2.create(); |
| 75 vec2.set(this.max_, value[0], value[1]); |
| 76 vec2.set(this.min_, value[0], value[1]); |
| 77 this.isEmpty_ = false; |
| 78 return; |
| 79 } |
| 80 this.max_[0] = Math.max(this.max_[0], value[0]); |
| 81 this.max_[1] = Math.max(this.max_[1], value[1]); |
| 82 this.min_[0] = Math.min(this.min_[0], value[0]); |
| 83 this.min_[1] = Math.min(this.min_[1], value[1]); |
| 84 }, |
| 85 |
| 86 /** |
| 87 * Adds value_x, value_y in the form {x: value_x, y: value_y} to the range. |
| 88 */ |
| 89 addPoint: function(value) { |
| 90 if (this.isEmpty_) { |
| 91 this.max_ = vec2.create(); |
| 92 this.min_ = vec2.create(); |
| 93 vec2.set(this.max_, value.x, value.y); |
| 94 vec2.set(this.min_, value.x, value.y); |
| 95 this.isEmpty_ = false; |
| 96 return; |
| 97 } |
| 98 this.max_[0] = Math.max(this.max_[0], value.x); |
| 99 this.max_[1] = Math.max(this.max_[1], value.y); |
| 100 this.min_[0] = Math.min(this.min_[0], value.x); |
| 101 this.min_[1] = Math.min(this.min_[1], value.y); |
| 102 }, |
| 103 |
| 104 addQuad: function(quad) { |
| 105 this.addPoint(quad.p1); |
| 106 this.addPoint(quad.p2); |
| 107 this.addPoint(quad.p3); |
| 108 this.addPoint(quad.p4); |
| 109 }, |
| 110 |
| 111 get minVec2() { |
| 112 if (this.isEmpty_) |
| 113 return undefined; |
| 114 return this.min_; |
| 115 }, |
| 116 |
| 117 get maxVec2() { |
| 118 if (this.isEmpty_) |
| 119 return undefined; |
| 120 return this.max_; |
| 121 }, |
| 122 |
| 123 get minPoint() { |
| 124 if (this.isEmpty_) |
| 125 return undefined; |
| 126 return {x: this.min_[0], |
| 127 y: this.min_[1]}; |
| 128 }, |
| 129 |
| 130 get maxPoint() { |
| 131 if (this.isEmpty_) |
| 132 return undefined; |
| 133 return {x: this.max_[0], |
| 134 y: this.max_[1]}; |
| 135 }, |
| 136 |
| 137 get sizeAsVec2() { |
| 138 if (this.isEmpty_) |
| 139 throw new Error('Empty BBox2 has no size'); |
| 140 var size = vec2.create(); |
| 141 vec2.subtract(size, this.max_, this.min_); |
| 142 return size; |
| 143 }, |
| 144 |
| 145 get size() { |
| 146 if (this.isEmpty_) |
| 147 throw new Error('Empty BBox2 has no size'); |
| 148 return {width: this.max_[0] - this.min_[0], |
| 149 height: this.max_[1] - this.min_[1]}; |
| 150 }, |
| 151 |
| 152 get width() { |
| 153 if (this.isEmpty_) |
| 154 throw new Error('Empty BBox2 has no width'); |
| 155 return this.max_[0] - this.min_[0]; |
| 156 }, |
| 157 |
| 158 get height() { |
| 159 if (this.isEmpty_) |
| 160 throw new Error('Empty BBox2 has no width'); |
| 161 return this.max_[1] - this.min_[1]; |
| 162 }, |
| 163 |
| 164 toString: function() { |
| 165 if (this.isEmpty_) |
| 166 return 'empty'; |
| 167 return 'min=(' + this.min_[0] + ',' + this.min_[1] + ') ' + |
| 168 'max=(' + this.max_[0] + ',' + this.max_[1] + ')'; |
| 169 }, |
| 170 |
| 171 asRect: function() { |
| 172 return base.Rect2.FromXYWH( |
| 173 this.min_[0], |
| 174 this.min_[1], |
| 175 this.max_[0] - this.min_[0], |
| 176 this.max_[1] - this.min_[1]); |
| 177 }, |
| 178 }; |
| 179 |
| 180 return { |
| 181 BBox2: BBox2 |
| 182 }; |
| 183 |
| 184 }); |
OLD | NEW |