All files / core/domain/classes/quadtree EdgeQuad.ts

59.09% Statements 13/22
14.29% Branches 2/14
50% Functions 2/4
63.16% Lines 12/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1034x 4x 4x 4x 4x     4x                                                                                     4x 13x 13x     4x                                                           4x                               4x  
import getQuadraticControlPoint from "../../utils/geometry/getQuadraticControlPoint";
import splitSquare from "../../utils/geometry/splitSquare";
import selfLoopToSquare from "../../utils/geometry/selfLoopToSquare";
import quadraticCurveToSquare from "../../utils/geometry/quadraticCurveToSquare";
import lineToSquare from "../../utils/geometry/lineToSquare";
import { QuadTree, Boundaries } from "../../utils/geometry/interfaces";
import { Edge } from "../../../interfaces";
import { AbstractQuad } from "./AbstractQuad";
import Graph from "../Graph";
 
/**
 * Sigma Quadtree Module for edges
 * ===============================
 *
 * Author: Sébastien Heymann,
 *   from the quad of Guillaume Plique (Yomguithereal)
 * Version: 0.2
 */
 
/**
 * Quad Functions
 * ------------
 *
 * The Quadtree functions themselves.
 * For each of those functions, we consider that in a splitted quad, the
 * index of each node is the following:
 * 0: top left
 * 1: top right
 * 2: bottom left
 * 3: bottom right
 *
 * Moreover, the hereafter quad's philosophy is to consider that if an element
 * collides with more than one nodes, this element belongs to each of the
 * nodes it collides with where other would let it lie on a higher node.
 */
 
/**
 * Sigma Quad Constructor
 * ----------------------
 *
 * The edgequad API as exposed to sigma
 */
 
/**
 * The edgequad core that will become the sigma interface with the quadtree.
 *
 * property {object} tree     Property holding the quadtree object.
 * property {object} cache    Cache for the area method.
 * property {boolean} enabled Can index and retreive elements.
 */
export default class EdgeQuad extends AbstractQuad<Edge> {
  constructor(private graph: Graph) {
    super();
  }
 
  protected addItem(edge: Edge) {
    const { prefix } = this;
    const [source, target] = this.graph.nodes(edge.source, edge.target);
 
    const e = {
      x1: source[`${prefix}x`],
      y1: source[`${prefix}y`],
      x2: target[`${prefix}x`],
      y2: target[`${prefix}y`],
      size: edge[`${prefix}size`] || 0
    };
 
    // Inserting edge
    if (edge.type === "curve" || edge.type === "curvedArrow") {
      if (source.id === target.id) {
        const n = {
          x: source[`${prefix}x`],
          y: source[`${prefix}y`],
          size: source[`${prefix}size`] || 0
        };
        this.quadInsert(edge, selfLoopToSquare(n), this.tree!);
      } else {
        const cp = getQuadraticControlPoint(e.x1, e.y1, e.x2, e.y2);
        this.quadInsert(edge, quadraticCurveToSquare(e, cp), this.tree!);
      }
    } else {
      this.quadInsert(edge, lineToSquare(e), this.tree!);
    }
  }
 
  protected createQuadTree(
    bounds: Boundaries,
    level: number,
    maxElements: number,
    maxLevel: number
  ): QuadTree<Edge> {
    return {
      level: level || 0,
      bounds,
      corners: splitSquare(bounds),
      maxElements: maxElements || 40,
      maxLevel: maxLevel || 8,
      elements: [],
      nodes: []
    };
  }
}