// $Id$
//
//  Copyright (C) 2004-2014 Greg Landrum and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
//
#include <list>
#include <RDGeneral/RDLog.h>
#include "MolFileStereochem.h"
#include <Geometry/point.h>
#include <boost/dynamic_bitset.hpp>
#include <algorithm>
#include "MolFileStereochem.h"
#include <RDGeneral/Ranking.h>

namespace RDKit {
typedef std::list<double> DOUBLE_LIST;

// ----------------------------------- -----------------------------------
// This algorithm is identical to that used in the CombiCode Mol file
//  parser (also developed by RD).
//
//
// SUMMARY:
//   Derive a chiral code for an atom that has a wedged (or dashed) bond
//   drawn to it.
//
// RETURNS:
//   The chiral type
//
// CAVEATS:
//   This is careful to ensure that the central atom has 4 neighbors and
//   only single bonds to it, but that's about it.
//
// NOTE: this isn't careful at all about checking to make sure that
// things actually *should* be chiral. e.g. if the file has a
// 3-coordinate N with a wedged bond, it will make some erroneous
// assumptions about the chirality.
//
// ----------------------------------- -----------------------------------

Atom::ChiralType FindAtomStereochemistry(const RWMol &mol, const Bond *bond,
                                         const Conformer *conf) {
  PRECONDITION(bond, "no bond");
  PRECONDITION(conf, "no conformer");
  Bond::BondDir bondDir = bond->getBondDir();
  PRECONDITION(bondDir == Bond::BEGINWEDGE || bondDir == Bond::BEGINDASH,
               "bad bond direction");

  // NOTE that according to the CT file spec, wedging assigns chirality
  // to the atom at the point of the wedge, (atom 1 in the bond).
  const Atom *atom = bond->getBeginAtom();
  PRECONDITION(atom, "no atom");

  // we can't do anything with atoms that have more than 4 neighbors:
  if (atom->getDegree() > 4) {
    return Atom::CHI_UNSPECIFIED;
  }
  const Atom *bondAtom = bond->getEndAtom();

  Atom::ChiralType res = Atom::CHI_UNSPECIFIED;

  INT_LIST neighborBondIndices;
  RDGeom::Point3D centerLoc, tmpPt;
  centerLoc = conf->getAtomPos(atom->getIdx());
  tmpPt = conf->getAtomPos(bondAtom->getIdx());
  centerLoc.z = 0.0;
  tmpPt.z = 0.0;

  RDGeom::Point3D refVect = centerLoc.directionVector(tmpPt);

  //----------------------------------------------------------
  //
  //  start by ensuring that all the bonds to neighboring atoms
  //  are single bonds and collecting a list of neighbor indices:
  //
  //----------------------------------------------------------
  bool hSeen = false;

  neighborBondIndices.push_back(bond->getIdx());
  if (bondAtom->getAtomicNum() == 1 && bondAtom->getIsotope() == 0)
    hSeen = true;

  bool allSingle = true;
  ROMol::OEDGE_ITER beg, end;
  boost::tie(beg, end) = mol.getAtomBonds(atom);
  while (beg != end) {
    Bond *nbrBond = mol[*beg].get();
    if (nbrBond->getBondType() != Bond::SINGLE) {
      allSingle = false;
      // break;
    }
    if (nbrBond != bond) {
      if ((nbrBond->getOtherAtom(atom)->getAtomicNum() == 1 &&
           nbrBond->getOtherAtom(atom)->getIsotope() == 0))
        hSeen = true;
      neighborBondIndices.push_back(nbrBond->getIdx());
    }
    ++beg;
  }
  int nNbrs = neighborBondIndices.size();

  //----------------------------------------------------------
  //
  //  Return now if there aren't at least 3 non-H bonds to the atom.
  //  (we can implicitly add a single H to 3 coordinate atoms, but
  //  we're horked otherwise).
  //
  //----------------------------------------------------------
  if (nNbrs < 3 || (hSeen && nNbrs < 4)) {
    return Atom::CHI_UNSPECIFIED;
  }

  //----------------------------------------------------------
  //
  //  Continue if there are all single bonds or if we're considering
  //  4-coordinate P or S
  //
  //----------------------------------------------------------
  if (allSingle || atom->getAtomicNum() == 15 || atom->getAtomicNum() == 16) {
    //------------------------------------------------------------
    //
    //  Here we need to figure out the rotation direction between
    //  the neighbor bonds and the wedged bond:
    //
    //------------------------------------------------------------
    bool isCCW = true;
    double angle0, angle1, angle2;
    const Bond *bond1, *bond2, *bond3;
    RDGeom::Point3D atomVect0, atomVect1, atomVect2;
    INT_LIST::const_iterator bondIter = neighborBondIndices.begin();
    ++bondIter;
    bond1 = mol.getBondWithIdx(*bondIter);
    int oaid = bond1->getOtherAtom(atom)->getIdx();
    tmpPt = conf->getAtomPos(oaid);
    tmpPt.z = 0;
    atomVect0 = centerLoc.directionVector(tmpPt);
    angle0 = refVect.signedAngleTo(atomVect0);
    if (angle0 < 0) angle0 += 2. * M_PI;

    ++bondIter;
    bond2 = mol.getBondWithIdx(*bondIter);
    oaid = bond2->getOtherAtom(atom)->getIdx();
    tmpPt = conf->getAtomPos(oaid);
    tmpPt.z = 0;
    atomVect1 = centerLoc.directionVector(tmpPt);
    angle1 = refVect.signedAngleTo(atomVect1);
    if (angle1 < 0) angle1 += 2. * M_PI;

    // We proceed differently for 3 and 4 coordinate atoms:
    double firstAngle, secondAngle;
    if (nNbrs == 4) {
      bool flipIt = false;
      // grab the angle to the last neighbor:
      ++bondIter;
      bond3 = mol.getBondWithIdx(*bondIter);
      oaid = bond3->getOtherAtom(atom)->getIdx();
      tmpPt = conf->getAtomPos(oaid);
      tmpPt.z = 0;
      atomVect2 = centerLoc.directionVector(tmpPt);
      angle2 = refVect.signedAngleTo(atomVect2);
      if (angle2 < 0) angle2 += 2. * M_PI;

      // find the lowest and second-lowest angle and keep track of
      // whether or not we have to do a non-cyclic permutation to
      // get there:
      if (angle0 < angle1) {
        if (angle1 < angle2) {
          // order is angle0 -> angle1 -> angle2
          firstAngle = angle0;
          secondAngle = angle1;
        } else if (angle0 < angle2) {
          // order is angle0 -> angle2 -> angle1
          firstAngle = angle0;
          secondAngle = angle2;
          flipIt = true;
        } else {
          // order is angle2 -> angle0 -> angle1
          firstAngle = angle2;
          secondAngle = angle0;
        }
      } else if (angle0 < angle2) {
        // order is angle1 -> angle0 -> angle2
        firstAngle = angle1;
        secondAngle = angle0;
        flipIt = true;
      } else {
        if (angle1 < angle2) {
          // order is angle1 -> angle2 -> angle0
          firstAngle = angle1;
          secondAngle = angle2;
        } else {
          // order is angle2 -> angle1 -> angle0
          firstAngle = angle2;
          secondAngle = angle1;
          flipIt = true;
        }
      }
      if (flipIt) {
        isCCW = !isCCW;
      }
    } else {
      // it's three coordinate.  Things are a bit different here
      // because we have to at least kind of figure out where the
      // hydrogen might be.

      // before getting started with that, use some of the inchi rules
      // for contradictory stereochemistry
      // (Table 10 in the InChi v1 technical manual)

      angle2 = atomVect0.signedAngleTo(atomVect1);
      if (angle2 < 0) angle2 += 2. * M_PI;

      //  this one is never allowed:
      //     0   2
      //      \ /
      //       C
      //       *
      //       1
      if (angle0 < (M_PI - 1e-3) && angle1 < (M_PI - 1e-3) &&
          angle2 < (M_PI - 1e-3)) {
        if ((bond1->getBondDir() != Bond::NONE &&
             bond1->getBeginAtomIdx() == bond->getBeginAtomIdx() &&
             (bond1->getBondDir() != bond->getBondDir() ||
              (bond2->getBondDir() != Bond::NONE &&
               bond2->getBeginAtomIdx() == bond->getBeginAtomIdx() &&
               bond2->getBondDir() != bond1->getBondDir()))) ||
            (bond2->getBondDir() != Bond::NONE &&
             bond2->getBeginAtomIdx() == bond->getBeginAtomIdx() &&
             bond2->getBondDir() != bond->getBondDir())) {
          BOOST_LOG(rdWarningLog)
              << "Warning: conflicting stereochemistry at atom "
              << bond->getBeginAtomIdx() << " ignored."
              << std::endl;  // by rule 1." << std::endl;
          return Atom::CHI_UNSPECIFIED;
        }
      }
      if (bond1->getBondDir() != Bond::NONE &&
          bond1->getBeginAtomIdx() == bond->getBeginAtomIdx()) {
        if (!(bond2->getBondDir() != Bond::NONE &&
              bond2->getBeginAtomIdx() == bond->getBeginAtomIdx())) {
          BOOST_LOG(rdWarningLog)
              << "Warning: conflicting stereochemistry at atom "
              << bond->getBeginAtomIdx() << " ignored."
              << std::endl;  // by rule 2a." << std::endl;
        }
        if (bond1->getBondDir() != bond->getBondDir()) {
          // bond1 has a spec and does not match the bond0 spec.
          // the only cases this is allowed are:
          //      1        0 1 2
          //      *         \*/
          //  0 - C - 2      C
          //    and
          //      1        2 1 0
          //      *         \*/
          //  2 - C - 0      C
          //
          if ((angle0 > M_PI && angle0 < angle1) ||
              (angle0 < M_PI && angle0 > angle1)) {
            BOOST_LOG(rdWarningLog)
                << "Warning: conflicting stereochemistry at atom "
                << bond->getBeginAtomIdx() << " ignored."
                << std::endl;  // by rule 2b." << std::endl;
            return Atom::CHI_UNSPECIFIED;
          }
        } else {
          // bond1 matches, what about bond2 ?
          if (bond2->getBondDir() != bond->getBondDir()) {
            // the only cases this is allowed are:
            //      2        0 2 1
            //      *         \*/
            //  0 - C - 1      C
            //    and
            //      2        1 2 0
            //      *         \*/
            //  1 - C - 0      C
            //
            if ((angle1 > M_PI && angle1 < angle0) ||
                (angle1 < M_PI && angle1 > angle0)) {
              BOOST_LOG(rdWarningLog)
                  << "Warning: conflicting stereochemistry at atom "
                  << bond->getBeginAtomIdx() << " ignored."
                  << std::endl;  // by rule 2c." << std::endl;
              return Atom::CHI_UNSPECIFIED;
            }
          }
        }
      } else if (bond2->getBondDir() != Bond::NONE &&
                 bond2->getBeginAtomIdx() == bond->getBeginAtomIdx() &&
                 bond2->getBondDir() != bond->getBondDir()) {
        // bond2 has a spec and does not match the bond0 spec, but bond1
        // is not set: this is never allowed.
        BOOST_LOG(rdWarningLog)
            << "Warning: conflicting stereochemistry at atom "
            << bond->getBeginAtomIdx() << " ignored."
            << std::endl;  // by rule 3." << std::endl;
        return Atom::CHI_UNSPECIFIED;
      }

      if (angle0 < angle1) {
        firstAngle = angle0;
        secondAngle = angle1;
        isCCW = true;
      } else {
        firstAngle = angle1;
        secondAngle = angle0;
        isCCW = false;
      }
      if (secondAngle - firstAngle >= (M_PI - 1e-4)) {
        // it's a situation like one of these:
        //
        //      0        1 0 2
        //      *         \*/
        //  1 - C - 2      C
        //
        // In each of these cases, the implicit H is between atoms 1
        // and 2, so we need to flip the rotation direction (go
        // around the back).
        isCCW = !isCCW;
      }
    }
    // reverse the rotation direction if the reference is wedged down:
    if (bondDir == Bond::BEGINDASH) {
      isCCW = !isCCW;
    }

    // ----------------
    //
    // We now have the rotation direction using mol-file order.
    // We need to convert that into the appropriate label for the
    // central atom
    //
    // ----------------
    int nSwaps = atom->getPerturbationOrder(neighborBondIndices);
    if (nSwaps % 2) isCCW = !isCCW;
    if (isCCW)
      res = Atom::CHI_TETRAHEDRAL_CCW;
    else
      res = Atom::CHI_TETRAHEDRAL_CW;
  }

  return res;
}

void WedgeMolBonds(ROMol &mol, const Conformer *conf) {
  PRECONDITION(conf, "no conformer");
  INT_MAP_INT wedgeBonds = pickBondsToWedge(mol);
  for (ROMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
       ++bondIt) {
    Bond *bond = *bondIt;
    if (bond->getBondType() == Bond::SINGLE) {
      Bond::BondDir dir = DetermineBondWedgeState(bond, wedgeBonds, conf);
      if (dir == Bond::BEGINWEDGE || dir == Bond::BEGINDASH) {
        bond->setBondDir(dir);
      }
    }
  }
}

INT_MAP_INT pickBondsToWedge(const ROMol &mol) {
  // we need ring information; make sure findSSSR has been called before
  // if not call now
  if (!mol.getRingInfo()->isInitialized()) {
    MolOps::findSSSR(mol);
  }

  static int noNbrs = 100;
  INT_VECT nChiralNbrs(mol.getNumAtoms(), noNbrs);

  // start by looking for bonds that are already wedged
  for (ROMol::ConstBondIterator cbi = mol.beginBonds(); cbi != mol.endBonds();
       ++cbi) {
    const Bond *bond = *cbi;
    if (bond->getBondDir() == Bond::BEGINWEDGE ||
        bond->getBondDir() == Bond::BEGINDASH ||
        bond->getBondDir() == Bond::UNKNOWN) {
      if (bond->getBeginAtom()->getChiralTag() == Atom::CHI_TETRAHEDRAL_CW ||
          bond->getBeginAtom()->getChiralTag() == Atom::CHI_TETRAHEDRAL_CCW)
        nChiralNbrs[bond->getBeginAtomIdx()] = noNbrs + 1;
      else if (bond->getEndAtom()->getChiralTag() == Atom::CHI_TETRAHEDRAL_CW ||
               bond->getEndAtom()->getChiralTag() == Atom::CHI_TETRAHEDRAL_CCW)
        nChiralNbrs[bond->getEndAtomIdx()] = noNbrs + 1;
    }
  }

  // now rank atoms by the number of chiral neighbors or Hs they have:
  bool chiNbrs = false;
  for (ROMol::ConstAtomIterator cai = mol.beginAtoms(); cai != mol.endAtoms();
       ++cai) {
    const Atom *at = *cai;
    if (nChiralNbrs[at->getIdx()] > noNbrs) {
      // std::cerr << " SKIPPING1: " << at->getIdx() << std::endl;
      continue;
    }
    Atom::ChiralType type = at->getChiralTag();
    if (type != Atom::CHI_TETRAHEDRAL_CW && type != Atom::CHI_TETRAHEDRAL_CCW)
      continue;
    nChiralNbrs[at->getIdx()] = 0;
    chiNbrs = true;
    ROMol::ADJ_ITER nbrIdx, endNbrs;
    boost::tie(nbrIdx, endNbrs) = mol.getAtomNeighbors(at);
    while (nbrIdx != endNbrs) {
      const ATOM_SPTR nat = mol[*nbrIdx];
      ++nbrIdx;
      if (nat->getAtomicNum() == 1) {
        // special case: it's an H... we weight these especially high:
        nChiralNbrs[at->getIdx()] -= 10;
        continue;
      }
      type = nat->getChiralTag();
      if (type != Atom::CHI_TETRAHEDRAL_CW && type != Atom::CHI_TETRAHEDRAL_CCW)
        continue;
      nChiralNbrs[at->getIdx()] -= 1;
    }
  }
  std::vector<unsigned int> indices(mol.getNumAtoms());
  for (unsigned int i = 0; i < mol.getNumAtoms(); ++i) indices[i] = i;
  if (chiNbrs) {
    std::sort(indices.begin(), indices.end(),
              Rankers::argless<INT_VECT>(nChiralNbrs));
  }
#if 0
  std::cerr << "  nbrs: ";
  std::copy(nChiralNbrs.begin(), nChiralNbrs.end(),
            std::ostream_iterator<int>(std::cerr, " "));
  std::cerr << std::endl;
  std::cerr << "  order: ";
  std::copy(indices.begin(), indices.end(),
            std::ostream_iterator<int>(std::cerr, " "));
  std::cerr << std::endl;
#endif
  // picks a bond for each atom that we will wedge when we write the mol file
  // here is what we are going to do
  // - at each chiral center look for a bond that is begins at the atom and
  //   is not yet picked to be wedged for a different chiral center, preferring
  //   bonds to Hs
  // - if we do not find a bond that begins at the chiral center - we will take
  //   the first bond that is not yet picked by any other chiral centers
  // we use the orders calculated above to determine which order to do the
  // wedging
  INT_MAP_INT res;
  BOOST_FOREACH (unsigned int idx, indices) {
    if (nChiralNbrs[idx] > noNbrs) {
      // std::cerr << " SKIPPING2: " << idx << std::endl;
      continue;  // already have a wedged bond here
    }
    const Atom *atom = mol.getAtomWithIdx(idx);
    Atom::ChiralType type = atom->getChiralTag();
    // the indices are ordered such that all chiral atoms come first. If
    // this has no chiral flag, we can stop the whole loop:
    if (type != Atom::CHI_TETRAHEDRAL_CW && type != Atom::CHI_TETRAHEDRAL_CCW)
      break;
    RDKit::ROMol::OBOND_ITER_PAIR atomBonds = mol.getAtomBonds(atom);
    std::vector<std::pair<int, int> > nbrScores;
    while (atomBonds.first != atomBonds.second) {
      const Bond *bond = mol[*atomBonds.first].get();
      ++atomBonds.first;

      // can only wedge single bonds:
      if (bond->getBondType() != Bond::SINGLE) continue;

      int bid = bond->getIdx();
      if (res.find(bid) == res.end()) {
        // very strong preference for Hs:
        if (bond->getOtherAtom(atom)->getAtomicNum() == 1) {
          nbrScores.push_back(std::make_pair(
              -1000000, bid));  // lower than anything else can be
          continue;
        }
        // prefer lower atomic numbers with lower degrees and no specified
        // chirality:
        const Atom *oatom = bond->getOtherAtom(atom);
        int nbrScore = oatom->getAtomicNum() + 10 * oatom->getDegree() +
                       100 * ((oatom->getChiralTag() != Atom::CHI_UNSPECIFIED));
        // prefer neighbors that are nonchiral or have as few chiral neighbors
        // as possible:
        int oIdx = oatom->getIdx();
        if (nChiralNbrs[oIdx] < noNbrs) {
          // the counts are negative, so we have to subtract them off
          nbrScore -= 10000 * nChiralNbrs[oIdx];
        }
        // prefer bonds to non-ring atoms:
        nbrScore += 1000 * mol.getRingInfo()->numAtomRings(oIdx);
        // prefer non-ring bonds;
        nbrScore += 1000 * mol.getRingInfo()->numBondRings(bid);
        // std::cerr << "    nrbScore: " << idx << " - " << oIdx << " : "
        //           << nbrScore << " nChiralNbrs: " << nChiralNbrs[oIdx]
        //           << std::endl;
        nbrScores.push_back(std::make_pair(nbrScore, bid));
      }
    }
    // There's still one situation where this whole thing can fail: an unlucky
    // situation where all neighbors of all neighbors of an atom are chiral and
    // that atom ends up being the last one picked for stereochem assignment.
    //
    // We'll catch that as an error here and hope that it's as unlikely to occur
    // as it seems like it is. (I'm going into this knowing that it's bound to
    // happen; I'll kick myself and do the hard solution at that point.)
    CHECK_INVARIANT(nbrScores.size(),
                    "no eligible neighbors for chiral center");
    std::sort(nbrScores.begin(), nbrScores.end(),
              Rankers::pairLess<int, int>());
    res[nbrScores[0].second] = idx;
  }
  return res;
}

//
// Determine bond wedge state
///
Bond::BondDir DetermineBondWedgeState(const Bond *bond,
                                      const INT_MAP_INT &wedgeBonds,
                                      const Conformer *conf) {
  PRECONDITION(bond, "no bond");
  PRECONDITION(bond->getBondType() == Bond::SINGLE,
               "bad bond order for wedging");
  const ROMol *mol = &(bond->getOwningMol());
  PRECONDITION(mol, "no mol");

  Bond::BondDir res = bond->getBondDir();
  if (!conf) {
    return res;
  }

  int bid = bond->getIdx();
  INT_MAP_INT_CI wbi = wedgeBonds.find(bid);
  if (wbi == wedgeBonds.end()) {
    return res;
  }

  unsigned int waid = wbi->second;

  Atom *atom, *bondAtom;  // = bond->getBeginAtom();
  if (bond->getBeginAtom()->getIdx() == waid) {
    atom = bond->getBeginAtom();
    bondAtom = bond->getEndAtom();
  } else {
    atom = bond->getEndAtom();
    bondAtom = bond->getBeginAtom();
  }

  Atom::ChiralType chiralType = atom->getChiralTag();
  CHECK_INVARIANT(chiralType == Atom::CHI_TETRAHEDRAL_CW ||
                      chiralType == Atom::CHI_TETRAHEDRAL_CCW,
                  "");

  // if we got this far, we really need to think about it:
  INT_LIST neighborBondIndices;
  DOUBLE_LIST neighborBondAngles;
  RDGeom::Point3D centerLoc, tmpPt;
  centerLoc = conf->getAtomPos(atom->getIdx());
  tmpPt = conf->getAtomPos(bondAtom->getIdx());
  centerLoc.z = 0.0;
  tmpPt.z = 0.0;
  RDGeom::Point3D refVect = centerLoc.directionVector(tmpPt);

  neighborBondIndices.push_back(bond->getIdx());
  neighborBondAngles.push_back(0.0);

  ROMol::OEDGE_ITER beg, end;
  boost::tie(beg, end) = mol->getAtomBonds(atom);
  while (beg != end) {
    Bond *nbrBond = (*mol)[*beg].get();
    Atom *otherAtom = nbrBond->getOtherAtom(atom);
    if (nbrBond != bond) {
      tmpPt = conf->getAtomPos(otherAtom->getIdx());
      tmpPt.z = 0.0;
      RDGeom::Point3D tmpVect = centerLoc.directionVector(tmpPt);
      double angle = refVect.signedAngleTo(tmpVect);
      if (angle < 0.0) angle += 2. * M_PI;
      INT_LIST::iterator nbrIt = neighborBondIndices.begin();
      DOUBLE_LIST::iterator angleIt = neighborBondAngles.begin();
      // find the location of this neighbor in our angle-sorted list
      // of neighbors:
      while (angleIt != neighborBondAngles.end() && angle > (*angleIt)) {
        ++angleIt;
        ++nbrIt;
      }
      neighborBondAngles.insert(angleIt, angle);
      neighborBondIndices.insert(nbrIt, nbrBond->getIdx());
    }
    ++beg;
  }

  // at this point, neighborBondIndices contains a list of bond
  // indices from the central atom.  They are arranged starting
  // at the reference bond in CCW order (based on the current
  // depiction).
  int nSwaps = atom->getPerturbationOrder(neighborBondIndices);

  // in the case of three-coordinated atoms we may have to worry about
  // the location of the implicit hydrogen - Issue 209
  // Check if we have one of these situation
  //
  //      0        1 0 2
  //      *         \*/
  //  1 - C - 2      C
  //
  // here the hydrogen will be between 1 and 2 and we need to add an additional
  // swap
  if (neighborBondAngles.size() == 3) {
    // three coordinated
    DOUBLE_LIST::iterator angleIt = neighborBondAngles.begin();
    ++angleIt;  // the first is the 0 (or reference bond - we will ignoire that
    double angle1 = (*angleIt);
    ++angleIt;
    double angle2 = (*angleIt);
    if (angle2 - angle1 >= (M_PI - 1e-4)) {
      // we have the above situation
      nSwaps++;
    }
  }

#ifdef VERBOSE_STEREOCHEM
  BOOST_LOG(rdDebugLog) << "--------- " << nSwaps << std::endl;
  std::copy(neighborBondIndices.begin(), neighborBondIndices.end(),
            std::ostream_iterator<int>(BOOST_LOG(rdDebugLog), " "));
  BOOST_LOG(rdDebugLog) << std::endl;
  std::copy(neighborBondAngles.begin(), neighborBondAngles.end(),
            std::ostream_iterator<double>(BOOST_LOG(rdDebugLog), " "));
  BOOST_LOG(rdDebugLog) << std::endl;
#endif
  if (chiralType == Atom::CHI_TETRAHEDRAL_CCW) {
    if (nSwaps % 2 == 1) {  // ^ reverse) {
      res = Bond::BEGINDASH;
    } else {
      res = Bond::BEGINWEDGE;
    }
  } else {
    if (nSwaps % 2 == 1) {  // ^ reverse) {
      res = Bond::BEGINWEDGE;
    } else {
      res = Bond::BEGINDASH;
    }
  }

  return res;
}

// handles stereochem markers set by the Mol file parser and
// converts them to the RD standard:
void DetectAtomStereoChemistry(RWMol &mol, const Conformer *conf) {
  PRECONDITION(conf, "no conformer");

  for (RWMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
       ++bondIt) {
    Bond *bond = *bondIt;
    if (bond->getBondDir() != Bond::UNKNOWN) {
      Bond::BondDir dir = bond->getBondDir();
      // the bond is marked as chiral:
      if (dir == Bond::BEGINWEDGE || dir == Bond::BEGINDASH) {
        Atom *atom = bond->getBeginAtom();
        if (atom->getImplicitValence() == -1) {
          atom->calcExplicitValence();
          atom->calcImplicitValence(false);
        }
        Atom::ChiralType code = FindAtomStereochemistry(mol, bond, conf);
        atom->setChiralTag(code);
        // within the RD representation, if a three-coordinate atom
        // is chiral and has an implicit H, that H needs to be made explicit:
        if (atom->getDegree() == 3 && !atom->getNumExplicitHs() &&
            atom->getNumImplicitHs() == 1) {
          atom->setNumExplicitHs(1);
          // recalculated number of implicit Hs:
          atom->updatePropertyCache();
        }
      }
    }
  }
}

void setBondDirRelativeToAtom(Bond *bond, Atom *atom, Bond::BondDir dir,
                              bool reverse, boost::dynamic_bitset<> &needsDir) {
  PRECONDITION(bond, "bad bond");
  PRECONDITION(atom, "bad atom");
  PRECONDITION(dir == Bond::ENDUPRIGHT || dir == Bond::ENDDOWNRIGHT, "bad dir");
  PRECONDITION(atom == bond->getBeginAtom() || atom == bond->getEndAtom(),
               "atom doesn't belong to bond");
  // std::cerr << "\t\t>sbdra :  bond " << bond->getIdx() << " atom "
  //           << atom->getIdx() << " dir : " << dir << " reverse: " << reverse
  //           << std::endl;
  Atom *oAtom;
  if (bond->getBeginAtom() != atom) {
    reverse = !reverse;
    oAtom = bond->getBeginAtom();
  } else {
    oAtom = bond->getEndAtom();
  }
  if (reverse) {
    dir = (dir == Bond::ENDUPRIGHT ? Bond::ENDDOWNRIGHT : Bond::ENDUPRIGHT);
  }
  // to ensure maximum compatibility, even when a bond has unknown stereo (set
  // explicitly and recorded in _UnknownStereo property), I will still let a
  // direction to be computed. You must check the _UnknownStereo property to
  // make sure whether this bond is explictly set to have no direction info.
  // This makes sense because the direction info are all derived from
  // coordinates, the _UnknownStereo property is like extra metadata to be
  // used with the direction info.
  bond->setBondDir(dir);
  // std::cerr<<"\t\t\t\t -> dir "<<dir<<std::endl;
  // check for other single bonds around the other atom who need their
  // direction set and set it as demanded by the direction of this one:
  ROMol::OEDGE_ITER beg, end;
  boost::tie(beg, end) = oAtom->getOwningMol().getAtomBonds(oAtom);
  while (beg != end) {
    Bond *nbrBond = oAtom->getOwningMol()[*beg].get();
    ++beg;
    if (nbrBond != bond && nbrBond->getBondType() != Bond::DOUBLE &&
        needsDir[nbrBond->getIdx()]) {
      Bond::BondDir nbrDir = Bond::NONE;
      if ((nbrBond->getBeginAtom() == oAtom && bond->getBeginAtom() == oAtom) ||
          (nbrBond->getEndAtom() == oAtom && bond->getEndAtom() == oAtom)) {
        // both bonds either start or end here; they *must* have different
        // directions:
        nbrDir =
            (dir == Bond::ENDUPRIGHT ? Bond::ENDDOWNRIGHT : Bond::ENDUPRIGHT);
      } else {
        // one starts here, the other ends here, they need to have the same
        // direction:
        nbrDir = dir;
      }
      nbrBond->setBondDir(nbrDir);
      needsDir[nbrBond->getIdx()] = 0;
      // std::cerr << "\t\t\t\t update bond " << nbrBond->getIdx() << " to dir "
      //           << nbrDir << std::endl;
    }
  }
}

bool isLinearArrangement(const RDGeom::Point3D &v1, const RDGeom::Point3D &v2,
                         double tol = 0.035) {  // tolerance of 2 degrees
  return fabs(v2.angleTo(v1) - M_PI) < tol;
}

void updateDoubleBondNeighbors(ROMol &mol, Bond *dblBond, const Conformer *conf,
                               boost::dynamic_bitset<> &needsDir,
                               std::vector<unsigned int> &singleBondCounts,
                               const VECT_INT_VECT &singleBondNbrs) {
  // we want to deal only with double bonds:
  PRECONDITION(dblBond, "bad bond");
  PRECONDITION(dblBond->getBondType() == Bond::DOUBLE, "not a double bond");
  PRECONDITION(conf, "no conformer");
  if (!needsDir[dblBond->getIdx()]) return;
  needsDir.set(dblBond->getIdx(), 0);
#if 0
  std::cerr << "**********************\n";
  std::cerr << "**********************\n";
  std::cerr << "**********************\n";
  std::cerr << "UDBN: " << dblBond->getIdx() << " "
            << dblBond->getBeginAtomIdx() << "=" << dblBond->getEndAtomIdx()
            << "\n";
#endif

  ROMol::OEDGE_ITER beg, end;
  std::vector<Bond *> followupBonds;

  Bond *bond1 = 0, *obond1 = 0;
  bool squiggleBondSeen = false;
  boost::tie(beg, end) = mol.getAtomBonds(dblBond->getBeginAtom());
  while (beg != end) {
    Bond *tBond = mol[*beg].get();
    if (tBond->getBondType() == Bond::SINGLE ||
        tBond->getBondType() == Bond::AROMATIC) {
      // prefer bonds that already have their directionality set
      // or that are adjacent to more double bonds:
      if (!bond1) {
        bond1 = tBond;
      } else if (needsDir[tBond->getIdx()]) {
        if (singleBondCounts[tBond->getIdx()] >
            singleBondCounts[bond1->getIdx()]) {
          obond1 = bond1;
          bond1 = tBond;
        } else {
          obond1 = tBond;
        }
      } else {
        obond1 = bond1;
        bond1 = tBond;
      }
    }
    if (tBond->getBondType() == Bond::SINGLE &&
        tBond->getBondDir() == Bond::UNKNOWN) {
      squiggleBondSeen = true;
      break;
    }

    ++beg;
  }
  // Don't do any direction setting if we've seen a squiggle bond, but do mark
  // the double bond as a crossed bond and return
  if (!bond1 || squiggleBondSeen) {
    dblBond->setBondDir(Bond::EITHERDOUBLE);
    return;
  }

  Bond *bond2 = 0, *obond2 = 0;
  boost::tie(beg, end) = mol.getAtomBonds(dblBond->getEndAtom());
  while (beg != end) {
    Bond *tBond = mol[*beg].get();
    if (tBond->getBondType() == Bond::SINGLE ||
        tBond->getBondType() == Bond::AROMATIC) {
      if (!bond2) {
        bond2 = tBond;
      } else if (needsDir[tBond->getIdx()]) {
        if (singleBondCounts[tBond->getIdx()] >
            singleBondCounts[bond2->getIdx()]) {
          obond2 = bond2;
          bond2 = tBond;
        } else {
          obond2 = tBond;
        }
      } else {
        // we already had a bond2 and we don't need to set the direction
        // on the new one, so swap.
        obond2 = bond2;
        bond2 = tBond;
      }
    }
    if (tBond->getBondType() == Bond::SINGLE &&
        tBond->getBondDir() == Bond::UNKNOWN) {
      squiggleBondSeen = true;
      break;
    }

    ++beg;
  }
  // Don't do any direction setting if we've seen a squiggle bond, but do mark
  // the double bond as a crossed bond and return
  if (!bond2 || squiggleBondSeen) {
    dblBond->setBondDir(Bond::EITHERDOUBLE);
    return;
  }

  CHECK_INVARIANT(bond1 && bond2, "no bonds found");
  RDGeom::Point3D beginP = conf->getAtomPos(dblBond->getBeginAtomIdx());
  RDGeom::Point3D endP = conf->getAtomPos(dblBond->getEndAtomIdx());
  RDGeom::Point3D bond1P =
      conf->getAtomPos(bond1->getOtherAtomIdx(dblBond->getBeginAtomIdx()));
  RDGeom::Point3D bond2P =
      conf->getAtomPos(bond2->getOtherAtomIdx(dblBond->getEndAtomIdx()));
  // check for a linear arrangement of atoms on either end:
  bool linear = false;
  RDGeom::Point3D p1;
  RDGeom::Point3D p2;
  p1 = bond1P - beginP;
  p2 = endP - beginP;
  if (isLinearArrangement(p1, p2)) {
    if (!obond1) {
      linear = true;
    } else {
      // one of the bonds was linear; what about the other one?
      Bond *tBond = bond1;
      bond1 = obond1;
      obond1 = tBond;
      bond1P =
          conf->getAtomPos(bond1->getOtherAtomIdx(dblBond->getBeginAtomIdx()));
      p1 = bond1P - beginP;
      if (isLinearArrangement(p1, p2)) {
        linear = true;
      }
    }
  }
  if (!linear) {
    p1 = bond2P - endP;
    p2 = beginP - endP;
    if (isLinearArrangement(p1, p2)) {
      if (!obond2) {
        linear = true;
      } else {
        Bond *tBond = bond2;
        bond2 = obond2;
        obond2 = tBond;
        bond2P =
            conf->getAtomPos(bond2->getOtherAtomIdx(dblBond->getEndAtomIdx()));
        p1 = bond2P - beginP;
        if (isLinearArrangement(p1, p2)) {
          linear = true;
        }
      }
    }
  }
  if (linear) {
    dblBond->setBondDir(Bond::EITHERDOUBLE);
    return;
  }

  double ang = RDGeom::computeDihedralAngle(bond1P, beginP, endP, bond2P);
  bool sameTorsionDir;
  if (ang < M_PI / 2) {
    sameTorsionDir = false;
  } else {
    sameTorsionDir = true;
  }
  // std::cerr << "   angle: " << ang << " sameTorsionDir: " << sameTorsionDir
  // << "\n";

  /*
     Time for some clarificatory text, because this gets really
     confusing really fast.

     The dihedral angle analysis above is based on viewing things
     with an atom order as follows:

     1
      \
       2 = 3
            \
             4

     so dihedrals > 90 correspond to sameDir=true

     however, the stereochemistry representation is
     based on something more like this:

     2
      \
       1 = 3
            \
             4
     (i.e. we consider the direction-setting single bonds to be
      starting at the double-bonded atom)

  */
  bool reverseBondDir = sameTorsionDir;

  Atom *atom1 = dblBond->getBeginAtom(), *atom2 = dblBond->getEndAtom();
  if (needsDir[bond1->getIdx()]) {
    BOOST_FOREACH (int bidx, singleBondNbrs[bond1->getIdx()]) {
      // std::cerr << "       neighbor from: " << bond1->getIdx() << " " << bidx
      //           << ": " << needsDir[bidx] << std::endl;
      if (needsDir[bidx]) followupBonds.push_back(mol.getBondWithIdx(bidx));
    }
  }
  if (needsDir[bond2->getIdx()]) {
    BOOST_FOREACH (int bidx, singleBondNbrs[bond2->getIdx()]) {
      // std::cerr << "       neighbor from: " << bond2->getIdx() << " " << bidx
      //           << ": " << needsDir[bidx] << std::endl;
      if (needsDir[bidx]) followupBonds.push_back(mol.getBondWithIdx(bidx));
    }
  }
  if (!needsDir[bond1->getIdx()]) {
    if (!needsDir[bond2->getIdx()]) {
      // check that we agree
    } else {
      if (bond1->getBeginAtom() != atom1) {
        reverseBondDir = !reverseBondDir;
      }
      setBondDirRelativeToAtom(bond2, atom2, bond1->getBondDir(),
                               reverseBondDir, needsDir);
    }
  } else if (!needsDir[bond2->getIdx()]) {
    if (bond2->getBeginAtom() != atom2) {
      reverseBondDir = !reverseBondDir;
    }
    setBondDirRelativeToAtom(bond1, atom1, bond2->getBondDir(), reverseBondDir,
                             needsDir);
  } else {
    setBondDirRelativeToAtom(bond1, atom1, Bond::ENDDOWNRIGHT, false, needsDir);
    setBondDirRelativeToAtom(bond2, atom2, Bond::ENDDOWNRIGHT, reverseBondDir,
                             needsDir);
  }
  needsDir[bond1->getIdx()] = 0;
  needsDir[bond2->getIdx()] = 0;
  if (obond1 && needsDir[obond1->getIdx()]) {
    setBondDirRelativeToAtom(obond1, atom1, bond1->getBondDir(),
                             bond1->getBeginAtom() == atom1, needsDir);
    needsDir[obond1->getIdx()] = 0;
  }
  if (obond2 && needsDir[obond2->getIdx()]) {
    setBondDirRelativeToAtom(obond2, atom2, bond2->getBondDir(),
                             bond2->getBeginAtom() == atom2, needsDir);
    needsDir[obond2->getIdx()] = 0;
  }
#if 0
  std::cerr << "  1:" << bond1->getIdx() << " ";
  if (obond1)
    std::cerr << obond1->getIdx() << std::endl;
  else
    std::cerr << "N/A" << std::endl;
  std::cerr << "  2:" << bond2->getIdx() << " ";
  if (obond2)
    std::cerr << obond2->getIdx() << std::endl;
  else
    std::cerr << "N/A" << std::endl;
  std::cerr << "**********************\n";
  std::cerr << "**********************\n";
  std::cerr << "**********************\n";
#endif
  BOOST_FOREACH (Bond *oDblBond, followupBonds) {
    // std::cerr << "FOLLOWUP: " << oDblBond->getIdx() << " "
    //           << needsDir[oDblBond->getIdx()] << std::endl;
    updateDoubleBondNeighbors(mol, oDblBond, conf, needsDir, singleBondCounts,
                              singleBondNbrs);
  }
}

void ClearSingleBondDirFlags(ROMol &mol) {
  for (RWMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
       ++bondIt) {
    if ((*bondIt)->getBondType() == Bond::SINGLE) {
      if ((*bondIt)->getBondDir() == Bond::UNKNOWN)
        (*bondIt)->setProp(common_properties::_UnknownStereo, 1);
      (*bondIt)->setBondDir(Bond::NONE);
    }
  }
}

void DetectBondStereoChemistry(ROMol &mol, const Conformer *conf) {
  PRECONDITION(conf, "no conformer");
#if 0
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
    std::cerr << "DBSN: "<<"\n";
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
    std::cerr << ">>>>>>>>>>>>>>>>>>>>>*\n";
#endif
  // used to store the number of single bonds a given
  // single bond is adjacent to
  std::vector<unsigned int> singleBondCounts(mol.getNumBonds(), 0);
  std::vector<Bond *> bondsInPlay;
  // keeps track of which single bonds are adjacent to each double bond:
  VECT_INT_VECT dblBondNbrs(mol.getNumBonds());
  // keeps track of which double bonds are adjacent to each single bond:
  VECT_INT_VECT singleBondNbrs(mol.getNumBonds());
  // keeps track of which single bonds need a dir set and which double bonds
  // need to have their neighbors' dirs set
  boost::dynamic_bitset<> needsDir(mol.getNumBonds());

  // find double bonds that should be considered for
  // stereochemistry
  // NOTE that we are explicitly excluding double bonds in rings
  // with this test.
  bool resetRings = false;
  if (!mol.getRingInfo()->isInitialized()) {
    resetRings = true;
    MolOps::fastFindRings(mol);
  }

  for (RWMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
       ++bondIt) {
    if ((*bondIt)->getBondType() == Bond::DOUBLE &&
        (*bondIt)->getStereo() != Bond::STEREOANY &&
        (*bondIt)->getBondDir() != Bond::EITHERDOUBLE &&
        (*bondIt)->getBeginAtom()->getDegree() > 1 &&
        (*bondIt)->getEndAtom()->getDegree() > 1 &&
        !(mol.getRingInfo()->numBondRings((*bondIt)->getIdx()))) {
      const Atom *a1 = (*bondIt)->getBeginAtom();
      const Atom *a2 = (*bondIt)->getEndAtom();

      ROMol::OEDGE_ITER beg, end;
      boost::tie(beg, end) = mol.getAtomBonds(a1);
      while (beg != end) {
        const Bond *nbrBond = mol[*beg].get();
        if (nbrBond->getBondType() == Bond::SINGLE ||
            nbrBond->getBondType() == Bond::AROMATIC) {
          singleBondCounts[nbrBond->getIdx()] += 1;
          needsDir[nbrBond->getIdx()] = 1;
          needsDir[(*bondIt)->getIdx()] = 1;
          dblBondNbrs[(*bondIt)->getIdx()].push_back(nbrBond->getIdx());
          // the search may seem inefficient, but these vectors are going to be
          // at most 2 long (with very few exceptions). It's just not worth
          // using a different data structure
          if (std::find(singleBondNbrs[nbrBond->getIdx()].begin(),
                        singleBondNbrs[nbrBond->getIdx()].end(),
                        (*bondIt)->getIdx()) ==
              singleBondNbrs[nbrBond->getIdx()].end()) {
            singleBondNbrs[nbrBond->getIdx()].push_back((*bondIt)->getIdx());
          }
        }
        ++beg;
      }
      boost::tie(beg, end) = mol.getAtomBonds(a2);
      while (beg != end) {
        const Bond *nbrBond = mol[*beg].get();
        if (nbrBond->getBondType() == Bond::SINGLE ||
            nbrBond->getBondType() == Bond::AROMATIC) {
          singleBondCounts[nbrBond->getIdx()] += 1;
          needsDir[nbrBond->getIdx()] = 1;
          needsDir[(*bondIt)->getIdx()] = 1;
          dblBondNbrs[(*bondIt)->getIdx()].push_back(nbrBond->getIdx());

          // the search may seem inefficient, but these vectors are going to be
          // at most 2 long (with very few exceptions). It's just not worth
          // using a different data structure
          if (std::find(singleBondNbrs[nbrBond->getIdx()].begin(),
                        singleBondNbrs[nbrBond->getIdx()].end(),
                        (*bondIt)->getIdx()) ==
              singleBondNbrs[nbrBond->getIdx()].end()) {
            singleBondNbrs[nbrBond->getIdx()].push_back((*bondIt)->getIdx());
          }
        }
        ++beg;
      }
      bondsInPlay.push_back(*bondIt);
    }
  }

  if (!bondsInPlay.size()) {
    if (resetRings) mol.getRingInfo()->reset();
    return;
  }

  // order the double bonds based on the singleBondCounts of their neighbors:
  std::vector<std::pair<unsigned int, Bond *> > orderedBondsInPlay;
  for (unsigned int i = 0; i < bondsInPlay.size(); ++i) {
    Bond *dblBond = bondsInPlay[i];
    unsigned int countHere =
        std::accumulate(dblBondNbrs[dblBond->getIdx()].begin(),
                        dblBondNbrs[dblBond->getIdx()].end(), 0);
    // and favor double bonds that are *not* in rings. The combination of using
    // the sum
    // above (instead of the max) and this ring-membershipt test seem to fix
    // sf.net issue 3009836
    if (!(mol.getRingInfo()->numBondRings(dblBond->getIdx()))) countHere *= 10;
    orderedBondsInPlay.push_back(std::make_pair(countHere, dblBond));
  }
  std::sort(orderedBondsInPlay.begin(), orderedBondsInPlay.end());

  // oof, now loop over the double bonds in that order and
  // update their neighbor directionalities:
  std::vector<std::pair<unsigned int, Bond *> >::reverse_iterator pairIter;
  for (pairIter = orderedBondsInPlay.rbegin();
       pairIter != orderedBondsInPlay.rend(); ++pairIter) {
    updateDoubleBondNeighbors(mol, pairIter->second, conf, needsDir,
                              singleBondCounts, singleBondNbrs);
  }
  if (resetRings) mol.getRingInfo()->reset();
}
}


/**************************************************************************\
|
|    Copyright (C) 2009 Marc Stevens
|
|    This program is free software: you can redistribute it and/or modify
|    it under the terms of the GNU General Public License as published by
|    the Free Software Foundation, either version 3 of the License, or
|    (at your option) any later version.
|
|    This program is distributed in the hope that it will be useful,
|    but WITHOUT ANY WARRANTY; without even the implied warranty of
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|    GNU General Public License for more details.
|
|    You should have received a copy of the GNU General Public License
|    along with this program.  If not, see <http://www.gnu.org/licenses/>.
|
\**************************************************************************/

#include <vector>
#include <algorithm>
#include <stdexcept>
#include <map>
#include <utility>
#include <algorithm>
#include <string>
#include <iostream>
#include <time.h>

#include <boost/lexical_cast.hpp>

#include <hashclash/saveload_bz2.hpp>
#include <hashclash/sha1detail.hpp>
#include <hashclash/rng.hpp>
#include <hashclash/sha1differentialpath.hpp>
#include <hashclash/progress_display.hpp>

#include "main.hpp"

unsigned int lowpathindex;

void random_permutation(vector<sha1differentialpath>& paths, path_container& container)
{
	// use a pseudo-random permutation fixed by inputs
	seed(paths.size()); addseed(container.t); addseed(container.modn);
	for (unsigned i = 0; i < paths.size(); ++i)
	{
		unsigned k = xrng64() % paths.size();
		paths[i].swap(paths[k]);
	}
	addseed(time(NULL));
}

inline std::string pathsstring(const std::string& basepath, unsigned modi, unsigned modn)
{
	return workdir +  "/"  + basepath
		+ "_" + boost::lexical_cast<std::string>(modi)
		+ "of" + boost::lexical_cast<std::string>(modn);
}

unsigned lower_eqbits(const sha1differentialpath& _Left, const sha1differentialpath& _Right)
{
	unsigned t = _Left.tend() - 1;
	uint32 LdFt = 0 - _Left[t].getsdr().rotate_left(5).adddiff() - _Left[t-4].getsdr().rotate_left(30).adddiff();
	uint32 LdFtp1 = 0 - _Left[t-3].getsdr().rotate_left(30).adddiff();
	uint32 LdFtp2 = 0 - _Left[t-2].getsdr().rotate_left(30).adddiff();
	uint32 LdFtp3 = 0 - _Left[t-1].getsdr().rotate_left(30).adddiff();
	uint32 LdFtp4 = 0 - _Left[t].getsdr().rotate_left(30).adddiff();
	uint32 LdQtm1 = _Left[t-1].diff();
	uint32 LdQt = _Left[t].diff();
	uint32 RdFt = 0 - _Right[t].getsdr().rotate_left(5).adddiff() - _Right[t-4].getsdr().rotate_left(30).adddiff();
	uint32 RdFtp1 = 0 - _Right[t-3].getsdr().rotate_left(30).adddiff();
	uint32 RdFtp2 = 0 - _Right[t-2].getsdr().rotate_left(30).adddiff();
	uint32 RdFtp3 = 0 - _Right[t-1].getsdr().rotate_left(30).adddiff();
	uint32 RdFtp4 = 0 - _Right[t].getsdr().rotate_left(30).adddiff();
	uint32 RdQtm1 = _Right[t-1].diff();
	uint32 RdQt = _Right[t].diff();
	unsigned b = 0;
	while (b < 32+8) {
		if (b < 32) {
			if (_Left(t-3,(b+2)&31)!=_Right(t-3,(b+2)&31)) break;
			if (_Left(t-2,(b+2)&31)!=_Right(t-2,(b+2)&31)) break;
			if ((LdFt^RdFt)&(1<<b)) break;
			if ((LdQtm1^RdQtm1)&(1<<b)) break;
		}
		if (b >= 2 && b-2 < 32) {
			if ((LdFtp1^RdFtp1)&(1<<(b-2))) break;
			if ((LdQt^RdQt)&(1<<(b-2))) break;
		}
		if (b >= 4 && b-4 < 32 && ((LdFtp2^RdFtp2)&(1<<(b-4)))) break;
		if (b >= 6 && b-6 < 32 && ((LdFtp3^RdFtp3)&(1<<(b-6)))) break;
		if (b >= 8 && b-8 < 32 && ((LdFtp4^RdFtp4)&(1<<(b-8)))) break;
		++b;
	}
	return b;
}

