Skip to content

Commit

Permalink
Moved interpolation constructor to Utils static function.
Browse files Browse the repository at this point in the history
We need static factory methods to support different interpolation techniques with same signature (a vector of points and a dimension). ref #39.
  • Loading branch information
Marcel Steinbeck committed Apr 14, 2017
1 parent dd52266 commit 222e146
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 15 additions & 12 deletions library/tinysplinecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,6 @@ ts::BSpline::BSpline(const size_t deg, const size_t dim, const size_t nCtrlp,
throw std::runtime_error(ts_enum_str(err));
}

ts::BSpline::BSpline(const std::vector<ts::rational> points, const size_t dim)
{
if (dim == 0)
throw std::runtime_error(ts_enum_str(TS_DIM_ZERO));
if (points.size() % dim != 0)
throw std::runtime_error("#points % dim == 0 failed");
const tsError err = ts_bspline_interpolate(
points.data(), points.size()/dim, dim, &bspline);
if (err < 0)
throw std::runtime_error(ts_enum_str(err));
}

ts::BSpline::~BSpline()
{
ts_bspline_free(&bspline);
Expand Down Expand Up @@ -350,6 +338,21 @@ void ts::BSpline::swap(ts::BSpline &other)
* Utils *
* *
********************************************************/
ts::BSpline ts::Utils::interpolateCubic(
const std::vector<ts::rational> *points, const size_t dim)
{
if (dim == 0)
throw std::runtime_error(ts_enum_str(TS_DIM_ZERO));
if (points->size() % dim != 0)
throw std::runtime_error("#points % dim == 0 failed");
ts::BSpline bspline;
const tsError err = ts_bspline_interpolate(
points->data(), points->size()/dim, dim, bspline.data());
if (err < 0)
throw std::runtime_error(ts_enum_str(err));
return bspline;
}

bool ts::Utils::fequals(const ts::rational x, const ts::rational y)
{
return ts_fequals(x, y) == 1;
Expand Down
3 changes: 2 additions & 1 deletion library/tinysplinecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class BSpline {
BSpline(const BSpline& other);
BSpline(const size_t deg, const size_t dim, const size_t nCtrlp,
const tsBSplineType type);
BSpline(const std::vector<rational> points, const size_t dim);
~BSpline();

/* Operators */
Expand Down Expand Up @@ -94,6 +93,8 @@ class BSpline {

class Utils {
public:
static BSpline interpolateCubic(const std::vector<rational> *points,
const size_t dim);
static bool fequals(const rational x, const rational y);
static std::string enum_str(const tsError err);
static tsError str_enum(const std::string str);
Expand Down

0 comments on commit 222e146

Please sign in to comment.