BSpline.cpp 1.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "BSpline.h"

BSpline::BSpline(std::vector<glm::vec4> cpoints, int k)
    : m_controlPoints(std::move(cpoints)), m_k(k) {}

BSpline::~BSpline(void) {}

glm::vec4 BSpline::evaluate(float t) {
    glm::vec4 result;
    for (unsigned int i = 0; i < m_controlPoints.size(); i++) {
        result += m_controlPoints[i] * coxDeBoor(t, i, m_k);
    }
    return result;
}

float BSpline::coxDeBoor(float t, int i, int k) {
    if (k == 1) {
        if (t >= m_knotVector[i] && t < m_knotVector[i + 1]) {
            return 1.0;
        } else {
            return 0.0;
        }
    } else {
        float nom0 = ((t - m_knotVector[i]) * coxDeBoor(t, i, k - 1));
        float denom0 = (m_knotVector[i + k - 1] - m_knotVector[i]);

        float nom1 = ((m_knotVector[i + k] - t) * coxDeBoor(t, i + 1, k - 1));
        float denom1 = (m_knotVector[i + k] - m_knotVector[i + 1]);

        float frac0, frac1;
        denom0 > 0.0 ? frac0 = nom0 / denom0 : frac0 = 0.0;
        denom1 > 0.0 ? frac1 = nom1 / denom1 : frac1 = 0.0;

        return frac0 + frac1;
    }
}

void BSpline::setK(int val) { m_k = val; }
int BSpline::getK() const { return m_k; }

const std::vector<glm::vec4>& BSpline::getVertices() { return m_vertices; }
const std::vector<glm::vec4>& BSpline::getControlPoints() {
    return m_controlPoints;
}