UNPKG

8.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.toPoints = exports.getRegionBBox = exports.BBox = void 0;
4var util_1 = require("@antv/util");
5var constant_1 = require("../constant");
6/**
7 * 用于包围盒计算。
8 */
9var BBox = /** @class */ (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 // end 计算属性
123 /**
124 * 包围盒是否相等
125 * @param {BBox} bbox 包围盒
126 * @returns 包围盒是否相等
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 * @param child
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 * @returns 包围盒
141 */
142 BBox.prototype.clone = function () {
143 return new BBox(this.x, this.y, this.width, this.height);
144 };
145 /**
146 * 取并集
147 * @param subBBox
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 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 * @param subBBox
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 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 * bbox 剪裁
183 * @param subBBox
184 * @param direction
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 * @param gap
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 * @param gap
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 * get the gap of two bbox, if not exceed, then 0
229 * @param bbox
230 * @returns [top, right, bottom, left]
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 * @param bbox
243 */
244 BBox.prototype.collide = function (bbox) {
245 return this.minX < bbox.maxX && this.maxX > bbox.minX &&
246 this.minY < bbox.maxY && this.maxY > bbox.minY;
247 };
248 /**
249 * 获取包围盒大小
250 * @returns 包围盒大小
251 */
252 BBox.prototype.size = function () {
253 return this.width * this.height;
254 };
255 /**
256 * 点是否在 bbox 中
257 * @param p
258 */
259 BBox.prototype.isPointIn = function (p) {
260 return p.x >= this.minX && p.x <= this.maxX && p.y >= this.minY && p.y <= this.maxY;
261 };
262 return BBox;
263}());
264exports.BBox = BBox;
265/**
266 * 从一个 bbox 的 region 获取 bbox
267 * @param bbox
268 * @param region
269 */
270exports.getRegionBBox = function (bbox, region) {
271 var start = region.start, end = region.end;
272 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));
273};
274/**
275 * 将 bbox 转换成 points
276 * @param bbox
277 */
278function 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}
286exports.toPoints = toPoints;
287//# sourceMappingURL=bbox.js.map
\No newline at end of file