Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer system #709

Merged
merged 12 commits into from
Aug 10, 2021
2 changes: 2 additions & 0 deletions avogadro/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(HEADERS
gaussianset.h
gaussiansettools.h
graph.h
layer.h
matrix.h
mesh.h
molecule.h
Expand Down Expand Up @@ -66,6 +67,7 @@ set(SOURCES
gaussianset.cpp
gaussiansettools.cpp
graph.cpp
layer.cpp
mesh.cpp
mdlvalence_p.h
molecule.cpp
Expand Down
79 changes: 75 additions & 4 deletions avogadro/core/connectedgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ ConnectedGroup::ConnectedGroup(size_t n) : m_groupToNode(n)
resetToSize(n);
}

void ConnectedGroup::resizeGroups(size_t n)
serk12 marked this conversation as resolved.
Show resolved Hide resolved
{
m_groupToNode.resize(n, std::set<size_t>());
}

void ConnectedGroup::addGroup(size_t group)
{
for (auto& nodeGroup : m_nodeToGroup) {
if (nodeGroup.second >= group) {
++nodeGroup.second;
}
}
m_groupToNode.insert(m_groupToNode.begin() + group, std::set<size_t>());
}

ConnectedGroup::~ConnectedGroup() {}

void ConnectedGroup::addNode(size_t index)
Expand All @@ -37,6 +52,23 @@ void ConnectedGroup::addNode(size_t index)
}
}

void ConnectedGroup::addNode(size_t node, size_t group)
{
assert(group < m_groupToNode.size());
auto it = m_nodeToGroup.find(node);
// if node already exists, remove it
if (it == m_nodeToGroup.end()) {
m_nodeToGroup[node] = group;
m_groupToNode[group].insert(node);
} else {
auto old_group = m_nodeToGroup[node];
m_nodeToGroup[node] = group;

m_groupToNode[old_group].erase(node);
m_groupToNode[group].insert(node);
}
}

void ConnectedGroup::addNodes(size_t n)
{
size_t offset = m_nodeToGroup.size();
Expand All @@ -45,6 +77,14 @@ void ConnectedGroup::addNodes(size_t n)
}
}

void ConnectedGroup::addNodes(size_t n, size_t group)
{
size_t offset = m_nodeToGroup.size();
for (size_t i = 0; i < n; ++i) {
addNode(i + offset, group);
}
}

void ConnectedGroup::addConnection(size_t a, size_t b)
{
assert(m_nodeToGroup.find(a) != m_nodeToGroup.end());
Expand All @@ -64,10 +104,13 @@ void ConnectedGroup::addConnection(size_t a, size_t b)

void ConnectedGroup::removeNode(size_t index)
{
assert(m_nodeToGroup.find(index) != m_nodeToGroup.end());
removeConnection(index);
size_t group = m_nodeToGroup[index];
m_nodeToGroup.erase(group);
auto it = m_nodeToGroup.find(index);
if (it != m_nodeToGroup.end()) {
size_t group = it->second;
m_nodeToGroup.erase(it);
m_groupToNode[group].erase(index);
checkRemove(m_groupToNode);
}
}

void ConnectedGroup::removeConnections()
Expand Down Expand Up @@ -106,6 +149,24 @@ void ConnectedGroup::removeConnection(size_t a, size_t b,
checkRemove(m_groupToNode);
}

void ConnectedGroup::removeGroup(size_t group)
{
assert(group < m_nodeToGroup.size());
auto it = m_nodeToGroup.begin();
while (it != m_nodeToGroup.end()) {
if (it->second > group) {
--it->second;
++it;
} else if (it->second == group) {
it = m_nodeToGroup.erase(it);
} else {
++it;
}
}

m_groupToNode.erase(m_groupToNode.begin() + group);
}

void ConnectedGroup::clear()
{
m_nodeToGroup.clear();
Expand Down Expand Up @@ -142,10 +203,20 @@ size_t ConnectedGroup::groupCount() const
return m_groupToNode.size();
}

size_t ConnectedGroup::nodesCount() const
serk12 marked this conversation as resolved.
Show resolved Hide resolved
{
return m_nodeToGroup.size();
}

size_t ConnectedGroup::getGroupSize(size_t node) const
{
return m_groupToNode.at(m_nodeToGroup.at(node)).size();
}

bool ConnectedGroup::hasAtom(size_t atom) const
{
return m_nodeToGroup.find(atom) != m_nodeToGroup.end();
}

} // namespace Core
} // namespace Avogadro
10 changes: 10 additions & 0 deletions avogadro/core/connectedgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class AVOGADROCORE_EXPORT ConnectedGroup

/** check if @p index is already in a group otherwise create one for it */
void addNode(size_t index);
void addNode(size_t node, size_t group);

/** create @p n groups with 1 node each */
void addNodes(size_t n);
void addNodes(size_t n, size_t group);

/** node @p a and @p b will be in the same group */
void addConnection(size_t a, size_t b);
Expand All @@ -55,6 +57,10 @@ class AVOGADROCORE_EXPORT ConnectedGroup
* they don't */
void removeConnection(size_t a, size_t b, const std::set<size_t>& neighbors);

void removeGroup(size_t group);

