From 8067e0fa459c0d7edb2e4ed328b04fb4bba6c308 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Tue, 21 Feb 2017 01:25:00 -0700 Subject: [PATCH] mathlib: add PointsAlongLine --- common/mathlib.cc | 17 +++++++++++++++++ include/common/mathlib.hh | 2 ++ light/test_light.cc | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/common/mathlib.cc b/common/mathlib.cc index 77dd6b45..c387b2c3 100644 --- a/common/mathlib.cc +++ b/common/mathlib.cc @@ -540,3 +540,20 @@ std::vector GLM_ShrinkPoly(const std::vector &poly, const return clipped; } + +std::vector PointsAlongLine(const glm::vec3 &start, const glm::vec3 &end, const float step) +{ + const vec3 linesegment = end - start; + const float len = length(linesegment); + if (len == 0) + return {}; + + vector result; + const vec3 dir = linesegment / len; + const int stepCount = static_cast(len / step); + for (int i=0; i<=stepCount; i++) { + vec3 pt = start + (dir * (step * i)); + result.push_back(pt); + } + return result; +} diff --git a/include/common/mathlib.hh b/include/common/mathlib.hh index 9f773eb3..0c634a04 100644 --- a/include/common/mathlib.hh +++ b/include/common/mathlib.hh @@ -397,4 +397,6 @@ V bilinearInterpolate(const V &f00, const V &f10, const V &f01, const V &f11, co return fxy; } +std::vector PointsAlongLine(const glm::vec3 &start, const glm::vec3 &end, const float step); + #endif /* __COMMON_MATHLIB_H__ */ diff --git a/light/test_light.cc b/light/test_light.cc index 6a62e8f5..ec2fef00 100644 --- a/light/test_light.cc +++ b/light/test_light.cc @@ -589,3 +589,10 @@ TEST(mathlib, bilinearWeightsAndCoords2) { EXPECT_EQ(vec2(2, 1.5), sum); } +TEST(mathlib, pointsAlongLine) { + const auto res = PointsAlongLine(vec3(1,0,0), vec3(3.5, 0, 0), 1.5f); + + ASSERT_EQ(2, res.size()); + ASSERT_TRUE(pointsEqualEpsilon(vec3(1,0,0), res[0], POINT_EQUAL_EPSILON)); + ASSERT_TRUE(pointsEqualEpsilon(vec3(2.5,0,0), res[1], POINT_EQUAL_EPSILON)); +}