Home Reference Source Repository

src/app/components/intersection-component/models/intersection-model.js

/*
 * iGeom by LInE
 * Provides intersetion point to Geometric Objects (GO)
 * www.matematica.br/igeom
 * ./app/components/intersection-component/models/intersection-model.js
 * @calledby ./app/components/<go>-component/models/<go>-model.js
 * @version 2020/11/02: Implemented Line instersection; changed "this.r" and "this.s" to "this.og1" and "this.og2"
 */

import { PointModel } from "../../point-component/models/point-model";
import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
import { GeometricObject } from "../../../core/models/objects/geometric-object";
import { setWith } from "lodash";

export class IntersectionModel extends PointModel {

  // @param {GeometricObject} og1 Geometric Object 
  // @param {GeometricObject} og2 Geometric Object
  // @param {boolean} visible Visiblity of Object 
  // @param {number} index Index position of Object ex (1)
  // @param {id} id indentity of intersection ex: 0
  constructor(posX, posY, label, og1, og2, visible, index, id) {
    try { //D //leo
      super(posX, posY, label, id);
      this.og1 = og1;
      this.og2 = og2;
      super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
      this.visible = visible;
      this.index = index;
      this.color = -65536;
      this.definitions = this.getDefinitions();
    } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: constructor(.): erro!\n" + e.stack); }
  }

  // Update properties of this Intersection
  // @param {DrawerAggregator} aggregator Drawer Aggregator 
  // @param {event} event
  update(aggregator, event) {
    try { //D //leo
      const intersections = this.og1.getIntersection(this.og2);
      if (intersections.length == 1) {
        this.visible = true;
        const intersection = intersections[0];
        this.posX = parseFloat(intersection.posX.toFixed(2));
        this.posY = parseFloat(intersection.posY.toFixed(2));
        // this.visible = intersection.visible;
        if (!this.og1.insideSegment(this.posX, this.posY)) {
          this.posX = Number.MAX_SAFE_INTEGER;
          this.posY = Number.MAX_SAFE_INTEGER;
          this.visible = false;
          this.definitions = this.getDefinitions();
          return;
        }
        if (!this.og2.insideSegment(this.posX, this.posY)) {
          this.posX = Number.MAX_SAFE_INTEGER;
          this.posY = Number.MAX_SAFE_INTEGER;
          this.visible = false;
          this.definitions = this.getDefinitions();
          return;
        }
        return;
      }
      if (intersections.length > 1) {
        for (let index = 0; index < intersections.length; index++) {
          const intersection = intersections[index];
          if (this.index == index) {
            this.posX = parseFloat(intersection.posX.toFixed(2));
            this.posY = parseFloat(intersection.posY.toFixed(2));
            this.visible = intersection.visible;
          }
        }
      }
      this.definitions = this.getDefinitions();
    } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: update(aggregator, event): erro!"); }
  }

  getDefinitions() {
    try { //D //leo
      return [{ id: this.og1.id }, { id: this.og2.id }, { id: this.index + 1 }, { id: this.visible ? 1 : 0 }];
    } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: getDefinitions(): erro!"); }
  }

  bind(posX, posY, label, og1, og2, visible, index) {
    try { //D //leo
      super.bind(posX, posY, label);
      this.og1 = og1;
      this.og2 = og2;
      this.visible = visible;
      this.index = index;
      this.color = -65536;
      this.definitions = this.getDefinitions();
      super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
    } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: bind(.): erro!\n" + e.stack); }
  }

  // Create new Intersection By Line of Script .geo
  // @param {Map} map JavaScript Map
  // @param {[]} list List of Generic Objects
  static do(map, list) {
    try { //D //leo
      const id = map.get("id");
      const og1_Id = map.get("param")[0];
      const og2_Id = map.get("param")[1];
      const index = map.get("param")[2] - 1;
      const visible = map.get("param")[5] == 1;
      const label = map.get("label")[0];
      const og1 = list.find(x => x.id == og1_Id);
      const og2 = list.find(x => x.id == og2_Id);

      const intersections = og1.getIntersection(og2); // intersection providade by the first geometric object

      if (intersections.length == 1) {
        const i = intersections[0];
        i.bind(i.posX, i.posY, label, og1, og2, true, index);
        return i;
      } else {
        const i = intersections.find(x => x.index == index);
        i.bind(i.posX, i.posY, label, og1, og2, true, index);
        return i;
      }
    } catch (e) { console.log("app/components/intersection-component/models/intersection-model.js: do(.): erro!"); }
  }

}