Source: core/display/Bounds.js

core/display/Bounds.js

  1. import { Rectangle } from '../math';
  2. /**
  3. * 'Builder' pattern for bounds rectangles
  4. * Axis-Aligned Bounding Box
  5. * It is not a shape! Its mutable thing, no 'EMPTY' or that kind of problems
  6. *
  7. * @class
  8. * @memberof PIXI
  9. */
  10. export default class Bounds
  11. {
  12. /**
  13. *
  14. */
  15. constructor()
  16. {
  17. /**
  18. * @member {number}
  19. * @default 0
  20. */
  21. this.minX = Infinity;
  22. /**
  23. * @member {number}
  24. * @default 0
  25. */
  26. this.minY = Infinity;
  27. /**
  28. * @member {number}
  29. * @default 0
  30. */
  31. this.maxX = -Infinity;
  32. /**
  33. * @member {number}
  34. * @default 0
  35. */
  36. this.maxY = -Infinity;
  37. this.rect = null;
  38. }
  39. /**
  40. * Checks if bounds are empty.
  41. *
  42. * @return {boolean} True if empty.
  43. */
  44. isEmpty()
  45. {
  46. return this.minX > this.maxX || this.minY > this.maxY;
  47. }
  48. /**
  49. * Clears the bounds and resets.
  50. *
  51. */
  52. clear()
  53. {
  54. this.updateID++;
  55. this.minX = Infinity;
  56. this.minY = Infinity;
  57. this.maxX = -Infinity;
  58. this.maxY = -Infinity;
  59. }
  60. /**
  61. * Can return Rectangle.EMPTY constant, either construct new rectangle, either use your rectangle
  62. * It is not guaranteed that it will return tempRect
  63. *
  64. * @param {PIXI.Rectangle} rect - temporary object will be used if AABB is not empty
  65. * @returns {PIXI.Rectangle} A rectangle of the bounds
  66. */
  67. getRectangle(rect)
  68. {
  69. if (this.minX > this.maxX || this.minY > this.maxY)
  70. {
  71. return Rectangle.EMPTY;
  72. }
  73. rect = rect || new Rectangle(0, 0, 1, 1);
  74. rect.x = this.minX;
  75. rect.y = this.minY;
  76. rect.width = this.maxX - this.minX;
  77. rect.height = this.maxY - this.minY;
  78. return rect;
  79. }
  80. /**
  81. * This function should be inlined when its possible.
  82. *
  83. * @param {PIXI.Point} point - The point to add.
  84. */
  85. addPoint(point)
  86. {
  87. this.minX = Math.min(this.minX, point.x);
  88. this.maxX = Math.max(this.maxX, point.x);
  89. this.minY = Math.min(this.minY, point.y);
  90. this.maxY = Math.max(this.maxY, point.y);
  91. }
  92. /**
  93. * Adds a quad, not transformed
  94. *
  95. * @param {Float32Array} vertices - The verts to add.
  96. */
  97. addQuad(vertices)
  98. {
  99. let minX = this.minX;
  100. let minY = this.minY;
  101. let maxX = this.maxX;
  102. let maxY = this.maxY;
  103. let x = vertices[0];
  104. let y = vertices[1];
  105. minX = x < minX ? x : minX;
  106. minY = y < minY ? y : minY;
  107. maxX = x > maxX ? x : maxX;
  108. maxY = y > maxY ? y : maxY;
  109. x = vertices[2];
  110. y = vertices[3];
  111. minX = x < minX ? x : minX;
  112. minY = y < minY ? y : minY;
  113. maxX = x > maxX ? x : maxX;
  114. maxY = y > maxY ? y : maxY;
  115. x = vertices[4];
  116. y = vertices[5];
  117. minX = x < minX ? x : minX;
  118. minY = y < minY ? y : minY;
  119. maxX = x > maxX ? x : maxX;
  120. maxY = y > maxY ? y : maxY;
  121. x = vertices[6];
  122. y = vertices[7];
  123. minX = x < minX ? x : minX;
  124. minY = y < minY ? y : minY;
  125. maxX = x > maxX ? x : maxX;
  126. maxY = y > maxY ? y : maxY;
  127. this.minX = minX;
  128. this.minY = minY;
  129. this.maxX = maxX;
  130. this.maxY = maxY;
  131. }
  132. /**
  133. * Adds sprite frame, transformed.
  134. *
  135. * @param {PIXI.TransformBase} transform - TODO
  136. * @param {number} x0 - TODO
  137. * @param {number} y0 - TODO
  138. * @param {number} x1 - TODO
  139. * @param {number} y1 - TODO
  140. */
  141. addFrame(transform, x0, y0, x1, y1)
  142. {
  143. const matrix = transform.worldTransform;
  144. const a = matrix.a;
  145. const b = matrix.b;
  146. const c = matrix.c;
  147. const d = matrix.d;
  148. const tx = matrix.tx;
  149. const ty = matrix.ty;
  150. let minX = this.minX;
  151. let minY = this.minY;
  152. let maxX = this.maxX;
  153. let maxY = this.maxY;
  154. let x = (a * x0) + (c * y0) + tx;
  155. let y = (b * x0) + (d * y0) + ty;
  156. minX = x < minX ? x : minX;
  157. minY = y < minY ? y : minY;
  158. maxX = x > maxX ? x : maxX;
  159. maxY = y > maxY ? y : maxY;
  160. x = (a * x1) + (c * y0) + tx;
  161. y = (b * x1) + (d * y0) + ty;
  162. minX = x < minX ? x : minX;
  163. minY = y < minY ? y : minY;
  164. maxX = x > maxX ? x : maxX;
  165. maxY = y > maxY ? y : maxY;
  166. x = (a * x0) + (c * y1) + tx;
  167. y = (b * x0) + (d * y1) + ty;
  168. minX = x < minX ? x : minX;
  169. minY = y < minY ? y : minY;
  170. maxX = x > maxX ? x : maxX;
  171. maxY = y > maxY ? y : maxY;
  172. x = (a * x1) + (c * y1) + tx;
  173. y = (b * x1) + (d * y1) + ty;
  174. minX = x < minX ? x : minX;
  175. minY = y < minY ? y : minY;
  176. maxX = x > maxX ? x : maxX;
  177. maxY = y > maxY ? y : maxY;
  178. this.minX = minX;
  179. this.minY = minY;
  180. this.maxX = maxX;
  181. this.maxY = maxY;
  182. }
  183. /**
  184. * Add an array of vertices
  185. *
  186. * @param {PIXI.TransformBase} transform - TODO
  187. * @param {Float32Array} vertices - TODO
  188. * @param {number} beginOffset - TODO
  189. * @param {number} endOffset - TODO
  190. */
  191. addVertices(transform, vertices, beginOffset, endOffset)
  192. {
  193. const matrix = transform.worldTransform;
  194. const a = matrix.a;
  195. const b = matrix.b;
  196. const c = matrix.c;
  197. const d = matrix.d;
  198. const tx = matrix.tx;
  199. const ty = matrix.ty;
  200. let minX = this.minX;
  201. let minY = this.minY;
  202. let maxX = this.maxX;
  203. let maxY = this.maxY;
  204. for (let i = beginOffset; i < endOffset; i += 2)
  205. {
  206. const rawX = vertices[i];
  207. const rawY = vertices[i + 1];
  208. const x = (a * rawX) + (c * rawY) + tx;
  209. const y = (d * rawY) + (b * rawX) + ty;
  210. minX = x < minX ? x : minX;
  211. minY = y < minY ? y : minY;
  212. maxX = x > maxX ? x : maxX;
  213. maxY = y > maxY ? y : maxY;
  214. }
  215. this.minX = minX;
  216. this.minY = minY;
  217. this.maxX = maxX;
  218. this.maxY = maxY;
  219. }
  220. /**
  221. * Adds other Bounds
  222. *
  223. * @param {PIXI.Bounds} bounds - TODO
  224. */
  225. addBounds(bounds)
  226. {
  227. const minX = this.minX;
  228. const minY = this.minY;
  229. const maxX = this.maxX;
  230. const maxY = this.maxY;
  231. this.minX = bounds.minX < minX ? bounds.minX : minX;
  232. this.minY = bounds.minY < minY ? bounds.minY : minY;
  233. this.maxX = bounds.maxX > maxX ? bounds.maxX : maxX;
  234. this.maxY = bounds.maxY > maxY ? bounds.maxY : maxY;
  235. }
  236. /**
  237. * Adds other Bounds, masked with Bounds
  238. *
  239. * @param {PIXI.Bounds} bounds - TODO
  240. * @param {PIXI.Bounds} mask - TODO
  241. */
  242. addBoundsMask(bounds, mask)
  243. {
  244. const _minX = bounds.minX > mask.minX ? bounds.minX : mask.minX;
  245. const _minY = bounds.minY > mask.minY ? bounds.minY : mask.minY;
  246. const _maxX = bounds.maxX < mask.maxX ? bounds.maxX : mask.maxX;
  247. const _maxY = bounds.maxY < mask.maxY ? bounds.maxY : mask.maxY;
  248. if (_minX <= _maxX && _minY <= _maxY)
  249. {
  250. const minX = this.minX;
  251. const minY = this.minY;
  252. const maxX = this.maxX;
  253. const maxY = this.maxY;
  254. this.minX = _minX < minX ? _minX : minX;
  255. this.minY = _minY < minY ? _minY : minY;
  256. this.maxX = _maxX > maxX ? _maxX : maxX;
  257. this.maxY = _maxY > maxY ? _maxY : maxY;
  258. }
  259. }
  260. /**
  261. * Adds other Bounds, masked with Rectangle
  262. *
  263. * @param {PIXI.Bounds} bounds - TODO
  264. * @param {PIXI.Rectangle} area - TODO
  265. */
  266. addBoundsArea(bounds, area)
  267. {
  268. const _minX = bounds.minX > area.x ? bounds.minX : area.x;
  269. const _minY = bounds.minY > area.y ? bounds.minY : area.y;
  270. const _maxX = bounds.maxX < area.x + area.width ? bounds.maxX : (area.x + area.width);
  271. const _maxY = bounds.maxY < area.y + area.height ? bounds.maxY : (area.y + area.height);
  272. if (_minX <= _maxX && _minY <= _maxY)
  273. {
  274. const minX = this.minX;
  275. const minY = this.minY;
  276. const maxX = this.maxX;
  277. const maxY = this.maxY;
  278. this.minX = _minX < minX ? _minX : minX;
  279. this.minY = _minY < minY ? _minY : minY;
  280. this.maxX = _maxX > maxX ? _maxX : maxX;
  281. this.maxY = _maxY > maxY ? _maxY : maxY;
  282. }
  283. }
  284. }