progress_display* dostep_progress = 0;
unsigned dostep_index = 0;
struct dostep_thread {
	dostep_thread(vector<sha1differentialpath>& inlow, vector<sha1differentialpath>& inhigh, path_container& out, bool randomize)
		: pathsinlow(inlow), pathsinhigh(inhigh), container(out), random(randomize), worker(0)
	{}
	~dostep_thread()
	{ if (worker) delete worker; }
	vector<sha1differentialpath>& pathsinlow;
	vector<sha1differentialpath>& pathsinhigh;
	path_container& container;
	sha1_connect_thread* worker;
	sha1differentialpath prelowpath;
	vector<sha1differentialpath> lowpaths;
	bool random;
	void operator()() {
		try {
			worker = new sha1_connect_thread;
			while (true) {
				mut.lock();
				if (dostep_index >= pathsinlow.size())
				{
					mut.unlock();
					break;
				}
				// obtain a stripe of consecutive lowpaths
				static const unsigned stripe = (1<<5); // must be a power of 2
				unsigned count = stripe;
				if (dostep_index+count >= pathsinlow.size())
					count = pathsinlow.size() - dostep_index;
				dostep_index += count;
				(*dostep_progress) += count;
				lowpaths.clear();
				if (random) {
					unsigned kr;
					do {
						kr = (xrng128()%pathsinlow.size());
						kr &= ~(stripe-1); // set kr to beginning of stripe (set necessary low bits to zero)
					} while (pathsinlow[kr].path.size() == 0);
					for (unsigned i = 0; i < count; ++i)
					{
						lowpaths.push_back(pathsinlow[kr+i]);
						pathsinlow[kr+i].clear();
						worker->lowpathindex = kr;
					}
				} else {
					unsigned kr = dostep_index - count;
					for (unsigned i = 0; i < count; ++i)
					{
						lowpaths.push_back(pathsinlow[kr+i]);
						pathsinlow[kr+i].clear();
						worker->lowpathindex = kr;
					}
				}
				mut.unlock();

				for (unsigned i = 0; i < lowpaths.size(); ++i,++worker->lowpathindex)
				{
					if (prelowpath.path.size())
						worker->sha1_connect(lowpaths[i], pathsinhigh, container, lower_eqbits(lowpaths[i],prelowpath));
					else
						worker->sha1_connect(lowpaths[i], pathsinhigh, container, 0);
					prelowpath.swap(lowpaths[i]);
				}
			}
		}
		catch (std::exception & e) { cerr << "Worker thread: caught exception:" << endl << e.what() << endl; }
		catch (...) { cerr << "Worker thread: caught unknown exception:" << endl; }
	}
};
void dostep_threaded(vector<sha1differentialpath>& inlow, vector<sha1differentialpath>& inhigh, path_container& out, bool randomize = true)
{
	mut.lock();
	dostep_index = 0;
	cout << "Starting threads..." << flush;
	boost::thread_group mythreads;
	for (unsigned i = 0; i < out.threads; ++i) {
		cout << i << flush;
		mythreads.create_thread(*new dostep_thread(inlow,inhigh,out,randomize));
		cout << i << flush;
	}
	cout << endl;
	std::string tstring = "t=" + boost::lexical_cast<std::string>(out.t) + ": ";
	if (tstring.size() == 5) tstring += " ";
	dostep_progress = new progress_display(inlow.size(), true, cout, tstring, "      ", "      ");
	mut.unlock();
	mythreads.join_all();
	delete dostep_progress;
}

