| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2017 Frederik Heber. All rights reserved.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * BoostGraphCreator.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: May 17, 2017
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "BoostGraphCreator.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <algorithm>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "Atom/atom.hpp"
 | 
|---|
| 45 | #include "molecule.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void BoostGraphCreator::createFromMolecule(
 | 
|---|
| 48 |     const molecule &_mol,
 | 
|---|
| 49 |     const predicate_t &_pred)
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   createFromRange<molecule::const_iterator>(_mol.begin(), _mol.end(), _mol.getAtomCount(), _pred);
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | static bool predicateAnd(
 | 
|---|
| 55 |     const BoostGraphCreator::predicate_t &_pred1,
 | 
|---|
| 56 |     const BoostGraphCreator::predicate_t &_pred2,
 | 
|---|
| 57 |     const bond &_bond)
 | 
|---|
| 58 | { return (_pred1(_bond) && _pred2(_bond)); }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | static atomId_t getAtomId(
 | 
|---|
| 61 |     const atom *_atom
 | 
|---|
| 62 |     )
 | 
|---|
| 63 | { return _atom->getId(); }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | static bool inSetPredicate(
 | 
|---|
| 66 |   const std::vector<atomId_t> &_atomids,
 | 
|---|
| 67 |   const bond &_bond)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   const atomId_t leftid = _bond.leftatom->getId();
 | 
|---|
| 70 |   const atomId_t rightid = _bond.rightatom->getId();
 | 
|---|
| 71 |   return std::binary_search(_atomids.begin(), _atomids.end(), leftid)
 | 
|---|
| 72 |       && std::binary_search(_atomids.begin(), _atomids.end(), rightid);
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | void BoostGraphCreator::createFromAtoms(
 | 
|---|
| 76 |     const std::vector<atom *> &_atoms,
 | 
|---|
| 77 |     const predicate_t &_pred)
 | 
|---|
| 78 | {
 | 
|---|
| 79 |   // sort makes binary_search possible
 | 
|---|
| 80 |   std::vector<atomId_t> atomids;
 | 
|---|
| 81 |   std::transform(_atoms.begin(), _atoms.end(),
 | 
|---|
| 82 |       std::back_inserter(atomids), getAtomId);
 | 
|---|
| 83 |   ASSERT( _atoms.size() == atomids.size(),
 | 
|---|
| 84 |       "BoostGraphCreator::createFromAtom() - atomids "
 | 
|---|
| 85 |       +toString(atomids.size())+" and atoms "+toString(_atoms.size())
 | 
|---|
| 86 |       +" differ in size?");
 | 
|---|
| 87 |   std::sort(atomids.begin(), atomids.end());
 | 
|---|
| 88 |   const predicate_t predicate = boost::bind(inSetPredicate, boost::ref(atomids), _1);
 | 
|---|
| 89 |   createFromRange<std::vector<atom *>::const_iterator>(
 | 
|---|
| 90 |       const_cast<const std::vector<atom *> &>(_atoms).begin(),
 | 
|---|
| 91 |       const_cast<const std::vector<atom *> &>(_atoms).end(),
 | 
|---|
| 92 |       _atoms.size(),
 | 
|---|
| 93 |       boost::bind(predicateAnd, _pred, predicate, _1));
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | BoostGraphCreator::nodeId_t BoostGraphCreator::getNodeId(
 | 
|---|
| 97 |     const atomId_t &_atomid) const
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   atomids_nodeids_t::const_iterator iter =
 | 
|---|
| 100 |       atomids_nodeids.find(_atomid);
 | 
|---|
| 101 |   return (iter == atomids_nodeids.end()) ? (nodeId_t)-1 : iter->second;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|