Skip to content
Snippets Groups Projects
Commit 46e4e9f8 authored by BATON Theau's avatar BATON Theau
Browse files

CurveTools Refactoring

parent d9095f70
No related branches found
No related tags found
No related merge requests found
Showing
with 122 additions and 140 deletions
#include "CurveTools/CPU/BSplineGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
BSplineGenerator::BSplineGenerator(CurveGenerator* generator) :
CurveGenerator(0),
m_generator(generator)
{
namespace ct {
BSplineGenerator::BSplineGenerator(CurveGenerator* generator)
: CurveGenerator(0), m_generator(generator) {}
}
Point BSplineGenerator::operator()(const Curve& points, double u) const
{
Point BSplineGenerator::generate(const Curve& points, double u) const {
unsigned int degree = m_generator->getDegree();
if (points.size() < degree)
if (points.size() < degree) {
return Point(0.0);
}
unsigned int nbCurves = static_cast<unsigned int>(std::ceil((points.size() - degree + 1) / static_cast<double>(degree - 1)));
u = glm::clamp(u, 0.0, static_cast<double>(nbCurves));
......@@ -23,14 +18,14 @@ namespace ct
double t = std::modf(u, &k);
std::vector<Point> generatorPoints(degree);
for (unsigned int i = 0; i < degree; ++i)
{
if (u == nbCurves)
generatorPoints[i] = points[static_cast<size_t>((degree - 1) * (k - 1) + i)];
else
generatorPoints[i] = points[static_cast<size_t>((degree - 1) * k + i)];
for (unsigned int i = 0; i < degree; ++i) {
generatorPoints[i] = u == nbCurves ? points[static_cast<size_t>((degree - 1) * (k - 1) + i)] : points[static_cast<size_t>((degree - 1) * k + i)];
}
return this->m_generator->generate(generatorPoints, t);
}
return (*m_generator)(generatorPoints, t);
std::vector<Point> BSplineGenerator::generate(const Curve& points, const std::vector<double> & us) const {
return CurveSampler::SampleCurve(points, *this, us);
}
}
\ No newline at end of file
#include "CurveTools/CPU/BezierGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
BezierGenerator::BezierGenerator(unsigned int degree) :
CurveGenerator(degree)
{
namespace ct {
BezierGenerator::BezierGenerator(unsigned int degree)
: CurveGenerator(degree) {}
}
Point BezierGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point BezierGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
std::vector<Point> currentPoints = points;
while (currentPoints.size() != 1)
{
while (currentPoints.size() != 1) {
std::vector<Point> nextPoints;
for (size_t i = 0; i < currentPoints.size() - 1; ++i)
{
for (size_t i = 0; i < currentPoints.size() - 1; ++i) {
Point a = currentPoints[i];
Point b = currentPoints[i + 1];
nextPoints.push_back(lerp(a, b, t));
}
......@@ -32,4 +25,8 @@ namespace ct
return currentPoints.front();
}
std::vector<Point> BezierGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
#include "CurveTools/CPU/CatmullRomGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
CatmullRomGenerator::CatmullRomGenerator() :
CurveGenerator(4)
{
namespace ct {
CatmullRomGenerator::CatmullRomGenerator()
: CurveGenerator(4) {}
}
Point CatmullRomGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point CatmullRomGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
t = glm::clamp(t, 0.0, 1.0);
......@@ -25,4 +22,8 @@ namespace ct
return (f1 * points[0] + f2 * points[1] + f3 * points[2] + f4 * points[3]) * 0.5;
}
std::vector<Point> CatmullRomGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
#include "CurveTools/CPU/CurveGenerator.hpp"
namespace ct
{
CurveGenerator::CurveGenerator(unsigned int degree) :
m_degree(degree)
{
namespace ct {
CurveGenerator::CurveGenerator(uint16_t degree)
: Generator(degree) {}
}
unsigned int CurveGenerator::getDegree() const
{
return m_degree;
}
Point CurveGenerator::lerp(const Point& a, const Point& b, double t)
{
Point CurveGenerator::lerp(const Point& a, const Point& b, double t) {
t = glm::clamp(t, 0.0, 1.0);
return (1.0f - t) * a + t * b;
}
Point CurveGenerator::smoothstep(const Point& a, const Point& b, double t)
{
Point CurveGenerator::smoothstep(const Point& a, const Point& b, double t) {
t = glm::clamp(t, 0.0, 1.0);
t = t * t * (3.0 - 2.0 * t);
return lerp(a, b, t);
}
Point CurveGenerator::smootherstep(const Point& a, const Point& b, double t)
{
Point CurveGenerator::smootherstep(const Point& a, const Point& b, double t) {
t = glm::clamp(t, 0.0, 1.0);
t = t * t * t * (6.0 * t * t - 15.0 * t + 10.0);
......
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
std::vector<double> CurveSampler::generateSampleValues(unsigned int count, double min, double max)
{
namespace ct {
std::vector<double> CurveSampler::generateSampleValues(unsigned int count, double min, double max) {
std::vector<double> sampleValues(count);
double step = (max - min) / static_cast<double>(count - 1);
......@@ -14,8 +12,7 @@ namespace ct
return sampleValues;
}
Curve CurveSampler::sampleCurve(const Curve& curve, const CurveGenerator& generator, const std::vector<double>& t)
{
Curve CurveSampler::SampleCurve(const Curve& curve, const CurveGenerator& generator, const std::vector<double>& t) {
ct::Curve sampledCurve(t.size());
for (unsigned int i = 0; i < t.size(); ++i)
......
#include "CurveTools/CPU/HermiteGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
HermiteGenerator::HermiteGenerator() :
CurveGenerator(4)
{
namespace ct {
HermiteGenerator::HermiteGenerator()
: CurveGenerator(4) {}
}
Point HermiteGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point HermiteGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
t = glm::clamp(t, 0.0, 1.0);
......@@ -25,4 +22,8 @@ namespace ct
return f1 * points[0] + f2 * points[1] + f3 * points[3] + f4 * points[2];
}
std::vector<Point> HermiteGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
#include "CurveTools/CPU/LinearGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
LinearGenerator::LinearGenerator() :
CurveGenerator(2)
{
namespace ct {
LinearGenerator::LinearGenerator()
: CurveGenerator(2) {}
}
Point LinearGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point LinearGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
return lerp(points[0], points[1], t);
}
std::vector<Point> LinearGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
#include "CurveTools/CPU/SmootherstepGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
SmootherstepGenerator::SmootherstepGenerator() :
CurveGenerator(2)
{
namespace ct {
SmootherstepGenerator::SmootherstepGenerator()
: CurveGenerator(2) {}
}
Point SmootherstepGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point SmootherstepGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
return smootherstep(points[0], points[1], t);
}
std::vector<Point> SmootherstepGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
#include "CurveTools/CPU/SmoothstepGenerator.hpp"
#include "CurveTools/CPU/CurveSampler.hpp"
namespace ct
{
SmoothstepGenerator::SmoothstepGenerator() :
CurveGenerator(2)
{
namespace ct {
SmoothstepGenerator::SmoothstepGenerator()
: CurveGenerator(2) {}
}
Point SmoothstepGenerator::operator()(const Curve& points, double t) const
{
if (points.size() < m_degree)
Point SmoothstepGenerator::generate(const Curve& points, double t) const {
if (points.size() < this->getDegree()) {
return Point(0.0);
}
return smoothstep(points[0], points[1], t);
}
std::vector<Point> SmoothstepGenerator::generate(const Curve& points, const std::vector<double> & ts) const {
return CurveSampler::SampleCurve(points, *this, ts);
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@
#include <iostream>
namespace ct::gpu {
__host__ Point BezierGenerator::operator()(const thrust::universal_vector<Point> & points, const float t) const {
__host__ Point BezierGenerator::generate(const thrust::universal_vector<Point> & points, const float t) const {
size_t max_size = points.size();
thrust::device_vector<Point> dev_p(points);
thrust::device_vector<Point> dev_r(max_size-1);
......@@ -21,7 +21,7 @@ namespace ct::gpu {
return dev_r[0];
}
__host__ Curve BezierGenerator::operator()(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve BezierGenerator::generate(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
size_t ts_size = ts.size();
Curve result(ts_size);
......
......@@ -8,7 +8,7 @@
#include <iostream>
namespace ct::gpu {
__host__ Point CatmullRomGenerator::operator()(const thrust::universal_vector<Point> & points, const float t) const {
__host__ Point CatmullRomGenerator::generate(const thrust::universal_vector<Point> & points, const float t) const {
if (points.size() < 4) {
return Point(0.0);
}
......@@ -24,7 +24,7 @@ namespace ct::gpu {
return r;
}
__host__ Curve CatmullRomGenerator::operator()(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve CatmullRomGenerator::generate(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
if (points.size() < 4) {
return {};
}
......
......@@ -8,7 +8,7 @@
#include <iostream>
namespace ct::gpu {
__host__ Point HermiteGenerator::operator()(const thrust::universal_vector<Point> & points, const float t) const {
__host__ Point HermiteGenerator::generate(const thrust::universal_vector<Point> & points, const float t) const {
if (points.size() < 4) {
return Point(0.0);
}
......@@ -24,7 +24,7 @@ namespace ct::gpu {
return r;
}
__host__ Curve HermiteGenerator::operator()(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve HermiteGenerator::generate(const thrust::universal_vector<Point> & points, const std::vector<float> & ts, const cudaStream_t & stream) const {
if (points.size() < 4) {
return {};
}
......
......@@ -9,7 +9,7 @@ namespace ct::gpu {
LinearGenerator::LinearGenerator()
: StreamedGenerator() {}
__host__ Point LinearGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
__host__ Point LinearGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
size_t max_size = ctrlPoint.size();
float range = 1.f/static_cast<float>(max_size);
......@@ -37,7 +37,7 @@ namespace ct::gpu {
return r;
}
__host__ Curve LinearGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve LinearGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
size_t ts_size = ts.size();
Curve result(ts_size);
......
......@@ -10,7 +10,7 @@ namespace ct::gpu {
SmootherstepGenerator::SmootherstepGenerator()
: StreamedGenerator() {}
__host__ Point SmootherstepGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
__host__ Point SmootherstepGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
if (ctrlPoint.size() < 2) {
return Point(0.0);
}
......@@ -26,7 +26,7 @@ namespace ct::gpu {
return r;
}
__host__ Curve SmootherstepGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve SmootherstepGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
if(ctrlPoint.size() < 2) {
return {};
}
......
......@@ -10,7 +10,7 @@ namespace ct::gpu {
SmoothstepGenerator::SmoothstepGenerator()
: StreamedGenerator() {}
__host__ Point SmoothstepGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
__host__ Point SmoothstepGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const float t) const {
if (ctrlPoint.size() < 2) {
return Point(0.0);
}
......@@ -26,7 +26,7 @@ namespace ct::gpu {
return r;
}
__host__ Curve SmoothstepGenerator::operator()(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
__host__ Curve SmoothstepGenerator::generate(const thrust::universal_vector<Point> & ctrlPoint, const std::vector<float> & ts, const cudaStream_t & stream) const {
if(ctrlPoint.size() < 2) {
return {};
}
......
......@@ -7,34 +7,34 @@
#include <iostream>
namespace ct::gpu {
SplineGenerator::SplineGenerator(StreamedGenerator * generator, size_t cut, size_t streams)
: _streamsCount(streams), _cut(cut), _generator(generator) {
SplineGenerator::SplineGenerator(StreamedGenerator * generator, size_t degree, size_t streams)
: Generator(degree), _streamsCount(streams), _generator(generator) {}
}
__host__ Point SplineGenerator::operator()(const thrust::universal_vector<Point> & points, float u) const {
thrust::universal_vector<Point> subPoints(this->_cut);
__host__ Point SplineGenerator::generate(const thrust::universal_vector<Point> & points, float u) const {
size_t degree = this->getDegree();
thrust::universal_vector<Point> subPoints(degree);
float e = 0;
float f = std::modf(u, &e);
size_t start = static_cast<size_t>(e * this->_cut);
size_t start = static_cast<size_t>(e * degree);
if(start != 0) {
start--;
}
if(e == (points.size()-1)/(this->_cut-1)) {
start -= this->_cut;
if(e == (points.size()-1)/(degree-1)) {
start -= degree;
f = 1.0f;
}
std::copy(points.begin() + start, points.begin() + start + this->_cut, subPoints.begin());
return this->_generator->operator()(subPoints, f);
std::copy(points.begin() + start, points.begin() + start + degree, subPoints.begin());
return this->_generator->generate(subPoints, f);
}
__host__ Curve SplineGenerator::operator()(const thrust::universal_vector<Point> & points, const std::vector<float> & us) const {
__host__ Curve SplineGenerator::generate(const thrust::universal_vector<Point> & points, const std::vector<float> & us) const {
Curve result(us.size());
size_t degree = this->getDegree();
size_t subCurveCount = static_cast<size_t>(std::ceil(points.size() / this->_cut)) + 1;
size_t subCurveCount = static_cast<size_t>(std::ceil(points.size() / degree)) + 1;
std::vector<Curve> subCurves(subCurveCount);
std::vector<cudaStream_t> streams;
......@@ -49,8 +49,8 @@ namespace ct::gpu {
for(size_t i = 0; i < subCurveCount; i++) {
size_t currentStream = i%subCurveCount;
thrust::universal_vector<Point> subPoints(this->_cut);
thrust::copy(points.begin()+currentPoint, points.begin()+currentPoint+this->_cut, subPoints.begin());
thrust::universal_vector<Point> subPoints(degree);
thrust::copy(points.begin()+currentPoint, points.begin()+currentPoint+degree, subPoints.begin());
std::vector<float> ts;
......@@ -66,10 +66,10 @@ namespace ct::gpu {
}
}
Curve subResult = this->_generator->operator()(subPoints, ts, streams[currentStream]);
Curve subResult = this->_generator->generate(subPoints, ts, streams[currentStream]);
memmove(&result[usCount == 0 ? 0 : usCount-1], subResult.data(), sizeof(Point) * subResult.size());
currentPoint += this->_cut-1;
currentPoint += degree-1;
}
for(auto & stream : streams) {
......
......@@ -40,7 +40,7 @@ namespace pg {
ps.push_back(i);
}
ct::Curve curve = ct::CurveSampler::sampleCurve(this->_generator->getControlPoint(), *this->_generator->getGenerator(), ps);
ct::Curve curve = ct::CurveSampler::SampleCurve(this->_generator->getControlPoint(), *this->_generator->getGenerator(), ps);
for(auto & point : curve) {
traj.push_back(static_cast<float>(point.x));
......@@ -64,7 +64,7 @@ namespace pg {
this->_program.setUniform("uView", camera.getViewMatrix());
GLsizei raw = static_cast<GLsizei>(this->_generator->getControlPoint().size());
GLsizei total = this->_vbo.vertices();
GLsizei total = static_cast<GLsizei>(this->_vbo.vertices());
glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(this->_generator->getControlPoint().size()));
glDrawArrays(GL_LINE_STRIP, raw, total - raw);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment