summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-02-13 01:36:42 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-02-13 01:36:42 +0000
commit849f4aa735352704995ffb51bf23dadf795bb119 (patch)
tree6f610f55791d7dc7ade6ebf5be42227f63979f75
parentAdd helpers for testing for triangle overlap/containedness (diff)
downloadilt-849f4aa735352704995ffb51bf23dadf795bb119.tar.bz2
ilt-849f4aa735352704995ffb51bf23dadf795bb119.tar.xz
ilt-849f4aa735352704995ffb51bf23dadf795bb119.zip
Include face handle in intersectRay result
-rw-r--r--game/geoData.cpp8
-rw-r--r--game/geoData.h6
-rw-r--r--test/test-geoData.cpp2
-rw-r--r--ui/builders/freeExtend.cpp6
-rw-r--r--ui/builders/straight.cpp6
5 files changed, 15 insertions, 13 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index ae43f3f..0dbc0ac 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -213,16 +213,16 @@ GeoData::positionAt(const PointFace & p) const
return positionOnTriangle(p.point, triangle<3>(p.face(this)));
}
-[[nodiscard]] std::optional<GlobalPosition3D>
+[[nodiscard]] GeoData::IntersectionResult
GeoData::intersectRay(const Ray<GlobalPosition3D> & ray) const
{
return intersectRay(ray, findPoint(ray.start));
}
-[[nodiscard]] std::optional<GlobalPosition3D>
+[[nodiscard]] GeoData::IntersectionResult
GeoData::intersectRay(const Ray<GlobalPosition3D> & ray, FaceHandle face) const
{
- std::optional<GlobalPosition3D> out;
+ GeoData::IntersectionResult out;
walkUntil(PointFace {ray.start, face},
ray.start.xy()
+ GlobalPosition2D(ray.direction.xy() * RelativePosition2D(upperExtent.xy() - lowerExtent.xy())),
@@ -231,7 +231,7 @@ GeoData::intersectRay(const Ray<GlobalPosition3D> & ray, FaceHandle face) const
RelativeDistance dist {};
const auto t = triangle<3>(face);
if (ray.intersectTriangle(t.x, t.y, t.z, bari, dist)) {
- out = t * bari;
+ out.emplace(t * bari, face);
return true;
}
return false;
diff --git a/game/geoData.h b/game/geoData.h
index 2fd3aa5..f599552 100644
--- a/game/geoData.h
+++ b/game/geoData.h
@@ -74,8 +74,10 @@ public:
[[nodiscard]] FaceHandle findPoint(GlobalPosition2D, FaceHandle start) const;
[[nodiscard]] GlobalPosition3D positionAt(const PointFace &) const;
- [[nodiscard]] std::optional<GlobalPosition3D> intersectRay(const Ray<GlobalPosition3D> &) const;
- [[nodiscard]] std::optional<GlobalPosition3D> intersectRay(const Ray<GlobalPosition3D> &, FaceHandle start) const;
+ using IntersectionLocation = std::pair<GlobalPosition3D, FaceHandle>;
+ using IntersectionResult = std::optional<IntersectionLocation>;
+ [[nodiscard]] IntersectionResult intersectRay(const Ray<GlobalPosition3D> &) const;
+ [[nodiscard]] IntersectionResult intersectRay(const Ray<GlobalPosition3D> &, FaceHandle start) const;
void walk(const PointFace & from, const GlobalPosition2D to, const std::function<void(FaceHandle)> & op) const;
void walkUntil(const PointFace & from, const GlobalPosition2D to, const std::function<bool(FaceHandle)> & op) const;
diff --git a/test/test-geoData.cpp b/test/test-geoData.cpp
index efe845c..f8437c3 100644
--- a/test/test-geoData.cpp
+++ b/test/test-geoData.cpp
@@ -114,7 +114,7 @@ BOOST_DATA_TEST_CASE(findRayIntersect,
}),
p, d, i)
{
- BOOST_CHECK_EQUAL(fixedTerrtain.intersectRay({p, d}).value(), i);
+ BOOST_CHECK_EQUAL(fixedTerrtain.intersectRay({p, d})->first, i);
}
BOOST_AUTO_TEST_CASE(boundaryWalk)
diff --git a/ui/builders/freeExtend.cpp b/ui/builders/freeExtend.cpp
index 47356c3..db127e6 100644
--- a/ui/builders/freeExtend.cpp
+++ b/ui/builders/freeExtend.cpp
@@ -19,7 +19,7 @@ BuilderFreeExtend::move(
candidateLinks.objects = network->candidateJoins(*p1, p->pos);
}
else if (const auto p = geoData->intersectRay(ray)) {
- candidateLinks.objects = network->candidateExtend(*p1, *p);
+ candidateLinks.objects = network->candidateExtend(*p1, p->first);
}
else {
candidateLinks.removeAll();
@@ -42,8 +42,8 @@ BuilderFreeExtend::click(
p1 = p->pos;
}
else if (const auto p = geoData->intersectRay(ray)) {
- network->addExtend(*p1, *p);
- p1 = *p;
+ network->addExtend(*p1, p->first);
+ p1 = p->first;
}
}
else {
diff --git a/ui/builders/straight.cpp b/ui/builders/straight.cpp
index 0c4a3e2..43f5ec8 100644
--- a/ui/builders/straight.cpp
+++ b/ui/builders/straight.cpp
@@ -16,7 +16,7 @@ BuilderStraight::move(
{
if (p1) {
if (const auto p = geoData->intersectRay(ray)) {
- candidateLinks.objects = network->candidateStraight(*p1, *p);
+ candidateLinks.objects = network->candidateStraight(*p1, p->first);
}
else {
candidateLinks.removeAll();
@@ -32,12 +32,12 @@ BuilderStraight::click(
case SDL_BUTTON_LEFT:
if (const auto p = geoData->intersectRay(ray)) {
if (p1) {
- create(network, *p1, *p);
+ create(network, *p1, p->first);
candidateLinks.removeAll();
p1.reset();
}
else {
- p1 = p;
+ p1 = p->first;
}
}
return;