MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
bezier_curve.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef UTIL_BEZIERCURVE_HEADER
11#define UTIL_BEZIERCURVE_HEADER
12
13#include <vector>
14#include <stdexcept>
15
16#include "math/defines.h"
17
19
23template <class T>
25{
26public:
27 BezierCurve (void);
28 virtual ~BezierCurve (void);
29
36 void append_point (T const& p);
37
39 void clear (void);
40
42 std::size_t size (void) const;
43
45 T const& operator[] (std::size_t index) const;
46
48 T evaluate (float t) const;
49
50private:
51 typedef std::vector<T> ControlPointVector;
52 ControlPointVector cp; // The control points
53};
54
55/* ------------------------------------------------------------ */
56
57template <class T>
58inline
62
63template <class T>
64inline
68
69template <class T>
70inline void
72{
73 this->cp.push_back(p);
74}
75
76template <class T>
77inline T
79{
80 if (this->cp.size() < 2)
81 throw std::domain_error("Curve must have at least two end points!");
82
83 /*
84 * Implementation of the de Casteljau algorithm.
85 * Complexity: O(d^2) for polynomial degree d = size(cp) - 1.
86 *
87 * 1. Create copy of the control point vector.
88 * 2. Simplify using linear interpolation.
89 * 3. Return the value of the last simplification step.
90 */
91
92 t = std::max(0.0f, std::min(1.0f, t));
93 ControlPointVector cpr(this->cp);
94 while (cpr.size() > 1)
95 {
96 for (std::size_t i = 0; i < cpr.size() - 1; ++i)
97 cpr[i] = cpr[i] * (1.0f - t) + cpr[i+1] * t;
98 cpr.pop_back();
99 }
100
101 return cpr.front();
102}
103
104template <class T>
105inline void
107{
108 this->cp.clear();
109}
110
111
112template <class T>
113inline std::size_t
115{
116 return this->cp.size();
117}
118
119template <class T>
120inline T const&
121BezierCurve<T>::operator[] (std::size_t index) const
122{
123 return this->cp[index];
124}
125
127
128#endif/* UTIL_BEZIERCURVE_HEADER */
A class for defining and evaluating Bezier curves in all dimensions.
void clear(void)
Removes all control points, resetting the curve.
std::size_t size(void) const
Returns the amount of control points in the vector.
#define MATH_NAMESPACE_BEGIN
Definition defines.h:15
#define MATH_NAMESPACE_END
Definition defines.h:16