MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
bspline.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 MATH_BSPLINE_HEADER
11#define MATH_BSPLINE_HEADER
12
13#include <vector>
14
15#include "math/defines.h"
16#include "math/matrix.h"
17
19
35template <class V, class T = float>
37{
38public:
39 typedef std::vector<T> KnotVector;
40 typedef std::vector<V> PointVector;
41
42public:
43 BSpline (void);
44
46 bool empty (void) const;
47
49 void set_degree (int degree);
51 int get_degree (void) const;
52
54 void reserve (std::size_t n_points);
55
57 void add_point (V const& p);
59 void add_knot (T const& t);
61 void uniform_knots (T const& min, T const& max);
63 void scale_knots (T const& min, T const& max);
64
66 PointVector const& get_points (void) const;
68 KnotVector const& get_knots (void) const;
69
71 V evaluate (T const& t) const;
72
74 void transform (math::Matrix4f const& transf);
75
76private:
78 T deboor (int i, int k, T const& x) const;
79
80private:
81 int n;
82 KnotVector knots;
83 PointVector points;
84};
85
87
88/* ----------------------- Implementation ------------------------ */
89
91
92template <class V, class T>
93inline
95 : n(3)
96{
97}
98
99template <class V, class T>
100bool
102{
103 return this->points.empty();
104}
105
106template <class V, class T>
107inline void
109{
110 this->n = degree;
111}
112
113template <class V, class T>
114inline int
116{
117 return this->n;
118}
119
120template <class V, class T>
121inline void
122BSpline<V,T>::reserve (std::size_t n_points)
123{
124 this->points.reserve(n_points);
125 this->knots.reserve(n_points + n + 1);
126}
127
128template <class V, class T>
129inline void
131{
132 this->points.push_back(p);
133}
134
135template <class V, class T>
136inline void
138{
139 this->knots.push_back(t);
140}
141
142template <class V, class T>
143inline void
145{
146 T width = max - min;
147 int n_knots = this->points.size() + this->n + 1;
148 int segments = this->points.size() - this->n;
149 this->knots.clear();
150 this->knots.reserve(n_knots);
151
152 for (int i = 0; i < this->n; ++i)
153 this->knots.push_back(min);
154 for (int i = 0; i < segments + 1; ++i)
155 this->knots.push_back(min + T(i) * width / T(segments));
156 for (int i = 0; i < this->n - 1; ++i)
157 this->knots.push_back(max);
158 if (this->n > 0)
159 this->knots.push_back(max + T(1));
160
161#if 0
162 std::cout << "Made knots: ";
163 for (std::size_t i = 0; i < this->knots.size(); ++i)
164 std::cout << this->knots[i] << " ";
165 std::cout << std::endl;
166#endif
167}
168
169
170template <class V, class T>
171inline void
173{
174 T first = this->knots[this->n];
175 T const& last = this->knots[this->knots.size() - this->n - 1];
176 T scale = (max - min) / (last - first);
177 for (std::size_t i = 0; i < this->knots.size(); ++i)
178 this->knots[i] = (this->knots[i] - first) * scale;
179}
180
181template <class V, class T>
182inline typename BSpline<V,T>::PointVector const&
184{
185 return this->points;
186}
187
188template <class V, class T>
189inline typename BSpline<V,T>::KnotVector const&
191{
192 return this->knots;
193}
194
195template <class V, class T>
196inline V
197BSpline<V,T>::evaluate (T const& t) const
198{
199 /* FIXME inefficient */
200 V p = this->points[0] * this->deboor(0, this->n, t);
201 for (std::size_t i = 1; i < this->points.size(); ++i)
202 p += this->points[i] * this->deboor(i, this->n, t);
203 return p;
204}
205
206template <class V, class T>
207inline T
208BSpline<V,T>::deboor (int i, int k, T const& x) const
209{
210 if (k == 0)
211 {
212 //FIXME
213 return (x >= this->knots[i] && x < this->knots[i+1]) ? T(1) : T(0);
214 //return (x == this->knots[i] || (x > this->knots[i] && x < this->knots[i+1])) ? T(1) : T(0);
215 }
216 float d1 = this->knots[i+k] - this->knots[i];
217 float d2 = this->knots[i+k+1] - this->knots[i+1];
218 T v1 = d1 > T(0) ? (x - this->knots[i]) / d1 : T(0);
219 T v2 = d2 > T(0) ? (this->knots[i+k+1] - x) / d2 : T(0);
220 return v1 * deboor(i, k-1, x) + v2 * deboor(i+1, k-1, x);
221}
222
223template <class V, class T>
224inline void
226{
227 for (std::size_t i = 0; i < points.size(); ++i)
228 points[i] = transf.mult(points[i], 1.0f);
229}
230
232
233#endif /* MATH_BSPLINE_HEADER */
Implementation of non-uniform B-Spline curves according to.
Definition bspline.h:37
void scale_knots(T const &min, T const &max)
Scales the knots such that evaluation is valid in [min, max].
Definition bspline.h:172
void set_degree(int degree)
Sets the degree of spline segments.
Definition bspline.h:108
void transform(math::Matrix4f const &transf)
Transforms the B-Spline.
Definition bspline.h:225
void add_point(V const &p)
Adds a point to the control point vector.
Definition bspline.h:130
std::vector< T > KnotVector
Definition bspline.h:39
void reserve(std::size_t n_points)
Reserves space for points and the knot vector.
Definition bspline.h:122
bool empty(void) const
Returns whether there are no points in this spline.
Definition bspline.h:101
void uniform_knots(T const &min, T const &max)
Initializes the knot vector to be uniform.
Definition bspline.h:144
PointVector const & get_points(void) const
Returns the point vector.
Definition bspline.h:183
V evaluate(T const &t) const
Evalutes the B-Spline.
Definition bspline.h:197
std::vector< V > PointVector
Definition bspline.h:40
void add_knot(T const &t)
Adds a knot to the knot vector.
Definition bspline.h:137
KnotVector const & get_knots(void) const
Returns the knot vector.
Definition bspline.h:190
int get_degree(void) const
Returns the degree of the spline segments.
Definition bspline.h:115
Matrix class for arbitrary dimensions and types.
Definition matrix.h:54
Matrix< T, N, U > mult(Matrix< T, M, U > const &rhs) const
Matrix with matrix multiplication.
Definition matrix.h:462
#define MATH_NAMESPACE_BEGIN
Definition defines.h:15
#define MATH_NAMESPACE_END
Definition defines.h:16
std::size_t first
Definition mesh_info.cc:46
T const & max(T const &a, T const &b, T const &c)
Returns the maximum value of three arguments.
Definition functions.h:227
T const & min(T const &a, T const &b, T const &c)
Returns the minimum value of three arguments.
Definition functions.h:219