BoundingBox.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. import {ArgumentOutOfRangeException} from "../Exceptions";
  2. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  3. import {SizeF2D} from "../../Common/DataObjects/SizeF2D";
  4. import {RectangleF2D} from "../../Common/DataObjects/RectangleF2D";
  5. /**
  6. * A bounding box delimits an area on the 2D plane.
  7. */
  8. export class BoundingBox {
  9. protected isSymbol: boolean = false;
  10. protected relativePositionHasBeenSet: boolean = false;
  11. protected xBordersHaveBeenSet: boolean = false;
  12. protected yBordersHaveBeenSet: boolean = false;
  13. protected absolutePosition: PointF2D = new PointF2D();
  14. protected relativePosition: PointF2D = new PointF2D();
  15. protected size: SizeF2D = new SizeF2D();
  16. protected marginSize: SizeF2D = new SizeF2D();
  17. protected upperLeftCorner: PointF2D = new PointF2D();
  18. protected upperLeftMarginCorner: PointF2D = new PointF2D();
  19. protected borderLeft: number = 0;
  20. protected borderRight: number = 0;
  21. protected borderTop: number = 0;
  22. protected borderBottom: number = 0;
  23. protected borderMarginLeft: number = 0;
  24. protected borderMarginRight: number = 0;
  25. protected borderMarginTop: number = 0;
  26. protected borderMarginBottom: number = 0;
  27. protected boundingRectangle: RectangleF2D;
  28. protected boundingMarginRectangle: RectangleF2D;
  29. protected childElements: BoundingBox[] = [];
  30. protected parent: BoundingBox;
  31. protected dataObject: Object;
  32. constructor(dataObject: Object = undefined, parent: BoundingBox = undefined) {
  33. this.parent = parent;
  34. this.dataObject = dataObject;
  35. this.xBordersHaveBeenSet = false;
  36. this.yBordersHaveBeenSet = false;
  37. }
  38. public get RelativePositionHasBeenSet(): boolean {
  39. return this.relativePositionHasBeenSet;
  40. }
  41. public get XBordersHaveBeenSet(): boolean {
  42. return this.xBordersHaveBeenSet;
  43. }
  44. public set XBordersHaveBeenSet(value: boolean) {
  45. this.xBordersHaveBeenSet = value;
  46. }
  47. public get YBordersHaveBeenSet(): boolean {
  48. return this.yBordersHaveBeenSet;
  49. }
  50. public set YBordersHaveBeenSet(value: boolean) {
  51. this.yBordersHaveBeenSet = value;
  52. }
  53. public get AbsolutePosition(): PointF2D {
  54. return this.absolutePosition;
  55. }
  56. public set AbsolutePosition(value: PointF2D) {
  57. this.absolutePosition = value;
  58. }
  59. public get RelativePosition(): PointF2D {
  60. return this.relativePosition;
  61. }
  62. public set RelativePosition(value: PointF2D) {
  63. this.relativePosition = value;
  64. this.relativePositionHasBeenSet = true;
  65. }
  66. public get Size(): SizeF2D {
  67. return this.size;
  68. }
  69. public set Size(value: SizeF2D) {
  70. this.size = value;
  71. }
  72. public get MarginSize(): SizeF2D {
  73. return this.marginSize;
  74. }
  75. public get UpperLeftCorner(): PointF2D {
  76. return this.upperLeftCorner;
  77. }
  78. public get UpperLeftMarginCorner(): PointF2D {
  79. return this.upperLeftMarginCorner;
  80. }
  81. public get BorderLeft(): number {
  82. return this.borderLeft;
  83. }
  84. public set BorderLeft(value: number) {
  85. this.borderLeft = value;
  86. this.calculateRectangle();
  87. }
  88. public get BorderRight(): number {
  89. return this.borderRight;
  90. }
  91. public set BorderRight(value: number) {
  92. this.borderRight = value;
  93. this.calculateRectangle();
  94. }
  95. public get BorderTop(): number {
  96. return this.borderTop;
  97. }
  98. public set BorderTop(value: number) {
  99. this.borderTop = value;
  100. this.calculateRectangle();
  101. }
  102. public get BorderBottom(): number {
  103. return this.borderBottom;
  104. }
  105. public set BorderBottom(value: number) {
  106. this.borderBottom = value;
  107. this.calculateRectangle();
  108. }
  109. public get BorderMarginLeft(): number {
  110. return this.borderMarginLeft;
  111. }
  112. public set BorderMarginLeft(value: number) {
  113. this.borderMarginLeft = value;
  114. this.calculateMarginRectangle();
  115. }
  116. public get BorderMarginRight(): number {
  117. return this.borderMarginRight;
  118. }
  119. public set BorderMarginRight(value: number) {
  120. this.borderMarginRight = value;
  121. this.calculateMarginRectangle();
  122. }
  123. public get BorderMarginTop(): number {
  124. return this.borderMarginTop;
  125. }
  126. public set BorderMarginTop(value: number) {
  127. this.borderMarginTop = value;
  128. this.calculateMarginRectangle();
  129. }
  130. public get BorderMarginBottom(): number {
  131. return this.borderMarginBottom;
  132. }
  133. public set BorderMarginBottom(value: number) {
  134. this.borderMarginBottom = value;
  135. this.calculateMarginRectangle();
  136. }
  137. public get BoundingRectangle(): RectangleF2D {
  138. return this.boundingRectangle;
  139. }
  140. public get BoundingMarginRectangle(): RectangleF2D {
  141. return this.boundingMarginRectangle;
  142. }
  143. public get ChildElements(): BoundingBox[] {
  144. return this.childElements;
  145. }
  146. public set ChildElements(value: BoundingBox[]) {
  147. this.childElements = value;
  148. }
  149. public get Parent(): BoundingBox {
  150. return this.parent;
  151. }
  152. public set Parent(value: BoundingBox) {
  153. this.parent = value;
  154. }
  155. public get DataObject(): Object {
  156. return this.dataObject;
  157. }
  158. public setAbsolutePositionFromParent(): void {
  159. if (this.parent !== undefined) {
  160. this.absolutePosition.x = this.parent.AbsolutePosition.x + this.relativePosition.x;
  161. this.absolutePosition.y = this.parent.AbsolutePosition.y + this.relativePosition.y;
  162. } else {
  163. this.absolutePosition = this.relativePosition;
  164. }
  165. }
  166. /**
  167. * Calculate the the absolute position by adding up all relative positions of all parents (including the own rel. pos.)
  168. */
  169. public calculateAbsolutePosition(): void {
  170. this.absolutePosition.x = this.relativePosition.x;
  171. this.absolutePosition.y = this.relativePosition.y;
  172. let parent: BoundingBox = this.parent;
  173. while (parent !== undefined) {
  174. this.absolutePosition.x += parent.relativePosition.x;
  175. this.absolutePosition.y += parent.relativePosition.y;
  176. parent = parent.parent;
  177. }
  178. }
  179. /**
  180. * This method calculates the Absolute Positions recursively
  181. */
  182. public calculateAbsolutePositionsRecursiveWithoutTopelement(): void {
  183. this.absolutePosition.x = 0.0;
  184. this.absolutePosition.y = 0.0;
  185. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  186. const child: BoundingBox = this.ChildElements[idx];
  187. child.calculateAbsolutePositionsRecursive(this.absolutePosition.x, this.absolutePosition.y);
  188. }
  189. }
  190. /**
  191. * This method calculates the Absolute Positions recursively
  192. * from the root element down to the leaf elements
  193. * @param x
  194. * @param y
  195. */
  196. public calculateAbsolutePositionsRecursive(x: number, y: number): void {
  197. this.absolutePosition.x = this.relativePosition.x + x;
  198. this.absolutePosition.y = this.relativePosition.y + y;
  199. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  200. const child: BoundingBox = this.ChildElements[idx];
  201. child.calculateAbsolutePositionsRecursive(this.absolutePosition.x, this.absolutePosition.y);
  202. }
  203. }
  204. /**
  205. * calculates the absolute positions of all children of this boundingBox
  206. */
  207. public calculateAbsolutePositionsOfChildren(): void {
  208. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  209. const child: BoundingBox = this.ChildElements[idx];
  210. child.calculateAbsolutePositionsRecursive(this.absolutePosition.x, this.absolutePosition.y);
  211. }
  212. }
  213. /**
  214. * This method calculates the BoundingBoxes
  215. */
  216. public calculateBoundingBox(): void {
  217. if (this.childElements.length === 0) {
  218. return;
  219. }
  220. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  221. const childElement: BoundingBox = this.ChildElements[idx];
  222. childElement.calculateBoundingBox();
  223. }
  224. // initialize with max/min values
  225. let minLeft: number = Number.MAX_VALUE;
  226. let maxRight: number = Number.MIN_VALUE;
  227. let minTop: number = Number.MAX_VALUE;
  228. let maxBottom: number = Number.MIN_VALUE;
  229. let minMarginLeft: number = Number.MAX_VALUE;
  230. let maxMarginRight: number = Number.MIN_VALUE;
  231. let minMarginTop: number = Number.MAX_VALUE;
  232. let maxMarginBottom: number = Number.MIN_VALUE;
  233. // apart from symbol elements, where we initialize with the symbol's borders
  234. if (this.isSymbol) {
  235. minLeft = this.borderLeft;
  236. maxRight = this.borderRight;
  237. minTop = this.borderTop;
  238. maxBottom = this.borderBottom;
  239. minMarginLeft = this.borderMarginLeft;
  240. maxMarginRight = this.borderMarginRight;
  241. minMarginTop = this.borderMarginTop;
  242. maxMarginBottom = this.borderMarginBottom;
  243. }
  244. // ChildElements will have their borders calculated, so calculate current borders
  245. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  246. const childElement: BoundingBox = this.ChildElements[idx];
  247. minLeft = Math.min(minLeft, childElement.relativePosition.x + childElement.borderLeft);
  248. maxRight = Math.max(maxRight, childElement.relativePosition.x + childElement.borderRight);
  249. minTop = Math.min(minTop, childElement.relativePosition.y + childElement.borderTop);
  250. maxBottom = Math.max(maxBottom, childElement.relativePosition.y + childElement.borderBottom);
  251. minMarginLeft = Math.min(minMarginLeft, childElement.relativePosition.x + childElement.borderMarginLeft);
  252. maxMarginRight = Math.max(maxMarginRight, childElement.relativePosition.x + childElement.borderMarginRight);
  253. minMarginTop = Math.min(minMarginTop, childElement.relativePosition.y + childElement.borderMarginTop);
  254. maxMarginBottom = Math.max(maxMarginBottom, childElement.relativePosition.y + childElement.borderMarginBottom);
  255. }
  256. // ChildElements will have their borders calculated, so calculate current borders
  257. this.borderLeft = minLeft;
  258. this.borderRight = maxRight;
  259. this.borderTop = minTop;
  260. this.borderBottom = maxBottom;
  261. this.borderMarginLeft = minMarginLeft;
  262. this.borderMarginRight = maxMarginRight;
  263. this.borderMarginTop = minMarginTop;
  264. this.borderMarginBottom = maxMarginBottom;
  265. this.calculateRectangle();
  266. this.calculateMarginRectangle();
  267. this.xBordersHaveBeenSet = true;
  268. this.yBordersHaveBeenSet = true;
  269. }
  270. public calculateTopBottomBorders(): void {
  271. if (this.childElements.length === 0) {
  272. return;
  273. }
  274. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  275. const childElement: BoundingBox = this.ChildElements[idx];
  276. childElement.calculateTopBottomBorders();
  277. }
  278. let minTop: number = Number.MAX_VALUE;
  279. let maxBottom: number = Number.MIN_VALUE;
  280. let minMarginTop: number = Number.MAX_VALUE;
  281. let maxMarginBottom: number = Number.MIN_VALUE;
  282. if (this.yBordersHaveBeenSet) {
  283. minTop = this.borderTop;
  284. maxBottom = this.borderBottom;
  285. minMarginTop = this.borderMarginTop;
  286. maxMarginBottom = this.borderMarginBottom;
  287. }
  288. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  289. const childElement: BoundingBox = this.ChildElements[idx];
  290. minTop = Math.min(minTop, childElement.relativePosition.y + childElement.borderTop);
  291. maxBottom = Math.max(maxBottom, childElement.relativePosition.y + childElement.borderBottom);
  292. minMarginTop = Math.min(minMarginTop, childElement.relativePosition.y + childElement.borderMarginTop);
  293. maxMarginBottom = Math.max(maxMarginBottom, childElement.relativePosition.y + childElement.borderMarginBottom);
  294. }
  295. this.borderTop = minTop;
  296. this.borderBottom = maxBottom;
  297. this.borderMarginTop = minMarginTop;
  298. this.borderMarginBottom = maxMarginBottom;
  299. this.calculateRectangle();
  300. this.calculateMarginRectangle();
  301. }
  302. /**
  303. * This method computes the first non-overlapping position in the placementPsi Element for the current (this) positionAndShapeInfo
  304. * @param placementPsi
  305. * @param direction
  306. * @param position
  307. */
  308. public computeNonOverlappingPositionWithMargin(placementPsi: BoundingBox, direction: ColDirEnum, position: PointF2D): void {
  309. this.RelativePosition = new PointF2D(position.x, position.y);
  310. this.setAbsolutePositionFromParent();
  311. let currentPosition: number = 0.0;
  312. let hasBeenMoved: boolean = false;
  313. do {
  314. switch (direction) {
  315. case ColDirEnum.Left:
  316. case ColDirEnum.Right:
  317. currentPosition = this.relativePosition.x;
  318. placementPsi.calculateMarginPositionAlongDirection(this, direction);
  319. hasBeenMoved = Math.abs(currentPosition - this.relativePosition.x) > 0.001;
  320. break;
  321. case ColDirEnum.Up:
  322. case ColDirEnum.Down:
  323. currentPosition = this.relativePosition.y;
  324. placementPsi.calculateMarginPositionAlongDirection(this, direction);
  325. hasBeenMoved = Math.abs(currentPosition - this.relativePosition.y) > 0.001;
  326. break;
  327. default:
  328. throw new ArgumentOutOfRangeException("direction");
  329. }
  330. }
  331. while (hasBeenMoved);
  332. }
  333. /**
  334. * This method detects a collision (without margins)
  335. * @param psi
  336. * @returns {boolean}
  337. */
  338. public collisionDetection(psi: BoundingBox): boolean {
  339. const overlapWidth: number = Math.min(this.AbsolutePosition.x + this.borderRight, psi.absolutePosition.x + psi.borderRight)
  340. - Math.max(this.AbsolutePosition.x + this.borderLeft, psi.absolutePosition.x + psi.borderLeft);
  341. const overlapHeight: number = Math.min(this.AbsolutePosition.y + this.borderBottom, psi.absolutePosition.y + psi.borderBottom)
  342. - Math.max(this.AbsolutePosition.y + this.borderTop, psi.absolutePosition.y + psi.borderTop);
  343. if (overlapWidth > 0 && overlapHeight > 0) {
  344. return true;
  345. }
  346. return false;
  347. }
  348. /**
  349. * This method checks if the given Psi's Margins lie inside the current Psi's Margins.
  350. * @param psi
  351. * @returns {boolean}
  352. */
  353. public liesInsideBorders(psi: BoundingBox): boolean {
  354. const leftBorderInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= (psi.absolutePosition.x + psi.borderLeft)
  355. && (psi.absolutePosition.x + psi.borderLeft) <= (this.AbsolutePosition.x + this.borderRight);
  356. const rightBorderInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= (psi.absolutePosition.x + psi.borderRight)
  357. && (psi.absolutePosition.x + psi.borderRight) <= (this.AbsolutePosition.x + this.borderRight);
  358. if (leftBorderInside && rightBorderInside) {
  359. const topBorderInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= (psi.absolutePosition.y + psi.borderTop)
  360. && (psi.absolutePosition.y + psi.borderTop) <= (this.AbsolutePosition.y + this.borderBottom);
  361. const bottomBorderInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= (psi.absolutePosition.y + psi.borderBottom)
  362. && (psi.absolutePosition.y + psi.borderBottom) <= (this.AbsolutePosition.y + this.borderBottom);
  363. if (topBorderInside && bottomBorderInside) {
  364. return true;
  365. }
  366. }
  367. return false;
  368. }
  369. public pointLiesInsideBorders(position: PointF2D): boolean {
  370. const xInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= position.x && position.x <= (this.AbsolutePosition.x + this.borderRight);
  371. if (xInside) {
  372. const yInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= position.y && position.y <= (this.AbsolutePosition.y + this.borderBottom);
  373. if (yInside) {
  374. return true;
  375. }
  376. }
  377. return false;
  378. }
  379. /**
  380. * This method detects a collision (margin-wide)
  381. * @param psi
  382. * @returns {boolean}
  383. */
  384. public marginCollisionDetection(psi: BoundingBox): boolean {
  385. const overlapWidth: number = Math.min(this.AbsolutePosition.x + this.borderMarginRight, psi.absolutePosition.x + psi.borderMarginRight)
  386. - Math.max(this.AbsolutePosition.x + this.borderMarginLeft, psi.absolutePosition.x + psi.borderMarginLeft);
  387. const overlapHeight: number = Math.min(this.AbsolutePosition.y + this.borderMarginBottom, psi.absolutePosition.y + psi.borderMarginBottom)
  388. - Math.max(this.AbsolutePosition.y + this.borderMarginTop, psi.absolutePosition.y + psi.borderMarginTop);
  389. if (overlapWidth > 0 && overlapHeight > 0) {
  390. return true;
  391. }
  392. return false;
  393. }
  394. /**
  395. * This method checks if the given Psi's Margins lie inside the current Psi's Margins
  396. * @param psi
  397. * @returns {boolean}
  398. */
  399. public liesInsideMargins(psi: BoundingBox): boolean {
  400. const leftMarginInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= (psi.absolutePosition.x + psi.borderMarginLeft)
  401. && (psi.absolutePosition.x + psi.borderMarginLeft) <= (this.AbsolutePosition.x + this.borderMarginRight);
  402. const rightMarginInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= (psi.absolutePosition.x + psi.borderMarginRight)
  403. && (psi.absolutePosition.x + psi.borderMarginRight) <= (this.AbsolutePosition.x + this.borderMarginRight);
  404. if (leftMarginInside && rightMarginInside) {
  405. const topMarginInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= (psi.absolutePosition.y + psi.borderMarginTop)
  406. && (psi.absolutePosition.y + psi.borderMarginTop) <= (this.AbsolutePosition.y + this.borderMarginBottom);
  407. const bottomMarginInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= (psi.absolutePosition.y + psi.borderMarginBottom)
  408. && (psi.absolutePosition.y + psi.borderMarginBottom) <= (this.AbsolutePosition.y + this.borderMarginBottom);
  409. if (topMarginInside && bottomMarginInside) {
  410. return true;
  411. }
  412. }
  413. return false;
  414. }
  415. public pointLiesInsideMargins(position: PointF2D): boolean {
  416. const xInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= position.x
  417. && position.x <= (this.AbsolutePosition.x + this.borderMarginRight);
  418. if (xInside) {
  419. const yInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= position.y
  420. && position.y <= (this.AbsolutePosition.y + this.borderMarginBottom);
  421. if (yInside) {
  422. return true;
  423. }
  424. }
  425. return false;
  426. }
  427. /**
  428. * This method computes the first non-overlapping position in the placementPsi Element for the current (this) positionAndShapeInfo
  429. * @param placementPsi
  430. * @param direction
  431. * @param position
  432. */
  433. public computeNonOverlappingPosition(placementPsi: BoundingBox, direction: ColDirEnum, position: PointF2D): void {
  434. this.RelativePosition = new PointF2D(position.x, position.y);
  435. this.setAbsolutePositionFromParent();
  436. let currentPosition: number = 0.0;
  437. let hasBeenMoved: boolean = false;
  438. do {
  439. switch (direction) {
  440. case ColDirEnum.Left:
  441. case ColDirEnum.Right:
  442. currentPosition = this.relativePosition.x;
  443. placementPsi.calculatePositionAlongDirection(this, direction);
  444. hasBeenMoved = Math.abs(currentPosition - this.relativePosition.x) > 0.0001;
  445. break;
  446. case ColDirEnum.Up:
  447. case ColDirEnum.Down:
  448. currentPosition = this.relativePosition.y;
  449. placementPsi.calculatePositionAlongDirection(this, direction);
  450. hasBeenMoved = Math.abs(currentPosition - this.relativePosition.y) > 0.0001;
  451. break;
  452. default:
  453. throw new ArgumentOutOfRangeException("direction");
  454. }
  455. } while (hasBeenMoved); // as long as the element is moved
  456. }
  457. public getClickedObjectOfType<T>(clickPosition: PointF2D): T {
  458. const obj: Object = this.dataObject;
  459. if (this.pointLiesInsideBorders(clickPosition) && (<T>obj !== undefined)) {
  460. return (obj as T);
  461. }
  462. for (let idx: number = 0, len: number = this.childElements.length; idx < len; ++idx) {
  463. const psi: BoundingBox = this.childElements[idx];
  464. const innerObject: Object = psi.getClickedObjectOfType<T>(clickPosition);
  465. if (innerObject !== undefined) {
  466. return (innerObject as T);
  467. }
  468. }
  469. return undefined;
  470. }
  471. public getObjectsInRegion<T>(region: BoundingBox, liesInside: boolean = true): T[] {
  472. if (<T>this.dataObject !== undefined) {
  473. if (liesInside) {
  474. if (region.liesInsideBorders(this)) {
  475. return [this.dataObject as T];
  476. }
  477. } else {
  478. if (region.collisionDetection(this)) {
  479. return [this.dataObject as T];
  480. }
  481. }
  482. // FIXME Andrea: add here "return []"?
  483. }
  484. const result: T[] = [];
  485. for (const child of this.childElements) {
  486. result.concat(child.getObjectsInRegion<T>(region, liesInside));
  487. }
  488. return result;
  489. //return this.childElements.SelectMany(psi => psi.getObjectsInRegion<T>(region, liesInside));
  490. }
  491. protected calculateRectangle(): void {
  492. this.upperLeftCorner = new PointF2D(this.borderLeft, this.borderTop);
  493. this.size = new SizeF2D(this.borderRight - this.borderLeft, this.borderBottom - this.borderTop);
  494. this.boundingRectangle = RectangleF2D.createFromLocationAndSize(this.upperLeftCorner, this.size);
  495. }
  496. protected calculateMarginRectangle(): void {
  497. this.upperLeftMarginCorner = new PointF2D(this.borderMarginLeft, this.borderMarginTop);
  498. this.marginSize = new SizeF2D(this.borderMarginRight - this.borderMarginLeft, this.borderMarginBottom - this.borderMarginTop);
  499. this.boundingMarginRectangle = RectangleF2D.createFromLocationAndSize(this.upperLeftMarginCorner, this.marginSize);
  500. }
  501. /**
  502. * This method calculates the margin border along the given direction so that no collision takes place along this direction
  503. * @param toBePlaced
  504. * @param direction
  505. */
  506. private calculateMarginPositionAlongDirection(toBePlaced: BoundingBox, direction: ColDirEnum): void {
  507. // now this will be the "known" Element, about to get bigger with the toBePlaced
  508. // eg toBePlaced will always be in the PositionAndShape hierarchy a Child of this
  509. // example: this = StaffEntry, toBePlaced = Accidental
  510. // logical return
  511. if (this === toBePlaced) {
  512. return;
  513. }
  514. // check for collision only at symbols and return border
  515. if (this.isSymbol && this.marginCollisionDetection(toBePlaced)) {
  516. let shiftDistance: number = 0;
  517. switch (direction) {
  518. case ColDirEnum.Left:
  519. shiftDistance = (this.absolutePosition.x + this.borderMarginLeft) - (toBePlaced.absolutePosition.x + toBePlaced.borderMarginRight);
  520. toBePlaced.relativePosition.x += shiftDistance;
  521. toBePlaced.absolutePosition.x += shiftDistance;
  522. return;
  523. case ColDirEnum.Right:
  524. shiftDistance = (this.absolutePosition.x + this.borderMarginRight) - (toBePlaced.absolutePosition.x + toBePlaced.borderMarginLeft);
  525. toBePlaced.relativePosition.x += shiftDistance;
  526. toBePlaced.absolutePosition.x += shiftDistance;
  527. return;
  528. case ColDirEnum.Up:
  529. shiftDistance = (this.absolutePosition.y + this.borderMarginTop) - (toBePlaced.absolutePosition.y + toBePlaced.borderMarginBottom);
  530. toBePlaced.relativePosition.y += shiftDistance;
  531. toBePlaced.absolutePosition.y += shiftDistance;
  532. return;
  533. case ColDirEnum.Down:
  534. shiftDistance = (this.absolutePosition.y + this.borderMarginBottom) - (toBePlaced.absolutePosition.y + toBePlaced.borderMarginTop);
  535. toBePlaced.relativePosition.y += shiftDistance;
  536. toBePlaced.absolutePosition.y += shiftDistance;
  537. return;
  538. default:
  539. throw new ArgumentOutOfRangeException("direction");
  540. }
  541. }
  542. // perform check for all children iteratively and return border from children symbols
  543. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  544. const childElement: BoundingBox = this.ChildElements[idx];
  545. childElement.calculateMarginPositionAlongDirection(toBePlaced, direction);
  546. }
  547. }
  548. /**
  549. * This method calculates the border along the given direction so that no collision takes place along this direction
  550. * @param toBePlaced
  551. * @param direction
  552. */
  553. private calculatePositionAlongDirection(toBePlaced: BoundingBox, direction: ColDirEnum): void {
  554. // now this will be the "known" Element, about to get bigger with the toBePlaced
  555. // eg toBePlaced will always be in the PositionAndShape hierarchy a Child of this
  556. // example: this = StaffEntry, toBePlaced = Accidental
  557. // logical return
  558. if (this === toBePlaced) {
  559. return;
  560. }
  561. // check for collision only at symbols and return border
  562. if (this.isSymbol && this.collisionDetection(toBePlaced)) {
  563. let shiftDistance: number;
  564. switch (direction) {
  565. case ColDirEnum.Left:
  566. shiftDistance = (this.absolutePosition.x + this.borderLeft) - (toBePlaced.absolutePosition.x + toBePlaced.borderRight);
  567. toBePlaced.relativePosition.x += shiftDistance;
  568. toBePlaced.absolutePosition.x += shiftDistance;
  569. return;
  570. case ColDirEnum.Right:
  571. shiftDistance = (this.absolutePosition.x + this.borderRight) - (toBePlaced.absolutePosition.x + toBePlaced.borderLeft);
  572. toBePlaced.relativePosition.x += shiftDistance;
  573. toBePlaced.absolutePosition.x += shiftDistance;
  574. return;
  575. case ColDirEnum.Up:
  576. shiftDistance = (this.absolutePosition.y + this.borderTop) - (toBePlaced.absolutePosition.y + toBePlaced.borderBottom);
  577. toBePlaced.relativePosition.y += shiftDistance;
  578. toBePlaced.absolutePosition.y += shiftDistance;
  579. return;
  580. case ColDirEnum.Down:
  581. shiftDistance = (this.absolutePosition.y + this.borderBottom) - (toBePlaced.absolutePosition.y + toBePlaced.borderTop);
  582. toBePlaced.relativePosition.y += shiftDistance;
  583. toBePlaced.absolutePosition.y += shiftDistance;
  584. return;
  585. default:
  586. throw new ArgumentOutOfRangeException("direction");
  587. }
  588. }
  589. // perform check for all children iteratively and return border from children symbols
  590. for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
  591. const childElement: BoundingBox = this.ChildElements[idx];
  592. childElement.calculatePositionAlongDirection(toBePlaced, direction);
  593. }
  594. }
  595. }
  596. export enum ColDirEnum {
  597. Left = 0,
  598. Right = 1,
  599. Up = 2,
  600. Down = 3
  601. }