_tryBond()

in packages/miew/src/chem/AromaticLoopsMarker.js [156:206]


  _tryBond(prevBond, currRight, currDir) {
    const bondsOrder = [];
    const bondsData = this._bondsData;
    const currLeft = _anotherAtom(prevBond, currRight);
    const currVec = currRight.position.clone().sub(currLeft.position);
    const startAtomRef = this._currStart;
    const self = this;
    const bondMarks = this._bondMarks;
    let checkAromatic = this._checkBond;
    bondMarks[prevBond._index] = true;
    checkAromatic = checkAromatic === undefined ? _isAromatic : checkAromatic;
    currRight.forEachBond((newBond) => {
      if (!checkAromatic(newBond)
        || newBond === prevBond
        || bondMarks[newBond._index]
        || self._haveSameCycle(bondsData, prevBond, newBond)) {
        return;
      }
      const anotherAtom = _anotherAtom(newBond, currRight);
      const anotherVec = anotherAtom.position.clone().sub(currRight.position);
      const val = anotherAtom === startAtomRef ? -2.0 : 1 - _cosBetween(currVec, anotherVec);
      const newDir = anotherVec.cross(currVec);
      if (!_coDirVectors(newDir, currDir)) {
        return;
      }
      let idx = 0;
      while (idx < bondsOrder.length && bondsOrder[idx].val < val) {
        ++idx;
      }
      bondsOrder.splice(idx, 0, { bond: newBond, val, dir: newDir });
    });

    for (let i = 0, n = bondsOrder.length; i < n; ++i) {
      const { bond } = bondsOrder[i];
      const newRight = bond._left === currRight ? bond._right : bond._left;
      if (newRight === startAtomRef) {
        ++this._currIdx;
        this._cycles.push([currRight]);
        bondMarks[prevBond._index] = false;
        return true;
      }
      if (this._tryBond(bond, newRight, bondsOrder[i].dir)) {
        _insertAscending(bondsData[bond._index], this._currIdx);
        this._cycles[this._currIdx].push(currRight);
        bondMarks[prevBond._index] = false;
        return true;
      }
    }
    bondMarks[prevBond._index] = false;
    return false;
  }