Skip to content

Instantly share code, notes, and snippets.

@cwhitney
Created November 18, 2015 17:17
Show Gist options
  • Save cwhitney/8064fc36196c1e259cb8 to your computer and use it in GitHub Desktop.
Save cwhitney/8064fc36196c1e259cb8 to your computer and use it in GitHub Desktop.
CatmullRom from points
// From: https://github.com/sansumbrella/Pockets/blob/master/src/archive/pockets/CurveUtils.cpp
template <typename T>
vector<T> Predator::curveThrough(const vector<T> &points)
{
assert(points.size() > 2);
vector<T> curvePoints;
T p1 = points.at(0);
T p2 = points.at(1);
T p3 = points.at(2);
T b1 = p1 + (p2 - p1) / 6.0f;
T b2 = p2 - (p3 - p1) / 6.0f;
curvePoints.push_back(p1);
curvePoints.push_back(b1);
curvePoints.push_back(b2);
curvePoints.push_back(p2);
for (int i = 1; i < points.size() - 2; ++i)
{
T p0 = points.at(i - 1);
T p1 = points.at(i);
T p2 = points.at(i + 1);
T p3 = points.at(i + 2);
T b1 = p1 + (p2 - p0) / 6.0f;
T b2 = p2 - (p3 - p1) / 6.0f;
curvePoints.push_back(p1);
curvePoints.push_back(b1);
curvePoints.push_back(b2);
curvePoints.push_back(p2);
}
size_t i2 = points.size() - 1;
size_t i1 = i2 - 1;
size_t i0 = i2 - 2;
T p0 = points.at(i0);
p1 = points.at(i1);
p2 = points.at(i2);
b1 = p1 + (p2 - p0) / 6.0f;
b2 = p2 - (p2 - p1) / 6.0f;
curvePoints.push_back(p1);
curvePoints.push_back(b1);
curvePoints.push_back(b2);
curvePoints.push_back(p2);
return curvePoints;
}
template vector<vec2> Predator::curveThrough<vec2>(const vector<vec2> &points);
template vector<vec3> Predator::curveThrough<vec3>(const vector<vec3> &points);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment