Move bicubic point interpolation logic to SplineCurve

Originally committed to SVN as r6592.
This commit is contained in:
Thomas Goyne 2012-03-20 00:39:33 +00:00
parent ca76c5cf21
commit d86d56d816
3 changed files with 52 additions and 41 deletions

View file

@ -196,24 +196,6 @@ void Spline::MovePoint(iterator curve,int point,Vector2D pos) {
} }
} }
static int render_bicubic(Spline::iterator cur, std::vector<float> &points) {
int len = int(
(cur->p2 - cur->p1).Len() +
(cur->p3 - cur->p2).Len() +
(cur->p4 - cur->p3).Len());
int steps = len/8;
for (int i = 0; i <= steps; ++i) {
// Get t and t-1 (u)
float t = i / float(steps);
Vector2D p = cur->GetPoint(t);
points.push_back(p.X());
points.push_back(p.Y());
}
return steps + 1;
}
void Spline::GetPointList(std::vector<float>& points, std::vector<int>& first, std::vector<int>& count) { void Spline::GetPointList(std::vector<float>& points, std::vector<int>& first, std::vector<int>& count) {
points.clear(); points.clear();
first.clear(); first.clear();
@ -224,30 +206,15 @@ void Spline::GetPointList(std::vector<float>& points, std::vector<int>& first, s
// Generate points for each curve // Generate points for each curve
for (iterator cur = begin(); cur != end(); ++cur) { for (iterator cur = begin(); cur != end(); ++cur) {
switch (cur->type) { if (cur->type == SplineCurve::POINT) {
case SplineCurve::POINT: if (curCount > 0)
if (curCount > 0) count.push_back(curCount);
count.push_back(curCount);
// start new path // start new path
first.push_back(points.size() / 2); first.push_back(points.size() / 2);
points.push_back(cur->p1.X()); curCount = 0;
points.push_back(cur->p1.Y());
curCount = 1;
break;
case SplineCurve::LINE:
points.push_back(cur->p2.X());
points.push_back(cur->p2.Y());
++curCount;
break;
case SplineCurve::BICUBIC:
curCount += render_bicubic(cur, points);
break;
default: break;
} }
curCount += cur->GetPoints(points);
} }
count.push_back(curCount); count.push_back(curCount);
@ -265,7 +232,7 @@ void Spline::GetPointList(std::vector<float> &points, iterator curve) {
break; break;
case SplineCurve::BICUBIC: case SplineCurve::BICUBIC:
render_bicubic(curve, points); curve->GetPoints(points);
break; break;
default: break; default: break;

View file

@ -169,3 +169,38 @@ float SplineCurve::GetClosestSegmentDistance(Vector2D pt1, Vector2D pt2, Vector2
float t = GetClosestSegmentPart(pt1, pt2, pt3); float t = GetClosestSegmentPart(pt1, pt2, pt3);
return (pt1 * (1.f - t) + pt2 * t - pt3).Len(); return (pt1 * (1.f - t) + pt2 * t - pt3).Len();
} }
int SplineCurve::GetPoints(std::vector<float> &points) const {
switch (type) {
case POINT:
points.push_back(p1.X());
points.push_back(p1.Y());
return 1;
case LINE:
points.push_back(p2.X());
points.push_back(p2.Y());
return 1;
case BICUBIC: {
int len = int(
(p2 - p1).Len() +
(p3 - p2).Len() +
(p4 - p3).Len());
int steps = len/8;
for (int i = 0; i <= steps; ++i) {
// Get t and t-1 (u)
float t = i / float(steps);
Vector2D p = GetPoint(t);
points.push_back(p.X());
points.push_back(p.Y());
}
return steps + 1;
}
default:
return 0;
}
}

View file

@ -36,6 +36,10 @@
#include "vector2d.h" #include "vector2d.h"
#ifndef AGI_PRE
#include <vector>
#endif
/// DOCME /// DOCME
/// @class SplineCurve /// @class SplineCurve
/// @brief DOCME /// @brief DOCME
@ -83,4 +87,9 @@ public:
float GetClosestParam(Vector2D ref) const; float GetClosestParam(Vector2D ref) const;
/// Get distance from ref to the closest point on the curve /// Get distance from ref to the closest point on the curve
float GetQuickDistance(Vector2D ref) const; float GetQuickDistance(Vector2D ref) const;
/// Get the coordinates of each point on this curve
/// @param[out] points Vector to add points to
/// @return Number of points in the curve
int GetPoints(std::vector<float> &points) const;
}; };