void addGroup(size_t group);

/** Removes everything. */
void clear();

Expand All @@ -72,6 +78,10 @@ class AVOGADROCORE_EXPORT ConnectedGroup

/** @return the total groups existing */
size_t groupCount() const;
size_t nodesCount() const;

void resizeGroups(size_t n);
bool hasAtom(size_t atom) const;

private:
std::map<size_t, size_t> m_nodeToGroup;
Expand Down
120 changes: 120 additions & 0 deletions avogadro/core/layer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "layer.h"
#include <cassert>

namespace Avogadro {
namespace Core {

using std::swap;

Layer::Layer() : m_activeLayer(0), m_maxLayer(0) {}

void Layer::addAtom()
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
{
addAtom(m_activeLayer, m_atomAndLayers.size());
}

void Layer::addAtom(size_t layer)
{
addAtom(layer, m_atomAndLayers.size());
}

void Layer::addAtom(size_t layer, Index atom)
{
assert(layer <= m_maxLayer);
if (atom == m_atomAndLayers.size()) {
m_atomAndLayers.push_back(layer);
} else if (atom > m_atomAndLayers.size()) {
m_atomAndLayers.resize(layer + 1, MaxIndex);
m_atomAndLayers[atom] = layer;
} else {
m_atomAndLayers[atom] = layer;
}
}

void Layer::addAtomToActiveLayer(Index atom)
{
addAtom(m_activeLayer, atom);
}

void Layer::setActiveLayer(size_t layer)
{
assert(layer != MaxIndex);
m_activeLayer = layer;
}

void Layer::removeAtom(Index atom)
{
m_atomAndLayers.swapAndPop(atom);
}

void Layer::addLayer()
{
++m_maxLayer;
}

void Layer::addLayer(size_t layer)
{
assert(layer <= m_maxLayer + 1);
for (auto& atomLayer : m_atomAndLayers) {
if (atomLayer >= layer) {
++atomLayer;
}
}
++m_maxLayer;
}

size_t Layer::getLayerID(Index atom) const
{
if (atom >= m_atomAndLayers.size()) {
return MaxIndex;
} else {
return m_atomAndLayers[atom];
}
}

void Layer::clear()
{
m_atomAndLayers.clear();
m_activeLayer = m_maxLayer = 0;
}

size_t Layer::activeLayer() const
{
return m_activeLayer;
}

size_t Layer::maxLayer() const
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
{
return m_maxLayer;
}

void Layer::removeLayer(size_t layer)
{
assert(layer <= m_maxLayer);
if (m_maxLayer >= 1) {
for (auto it = m_atomAndLayers.begin(); it != m_atomAndLayers.end();) {
if (*it == layer) {
it = m_atomAndLayers.erase(it);
} else {
if (*it > layer) {
--(*it);
}
++it;
}
}
--m_maxLayer;
}
}

void Layer::swapLayer(Index a, Index b)
{
swap(m_atomAndLayers[a], m_atomAndLayers[b]);
}

} // namespace Core
} // namespace Avogadro
48 changes: 48 additions & 0 deletions avogadro/core/layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_CORE_LAYER_H
#define AVOGADRO_CORE_LAYER_H

#include "avogadrocore.h"

#include <avogadro/core/array.h>
#include <avogadro/core/connectedgroup.h>

namespace Avogadro {
namespace Core {

class AVOGADROCORE_EXPORT Layer
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
{
public:
Layer();

void addAtom();
void addAtom(size_t layer);
void addAtom(size_t layer, Index atom);
void addAtomToActiveLayer(Index atom);
void removeAtom(Index atom);
void removeLayer(size_t layer);

size_t getLayerID(Index atom) const;
size_t activeLayer() const;
size_t maxLayer() const;

void clear();
void addLayer();
void addLayer(size_t layer);
void setActiveLayer(size_t layer);
void swapLayer(Index a, Index b);

private:
Core::Array<size_t> m_atomAndLayers;
size_t m_activeLayer;
size_t m_maxLayer;
};

} // namespace Core
} // namespace Avogadro

#endif
2 changes: 1 addition & 1 deletion avogadro/core/molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ Molecule::AtomType Molecule::addAtom(unsigned char number, Vector3 position3d)
if (m_positions3d.size() == atomCount()) {
m_positions3d.push_back(position3d);
}
return addAtom(number);
return Molecule::addAtom(number);
}

void Molecule::swapBond(Index a, Index b)
Expand Down
4 changes: 4 additions & 0 deletions avogadro/qtgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ set(HEADERS
interfacescript.h
interfacewidget.h
jsonwidget.h
layermodel.h
layermanager.h
meshgenerator.h
molecule.h
moleculemodel.h
Expand Down Expand Up @@ -77,6 +79,8 @@ set(SOURCES
interfacescript.cpp
interfacewidget.cpp
jsonwidget.cpp
layermodel.cpp
layermanager.cpp
meshgenerator.cpp
molecule.cpp
moleculemodel.cpp
Expand Down
Binary file added avogadro/qtgui/icons/fallback/32x32/cross.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/cross.png~
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/dots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/edit-add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/lock-open.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/lock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avogadro/qtgui/icons/fallback/32x32/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading