| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "dihedraliterator.h" |
| 7 |
|
| 8 |
#include <avogadro/core/graph.h> |
| 9 |
#include <avogadro/core/molecule.h> |
| 10 |
|
| 11 |
namespace Avogadro::Core { |
| 12 |
|
| 13 |
DihedralIterator::DihedralIterator(const Molecule* mol) |
| 14 |
: m_i(MaxIndex), m_jk(0), m_l(MaxIndex), m_mol(mol) |
| 15 |
{ |
| 16 |
m_current = std::make_tuple(MaxIndex, MaxIndex, MaxIndex, MaxIndex); |
| 17 |
} |
| 18 |
|
| 19 |
Dihedral DihedralIterator::begin() |
| 20 |
{ |
| 21 |
// all the logic is in the operator++ |
| 22 |
return ++(*this); |
| 23 |
} |
| 24 |
|
| 25 |
Dihedral DihedralIterator::operator++() |
| 26 |
{ |
| 27 |
// impossible to have a dihedral, so quit |
| 28 |
if (m_mol == nullptr || m_mol->atomCount() < 4 || m_mol->bondCount() < 3) |
| 29 |
return std::make_tuple(MaxIndex, MaxIndex, MaxIndex, MaxIndex); |
| 30 |
|
| 31 |
Graph graph = m_mol->graph(); |
| 32 |
Index count = m_mol->bondCount(); |
| 33 |
|
| 34 |
// true if we have a valid current state |
| 35 |
// (i.e. false at the start) |
| 36 |
bool valid = (m_i != MaxIndex && m_l != MaxIndex); |
| 37 |
|
| 38 |
// m_jk is the current bond (i.e. index into the bonds) |
| 39 |
// m_i and m_l are instead index into the neighbors of m_jk |
| 40 |
// (i.e., m_i is the index into the neighbors of the first atom of the bond) |
| 41 |
// and m_l is the index into the neighbors of the second atom of the bond |
| 42 |
Index iIndex = MaxIndex; |
| 43 |
Index lIndex = MaxIndex; |
| 44 |
|
| 45 |
// if we don't have a valid state, try to find an initial dihedral |
| 46 |
if (!valid) { |
| 47 |
for (Index i = 0; i < count; ++i) { |
| 48 |
const Bond bond = m_mol->bond(i); |
| 49 |
const auto& neighbors1 = graph.neighbors(bond.atom1().index()); |
| 50 |
const auto& neighbors2 = graph.neighbors(bond.atom2().index()); |
| 51 |
|
| 52 |
if (neighbors1.size() < 1 || neighbors2.size() < 1) |
| 53 |
continue; // need to have at least one neighbor |
| 54 |
|
| 55 |
m_jk = i; |
| 56 |
// make sure that m_i doesn't point to atom2 |
| 57 |
m_i = 0; |
| 58 |
if (neighbors1[m_i] == bond.atom2().index()) { |
| 59 |
// try to increment m_i |
| 60 |
if (m_i + 1 < neighbors1.size()) { |
| 61 |
++m_i; |
| 62 |
} else { |
| 63 |
continue; // this central bond doesn't work |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
m_l = 0; |
| 68 |
// make sure that m_l doesn't point to atom1 |
| 69 |
if (neighbors2[m_l] == bond.atom1().index()) { |
| 70 |
// try to increment m_l |
| 71 |
if (m_l + 1 < neighbors2.size()) { |
| 72 |
++m_l; |
| 73 |
} else { |
| 74 |
continue; // this central bond doesn't work |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
iIndex = neighbors1[m_i]; |
| 79 |
lIndex = neighbors2[m_l]; |
| 80 |
|
| 81 |
valid = true; |
| 82 |
break; |
| 83 |
} |
| 84 |
} else { |
| 85 |
// we have a valid state, try to find the next dihedral |
| 86 |
const Bond bond = m_mol->bond(m_jk); |
| 87 |
const auto& neighbors1 = graph.neighbors(bond.atom1().index()); |
| 88 |
const auto& neighbors2 = graph.neighbors(bond.atom2().index()); |
| 89 |
|
| 90 |
// first check if we can increment m_l |
| 91 |
while (m_l + 1 < neighbors2.size()) { |
| 92 |
++m_l; |
| 93 |
|
| 94 |
// make sure that m_l doesn't point to atom1 |
| 95 |
if (neighbors2[m_l] == bond.atom1().index()) { |
| 96 |
continue; // increment it again |
| 97 |
} |
| 98 |
|
| 99 |
iIndex = neighbors1[m_i]; |
| 100 |
lIndex = neighbors2[m_l]; |
| 101 |
m_current = std::make_tuple(iIndex, bond.atom1().index(), |
| 102 |
bond.atom2().index(), lIndex); |
| 103 |
return m_current; |
| 104 |
} |
| 105 |
|
| 106 |
// we can try to increment m_i |
| 107 |
while (m_i + 1 < neighbors1.size()) { |
| 108 |
++m_i; |
| 109 |
// make sure that m_i doesn't point to atom2 |
| 110 |
if (neighbors1[m_i] == bond.atom2().index()) { |
| 111 |
// try to increment m_i again |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
// reset m_l and make sure it doesn't point to atom1 |
| 116 |
m_l = 0; |
| 117 |
if (neighbors2[m_l] == bond.atom1().index()) { |
| 118 |
// try to increment m_l |
| 119 |
if (m_l + 1 < neighbors2.size()) { |
| 120 |
++m_l; |
| 121 |
} else { |
| 122 |
continue; // this combination doesn't work |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
iIndex = neighbors1[m_i]; |
| 127 |
lIndex = neighbors2[m_l]; |
| 128 |
m_current = std::make_tuple(iIndex, bond.atom1().index(), |
| 129 |
bond.atom2().index(), lIndex); |
| 130 |
return m_current; |
| 131 |
} |
| 132 |
|
| 133 |
// okay, try to increment m_jk and reset m_i and m_l |
| 134 |
valid = false; |
| 135 |
for (Index i = m_jk + 1; i < count; ++i) { |
| 136 |
const Bond bondI = m_mol->bond(i); |
| 137 |
|
| 138 |
const auto& newNeighbors1 = graph.neighbors(bondI.atom1().index()); |
| 139 |
const auto& newNeighbors2 = graph.neighbors(bondI.atom2().index()); |
| 140 |
|
| 141 |
if (newNeighbors1.size() < 1 || newNeighbors2.size() < 1) |
| 142 |
continue; // need to have at least one neighbor |
| 143 |
|
| 144 |
m_jk = i; |
| 145 |
// make sure that m_i doesn't point to atom2 |
| 146 |
m_i = 0; |
| 147 |
if (newNeighbors1[m_i] == bondI.atom2().index()) { |
| 148 |
// try to increment m_i |
| 149 |
if (m_i + 1 < newNeighbors1.size()) { |
| 150 |
++m_i; |
| 151 |
} else { |
| 152 |
continue; // this central bond doesn't work |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
m_l = 0; |
| 157 |
// make sure that m_l doesn't point to atom1 |
| 158 |
if (newNeighbors2[m_l] == bondI.atom1().index()) { |
| 159 |
// try to increment m_l |
| 160 |
if (m_l + 1 < newNeighbors2.size()) { |
| 161 |
++m_l; |
| 162 |
} else { |
| 163 |
continue; // this central bond doesn't work |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
iIndex = newNeighbors1[m_i]; |
| 168 |
lIndex = newNeighbors2[m_l]; |
| 169 |
|
| 170 |
valid = true; |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// did we find anything? |
| 176 |
if (valid) { |
| 177 |
const Bond bond = m_mol->bond(m_jk); |
| 178 |
m_current = std::make_tuple(iIndex, bond.atom1().index(), |
| 179 |
bond.atom2().index(), lIndex); |
| 180 |
return m_current; |
| 181 |
} else |
| 182 |
return std::make_tuple(MaxIndex, MaxIndex, MaxIndex, MaxIndex); |
| 183 |
} // end ++ operator |
| 184 |
|
| 185 |
} // namespace Avogadro::Core |