mathlib: add PointsAlongLine

This commit is contained in:
Eric Wasylishen 2017-02-21 01:25:00 -07:00
parent b31ae5d243
commit 8067e0fa45
3 changed files with 26 additions and 0 deletions

View File

@ -540,3 +540,20 @@ std::vector<glm::vec3> GLM_ShrinkPoly(const std::vector<glm::vec3> &poly, const
return clipped;
}
std::vector<glm::vec3> 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<vec3> result;
const vec3 dir = linesegment / len;
const int stepCount = static_cast<int>(len / step);
for (int i=0; i<=stepCount; i++) {
vec3 pt = start + (dir * (step * i));
result.push_back(pt);
}
return result;
}

View File

@ -397,4 +397,6 @@ V bilinearInterpolate(const V &f00, const V &f10, const V &f01, const V &f11, co
return fxy;
}
std::vector<glm::vec3> PointsAlongLine(const glm::vec3 &start, const glm::vec3 &end, const float step);
#endif /* __COMMON_MATHLIB_H__ */

View File

@ -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));
}