All files / src/pages/flowsheet-page/flowsheet/Canvas ConnectionGraphic.tsx

59.88% Statements 100/167
31.18% Branches 29/93
81.81% Functions 18/22
58.64% Lines 95/162

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577                      357071x 357071x   357071x 357071x       33x 33x 33x 33x 33x                             178210x       71284x                   35642x     35642x                                                   17821x         66892x 17821x 17821x       17821x 17821x 16723x       16723x       16723x                                             16723x                         16723x 16723x       16723x       16723x           1098x       1098x         1098x       1098x                 1098x                                                                           17821x   17821x             17821x 9078x 8743x 8743x         17821x                   17821x 17821x 17821x                                     71284x   71284x                         71284x                                   71284x     33x     35642x                               17821x 17821x   17821x 17821x     17821x       17821x 17821x 17821x     17821x 17821x 17821x       17821x     17821x         17821x         17821x         17821x             17821x       17821x         17821x   17821x       17821x         17821x 17821x             17821x               17821x   17821x                                                                                                             33x       17821x             17821x 17821x 17821x                                                                       17821x             106926x                                               235x 235x 235x 235x 235x   207x 207x 207x   206x 206x             206x           206x             206x         618x        
import { Arrow, Line } from "react-konva";
import { DirectionEnum } from "@/api/apiStore.gen";
import {
  useFlowsheetObjectsIdMap,
  useObjectsGraphicObjectsMap,
} from "@/hooks/flowsheetObjects";
 
class SkPoint {
  X: number;
  Y: number;
  constructor(
    public x: number,
    public y: number,
  ) {
    this.X = x;
    this.Y = y;
  }
}
 
enum Direction {
  Up,
  Down,
  Left,
  Right,
}
 
export type ConnectedObjectInfo = {
  position: SkPoint; // absolute coordinates of the object
  size: SkPoint; // width and height of the object
  rotation: number; // degrees
  is_flipped: boolean;
};
export type ConnectionInfo = {
  location: SkPoint; // relative offset, between O and 1, of the connections position on the unit op.
  direction: DirectionEnum;
};
 
function add(p1: SkPoint, p2: SkPoint): SkPoint {
  return new SkPoint(p1.x + p2.x, p1.y + p2.y);
}
 
function multiply(p: SkPoint, p2: SkPoint): SkPoint {
  return new SkPoint(p.x * p2.x, p.y * p2.y);
}
 
function snapToGrid(value) {
  return value;
}
 
function getConDirection(end: SkPoint, notch: SkPoint) {
  //Gets the direction of connection with reference to the operation it is connected to.
  //eg. If a connection is at the top of an operation, it will have the direction Up.
  Iif (end.x == notch.x) {
    return end.y > notch.y ? Direction.Up : Direction.Down;
  }
  return end.x > notch.x ? Direction.Left : Direction.Right;
}
 
type GetSegmentsProps = {
  isVertical: boolean;
  startConLocation: SkPoint;
  endConLocation: SkPoint;
  startNotchLocation: SkPoint;
  endNotchLocation: SkPoint;
};
 
