scatterdatamodifier.cpp Example File
rotations/scatterdatamodifier.cpp
#include "scatterdatamodifier.h"
#include <QtDataVisualization/qscatterdataproxy.h>
#include <QtDataVisualization/qvalue3daxis.h>
#include <QtDataVisualization/q3dscene.h>
#include <QtDataVisualization/q3dcamera.h>
#include <QtDataVisualization/qscatter3dseries.h>
#include <QtDataVisualization/q3dtheme.h>
#include <QtDataVisualization/QCustom3DItem>
#include <QtCore/qmath.h>
using namespace QtDataVisualization;
static const float verticalRange = 8.0f;
static const float horizontalRange = verticalRange;
static const float ellipse_a = horizontalRange / 3.0f;
static const float ellipse_b = verticalRange;
static const float doublePi = float(M_PI) * 2.0f;
static const float radiansToDegrees = 360.0f / doublePi;
static const float animationFrames = 30.0f;
ScatterDataModifier::ScatterDataModifier(Q3DScatter *scatter)
: m_graph(scatter),
m_fieldLines(12),
m_arrowsPerLine(16),
m_magneticField(new QScatter3DSeries),
m_sun(new QCustom3DItem),
m_magneticFieldArray(0),
m_angleOffset(0.0f),
m_angleStep(doublePi / m_arrowsPerLine / animationFrames)
{
m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);
m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront);
m_magneticField->setItemSize(0.2f);
m_magneticField->setMesh(QAbstract3DSeries::MeshUserDefined);
m_magneticField->setUserDefinedMesh(QStringLiteral(":/mesh/narrowarrow.obj"));
QLinearGradient fieldGradient(0, 0, 16, 1024);
fieldGradient.setColorAt(0.0, Qt::black);
fieldGradient.setColorAt(1.0, Qt::white);
m_magneticField->setBaseGradient(fieldGradient);
m_magneticField->setColorStyle(Q3DTheme::ColorStyleRangeGradient);
m_sun->setScaling(QVector3D(0.07f, 0.07f, 0.07f));
m_sun->setMeshFile(QStringLiteral(":/mesh/largesphere.obj"));
QImage sunColor = QImage(2, 2, QImage::Format_RGB32);
sunColor.fill(QColor(0xff, 0xbb, 0x00));
m_sun->setTextureImage(sunColor);
m_graph->addSeries(m_magneticField);
m_graph->addCustomItem(m_sun);
m_graph->axisX()->setRange(-horizontalRange, horizontalRange);
m_graph->axisY()->setRange(-verticalRange, verticalRange);
m_graph->axisZ()->setRange(-horizontalRange, horizontalRange);
m_graph->axisX()->setSegmentCount(int(horizontalRange));
m_graph->axisZ()->setSegmentCount(int(horizontalRange));
QObject::connect(&m_rotationTimer, &QTimer::timeout, this,
&ScatterDataModifier::triggerRotation);
toggleRotation();
generateData();
}
ScatterDataModifier::~ScatterDataModifier()
{
delete m_graph;
}
void ScatterDataModifier::generateData()
{
if (!m_magneticFieldArray)
m_magneticFieldArray = new QScatterDataArray;
int arraySize = m_fieldLines * m_arrowsPerLine;
if (arraySize != m_magneticFieldArray->size())
m_magneticFieldArray->resize(arraySize);
QScatterDataItem *ptrToDataArray = &m_magneticFieldArray->first();
for (float i = 0; i < m_fieldLines; i++) {
float horizontalAngle = (doublePi * i) / m_fieldLines;
float xCenter = ellipse_a * qCos(horizontalAngle);
float zCenter = ellipse_a * qSin(horizontalAngle);
QQuaternion yRotation = QQuaternion::fromAxisAndAngle(0.0f, 1.0f, 0.0f, horizontalAngle * radiansToDegrees);
for (float j = 0; j < m_arrowsPerLine; j++) {
float verticalAngle = ((doublePi * j) / m_arrowsPerLine) + m_angleOffset;
float xUnrotated = ellipse_a * qCos(verticalAngle);
float y = ellipse_b * qSin(verticalAngle);
float xRotated = xUnrotated * qCos(horizontalAngle);
float zRotated = xUnrotated * qSin(horizontalAngle);
float x = xCenter + xRotated;
float z = zCenter + zRotated;
QQuaternion zRotation = QQuaternion::fromAxisAndAngle(0.0f, 0.0f, 1.0f, verticalAngle * radiansToDegrees);
QQuaternion totalRotation = yRotation * zRotation;
ptrToDataArray->setPosition(QVector3D(x, y, z));
ptrToDataArray->setRotation(totalRotation);
ptrToDataArray++;
}
}
if (m_graph->selectedSeries() == m_magneticField)
m_graph->clearSelection();
m_magneticField->dataProxy()->resetArray(m_magneticFieldArray);
}
void ScatterDataModifier::setFieldLines(int lines)
{
m_fieldLines = lines;
generateData();
}
void ScatterDataModifier::setArrowsPerLine(int arrows)
{
m_angleOffset = 0.0f;
m_angleStep = doublePi / m_arrowsPerLine / animationFrames;
m_arrowsPerLine = arrows;
generateData();
}
void ScatterDataModifier::triggerRotation()
{
m_angleOffset += m_angleStep;
generateData();
}
void ScatterDataModifier::toggleSun()
{
m_sun->setVisible(!m_sun->isVisible());
}
void ScatterDataModifier::toggleRotation()
{
if (m_rotationTimer.isActive())
m_rotationTimer.stop();
else
m_rotationTimer.start(15);
}