From ba5714cfc6e74c656b9c380db5270d56a739a9dc Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 15:43:01 +0100 Subject: [PATCH 01/16] Add enum for text alignment modes, and implement helper functions to achieve alignment about Z. --- src/entities/text.cpp | 29 ++++++++++++++++++++++++++++- src/entities/text.h | 12 ++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/entities/text.cpp b/src/entities/text.cpp index f878ec8..41f4219 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -3,7 +3,7 @@ using namespace Mildred; // Enables showing of bounding boxes around Text Entities. -constexpr auto showBoundingBoxes = false; +constexpr auto showBoundingBoxes = true; //! Construct a new text entity /*! @@ -85,6 +85,10 @@ void TextEntity::updateTranslation() // Set the main positional transform positionalTransform_->setTranslation(anchorPosition_); + // Set the rotation about z. + positionalTransform_->setRotationZ(rotationZFromAlignment()); + + // Set the text translation vector so that the defined anchor point is located at {0,0,0} auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth()); auto v = QVector3D(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); @@ -142,6 +146,29 @@ void TextEntity::setAnchorPosition(QVector3D p) updateTranslation(); } +//! Set alignment of text +void TextEntity::setTextAlignment(Mildred::TextAlignment alignment) +{ + + alignment_ = alignment; + + updateTranslation(); +} + +//! Gets the required rotation, around z, in degrees, to achieve the current alignment. +float TextEntity::rotationZFromAlignment() +{ + switch(alignment_) + { + case (Mildred::TextAlignment::Horizontal): + return 0.0; + case (Mildred::TextAlignment::Vertical): + return 90.0; + default: + throw(std::runtime_error("Unhandled text alignment mode.")); + } +} + //! Return simple bounding cuboid for text, along with baseline descent from font metrics /*! * Calculates a bounding cuboid in the XY plane for the specified @param font and @param text. The @param depth is applied in diff --git a/src/entities/text.h b/src/entities/text.h index a067250..dd00222 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -10,6 +10,12 @@ namespace Mildred { + +enum class TextAlignment { + Horizontal, + Vertical +}; + //! TextEntity represents a 3D text entity /*! * TextEntity represents a renderable entity displaying a specified string with a given transform. @@ -23,6 +29,8 @@ class TextEntity : public Qt3DCore::QEntity private: // Collective positional transform Qt3DCore::QTransform *positionalTransform_{nullptr}; + // Alignment + Mildred::TextAlignment alignment_; // Material RenderableMaterial *material_{nullptr}; // Main entity @@ -64,6 +72,10 @@ class TextEntity : public Qt3DCore::QEntity void setAnchorPoint(MildredMetrics::AnchorPoint anchor); // Set anchor position void setAnchorPosition(QVector3D p); + // Set rotation + void setTextAlignment(Mildred::TextAlignment alignment); + // Get required rotation in degrees + float rotationZFromAlignment(); // Return simple bounding cuboid for text, along with baseline descent from font metrics static std::pair boundingCuboid(const QFont &font, const QString &text, float depth = 0.1f); // Return bounding cuboid with translation and anchor point applied, and required translation vector for text mesh From 5f0b5e5c4d67619a8bb8d5302a34030d809c3ca4 Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 16:05:45 +0100 Subject: [PATCH 02/16] Default alignment to horizontal. --- src/entities/text.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entities/text.h b/src/entities/text.h index dd00222..bb00062 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -30,7 +30,7 @@ class TextEntity : public Qt3DCore::QEntity // Collective positional transform Qt3DCore::QTransform *positionalTransform_{nullptr}; // Alignment - Mildred::TextAlignment alignment_; + Mildred::TextAlignment alignment_{Mildred::TextAlignment::Horizontal}; // Material RenderableMaterial *material_{nullptr}; // Main entity From 7ae037052e10f68cd2c16d3b574419586493e4ef Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 16:06:08 +0100 Subject: [PATCH 03/16] Disable bounding boxes. --- src/entities/text.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 41f4219..909a3a8 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -3,7 +3,7 @@ using namespace Mildred; // Enables showing of bounding boxes around Text Entities. -constexpr auto showBoundingBoxes = true; +constexpr auto showBoundingBoxes = false; //! Construct a new text entity /*! From f51e8da01bb2f4d3202a648559b8f4d5670901b5 Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 16:06:38 +0100 Subject: [PATCH 04/16] Functions for setting the alignment of tick labels / axis title labels. --- src/entities/axis.cpp | 15 +++++++++++++++ src/entities/axis.h | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index 3d3f9eb..a699589 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -466,6 +466,21 @@ void AxisEntity::setType(AxisType type) } } +//! Define title alignment +/*! + * Set the alignment of the title label to @param alignment. + */ +void AxisEntity::setTitleLabelAlignment(Mildred::TextAlignment alignment) +{ + axisTitleEntity_->setTextAlignment(alignment); +} + +void AxisEntity::setTickLabelAlignment(Mildred::TextAlignment alignment) +{ + for (auto &entity : tickLabelEntities_) + entity->setTextAlignment(alignment); +} + //! Define direction /*! * Set the direction of the axis in 3D space to the supplied vector @param v. diff --git a/src/entities/axis.h b/src/entities/axis.h index e54f30f..29c88d7 100644 --- a/src/entities/axis.h +++ b/src/entities/axis.h @@ -114,6 +114,10 @@ class AxisEntity : public Qt3DCore::QEntity public: // Set axis type void setType(AxisType type); + // Set title label alignment + void setTitleLabelAlignment(Mildred::TextAlignment alignment); + // Set tick label alignment + void setTickLabelAlignment(Mildred::TextAlignment alignment); // Set explicit direction void setDirection(QVector3D v); // Return explicit direction From 0089359d3a6b6b74064a4675db90ef16bdf2bf30 Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 16:06:51 +0100 Subject: [PATCH 05/16] Update basic example to enable rotation of axis labels. --- examples/basic/mainwindow.h | 3 + examples/basic/mainwindow.ui | 176 ++++++++++++++-------------- examples/basic/mainwindow_funcs.cpp | 54 ++++++++- 3 files changed, 147 insertions(+), 86 deletions(-) diff --git a/examples/basic/mainwindow.h b/examples/basic/mainwindow.h index 3e37518..8eeccd7 100644 --- a/examples/basic/mainwindow.h +++ b/examples/basic/mainwindow.h @@ -15,6 +15,9 @@ class MainWindow : public QMainWindow */ private slots: void on_mouseCoordStyleCombo_currentIndexChanged(int index); + void on_XLabelAlignmentCombo_currentIndexChanged(int index); + void on_YLabelAlignmentCombo_currentIndexChanged(int index); + void on_ZLabelAlignmentCombo_currentIndexChanged(int index); public slots: // Set mouse coordinates in external label. diff --git a/examples/basic/mainwindow.ui b/examples/basic/mainwindow.ui index 90eb174..119a8dc 100644 --- a/examples/basic/mainwindow.ui +++ b/examples/basic/mainwindow.ui @@ -96,93 +96,99 @@ Axes - - - 4 - - - 4 - - - 4 - - - 4 - - - 4 - - - - - Z - - - - - - - - 0 - 0 - - - - - - - - X - - - - - - - - 0 - 0 - - - - - - - - Y - - - - - - - - 0 - 0 - - - - - - - - Visible - - + + + + + + + X + + + + + + + + 0 + 0 + + + + + + + + Visible + + + + + + + - - - - Visible - - + + + + + + Y + + + + + + + + 0 + 0 + + + + + + + + Visible + + + + + + + - - - - Visible - - + + + + + + Z + + + + + + + + 0 + 0 + + + + + + + + Visible + + + + + + + diff --git a/examples/basic/mainwindow_funcs.cpp b/examples/basic/mainwindow_funcs.cpp index 6801cab..257b334 100644 --- a/examples/basic/mainwindow_funcs.cpp +++ b/examples/basic/mainwindow_funcs.cpp @@ -35,6 +35,14 @@ MainWindow::MainWindow() : QMainWindow() ui_.externalMouseCoordLabel->setVisible(false); connect(ui_.TestingWidget, SIGNAL(mouseCoordChanged(QPointF)), this, SLOT(setExternalMouseCoordinatesText(QPointF))); + // Axis Label Rotation + ui_.XLabelAlignmentCombo->addItem(QString("Horizontal")); + ui_.XLabelAlignmentCombo->addItem(QString("Vertical")); + ui_.YLabelAlignmentCombo->addItem(QString("Horizontal")); + ui_.YLabelAlignmentCombo->addItem(QString("Vertical")); + ui_.ZLabelAlignmentCombo->addItem(QString("Horizontal")); + ui_.ZLabelAlignmentCombo->addItem(QString("Vertical")); + // Data const auto nPoints = 1000; const auto delta = 2.0 * M_PI / nPoints; @@ -71,11 +79,55 @@ void MainWindow::on_mouseCoordStyleCombo_currentIndexChanged(int index) break; default: throw(std::runtime_error("Unhandled coordinate display style.\n")); - break; } } void MainWindow::setExternalMouseCoordinatesText(QPointF p) { ui_.externalMouseCoordLabel->setText(QString("%1 %2").arg(QString::number(p.x()), QString::number(p.y()))); +} + +void MainWindow::on_XLabelAlignmentCombo_currentIndexChanged(int index) +{ + switch (index) + { + case 0: + ui_.TestingWidget->xAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + break; + case 1: + ui_.TestingWidget->xAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + break; + default: + throw(std::runtime_error("Unhandled text alignment mode.\n")); + } +} + +void MainWindow::on_YLabelAlignmentCombo_currentIndexChanged(int index) +{ + switch (index) + { + case 0: + ui_.TestingWidget->yAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + break; + case 1: + ui_.TestingWidget->yAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + break; + default: + throw(std::runtime_error("Unhandled text alignment mode.\n")); + } +} + +void MainWindow::on_ZLabelAlignmentCombo_currentIndexChanged(int index) +{ + switch (index) + { + case 0: + ui_.TestingWidget->zAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + break; + case 1: + ui_.TestingWidget->zAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + break; + default: + throw(std::runtime_error("Unhandled text alignment mode.\n")); + } } \ No newline at end of file From 86c14836c9ade7a9d94b24e22805043f600c7a4f Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Fri, 20 May 2022 16:07:35 +0100 Subject: [PATCH 06/16] Clang format. --- src/entities/axis.cpp | 5 +---- src/entities/text.cpp | 3 +-- src/entities/text.h | 3 ++- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index a699589..3c015a6 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -470,10 +470,7 @@ void AxisEntity::setType(AxisType type) /*! * Set the alignment of the title label to @param alignment. */ -void AxisEntity::setTitleLabelAlignment(Mildred::TextAlignment alignment) -{ - axisTitleEntity_->setTextAlignment(alignment); -} +void AxisEntity::setTitleLabelAlignment(Mildred::TextAlignment alignment) { axisTitleEntity_->setTextAlignment(alignment); } void AxisEntity::setTickLabelAlignment(Mildred::TextAlignment alignment) { diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 909a3a8..7a26684 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -88,7 +88,6 @@ void TextEntity::updateTranslation() // Set the rotation about z. positionalTransform_->setRotationZ(rotationZFromAlignment()); - // Set the text translation vector so that the defined anchor point is located at {0,0,0} auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth()); auto v = QVector3D(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); @@ -158,7 +157,7 @@ void TextEntity::setTextAlignment(Mildred::TextAlignment alignment) //! Gets the required rotation, around z, in degrees, to achieve the current alignment. float TextEntity::rotationZFromAlignment() { - switch(alignment_) + switch (alignment_) { case (Mildred::TextAlignment::Horizontal): return 0.0; diff --git a/src/entities/text.h b/src/entities/text.h index bb00062..ef57e1d 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -11,7 +11,8 @@ namespace Mildred { -enum class TextAlignment { +enum class TextAlignment +{ Horizontal, Vertical }; From c7fe3ac172d345208c3b271ea7f6c66406537195 Mon Sep 17 00:00:00 2001 From: jswift-stfc Date: Fri, 20 May 2022 17:09:31 +0100 Subject: [PATCH 07/16] Include stdexcept? --- src/entities/text.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 7a26684..164fa5e 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -1,4 +1,5 @@ #include "entities/axis.h" +#include using namespace Mildred; From d9d676feb3986ee9ac1a9b9325af35e9072bdadc Mon Sep 17 00:00:00 2001 From: Jared Swift <87013792+jswift-stfc@users.noreply.github.com> Date: Sat, 21 May 2022 10:59:02 +0100 Subject: [PATCH 08/16] Update src/entities/text.cpp Co-authored-by: Tristan Youngs --- src/entities/text.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 164fa5e..4da18bb 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -149,7 +149,6 @@ void TextEntity::setAnchorPosition(QVector3D p) //! Set alignment of text void TextEntity::setTextAlignment(Mildred::TextAlignment alignment) { - alignment_ = alignment; updateTranslation(); From b3cded04fa8440d88a04d7726c6b63c0c121b35e Mon Sep 17 00:00:00 2001 From: Jared Swift Date: Mon, 23 May 2022 11:43:14 +0100 Subject: [PATCH 09/16] Remove enum and change naming conventions. --- examples/basic/mainwindow.h | 6 ++-- examples/basic/mainwindow.ui | 6 ++-- examples/basic/mainwindow_funcs.cpp | 44 ++++++++++++++++++++--------- src/entities/axis.cpp | 14 +++++---- src/entities/axis.h | 8 +++--- src/entities/text.cpp | 26 ++++------------- src/entities/text.h | 14 ++------- 7 files changed, 58 insertions(+), 60 deletions(-) diff --git a/examples/basic/mainwindow.h b/examples/basic/mainwindow.h index 8eeccd7..7b2772b 100644 --- a/examples/basic/mainwindow.h +++ b/examples/basic/mainwindow.h @@ -15,9 +15,9 @@ class MainWindow : public QMainWindow */ private slots: void on_mouseCoordStyleCombo_currentIndexChanged(int index); - void on_XLabelAlignmentCombo_currentIndexChanged(int index); - void on_YLabelAlignmentCombo_currentIndexChanged(int index); - void on_ZLabelAlignmentCombo_currentIndexChanged(int index); + void on_XLabelRotationCombo_currentIndexChanged(int index); + void on_YLabelRotationCombo_currentIndexChanged(int index); + void on_ZLabelRotationCombo_currentIndexChanged(int index); public slots: // Set mouse coordinates in external label. diff --git a/examples/basic/mainwindow.ui b/examples/basic/mainwindow.ui index 119a8dc..6624843 100644 --- a/examples/basic/mainwindow.ui +++ b/examples/basic/mainwindow.ui @@ -124,7 +124,7 @@ - + @@ -155,7 +155,7 @@ - + @@ -186,7 +186,7 @@ - + diff --git a/examples/basic/mainwindow_funcs.cpp b/examples/basic/mainwindow_funcs.cpp index 257b334..0b7eda2 100644 --- a/examples/basic/mainwindow_funcs.cpp +++ b/examples/basic/mainwindow_funcs.cpp @@ -36,12 +36,12 @@ MainWindow::MainWindow() : QMainWindow() connect(ui_.TestingWidget, SIGNAL(mouseCoordChanged(QPointF)), this, SLOT(setExternalMouseCoordinatesText(QPointF))); // Axis Label Rotation - ui_.XLabelAlignmentCombo->addItem(QString("Horizontal")); - ui_.XLabelAlignmentCombo->addItem(QString("Vertical")); - ui_.YLabelAlignmentCombo->addItem(QString("Horizontal")); - ui_.YLabelAlignmentCombo->addItem(QString("Vertical")); - ui_.ZLabelAlignmentCombo->addItem(QString("Horizontal")); - ui_.ZLabelAlignmentCombo->addItem(QString("Vertical")); + ui_.XLabelRotationCombo->addItem(QString("Horizontal")); + ui_.XLabelRotationCombo->addItem(QString("Vertical")); + ui_.YLabelRotationCombo->addItem(QString("Horizontal")); + ui_.YLabelRotationCombo->addItem(QString("Vertical")); + ui_.ZLabelRotationCombo->addItem(QString("Horizontal")); + ui_.ZLabelRotationCombo->addItem(QString("Vertical")); // Data const auto nPoints = 1000; @@ -61,6 +61,7 @@ MainWindow::MainWindow() : QMainWindow() ui_.TestingWidget->showAllData(); }; +<<<<<<< HEAD void MainWindow::on_mouseCoordStyleCombo_currentIndexChanged(int index) { switch (index) @@ -88,46 +89,61 @@ void MainWindow::setExternalMouseCoordinatesText(QPointF p) } void MainWindow::on_XLabelAlignmentCombo_currentIndexChanged(int index) +======= +void MainWindow::on_XLabelRotationCombo_currentIndexChanged(int index) +>>>>>>> 007008d (Remove enum and change naming conventions.) { switch (index) { case 0: - ui_.TestingWidget->xAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + ui_.TestingWidget->xAxis()->setTitleLabelRotation(0.0); break; case 1: - ui_.TestingWidget->xAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + ui_.TestingWidget->xAxis()->setTitleLabelRotation(90.0); break; default: +<<<<<<< HEAD throw(std::runtime_error("Unhandled text alignment mode.\n")); +======= + throw(std::runtime_error("Unhandled text rotation.")); +>>>>>>> 007008d (Remove enum and change naming conventions.) } } -void MainWindow::on_YLabelAlignmentCombo_currentIndexChanged(int index) +void MainWindow::on_YLabelRotationCombo_currentIndexChanged(int index) { switch (index) { case 0: - ui_.TestingWidget->yAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + ui_.TestingWidget->yAxis()->setTitleLabelRotation(0.0); break; case 1: - ui_.TestingWidget->yAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + ui_.TestingWidget->yAxis()->setTitleLabelRotation(90.0); break; default: +<<<<<<< HEAD throw(std::runtime_error("Unhandled text alignment mode.\n")); +======= + throw(std::runtime_error("Unhandled text rotation.")); +>>>>>>> 007008d (Remove enum and change naming conventions.) } } -void MainWindow::on_ZLabelAlignmentCombo_currentIndexChanged(int index) +void MainWindow::on_ZLabelRotationCombo_currentIndexChanged(int index) { switch (index) { case 0: - ui_.TestingWidget->zAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Horizontal); + ui_.TestingWidget->zAxis()->setTitleLabelRotation(0.0); break; case 1: - ui_.TestingWidget->zAxis()->setTitleLabelAlignment(Mildred::TextAlignment::Vertical); + ui_.TestingWidget->zAxis()->setTitleLabelRotation(90.0); break; default: +<<<<<<< HEAD throw(std::runtime_error("Unhandled text alignment mode.\n")); +======= + throw(std::runtime_error("Unhandled text Rotation mode.")); +>>>>>>> 007008d (Remove enum and change naming conventions.) } } \ No newline at end of file diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index 3c015a6..15db723 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -466,16 +466,20 @@ void AxisEntity::setType(AxisType type) } } -//! Define title alignment +//! Define title rotation /*! - * Set the alignment of the title label to @param alignment. + * Set the rotation of the title label to @param rotation. */ -void AxisEntity::setTitleLabelAlignment(Mildred::TextAlignment alignment) { axisTitleEntity_->setTextAlignment(alignment); } +void AxisEntity::setTitleLabelRotation(double rotation) { axisTitleEntity_->setRotation(rotation); } -void AxisEntity::setTickLabelAlignment(Mildred::TextAlignment alignment) +//! Define tick label rotation +/*! + * Set the rotation of the tick labels to @param rotation. + */ +void AxisEntity::setTickLabelRotation(double rotation) { for (auto &entity : tickLabelEntities_) - entity->setTextAlignment(alignment); + entity->setRotation(rotation); } //! Define direction diff --git a/src/entities/axis.h b/src/entities/axis.h index 29c88d7..79dc454 100644 --- a/src/entities/axis.h +++ b/src/entities/axis.h @@ -114,10 +114,10 @@ class AxisEntity : public Qt3DCore::QEntity public: // Set axis type void setType(AxisType type); - // Set title label alignment - void setTitleLabelAlignment(Mildred::TextAlignment alignment); - // Set tick label alignment - void setTickLabelAlignment(Mildred::TextAlignment alignment); + // Set title label rotation + void setTitleLabelRotation(double rotation); + // Set tick label rotation + void setTickLabelRotation(double rotation); // Set explicit direction void setDirection(QVector3D v); // Return explicit direction diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 4da18bb..46a15c2 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -87,10 +87,10 @@ void TextEntity::updateTranslation() positionalTransform_->setTranslation(anchorPosition_); // Set the rotation about z. - positionalTransform_->setRotationZ(rotationZFromAlignment()); + positionalTransform_->setRotationZ(rotation_); // Set the text translation vector so that the defined anchor point is located at {0,0,0} - auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth()); + auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth(), rotation_); auto v = QVector3D(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); textTransform_->setTranslation(v + translation); @@ -146,28 +146,14 @@ void TextEntity::setAnchorPosition(QVector3D p) updateTranslation(); } -//! Set alignment of text -void TextEntity::setTextAlignment(Mildred::TextAlignment alignment) +//! Set rotation of text +void TextEntity::setRotation(double rotation) { - alignment_ = alignment; + rotation_ = rotation; updateTranslation(); } -//! Gets the required rotation, around z, in degrees, to achieve the current alignment. -float TextEntity::rotationZFromAlignment() -{ - switch (alignment_) - { - case (Mildred::TextAlignment::Horizontal): - return 0.0; - case (Mildred::TextAlignment::Vertical): - return 90.0; - default: - throw(std::runtime_error("Unhandled text alignment mode.")); - } -} - //! Return simple bounding cuboid for text, along with baseline descent from font metrics /*! * Calculates a bounding cuboid in the XY plane for the specified @param font and @param text. The @param depth is applied in @@ -224,4 +210,4 @@ std::pair TextEntity::boundingCuboid(const QFont &font, const cuboid.translate(-QVector3D(cuboid.xExtent() * anchorFrac.x(), cuboid.yExtent() * anchorFrac.y(), 0.0)); return {cuboid, meshTranslation}; -} +} \ No newline at end of file diff --git a/src/entities/text.h b/src/entities/text.h index ef57e1d..2f76217 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -11,12 +11,6 @@ namespace Mildred { -enum class TextAlignment -{ - Horizontal, - Vertical -}; - //! TextEntity represents a 3D text entity /*! * TextEntity represents a renderable entity displaying a specified string with a given transform. @@ -30,8 +24,8 @@ class TextEntity : public Qt3DCore::QEntity private: // Collective positional transform Qt3DCore::QTransform *positionalTransform_{nullptr}; - // Alignment - Mildred::TextAlignment alignment_{Mildred::TextAlignment::Horizontal}; + // Rotation + double rotation_{0.0}; // Material RenderableMaterial *material_{nullptr}; // Main entity @@ -74,9 +68,7 @@ class TextEntity : public Qt3DCore::QEntity // Set anchor position void setAnchorPosition(QVector3D p); // Set rotation - void setTextAlignment(Mildred::TextAlignment alignment); - // Get required rotation in degrees - float rotationZFromAlignment(); + void setRotation(double rotation); // Return simple bounding cuboid for text, along with baseline descent from font metrics static std::pair boundingCuboid(const QFont &font, const QString &text, float depth = 0.1f); // Return bounding cuboid with translation and anchor point applied, and required translation vector for text mesh From 093c5bc47bc067be136e6f8451d6c3e77468255e Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Sun, 28 Aug 2022 14:47:29 +0100 Subject: [PATCH 10/16] Add overloaded addVertex() function for convenience. --- src/entities/line.cpp | 7 +++++++ src/entities/line.h | 1 + src/entities/text.cpp | 10 +++++----- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/entities/line.cpp b/src/entities/line.cpp index 3c46c10..0a24652 100644 --- a/src/entities/line.cpp +++ b/src/entities/line.cpp @@ -65,6 +65,13 @@ LineEntity::LineEntity(Qt3DCore::QNode *parent, Qt3DRender::QGeometryRenderer::P */ void LineEntity::addVertex(QVector3D v) { cachedVertices_.emplace_back(v); } +//! Append vertex to cached data +/*! + * Append the vertex defined by the coordinates @param x, @param y, and @param z to the cached vertices. The buffer objects (and hence the display primitive) are not regenerated + * until a call to the finalise() method is made. + */ +void LineEntity::addVertex(double x, double y, double z) { cachedVertices_.emplace_back(QVector3D{float(x), float(y), float(z)});} + //! Append vertex and colour to cached data /*! * Append the vertex @param v to the cached vertices, with the specific @param colour. The buffer objects (and hence the display diff --git a/src/entities/line.h b/src/entities/line.h index 3e9ab13..169d70d 100644 --- a/src/entities/line.h +++ b/src/entities/line.h @@ -45,6 +45,7 @@ class LineEntity : public Qt3DCore::QEntity public: // Append vertices to cached data void addVertex(QVector3D v); + void addVertex(double x, double y, double z); void addVertex(QVector3D v, QColor colour); void addVertices(const std::vector &vertices); // Append indices to cached data diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 46a15c2..47ddf25 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -98,11 +98,11 @@ void TextEntity::updateTranslation() if (boundingBoxEntity_) { boundingBoxEntity_->clear(); - boundingBoxEntity_->addVertex({textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0}); - boundingBoxEntity_->addVertex({textCuboid.upperRightFront().x(), textCuboid.lowerLeftBack().y(), 0}); - boundingBoxEntity_->addVertex({textCuboid.upperRightFront().x(), textCuboid.upperRightFront().y(), 0}); - boundingBoxEntity_->addVertex({textCuboid.lowerLeftBack().x(), textCuboid.upperRightFront().y(), 0}); - boundingBoxEntity_->addVertex({textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0}); + boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); + boundingBoxEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.lowerLeftBack().y(), 0.0); + boundingBoxEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.upperRightFront().y(), 0.0); + boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.upperRightFront().y(), 0.0); + boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); boundingBoxEntity_->setBasicIndices(); boundingBoxEntity_->finalise(); } From ce891201a3ef1d61580637f42dff16973b85a93b Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Sun, 28 Aug 2022 14:48:29 +0100 Subject: [PATCH 11/16] Fix typo. --- src/entities/line.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entities/line.cpp b/src/entities/line.cpp index 0a24652..e528d43 100644 --- a/src/entities/line.cpp +++ b/src/entities/line.cpp @@ -83,7 +83,7 @@ void LineEntity::addVertex(QVector3D v, QColor colour) cachedVertexColours_.emplace_back(colour); } -//! Append verteices to cached data +//! Append vertices to cached data /*! * Append the vector of @param vertices to the cached vertices. The buffer objects (and hence the display primitive) are not * regenerated until a call to the finalise() method is made. From f8eae876d88f6b3fd4f05bb31d257e753973c506 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Wed, 7 Sep 2022 13:39:11 +0100 Subject: [PATCH 12/16] Fix merge errors. --- examples/basic/mainwindow_funcs.cpp | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/examples/basic/mainwindow_funcs.cpp b/examples/basic/mainwindow_funcs.cpp index 0b7eda2..c4bdee6 100644 --- a/examples/basic/mainwindow_funcs.cpp +++ b/examples/basic/mainwindow_funcs.cpp @@ -61,7 +61,6 @@ MainWindow::MainWindow() : QMainWindow() ui_.TestingWidget->showAllData(); }; -<<<<<<< HEAD void MainWindow::on_mouseCoordStyleCombo_currentIndexChanged(int index) { switch (index) @@ -88,10 +87,7 @@ void MainWindow::setExternalMouseCoordinatesText(QPointF p) ui_.externalMouseCoordLabel->setText(QString("%1 %2").arg(QString::number(p.x()), QString::number(p.y()))); } -void MainWindow::on_XLabelAlignmentCombo_currentIndexChanged(int index) -======= void MainWindow::on_XLabelRotationCombo_currentIndexChanged(int index) ->>>>>>> 007008d (Remove enum and change naming conventions.) { switch (index) { @@ -102,11 +98,7 @@ void MainWindow::on_XLabelRotationCombo_currentIndexChanged(int index) ui_.TestingWidget->xAxis()->setTitleLabelRotation(90.0); break; default: -<<<<<<< HEAD - throw(std::runtime_error("Unhandled text alignment mode.\n")); -======= - throw(std::runtime_error("Unhandled text rotation.")); ->>>>>>> 007008d (Remove enum and change naming conventions.) + throw(std::runtime_error("Unhandled text rotation.\n")); } } @@ -121,11 +113,7 @@ void MainWindow::on_YLabelRotationCombo_currentIndexChanged(int index) ui_.TestingWidget->yAxis()->setTitleLabelRotation(90.0); break; default: -<<<<<<< HEAD - throw(std::runtime_error("Unhandled text alignment mode.\n")); -======= - throw(std::runtime_error("Unhandled text rotation.")); ->>>>>>> 007008d (Remove enum and change naming conventions.) + throw(std::runtime_error("Unhandled text rotation.\n")); } } @@ -140,10 +128,6 @@ void MainWindow::on_ZLabelRotationCombo_currentIndexChanged(int index) ui_.TestingWidget->zAxis()->setTitleLabelRotation(90.0); break; default: -<<<<<<< HEAD - throw(std::runtime_error("Unhandled text alignment mode.\n")); -======= - throw(std::runtime_error("Unhandled text Rotation mode.")); ->>>>>>> 007008d (Remove enum and change naming conventions.) + throw(std::runtime_error("Unhandled text Rotation mode.\n")); } -} \ No newline at end of file +} From 60f09cc561b86692d5d4f303aa1ffbdc1a118d62 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Wed, 7 Sep 2022 13:52:11 +0100 Subject: [PATCH 13/16] Partial implementation of rotated labels (bounding cuboid limits still calculated incorrectly). --- src/classes/cuboid.cpp | 30 ++++++++++++++++++++++++++++++ src/classes/cuboid.h | 2 ++ src/entities/axis.cpp | 20 ++++++++------------ src/entities/line.cpp | 9 ++++++--- src/entities/text.cpp | 15 +++++++++++---- src/entities/text.h | 6 +++++- 6 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/classes/cuboid.cpp b/src/classes/cuboid.cpp index 5406644..beed85e 100644 --- a/src/classes/cuboid.cpp +++ b/src/classes/cuboid.cpp @@ -1,4 +1,5 @@ #include "classes/cuboid.h" +#include using namespace Mildred; @@ -195,3 +196,32 @@ float Cuboid::zExtent() const //! Return vector of extents QVector3D Cuboid::extents() const { return {xExtent(), yExtent(), zExtent()}; } + +//! Return bounding box cuboid required for rotated original cuboid (in degrees, around z-axis) +/*! + * Returns the bounding cuboid required to contain the original cuboid rotated @param rotation + * @return + */ +Cuboid Cuboid::zRotatedBoundingCuboid(QVector3D rotationOrigin, double thetaZ) const +{ + QMatrix4x4 m; + m.rotate(thetaZ, QVector3D{0.0, 0.0, 1.0}); + std::array corners; + corners[0] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y()); + corners[1] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y()); + corners[2] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y()); + corners[3] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y()); + + Cuboid rotated; + for (const auto &p : corners) + { + auto newP = m.map(p); + rotated.expand(newP.x() + rotationOrigin.x(), newP.y() + rotationOrigin.y(), {}); + } + + // Set Z extents + rotated.v1z_ = v1z_; + rotated.v2z_ = v2z_; + + return rotated; +} diff --git a/src/classes/cuboid.h b/src/classes/cuboid.h index 5bb24e8..6d4aee1 100644 --- a/src/classes/cuboid.h +++ b/src/classes/cuboid.h @@ -60,5 +60,7 @@ class Cuboid float zExtent() const; // Return vector of extents QVector3D extents() const; + // Return bounding box cuboid required for rotated original cuboid (in degrees, around z-axis) + Cuboid zRotatedBoundingCuboid(QVector3D rotationOrigin, double thetaZ) const; }; } // namespace Mildred \ No newline at end of file diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index 15db723..3f733c1 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -594,11 +594,7 @@ Cuboid AxisEntity::createTickAndLabelEntities(const std::vectorsetText(QString::number(v)); (*tickLabelEntity) ->setAnchorPosition(axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics_.tickLabelPixelGap())); - boundingCuboid.expand(TextEntity::boundingCuboid( - metrics_.axisTickLabelFont(), QString::number(v), - {axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics_.tickLabelPixelGap())}, - labelAnchorPoint_) - .first); + boundingCuboid.expand((*tickLabelEntity)->boundingCuboid(metrics_.axisTickLabelFont()).first); ++tickLabelEntity; } else @@ -713,7 +709,7 @@ Cuboid AxisEntity::boundingCuboid(const MildredMetrics &metrics) const cuboid.expand( TextEntity::boundingCuboid(metrics.axisTickLabelFont(), QString::number(v), axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics.tickLabelPixelGap()), - labelAnchorPoint_) + labelAnchorPoint_, 0.0) .first); } else @@ -721,12 +717,12 @@ Cuboid AxisEntity::boundingCuboid(const MildredMetrics &metrics) const } // -- Title if (!axisTitleEntity_->text().isEmpty()) - cuboid.expand(TextEntity::boundingCuboid(metrics.axisTitleFont(), axisTitleEntity_->text(), - direction_ * metrics.displayVolumeExtent()[axisDirectionIndex_] * 0.5 + - tickDirection_ * - (cuboid.extents()[tickDirectionIndex_] + metrics.tickLabelPixelGap()), - labelAnchorPoint_) - .first); + cuboid.expand( + axisTitleEntity_ + ->boundingCuboid(metrics.axisTitleFont(), + direction_ * metrics.displayVolumeExtent()[axisDirectionIndex_] * 0.5 + + tickDirection_ * (cuboid.extents()[tickDirectionIndex_] + metrics.tickLabelPixelGap())) + .first); return cuboid; } diff --git a/src/entities/line.cpp b/src/entities/line.cpp index e528d43..e8a8a3e 100644 --- a/src/entities/line.cpp +++ b/src/entities/line.cpp @@ -67,10 +67,13 @@ void LineEntity::addVertex(QVector3D v) { cachedVertices_.emplace_back(v); } //! Append vertex to cached data /*! - * Append the vertex defined by the coordinates @param x, @param y, and @param z to the cached vertices. The buffer objects (and hence the display primitive) are not regenerated - * until a call to the finalise() method is made. + * Append the vertex defined by the coordinates @param x, @param y, and @param z to the cached vertices. The buffer objects (and + * hence the display primitive) are not regenerated until a call to the finalise() method is made. */ -void LineEntity::addVertex(double x, double y, double z) { cachedVertices_.emplace_back(QVector3D{float(x), float(y), float(z)});} +void LineEntity::addVertex(double x, double y, double z) +{ + cachedVertices_.emplace_back(QVector3D{float(x), float(y), float(z)}); +} //! Append vertex and colour to cached data /*! diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 47ddf25..13fc9e2 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -4,7 +4,7 @@ using namespace Mildred; // Enables showing of bounding boxes around Text Entities. -constexpr auto showBoundingBoxes = false; +constexpr auto showBoundingBoxes = true; //! Construct a new text entity /*! @@ -90,7 +90,7 @@ void TextEntity::updateTranslation() positionalTransform_->setRotationZ(rotation_); // Set the text translation vector so that the defined anchor point is located at {0,0,0} - auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth(), rotation_); + auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth()); auto v = QVector3D(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); textTransform_->setTranslation(v + translation); @@ -154,6 +154,12 @@ void TextEntity::setRotation(double rotation) updateTranslation(); } +// Return bounding cuboid and anchor translation vector for current text and positioning +std::pair TextEntity::boundingCuboid(const QFont &font, std::optional newAnchorPosition) const +{ + return boundingCuboid(font, text(), newAnchorPosition.value_or(anchorPosition_), anchorPoint_, rotation_, 0.01); +} + //! Return simple bounding cuboid for text, along with baseline descent from font metrics /*! * Calculates a bounding cuboid in the XY plane for the specified @param font and @param text. The @param depth is applied in @@ -182,7 +188,7 @@ std::pair TextEntity::boundingCuboid(const QFont &font, const QStri * should be applied to the mesh to be rendered in order to get the correct positioning. */ std::pair TextEntity::boundingCuboid(const QFont &font, const QString &text, QVector3D anchorPosition, - MildredMetrics::AnchorPoint anchorPoint, float depth) + MildredMetrics::AnchorPoint anchorPoint, double rotation, float depth) { // Get basic bounding cuboid for the text auto [cuboid, descent] = boundingCuboid(font, text, depth); @@ -209,5 +215,6 @@ std::pair TextEntity::boundingCuboid(const QFont &font, const auto anchorFrac = MildredMetrics::anchorLocation(anchorPoint); cuboid.translate(-QVector3D(cuboid.xExtent() * anchorFrac.x(), cuboid.yExtent() * anchorFrac.y(), 0.0)); - return {cuboid, meshTranslation}; + // Return the bounding cuboid (accounting for rotation) + return {cuboid.zRotatedBoundingCuboid(anchorPosition, rotation), meshTranslation}; } \ No newline at end of file diff --git a/src/entities/text.h b/src/entities/text.h index 2f76217..b66d81e 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -69,10 +69,14 @@ class TextEntity : public Qt3DCore::QEntity void setAnchorPosition(QVector3D p); // Set rotation void setRotation(double rotation); + // Return bounding cuboid and anchor translation vector for current text and positioning + std::pair boundingCuboid(const QFont &font, + std::optional newAnchorPosition = std::nullopt) const; // Return simple bounding cuboid for text, along with baseline descent from font metrics static std::pair boundingCuboid(const QFont &font, const QString &text, float depth = 0.1f); // Return bounding cuboid with translation and anchor point applied, and required translation vector for text mesh static std::pair boundingCuboid(const QFont &font, const QString &text, QVector3D anchorPosition, - MildredMetrics::AnchorPoint anchorPoint, float depth = 0.1f); + MildredMetrics::AnchorPoint anchorPoint, double rotation, + float depth = 0.1f); }; } // namespace Mildred \ No newline at end of file From ad0d55f7753faf1993e105556eb6c6876a1edef5 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Wed, 7 Sep 2022 14:13:40 +0100 Subject: [PATCH 14/16] Rename axis main label entity and functions. --- examples/basic/mainwindow.ui | 12 +++---- examples/basic/mainwindow_funcs.cpp | 14 ++++---- src/entities/axis.cpp | 50 ++++++++++++++--------------- src/entities/axis.h | 16 ++++----- src/scenegraph.cpp | 30 ++++++++--------- src/widget.h | 8 ++--- 6 files changed, 66 insertions(+), 64 deletions(-) diff --git a/examples/basic/mainwindow.ui b/examples/basic/mainwindow.ui index 6624843..79be446 100644 --- a/examples/basic/mainwindow.ui +++ b/examples/basic/mainwindow.ui @@ -107,9 +107,9 @@ - + - + 0 0 @@ -138,9 +138,9 @@ - + - + 0 0 @@ -169,9 +169,9 @@ - + - + 0 0 diff --git a/examples/basic/mainwindow_funcs.cpp b/examples/basic/mainwindow_funcs.cpp index c4bdee6..4943f67 100644 --- a/examples/basic/mainwindow_funcs.cpp +++ b/examples/basic/mainwindow_funcs.cpp @@ -15,12 +15,14 @@ MainWindow::MainWindow() : QMainWindow() connect(ui_.FlatViewCheck, SIGNAL(clicked(bool)), ui_.TestingWidget, SLOT(setFlatView(bool))); // Axes - ui_.XTitleEdit->setText(ui_.TestingWidget->xAxis()->titleText()); - connect(ui_.XTitleEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setXAxisTitle(const QString &))); - ui_.YTitleEdit->setText(ui_.TestingWidget->yAxis()->titleText()); - connect(ui_.YTitleEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setYAxisTitle(const QString &))); - ui_.ZTitleEdit->setText(ui_.TestingWidget->zAxis()->titleText()); - connect(ui_.ZTitleEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setZAxisTitle(const QString &))); + // -- Titles + ui_.XLabelEdit->setText(ui_.TestingWidget->xAxis()->labelText()); + connect(ui_.XLabelEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setXAxisLabel(const QString &))); + ui_.YLabelEdit->setText(ui_.TestingWidget->yAxis()->labelText()); + connect(ui_.YLabelEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setYAxisLabel(const QString &))); + ui_.ZLabelEdit->setText(ui_.TestingWidget->zAxis()->labelText()); + connect(ui_.ZLabelEdit, SIGNAL(textChanged(const QString &)), ui_.TestingWidget, SLOT(setZAxisLabel(const QString &))); + // -- Visibility ui_.XVisibleCheck->setChecked(ui_.TestingWidget->xAxis()->isEnabled()); connect(ui_.XVisibleCheck, SIGNAL(toggled(bool)), ui_.TestingWidget->xAxis(), SLOT(setEnabled(bool))); ui_.YVisibleCheck->setChecked(ui_.TestingWidget->yAxis()->isEnabled()); diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index 3f733c1..14be7ba 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -23,8 +23,8 @@ AxisEntity::AxisEntity(Qt3DCore::QNode *parent, AxisType type, const MildredMetr axisBarEntity_ = new LineEntity(this); ticksEntity_ = new LineEntity(this, Qt3DRender::QGeometryRenderer::Lines); subTicksEntity_ = new LineEntity(this, Qt3DRender::QGeometryRenderer::Lines); - axisTitleEntity_ = new TextEntity(this, "Unnamed Axis"); - axisTitleEntity_->setAnchorPoint(MildredMetrics::AnchorPoint::TopMiddle); + labelEntity_ = new TextEntity(this, "Unnamed Axis"); + labelEntity_->setAnchorPoint(MildredMetrics::AnchorPoint::TopMiddle); // Assign material components axisBarMaterial_ = barMaterial; @@ -32,7 +32,7 @@ AxisEntity::AxisEntity(Qt3DCore::QNode *parent, AxisType type, const MildredMetr ticksEntity_->addComponent(axisBarMaterial_); subTicksEntity_->addComponent(axisBarMaterial_); labelMaterial_ = labelMaterial; - axisTitleEntity_->setMaterial(labelMaterial_); + labelEntity_->setMaterial(labelMaterial_); // Update data dependent on the axis type setType(type_); @@ -303,24 +303,24 @@ void AxisEntity::shiftLimitsByPixels(int pixelDelta) //! Return whether the axis is logarithmic bool AxisEntity::isLogarithmic() const { return logarithmic_; } -//! Set title text +//! Set label text /*! - * Sets the display title of the axis to @param text, modifying the underlying @class TextEntity object. + * Sets the display label of the axis to @param text, modifying the underlying @class TextEntity object. */ -void AxisEntity::setTitleText(const QString &text) +void AxisEntity::setLabelText(const QString &text) { - assert(axisTitleEntity_); - axisTitleEntity_->setText(text); + assert(labelEntity_); + labelEntity_->setText(text); } -//! Return title text +//! Return label text /*! - * Returns the current text displayed as the axis title. + * Returns the current text displayed as the axis label. */ -QString AxisEntity::titleText() const +QString AxisEntity::labelText() const { - assert(axisTitleEntity_); - return axisTitleEntity_->text(); + assert(labelEntity_); + return labelEntity_->text(); } //! Set axis minimum @@ -468,9 +468,9 @@ void AxisEntity::setType(AxisType type) //! Define title rotation /*! - * Set the rotation of the title label to @param rotation. + * Set the rotation of the main label to @param rotation (degrees). */ -void AxisEntity::setTitleLabelRotation(double rotation) { axisTitleEntity_->setRotation(rotation); } +void AxisEntity::setLabelRotation(double rotation) { labelEntity_->setRotation(rotation); } //! Define tick label rotation /*! @@ -648,17 +648,17 @@ void AxisEntity::recreate() auto tickLabelBounds = createTickAndLabelEntities(ticks); // Axis title - if (!axisTitleEntity_->text().isEmpty()) + if (!labelEntity_->text().isEmpty()) { - axisTitleEntity_->setEnabled(true); - axisTitleEntity_->setFont(metrics_.axisTitleFont()); - axisTitleEntity_->setAnchorPoint(labelAnchorPoint_); - axisTitleEntity_->setAnchorPosition( - direction_ * metrics_.displayVolumeExtent()[axisDirectionIndex_] * 0.5 + - tickDirection_ * (tickLabelBounds.extents()[tickDirectionIndex_] + metrics_.tickLabelPixelGap())); + labelEntity_->setEnabled(true); + labelEntity_->setFont(metrics_.axisTitleFont()); + labelEntity_->setAnchorPoint(labelAnchorPoint_); + labelEntity_->setAnchorPosition(direction_ * metrics_.displayVolumeExtent()[axisDirectionIndex_] * 0.5 + + tickDirection_ * + (tickLabelBounds.extents()[tickDirectionIndex_] + metrics_.tickLabelPixelGap())); } else - axisTitleEntity_->setEnabled(false); + labelEntity_->setEnabled(false); ticksEntity_->setBasicIndices(); ticksEntity_->finalise(); @@ -716,9 +716,9 @@ Cuboid AxisEntity::boundingCuboid(const MildredMetrics &metrics) const cuboid.expand({axisPos, axisPos + tickDirection_ * metrics_.tickPixelSize() * 0.5}); } // -- Title - if (!axisTitleEntity_->text().isEmpty()) + if (!labelEntity_->text().isEmpty()) cuboid.expand( - axisTitleEntity_ + labelEntity_ ->boundingCuboid(metrics.axisTitleFont(), direction_ * metrics.displayVolumeExtent()[axisDirectionIndex_] * 0.5 + tickDirection_ * (cuboid.extents()[tickDirectionIndex_] + metrics.tickLabelPixelGap())) diff --git a/src/entities/axis.h b/src/entities/axis.h index 79dc454..29e4683 100644 --- a/src/entities/axis.h +++ b/src/entities/axis.h @@ -78,10 +78,6 @@ class AxisEntity : public Qt3DCore::QEntity void shiftLimitsByPixels(int pixelDelta); // Return whether the axis is logarithmic bool isLogarithmic() const; - // Set title text - void setTitleText(const QString &text); - // Return title text - QString titleText() const; public slots: // Set axis minimum @@ -114,8 +110,12 @@ class AxisEntity : public Qt3DCore::QEntity public: // Set axis type void setType(AxisType type); - // Set title label rotation - void setTitleLabelRotation(double rotation); + // Set label text + void setLabelText(const QString &text); + // Return label text + QString labelText() const; + // Set main label rotation + void setLabelRotation(double rotation); // Set tick label rotation void setTickLabelRotation(double rotation); // Set explicit direction @@ -143,8 +143,8 @@ class AxisEntity : public Qt3DCore::QEntity LineEntity *ticksEntity_{nullptr}, *subTicksEntity_{nullptr}; // Tick labels std::vector tickLabelEntities_; - // Axis title - TextEntity *axisTitleEntity_{nullptr}; + // Main label + TextEntity *labelEntity_{nullptr}; private: // Create / update ticks and labels at specified axis values, returning their bounding cuboid diff --git a/src/scenegraph.cpp b/src/scenegraph.cpp index a6b5613..c8b9ce9 100644 --- a/src/scenegraph.cpp +++ b/src/scenegraph.cpp @@ -77,7 +77,7 @@ void MildredWidget::createSceneGraph() RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome); xAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255)); xAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Horizontal, metrics_, xAxisBarMaterial, xAxisLabelMaterial); - xAxis_->setTitleText("X"); + xAxis_->setLabelText("X"); connect(xAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics())); connect(xAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics())); @@ -90,7 +90,7 @@ void MildredWidget::createSceneGraph() RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome); yAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255)); yAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Vertical, metrics_, yAxisBarMaterial, yAxisLabelMaterial); - yAxis_->setTitleText("Y"); + yAxis_->setLabelText("Y"); connect(yAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics())); connect(yAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics())); @@ -103,7 +103,7 @@ void MildredWidget::createSceneGraph() RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome); zAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255)); zAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Depth, metrics_, zAxisBarMaterial, zAxisLabelMaterial); - zAxis_->setTitleText("Z"); + zAxis_->setLabelText("Z"); connect(zAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics())); connect(zAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics())); zAxis_->setEnabled(false); @@ -244,36 +244,36 @@ void MildredWidget::showAllData() updateMetrics(); } -//! Change the text of the x-axis title. +//! Change the text of the x-axis label. /*! - * Set the displayed text of the x-axis title to @param title. Metrics are updated after the change. + * Set the displayed text of the x-axis label to @param text. Metrics are updated after the change. */ -void MildredWidget::setXAxisTitle(const QString &title) +void MildredWidget::setXAxisLabel(const QString &text) { assert(xAxis_); - xAxis_->setTitleText(title); + xAxis_->setLabelText(text); updateMetrics(); } -//! Change the text of the y-axis title. +//! Change the text of the y-axis label. /*! - * Set the displayed text of the y-axis title to @param title. Metrics are updated after the change. + * Set the displayed text of the y-axis title to @param text. Metrics are updated after the change. */ -void MildredWidget::setYAxisTitle(const QString &title) +void MildredWidget::setYAxisLabel(const QString &text) { assert(yAxis_); - yAxis_->setTitleText(title); + yAxis_->setLabelText(text); updateMetrics(); } -//! Change the text of the z-axis title. +//! Change the text of the z-axis label. /*! - * Set the displayed text of the z-axis title to @param title. Metrics are updated after the change. + * Set the displayed text of the z-axis label to @param text. Metrics are updated after the change. */ -void MildredWidget::setZAxisTitle(const QString &title) +void MildredWidget::setZAxisLabel(const QString &text) { assert(zAxis_); - zAxis_->setTitleText(title); + zAxis_->setLabelText(text); updateMetrics(); } diff --git a/src/widget.h b/src/widget.h index 07b1d22..d5771fa 100644 --- a/src/widget.h +++ b/src/widget.h @@ -151,10 +151,10 @@ class MildredWidget : public QWidget void resetView(); // Show all data in view void showAllData(); - // Axis Titles - void setXAxisTitle(const QString &title); - void setYAxisTitle(const QString &title); - void setZAxisTitle(const QString &title); + // Axis Main Labels + void setXAxisLabel(const QString &text); + void setYAxisLabel(const QString &text); + void setZAxisLabel(const QString &text); // Debug Objects void setSceneCuboidEnabled(bool enabled); From db50eb19ffba985790dac81cb3821d0c4ebbe670 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Wed, 7 Sep 2022 14:29:34 +0100 Subject: [PATCH 15/16] Use pass-through on main widget to set axis label rotations. --- examples/basic/mainwindow.h | 3 -- examples/basic/mainwindow.ui | 47 +++++++++++++++++++++-- examples/basic/mainwindow_funcs.cpp | 58 +++-------------------------- src/entities/axis.cpp | 3 ++ src/entities/axis.h | 2 + src/entities/text.cpp | 3 ++ src/entities/text.h | 4 +- src/scenegraph.cpp | 33 ++++++++++++++++ src/widget.h | 3 ++ 9 files changed, 95 insertions(+), 61 deletions(-) diff --git a/examples/basic/mainwindow.h b/examples/basic/mainwindow.h index 7b2772b..3e37518 100644 --- a/examples/basic/mainwindow.h +++ b/examples/basic/mainwindow.h @@ -15,9 +15,6 @@ class MainWindow : public QMainWindow */ private slots: void on_mouseCoordStyleCombo_currentIndexChanged(int index); - void on_XLabelRotationCombo_currentIndexChanged(int index); - void on_YLabelRotationCombo_currentIndexChanged(int index); - void on_ZLabelRotationCombo_currentIndexChanged(int index); public slots: // Set mouse coordinates in external label. diff --git a/examples/basic/mainwindow.ui b/examples/basic/mainwindow.ui index 79be446..220774c 100644 --- a/examples/basic/mainwindow.ui +++ b/examples/basic/mainwindow.ui @@ -124,7 +124,20 @@ - + + + 0 + + + -180.000000000000000 + + + 180.000000000000000 + + + 5.000000000000000 + + @@ -155,7 +168,20 @@ - + + + 0 + + + -180.000000000000000 + + + 180.000000000000000 + + + 5.000000000000000 + + @@ -186,7 +212,20 @@ - + + + 0 + + + -180.000000000000000 + + + 180.000000000000000 + + + 5.000000000000000 + + @@ -247,7 +286,7 @@ 0 0 956 - 22 + 23 diff --git a/examples/basic/mainwindow_funcs.cpp b/examples/basic/mainwindow_funcs.cpp index 4943f67..b75daca 100644 --- a/examples/basic/mainwindow_funcs.cpp +++ b/examples/basic/mainwindow_funcs.cpp @@ -29,6 +29,11 @@ MainWindow::MainWindow() : QMainWindow() connect(ui_.YVisibleCheck, SIGNAL(toggled(bool)), ui_.TestingWidget->yAxis(), SLOT(setEnabled(bool))); ui_.ZVisibleCheck->setChecked(ui_.TestingWidget->zAxis()->isEnabled()); connect(ui_.ZVisibleCheck, SIGNAL(toggled(bool)), ui_.TestingWidget->zAxis(), SLOT(setEnabled(bool))); + // -- Rotation + ui_.XLabelRotationSpin->setValue(ui_.TestingWidget->xAxis()->labelRotation()); + connect(ui_.XLabelRotationSpin, SIGNAL(valueChanged(double)), ui_.TestingWidget, SLOT(setXAxisLabelRotation(double))); + connect(ui_.YLabelRotationSpin, SIGNAL(valueChanged(double)), ui_.TestingWidget, SLOT(setYAxisLabelRotation(double))); + connect(ui_.ZLabelRotationSpin, SIGNAL(valueChanged(double)), ui_.TestingWidget, SLOT(setZAxisLabelRotation(double))); // Mouse Coordinates ui_.mouseCoordStyleCombo->addItem(QString("Fixed Anchor")); @@ -37,14 +42,6 @@ MainWindow::MainWindow() : QMainWindow() ui_.externalMouseCoordLabel->setVisible(false); connect(ui_.TestingWidget, SIGNAL(mouseCoordChanged(QPointF)), this, SLOT(setExternalMouseCoordinatesText(QPointF))); - // Axis Label Rotation - ui_.XLabelRotationCombo->addItem(QString("Horizontal")); - ui_.XLabelRotationCombo->addItem(QString("Vertical")); - ui_.YLabelRotationCombo->addItem(QString("Horizontal")); - ui_.YLabelRotationCombo->addItem(QString("Vertical")); - ui_.ZLabelRotationCombo->addItem(QString("Horizontal")); - ui_.ZLabelRotationCombo->addItem(QString("Vertical")); - // Data const auto nPoints = 1000; const auto delta = 2.0 * M_PI / nPoints; @@ -88,48 +85,3 @@ void MainWindow::setExternalMouseCoordinatesText(QPointF p) { ui_.externalMouseCoordLabel->setText(QString("%1 %2").arg(QString::number(p.x()), QString::number(p.y()))); } - -void MainWindow::on_XLabelRotationCombo_currentIndexChanged(int index) -{ - switch (index) - { - case 0: - ui_.TestingWidget->xAxis()->setTitleLabelRotation(0.0); - break; - case 1: - ui_.TestingWidget->xAxis()->setTitleLabelRotation(90.0); - break; - default: - throw(std::runtime_error("Unhandled text rotation.\n")); - } -} - -void MainWindow::on_YLabelRotationCombo_currentIndexChanged(int index) -{ - switch (index) - { - case 0: - ui_.TestingWidget->yAxis()->setTitleLabelRotation(0.0); - break; - case 1: - ui_.TestingWidget->yAxis()->setTitleLabelRotation(90.0); - break; - default: - throw(std::runtime_error("Unhandled text rotation.\n")); - } -} - -void MainWindow::on_ZLabelRotationCombo_currentIndexChanged(int index) -{ - switch (index) - { - case 0: - ui_.TestingWidget->zAxis()->setTitleLabelRotation(0.0); - break; - case 1: - ui_.TestingWidget->zAxis()->setTitleLabelRotation(90.0); - break; - default: - throw(std::runtime_error("Unhandled text Rotation mode.\n")); - } -} diff --git a/src/entities/axis.cpp b/src/entities/axis.cpp index 14be7ba..fe1e9ab 100644 --- a/src/entities/axis.cpp +++ b/src/entities/axis.cpp @@ -472,6 +472,9 @@ void AxisEntity::setType(AxisType type) */ void AxisEntity::setLabelRotation(double rotation) { labelEntity_->setRotation(rotation); } +//! Return main label rotation +double AxisEntity::labelRotation() const { return labelEntity_->rotation(); } + //! Define tick label rotation /*! * Set the rotation of the tick labels to @param rotation. diff --git a/src/entities/axis.h b/src/entities/axis.h index 29e4683..1f6efae 100644 --- a/src/entities/axis.h +++ b/src/entities/axis.h @@ -116,6 +116,8 @@ class AxisEntity : public Qt3DCore::QEntity QString labelText() const; // Set main label rotation void setLabelRotation(double rotation); + // Return label rotation + double labelRotation() const; // Set tick label rotation void setTickLabelRotation(double rotation); // Set explicit direction diff --git a/src/entities/text.cpp b/src/entities/text.cpp index 13fc9e2..d2b72cb 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -154,6 +154,9 @@ void TextEntity::setRotation(double rotation) updateTranslation(); } +//! Return rotation about anchor point +double TextEntity::rotation() const { return rotation_; } + // Return bounding cuboid and anchor translation vector for current text and positioning std::pair TextEntity::boundingCuboid(const QFont &font, std::optional newAnchorPosition) const { diff --git a/src/entities/text.h b/src/entities/text.h index b66d81e..53ec8ca 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -67,8 +67,10 @@ class TextEntity : public Qt3DCore::QEntity void setAnchorPoint(MildredMetrics::AnchorPoint anchor); // Set anchor position void setAnchorPosition(QVector3D p); - // Set rotation + // Set rotation about anchor point void setRotation(double rotation); + // Return rotation about anchor point + double rotation() const; // Return bounding cuboid and anchor translation vector for current text and positioning std::pair boundingCuboid(const QFont &font, std::optional newAnchorPosition = std::nullopt) const; diff --git a/src/scenegraph.cpp b/src/scenegraph.cpp index c8b9ce9..ebe1c9f 100644 --- a/src/scenegraph.cpp +++ b/src/scenegraph.cpp @@ -277,6 +277,39 @@ void MildredWidget::setZAxisLabel(const QString &text) updateMetrics(); } +//! Change the rotation of the x-axis label. +/*! + * Set the rotation of the x-axis label about its anchor point to @param theta degrees. Metrics are updated after the change. + */ +void MildredWidget::setXAxisLabelRotation(double theta) +{ + assert(xAxis_); + xAxis_->setLabelRotation(theta); + updateMetrics(); +} + +//! Change the rotation of the y-axis label. +/*! + * Set the rotation of the y-axis label about its anchor point to @param theta degrees. Metrics are updated after the change. + */ +void MildredWidget::setYAxisLabelRotation(double theta) +{ + assert(yAxis_); + yAxis_->setLabelRotation(theta); + updateMetrics(); +} + +//! Change the rotation of the z-axis label. +/*! + * Set the rotation of the z-axis label about its anchor point to @param theta degrees. Metrics are updated after the change. + */ +void MildredWidget::setZAxisLabelRotation(double theta) +{ + assert(zAxis_); + zAxis_->setLabelRotation(theta); + updateMetrics(); +} + //! Change the visibility of the scene debug cuboid. /*! * Sets whether the scene debug cuboid is enabled (visible), and which illustrates the current view volume extent. diff --git a/src/widget.h b/src/widget.h index d5771fa..518fc9a 100644 --- a/src/widget.h +++ b/src/widget.h @@ -155,6 +155,9 @@ class MildredWidget : public QWidget void setXAxisLabel(const QString &text); void setYAxisLabel(const QString &text); void setZAxisLabel(const QString &text); + void setXAxisLabelRotation(double theta); + void setYAxisLabelRotation(double theta); + void setZAxisLabelRotation(double theta); // Debug Objects void setSceneCuboidEnabled(bool enabled); From 1107d31662fdcd6b0c40a0920e0b7992dcdae4b7 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Wed, 7 Sep 2022 14:42:40 +0100 Subject: [PATCH 16/16] Rename box for cuboid. --- src/entities/text.cpp | 32 ++++++++++++++++---------------- src/entities/text.h | 8 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/entities/text.cpp b/src/entities/text.cpp index d2b72cb..9d49bba 100644 --- a/src/entities/text.cpp +++ b/src/entities/text.cpp @@ -3,8 +3,8 @@ using namespace Mildred; -// Enables showing of bounding boxes around Text Entities. -constexpr auto showBoundingBoxes = true; +// Enables showing of bounding cuboids around Text Entities. +constexpr auto showBoundingCuboids = true; //! Construct a new text entity /*! @@ -35,12 +35,12 @@ TextEntity::TextEntity(Qt3DCore::QNode *parent, QString text) : Qt3DCore::QEntit textTransform_->setScale3D({10.0f, 10.0f, 1.0f}); textEntity_->addComponent(textTransform_); - // Add bounding box entity - if (showBoundingBoxes) + // Add bounding cuboid entity + if (showBoundingCuboids) { - boundingBoxEntity_ = new LineEntity(this); - boundingBoxTransform_ = new Qt3DCore::QTransform(boundingBoxEntity_); - boundingBoxEntity_->addComponent(boundingBoxTransform_); + boundingCuboidEntity_ = new LineEntity(this); + boundingCuboidTransform_ = new Qt3DCore::QTransform(boundingCuboidEntity_); + boundingCuboidEntity_->addComponent(boundingCuboidTransform_); anchorPointEntity_ = new LineEntity(this, Qt3DRender::QGeometryRenderer::PrimitiveType::Lines); anchorPointEntity_->addVertex({3.0, 0.0, 0.0}); anchorPointEntity_->addVertex({-3.0, 0.0, 0.0}); @@ -95,16 +95,16 @@ void TextEntity::updateTranslation() textTransform_->setTranslation(v + translation); // Update the bounding box entity - if (boundingBoxEntity_) + if (boundingCuboidEntity_) { - boundingBoxEntity_->clear(); - boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); - boundingBoxEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.lowerLeftBack().y(), 0.0); - boundingBoxEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.upperRightFront().y(), 0.0); - boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.upperRightFront().y(), 0.0); - boundingBoxEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); - boundingBoxEntity_->setBasicIndices(); - boundingBoxEntity_->finalise(); + boundingCuboidEntity_->clear(); + boundingCuboidEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); + boundingCuboidEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.lowerLeftBack().y(), 0.0); + boundingCuboidEntity_->addVertex(textCuboid.upperRightFront().x(), textCuboid.upperRightFront().y(), 0.0); + boundingCuboidEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.upperRightFront().y(), 0.0); + boundingCuboidEntity_->addVertex(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0); + boundingCuboidEntity_->setBasicIndices(); + boundingCuboidEntity_->finalise(); } } diff --git a/src/entities/text.h b/src/entities/text.h index 53ec8ca..a4ea684 100644 --- a/src/entities/text.h +++ b/src/entities/text.h @@ -34,10 +34,10 @@ class TextEntity : public Qt3DCore::QEntity Qt3DExtras::QExtrudedTextMesh *textMesh_{nullptr}; // Transform Qt3DCore::QTransform *textTransform_{nullptr}; - // Bounding box and anchor point entities - LineEntity *boundingBoxEntity_{nullptr}, *anchorPointEntity_{nullptr}; - // Transforms for bounding box and anchor point entities - Qt3DCore::QTransform *boundingBoxTransform_{nullptr}; + // Bounding cuboid and anchor point entities + LineEntity *boundingCuboidEntity_{nullptr}, *anchorPointEntity_{nullptr}; + // Transforms for bounding cuboid and anchor point entities + Qt3DCore::QTransform *boundingCuboidTransform_{nullptr}; public: // Set text material