Box.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var utils_1 = require("../utils");
  4. var Point_1 = require("./Point");
  5. var Box = /** @class */ (function () {
  6. function Box(_box, allowNegativeDimensions) {
  7. if (allowNegativeDimensions === void 0) { allowNegativeDimensions = true; }
  8. var box = (_box || {});
  9. var isBbox = [box.left, box.top, box.right, box.bottom].every(utils_1.isValidNumber);
  10. var isRect = [box.x, box.y, box.width, box.height].every(utils_1.isValidNumber);
  11. if (!isRect && !isBbox) {
  12. throw new Error("Box.constructor - expected box to be IBoundingBox | IRect, instead have " + JSON.stringify(box));
  13. }
  14. var _a = isRect
  15. ? [box.x, box.y, box.width, box.height]
  16. : [box.left, box.top, box.right - box.left, box.bottom - box.top], x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  17. Box.assertIsValidBox({ x: x, y: y, width: width, height: height }, 'Box.constructor', allowNegativeDimensions);
  18. this._x = x;
  19. this._y = y;
  20. this._width = width;
  21. this._height = height;
  22. }
  23. Box.isRect = function (rect) {
  24. return !!rect && [rect.x, rect.y, rect.width, rect.height].every(utils_1.isValidNumber);
  25. };
  26. Box.assertIsValidBox = function (box, callee, allowNegativeDimensions) {
  27. if (allowNegativeDimensions === void 0) { allowNegativeDimensions = false; }
  28. if (!Box.isRect(box)) {
  29. throw new Error(callee + " - invalid box: " + JSON.stringify(box) + ", expected object with properties x, y, width, height");
  30. }
  31. if (!allowNegativeDimensions && (box.width < 0 || box.height < 0)) {
  32. throw new Error(callee + " - width (" + box.width + ") and height (" + box.height + ") must be positive numbers");
  33. }
  34. };
  35. Object.defineProperty(Box.prototype, "x", {
  36. get: function () { return this._x; },
  37. enumerable: true,
  38. configurable: true
  39. });
  40. Object.defineProperty(Box.prototype, "y", {
  41. get: function () { return this._y; },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. Object.defineProperty(Box.prototype, "width", {
  46. get: function () { return this._width; },
  47. enumerable: true,
  48. configurable: true
  49. });
  50. Object.defineProperty(Box.prototype, "height", {
  51. get: function () { return this._height; },
  52. enumerable: true,
  53. configurable: true
  54. });
  55. Object.defineProperty(Box.prototype, "left", {
  56. get: function () { return this.x; },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. Object.defineProperty(Box.prototype, "top", {
  61. get: function () { return this.y; },
  62. enumerable: true,
  63. configurable: true
  64. });
  65. Object.defineProperty(Box.prototype, "right", {
  66. get: function () { return this.x + this.width; },
  67. enumerable: true,
  68. configurable: true
  69. });
  70. Object.defineProperty(Box.prototype, "bottom", {
  71. get: function () { return this.y + this.height; },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(Box.prototype, "area", {
  76. get: function () { return this.width * this.height; },
  77. enumerable: true,
  78. configurable: true
  79. });
  80. Object.defineProperty(Box.prototype, "topLeft", {
  81. get: function () { return new Point_1.Point(this.left, this.top); },
  82. enumerable: true,
  83. configurable: true
  84. });
  85. Object.defineProperty(Box.prototype, "topRight", {
  86. get: function () { return new Point_1.Point(this.right, this.top); },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(Box.prototype, "bottomLeft", {
  91. get: function () { return new Point_1.Point(this.left, this.bottom); },
  92. enumerable: true,
  93. configurable: true
  94. });
  95. Object.defineProperty(Box.prototype, "bottomRight", {
  96. get: function () { return new Point_1.Point(this.right, this.bottom); },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Box.prototype.round = function () {
  101. var _a = [this.x, this.y, this.width, this.height]
  102. .map(function (val) { return Math.round(val); }), x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  103. return new Box({ x: x, y: y, width: width, height: height });
  104. };
  105. Box.prototype.floor = function () {
  106. var _a = [this.x, this.y, this.width, this.height]
  107. .map(function (val) { return Math.floor(val); }), x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  108. return new Box({ x: x, y: y, width: width, height: height });
  109. };
  110. Box.prototype.toSquare = function () {
  111. var _a = this, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  112. var diff = Math.abs(width - height);
  113. if (width < height) {
  114. x -= (diff / 2);
  115. width += diff;
  116. }
  117. if (height < width) {
  118. y -= (diff / 2);
  119. height += diff;
  120. }
  121. return new Box({ x: x, y: y, width: width, height: height });
  122. };
  123. Box.prototype.rescale = function (s) {
  124. var scaleX = utils_1.isDimensions(s) ? s.width : s;
  125. var scaleY = utils_1.isDimensions(s) ? s.height : s;
  126. return new Box({
  127. x: this.x * scaleX,
  128. y: this.y * scaleY,
  129. width: this.width * scaleX,
  130. height: this.height * scaleY
  131. });
  132. };
  133. Box.prototype.pad = function (padX, padY) {
  134. var _a = [
  135. this.x - (padX / 2),
  136. this.y - (padY / 2),
  137. this.width + padX,
  138. this.height + padY
  139. ], x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  140. return new Box({ x: x, y: y, width: width, height: height });
  141. };
  142. Box.prototype.clipAtImageBorders = function (imgWidth, imgHeight) {
  143. var _a = this, x = _a.x, y = _a.y, right = _a.right, bottom = _a.bottom;
  144. var clippedX = Math.max(x, 0);
  145. var clippedY = Math.max(y, 0);
  146. var newWidth = right - clippedX;
  147. var newHeight = bottom - clippedY;
  148. var clippedWidth = Math.min(newWidth, imgWidth - clippedX);
  149. var clippedHeight = Math.min(newHeight, imgHeight - clippedY);
  150. return (new Box({ x: clippedX, y: clippedY, width: clippedWidth, height: clippedHeight })).floor();
  151. };
  152. Box.prototype.shift = function (sx, sy) {
  153. var _a = this, width = _a.width, height = _a.height;
  154. var x = this.x + sx;
  155. var y = this.y + sy;
  156. return new Box({ x: x, y: y, width: width, height: height });
  157. };
  158. Box.prototype.padAtBorders = function (imageHeight, imageWidth) {
  159. var w = this.width + 1;
  160. var h = this.height + 1;
  161. var dx = 1;
  162. var dy = 1;
  163. var edx = w;
  164. var edy = h;
  165. var x = this.left;
  166. var y = this.top;
  167. var ex = this.right;
  168. var ey = this.bottom;
  169. if (ex > imageWidth) {
  170. edx = -ex + imageWidth + w;
  171. ex = imageWidth;
  172. }
  173. if (ey > imageHeight) {
  174. edy = -ey + imageHeight + h;
  175. ey = imageHeight;
  176. }
  177. if (x < 1) {
  178. edy = 2 - x;
  179. x = 1;
  180. }
  181. if (y < 1) {
  182. edy = 2 - y;
  183. y = 1;
  184. }
  185. return { dy: dy, edy: edy, dx: dx, edx: edx, y: y, ey: ey, x: x, ex: ex, w: w, h: h };
  186. };
  187. Box.prototype.calibrate = function (region) {
  188. return new Box({
  189. left: this.left + (region.left * this.width),
  190. top: this.top + (region.top * this.height),
  191. right: this.right + (region.right * this.width),
  192. bottom: this.bottom + (region.bottom * this.height)
  193. }).toSquare().round();
  194. };
  195. return Box;
  196. }());
  197. exports.Box = Box;
  198. //# sourceMappingURL=Box.js.map