4ab965f42ecd5d2b036bf25d2f87af7f3ef12291
1 /**
2 * \file bezier.h
3 * \brief \todo brief description
4 *
5 * Copyright 2007 MenTaLguY <mental@rydia.net>
6 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
7 * Copyright 2007 Nathan Hurst <njh@njhurst.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 *
32 */
34 #ifndef SEEN_BEZIER_H
35 #define SEEN_BEZIER_H
37 #include <2geom/coord.h>
38 #include <valarray>
39 #include <2geom/isnan.h>
40 #include <2geom/d2.h>
41 #include <2geom/solver.h>
42 #include <boost/optional/optional.hpp>
44 namespace Geom {
46 inline Coord subdivideArr(Coord t, Coord const *v, Coord *left, Coord *right, unsigned order) {
47 /*
48 * Bernstein :
49 * Evaluate a Bernstein function at a particular parameter value
50 * Fill in control points for resulting sub-curves.
51 *
52 */
54 unsigned N = order+1;
55 std::valarray<Coord> vtemp(2*N);
56 for (unsigned i = 0; i < N; i++)
57 vtemp[i] = v[i];
59 // Triangle computation
60 const double omt = (1-t);
61 if(left)
62 left[0] = vtemp[0];
63 if(right)
64 right[order] = vtemp[order];
65 double *prev_row = &vtemp[0];
66 double *row = &vtemp[N];
67 for (unsigned i = 1; i < N; i++) {
68 for (unsigned j = 0; j < N - i; j++) {
69 row[j] = omt*prev_row[j] + t*prev_row[j+1];
70 }
71 if(left)
72 left[i] = row[0];
73 if(right)
74 right[order-i] = row[order-i];
75 std::swap(prev_row, row);
76 }
77 return (prev_row[0]);
78 /*
79 Coord vtemp[order+1][order+1];
81 // Copy control points
82 std::copy(v, v+order+1, vtemp[0]);
84 // Triangle computation
85 for (unsigned i = 1; i <= order; i++) {
86 for (unsigned j = 0; j <= order - i; j++) {
87 vtemp[i][j] = lerp(t, vtemp[i-1][j], vtemp[i-1][j+1]);
88 }
89 }
90 if(left != NULL)
91 for (unsigned j = 0; j <= order; j++)
92 left[j] = vtemp[j][0];
93 if(right != NULL)
94 for (unsigned j = 0; j <= order; j++)
95 right[j] = vtemp[order-j][j];
97 return (vtemp[order][0]);*/
98 }
101 class Bezier {
102 private:
103 std::valarray<Coord> c_;
105 friend Bezier portion(const Bezier & a, Coord from, Coord to);
107 friend OptInterval bounds_fast(Bezier const & b);
109 friend Bezier derivative(const Bezier & a);
111 protected:
112 Bezier(Coord const c[], unsigned ord) : c_(c, ord+1){
113 //std::copy(c, c+order()+1, &c_[0]);
114 }
116 public:
117 unsigned int order() const { return c_.size()-1;}
118 unsigned int size() const { return c_.size();}
120 Bezier() {}
121 Bezier(const Bezier& b) :c_(b.c_) {}
122 Bezier &operator=(Bezier const &other) {
123 if ( c_.size() != other.c_.size() ) {
124 c_.resize(other.c_.size());
125 }
126 c_ = other.c_;
127 return *this;
128 }
130 struct Order {
131 unsigned order;
132 explicit Order(Bezier const &b) : order(b.order()) {}
133 explicit Order(unsigned o) : order(o) {}
134 operator unsigned() const { return order; }
135 };
137 //Construct an arbitrary order bezier
138 Bezier(Order ord) : c_(0., ord.order+1) {
139 assert(ord.order == order());
140 }
142 explicit Bezier(Coord c0) : c_(0., 1) {
143 c_[0] = c0;
144 }
146 //Construct an order-1 bezier (linear Bézier)
147 Bezier(Coord c0, Coord c1) : c_(0., 2) {
148 c_[0] = c0; c_[1] = c1;
149 }
151 //Construct an order-2 bezier (quadratic Bézier)
152 Bezier(Coord c0, Coord c1, Coord c2) : c_(0., 3) {
153 c_[0] = c0; c_[1] = c1; c_[2] = c2;
154 }
156 //Construct an order-3 bezier (cubic Bézier)
157 Bezier(Coord c0, Coord c1, Coord c2, Coord c3) : c_(0., 4) {
158 c_[0] = c0; c_[1] = c1; c_[2] = c2; c_[3] = c3;
159 }
161 void resize (unsigned int n, Coord v = 0)
162 {
163 c_.resize (n, v);
164 }
166 void clear()
167 {
168 c_.resize(0);
169 }
171 inline unsigned degree() const { return order(); }
173 //IMPL: FragmentConcept
174 typedef Coord output_type;
175 inline bool isZero() const {
176 for(unsigned i = 0; i <= order(); i++) {
177 if(c_[i] != 0) return false;
178 }
179 return true;
180 }
181 inline bool isConstant() const {
182 for(unsigned i = 1; i <= order(); i++) {
183 if(c_[i] != c_[0]) return false;
184 }
185 return true;
186 }
187 inline bool isFinite() const {
188 for(unsigned i = 0; i <= order(); i++) {
189 if(!IS_FINITE(c_[i])) return false;
190 }
191 return true;
192 }
193 inline Coord at0() const { return c_[0]; }
194 inline Coord at1() const { return c_[order()]; }
196 inline Coord valueAt(double t) const {
197 int n = order();
198 double u, bc, tn, tmp;
199 int i;
200 u = 1.0 - t;
201 bc = 1;
202 tn = 1;
203 tmp = c_[0]*u;
204 for(i=1; i<n; i++){
205 tn = tn*t;
206 bc = bc*(n-i+1)/i;
207 tmp = (tmp + tn*bc*c_[i])*u;
208 }
209 return (tmp + tn*t*c_[n]);
210 //return subdivideArr(t, &c_[0], NULL, NULL, order());
211 }
212 inline Coord operator()(double t) const { return valueAt(t); }
214 SBasis toSBasis() const;
215 // inline SBasis toSBasis() const {
216 // SBasis sb;
217 // bezier_to_sbasis(sb, (*this));
218 // return sb;
219 // //return bezier_to_sbasis(&c_[0], order());
220 // }
222 //Only mutator
223 inline Coord &operator[](unsigned ix) { return c_[ix]; }
224 inline Coord const &operator[](unsigned ix) const { return const_cast<std::valarray<Coord>&>(c_)[ix]; }
225 //inline Coord const &operator[](unsigned ix) const { return c_[ix]; }
226 inline void setPoint(unsigned ix, double val) { c_[ix] = val; }
228 /* This is inelegant, as it uses several extra stores. I think there might be a way to
229 * evaluate roughly in situ. */
231 std::vector<Coord> valueAndDerivatives(Coord t, unsigned n_derivs) const {
232 std::vector<Coord> val_n_der;
233 std::valarray<Coord> d_(order()+1);
234 unsigned nn = n_derivs + 1; // the size of the result vector equals n_derivs+1 ...
235 if(nn > order())
236 nn = order()+1; // .. but with a maximum of order() + 1!
237 for(unsigned i = 0; i < size(); i++)
238 d_[i] = c_[i];
239 for(unsigned di = 0; di < nn; di++) {
240 val_n_der.push_back(subdivideArr(t, &d_[0], NULL, NULL, order() - di));
241 for(unsigned i = 0; i < order() - di; i++) {
242 d_[i] = (order()-di)*(d_[i+1] - d_[i]);
243 }
244 }
245 val_n_der.resize(n_derivs);
246 return val_n_der;
247 }
249 std::pair<Bezier, Bezier > subdivide(Coord t) const {
250 Bezier a(Bezier::Order(*this)), b(Bezier::Order(*this));
251 subdivideArr(t, &const_cast<std::valarray<Coord>&>(c_)[0], &a.c_[0], &b.c_[0], order());
252 return std::pair<Bezier, Bezier >(a, b);
253 }
255 std::vector<double> roots() const {
256 std::vector<double> solutions;
257 find_bernstein_roots(&const_cast<std::valarray<Coord>&>(c_)[0], order(), solutions, 0, 0.0, 1.0);
258 return solutions;
259 }
260 };
263 void bezier_to_sbasis (SBasis & sb, Bezier const& bz);
266 inline
267 SBasis Bezier::toSBasis() const {
268 SBasis sb;
269 bezier_to_sbasis(sb, (*this));
270 return sb;
271 //return bezier_to_sbasis(&c_[0], order());
272 }
274 //TODO: implement others
275 inline Bezier operator+(const Bezier & a, double v) {
276 Bezier result = Bezier(Bezier::Order(a));
277 for(unsigned i = 0; i <= a.order(); i++)
278 result[i] = a[i] + v;
279 return result;
280 }
282 inline Bezier operator-(const Bezier & a, double v) {
283 Bezier result = Bezier(Bezier::Order(a));
284 for(unsigned i = 0; i <= a.order(); i++)
285 result[i] = a[i] - v;
286 return result;
287 }
289 inline Bezier operator*(const Bezier & a, double v) {
290 Bezier result = Bezier(Bezier::Order(a));
291 for(unsigned i = 0; i <= a.order(); i++)
292 result[i] = a[i] * v;
293 return result;
294 }
296 inline Bezier operator/(const Bezier & a, double v) {
297 Bezier result = Bezier(Bezier::Order(a));
298 for(unsigned i = 0; i <= a.order(); i++)
299 result[i] = a[i] / v;
300 return result;
301 }
303 inline Bezier reverse(const Bezier & a) {
304 Bezier result = Bezier(Bezier::Order(a));
305 for(unsigned i = 0; i <= a.order(); i++)
306 result[i] = a[a.order() - i];
307 return result;
308 }
310 inline Bezier portion(const Bezier & a, double from, double to) {
311 //TODO: implement better?
312 std::valarray<Coord> res(a.order() + 1);
313 if(from == 0) {
314 if(to == 1) { return Bezier(a); }
315 subdivideArr(to, &const_cast<Bezier&>(a).c_[0], &res[0], NULL, a.order());
316 return Bezier(&res[0], a.order());
317 }
318 subdivideArr(from, &const_cast<Bezier&>(a).c_[0], NULL, &res[0], a.order());
319 if(to == 1) return Bezier(&res[0], a.order());
320 std::valarray<Coord> res2(a.order()+1);
321 subdivideArr((to - from)/(1 - from), &res[0], &res2[0], NULL, a.order());
322 return Bezier(&res2[0], a.order());
323 }
325 // XXX Todo: how to handle differing orders
326 inline std::vector<Point> bezier_points(const D2<Bezier > & a) {
327 std::vector<Point> result;
328 for(unsigned i = 0; i <= a[0].order(); i++) {
329 Point p;
330 for(unsigned d = 0; d < 2; d++) p[d] = a[d][i];
331 result.push_back(p);
332 }
333 return result;
334 }
336 inline Bezier derivative(const Bezier & a) {
337 //if(a.order() == 1) return Bezier(0.0);
338 if(a.order() == 1) return Bezier(a.c_[1]-a.c_[0]);
339 Bezier der(Bezier::Order(a.order()-1));
341 for(unsigned i = 0; i < a.order(); i++) {
342 der.c_[i] = a.order()*(a.c_[i+1] - a.c_[i]);
343 }
344 return der;
345 }
347 inline Bezier integral(const Bezier & a) {
348 Bezier inte(Bezier::Order(a.order()+1));
350 inte[0] = 0;
351 for(unsigned i = 0; i < inte.order(); i++) {
352 inte[i+1] = inte[i] + a[i]/(inte.order());
353 }
354 return inte;
355 }
357 inline OptInterval bounds_fast(Bezier const & b) {
358 return Interval::fromArray(&const_cast<Bezier&>(b).c_[0], b.size());
359 }
361 //TODO: better bounds exact
362 inline OptInterval bounds_exact(Bezier const & b) {
363 return bounds_exact(b.toSBasis());
364 }
366 inline OptInterval bounds_local(Bezier const & b, OptInterval i) {
367 //return bounds_local(b.toSBasis(), i);
368 if (i) {
369 return bounds_fast(portion(b, i->min(), i->max()));
370 } else {
371 return OptInterval();
372 }
373 }
375 inline std::ostream &operator<< (std::ostream &out_file, const Bezier & b) {
376 for(unsigned i = 0; i < b.size(); i++) {
377 out_file << b[i] << ", ";
378 }
379 return out_file;
380 }
382 }
383 #endif //SEEN_BEZIER_H
385 /*
386 Local Variables:
387 mode:c++
388 c-file-style:"stroustrup"
389 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
390 indent-tabs-mode:nil
391 fill-column:99
392 End:
393 */
394 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :