All files / src/parsers/utils areConnected.ts

92% Statements 23/25
71.42% Branches 10/14
100% Functions 1/1
94.73% Lines 18/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            576438x 576438x 576438x   576438x 576438x 576438x 331329x 331329x 331329x 246438x 246438x 246438x   201902x   201902x 201902x 112087x 102118x           102118x    
import { bondLength } from "./bondLength";
 
/*
 * return true if atom1 and atom2 are probably bonded to each other based on distance alone
 */
export function areConnected(atom1, atom2) {
  var maxsq = bondLength(atom1.elem) + bondLength(atom2.elem);
  maxsq += 0.25; // fudge factor, especially important for md frames, also see 1i3d
  maxsq *= maxsq;
 
  var xdiff = atom1.x - atom2.x;
  xdiff *= xdiff;
  if (xdiff > maxsq) return false;
  var ydiff = atom1.y - atom2.y;
  ydiff *= ydiff;
  if (ydiff > maxsq) return false;
  var zdiff = atom1.z - atom2.z;
  zdiff *= zdiff;
  if (zdiff > maxsq) return false;
 
  var distSquared = xdiff + ydiff + zdiff;
 
  Iif (isNaN(distSquared)) return false;
  else if (distSquared < 0.5) return false; // maybe duplicate position.
  else if (distSquared > maxsq) return false;
  else Iif (
    atom1.altLoc != atom2.altLoc &&
    atom1.altLoc != " " &&
    atom2.altLoc != " "
  )
    return false; // don't connect across alternate locations
  else return true;
}