BoundingBox.ts 31 KB

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