function getMidPoints(props: GetSegmentsProps): SkPoint[] {
  /*
   *Method to set the mid points of a connection line between two operations.
   *@param isVertical - A boolean to determine whether the generated points should have the same x or the same y value
   *@param startConLocation - The coordinates of the start of the connection
   *@param endConLocation - The coordinates of the end of the connection
   *@param startNotchLocation - The coordinates of the first point (Notch) from the start
   *@param endNotchLocation - The coordinates of the last point (Notch) before the end
   */
  const {
    isVertical,
    startConLocation,
    endConLocation,
    startNotchLocation,
    endNotchLocation,
  } = props;
  let startPoint: SkPoint;
  let endPoint: SkPoint;
  //Removed midpoint as it is not currently ever used. Can be used in future and spliced into returned points if connection logic is revisited
  // let midPoint: SkPoint | undefined
  const avgX = (a: SkPoint, b: SkPoint) => (a.x + b.x) / 2;
  const avgY = (a: SkPoint, b: SkPoint) => (a.y + b.y) / 2;
  const startConDirection = getConDirection(
    startConLocation,
    startNotchLocation,
  );
  const endConDirection = getConDirection(endConLocation, endNotchLocation);
  if (isVertical) {
    startPoint = new SkPoint(
      avgX(startNotchLocation, endNotchLocation),
      startNotchLocation.y,
    );
    endPoint = new SkPoint(
      avgX(startNotchLocation, endNotchLocation),
      endNotchLocation.y,
    );
    Iif (
      startConDirection == Direction.Up ||
      startConDirection == Direction.Down
    ) {
      if (endConDirection == Direction.Left) {
        startPoint.x = Math.min(
          avgX(startNotchLocation, endNotchLocation),
          endNotchLocation.x,
        );
        endPoint.x = Math.min(
          avgX(startNotchLocation, endNotchLocation),
          endNotchLocation.x,
        );
      I} else if (endConDirection == Direction.Right) {
        startPoint.x = Math.max(
          avgX(startNotchLocation, endNotchLocation),
          endNotchLocation.x,
        );
        endPoint.x = Math.max(
          avgX(startNotchLocation, endNotchLocation),
          endNotchLocation.x,
        );
      }
    I} else if (startConDirection == Direction.Left) {
      startPoint.x = Math.min(
        avgX(startNotchLocation, endNotchLocation),
        startNotchLocation.x,
      );
      endPoint.x = Math.min(
        avgX(startNotchLocation, endNotchLocation),
        startNotchLocation.x,
      );
      Iif (endConDirection == Direction.Left) {
        endPoint.x = Math.min(startNotchLocation.x, endNotchLocation.x);
        startPoint.x = Math.min(startNotchLocation.x, endNotchLocation.x);
      }
    } else if (startConDirection == Direction.Right) {
      startPoint.x = Math.max(
        avgX(startNotchLocation, endNotchLocation),
        startNotchLocation.x,
      );
      endPoint.x = Math.max(
        avgX(startNotchLocation, endNotchLocation),
        startNotchLocation.x,
      );
      Iif (endConDirection == Direction.Right) {
        endPoint.x = Math.max(startNotchLocation.x, endNotchLocation.x);
        startPoint.x = Math.max(startNotchLocation.x, endNotchLocation.x);
      }
    }
  } else {
    startPoint = new SkPoint(
      startNotchLocation.x,
      avgY(startNotchLocation, endNotchLocation),
    );
    endPoint = new SkPoint(
      endNotchLocation.x,
      avgY(startNotchLocation, endNotchLocation),
    );
 
    if (
      startConDirection == Direction.Left ||
      startConDirection == Direction.Right
    ) {
      Iif (endConDirection == Direction.Up) {
        startPoint.y = Math.min(
          avgY(startNotchLocation, endNotchLocation),
          endNotchLocation.y,
        );
        endPoint.y = Math.min(
          avgY(startNotchLocation, endNotchLocation),
          endNotchLocation.y,
        );
      I} else if (endConDirection == Direction.Down) {
        startPoint.y = Math.max(
          avgY(startNotchLocation, endNotchLocation),
          endNotchLocation.y,
        );
        endPoint.y = Math.max(
          avgY(startNotchLocation, endNotchLocation),
          endNotchLocation.y,
        );
      }
    E} else if (startConDirection == Direction.Up) {
      startPoint.y = Math.min(
        avgY(startNotchLocation, endNotchLocation),
        startNotchLocation.y,
      );
      endPoint.y = Math.min(
        avgY(startNotchLocation, endNotchLocation),
        startNotchLocation.y,
      );
      Iif (endConDirection == Direction.Up) {
        startPoint.y = Math.min(startNotchLocation.y, endNotchLocation.y);
        endPoint.y = Math.min(startNotchLocation.y, endNotchLocation.y);
      }
    } else {
      startPoint.y = Math.max(
        avgY(startNotchLocation, endNotchLocation),
        startNotchLocation.y,
      );
      endPoint.y = Math.max(
        avgY(startNotchLocation, endNotchLocation),
        startNotchLocation.y,
      );
      Iif (endConDirection == Direction.Down) {
        startPoint.y = Math.max(startNotchLocation.y, endNotchLocation.y);
        endPoint.y = Math.max(startNotchLocation.y, endNotchLocation.y);
      }
    }
  }
  let points: SkPoint[] = [startPoint, endPoint];
  // if(midPoint){points.splice(1,0,midPoint)}
  return points;
}
 
function getUnitOpRotation(startOp, endOp) {
  //Gets the rotation of the unit operation, NOT the stream
  //Currently determines which is the unit op based on size, if refactoring, could look into adjusting this.
  let rotationUnitOp;
  if (startOp.size.x === 100) {
    rotationUnitOp = startOp;
  } else if (endOp.size.x === 100) {
    rotationUnitOp = endOp;
  } else E{
    rotationUnitOp = endOp;
  }
 
  return rotationUnitOp;
}
 
function checkVertical(
  startNotchLocation: SkPoint,
  endNotchLocation: SkPoint,
  startOp: ConnectedObjectInfo,
  endOp: ConnectedObjectInfo,
) {
  //Determines whether the middle segment of the line should be vertical or horizontal
  const rotationUnitOp = getUnitOpRotation(startOp, endOp);
  if (rotationUnitOp.rotation == 0) {
    return rotationUnitOp.is_flipped //Vertical if end op is on the same side as the outlet from start op
      ? Math.sign(startNotchLocation.x - endNotchLocation.x) === 1
      : Math.sign(startNotchLocation.x - endNotchLocation.x) === -1;
  E} else if (rotationUnitOp.rotation == 90) {
    //Vertical if end op is NOT on the same side as outlet from start op
    return rotationUnitOp.is_flipped
      ? Math.sign(startNotchLocation.y - endNotchLocation.y) === -1
      : Math.sign(startNotchLocation.y - endNotchLocation.y) === 1;
  I} else if (rotationUnitOp.rotation == 180) {
    return rotationUnitOp.is_flipped
      ? Math.sign(startNotchLocation.x - endNotchLocation.x) === -1
      : Math.sign(startNotchLocation.x - endNotchLocation.x) === 1;
  }
  return rotationUnitOp.is_flipped
    ? Math.sign(startNotchLocation.y - endNotchLocation.y) === 1
    : Math.sign(startNotchLocation.y - endNotchLocation.y) === -1;
}
 
function rotate(p: SkPoint, rotation: number): SkPoint {
  switch (rotation) {
    case 0:
      return p;
    case 90:
      return new SkPoint(-p.y, p.x);
    case 180:
      return new SkPoint(-p.x, -p.y);
    case 270:
      return new SkPoint(p.y, -p.x);
    default:
      throw new Error("Only 90, 180, and 270 degrees are supported.");
  }
}
 
function flip(p: SkPoint, flipH: boolean, rotation: number): SkPoint {
  Iif (flipH) {
    switch (rotation) {
      case 0:
        p.x = -p.x;
        return p;
      case 90:
        p.y = -p.y;
        return p;
      case 180:
        p.x = -p.x;
        return p;
      case 270:
        p.y = -p.y;
        return p;
      default:
        throw new Error("Only 90, 180, and 270 degrees are supported.");
    }
  }
  return p;
}
 
const half = new SkPoint(0.5, 0.5);
 
function convertToCenterRelative(point: SkPoint): SkPoint {
  return new SkPoint((point.x - 0.5) * 2, (point.y - 0.5) * 2);
}
 
