summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-04-24 02:00:08 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2025-04-24 02:00:08 +0100
commit9ccf9d330071454b5319a58227a3c5e5232f335f (patch)
tree88f60b8fe8b2c0bbb0e439566d519508bfba928c
parentAvoid out parameters with Ray::intersectTriangle (diff)
downloadilt-9ccf9d330071454b5319a58227a3c5e5232f335f.tar.bz2
ilt-9ccf9d330071454b5319a58227a3c5e5232f335f.tar.xz
ilt-9ccf9d330071454b5319a58227a3c5e5232f335f.zip
Avoid out parameters with Ray::intersectSphereHEADmain
-rw-r--r--game/network/network.cpp4
-rw-r--r--lib/ray.h25
2 files changed, 18 insertions, 11 deletions
diff --git a/game/network/network.cpp b/game/network/network.cpp
index a39a66c..c8482de 100644
--- a/game/network/network.cpp
+++ b/game/network/network.cpp
@@ -57,9 +57,7 @@ Network::intersectRayNodes(const Ray<GlobalPosition3D> & ray) const
// Click within 2m of a node
if (const auto node = std::find_if(nodes.begin(), nodes.end(),
[&ray](const Node::Ptr & node) {
- GlobalPosition3D ipos;
- Normal3D inorm;
- return ray.intersectSphere(node->pos, MIN_DISTANCE, ipos, inorm);
+ return ray.intersectSphere(node->pos, MIN_DISTANCE);
});
node != nodes.end()) {
return *node;
diff --git a/lib/ray.h b/lib/ray.h
index 2b117b7..642cd4d 100644
--- a/lib/ray.h
+++ b/lib/ray.h
@@ -78,21 +78,30 @@ public:
return std::nullopt;
}
- bool
- intersectSphere(const PositionType centre, const PositionType::value_type size, PositionType & position,
- Normal3D & normal) const
+ struct IntersectSphereResult {
+ PositionType position;
+ Normal3D normal;
+ };
+
+ std::optional<IntersectSphereResult>
+ intersectSphere(const PositionType centre, const PositionType::value_type size) const
{
+ IntersectSphereResult out;
if constexpr (std::is_floating_point_v<typename PositionType::value_type>) {
- return glm::intersectRaySphere(start, direction, centre, size, position, normal);
+ if (glm::intersectRaySphere(start, direction, centre, size, out.position, out.normal)) {
+ return out;
+ }
}
else {
const RelativePosition3D cr = centre - start;
RelativePosition3D positionF {};
- const auto r = glm::intersectRaySphere(
- {}, direction, cr, static_cast<RelativeDistance>(size), positionF, normal);
- position = GlobalPosition3D(positionF) + start;
- return r;
+ if (glm::intersectRaySphere(
+ {}, direction, cr, static_cast<RelativeDistance>(size), positionF, out.normal)) {
+ out.position = GlobalPosition3D(positionF) + start;
+ return out;
+ }
}
+ return std::nullopt;
}
};