Skip to content
This repository was archived by the owner on Mar 15, 2025. It is now read-only.

Commit a9a3257

Browse files
committed
Some cleanup in Transform.
- Removed static create in favor of just using make_unique/make_shared. - Added implicit conversion from a dual quaternion. - Added a hint, that copying is currently disabled. - Made full_transform_ mutable. - Fixed some refs/consts.
1 parent 3f6788f commit a9a3257

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

dang-gl/include/dang-gl/Math/Transform.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,34 @@ class Transform {
2323
public:
2424
using Event = dutils::Event<Transform>;
2525

26-
/// @brief Creates a new pointer-based transform.
27-
static UniqueTransform create();
26+
/// @brief Allows for implicit construction from a dual quaternion.
27+
Transform(const dquat& own_transform = {});
28+
29+
// TODO: Copy is currently disabled because of event subscription
30+
// Implement copy to properly update the subscription
2831

2932
/// @brief The own transformation, without any parent transform.
3033
const dquat& ownTransform() const;
3134
/// @brief Sets the own transform to the given quaternion, triggering the on_change event.
3235
void setOwnTransform(const dquat& transform);
3336

3437
/// @brief The full transformation, including all parent transformations.
35-
const dquat& fullTransform();
38+
const dquat& fullTransform() const;
3639

3740
/// @brief The optional parent of this transformation.
38-
SharedTransform parent() const;
41+
const SharedTransform& parent() const;
3942
/// @brief Checks, if the chain of parents contains the given transform.
4043
bool parentChainContains(const Transform& transform) const;
4144
/// @brief UNSAFE! Forces the parent of this transform to the given transform, without checking for potential
4245
/// cycles.
4346
/// @remark A cycle will cause an immediate stack overflow, from recursively calling parent change events.
44-
void forceParent(const SharedTransform& parent);
47+
void forceParent(SharedTransform parent);
4548
/// @brief Tries to set the parent of this transform to the given transform and returns false if it would introduce
4649
/// a cycle.
47-
bool trySetParent(const SharedTransform& parent);
50+
bool trySetParent(SharedTransform parent);
4851
/// @brief Tries to set the parent of this transform to the given transform and throws a TransformCycleError if it
4952
/// would introduce a cycle.
50-
void setParent(const SharedTransform& parent);
53+
void setParent(SharedTransform parent);
5154
/// @brief Removes the current parent, which is the same as setting the parent to nullptr.
5255
void resetParent();
5356

@@ -59,7 +62,7 @@ class Transform {
5962

6063
private:
6164
dquat own_transform_;
62-
std::optional<dquat> full_transform_;
65+
mutable std::optional<dquat> full_transform_;
6366
SharedTransform parent_;
6467
Event::Subscription parent_change_;
6568
};

dang-gl/include/dang-gl/Rendering/Camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Camera {
191191

192192
private:
193193
SharedProjectionProvider projection_provider_;
194-
SharedTransform transform_ = Transform::create();
194+
SharedTransform transform_ = std::make_shared<Transform>();
195195
mutable std::vector<CameraUniforms> uniforms_;
196196
};
197197

dang-gl/src/Math/Transform.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace dang::gl {
44

5-
UniqueTransform Transform::create() { return std::make_unique<Transform>(); }
5+
Transform::Transform(const dquat& own_transform)
6+
: own_transform_(own_transform)
7+
{}
68

79
const dquat& Transform::ownTransform() const { return own_transform_; }
810

@@ -13,18 +15,14 @@ void Transform::setOwnTransform(const dquat& transform)
1315
on_change(*this);
1416
}
1517

16-
const dquat& Transform::fullTransform()
18+
const dquat& Transform::fullTransform() const
1719
{
18-
if (!full_transform_) {
19-
if (parent_)
20-
full_transform_ = own_transform_ * parent_->fullTransform();
21-
else
22-
full_transform_ = own_transform_;
23-
}
20+
if (!full_transform_)
21+
full_transform_ = parent_ ? own_transform_ * parent_->fullTransform() : own_transform_;
2422
return *full_transform_;
2523
}
2624

27-
SharedTransform Transform::parent() const { return parent_; }
25+
const SharedTransform& Transform::parent() const { return parent_; }
2826

2927
bool Transform::parentChainContains(const Transform& transform) const
3028
{
@@ -39,15 +37,15 @@ bool Transform::parentChainContains(const Transform& transform) const
3937
return false;
4038
}
4139

42-
void Transform::forceParent(const SharedTransform& parent)
40+
void Transform::forceParent(SharedTransform parent)
4341
{
44-
parent_ = parent;
45-
if (parent) {
42+
parent_ = std::move(parent);
43+
if (parent_) {
4644
auto parent_change = [&] {
4745
full_transform_.reset();
4846
on_change(*this);
4947
};
50-
parent_change_ = parent->on_change.subscribe(parent_change);
48+
parent_change_ = parent_->on_change.subscribe(parent_change);
5149
}
5250
else {
5351
parent_change_.remove();
@@ -57,22 +55,22 @@ void Transform::forceParent(const SharedTransform& parent)
5755
on_change(*this);
5856
}
5957

60-
bool Transform::trySetParent(const SharedTransform& parent)
58+
bool Transform::trySetParent(SharedTransform parent)
6159
{
6260
if (parent == parent_)
6361
return true;
6462

6563
if (parent && parent->parentChainContains(*this))
6664
return false;
6765

68-
forceParent(parent);
66+
forceParent(std::move(parent));
6967

7068
return true;
7169
}
7270

73-
void Transform::setParent(const SharedTransform& parent)
71+
void Transform::setParent(SharedTransform parent)
7472
{
75-
if (!trySetParent(parent))
73+
if (!trySetParent(std::move(parent)))
7674
throw TransformCycleError("Cannot set transform parent, as it would introduce a cycle.");
7775
}
7876

0 commit comments

Comments
 (0)