[6afd46] | 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 | #include <iterator>
|
---|
| 42 |
|
---|
| 43 | #include "CodePatterns/Assert.hpp"
|
---|
| 44 | #include "CodePatterns/Log.hpp"
|
---|
| 45 |
|
---|
| 46 | #include "Atom/atom.hpp"
|
---|
| 47 | #include "Bond/bond.hpp"
|
---|
| 48 | #include "molecule.hpp"
|
---|
| 49 |
|
---|
| 50 | void BoostGraphCreator::createFromMolecule(
|
---|
| 51 | const molecule &_mol,
|
---|
| 52 | const predicate_t &_pred)
|
---|
| 53 | {
|
---|
| 54 | createFromRange<molecule::const_iterator>(_mol.begin(), _mol.end(), _mol.getAtomCount(), _pred);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static bool predicateAnd(
|
---|
| 58 | const BoostGraphCreator::predicate_t &_pred1,
|
---|
| 59 | const BoostGraphCreator::predicate_t &_pred2,
|
---|
| 60 | const bond &_bond)
|
---|
| 61 | { return (_pred1(_bond) && _pred2(_bond)); }
|
---|
| 62 |
|
---|
| 63 | static atomId_t getAtomId(
|
---|
| 64 | const atom *_atom
|
---|
| 65 | )
|
---|
| 66 | { return _atom->getId(); }
|
---|
| 67 |
|
---|
| 68 | static bool inSetPredicate(
|
---|
| 69 | const std::vector<atomId_t> &_atomids,
|
---|
| 70 | const bond &_bond)
|
---|
| 71 | {
|
---|
| 72 | const atomId_t leftid = _bond.leftatom->getId();
|
---|
| 73 | const atomId_t rightid = _bond.rightatom->getId();
|
---|
| 74 | return std::binary_search(_atomids.begin(), _atomids.end(), leftid)
|
---|
| 75 | && std::binary_search(_atomids.begin(), _atomids.end(), rightid);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void BoostGraphCreator::createFromAtoms(
|
---|
| 79 | const std::vector<atom *> &_atoms,
|
---|
| 80 | const predicate_t &_pred)
|
---|
| 81 | {
|
---|
| 82 | // sort makes binary_search possible
|
---|
| 83 | std::vector<atomId_t> atomids;
|
---|
| 84 | std::transform(_atoms.begin(), _atoms.end(),
|
---|
| 85 | std::back_inserter(atomids), getAtomId);
|
---|
| 86 | ASSERT( _atoms.size() == atomids.size(),
|
---|
| 87 | "BoostGraphCreator::createFromAtom() - atomids and atoms differ in size?");
|
---|
| 88 | std::sort(atomids.begin(), atomids.end());
|
---|
| 89 | const predicate_t predicate = boost::bind(inSetPredicate, boost::ref(atomids), _1);
|
---|
| 90 | createFromRange<std::vector<atom *>::const_iterator>(
|
---|
| 91 | const_cast<const std::vector<atom *> &>(_atoms).begin(),
|
---|
| 92 | const_cast<const std::vector<atom *> &>(_atoms).end(),
|
---|
| 93 | _atoms.size(),
|
---|
| 94 | boost::bind(predicateAnd, _pred, predicate, _1));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | template <typename iterator>
|
---|
| 98 | void BoostGraphCreator::createFromRange(
|
---|
| 99 | const iterator &_begin,
|
---|
| 100 | const iterator &_end,
|
---|
| 101 | const size_t &_no_nodes,
|
---|
| 102 | const predicate_t &_pred
|
---|
| 103 | ) {
|
---|
| 104 | // convert BondGraph into boost::graph
|
---|
| 105 | UndirectedGraph molgraph(_no_nodes);
|
---|
| 106 | for(iterator iter = _begin; iter != _end; ++iter) {
|
---|
| 107 | LOG(2, "DEBUG: Looking at node " << (*iter)->getId());
|
---|
| 108 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
---|
| 109 | for(BondList::const_iterator bonditer = ListOfBonds.begin();
|
---|
| 110 | bonditer != ListOfBonds.end(); ++bonditer) {
|
---|
| 111 | const atomId_t leftid = (*bonditer)->leftatom->getId();
|
---|
| 112 | const atomId_t rightid = (*bonditer)->rightatom->getId();
|
---|
| 113 | // only pick each bond once and evaluate predicate
|
---|
| 114 | if ((leftid == (*iter)->getId())
|
---|
| 115 | && (_pred(**bonditer))) {
|
---|
| 116 | LOG(3, "DEBUG: ADDING edge " << leftid << " <-> " << rightid);
|
---|
| 117 | boost::add_edge(leftid, rightid, graph);
|
---|
| 118 | } else {
|
---|
| 119 | LOG(3, "DEBUG: Discarding edge " << leftid << " <-> " << rightid);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | LOG(2, "DEBUG: We have " << getNumVertices() << " nodes and " << getNumEdges()
|
---|
| 124 | << " edges in the molecule graph.");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|