intersection-model.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * iGeom by LInE
  3. * Provides intersetion point to Geometric Objects (GO)
  4. * www.matematica.br/igeom
  5. * ./app/components/intersection-component/models/intersection-model.js
  6. * @calledby ./app/components/<go>-component/models/<go>-model.js
  7. * @version 2020/11/02: Implemented Line instersection; changed "this.r" and "this.s" to "this.og1" and "this.og2"
  8. */
  9. import { PointModel } from "../../point-component/models/point-model";
  10. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  11. import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
  12. import { GeometricObject } from "../../../core/models/objects/geometric-object";
  13. import { setWith } from "lodash";
  14. export class IntersectionModel extends PointModel {
  15. // @param {GeometricObject} og1 Geometric Object
  16. // @param {GeometricObject} og2 Geometric Object
  17. // @param {boolean} visible Visiblity of Object
  18. // @param {number} index Index position of Object ex (1)
  19. // @param {id} id indentity of intersection ex: 0
  20. constructor(posX, posY, label, og1, og2, visible, index, id) {
  21. try { //D //leo
  22. super(posX, posY, label, id);
  23. this.og1 = og1;
  24. this.og2 = og2;
  25. super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  26. this.visible = visible;
  27. this.index = index;
  28. this.color = -65536;
  29. this.definitions = this.getDefinitions();
  30. } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: constructor(.): erro!\n" + e.stack); }
  31. }
  32. // Update properties of this Intersection
  33. // @param {DrawerAggregator} aggregator Drawer Aggregator
  34. // @param {event} event
  35. update(aggregator, event) {
  36. try { //D //leo
  37. const intersections = this.og1.getIntersection(this.og2);
  38. if (intersections.length == 1) {
  39. this.visible = true;
  40. const intersection = intersections[0];
  41. this.posX = parseFloat(intersection.posX.toFixed(2));
  42. this.posY = parseFloat(intersection.posY.toFixed(2));
  43. // this.visible = intersection.visible;
  44. if (!this.og1.insideSegment(this.posX, this.posY)) {
  45. this.posX = Number.MAX_SAFE_INTEGER;
  46. this.posY = Number.MAX_SAFE_INTEGER;
  47. this.visible = false;
  48. this.definitions = this.getDefinitions();
  49. return;
  50. }
  51. if (!this.og2.insideSegment(this.posX, this.posY)) {
  52. this.posX = Number.MAX_SAFE_INTEGER;
  53. this.posY = Number.MAX_SAFE_INTEGER;
  54. this.visible = false;
  55. this.definitions = this.getDefinitions();
  56. return;
  57. }
  58. return;
  59. }
  60. if (intersections.length > 1) {
  61. for (let index = 0; index < intersections.length; index++) {
  62. const intersection = intersections[index];
  63. if (this.index == index) {
  64. this.posX = parseFloat(intersection.posX.toFixed(2));
  65. this.posY = parseFloat(intersection.posY.toFixed(2));
  66. this.visible = intersection.visible;
  67. }
  68. }
  69. }
  70. this.definitions = this.getDefinitions();
  71. } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: update(aggregator, event): erro!"); }
  72. }
  73. getDefinitions() {
  74. try { //D //leo
  75. return [{ id: this.og1.id }, { id: this.og2.id }, { id: this.index + 1 }, { id: this.visible ? 1 : 0 }];
  76. } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: getDefinitions(): erro!"); }
  77. }
  78. bind(posX, posY, label, og1, og2, visible, index) {
  79. try { //D //leo
  80. super.bind(posX, posY, label);
  81. this.og1 = og1;
  82. this.og2 = og2;
  83. this.visible = visible;
  84. this.index = index;
  85. this.color = -65536;
  86. this.definitions = this.getDefinitions();
  87. super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  88. } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: bind(.): erro!\n" + e.stack); }
  89. }
  90. // Create new Intersection By Line of Script .geo
  91. // @param {Map} map JavaScript Map
  92. // @param {[]} list List of Generic Objects
  93. static do(map, list) {
  94. try { //D //leo
  95. const id = map.get("id");
  96. const og1_Id = map.get("param")[0];
  97. const og2_Id = map.get("param")[1];
  98. const index = map.get("param")[2] - 1;
  99. const visible = map.get("param")[5] == 1;
  100. const label = map.get("label")[0];
  101. const og1 = list.find(x => x.id == og1_Id);
  102. const og2 = list.find(x => x.id == og2_Id);
  103. const intersections = og1.getIntersection(og2); // intersection providade by the first geometric object
  104. if (intersections.length == 1) {
  105. const i = intersections[0];
  106. i.bind(i.posX, i.posY, label, og1, og2, true, index);
  107. return i;
  108. } else {
  109. const i = intersections.find(x => x.index == index);
  110. i.bind(i.posX, i.posY, label, og1, og2, true, index);
  111. return i;
  112. }
  113. } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: do(.): erro!"); }
  114. }
  115. }