1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.toPoints = exports.getRegionBBox = exports.BBox = void 0;
|
4 | var util_1 = require("@antv/util");
|
5 | var constant_1 = require("../constant");
|
6 |
|
7 |
|
8 |
|
9 | var BBox = (function () {
|
10 | function BBox(x, y, width, height) {
|
11 | if (x === void 0) { x = 0; }
|
12 | if (y === void 0) { y = 0; }
|
13 | if (width === void 0) { width = 0; }
|
14 | if (height === void 0) { height = 0; }
|
15 | this.x = x;
|
16 | this.y = y;
|
17 | this.height = height;
|
18 | this.width = width;
|
19 | }
|
20 | BBox.fromRange = function (minX, minY, maxX, maxY) {
|
21 | return new BBox(minX, minY, maxX - minX, maxY - minY);
|
22 | };
|
23 | BBox.fromObject = function (bbox) {
|
24 | return new BBox(bbox.minX, bbox.minY, bbox.width, bbox.height);
|
25 | };
|
26 | Object.defineProperty(BBox.prototype, "minX", {
|
27 | get: function () {
|
28 | return this.x;
|
29 | },
|
30 | enumerable: false,
|
31 | configurable: true
|
32 | });
|
33 | Object.defineProperty(BBox.prototype, "maxX", {
|
34 | get: function () {
|
35 | return this.x + this.width;
|
36 | },
|
37 | enumerable: false,
|
38 | configurable: true
|
39 | });
|
40 | Object.defineProperty(BBox.prototype, "minY", {
|
41 | get: function () {
|
42 | return this.y;
|
43 | },
|
44 | enumerable: false,
|
45 | configurable: true
|
46 | });
|
47 | Object.defineProperty(BBox.prototype, "maxY", {
|
48 | get: function () {
|
49 | return this.y + this.height;
|
50 | },
|
51 | enumerable: false,
|
52 | configurable: true
|
53 | });
|
54 | Object.defineProperty(BBox.prototype, "tl", {
|
55 | get: function () {
|
56 | return { x: this.x, y: this.y };
|
57 | },
|
58 | enumerable: false,
|
59 | configurable: true
|
60 | });
|
61 | Object.defineProperty(BBox.prototype, "tr", {
|
62 | get: function () {
|
63 | return { x: this.maxX, y: this.y };
|
64 | },
|
65 | enumerable: false,
|
66 | configurable: true
|
67 | });
|
68 | Object.defineProperty(BBox.prototype, "bl", {
|
69 | get: function () {
|
70 | return { x: this.x, y: this.maxY };
|
71 | },
|
72 | enumerable: false,
|
73 | configurable: true
|
74 | });
|
75 | Object.defineProperty(BBox.prototype, "br", {
|
76 | get: function () {
|
77 | return { x: this.maxX, y: this.maxY };
|
78 | },
|
79 | enumerable: false,
|
80 | configurable: true
|
81 | });
|
82 | Object.defineProperty(BBox.prototype, "top", {
|
83 | get: function () {
|
84 | return {
|
85 | x: this.x + this.width / 2,
|
86 | y: this.minY,
|
87 | };
|
88 | },
|
89 | enumerable: false,
|
90 | configurable: true
|
91 | });
|
92 | Object.defineProperty(BBox.prototype, "right", {
|
93 | get: function () {
|
94 | return {
|
95 | x: this.maxX,
|
96 | y: this.y + this.height / 2,
|
97 | };
|
98 | },
|
99 | enumerable: false,
|
100 | configurable: true
|
101 | });
|
102 | Object.defineProperty(BBox.prototype, "bottom", {
|
103 | get: function () {
|
104 | return {
|
105 | x: this.x + this.width / 2,
|
106 | y: this.maxY,
|
107 | };
|
108 | },
|
109 | enumerable: false,
|
110 | configurable: true
|
111 | });
|
112 | Object.defineProperty(BBox.prototype, "left", {
|
113 | get: function () {
|
114 | return {
|
115 | x: this.minX,
|
116 | y: this.y + this.height / 2,
|
117 | };
|
118 | },
|
119 | enumerable: false,
|
120 | configurable: true
|
121 | });
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 | BBox.prototype.isEqual = function (bbox) {
|
129 | return this.x === bbox.x && this.y === bbox.y && this.width === bbox.width && this.height === bbox.height;
|
130 | };
|
131 | |
132 |
|
133 |
|
134 |
|
135 | BBox.prototype.contains = function (child) {
|
136 | return child.minX >= this.minX && child.maxX <= this.maxX && child.minY >= this.minY && child.maxY <= this.maxY;
|
137 | };
|
138 | |
139 |
|
140 |
|
141 |
|
142 | BBox.prototype.clone = function () {
|
143 | return new BBox(this.x, this.y, this.width, this.height);
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 | BBox.prototype.add = function () {
|
150 | var subBBox = [];
|
151 | for (var _i = 0; _i < arguments.length; _i++) {
|
152 | subBBox[_i] = arguments[_i];
|
153 | }
|
154 | var bbox = this.clone();
|
155 | (0, util_1.each)(subBBox, function (b) {
|
156 | bbox.x = Math.min(b.x, bbox.x);
|
157 | bbox.y = Math.min(b.y, bbox.y);
|
158 | bbox.width = Math.max(b.maxX, bbox.maxX) - bbox.x;
|
159 | bbox.height = Math.max(b.maxY, bbox.maxY) - bbox.y;
|
160 | });
|
161 | return bbox;
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 | BBox.prototype.merge = function () {
|
168 | var subBBox = [];
|
169 | for (var _i = 0; _i < arguments.length; _i++) {
|
170 | subBBox[_i] = arguments[_i];
|
171 | }
|
172 | var bbox = this.clone();
|
173 | (0, util_1.each)(subBBox, function (b) {
|
174 | bbox.x = Math.max(b.x, bbox.x);
|
175 | bbox.y = Math.max(b.y, bbox.y);
|
176 | bbox.width = Math.min(b.maxX, bbox.maxX) - bbox.x;
|
177 | bbox.height = Math.min(b.maxY, bbox.maxY) - bbox.y;
|
178 | });
|
179 | return bbox;
|
180 | };
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 | BBox.prototype.cut = function (subBBox, direction) {
|
187 | var width = subBBox.width;
|
188 | var height = subBBox.height;
|
189 | switch (direction) {
|
190 | case constant_1.DIRECTION.TOP:
|
191 | case constant_1.DIRECTION.TOP_LEFT:
|
192 | case constant_1.DIRECTION.TOP_RIGHT:
|
193 | return BBox.fromRange(this.minX, this.minY + height, this.maxX, this.maxY);
|
194 | case constant_1.DIRECTION.RIGHT:
|
195 | case constant_1.DIRECTION.RIGHT_TOP:
|
196 | case constant_1.DIRECTION.RIGHT_BOTTOM:
|
197 | return BBox.fromRange(this.minX, this.minY, this.maxX - width, this.maxY);
|
198 | case constant_1.DIRECTION.BOTTOM:
|
199 | case constant_1.DIRECTION.BOTTOM_LEFT:
|
200 | case constant_1.DIRECTION.BOTTOM_RIGHT:
|
201 | return BBox.fromRange(this.minX, this.minY, this.maxX, this.maxY - height);
|
202 | case constant_1.DIRECTION.LEFT:
|
203 | case constant_1.DIRECTION.LEFT_TOP:
|
204 | case constant_1.DIRECTION.LEFT_BOTTOM:
|
205 | return BBox.fromRange(this.minX + width, this.minY, this.maxX, this.maxY);
|
206 | default:
|
207 |
|
208 | return this;
|
209 | }
|
210 | };
|
211 | |
212 |
|
213 |
|
214 |
|
215 | BBox.prototype.shrink = function (gap) {
|
216 | var top = gap[0], right = gap[1], bottom = gap[2], left = gap[3];
|
217 | return new BBox(this.x + left, this.y + top, this.width - left - right, this.height - top - bottom);
|
218 | };
|
219 | |
220 |
|
221 |
|
222 |
|
223 | BBox.prototype.expand = function (gap) {
|
224 | var top = gap[0], right = gap[1], bottom = gap[2], left = gap[3];
|
225 | return new BBox(this.x - left, this.y - top, this.width + left + right, this.height + top + bottom);
|
226 | };
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 | BBox.prototype.exceed = function (bbox) {
|
233 | return [
|
234 | Math.max(-this.minY + bbox.minY, 0),
|
235 | Math.max(this.maxX - bbox.maxX, 0),
|
236 | Math.max(this.maxY - bbox.maxY, 0),
|
237 | Math.max(-this.minX + bbox.minX, 0),
|
238 | ];
|
239 | };
|
240 | |
241 |
|
242 |
|
243 |
|
244 | BBox.prototype.collide = function (bbox) {
|
245 | return this.minX < bbox.maxX && this.maxX > bbox.minX && this.minY < bbox.maxY && this.maxY > bbox.minY;
|
246 | };
|
247 | |
248 |
|
249 |
|
250 |
|
251 | BBox.prototype.size = function () {
|
252 | return this.width * this.height;
|
253 | };
|
254 | |
255 |
|
256 |
|
257 |
|
258 | BBox.prototype.isPointIn = function (p) {
|
259 | return p.x >= this.minX && p.x <= this.maxX && p.y >= this.minY && p.y <= this.maxY;
|
260 | };
|
261 | return BBox;
|
262 | }());
|
263 | exports.BBox = BBox;
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | var getRegionBBox = function (bbox, region) {
|
270 | var start = region.start, end = region.end;
|
271 | return new BBox(bbox.x + bbox.width * start.x, bbox.y + bbox.height * start.y, bbox.width * Math.abs(end.x - start.x), bbox.height * Math.abs(end.y - start.y));
|
272 | };
|
273 | exports.getRegionBBox = getRegionBBox;
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | function toPoints(bbox) {
|
279 | return [
|
280 | [bbox.minX, bbox.minY],
|
281 | [bbox.maxX, bbox.minY],
|
282 | [bbox.maxX, bbox.maxY],
|
283 | [bbox.minX, bbox.maxY],
|
284 | ];
|
285 | }
|
286 | exports.toPoints = toPoints;
|
287 |
|
\ | No newline at end of file |