All files / core/domain/middleware rescale.ts

4.44% Statements 2/45
0% Branches 0/36
0% Functions 0/4
4.88% Lines 2/41

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1274x                       4x                                                                                                                                                                                                                                    
import getBoundaries from "../utils/geometry/getBoundaries";
import Sigma from "../classes/Sigma";
import { Keyed } from "../../interfaces";
 
/**
 * This middleware will rescale the graph such that it takes an optimal space
 * on the renderer.
 *
 * @param {?string} readPrefix  The read prefix.
 * @param {?string} writePrefix The write prefix.
 * @param {object}  options     The parameters.
 */
export default function rescale(
  this: Sigma,
  readPrefix: string,
  writePrefix: string,
  options?: any
) {
  let a: number;
  let b: number;
  let c: number;
  let d: number;
  let scale: number;
  const settings = this.settings.embedObjects(options || {});
  const bounds =
    settings("bounds") || getBoundaries(this.graph, readPrefix, true);
 
  let { minX, minY, maxX, maxY } = bounds;
  const { sizeMax, weightMax } = bounds;
  const w = settings("width") || 1;
  const h = settings("height") || 1;
 
  let rescaleSettings = settings("autoRescale");
  const validSettings: Keyed<any> = {
    nodePosition: 1,
    nodeSize: 1,
    edgeSize: 1
  };
 
  /**
   * What elements should we rescale?
   */
  if (!(rescaleSettings instanceof Array)) {
    rescaleSettings = ["nodePosition", "nodeSize", "edgeSize"];
  }
 
  rescaleSettings.forEach((setting: any) => {
    if (!validSettings[setting])
      throw new Error(`The rescale setting "${setting}" is not recognized.`);
  });
 
  const np = ~rescaleSettings.indexOf("nodePosition");
  const ns = ~rescaleSettings.indexOf("nodeSize");
  const es = ~rescaleSettings.indexOf("edgeSize");
 
  /**
   * First, we compute the scaling ratio, without considering the sizes
   * of the nodes : Each node will have its center in the canvas, but might
   * be partially out of it.
   */
  scale =
    settings("scalingMode") === "outside"
      ? Math.max(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1))
      : Math.min(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1));
 
  /**
   * Then, we correct that scaling ratio considering a margin, which is
   * basically the size of the biggest node.
   * This has to be done as a correction since to compare the size of the
   * biggest node to the X and Y values, we have to first get an
   * approximation of the scaling ratio.
   * */
  const margin =
    (settings("rescaleIgnoreSize")
      ? 0
      : (settings("maxNodeSize") || sizeMax) / scale) +
    (settings("sideMargin") || 0);
  maxX += margin;
  minX -= margin;
  maxY += margin;
  minY -= margin;
 
  // Fix the scaling with the new extrema:
  scale =
    settings("scalingMode") === "outside"
      ? Math.max(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1))
      : Math.min(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1));
 
  // Size homothetic parameters:
  if (!settings("maxNodeSize") && !settings("minNodeSize")) {
    a = 1;
    b = 0;
  } else if (settings("maxNodeSize") === settings("minNodeSize")) {
    a = 0;
    b = +settings("maxNodeSize");
  } else {
    a = (settings("maxNodeSize") - settings("minNodeSize")) / sizeMax;
    b = +settings("minNodeSize");
  }
 
  if (!settings("maxEdgeSize") && !settings("minEdgeSize")) {
    c = 1;
    d = 0;
  } else if (settings("maxEdgeSize") === settings("minEdgeSize")) {
    c = 0;
    d = +settings("minEdgeSize");
  } else {
    c = (settings("maxEdgeSize") - settings("minEdgeSize")) / weightMax;
    d = +settings("minEdgeSize");
  }
 
  // Rescale the nodes and edges:
  this.graph.edges().forEach(edge => {
    edge[`${writePrefix}size`] =
      edge[`${readPrefix}size`] * (es ? c : 1) + (es ? d : 0);
  });
 
  this.graph.nodes().forEach(node => {
    node[`${writePrefix}size`] =
      node[`${readPrefix}size`] * (ns ? a : 1) + (ns ? b : 0);
    node[`${writePrefix}x`] =
      (node[`${readPrefix}x`] - (maxX + minX) / 2) * (np ? scale : 1);
    node[`${writePrefix}y`] =
      (node[`${readPrefix}y`] - (maxY + minY) / 2) * (np ? scale : 1);
  });
}