Skip to content

Commit

Permalink
Merge pull request #1542 from ghutchis/color-crystal-axes
Browse files Browse the repository at this point in the history
Add red = a, blue = b, green = c color axes for unit cells
  • Loading branch information
ghutchis authored Dec 19, 2023
2 parents 4b34df4 + 70b6696 commit a37d0ba
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
70 changes: 58 additions & 12 deletions avogadro/qtplugins/crystal/crystalscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <avogadro/rendering/linestripgeometry.h>

#include <QtCore/QSettings>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QDoubleSpinBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QVBoxLayout>
Expand All @@ -28,8 +29,11 @@ using Rendering::GeometryNode;
using Rendering::GroupNode;
using Rendering::LineStripGeometry;

CrystalScene::CrystalScene(QObject* p)
: ScenePlugin(p), m_setupWidget(nullptr)
const Vector3ub red = { 255, 0, 0 };
const Vector3ub green = { 0, 255, 0 };
const Vector3ub blue = { 0, 0, 255 };

CrystalScene::CrystalScene(QObject* p) : ScenePlugin(p), m_setupWidget(nullptr)
{
m_layerManager = QtGui::PluginLayerManager(m_name);

Expand All @@ -41,6 +45,8 @@ CrystalScene::CrystalScene(QObject* p)
m_color[0] = static_cast<unsigned char>(color.red());
m_color[1] = static_cast<unsigned char>(color.green());
m_color[2] = static_cast<unsigned char>(color.blue());

m_multiColor = settings.value("crystal/multiColor", true).toBool();
}

CrystalScene::~CrystalScene() {}
Expand All @@ -52,8 +58,8 @@ void CrystalScene::process(const QtGui::Molecule& molecule, GroupNode& node)
node.addChild(geometry);
auto* lines = new LineStripGeometry;
geometry->addDrawable(lines);

lines->setColor(m_color);
auto color = m_color;
float width = m_lineWidth;

Vector3f a = cell->aVector().cast<float>();
Expand All @@ -63,24 +69,50 @@ void CrystalScene::process(const QtGui::Molecule& molecule, GroupNode& node)
Vector3f vertex(Vector3f::Zero());

Array<Vector3f> strip;
// draw the a axis
strip.reserve(5);
strip.push_back(vertex);
strip.push_back(vertex += a);
strip.push_back(vertex += b);
strip.push_back(vertex -= a);
strip.push_back(vertex -= b);
strip.push_back(vertex + a);
if (!m_multiColor)
lines->addLineStrip(strip, color, width);
else // a axis is R-G-B
lines->addLineStrip(strip, red, width);

// now the b-axis
strip.clear();
strip.push_back(vertex);
strip.push_back(vertex + b);
if (!m_multiColor)
lines->addLineStrip(strip, color, width);
else // b axis is R-G-B
lines->addLineStrip(strip, green, width);

// now the rest of the ab plane
strip.clear();
strip.push_back(vertex + a);
strip.push_back(vertex + a + b);
strip.push_back(vertex + b);
lines->addLineStrip(strip, width);

for (auto & it : strip) {
it += c;
}
// now the ab plane "up" by axis c
strip.clear();
strip.push_back(vertex + c);
strip.push_back(vertex + a + c);
strip.push_back(vertex + a + b + c);
strip.push_back(vertex + b + c);
strip.push_back(vertex + c);
lines->addLineStrip(strip, width);

// now the c axis
strip.resize(2);
strip[0] = Vector3f::Zero();
strip[1] = c;
lines->addLineStrip(strip, width);
if (!m_multiColor)
lines->addLineStrip(strip, color, width);
else // c axis is R-G-B
lines->addLineStrip(strip, blue, width);

// now the remaining "struts" from ab plane along c axis
strip[0] += a;
strip[1] += a;
lines->addLineStrip(strip, width);
Expand Down Expand Up @@ -116,6 +148,15 @@ void CrystalScene::setColor(const QColor& color)
settings.setValue("crystal/color", color);
}

void CrystalScene::setMultiColor(bool multiColor)
{
m_multiColor = multiColor;
emit drawablesChanged();

QSettings settings;
settings.setValue("crystal/multiColor", multiColor);
}

QWidget* CrystalScene::setupWidget()
{
if (!m_setupWidget) {
Expand All @@ -132,6 +173,11 @@ QWidget* CrystalScene::setupWidget()
auto* form = new QFormLayout;
form->addRow(tr("Line width:"), spin);

auto* multiColor = new QCheckBox;
multiColor->setChecked(m_multiColor);
form->addRow(tr("Color axes:"), multiColor);
connect(multiColor, SIGNAL(toggled(bool)), SLOT(setMultiColor(bool)));

auto* color = new QtGui::ColorButton;
connect(color, SIGNAL(colorChanged(const QColor&)),
SLOT(setColor(const QColor&)));
Expand All @@ -145,4 +191,4 @@ QWidget* CrystalScene::setupWidget()
return m_setupWidget;
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
6 changes: 4 additions & 2 deletions avogadro/qtplugins/crystal/crystalscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <avogadro/core/vector.h>
#include <avogadro/qtgui/sceneplugin.h>

#include <QtGui/QColor>
#include <QtGui/QColor>

namespace Avogadro {
namespace QtPlugins {
Expand Down Expand Up @@ -38,15 +38,17 @@ class CrystalScene : public QtGui::ScenePlugin
QWidget* setupWidget() override;

private slots:
void setColor(const QColor &color);
void setColor(const QColor& color);
void setLineWidth(double width);
void setMultiColor(bool multiColor);

private:
std::string m_name = "Crystal Lattice";

QWidget* m_setupWidget;
float m_lineWidth;
Vector3ub m_color;
bool m_multiColor;
};

} // end namespace QtPlugins
Expand Down
29 changes: 26 additions & 3 deletions avogadro/rendering/linestripgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
namespace {
#include "linestrip_fs.h"
#include "linestrip_vs.h"
}
} // namespace

using Avogadro::Core::Array;
using Avogadro::Vector3f;
using Avogadro::Vector3ub;
using Avogadro::Vector4ub;
using Avogadro::Core::Array;

using std::cout;
using std::endl;
Expand Down Expand Up @@ -231,6 +231,29 @@ size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices,
return result;
}

size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices,
const Vector3ub& rgb, float lineWidth)
{
if (vertices.empty())
return InvalidIndex;

size_t result = m_lineStarts.size();
m_lineStarts.push_back(static_cast<unsigned int>(m_vertices.size()));
m_lineWidths.push_back(lineWidth);

auto vertIter(vertices.begin());
auto vertEnd(vertices.end());

m_vertices.reserve(m_vertices.size() + vertices.size());
Vector4ub tmpColor(rgb[0], rgb[1], rgb[2], m_opacity);
while (vertIter != vertEnd) {
m_vertices.push_back(PackedVertex(*(vertIter++), tmpColor));
}

m_dirty = true;
return result;
}

size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices,
float lineWidth)
{
Expand All @@ -253,4 +276,4 @@ size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices,
return result;
}

} // End namespace Avogadro
} // namespace Avogadro::Rendering
7 changes: 3 additions & 4 deletions avogadro/rendering/linestripgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class AVOGADRORENDERING_EXPORT LineStripGeometry : public Drawable
Vector3f vertex; // 12 bytes
Vector4ub color; // 4 bytes

PackedVertex(const Vector3f& v, const Vector4ub& c)
: vertex(v)
, color(c)
{}
PackedVertex(const Vector3f& v, const Vector4ub& c) : vertex(v), color(c) {}
static int vertexOffset() { return 0; }
static int colorOffset() { return static_cast<int>(sizeof(Vector3f)); }
};
Expand Down Expand Up @@ -76,6 +73,8 @@ class AVOGADRORENDERING_EXPORT LineStripGeometry : public Drawable
const Core::Array<Vector4ub>& color, float lineWidth);
size_t addLineStrip(const Core::Array<Vector3f>& vertices,
const Core::Array<Vector3ub>& color, float lineWidth);
size_t addLineStrip(const Core::Array<Vector3f>& vertices,
const Vector3ub& color, float lineWidth);
size_t addLineStrip(const Core::Array<Vector3f>& vertices, float lineWidth);
/** @} */

Expand Down

0 comments on commit a37d0ba

Please sign in to comment.