void load_upper_paths(vector<sha1differentialpath>& pathsinhigh, path_container& container)
{
        const unsigned t = container.t;
        const unsigned modn = container.modn;
        const unsigned modi = container.modi;
        const unsigned mode = container.splitmode;
        // 0 = split upperpath sorted, 1 = split upperpath random, 2 = split lowerpath sorted, 3 = split lowerpath random
        try {
                cout << "Loading " << container.inputfilehigh << "..." << flush;
                if (modn > 1 && mode <= 1) {
                        vector<sha1differentialpath> tmp;
                        load_bz2(tmp, binary_archive, container.inputfilehigh);
                        if (mode == 1) {
                                random_permutation(tmp, container);
                                pathsinhigh.reserve((tmp.size()+modn)/modn);
                                for (unsigned j = modi; j < tmp.size(); j += modn) {
                                        pathsinhigh.push_back(tmp[j]);
                                }
                                sort(pathsinhigh.begin(), pathsinhigh.end(), diffpathupper_less());
                        } else {
                                sort(tmp.begin(), tmp.end(), diffpathupper_less());
                                unsigned blockstart = (modi*tmp.size())/modn;
                                unsigned blockend = ((modi+1)*tmp.size())/modn;
                                pathsinhigh.reserve(blockend - blockstart);
                                for (unsigned k = blockstart; k < blockend; ++k)
                                        pathsinhigh.push_back(tmp[k]);
                        }
                } else {
                        load_bz2(pathsinhigh, binary_archive, container.inputfilehigh);
                        sort(pathsinhigh.begin(), pathsinhigh.end(), diffpathupper_less());
                }
                cout << "done: " << pathsinhigh.size() << "." << endl;
        } catch(exception& e) {
                cout << "failed.\n(" << e.what() << ")" << endl;
        } catch(...) {
                cout << "failed." << endl;
        }
}

void load_lower_paths(vector<sha1differentialpath>& pathsinlow, path_container& container)
{
        const unsigned t = container.t;
        const unsigned modn = container.modn;
        const unsigned modi = container.modi;
        const unsigned mode = container.splitmode;
        try {
                cout << "Loading " << container.inputfilelow << "..." << flush;
                if (modn > 1 && mode >= 2) {
                        vector< sha1differentialpath > pathstmp2;
                        load_bz2(pathstmp2, binary_archive, container.inputfilelow);
                        if (mode == 3) {
                                random_permutation(pathstmp2, container);
                                pathsinlow.reserve((pathstmp2.size()/modn)+1);
                                for (unsigned j = modi; j < pathstmp2.size(); j += modn)
                                        pathsinlow.push_back(pathstmp2[j]);
                                sort(pathsinlow.begin(), pathsinlow.end(), diffpathlower_less());
                        } else {
                                sort(pathstmp2.begin(), pathstmp2.end(), diffpathlower_less());
                                unsigned blockstart = (modi*pathstmp2.size())/modn;
                                unsigned blockend = ((modi+1)*pathstmp2.size())/modn;
                                pathsinlow.reserve(blockend - blockstart);
                                for (unsigned k = blockstart; k < blockend; ++k)
                                        pathsinlow.push_back(pathstmp2[k]);
                        }
                } else {
                        load_bz2(pathsinlow, binary_archive, container.inputfilelow);
                        sort(pathsinlow.begin(), pathsinlow.end(), diffpathlower_less());
                }
                cout << "done: " << pathsinlow.size() << "." << endl;
        } catch(exception& e) {
                cout << "failed.\n(" << e.what() << ")" << endl;
        } catch(...) {
                cout << "failed." << endl;
        }
}

void dostep(path_container& container)
{
        const unsigned t = container.t;
        const unsigned modn = container.modn;
        const unsigned modi = container.modi;
        const unsigned mode = container.splitmode;
        // 0 = split upperpath sorted, 1 = split upperpath random, 2 = split lowerpath sorted, 3 = split lowerpath random
        vector< sha1differentialpath > pathsinhigh, pathsinlow, pathsout;
        if (mode <= 1) {
                load_upper_paths(pathsinhigh, container);
                load_lower_paths(pathsinlow, container);
        } else {
                load_lower_paths(pathsinlow, container);
                load_upper_paths(pathsinhigh, container);
        }
        if (container.showinputpaths) {
                for (unsigned r = 0; r < pathsinlow.size(); ++r)
                        show_path(pathsinlow[r]);
                for (unsigned r = 0; r < pathsinhigh.size(); ++r)
                        show_path(pathsinhigh[r]);
        } else {
                if (pathsinlow.size())
                        show_path(pathsinlow[0]);
                if (pathsinhigh.size())
                        show_path(pathsinhigh[0]);
        }
        unsigned badcount = 0;
        for (unsigned i = 0; i < pathsinlow.size(); ++i)
                if (!test_path(pathsinlow[i]))
                        ++badcount;
        if (badcount) {
                cout << "Bad lowerpaths: " << badcount << " out of " << pathsinlow.size() << endl;
                for (unsigned i = 0; i < pathsinlow.size(); ++i)
                        if (!test_path(pathsinlow[i]) || pathsinlow[i].path.size()==0) {
                                show_path(pathsinlow[i]);
                                break;
                        }
        }
        badcount = 0;
        for (unsigned i = 0; i < pathsinhigh.size(); ++i)
                if (!test_path(pathsinhigh[i]))
                        ++badcount;
        if (badcount) {
                cout << "Bad upperpaths: " << badcount << " out of " << pathsinhigh.size() << endl;
                for (unsigned i = 0; i < pathsinhigh.size(); ++i)
                        if (!test_path(pathsinhigh[i]) || pathsinhigh[i].path.size()==0) {
                                show_path(pathsinhigh[i]);
                                break;
                        }
        }

	vector< sha1differentialpath > pathsredo, pathsredohigh, pathsredolow;
	if (container.inputfileredo.size()) {
		cout << "Loading redo inputs: " << flush;
		try {
			load_bz2(pathsredo, binary_archive, container.inputfileredo);
			cout << "done: " << pathsredo.size() << "." << endl;
		} catch (exception& e) {
			cout << "failed.\n(" << e.what() << ")" << endl;
			pathsredo.clear();
		} catch (...) {
			cout << "failed." << endl;
			pathsredo.clear();
		}
	}
	if (pathsredo.size() > 0) {
		cout << "Filtering..." << endl;
		progress_display pd(pathsinlow.size()+pathsinhigh.size());
		int lowtend = pathsinlow[0].tend()-1, hightbegin = pathsinhigh[0].tbegin();
		vector<uint32> redolowtend(pathsredo.size()), redohightbegin(pathsredo.size());
		if (lowtend+1 != hightbegin) {
			cout << "lowtend+1 != hightend: " << lowtend << " " << hightbegin << endl << endl;
		}
		for (unsigned i = 0; i < pathsredo.size(); ++i) {
			redolowtend[i] = pathsredo[i][lowtend].diff();
			redohightbegin[i] = pathsredo[i][hightbegin].diff();
		}
		for (unsigned k = 0; k < pathsinlow.size(); ++k,++pd) {
			bool redolow = false;
			uint32 tmp = pathsinlow[k][ pathsinlow[k].tend()-1 ].diff();
			for (unsigned j = 0; j < pathsredo.size(); ++j) {
				if (pathsinlow[k].tend()-1 != lowtend)
					throw std::runtime_error("tend()-1 != lowtend");
				if (tmp != redolowtend[j])
					continue;
//				if (pathsredo[j].tbegin() > pathsinlow[k].tbegin() || pathsredo[j].tend() < pathsinlow[k].tend())
//					continue;
				bool ok = true;
/*				for (int t = pathsinlow[k].tend()-2; t >= pathsinlow[k].tbegin(); --t)
					if (pathsinlow[k][t].diff() != pathsredo[j][t].diff()) {
						ok = false;
						break;
					}
*/				if (ok) {
					redolow = true;
					break;
				}
			}
			if (redolow) {
				pathsredolow.push_back(pathsinlow[k]);
			}
		}
		for (unsigned k = 0; k < pathsinhigh.size(); ++k,++pd) {
			bool redohigh = false;
			uint32 tmp = pathsinhigh[k][ pathsinhigh[k].tbegin() ].diff();
			for (unsigned j = 0; j < pathsredo.size(); ++j) {
				if (pathsinhigh[k].tbegin() != hightbegin)
					throw std::runtime_error("pathsinhigh[k].tbegin() != hightbegin");
				if (tmp != redohightbegin[j])
					continue;
//				if (pathsredo[j].tbegin() > pathsinhigh[k].tbegin() || pathsredo[j].tend() < pathsinhigh[k].tend())
//					throw std::runtime_error("blaaat");
//					continue;
				bool ok = true;
/*				for (int t = pathsinhigh[k].tbegin()+1; t < pathsinhigh[k].tend(); ++t)
					if (pathsinhigh[k][t].diff() != pathsredo[j][t].diff()) {
						ok = false;
						break;
					}
*/				if (ok) {
					redohigh = true;
					break;
				}
			}
			if (redohigh) {
				pathsredohigh.push_back(pathsinhigh[k]);
			}
		}
		cout << "# Lowerpaths to redo: " << pathsredolow.size() << endl;
		cout << "# Upperpaths to redo: " << pathsredohigh.size() << endl;
		cout << "Processing selected lowerpaths..." << endl;
		dostep_threaded(pathsredolow, pathsinhigh, container, false);
		container.save_bestpaths();

		cout << "Processing selected upperpaths..." << endl;
		dostep_threaded(pathsinlow, pathsredohigh, container, false);
		container.save_bestpaths();
		return;
	}
	if (container.loworder == 0)
		dostep_threaded(pathsinlow, pathsinhigh, container, true);
	else
	{
		if (container.loworder == 2)
			sort(pathsinlow.begin(), pathsinlow.end(), diffpathlower_less_weight());
		dostep_threaded(pathsinlow, pathsinhigh, container, false);
	}
	container.save_bestpaths();
}


/* main.cpp -- part of the fractal flames implementation
 *
 * Copyright (C) 2015 Alrik Firl
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#include "fflame_generator.hpp"
#include "fractal_flame.hpp"

#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <opencv2/opencv.hpp>

#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

namespace bfs = boost::filesystem;

//--------------------------------------------------------------------------------------------------------
// for debugging with gdb
double get_mat_pixel(cv::Mat_<cv::Vec<double, 3>> &image, int row, int col,
                     int channel) {
  auto px = image(row, col);
  return px(channel);
}
//--------------------------------------------------------------------------------------------------------

// generates num_frames frames in interpolating between the lhs and rhs frames
template <typename pixel_t>
int interpolate_frames(const cv::Mat_<pixel_t> &lhs_img,
                       const cv::Mat_<pixel_t> &rhs_img, const int num_frames,
                       const bfs::path out_filebasepath, int frame_idx) {
  auto output_fpath = out_filebasepath;
  output_fpath /= std::to_string(frame_idx++) + ".png";
  const std::string out_filepath = output_fpath.native();

  {
    // NOTE: we'll 'lose' the last frame generated in this manner?
    auto output_keyframe_fpath = out_filebasepath;
    output_keyframe_fpath /=
        "baseframe_" + std::to_string(frame_idx++) + ".png";
    const std::string out_keyframe_filepath = output_keyframe_fpath.native();
    cv::imwrite(out_keyframe_filepath, lhs_img);
  }

  // std::cout << "saving " << out_filepath << std::endl;
  cv::imwrite(out_filepath, lhs_img);

  cv::Mat_<pixel_t> pixel_diffimg =
      (rhs_img - lhs_img) / static_cast<double>(num_frames);
  // note: this is just a reference to lhs_img, not a clone
  cv::Mat_<pixel_t> working_frame = lhs_img;
  for (int i = 0; i < num_frames; ++i) {
    working_frame += pixel_diffimg;
    output_fpath = out_filebasepath;
    output_fpath /= std::to_string(frame_idx++) + ".png";
    const std::string out_filepath = output_fpath.native();
    cv::imwrite(out_filepath, working_frame);
    // std::cout << "saving " << out_filepath << std::endl;
  }

  // at this point, working_frame should be 1 step off from rhs_img
  return frame_idx;
}

template <typename pixel_t>
int interpolate_frames(const FFlames::flame_frame<pixel_t> &lhs_frame,
                       const FFlames::flame_frame<pixel_t> &rhs_frame,
                       const int num_frames, const bfs::path out_filebasepath,
                       int frame_idx) {
  cv::Mat_<pixel_t> lhs_img(lhs_frame.rows, lhs_frame.cols, lhs_frame.data);
  cv::Mat_<pixel_t> rhs_img(rhs_frame.rows, rhs_frame.cols, rhs_frame.data);
  return interpolate_frames(lhs_img, rhs_img, num_frames, out_filebasepath,
                            frame_idx);
}

template <class framequeue_t, template <class> class frame_t, typename pixel_t>
void run_frame_generation(framequeue_t &bg_framequeue,
                          const bfs::path output_dir, const int num_images,
                          bool do_interpolation) {
  int output_index = 0;
  std::unique_ptr<frame_t<pixel_t>> prev_image;
  // frame_t<pixel_t> prev_image;
  for (int frame_idx = 0; frame_idx < num_images; ++frame_idx) {

    bool got_frame = false;
    std::unique_ptr<frame_t<pixel_t>> fflame_frame;
    while (!got_frame) {
      fflame_frame = bg_framequeue.pop(got_frame);
    }

    if (got_frame && fflame_frame) {
      // save the frames out, in whatever configuration is specified
      if (do_interpolation) {
        if (frame_idx > 0) {
          output_index =
              interpolate_frames(*(prev_image.get()), *(fflame_frame.get()), 30,
                                 output_dir, output_index);
        }
        prev_image.swap(fflame_frame);
      } else {
        /*
        //Q: how do we normalize? We could get the maximum R, G, and B hcannels?
        pixel_t min_pxval;
        pixel_t max_pxval;
        std::for_each(fflame_frame->data, fflame_frame->data +
        fflame_frame->rows * fflame_frame->cols,
                    [&min_pxval, &max_pxval] (const pixel_t& pxval) {
                        for (int k = 0; k < pixel_t::channels; k++) {
                            if(pxval[k] > max_pxval[k]) {
                                max_pxval[k] = pxval[k];
                            }
                            if(pxval[k] < min_pxval[k]) {
                                min_pxval[k] = pxval[k];
                            }
                        }
                    });

        std::cout << "Per-channel MIN: {" << min_pxval[0] << ", " <<
        min_pxval[1] << ", " << min_pxval[2] << "}" << std::endl; std::cout <<
        "Per-channel MAX: {" << max_pxval[0] << ", " << max_pxval[1] << ", " <<
        max_pxval[2] << "}" << std::endl; pixel_t dynamic_channel_range =
        max_pxval - min_pxval; cv::divide(255, dynamic_channel_range,
        dynamic_channel_range); std::for_each(fflame_frame->data,
        fflame_frame->data + fflame_frame->rows * fflame_frame->cols,
                [min_pxval, dynamic_channel_range](pixel_t& pxval) {
                    pxval = (pxval - min_pxval).mul(dynamic_channel_range);
                });
        */
        cv::Mat_<pixel_t> keyframe_img(fflame_frame->rows, fflame_frame->cols,
                                       fflame_frame->data);

        // NOTE: we'll 'lose' the last frame generated in this manner?
        auto output_keyframe_fpath = output_dir;
        output_keyframe_fpath /= "baseframe_" + std::to_string(frame_idx);
        const std::string out_keyframe_filepath =
            output_keyframe_fpath.native();

        // write out the yml file...
        cv::FileStorage keyframe_ymlfile(out_keyframe_filepath + ".yml",
                                         cv::FileStorage::WRITE);
        keyframe_ymlfile << "baseframe_" + std::to_string(frame_idx)
                         << keyframe_img;

        cv::Mat logged_image;
        keyframe_img.convertTo(logged_image, CV_8UC3);
        //... and the image file
        cv::imwrite(out_keyframe_filepath + ".png", logged_image);
      }
    }
  }
}

