renice x-fade rendering w/cairo-antialiasing
[ardour.git] / libs / canvas / curve.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <exception>
22 #include <algorithm>
23
24 #include "canvas/curve.h"
25
26 using namespace ArdourCanvas;
27 using std::min;
28 using std::max;
29
30 Curve::Curve (Group* parent)
31         : Item (parent)
32         , PolyItem (parent)
33         , Fill (parent)
34         , n_samples (0)
35         , points_per_segment (16)
36         , curve_type (CatmullRomCentripetal)
37         , curve_fill (None)
38 {
39 }
40
41 /** When rendering the curve, we will always draw a fixed number of straight
42  * line segments to span the x-axis extent of the curve. More segments:
43  * smoother visual rendering. Less rendering: closer to a visibily poly-line
44  * render.
45  */
46 void
47 Curve::set_points_per_segment (uint32_t n)
48 {
49         /* this only changes our appearance rather than the bounding box, so we
50            just need to schedule a redraw rather than notify the parent of any
51            changes
52         */
53         points_per_segment = n;
54         interpolate ();
55         redraw ();
56 }
57
58 void
59 Curve::compute_bounding_box () const
60 {
61         PolyItem::compute_bounding_box ();
62
63         /* possibly add extents of any point indicators here if we ever do that */
64 }
65
66 void
67 Curve::set (Points const& p)
68 {
69         PolyItem::set (p);
70         interpolate ();
71 }
72
73 void
74 Curve::interpolate ()
75 {
76         samples.clear ();
77         InterpolatedCurve::interpolate (_points, points_per_segment, CatmullRomCentripetal, false, samples);
78         n_samples = samples.size();
79 }
80
81 void
82 Curve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
83 {
84         if (!_outline || _points.size() < 2 || !_bounding_box) {
85                 return;
86         }
87
88         Rect self = item_to_window (_bounding_box.get());
89         boost::optional<Rect> d = self.intersection (area);
90         assert (d);
91         Rect draw = d.get ();
92
93         /* Our approach is to always draw n_segments across our total size.
94          *
95          * This is very inefficient if we are asked to only draw a small
96          * section of the curve. For now we rely on cairo clipping to help
97          * with this.
98          */
99         
100
101         setup_outline_context (context);
102
103         if (_points.size() == 2) {
104
105                 /* straight line */
106
107                 Duple window_space;
108
109                 window_space = item_to_window (_points.front());
110                 context->move_to (window_space.x, window_space.y);
111                 window_space = item_to_window (_points.back());
112                 context->line_to (window_space.x, window_space.y);
113
114
115                 switch (curve_fill) {
116                         case None:
117                                 context->stroke();
118                                 break;
119                         case Inside:
120                                 context->stroke_preserve ();
121                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
122                                 context->line_to (window_space.x, window_space.y);
123                                 window_space = item_to_window (Duple(_points.front().x, draw.height()));
124                                 context->line_to (window_space.x, window_space.y);
125                                 context->close_path();
126                                 setup_fill_context(context);
127                                 context->fill ();
128                                 break;
129                         case Outside:
130                                 context->stroke_preserve ();
131                                 window_space = item_to_window (Duple(_points.back().x, 0.0));
132                                 context->line_to (window_space.x, window_space.y);
133                                 window_space = item_to_window (Duple(_points.front().x, 0.0));
134                                 context->line_to (window_space.x, window_space.y);
135                                 context->close_path();
136                                 setup_fill_context(context);
137                                 context->fill ();
138                                 break;
139                 }
140
141         } else {
142
143                 /* curve of at least 3 points */
144
145                 /* x-axis limits of the curve, in window space coordinates */
146
147                 Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
148                 Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
149
150                 /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
151
152                 context->save ();
153                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
154                 context->clip ();
155
156                 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
157                    they will still occur, but cairo's clipping will hide them.
158                  */
159
160                 draw = draw.expand (4.0);
161
162                 /* now clip it to the actual points in the curve */
163                 
164                 if (draw.x0 < w1.x) {
165                         draw.x0 = w1.x;
166                 }
167
168                 if (draw.x1 >= w2.x) {
169                         draw.x1 = w2.x;
170                 }
171
172                 /* find left and right-most sample */
173                 Duple window_space;
174                 Points::size_type left = 0;
175                 Points::size_type right = n_samples;
176
177                 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
178                         left = idx;
179                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
180                         if (window_space.x >= draw.x0) break;
181                 }
182                 for (Points::size_type idx = n_samples; idx > left + 1; --idx) {
183                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
184                         if (window_space.x <= draw.x1) break;
185                         right = idx;
186                 }
187
188                 /* draw line between samples */
189                 window_space = item_to_window (Duple (samples[left].x, samples[left].y));
190                 context->move_to (window_space.x, window_space.y);
191                 Coord last_x = round(window_space.x);
192                 Coord last_y = round(window_space.y);
193                 for (uint32_t idx = left + 1; idx < right; ++idx) {
194                         window_space = item_to_window (Duple (samples[idx].x, samples[idx].y));
195                         if (last_x == round(window_space.x)) continue;
196                         if (last_y == round(window_space.y)) continue;
197                         last_x = round(window_space.x);
198                         last_y = round(window_space.y);
199                         context->line_to (last_x - .5 , last_y + .5);
200                 }
201
202                 switch (curve_fill) {
203                         case None:
204                                 context->stroke();
205                                 break;
206                         case Inside:
207                                 context->stroke_preserve ();
208                                 window_space = item_to_window (Duple (samples[right-1].x, draw.height()));
209                                 context->line_to (window_space.x, window_space.y);
210                                 window_space = item_to_window (Duple (samples[left].x, draw.height()));
211                                 context->line_to (window_space.x, window_space.y);
212                                 context->close_path();
213                                 setup_fill_context(context);
214                                 context->fill ();
215                                 break;
216                         case Outside:
217                                 context->stroke_preserve ();
218                                 window_space = item_to_window (Duple (samples[right-1].x, 0.0));
219                                 context->line_to (window_space.x, window_space.y);
220                                 window_space = item_to_window (Duple (samples[left].x, 0.0));
221                                 context->line_to (window_space.x, window_space.y);
222                                 context->close_path();
223                                 setup_fill_context(context);
224                                 context->fill ();
225                                 break;
226                 }
227                 context->restore ();
228         }
229
230 #if 0
231         /* add points */
232         setup_outline_context (context);
233         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
234                 Duple window_space (item_to_window (*p));
235                 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
236                 context->stroke ();
237         }
238 #endif
239 }
240
241 bool
242 Curve::covers (Duple const & pc) const
243 {
244         Duple point = canvas_to_item (pc);
245
246         /* O(N) N = number of points, and not accurate */
247
248         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
249
250                 const Coord dx = point.x - (*p).x;
251                 const Coord dy = point.y - (*p).y;
252                 const Coord dx2 = dx * dx;
253                 const Coord dy2 = dy * dy;
254
255                 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {
256                         return true;
257                 }
258         }
259
260         return false;
261 }