function getConnectionLinePoints(
  startOp: ConnectedObjectInfo,
  startCon: ConnectionInfo,
  endOp: ConnectedObjectInfo,
  endCon: ConnectionInfo,
) {
  /*
   *Function to get the points for drawing the connection line between operations on the flowsheet.
   *Sets the locations for the starts and ends of the connection, as well as the notch points that are offset from each operation.
   *The first and last line segments are between the startCon/encCon locations and their respective notch points.
   *This method ensures that each of these line segments is oriented correctly based on the rotation and flip of their respective operation.
   *This method then calls "getMidPoints" to determine the coordinates of the points that make up the middle section of the connection.
   */
  const convertedStartConLocation = convertToCenterRelative(startCon.location);
  const convertedEndConLocation = convertToCenterRelative(endCon.location);
 
  const startOpSizeHalf = multiply(startOp.size, half);
  const endOpSizeHalf = multiply(endOp.size, half);
 
  // get locations relative to the center of the object
  const LocalStartLocation = multiply(
    startOpSizeHalf,
    convertedStartConLocation,
  );
  const LocalNotchStartLocation = add(LocalStartLocation, new SkPoint(10, 0)); // add notch offset
  const LocalEndLocation = multiply(endOpSizeHalf, convertedEndConLocation);
  const LocalNotchEndLocation = add(LocalEndLocation, new SkPoint(-10, 0)); // add notch offset
 
  // rotate
  const rotatedStartLocation = rotate(LocalStartLocation, startOp.rotation);
  const rotatedEndLocation = rotate(LocalEndLocation, endOp.rotation);
  const rotatedNotchStartLocation = rotate(
    LocalNotchStartLocation,
    startOp.rotation,
  );
  const rotatedNotchEndLocation = rotate(LocalNotchEndLocation, endOp.rotation);
 
  // flip
  const flippedStartLocation = flip(
    rotatedStartLocation,
    startOp.is_flipped,
    startOp.rotation,
  );
  const flippedEndLocation = flip(
    rotatedEndLocation,
    endOp.is_flipped,
    startOp.rotation,
  );
  const flippedNotchStartLocation = flip(
    rotatedNotchStartLocation,
    startOp.is_flipped,
    startOp.rotation,
  );
  const flippedNotchEndLocation = flip(
    rotatedNotchEndLocation,
    endOp.is_flipped,
    startOp.rotation,
  );
 
  // add the object position to get the absolute position
  const startConLocation = add(
    startOp.position,
    add(flippedStartLocation, startOpSizeHalf),
  );
  const startNotchLocation = add(
    startOp.position,
    add(flippedNotchStartLocation, startOpSizeHalf),
  );
 
  const startPoints: SkPoint[] = [startConLocation, startNotchLocation];
 
  const endConLocation = add(
    endOp.position,
    add(flippedEndLocation, endOpSizeHalf),
  );
  const endNotchLocation = add(
    endOp.position,
    add(flippedNotchEndLocation, endOpSizeHalf),
  );
 
  const endPoints: SkPoint[] = [endNotchLocation, endConLocation];
  const isVertical = checkVertical(
    startNotchLocation,
    endNotchLocation,
    startOp,
    endOp,
  );
  //Get the middle points of the connection line
  const midPoints = getMidPoints({
    isVertical,
    startConLocation,
    endConLocation,
    startNotchLocation,
    endNotchLocation,
  });
  // Create the array of points
  const points: SkPoint[] = startPoints.concat(midPoints).concat(endPoints);
 
  return { points, isVertical };
}
 
function snapToGridPoint(point) {
  return new SkPoint(snapToGrid(point.x), snapToGrid(point.y));
}
 
type ConnectionGraphicProps = {
  startOp: ConnectedObjectInfo;
  endOp: ConnectedObjectInfo;
  startCon: ConnectionInfo;
  endCon: ConnectionInfo;
  showArrow?: boolean;
  disabled?: boolean;
  strokeColor?: string;
  isEnergy?: boolean;
};
 
function checkTooSmall(isVertical, point1, point2, rotatedOP, pointerSize) {
  Iif (rotatedOP.rotation === 90 || rotatedOP.rotation === 270) {
    return isVertical
      ? Math.abs(point1.x - point2.x) < pointerSize * 3
      : Math.abs(point1.y - point2.y) < pointerSize * 3;
  }
  return isVertical
    ? Math.abs(point1.y - point2.y) < pointerSize * 3
    : Math.abs(point1.x - point2.x) < pointerSize * 3;
}
 
function createEndpoint(isVertical, point1, point2, rotatedOP, pointerSize) {
  // Handle special angle cases (90° and 270°)
  Iif (rotatedOP.rotation === 90 || rotatedOP.rotation === 270) {
    return isVertical
      ? new SkPoint(
          point2.x + pointerSize * Math.sign(point1.x - point2.x),
          point2.y,
        )
      : new SkPoint(
          point2.x,
          point2.y + pointerSize * Math.sign(point1.y - point2.y),
        );
  }
 
  // Default case for other angles
  return isVertical
    ? new SkPoint(
        point2.x,
        point2.y + pointerSize * Math.sign(point1.y - point2.y),
      )
    : new SkPoint(
        point2.x + pointerSize * Math.sign(point1.x - point2.x),
        point2.y,
      );
}
 
export const ConnectionGraphic = (props: ConnectionGraphicProps) => {
  /*
   *Returns a line to illustrate the connection between operations on the flowsheet
   */
  const { points, isVertical } = getConnectionLinePoints(
    props.startOp,
    props.startCon,
    props.endOp,
    props.endCon,
  );
 
  let arrowPoints: number[] = [];
  const pointerSize = 10;
  Iif (props.showArrow) {
    // Calculate the position for the arrow
    let point1 = points[2];
    let point2 = points[3];
    let midPoint = new SkPoint(0, 0);
    const tooSmall = checkTooSmall(
      isVertical,
      point1,
      point2,
      getUnitOpRotation(props.startOp, props.endOp),
      pointerSize,
    );
    if (tooSmall) {
      // arrow would be too large, draw it on the first line rather than the middle
      point1 = points[1];
      point2 = points[2];
      midPoint = isVertical
        ? new SkPoint(
            point2.x - pointerSize * -0 * Math.sign(point1.x - point2.x),
            (point1.y + point2.y) / 2,
          )
        : new SkPoint(
            (point1.x + point2.x) / 2,
            point2.y - pointerSize * -0 * Math.sign(point1.y - point2.y),
          );
    } else {
      midPoint = createEndpoint(
        isVertical,
        point1,
        point2,
        getUnitOpRotation(props.startOp, props.endOp),
        pointerSize,
      );
    }
    arrowPoints = [point1.x, point1.y, midPoint.x, midPoint.y];
  }
  return (
    <>
      <Line
        stroke={
          props.disabled ? "#aaaaaa"  : "#404040"
        }
        strokeWidth={2}
        points={points.flatMap((point) => [point.x, point.y])}
        tension={0}
      />
      {props.showArrow && (
        <Arrow
          points={arrowPoints}
          pointerLength={pointerSize}
          pointerWidth={pointerSize}
          fill={props.disabled ? "#aaaaaa" : "#404040"}
          stroke={props.disabled ? "#aaaaaa" : "#404040"}
          strokeWidth={2}
        />
      )}
    </>
  );
};
 
export function LogicConnectionGraphic({
  fromObjectId,
  toObjectId,
}: {
  fromObjectId: number;
  toObjectId: number;
}) {
  const simulationObjectsById = useFlowsheetObjectsIdMap();
  const objectsGraphicObjectsMap = useObjectsGraphicObjectsMap();
  const fromObject = simulationObjectsById.get(fromObjectId);
  const toObject = simulationObjectsById.get(toObjectId);
  if (!fromObject || !toObject) return null;
 
  const fromGraphicObject = objectsGraphicObjectsMap.get(fromObjectId);
  const toGraphicObject = objectsGraphicObjectsMap.get(toObjectId);
  if (!fromGraphicObject || !toGraphicObject) return null;
 
  const points: SkPoint[] = [];
  points.push(
    new SkPoint(
      fromGraphicObject.x! + fromGraphicObject.width! / 2,
      fromGraphicObject.y! + fromGraphicObject.height! / 2,
    ),
  );
  // intermediate point
  points.push(
    new SkPoint(
      toGraphicObject.x! + toGraphicObject.width! / 2,
      fromGraphicObject.y! + fromGraphicObject.height! / 2,
    ),
  );
  points.push(
    new SkPoint(
      toGraphicObject.x! + toGraphicObject.width! / 2,
      toGraphicObject.y! + toGraphicObject.height! / 2,
    ),
  );
 
  return (
    <Line
      stroke="#888"
      strokeWidth={2}
      dash={[10, 10]}
      points={points.flatMap((point) => [point.x, point.y])}
    />
  );
}