All files / src/pages/flowsheet-page/pinch-analysis/hen-generation/utils bounds.ts

94.73% Statements 18/19
100% Branches 10/10
100% Functions 4/4
92.85% Lines 13/14

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        453x                     198x 198x 198x         210x                                       525x 525x     491x 491x     52x 52x     1x 1x        
import { MARGIN_X, PINCH_GAP, streamCrossesPinch } from "./henLayoutUtils";
import { Bounds } from "../henTypes";
 
// full width of drawable area for streams/segments
  export const canvasBounds = (w: number) => ({
    left: MARGIN_X,
    right: (w) - MARGIN_X,
  });
 
/**
 * Computes the 1D intersection of two Bounds objects.
 * Returns the overlapping span { left, right } if they touch or overlap,
 * or null if the ranges dont overlap.
 */
export function ovBounds(a: Bounds, b: Bounds): Bounds | null {
  const left = Math.max(a.left, b.left);
  const right = Math.min(a.right, b.right);
  return left <= right ? { left, right } : null;
}
 
// mid point of bounds
export function center(b: Bounds) {
  return (b.left + b.right) / 2;
}
 
////////////////////////////////////////////////////////// NEED TO REFACTOR THIS TO CONSIDER ISHOT IF BOTH COLD/HOT PINCH EXIST.
/**
 * set horizontal bounds for where a stream/segment is allowed to render render (dependent on left/right/crossing of pinch line)
 * @param tSupply supply temp
 * @param tTarget target temp
 * @param pinch pinch temp value
 * @param width total diagram svg width
 * @param pinchX pinch line x position
 * @returns width of bounds for stream/segment.
 */
export function boundsFor(
  tSupply: number,
  tTarget: number,
  pinch: number | undefined,
  width: number,
  pinchX: number
): Bounds {
  const full = { left: MARGIN_X, right: (width) - MARGIN_X };
  if (pinch == null) return full;
 
  // if stream/segment crosses pinch line, bounds are full width of svg - x margin.
  const crosses = streamCrossesPinch(tSupply, tTarget, pinch);
  if (crosses) return full;
 
  // else if stream/segment temp range is below pinch temp, bounds are between pinch line (mindful of pinchgap margin) and left side of svg bounds.
  const above = tSupply > pinch && tTarget > pinch;
  if (above) return { left: full.left, right: pinchX - PINCH_GAP };
 
  // else if stream/segment temp rang is above pinch temp, bounds are between pinch line (mindful of pinchgap margin) and right side of svg bounds.
  const below = tSupply < pinch && tTarget < pinch;
  if (below) return { left: pinchX + PINCH_GAP, right: full.right };
 
  // if no pinch temp calculated, bounds are just the full svg width - x margins.
  return full;
}