1 /*! ========================================================================
2 ** Extended Template and Library
3 ** B-Spline Class Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__BSPLINE_H
28 #define __ETL__BSPLINE_H
30 /* === H E A D E R S ======================================================= */
34 #include "_curve_func.h"
36 /* === M A C R O S ========================================================= */
38 /* === T Y P E D E F S ===================================================== */
40 /* === C L A S S E S & S T R U C T S ======================================= */
44 template <class T, class K=float, class C=affine_combo<T,K>, class D=distance_func<T> >
45 class bspline : public std::unary_function<K,T>
50 typedef std::vector<knot_type> knot_container;
51 typedef std::vector<value_type> cpoint_container;
52 typedef typename knot_container::iterator knot_iterator;
53 typedef typename cpoint_container::iterator cpoint_iterator;
55 typedef C affine_func_type;
56 typedef D distance_func_type;
59 affine_func_type affine_func;
60 distance_func_type distance_func;
64 knot_container _knots;
65 cpoint_container _cpoints;
69 bspline():m(2),_loop(false) { }
71 int get_m()const { return m-1; };
72 int set_m(int new_m) { m=new_m+1; return m-1; };
74 bool set_loop(bool x) { _loop=x; reset_knots(); return _loop; }
76 knot_container & knots() { return _knots; };
77 cpoint_container & cpoints() { return _cpoints; };
79 const knot_container & knots()const { return _knots; };
80 const cpoint_container & cpoints()const { return _cpoints; };
91 while(m>(signed)_cpoints.size())
94 *_knots.insert(_knots.end())=0;
95 for(i=1;i<(signed)_cpoints.size()-m+1;i++)
96 *_knots.insert(_knots.end())=i;
98 *_knots.insert(_knots.end())=_cpoints.size()-m+1;
105 while(m>(signed)_cpoints.size())
107 for(i=0;i<=(signed)_cpoints.size()-m+1;i++)
108 *_knots.insert(_knots.end())=i;
112 int calc_curve_segment(knot_type t)const
118 t=_knots.back()-0.0001;
119 for(k=0;_knots[k]>t || _knots[k+1]<=t;k++)
125 knot_container get_segment_knots(int i)const
129 knot_container ret(_knots.begin(),_knots.begin()+i+m+1);
133 if(i+1>=(signed)_knots.size())
135 knot_container ret(_knots.begin()+i-m+1,_knots.end());
138 return knot_container(_knots.begin()+i-m+1, _knots.begin()+i+m);
141 cpoint_container get_segment_cpoints(int i)const
145 return cpoint_container();
147 if(i+1>=(signed)_knots.size())
149 return cpoint_container();
151 return cpoint_container(_cpoints.begin()+i-m+1, _cpoints.begin()+i+1);
154 cpoint_container calc_shell(knot_type t, int level)const
157 i=calc_curve_segment(t),
160 knot_container u=get_segment_knots(i);
162 cpoint_container d=get_segment_cpoints(i);
165 return cpoint_container();
167 for(j=0;d.size()>1 && j<level;d.pop_back(),j++)
169 for(k=0;k<d.size()-1;k++)
171 d[k]=affine_func(d[k],d[k+1],((t-u[j+k+1])/(u[m+k]-u[j+k+1])));
177 value_type operator()(knot_type t)const
179 return get_curve_val(calc_curve_segment(t),t);
182 value_type get_curve_val(int i,knot_type t)const
187 knot_container u=get_segment_knots(i);
189 cpoint_container d=get_segment_cpoints(i);
194 for(j=0;d.size()>1;d.pop_back(),j++)
196 for(k=0;k<(signed)d.size()-1;k++)
198 d[k]=affine_func(d[k],d[k+1],((t-u[j+k+1])/(u[m+k]-u[j+k+1])));
204 cpoint_iterator find_closest_cpoint(const value_type &point, typename distance_func_type::result_type max)
206 cpoint_iterator i=_cpoints.begin();
207 cpoint_iterator ret=i;
208 typename distance_func_type::result_type dist=distance_func(point,_cpoints[0]);
210 // The distance function returns "cooked" (ie: squared)
211 // distances, so we need to cook our max distance for
212 // the comparison to work correctly.
213 max=distance_func.cook(max);
215 for(++i;i<_cpoints.end();i++)
217 typename distance_func_type::result_type thisdist=distance_func(point,*i);
227 return _cpoints.end();
233 /* -- F U N C T I O N S ----------------------------------------------------- */
235 /* -- E N D ----------------------------------------------------------------- */