UNPKG

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