template <template <class> class frame_t, typename pixel_t, typename data_t>
void generate_fractal_flames(const bfs::path output_dir,
                             const int render_height, const int render_width,
                             const int num_working_variants,
                             const int num_images,
                             const bool do_interpolation = true) {
  using flame_gen_t = FFlames::fflame_generator<frame_t, data_t, pixel_t>;
  const int num_generator_threads = 4;
  auto bg_generator = std::unique_ptr<flame_gen_t>(
      new flame_gen_t(render_height, render_width, num_working_variants,
                      num_generator_threads));

  // pass in a queue to hold the finished flame frames
  // auto bg_framequeue = std::unique_ptr<EventQueue<frame_t<pixel_t>>>(new
  // EventQueue<frame_t<pixel_t>>(num_images));
  using bg_queue_t =
      typename flame_gen_t::template ts_queue_t<frame_t<pixel_t>>;
  bg_queue_t bg_framequeue;
  auto bg_framequeue_p = &bg_framequeue;
  bg_generator->register_framequeue(bg_framequeue_p);

  bg_generator->start_generation();
  run_frame_generation<bg_queue_t, frame_t, pixel_t>(
      bg_framequeue, output_dir, num_images, do_interpolation);
  bg_generator->stop_generation();
}

template <template <class> class frame_t, typename pixel_t, typename data_t>
void generate_fractal_flames(const bfs::path output_dir,
                             const int render_height, const int render_width,
                             std::vector<std::string> &&manual_variants,
                             const int num_images,
                             const bool do_interpolation = true) {
  using flame_gen_t = FFlames::fflame_generator<frame_t, data_t, pixel_t>;
  const int num_generator_threads = 4;
  auto bg_generator = std::unique_ptr<flame_gen_t>(
      new flame_gen_t(render_height, render_width, std::move(manual_variants),
                      num_generator_threads));

  // pass in a queue to hold the finished flame frames
  // auto bg_framequeue = std::unique_ptr<EventQueue<frame_t<pixel_t>>>(new
  // EventQueue<frame_t<pixel_t>>(num_images));
  using bg_queue_t =
      typename flame_gen_t::template ts_queue_t<frame_t<pixel_t>>;
  bg_queue_t bg_framequeue;
  auto bg_framequeue_p = &bg_framequeue;
  bg_generator->register_framequeue(bg_framequeue_p);

  bg_generator->start_generation();
  run_frame_generation<bg_queue_t, frame_t, pixel_t>(
      bg_framequeue, output_dir, num_images, do_interpolation);
  bg_generator->stop_generation();
}

//------------------------------------------------------------------------------------------------------
template <typename pixel_t>
using frame_t = FFlames::flame_frame<pixel_t>; // cv::Mat_<pixel_t>;

int main(int argc, char *argv[]) {
  std::vector<std::string> manual_variant_list;

  namespace bpo = boost::program_options;
  bpo::options_description bpo_desc("fractal flames options");
  bpo_desc.add_options()("help,h", "Print help message")(
      "output-path,o", bpo::value<std::string>(), "output flame-image path")(
      "num-variants,n", bpo::value<int>(),
      "number of working variants")("total-frames,t", bpo::value<int>(),
                                    "total number of keyframes to generate")(
      "variant-list,v",
      bpo::value<std::vector<std::string>>(&manual_variant_list)->multitoken(),
      "list of variants to use")("do-interpolation,i", bpo::value<int>(),
                                 "flag for writing out interpolated frames "
                                 "between keyframes (1: yes, 0: no)");

  bpo::variables_map vm;
  bpo::store(bpo::command_line_parser(argc, argv).options(bpo_desc).run(), vm);
  bpo::notify(vm);

  std::string output_path;
  int num_working_variants, num_images, do_interpolation;

  if (vm.count("help")) {
    std::cout << bpo_desc << std::endl;
    return 0;
  }

  if (vm.count("output-path")) {
    output_path = vm["output-path"].as<std::string>();
  } else {
    std::cout << bpo_desc << std::endl;
    return 0;
  }

  if (vm.count("num-variants")) {
    num_working_variants = vm["num-variants"].as<int>();
  } else {
    std::cout << bpo_desc << std::endl;
    return 0;
  }

  if (vm.count("total-frames")) {
    num_images = vm["total-frames"].as<int>();
  } else {
    std::cout << bpo_desc << std::endl;
    return 0;
  }

  if (vm.count("do-interpolation")) {
    do_interpolation = vm["do-interpolation"].as<int>();
  } else {
    std::cout << bpo_desc << std::endl;
    return 0;
  }

  using data_t = double;
  using pixel_t = cv::Vec<data_t, 3>;
  const int render_height = 1024;
  const int render_width = 1024;

  const bool use_manual_variants = manual_variant_list.size() > 0;
  if (use_manual_variants) {
    // invoke via something like
    //./fflame_gen -o ff_frames/ff_v13 -n 10 -t 100 -i 0 -v fisheye -v eyefish
    //-v swirl -v this -v is -v a -v drill
    //
    //... where the -v are the manually provided variants, given in sequential
    //order next we just have to figure out how to pass these in...
    std::cout << "Given Variant (" << manual_variant_list.size()
              << ") List: " << std::endl;
    num_working_variants = manual_variant_list.size();
    for (auto variant_id : manual_variant_list) {
      // search the list of variant names to make sure the provided ones are
      // valid
      std::string manual_var = variant_id;
      std::transform(manual_var.begin(), manual_var.end(), manual_var.begin(),
                     [](unsigned char c) { return std::tolower(c); });
      auto var_it = std::find(
          FFlames::affine_fcns::variant_list<data_t>::variant_names.begin(),
          FFlames::affine_fcns::variant_list<data_t>::variant_names.end(), manual_var);
      if (var_it != FFlames::affine_fcns::variant_list<data_t>::variant_names.end()) {
        std::cout << variant_id << std::endl;
      } else {
        std::cout << "ERROR -- variant " << manual_var
                  << " is not valid. Valid variants list: " << std::endl;
        for (auto var_name : FFlames::affine_fcns::variant_list<data_t>::variant_names) {
          std::cout << var_name << std::endl;
        }
        return 0;
      }
    }
  }

  // check the output directory path (if it doesn't exist, create it)
  bfs::path output_dir(output_path);
  if (!(bfs::exists(output_dir) && bfs::is_directory(output_dir))) {
    if (!boost::filesystem::create_directory(output_path)) {
      std::cout << "Invalid output file directory -- " << output_path
                << std::endl;
      throw std::invalid_argument("Invalid output file directory " +
                                  output_path);
    }
  }

  bool do_interpolation_flag = do_interpolation != 0;
  if (use_manual_variants) {
    generate_fractal_flames<frame_t, pixel_t, data_t>(
        output_dir, render_height, render_width, std::move(manual_variant_list),
        num_images, do_interpolation_flag);
  } else {
    generate_fractal_flames<frame_t, pixel_t, data_t>(
        output_dir, render_height, render_width, num_working_variants,
        num_images, do_interpolation_flag);
  }

  return 0;
}


/*
==============================================================================
KratosStructuralApplication
A library based on:
Kratos
A General Purpose Software for Multi-Physics Finite Element Analysis
Version 1.0 (Released on march 05, 2007).

Copyright 2007
Pooyan Dadvand, Riccardo Rossi, Janosch Stascheit, Felix Nagel
pooyan@cimne.upc.edu
rrossi@cimne.upc.edu
janosch.stascheit@rub.de
nagel@sd.rub.de
- CIMNE (International Center for Numerical Methods in Engineering),
Gran Capita' s/n, 08034 Barcelona, Spain
- Ruhr-University Bochum, Institute for Structural Mechanics, Germany

Permission is hereby granted, free  of charge, to any person obtaining
a  copy  of this  software  and  associated  documentation files  (the
"Software"), to  deal in  the Software without  restriction, including
without limitation  the rights to  use, copy, modify,  merge, publish,
distribute,  sublicense and/or  sell copies  of the  Software,  and to
permit persons to whom the Software  is furnished to do so, subject to
the following condition:

Distribution of this code for  any  commercial purpose  is permissible
ONLY BY DIRECT ARRANGEMENT WITH THE COPYRIGHT OWNERS.

The  above  copyright  notice  and  this permission  notice  shall  be
included in all copies or substantial portions of the Software.

THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT  SHALL THE AUTHORS OR COPYRIGHT HOLDERS  BE LIABLE FOR ANY
CLAIM, DAMAGES OR  OTHER LIABILITY, WHETHER IN AN  ACTION OF CONTRACT,
TORT  OR OTHERWISE, ARISING  FROM, OUT  OF OR  IN CONNECTION  WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

==============================================================================
*/

//
//   Project Name:        Kratos
//   Last Modified by:    $Author: anonymous $
//   Date:                $Date: 2008-10-23 14:27:01 $
//   Revision:            $Revision: 1.20 $
//
//

#if !defined(KRATOS_ADD_CUSTOM_UTILITIES_TO_PYTHON_H_INCLUDED )
#define  KRATOS_ADD_CUSTOM_UTILITIES_TO_PYTHON_H_INCLUDED

// System includes
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

// External includes
#include "boost/smart_ptr.hpp"

// Project includes
#include "custom_python/add_custom_utilities_to_python.h"
#include "includes/define.h"
#include "custom_utilities/deactivation_utility.h"
#include "custom_utilities/variable_transfer_utility.h"

#ifdef _OPENMP
#include "custom_utilities/parallel_variable_transfer_utility.h"
#endif

#include "spaces/ublas_space.h"
#include "linear_solvers/linear_solver.h"
#include "custom_utilities/contact_utility.h"
#include "custom_utilities/volume_utility.h"
#include "custom_utilities/restart_utility.h"
#include "custom_utilities/node_snapping_utility.h"
#include "custom_elements/rigid_body_3D.h"
#include "custom_utilities/output_utility.h"
#include "custom_utilities/dof_utility.h"
#include "custom_utilities/smoothing_utility.h"

//#include "custom_utilities/detect_elements_utility.h"
#include "custom_utilities/intra_fracture_triangle_utility.h"
#include "custom_utilities/inter_fracture_triangle_utility.h"
#include "custom_utilities/inter_fracture_tetrahedra_utility.h"
//#include "custom_utilities/mark_element_for_refinement.h"
#include "custom_utilities/disconnect_utility.h"

namespace Kratos
{

namespace Python
{

using namespace boost::python;

void AddNewRigidBody3D( ModelPart& structural_model_part,
                        ModelPart& skin_model_part,
                        Variable<double>& rSelectionVariable,
                        double selection_value,
                        Node<3>::Pointer CenterNode,
                        Element::PropertiesType::Pointer pProperties,
                        double nodal_mass,
                        Matrix& Inertia
                      )
{
    Geometry<Node<3> >::Pointer skin_nodes_geometry( new Geometry<Node<3> > ); ;

    //selecting the nodes in the model part having rSelectionVariable==selection_value

    for ( ModelPart::NodesContainerType::iterator it = skin_model_part.NodesBegin(); it != skin_model_part.NodesEnd(); it++ )
    {
        if ( it->FastGetSolutionStepValue( rSelectionVariable ) == selection_value )
            skin_nodes_geometry->push_back( *( it.base() ) );
    }

    //creating a geometry containing the center node
    Geometry<Node<3> >::Pointer center_node_geometry( new Geometry<Node<3> > ) ;

    center_node_geometry->push_back( Node<3>::Pointer( CenterNode ) );

    unsigned int last_id = 1;

    if ( structural_model_part.Elements().size() != 0 )
        last_id = ( structural_model_part.ElementsEnd() - 1 )->Id() + 1;

    array_1d<double, 3> zero = ZeroVector( 3 );

    Element::Pointer new_el = RigidBody3D::Pointer( new  RigidBody3D( last_id,
                              center_node_geometry,
                              pProperties,
                              skin_nodes_geometry,
                              nodal_mass,
                              Inertia, zero, zero ) );

    structural_model_part.Elements().push_back(
        new_el
    );
}

void AddNewRigidBodyAndSpring3D( ModelPart& structural_model_part,
                                 ModelPart& skin_model_part,
                                 Variable<double>& rSelectionVariable,
                                 double selection_value,
                                 Node<3>::Pointer CenterNode,
                                 Element::PropertiesType::Pointer pProperties,
                                 double nodal_mass,
                                 Matrix& Inertia,
                                 array_1d<double, 3>& translational_stiffness,
                                 array_1d<double, 3>& rotational_stiffness
                               )
{
    Geometry<Node<3> >::Pointer skin_nodes_geometry( new Geometry<Node<3> > ); ;

    //selecting the nodes in the model part having rSelectionVariable==selection_value

    for ( ModelPart::NodesContainerType::iterator it = skin_model_part.NodesBegin(); it != skin_model_part.NodesEnd(); it++ )
    {
        if ( it->FastGetSolutionStepValue( rSelectionVariable ) == selection_value )
            skin_nodes_geometry->push_back( *( it.base() ) );
    }

    //creating a geometry containing the center node
    Geometry<Node<3> >::Pointer center_node_geometry( new Geometry<Node<3> > ) ;

    center_node_geometry->push_back( Node<3>::Pointer( CenterNode ) );

    unsigned int last_id = 1;

    if ( structural_model_part.Elements().size() != 0 )
        last_id = ( structural_model_part.ElementsEnd() - 1 )->Id() + 1;

    array_1d<double, 3> zero = ZeroVector( 3 );

    Element::Pointer new_el = RigidBody3D::Pointer( new  RigidBody3D( last_id,
                              center_node_geometry,
                              pProperties,
                              skin_nodes_geometry,
                              nodal_mass,
                              Inertia,
                              translational_stiffness,
                              rotational_stiffness ) );

    structural_model_part.Elements().push_back(
        new_el
    );
}

void DoubleTransferVariablesToNodes(VariableTransferUtility& dummy,
        ModelPart& model_part, Variable<double>& rThisVariable)
{
    dummy.TransferVariablesToNodes(model_part, rThisVariable);
}

void VectorTransferVariablesToNodes(VariableTransferUtility& dummy,
        ModelPart& model_part, Variable<Vector>& rThisVariable)
{
    dummy.TransferVariablesToNodes(model_part, rThisVariable);
}

void DoubleTransferVariablesToGaussPoints(VariableTransferUtility& dummy,
        ModelPart& source_model_part, ModelPart& target_model_part, Variable<double>& rThisVariable)
{
    dummy.TransferVariablesToGaussPoints(source_model_part, target_model_part, rThisVariable);
}

void VectorTransferVariablesToGaussPoints(VariableTransferUtility& dummy,
        ModelPart& source_model_part, ModelPart& target_model_part, Variable<Vector>& rThisVariable)
{
    dummy.TransferVariablesToGaussPoints(source_model_part, target_model_part, rThisVariable);
}

void  AddCustomUtilitiesToPython()
{
    class_<DeactivationUtility, boost::noncopyable >
    ( "DeactivationUtility", init<>() )
    .def( init<int>() )
    .def( "Deactivate", &DeactivationUtility::Deactivate )
    .def( "Reactivate", &DeactivationUtility::Reactivate )
    .def( "ReactivateStressFree", &DeactivationUtility::ReactivateStressFree )
    .def( "ReactivateAll", &DeactivationUtility::ReactivateAll )
    .def( "Initialize", &DeactivationUtility::Initialize )
    .def( "GetName", &DeactivationUtility::GetName<Element> )
    .def( "GetName", &DeactivationUtility::GetName<Condition> )
    ;

    class_<VariableTransferUtility, boost::noncopyable >
    ( "VariableTransferUtility", init<>() )
    .def(init<VariableTransferUtility::LinearSolverType::Pointer>())
    .def( "TransferNodalVariables", &VariableTransferUtility::TransferNodalVariables )
    .def( "TransferConstitutiveLawVariables", &VariableTransferUtility::TransferConstitutiveLawVariables )
    .def( "TransferInSituStress", &VariableTransferUtility::TransferInSituStress )
    .def( "TransferPrestress", &VariableTransferUtility::TransferPrestress )
    .def( "TransferPrestressIdentically", &VariableTransferUtility::TransferPrestressIdentically )
    .def( "TransferSpecificVariable", &VariableTransferUtility::TransferSpecificVariable )
    .def( "InitializeModelPart", &VariableTransferUtility::InitializeModelPart )
    .def("TransferVariablesToNodes", &DoubleTransferVariablesToNodes)
    .def("TransferVariablesToNodes", &VectorTransferVariablesToNodes)
    .def("TransferVariablesToGaussPoints", &DoubleTransferVariablesToGaussPoints)
    .def("TransferVariablesToGaussPoints", &VectorTransferVariablesToGaussPoints)
    ;

#ifdef _OPENMP
    class_<ParallelVariableTransferUtility, boost::noncopyable >
    ( "ParallelVariableTransferUtility",
      init<>() )
    .def( "TransferNodalVariables", &ParallelVariableTransferUtility::TransferNodalVariables )
    .def( "TransferConstitutiveLawVariables", &ParallelVariableTransferUtility::TransferConstitutiveLawVariables )
    .def( "TransferInSituStress", &ParallelVariableTransferUtility::TransferInSituStress )
    .def( "InitializeModelPart", &ParallelVariableTransferUtility::InitializeModelPart )
    ;
#endif

    class_<ContactUtility, boost::noncopyable >
    ( "ContactUtility",
      init<int>() )
    .def( "SetUpContactConditions", &ContactUtility::SetUpContactConditions )
    .def( "SetUpContactConditionsLagrangeTying", &ContactUtility::SetUpContactConditionsLagrangeTying )
    .def( "Update", &ContactUtility::Update )
    .def( "IsConverged", &ContactUtility::IsConverged )
    .def( "Clean", &ContactUtility::Clean )
    .def( "CleanLagrangeTying", &ContactUtility::CleanLagrangeTying )
    ;
// VM
    class_<VolumeUtility, boost::noncopyable >
    ( "VolumeUtility",
      init<int>() )
    .def( "Calculate_this_Volume", &VolumeUtility::CalculateVolume ) // VM
    ;
//VM

    class_<RestartUtility, boost::noncopyable >
    ( "RestartUtility",
      init< std::string const& >() )
    .def( "ChangeFileName", &RestartUtility::ChangeFileName )
    .def( "StoreNodalVariables", &RestartUtility::StoreNodalVariables )
    .def( "WriteNodalVariables", &RestartUtility::WriteNodalVariables )
    .def( "StoreInSituStress", &RestartUtility::StoreInSituStress )
    .def( "WriteConstitutiveLawVariables", &RestartUtility::WriteConstitutiveLawVariables )
    .def( "StoreConstitutiveLawVariables", &RestartUtility::StoreConstitutiveLawVariables )
    .def( "WriteInSituStress", &RestartUtility::WriteInSituStress )
    ;

    class_<NodeSnappingUtility, boost::noncopyable >
    ( "NodeSnappingUtility",
      init<>() )
    .def( "MoveNode", &NodeSnappingUtility::MoveNode )
    .def( "AdjustNodes", &NodeSnappingUtility::AdjustNodes )
    .def( "AdjustToCircle", &NodeSnappingUtility::AdjustToCircle )
    .def( "AdjustToCylinder", &NodeSnappingUtility::AdjustToCylinder )
    .def( "AdjustToClosedCylinder", &NodeSnappingUtility::AdjustToClosedCylinder )
    .def( "IdentifyInsideElements", &NodeSnappingUtility::IdentifyInsideElements )
    .def( "SetInsituStress", &NodeSnappingUtility::SetInsituStress )
    .def( "ExtractCapNodes", &NodeSnappingUtility::ExtractCapNodes )
    .def( "TestElements", &NodeSnappingUtility::TestElements )
    ;

    class_<OutputUtility, boost::noncopyable >
    ( "OutputUtility",
      init<>() )
    .def( "GetStrain", &OutputUtility::GetStrain )
    .def( "GetStress", &OutputUtility::GetStress )
    .def( "GetInternalVariables", &OutputUtility::GetInternalVariables )
    ;

    def( "AddNewRigidBody3D", AddNewRigidBody3D );
    def( "AddNewRigidBodyAndSpring3D", AddNewRigidBodyAndSpring3D );
    ;

    /*
                class_<Detect_Elements_And_Nodes, boost::noncopyable >
                        ("DetectElementsAndNodes", init<ModelPart&, int >() )
          .def("DetectNode",              &Detect_Elements_And_Nodes::Detect_Node_To_Be_Splitted)
                        .def("DetectElements",          &Detect_Elements_And_Nodes::Detect_Elements_To_Be_Splitted)
                        .def("CalculateMapFailure",     &Detect_Elements_And_Nodes::Calculate_Map_Failure)
                        .def("Finalize",                &Detect_Elements_And_Nodes::Finalize)
                        ;
    */
    class_<Smoothing_Utility, boost::noncopyable >
    ( "SmoothingUtility", init<ModelPart&, int >() )
    .def( "WeightedRecoveryGradients", &Smoothing_Utility::WeightedRecoveryGradients<double> )
    .def( "WeightedRecoveryGradients", &Smoothing_Utility::WeightedRecoveryGradients<Matrix> ) // for matrices
    .def( "InterpolatedRecoveryGradients", &Smoothing_Utility::InterpolatedRecoveryGradients<Matrix> )
    .def( "SettingNodalValues", &Smoothing_Utility::SettingNodalValues )
    .def( "RecomputeValuesForNewMesh", &Smoothing_Utility::Recompute_Values_For_New_Mesh )
    .def( "Finalize", &Smoothing_Utility::Finalize )
    .def( "SettingNodalValues", &Smoothing_Utility::SettingNodalValues )
    ;

    class_<Disconnect_Triangle_Utilities, boost::noncopyable >
    ( "DisconnectTriangle", init<ModelPart&>() )
    .def( "DisconnectElements", &Disconnect_Triangle_Utilities::Disconnect_Elements )
    ;

    class_<Intra_Fracture_Triangle, boost::noncopyable >
    ( "IntraFractureTriangle", init<ModelPart&, int >() )
    .def( "DetectAndSplitElements",              &Intra_Fracture_Triangle::Detect_And_Split_Elements )
    ;

    class_<Inter_Fracture_Triangle, boost::noncopyable >
    ( "InterFractureTriangle", init<ModelPart&, int >() )
    .def( "DetectAndSplitElementsHeuristicFormula", &Inter_Fracture_Triangle::Detect_And_Split_Elements_Heuristic_Formula )
    .def( "DetectAndSplitElements",              &Inter_Fracture_Triangle::Detect_And_Split_Elements )
    .def( "Finalize",                            &Inter_Fracture_Triangle::Finalize )
    ;

    class_<Inter_Fracture_Tetrahedra, boost::noncopyable >
    ( "InterFractureTetrahedra", init<ModelPart&, int >() )
    .def( "DetectAndSplitElements",              &Inter_Fracture_Tetrahedra::Detect_And_Split_Elements )
    ;

    class_<DofUtility, boost::noncopyable >
    ( "DofUtility", init<>() )
    .def( "ListDofs", &DofUtility::ListDofs )
    ;
}
}  // namespace Python.
}  // namespace Kratos.

#endif // KRATOS_ADD_CUSTOM_UTILITIES_TO_PYTHON_H_INCLUDED  defined