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++);
124 knot_container get_segment_knots(int i)const
128 knot_container ret(_knots.begin(),_knots.begin()+i+m+1);
132 if(i+1>=(signed)_knots.size())
134 knot_container ret(_knots.begin()+i-m+1,_knots.end());
137 return knot_container(_knots.begin()+i-m+1, _knots.begin()+i+m);
140 cpoint_container get_segment_cpoints(int i)const
144 return cpoint_container();
146 if(i+1>=(signed)_knots.size())
148 return cpoint_container();
150 return cpoint_container(_cpoints.begin()+i-m+1, _cpoints.begin()+i+1);
153 cpoint_container calc_shell(knot_type t, int level)const
156 i=calc_curve_segment(t),
159 knot_container u=get_segment_knots(i);
161 cpoint_container d=get_segment_cpoints(i);
164 return cpoint_container();
166 for(j=0;d.size()>1 && j<level;d.pop_back(),j++)
168 for(k=0;k<d.size()-1;k++)
170 d[k]=affine_func(d[k],d[k+1],((t-u[j+k+1])/(u[m+k]-u[j+k+1])));
176 value_type operator()(knot_type t)const
178 return get_curve_val(calc_curve_segment(t),t);
181 value_type get_curve_val(int i,knot_type t)const
186 knot_container u=get_segment_knots(i);
188 cpoint_container d=get_segment_cpoints(i);
193 for(j=0;d.size()>1;d.pop_back(),j++)
195 for(k=0;k<(signed)d.size()-1;k++)
197 d[k]=affine_func(d[k],d[k+1],((t-u[j+k+1])/(u[m+k]-u[j+k+1])));
203 cpoint_iterator find_closest_cpoint(const value_type &point, typename distance_func_type::result_type max)
205 cpoint_iterator i=_cpoints.begin();
206 cpoint_iterator ret=i;
207 typename distance_func_type::result_type dist=distance_func(point,_cpoints[0]);
209 // The distance function returns "cooked" (ie: squared)
210 // distances, so we need to cook our max distance for
211 // the comparison to work correctly.
212 max=distance_func.cook(max);
214 for(++i;i<_cpoints.end();i++)
216 typename distance_func_type::result_type thisdist=distance_func(point,*i);
226 return _cpoints.end();
232 /* -- F U N C T I O N S ----------------------------------------------------- */
234 /* -- E N D ----------------------------------------------------------------- */