| 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 |  | 
|---|
| 104 | BoostGraphCreator::Edge BoostGraphCreator::findEdge(const atomId_t &_firstid, const atomId_t &_secondid) | 
|---|
| 105 | { | 
|---|
| 106 | edge_iter i, end; | 
|---|
| 107 | const name_map_t name_map = boost::get(boost::vertex_name, graph); | 
|---|
| 108 | for (boost::tie(i, end) = boost::edges(graph); i != end; ++i) { | 
|---|
| 109 | const BoostGraphCreator::Edge &e = *i; | 
|---|
| 110 | const Vertex u = source(e, graph); | 
|---|
| 111 | const Vertex v = target(e, graph); | 
|---|
| 112 | const atomId_t atomid_u = boost::get(name_map, u); | 
|---|
| 113 | const atomId_t atomid_v = boost::get(name_map, v); | 
|---|
| 114 | if (atomid_u == _firstid) { | 
|---|
| 115 | if (atomid_v == _secondid) | 
|---|
| 116 | break; | 
|---|
| 117 | } else if (atomid_u == _secondid) { | 
|---|
| 118 | if (atomid_v == _firstid) | 
|---|
| 119 | break; | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 | if (i != end) { | 
|---|
| 123 | BoostGraphCreator::Edge e = *i; | 
|---|
| 124 | return e; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | return BoostGraphCreator::Edge(); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | bool BoostGraphCreator::removeEdge(const atomId_t &_firstid, const atomId_t &_secondid) | 
|---|
| 131 | { | 
|---|
| 132 | // look through all edges | 
|---|
| 133 | BoostGraphCreator::Edge e = findEdge(_firstid, _secondid); | 
|---|
| 134 |  | 
|---|
| 135 | // edge was found | 
|---|
| 136 | if (e != BoostGraphCreator::Edge()) { | 
|---|
| 137 | const Vertex u = source(e, graph); | 
|---|
| 138 | const Vertex v = target(e, graph); | 
|---|
| 139 | if (DoLog(3)) { | 
|---|
| 140 | const name_map_t name_map = boost::get(boost::vertex_name, graph); | 
|---|
| 141 | LOG(3, "DEBUG: Found edge " << boost::get(name_map, u) <<  " <-> " | 
|---|
| 142 | << boost::get(name_map, v) << ", removing."); | 
|---|
| 143 | } | 
|---|
| 144 | boost::remove_edge(e, graph); | 
|---|
| 145 |  | 
|---|
| 146 | return true; | 
|---|
| 147 | } | 
|---|
| 148 | return false; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | bool BoostGraphCreator::addEdge(const atomId_t &_firstid, const atomId_t &_secondid) | 
|---|
| 152 | { | 
|---|
| 153 | // look through all edges | 
|---|
| 154 | BoostGraphCreator::Edge e = findEdge(_firstid, _secondid); | 
|---|
| 155 |  | 
|---|
| 156 | if (e != BoostGraphCreator::Edge()) { | 
|---|
| 157 | return false; | 
|---|
| 158 | } else { | 
|---|
| 159 | const nodeId_t leftnodeid = getNodeId(_firstid); | 
|---|
| 160 | const nodeId_t rightnodeid = getNodeId(_secondid); | 
|---|
| 161 |  | 
|---|
| 162 | boost::add_edge(leftnodeid, rightnodeid, graph); | 
|---|
| 163 |  | 
|---|
| 164 | return true; | 
|---|
| 165 | } | 
|---|
| 166 | } | 
|---|