Skip to content

Commit 60f09cc

Browse files
committed
Partial implementation of rotated labels (bounding cuboid limits still calculated incorrectly).
1 parent f8eae87 commit 60f09cc

File tree

6 files changed

+62
-20
lines changed

6 files changed

+62
-20
lines changed

src/classes/cuboid.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "classes/cuboid.h"
2+
#include <QMatrix4x4>
23

34
using namespace Mildred;
45

@@ -195,3 +196,32 @@ float Cuboid::zExtent() const
195196

196197
//! Return vector of extents
197198
QVector3D Cuboid::extents() const { return {xExtent(), yExtent(), zExtent()}; }
199+
200+
//! Return bounding box cuboid required for rotated original cuboid (in degrees, around z-axis)
201+
/*!
202+
* Returns the bounding cuboid required to contain the original cuboid rotated @param rotation
203+
* @return
204+
*/
205+
Cuboid Cuboid::zRotatedBoundingCuboid(QVector3D rotationOrigin, double thetaZ) const
206+
{
207+
QMatrix4x4 m;
208+
m.rotate(thetaZ, QVector3D{0.0, 0.0, 1.0});
209+
std::array<QPointF, 4> corners;
210+
corners[0] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y());
211+
corners[1] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y());
212+
corners[2] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y());
213+
corners[3] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y());
214+
215+
Cuboid rotated;
216+
for (const auto &p : corners)
217+
{
218+
auto newP = m.map(p);
219+
rotated.expand(newP.x() + rotationOrigin.x(), newP.y() + rotationOrigin.y(), {});
220+
}
221+
222+
// Set Z extents
223+
rotated.v1z_ = v1z_;
224+
rotated.v2z_ = v2z_;
225+
226+
return rotated;
227+
}

src/classes/cuboid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ class Cuboid
6060
float zExtent() const;
6161
// Return vector of extents
6262
QVector3D extents() const;
63+
// Return bounding box cuboid required for rotated original cuboid (in degrees, around z-axis)
64+
Cuboid zRotatedBoundingCuboid(QVector3D rotationOrigin, double thetaZ) const;
6365
};
6466
} // namespace Mildred

src/entities/axis.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,7 @@ Cuboid AxisEntity::createTickAndLabelEntities(const std::vector<std::pair<double
594594
(*tickLabelEntity)->setText(QString::number(v));
595595
(*tickLabelEntity)
596596
->setAnchorPosition(axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics_.tickLabelPixelGap()));
597-
boundingCuboid.expand(TextEntity::boundingCuboid(
598-
metrics_.axisTickLabelFont(), QString::number(v),
599-
{axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics_.tickLabelPixelGap())},
600-
labelAnchorPoint_)
601-
.first);
597+
boundingCuboid.expand((*tickLabelEntity)->boundingCuboid(metrics_.axisTickLabelFont()).first);
602598
++tickLabelEntity;
603599
}
604600
else
@@ -713,20 +709,20 @@ Cuboid AxisEntity::boundingCuboid(const MildredMetrics &metrics) const
713709
cuboid.expand(
714710
TextEntity::boundingCuboid(metrics.axisTickLabelFont(), QString::number(v),
715711
axisPos + tickDirection_ * (metrics_.tickPixelSize() + metrics.tickLabelPixelGap()),
716-
labelAnchorPoint_)
712+
labelAnchorPoint_, 0.0)
717713
.first);
718714
}
719715
else
720716
cuboid.expand({axisPos, axisPos + tickDirection_ * metrics_.tickPixelSize() * 0.5});
721717
}
722718
// -- Title
723719
if (!axisTitleEntity_->text().isEmpty())
724-
cuboid.expand(TextEntity::boundingCuboid(metrics.axisTitleFont(), axisTitleEntity_->text(),
725-
direction_ * metrics.displayVolumeExtent()[axisDirectionIndex_] * 0.5 +
726-
tickDirection_ *
727-
(cuboid.extents()[tickDirectionIndex_] + metrics.tickLabelPixelGap()),
728-
labelAnchorPoint_)
729-
.first);
720+
cuboid.expand(
721+
axisTitleEntity_
722+
->boundingCuboid(metrics.axisTitleFont(),
723+
direction_ * metrics.displayVolumeExtent()[axisDirectionIndex_] * 0.5 +
724+
tickDirection_ * (cuboid.extents()[tickDirectionIndex_] + metrics.tickLabelPixelGap()))
725+
.first);
730726

731727
return cuboid;
732728
}

src/entities/line.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ void LineEntity::addVertex(QVector3D v) { cachedVertices_.emplace_back(v); }
6767

