| 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 "surfacerender.h" |
| 7 |
|
| 8 |
#include <avogadro/core/array.h> |
| 9 |
#include <avogadro/core/mesh.h> |
| 10 |
#include <avogadro/qtgui/colorbutton.h> |
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
#include <avogadro/rendering/geometrynode.h> |
| 13 |
#include <avogadro/rendering/groupnode.h> |
| 14 |
#include <avogadro/rendering/meshgeometry.h> |
| 15 |
#include <avogadro/rendering/widelinegeometry.h> |
| 16 |
#include <avogadro/rendering/volumegeometry.h> |
| 17 |
|
| 18 |
#include <QtCore/QDebug> |
| 19 |
#include <QtCore/QSettings> |
| 20 |
#include <QtWidgets/QComboBox> |
| 21 |
#include <QtWidgets/QDoubleSpinBox> |
| 22 |
#include <QtWidgets/QFormLayout> |
| 23 |
#include <QtWidgets/QSlider> |
| 24 |
#include <QtWidgets/QVBoxLayout> |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
namespace Avogadro::QtPlugins { |
| 29 |
|
| 30 |
using Core::Mesh; |
| 31 |
using Rendering::GeometryNode; |
| 32 |
using Rendering::GroupNode; |
| 33 |
using Rendering::MeshGeometry; |
| 34 |
using Rendering::VolumeGeometry; |
| 35 |
using Rendering::WideLineGeometry; |
| 36 |
|
| 37 |
SurfaceRender::SurfaceRender(QObject* p) |
| 38 |
: ScenePlugin(p), m_setupWidget(nullptr) |
| 39 |
{ |
| 40 |
m_layerManager = QtGui::PluginLayerManager(m_name); |
| 41 |
|
| 42 |
QSettings settings; |
| 43 |
// out of 255 |
| 44 |
m_opacity = settings.value("meshes/opacity", 150).toUInt(); |
| 45 |
|
| 46 |
auto color = settings.value("meshes/color1", QColor(Qt::red)).value<QColor>(); |
| 47 |
m_color1[0] = static_cast<unsigned char>(color.red()); |
| 48 |
m_color1[1] = static_cast<unsigned char>(color.green()); |
| 49 |
m_color1[2] = static_cast<unsigned char>(color.blue()); |
| 50 |
|
| 51 |
color = settings.value("meshes/color2", QColor(Qt::blue)).value<QColor>(); |
| 52 |
m_color2[0] = static_cast<unsigned char>(color.red()); |
| 53 |
m_color2[1] = static_cast<unsigned char>(color.green()); |
| 54 |
m_color2[2] = static_cast<unsigned char>(color.blue()); |
| 55 |
|
| 56 |
auto style = settings.value("meshes/style", 0).toInt(); |
| 57 |
m_style = static_cast<Style>(style); |
| 58 |
|
| 59 |
m_lineWidth = settings.value("meshes/lineWidth", 1.0).toFloat(); |
| 60 |
} |
| 61 |
|
| 62 |
SurfaceRender::~SurfaceRender() {} |
| 63 |
|
| 64 |
void SurfaceRender::process(const QtGui::Molecule& mol, GroupNode& node) |
| 65 |
{ |
| 66 |
if (mol.meshCount()) { |
| 67 |
auto* geometry = new GeometryNode; |
| 68 |
node.addChild(geometry); |
| 69 |
|
| 70 |
// Handle the first mesh |
| 71 |
const Mesh* mesh = mol.mesh(0); |
| 72 |
Core::Array<Vector3f> triangles = mesh->triangles(); |
| 73 |
|
| 74 |
bool hasColors = (mesh->colors().size() != 0); |
| 75 |
|
| 76 |
if (m_style == SurfaceRender::Surface) { |
| 77 |
auto* mesh1 = new MeshGeometry; |
| 78 |
geometry->addDrawable(mesh1); |
| 79 |
mesh1->setOpacity(m_opacity); |
| 80 |
|
| 81 |
if (hasColors) { |
| 82 |
auto colors = mesh->colors(); |
| 83 |
Core::Array<Vector3ub> colorsRGB(colors.size()); |
| 84 |
for (size_t i = 0; i < colors.size(); i++) |
| 85 |
colorsRGB[i] = |
| 86 |
Vector3ub(static_cast<unsigned char>(colors[i].red() * 255), |
| 87 |
static_cast<unsigned char>(colors[i].green() * 255), |
| 88 |
static_cast<unsigned char>(colors[i].blue() * 255)); |
| 89 |
mesh1->addVertices(mesh->vertices(), mesh->normals(), colorsRGB); |
| 90 |
} else { |
| 91 |
mesh1->setColor(m_color1); |
| 92 |
mesh1->addVertices(mesh->vertices(), mesh->normals()); |
| 93 |
} |
| 94 |
|
| 95 |
// Add the triangles for the first mesh |
| 96 |
for (size_t i = 0; i < triangles.size(); ++i) { |
| 97 |
mesh1->addTriangle(triangles[i][0], triangles[i][1], triangles[i][2]); |
| 98 |
} |
| 99 |
|
| 100 |
mesh1->setRenderPass(m_opacity == 255 ? Rendering::SolidPass |
| 101 |
: Rendering::TranslucentPass); |
| 102 |
|
| 103 |
// Handle the second mesh if present |
| 104 |
if (mol.meshCount() >= 2) { |
| 105 |
auto* mesh2 = new MeshGeometry; |
| 106 |
geometry->addDrawable(mesh2); |
| 107 |
|
| 108 |
mesh = mol.mesh(1); |
| 109 |
|
| 110 |
// Retrieve the second mesh’s triangles |
| 111 |
Core::Array<Vector3f> triangles2 = mesh->triangles(); |
| 112 |
|
| 113 |
mesh2->setColor(m_color2); |
| 114 |
mesh2->setOpacity(m_opacity); |
| 115 |
mesh2->addVertices(mesh->vertices(), mesh->normals()); |
| 116 |
|
| 117 |
// Add the correct triangles for the second mesh |
| 118 |
for (size_t i = 0; i < triangles2.size(); ++i) { |
| 119 |
mesh2->addTriangle(triangles2[i][0], triangles2[i][1], |
| 120 |
triangles2[i][2]); |
| 121 |
} |
| 122 |
|
| 123 |
mesh2->setRenderPass(m_opacity == 255 ? Rendering::SolidPass |
| 124 |
: Rendering::TranslucentPass); |
| 125 |
} |
| 126 |
} else if (m_style == SurfaceRender::Wireframe) { |
| 127 |
auto* ls1 = new WideLineGeometry; |
| 128 |
geometry->addDrawable(ls1); |
| 129 |
// Thinner scale for surface wireframes (denser mesh than molecular bonds) |
| 130 |
float width = m_lineWidth * 0.01f; |
| 131 |
|
| 132 |
if (hasColors) { |
| 133 |
auto colors = mesh->colors(); |
| 134 |
Core::Array<Vector3ub> colorsRGB(colors.size()); |
| 135 |
for (size_t i = 0; i < colors.size(); i++) |
| 136 |
colorsRGB[i] = |
| 137 |
Vector3ub(static_cast<unsigned char>(colors[i].red() * 255), |
| 138 |
static_cast<unsigned char>(colors[i].green() * 255), |
| 139 |
static_cast<unsigned char>(colors[i].blue() * 255)); |
| 140 |
ls1->addLineStrip(mesh->vertices(), colorsRGB, width); |
| 141 |
} else { |
| 142 |
auto vertices = mesh->vertices(); |
| 143 |
Vector3ub color1(m_color1[0], m_color1[1], m_color1[2]); |
| 144 |
ls1->reserve(triangles.size() * 3); |
| 145 |
for (size_t i = 0; i < triangles.size(); ++i) { |
| 146 |
Vector3f v0 = vertices[triangles[i][0]]; |
| 147 |
Vector3f v1 = vertices[triangles[i][1]]; |
| 148 |
Vector3f v2 = vertices[triangles[i][2]]; |
| 149 |
ls1->addLine(v0, v1, color1, width); |
| 150 |
ls1->addLine(v1, v2, color1, width); |
| 151 |
ls1->addLine(v2, v0, color1, width); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Handle the second mesh if present (only has single colors) |
| 156 |
if (mol.meshCount() >= 2) { |
| 157 |
auto* mesh2Lines = new WideLineGeometry; |
| 158 |
geometry->addDrawable(mesh2Lines); |
| 159 |
|
| 160 |
mesh = mol.mesh(1); |
| 161 |
|
| 162 |
auto vertices2 = mesh->vertices(); |
| 163 |
triangles = mesh->triangles(); |
| 164 |
Vector3ub color2(m_color2[0], m_color2[1], m_color2[2]); |
| 165 |
mesh2Lines->reserve(triangles.size() * 3); |
| 166 |
for (size_t i = 0; i < triangles.size(); ++i) { |
| 167 |
Vector3f v0 = vertices2[triangles[i][0]]; |
| 168 |
Vector3f v1 = vertices2[triangles[i][1]]; |
| 169 |
Vector3f v2 = vertices2[triangles[i][2]]; |
| 170 |
mesh2Lines->addLine(v0, v1, color2, width); |
| 171 |
mesh2Lines->addLine(v1, v2, color2, width); |
| 172 |
mesh2Lines->addLine(v2, v0, color2, width); |
| 173 |
} |
| 174 |
} |
| 175 |
} // if style == Wireframe |
| 176 |
else if (m_style == SurfaceRender::Volume) { |
| 177 |
const Core::Cube* cube = mol.activeCube(); |
| 178 |
if (cube == nullptr) |
| 179 |
return; |
| 180 |
|
| 181 |
auto* volume = new VolumeGeometry; |
| 182 |
// qDebug() << "Volume data size: " << cube->data()->size(); |
| 183 |
volume->setCube(*cube); |
| 184 |
volume->setRenderPass(Rendering::TranslucentPass); |
| 185 |
|
| 186 |
// TODO: better support for color ramps |
| 187 |
volume->setPositiveColor(m_color1); |
| 188 |
volume->setNegativeColor(m_color2); |
| 189 |
|
| 190 |
geometry->addDrawable(volume); |
| 191 |
} |
| 192 |
} // if meshCount != 0 |
| 193 |
} |
| 194 |
|
| 195 |
void SurfaceRender::setOpacity(int opacity) |
| 196 |
{ |
| 197 |
m_opacity = opacity; |
| 198 |
emit drawablesChanged(); |
| 199 |
|
| 200 |
QSettings settings; |
| 201 |
settings.setValue("meshes/opacity", m_opacity); |
| 202 |
} |
| 203 |
|
| 204 |
void SurfaceRender::setLineWidth(double width) |
| 205 |
{ |
| 206 |
m_lineWidth = width; |
| 207 |
emit drawablesChanged(); |
| 208 |
|
| 209 |
QSettings settings; |
| 210 |
settings.setValue("meshes/lineWidth", m_lineWidth); |
| 211 |
} |
| 212 |
|
| 213 |
void SurfaceRender::setColor1(const QColor& color) |
| 214 |
{ |
| 215 |
m_color1[0] = static_cast<unsigned char>(color.red()); |
| 216 |
m_color1[1] = static_cast<unsigned char>(color.green()); |
| 217 |
m_color1[2] = static_cast<unsigned char>(color.blue()); |
| 218 |
|
| 219 |
emit drawablesChanged(); |
| 220 |
|
| 221 |
QSettings settings; |
| 222 |
settings.setValue("meshes/color1", color); |
| 223 |
} |
| 224 |
|
| 225 |
void SurfaceRender::setColor2(const QColor& color) |
| 226 |
{ |
| 227 |
m_color2[0] = static_cast<unsigned char>(color.red()); |
| 228 |
m_color2[1] = static_cast<unsigned char>(color.green()); |
| 229 |
m_color2[2] = static_cast<unsigned char>(color.blue()); |
| 230 |
|
| 231 |
emit drawablesChanged(); |
| 232 |
|
| 233 |
QSettings settings; |
| 234 |
settings.setValue("meshes/color2", color); |
| 235 |
} |
| 236 |
|
| 237 |
void SurfaceRender::setStyle(int style) |
| 238 |
{ |
| 239 |
m_style = static_cast<Style>(style); |
| 240 |
emit drawablesChanged(); |
| 241 |
|
| 242 |
// get the form layout |
| 243 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) |
| 244 |
auto* form = qobject_cast<QFormLayout*>(m_setupWidget->layout()); |
| 245 |
auto* slide = m_setupWidget->findChild<QSlider*>("opacitySlider"); |
| 246 |
auto* spin = m_setupWidget->findChild<QDoubleSpinBox*>("lineWidthSpin"); |
| 247 |
|
| 248 |
if (form && slide && spin) { |
| 249 |
if (m_style == Surface) { |
| 250 |
form->setRowVisible(slide, true); |
| 251 |
form->setRowVisible(spin, false); |
| 252 |
} else if (m_style == Wireframe) { |
| 253 |
form->setRowVisible(slide, false); |
| 254 |
form->setRowVisible(spin, true); |
| 255 |
} |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
QSettings settings; |
| 260 |
settings.setValue("meshes/style", m_style); |
| 261 |
} |
| 262 |
|
| 263 |
QWidget* SurfaceRender::setupWidget() |
| 264 |
{ |
| 265 |
if (!m_setupWidget) { |
| 266 |
m_setupWidget = new QWidget(qobject_cast<QWidget*>(parent())); |
| 267 |
// auto* v = new QVBoxLayout; |
| 268 |
auto* form = new QFormLayout; |
| 269 |
|
| 270 |
// Style |
| 271 |
auto* combo = new QComboBox; |
| 272 |
combo->addItem(tr("Surface")); |
| 273 |
combo->addItem(tr("Wireframe")); |
| 274 |
combo->addItem(tr("Volume")); |
| 275 |
combo->setCurrentIndex(m_style); |
| 276 |
connect(combo, SIGNAL(currentIndexChanged(int)), SLOT(setStyle(int))); |
| 277 |
form->addRow(tr("Style:"), combo); |
| 278 |
|
| 279 |
// Opacity |
| 280 |
auto* slide = new QSlider(Qt::Horizontal); |
| 281 |
slide->setRange(0, 255); |
| 282 |
slide->setTickInterval(5); |
| 283 |
slide->setValue(m_opacity); |
| 284 |
slide->setObjectName("opacitySlider"); |
| 285 |
connect(slide, SIGNAL(valueChanged(int)), SLOT(setOpacity(int))); |
| 286 |
form->addRow(tr("Opacity:"), slide); |
| 287 |
|
| 288 |
// Line Width |
| 289 |
auto* spin = new QDoubleSpinBox; |
| 290 |
spin->setRange(0.5, 5.0); |
| 291 |
spin->setSingleStep(0.25); |
| 292 |
spin->setDecimals(2); |
| 293 |
spin->setValue(m_lineWidth); |
| 294 |
spin->setObjectName("lineWidthSpin"); |
| 295 |
QObject::connect(spin, SIGNAL(valueChanged(double)), |
| 296 |
SLOT(setLineWidth(double))); |
| 297 |
form->addRow(QObject::tr("Line width:"), spin); |
| 298 |
|
| 299 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) |
| 300 |
if (m_style == Surface) { |
| 301 |
form->setRowVisible(slide, true); |
| 302 |
form->setRowVisible(spin, false); |
| 303 |
} else if (m_style == Wireframe) { |
| 304 |
form->setRowVisible(slide, false); |
| 305 |
form->setRowVisible(spin, true); |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
auto* color1 = new QtGui::ColorButton; |
| 310 |
color1->setColor(QColor(m_color1[0], m_color1[1], m_color1[2])); |
| 311 |
connect(color1, SIGNAL(colorChanged(const QColor&)), |
| 312 |
SLOT(setColor1(const QColor&))); |
| 313 |
form->addRow(tr("Color:"), color1); |
| 314 |
|
| 315 |
auto* color2 = new QtGui::ColorButton; |
| 316 |
color2->setColor(QColor(m_color2[0], m_color2[1], m_color2[2])); |
| 317 |
connect(color2, SIGNAL(colorChanged(const QColor&)), |
| 318 |
SLOT(setColor2(const QColor&))); |
| 319 |
form->addRow(tr("Color:"), color2); |
| 320 |
|
| 321 |
m_setupWidget->setLayout(form); |
| 322 |
} |
| 323 |
return m_setupWidget; |
| 324 |
} |
| 325 |
|
| 326 |
} // namespace Avogadro::QtPlugins |