bezier: initial eval for order 2-4

This commit is contained in:
Danny Robson 2015-01-21 23:42:45 +11:00
parent 5469fdf06b
commit 2419e07547
3 changed files with 167 additions and 0 deletions

View File

@ -12,6 +12,8 @@ SUBDIRS = test
UTIL_FILES = \ UTIL_FILES = \
backtrace.hpp \ backtrace.hpp \
bezier.cpp \
bezier.hpp \
bitwise.cpp \ bitwise.cpp \
bitwise.hpp \ bitwise.hpp \
colour.cpp \ colour.cpp \

124
bezier.cpp Normal file
View File

@ -0,0 +1,124 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "bezier.hpp"
#include "debug.hpp"
#include <algorithm>
#include <iterator>
//-----------------------------------------------------------------------------
template <size_t S>
util::bezier<S>::bezier (const util::point2f _points[S])
{
std::copy (_points, _points + S, m_points);
}
//-----------------------------------------------------------------------------
namespace util {
template <>
point2f
bezier<2>::eval (float t)
{
CHECK_GE (t, 0);
CHECK_LE (t, 1);
auto v0 = t * m_points[0];
auto v1 = (1 - t) * m_points[1];
return {
v0.x + v1.x,
v0.y + v0.y
};
}
}
//-----------------------------------------------------------------------------
namespace util {
template <>
point2f
bezier<3>::eval (float t)
{
CHECK_GE (t, 0);
CHECK_LE (t, 1);
auto v0 = pow2 (1 - t) * m_points[0];
auto v1 = 2 * (1 - t) * t * m_points[1];
auto v2 = pow2 (t) * m_points[2];
return {
v0.x + v1.x + v2.x,
v0.y + v1.y + v2.y
};
}
}
//-----------------------------------------------------------------------------
namespace util {
template <>
point2f
bezier<4>::eval (float t)
{
CHECK_GE (t, 0);
CHECK_LE (t, 1);
auto v0 = pow (1 - t, 3) * m_points[0];
auto v1 = 3 * pow2 (1 - t) * t * m_points[1];
auto v2 = 3 * pow2 (1 - t) * t * m_points[2];
auto v3 = pow (t, 3) * m_points[3];
return {
v0.x + v1.x + v2.x + v3.x,
v0.y + v1.y + v2.y + v3.y
};
}
}
//-----------------------------------------------------------------------------
template <size_t S>
util::point2f&
util::bezier<S>::operator[] (size_t idx)
{
CHECK_LT (idx, S);
return m_points[idx];
}
//-----------------------------------------------------------------------------
template <size_t S>
const util::point2f&
util::bezier<S>::operator[] (size_t idx) const
{
CHECK_LT (idx, S);
return m_points[idx];
}
//-----------------------------------------------------------------------------
template class util::bezier<2>;
template class util::bezier<3>;
template class util::bezier<4>;

41
bezier.hpp Normal file
View File

@ -0,0 +1,41 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_BEZIER_HPP
#define __UTIL_BEZIER_HPP
#include "point.hpp"
namespace util {
template <size_t S>
class bezier {
bezier (const util::point2f[S]);
point2f eval (float t);
float distance (point2f);
point2f& operator[] (size_t idx);
const point2f& operator[] (size_t idx) const;
private:
point2f m_points[S];
};
}
#endif