6868
//! Append vertex to cached data
6969
/*!
70-
* 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
71-
* until a call to the finalise() method is made.
70+
* Append the vertex defined by the coordinates @param x, @param y, and @param z to the cached vertices. The buffer objects (and
71+
* hence the display primitive) are not regenerated until a call to the finalise() method is made.
7272
*/
73-
void LineEntity::addVertex(double x, double y, double z) { cachedVertices_.emplace_back(QVector3D{float(x), float(y), float(z)});}
73+
void LineEntity::addVertex(double x, double y, double z)
74+
{
75+
cachedVertices_.emplace_back(QVector3D{float(x), float(y), float(z)});
76+
}
7477

7578
//! Append vertex and colour to cached data
7679
/*!

src/entities/text.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using namespace Mildred;
55

66
// Enables showing of bounding boxes around Text Entities.
7-
constexpr auto showBoundingBoxes = false;
7+
constexpr auto showBoundingBoxes = true;
88

99
//! Construct a new text entity
1010
/*!
@@ -90,7 +90,7 @@ void TextEntity::updateTranslation()
9090
positionalTransform_->setRotationZ(rotation_);
9191

9292
// Set the text translation vector so that the defined anchor point is located at {0,0,0}
93-
auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth(), rotation_);
93+
auto [textCuboid, translation] = boundingCuboid(textMesh_->font(), textMesh_->text(), {}, anchorPoint_, textMesh_->depth());
9494
auto v = QVector3D(textCuboid.lowerLeftBack().x(), textCuboid.lowerLeftBack().y(), 0.0);
9595
textTransform_->setTranslation(v + translation);
9696

@@ -154,6 +154,12 @@ void TextEntity::setRotation(double rotation)
154154
updateTranslation();
155155
}
156156

157+
// Return bounding cuboid and anchor translation vector for current text and positioning
158+
std::pair<Cuboid, QVector3D> TextEntity::boundingCuboid(const QFont &font, std::optional<QVector3D> newAnchorPosition) const
159+
{
160+
return boundingCuboid(font, text(), newAnchorPosition.value_or(anchorPosition_), anchorPoint_, rotation_, 0.01);
161+
}
162+
157163
//! Return simple bounding cuboid for text, along with baseline descent from font metrics
158164
/*!
159165
* 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<Cuboid, int> TextEntity::boundingCuboid(const QFont &font, const QStri
182188
* should be applied to the mesh to be rendered in order to get the correct positioning.
183189
*/
184190
std::pair<Cuboid, QVector3D> TextEntity::boundingCuboid(const QFont &font, const QString &text, QVector3D anchorPosition,
185-
MildredMetrics::AnchorPoint anchorPoint, float depth)
191+
MildredMetrics::AnchorPoint anchorPoint, double rotation, float depth)
186192
{
187193
// Get basic bounding cuboid for the text
188194
auto [cuboid, descent] = boundingCuboid(font, text, depth);
@@ -209,5 +215,6 @@ std::pair<Cuboid, QVector3D> TextEntity::boundingCuboid(const QFont &font, const
209215
auto anchorFrac = MildredMetrics::anchorLocation(anchorPoint);
210216
cuboid.translate(-QVector3D(cuboid.xExtent() * anchorFrac.x(), cuboid.yExtent() * anchorFrac.y(), 0.0));
211217

212-
return {cuboid, meshTranslation};
218+
// Return the bounding cuboid (accounting for rotation)
219+
return {cuboid.zRotatedBoundingCuboid(anchorPosition, rotation), meshTranslation};
213220
}

src/entities/text.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ class TextEntity : public Qt3DCore::QEntity
6969
void setAnchorPosition(QVector3D p);
7070
// Set rotation
7171
void setRotation(double rotation);
72+
// Return bounding cuboid and anchor translation vector for current text and positioning
73+
std::pair<Cuboid, QVector3D> boundingCuboid(const QFont &font,
74+
std::optional<QVector3D> newAnchorPosition = std::nullopt) const;
7275
// Return simple bounding cuboid for text, along with baseline descent from font metrics
7376
static std::pair<Cuboid, int> boundingCuboid(const QFont &font, const QString &text, float depth = 0.1f);
7477
// Return bounding cuboid with translation and anchor point applied, and required translation vector for text mesh
7578
static std::pair<Cuboid, QVector3D> boundingCuboid(const QFont &font, const QString &text, QVector3D anchorPosition,
76-
MildredMetrics::AnchorPoint anchorPoint, float depth = 0.1f);
79+
MildredMetrics::AnchorPoint anchorPoint, double rotation,
80+
float depth = 0.1f);
7781
};
7882
} // namespace Mildred

0 commit comments

Comments
 (0)