Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* cairo - a vector graphics library with display and print output
  2.  *
  3.  * Copyright © 2002 University of Southern California
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it either under the terms of the GNU Lesser General Public
  7.  * License version 2.1 as published by the Free Software Foundation
  8.  * (the "LGPL") or, at your option, under the terms of the Mozilla
  9.  * Public License Version 1.1 (the "MPL"). If you do not alter this
  10.  * notice, a recipient may use your version of this file under either
  11.  * the MPL or the LGPL.
  12.  *
  13.  * You should have received a copy of the LGPL along with this library
  14.  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  15.  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  16.  * You should have received a copy of the MPL along with this library
  17.  * in the file COPYING-MPL-1.1
  18.  *
  19.  * The contents of this file are subject to the Mozilla Public License
  20.  * Version 1.1 (the "License"); you may not use this file except in
  21.  * compliance with the License. You may obtain a copy of the License at
  22.  * http://www.mozilla.org/MPL/
  23.  *
  24.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  25.  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  26.  * the specific language governing rights and limitations.
  27.  *
  28.  * The Original Code is the cairo graphics library.
  29.  *
  30.  * The Initial Developer of the Original Code is University of Southern
  31.  * California.
  32.  *
  33.  * Contributor(s):
  34.  *      Carl D. Worth <cworth@cworth.org>
  35.  */
  36.  
  37. #include "cairoint.h"
  38.  
  39. #include "cairo-slope-private.h"
  40.  
  41. cairo_bool_t
  42. _cairo_spline_init (cairo_spline_t *spline,
  43.                     cairo_spline_add_point_func_t add_point_func,
  44.                     void *closure,
  45.                     const cairo_point_t *a, const cairo_point_t *b,
  46.                     const cairo_point_t *c, const cairo_point_t *d)
  47. {
  48.     spline->add_point_func = add_point_func;
  49.     spline->closure = closure;
  50.  
  51.     spline->knots.a = *a;
  52.     spline->knots.b = *b;
  53.     spline->knots.c = *c;
  54.     spline->knots.d = *d;
  55.  
  56.     if (a->x != b->x || a->y != b->y)
  57.         _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.b);
  58.     else if (a->x != c->x || a->y != c->y)
  59.         _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.c);
  60.     else if (a->x != d->x || a->y != d->y)
  61.         _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.d);
  62.     else
  63.         return FALSE;
  64.  
  65.     if (c->x != d->x || c->y != d->y)
  66.         _cairo_slope_init (&spline->final_slope, &spline->knots.c, &spline->knots.d);
  67.     else if (b->x != d->x || b->y != d->y)
  68.         _cairo_slope_init (&spline->final_slope, &spline->knots.b, &spline->knots.d);
  69.     else
  70.         _cairo_slope_init (&spline->final_slope, &spline->knots.a, &spline->knots.d);
  71.  
  72.     return TRUE;
  73. }
  74.  
  75. static cairo_status_t
  76. _cairo_spline_add_point (cairo_spline_t *spline, cairo_point_t *point)
  77. {
  78.     cairo_point_t *prev;
  79.  
  80.     prev = &spline->last_point;
  81.     if (prev->x == point->x && prev->y == point->y)
  82.         return CAIRO_STATUS_SUCCESS;
  83.  
  84.     spline->last_point = *point;
  85.     return spline->add_point_func (spline->closure, point);
  86. }
  87.  
  88. static void
  89. _lerp_half (const cairo_point_t *a, const cairo_point_t *b, cairo_point_t *result)
  90. {
  91.     result->x = a->x + ((b->x - a->x) >> 1);
  92.     result->y = a->y + ((b->y - a->y) >> 1);
  93. }
  94.  
  95. static void
  96. _de_casteljau (cairo_spline_knots_t *s1, cairo_spline_knots_t *s2)
  97. {
  98.     cairo_point_t ab, bc, cd;
  99.     cairo_point_t abbc, bccd;
  100.     cairo_point_t final;
  101.  
  102.     _lerp_half (&s1->a, &s1->b, &ab);
  103.     _lerp_half (&s1->b, &s1->c, &bc);
  104.     _lerp_half (&s1->c, &s1->d, &cd);
  105.     _lerp_half (&ab, &bc, &abbc);
  106.     _lerp_half (&bc, &cd, &bccd);
  107.     _lerp_half (&abbc, &bccd, &final);
  108.  
  109.     s2->a = final;
  110.     s2->b = bccd;
  111.     s2->c = cd;
  112.     s2->d = s1->d;
  113.  
  114.     s1->b = ab;
  115.     s1->c = abbc;
  116.     s1->d = final;
  117. }
  118.  
  119. /* Return an upper bound on the error (squared) that could result from
  120.  * approximating a spline as a line segment connecting the two endpoints. */
  121. static double
  122. _cairo_spline_error_squared (const cairo_spline_knots_t *knots)
  123. {
  124.     double bdx, bdy, berr;
  125.     double cdx, cdy, cerr;
  126.  
  127.     /* We are going to compute the distance (squared) between each of the the b
  128.      * and c control points and the segment a-b. The maximum of these two
  129.      * distances will be our approximation error. */
  130.  
  131.     bdx = _cairo_fixed_to_double (knots->b.x - knots->a.x);
  132.     bdy = _cairo_fixed_to_double (knots->b.y - knots->a.y);
  133.  
  134.     cdx = _cairo_fixed_to_double (knots->c.x - knots->a.x);
  135.     cdy = _cairo_fixed_to_double (knots->c.y - knots->a.y);
  136.  
  137.     if (knots->a.x != knots->d.x || knots->a.y != knots->d.y) {
  138.         /* Intersection point (px):
  139.          *     px = p1 + u(p2 - p1)
  140.          *     (p - px) ∙ (p2 - p1) = 0
  141.          * Thus:
  142.          *     u = ((p - p1) ∙ (p2 - p1)) / ∥p2 - p1∥²;
  143.          */
  144.  
  145.         double dx, dy, u, v;
  146.  
  147.         dx = _cairo_fixed_to_double (knots->d.x - knots->a.x);
  148.         dy = _cairo_fixed_to_double (knots->d.y - knots->a.y);
  149.          v = dx * dx + dy * dy;
  150.  
  151.         u = bdx * dx + bdy * dy;
  152.         if (u <= 0) {
  153.             /* bdx -= 0;
  154.              * bdy -= 0;
  155.              */
  156.         } else if (u >= v) {
  157.             bdx -= dx;
  158.             bdy -= dy;
  159.         } else {
  160.             bdx -= u/v * dx;
  161.             bdy -= u/v * dy;
  162.         }
  163.  
  164.         u = cdx * dx + cdy * dy;
  165.         if (u <= 0) {
  166.             /* cdx -= 0;
  167.              * cdy -= 0;
  168.              */
  169.         } else if (u >= v) {
  170.             cdx -= dx;
  171.             cdy -= dy;
  172.         } else {
  173.             cdx -= u/v * dx;
  174.             cdy -= u/v * dy;
  175.         }
  176.     }
  177.  
  178.     berr = bdx * bdx + bdy * bdy;
  179.     cerr = cdx * cdx + cdy * cdy;
  180.     if (berr > cerr)
  181.         return berr;
  182.     else
  183.         return cerr;
  184. }
  185.  
  186. static cairo_status_t
  187. _cairo_spline_decompose_into (cairo_spline_knots_t *s1, double tolerance_squared, cairo_spline_t *result)
  188. {
  189.     cairo_spline_knots_t s2;
  190.     cairo_status_t status;
  191.  
  192.     if (_cairo_spline_error_squared (s1) < tolerance_squared)
  193.         return _cairo_spline_add_point (result, &s1->a);
  194.  
  195.     _de_casteljau (s1, &s2);
  196.  
  197.     status = _cairo_spline_decompose_into (s1, tolerance_squared, result);
  198.     if (unlikely (status))
  199.         return status;
  200.  
  201.     return _cairo_spline_decompose_into (&s2, tolerance_squared, result);
  202. }
  203.  
  204. cairo_status_t
  205. _cairo_spline_decompose (cairo_spline_t *spline, double tolerance)
  206. {
  207.     cairo_spline_knots_t s1;
  208.     cairo_status_t status;
  209.  
  210.     s1 = spline->knots;
  211.     spline->last_point = s1.a;
  212.     status = _cairo_spline_decompose_into (&s1, tolerance * tolerance, spline);
  213.     if (unlikely (status))
  214.         return status;
  215.  
  216.     return _cairo_spline_add_point (spline, &spline->knots.d);
  217. }
  218.  
  219. /* Note: this function is only good for computing bounds in device space. */
  220. cairo_status_t
  221. _cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
  222.                      void *closure,
  223.                      const cairo_point_t *p0, const cairo_point_t *p1,
  224.                      const cairo_point_t *p2, const cairo_point_t *p3)
  225. {
  226.     double x0, x1, x2, x3;
  227.     double y0, y1, y2, y3;
  228.     double a, b, c;
  229.     double t[4];
  230.     int t_num = 0, i;
  231.     cairo_status_t status;
  232.  
  233.     x0 = _cairo_fixed_to_double (p0->x);
  234.     y0 = _cairo_fixed_to_double (p0->y);
  235.     x1 = _cairo_fixed_to_double (p1->x);
  236.     y1 = _cairo_fixed_to_double (p1->y);
  237.     x2 = _cairo_fixed_to_double (p2->x);
  238.     y2 = _cairo_fixed_to_double (p2->y);
  239.     x3 = _cairo_fixed_to_double (p3->x);
  240.     y3 = _cairo_fixed_to_double (p3->y);
  241.  
  242.     /* The spline can be written as a polynomial of the four points:
  243.      *
  244.      *   (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3
  245.      *
  246.      * for 0≤t≤1.  Now, the X and Y components of the spline follow the
  247.      * same polynomial but with x and y replaced for p.  To find the
  248.      * bounds of the spline, we just need to find the X and Y bounds.
  249.      * To find the bound, we take the derivative and equal it to zero,
  250.      * and solve to find the t's that give the extreme points.
  251.      *
  252.      * Here is the derivative of the curve, sorted on t:
  253.      *
  254.      *   3t²(-p0+3p1-3p2+p3) + 2t(3p0-6p1+3p2) -3p0+3p1
  255.      *
  256.      * Let:
  257.      *
  258.      *   a = -p0+3p1-3p2+p3
  259.      *   b =  p0-2p1+p2
  260.      *   c = -p0+p1
  261.      *
  262.      * Gives:
  263.      *
  264.      *   a.t² + 2b.t + c = 0
  265.      *
  266.      * With:
  267.      *
  268.      *   delta = b*b - a*c
  269.      *
  270.      * the extreme points are at -c/2b if a is zero, at (-b±√delta)/a if
  271.      * delta is positive, and at -b/a if delta is zero.
  272.      */
  273.  
  274. #define ADD(t0) \
  275.     { \
  276.         double _t0 = (t0); \
  277.         if (0 < _t0 && _t0 < 1) \
  278.             t[t_num++] = _t0; \
  279.     }
  280.  
  281. #define FIND_EXTREMES(a,b,c) \
  282.     { \
  283.         if (a == 0) { \
  284.             if (b != 0) \
  285.                 ADD (-c / (2*b)); \
  286.         } else { \
  287.             double b2 = b * b; \
  288.             double delta = b2 - a * c; \
  289.             if (delta > 0) { \
  290.                 cairo_bool_t feasible; \
  291.                 double _2ab = 2 * a * b; \
  292.                 /* We are only interested in solutions t that satisfy 0<t<1 \
  293.                  * here.  We do some checks to avoid sqrt if the solutions \
  294.                  * are not in that range.  The checks can be derived from: \
  295.                  * \
  296.                  *   0 < (-b±√delta)/a < 1 \
  297.                  */ \
  298.                 if (_2ab >= 0) \
  299.                     feasible = delta > b2 && delta < a*a + b2 + _2ab; \
  300.                 else if (-b / a >= 1) \
  301.                     feasible = delta < b2 && delta > a*a + b2 + _2ab; \
  302.                 else \
  303.                     feasible = delta < b2 || delta < a*a + b2 + _2ab; \
  304.                 \
  305.                 if (unlikely (feasible)) { \
  306.                     double sqrt_delta = sqrt (delta); \
  307.                     ADD ((-b - sqrt_delta) / a); \
  308.                     ADD ((-b + sqrt_delta) / a); \
  309.                 } \
  310.             } else if (delta == 0) { \
  311.                 ADD (-b / a); \
  312.             } \
  313.         } \
  314.     }
  315.  
  316.     /* Find X extremes */
  317.     a = -x0 + 3*x1 - 3*x2 + x3;
  318.     b =  x0 - 2*x1 + x2;
  319.     c = -x0 + x1;
  320.     FIND_EXTREMES (a, b, c);
  321.  
  322.     /* Find Y extremes */
  323.     a = -y0 + 3*y1 - 3*y2 + y3;
  324.     b =  y0 - 2*y1 + y2;
  325.     c = -y0 + y1;
  326.     FIND_EXTREMES (a, b, c);
  327.  
  328.     status = add_point_func (closure, p0);
  329.     if (unlikely (status))
  330.         return status;
  331.  
  332.     for (i = 0; i < t_num; i++) {
  333.         cairo_point_t p;
  334.         double x, y;
  335.         double t_1_0, t_0_1;
  336.         double t_2_0, t_0_2;
  337.         double t_3_0, t_2_1_3, t_1_2_3, t_0_3;
  338.  
  339.         t_1_0 = t[i];          /*      t  */
  340.         t_0_1 = 1 - t_1_0;     /* (1 - t) */
  341.  
  342.         t_2_0 = t_1_0 * t_1_0; /*      t  *      t  */
  343.         t_0_2 = t_0_1 * t_0_1; /* (1 - t) * (1 - t) */
  344.  
  345.         t_3_0   = t_2_0 * t_1_0;     /*      t  *      t  *      t      */
  346.         t_2_1_3 = t_2_0 * t_0_1 * 3; /*      t  *      t  * (1 - t) * 3 */
  347.         t_1_2_3 = t_1_0 * t_0_2 * 3; /*      t  * (1 - t) * (1 - t) * 3 */
  348.         t_0_3   = t_0_1 * t_0_2;     /* (1 - t) * (1 - t) * (1 - t)     */
  349.  
  350.         /* Bezier polynomial */
  351.         x = x0 * t_0_3
  352.           + x1 * t_1_2_3
  353.           + x2 * t_2_1_3
  354.           + x3 * t_3_0;
  355.         y = y0 * t_0_3
  356.           + y1 * t_1_2_3
  357.           + y2 * t_2_1_3
  358.           + y3 * t_3_0;
  359.  
  360.         p.x = _cairo_fixed_from_double (x);
  361.         p.y = _cairo_fixed_from_double (y);
  362.         status = add_point_func (closure, &p);
  363.         if (unlikely (status))
  364.             return status;
  365.     }
  366.  
  367.     return add_point_func (closure, p3);
  368. }
  369.