Totally break Quadtree Béziers.
[ipdf/code.git] / src / bezier.h
1 #ifndef _BEZIER_H
2 #define _BEZIER_H
3
4 #include "real.h"
5 #include "rect.h"
6 namespace IPDF
7 {
8         extern int Factorial(int n);
9         extern int BinomialCoeff(int n, int k);
10         extern Real Bernstein(int k, int n, const Real & u);
11         
12         inline std::pair<Real,Real> SolveQuadratic(const Real & a, const Real & b, const Real & c)
13         {
14                 Real x0((-b + Sqrt(b*b - Real(4)*a*c))/(Real(2)*a));
15                 Real x1((-b - Sqrt(b*b - Real(4)*a*c))/(Real(2)*a));
16                 return std::pair<Real,Real>(x0,x1);
17         }
18
19         inline std::vector<Real> SolveCubic(const Real & a, const Real & b, const Real & c, const Real & d)
20         {
21                 // This is going to be a big one...
22                 // See http://en.wikipedia.org/wiki/Cubic_function#General_formula_for_roots
23
24                 // delta = 18abcd - 4 b^3 d + b^2 c^2 - 4ac^3 - 27 a^2 d^2
25                 /*
26                 Real discriminant = Real(18) * a * b * c * d - Real(4) * (b * b * b) * d 
27                                 + (b * b) * (c * c) - Real(4) * a * (c * c * c)
28                                 - Real(27) * (a * a) * (d * d);
29                 */
30                 // discriminant > 0 => 3 distinct, real roots.
31                 // discriminant = 0 => a multiple root (1 or 2 real roots)
32                 // discriminant < 0 => 1 real root, 2 complex conjugate roots
33
34                 ////HACK: We know any roots we care about will be between 0 and 1, so...
35                 Real maxi(100);
36                 Real prevRes(d);
37                 std::vector<Real> roots;
38                 for(int i = 0; i <= 100; ++i)
39                 {
40                         Real x(i);
41                         x /= maxi;
42                         Real y = a*(x*x*x) + b*(x*x) + c*x + d;
43                         if (y == Real(0) || (y < Real(0) && prevRes > Real(0)) || (y > Real(0) && prevRes < Real(0)))
44                         {
45                                 roots.push_back(x);
46                         }
47                 }
48                 return roots;
49                         
50         }
51
52         /** A _cubic_ bezier. **/
53         struct Bezier
54         {
55                 Real x0; Real y0;
56                 Real x1; Real y1;
57                 Real x2; Real y2;
58                 Real x3; Real y3;
59                 Bezier() = default; // Needed so we can fread/fwrite this struct... for now.
60                 Bezier(Real _x0, Real _y0, Real _x1, Real _y1, Real _x2, Real _y2, Real _x3, Real _y3) : x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2), x3(_x3), y3(_y3) 
61                 {
62                         
63                 }
64                 
65                 Bezier(Real _x0, Real _y0, Real _x1, Real _y1, Real _x2, Real _y2) : x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2), x3(_x2), y3(_y2) {}
66                 
67                 std::string Str() const
68                 {
69                         std::stringstream s;
70                         s << "Bezier{" << Float(x0) << "," << Float(y0) << " -> " << Float(x1) << "," << Float(y1) << " -> " << Float(x2) << "," << Float(y2) << " -> " << Float(x3) << "," << Float(y3) << "}";
71                         return s.str();
72                 }
73                 
74                 /**
75                  * Construct absolute control points using relative control points to a bounding rectangle
76                  * ie: If cpy is relative to bounds rectangle, this will be absolute
77                  */
78                 Bezier(const Bezier & cpy, const Rect & t = Rect(0,0,1,1)) : x0(cpy.x0), y0(cpy.y0), x1(cpy.x1), y1(cpy.y1), x2(cpy.x2),y2(cpy.y2), x3(cpy.x3), y3(cpy.y3)
79                 {
80                         x0 *= t.w;
81                         y0 *= t.h;
82                         x1 *= t.w;
83                         y1 *= t.h;
84                         x2 *= t.w;
85                         y2 *= t.h;
86                         x3 *= t.w;
87                         y3 *= t.h;
88                         x0 += t.x;
89                         y0 += t.y;
90                         x1 += t.x;
91                         y1 += t.y;
92                         x2 += t.x;
93                         y2 += t.y;
94                         x3 += t.x;
95                         y3 += t.y;
96                 }
97
98                 Rect SolveBounds() const;
99                 
100                 Bezier ToAbsolute(const Rect & bounds) const
101                 {
102                         return Bezier(*this, bounds);
103                 }
104                 
105                 /** Convert absolute control points to control points relative to bounds
106                  * (This basically does the opposite of the Copy constructor)
107                  * ie: If this is absolute, the returned Bezier will be relative to the bounds rectangle
108                  */
109                 Bezier ToRelative(const Rect & bounds) const
110                 {
111                         // x' <- (x - x0)/w etc
112                         // special cases when w or h = 0
113                         // (So can't just use the Copy constructor on the inverse of bounds)
114                         // Rect inverse = {-bounds.x/bounds.w, -bounds.y/bounds.h, Real(1)/bounds.w, Real(1)/bounds.h};
115                         Bezier result;
116                         if (bounds.w == 0)
117                         {
118                                 result.x0 = 0;
119                                 result.x1 = 0;
120                                 result.x2 = 0;
121                                 result.x3 = 0;
122                         }
123                         else
124                         {
125                                 result.x0 = (x0 - bounds.x)/bounds.w;   
126                                 result.x1 = (x1 - bounds.x)/bounds.w;
127                                 result.x2 = (x2 - bounds.x)/bounds.w;
128                                 result.x3 = (x3 - bounds.x)/bounds.w;
129                         }
130
131                         if (bounds.h == 0)
132                         {
133                                 result.y0 = 0;
134                                 result.y1 = 0;
135                                 result.y2 = 0;
136                                 result.y3 = 0;
137                         }
138                         else
139                         {
140                                 result.y0 = (y0 - bounds.y)/bounds.h;   
141                                 result.y1 = (y1 - bounds.y)/bounds.h;
142                                 result.y2 = (y2 - bounds.y)/bounds.h;
143                                 result.y3 = (y3 - bounds.y)/bounds.h;
144                         }
145                         return result;
146                 }
147
148                 Bezier ReParametrise(const Real& t0, const Real& t1)
149                 {
150                         // This function is very, very ugly, but with luck my derivation is correct (even if it isn't optimal, performance wise)
151                         // (Very) rough working for the derivation is at: http://davidgow.net/stuff/cubic_bezier_reparam.pdf
152                         Bezier new_bezier;
153                         Real tdiff = t1 - t0;
154                         Real tdiff_squared = tdiff*tdiff;
155                         Real tdiff_cubed = tdiff*tdiff_squared;
156
157                         Real t0_squared = t0*t0;
158                         Real t0_cubed = t0*t0_squared;
159                         
160                         // X coordinates
161                         Real Dx0 = x0 / tdiff_cubed;
162                         Real Dx1 = x1 / (tdiff_squared - tdiff_cubed);
163                         Real Dx2 = x2 / (tdiff - Real(2)*tdiff_squared + tdiff_cubed);
164                         Real Dx3 = x3 / (Real(1) - Real(3)*tdiff + Real(3)*tdiff_squared - tdiff_cubed);
165
166                         new_bezier.x3 = Dx3*t0_cubed + Real(3)*Dx3*t0_squared + Real(3)*Dx3*t0 + Dx3 - Dx2*t0_cubed - Real(2)*Dx2*t0_squared - Dx2*t0 + Dx1*t0_cubed + Dx1*t0_squared - Dx0*t0_cubed;
167                         new_bezier.x2 = Real(3)*Dx0*t0_squared - Real(2)*Dx1*t0 - Real(3)*Dx1*t0_squared + Dx2 + Real(4)*Dx2*t0 + Real(3)*Dx2*t0_squared - Real(3)*Dx3 - Real(6)*Dx3*t0 - Real(3)*Dx3*t0_squared + Real(3)*new_bezier.x3;
168                         new_bezier.x1 = Real(-3)*Dx0*t0 + Real(3)*Dx1*t0 + Dx1 - Real(2)*Dx2 - Real(3)*Dx2*t0 + Real(3)*Dx3 + Real(3)*Dx3*t0 + Real(2)*new_bezier.x2 - Real(3)*new_bezier.x3;
169                         new_bezier.x0 = Dx0 - Dx1 + Dx2 - Dx3 + new_bezier.x1 - new_bezier.x2 + new_bezier.x3;
170
171                         // Y coordinates
172                         Real Dy0 = y0 / tdiff_cubed;
173                         Real Dy1 = y1 / (tdiff_squared - tdiff_cubed);
174                         Real Dy2 = y2 / (tdiff - Real(2)*tdiff_squared + tdiff_cubed);
175                         Real Dy3 = y3 / (Real(1) - Real(3)*tdiff + Real(3)*tdiff_squared - tdiff_cubed);
176
177                         new_bezier.y3 = Dy3*t0_cubed + Real(3)*Dy3*t0_squared + Real(3)*Dy3*t0 + Dy3 - Dy2*t0_cubed - Real(2)*Dy2*t0_squared - Dy2*t0 + Dy1*t0_cubed + Dy1*t0_squared - Dy0*t0_cubed;
178                         new_bezier.y2 = Real(3)*Dy0*t0_squared - Real(2)*Dy1*t0 - Real(3)*Dy1*t0_squared + Dy2 + Real(4)*Dy2*t0 + Real(3)*Dy2*t0_squared - Real(3)*Dy3 - Real(6)*Dy3*t0 - Real(3)*Dy3*t0_squared + Real(3)*new_bezier.y3;
179                         new_bezier.y1 = Real(-3)*Dy0*t0 + Real(3)*Dy1*t0 + Dy1 - Real(2)*Dy2 - Real(3)*Dy2*t0 + Real(3)*Dy3 + Real(3)*Dy3*t0 + Real(2)*new_bezier.y2 - Real(3)*new_bezier.y3;
180                         new_bezier.y0 = Dy0 - Dy1 + Dy2 - Dy3 + new_bezier.y1 - new_bezier.y2 + new_bezier.y3;
181
182
183                         return new_bezier;
184                 }
185                 
186                 std::vector<Bezier> ClipToRectangle(const Rect& r)
187                 {
188                         // Find points of intersection with the rectangle.
189
190                         // Convert bezier coefficients -> cubic coefficients
191                         Real xa = x0-x1+x2-x3;
192                         Real xb = x1 - Real(2)*x2 + Real(3)*x3;
193                         Real xc = x2 - Real(3)*x3;
194                         Real xd = x3 + r.x;
195
196                         // Find its roots.
197                         std::vector<Real> x_intersection = SolveCubic(xa, xb, xc, xd);
198
199                         // And for the other side.
200                         xd = x3 + r.x + r.w;
201
202                         std::vector<Real> x_intersection_pt2 = SolveCubic(xa, xb, xc, xd);
203                         x_intersection.insert(x_intersection.end(), x_intersection_pt2.begin(), x_intersection_pt2.end());
204
205                         // Similarly for y-coordinates.
206                         // Convert bezier coefficients -> cubic coefficients
207                         Real ya = y0-y1+y2-y3;
208                         Real yb = y1 - Real(2)*y2 + Real(3)*y3;
209                         Real yc = y2 - Real(3)*y3;
210                         Real yd = y3 + r.y;
211
212                         // Find its roots.
213                         std::vector<Real> y_intersection = SolveCubic(ya, yb, yc, yd);
214
215                         // And for the other side.
216                         yd = y3 + r.y + r.h;
217
218                         std::vector<Real> y_intersection_pt2 = SolveCubic(ya, yb, yc, yd);
219                         y_intersection.insert(y_intersection.end(), y_intersection_pt2.begin(), y_intersection_pt2.end());
220
221                         // Merge and sort.
222                         x_intersection.insert(x_intersection.end(), y_intersection.begin(), y_intersection.end());
223
224                         Debug("Found %d intersections.\n", x_intersection.size());
225                         
226                         std::vector<Bezier> all_beziers;
227                         if (x_intersection.empty())
228                         {
229                                 all_beziers.push_back(*this);
230                                 return all_beziers;
231                         }
232                         Real t0 = *(x_intersection.begin());
233                         for (auto it = x_intersection.begin()+1; it != x_intersection.end(); ++it)
234                         {
235                                 Real t1 = *it;
236                                 all_beziers.push_back(this->ReParametrise(t0, t1));
237                                 t0 = t1;
238                         }
239                         return all_beziers;
240                 }
241
242                 /** Evaluate the Bezier at parametric parameter u, puts resultant point in (x,y) **/
243                 void Evaluate(Real & x, Real & y, const Real & u) const
244                 {
245                         Real coeff[4];
246                         for (unsigned i = 0; i < 4; ++i)
247                                 coeff[i] = Bernstein(i,3,u);
248                         x = x0*coeff[0] + x1*coeff[1] + x2*coeff[2] + x3*coeff[3];
249                         y = y0*coeff[0] + y1*coeff[1] + y2*coeff[2] + y3*coeff[3];
250                 }
251
252         };
253
254
255
256 }
257
258 #endif //_BEZIER_H

UCC git Repository :: git.ucc.asn.au