Tempo curve cleanup, now also works on optimized builds.
[ardour.git] / libs / canvas / framed_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/framed_curve.h"
25
26 using namespace ArdourCanvas;
27 using std::min;
28 using std::max;
29
30 FramedCurve::FramedCurve (Canvas* c)
31         : PolyItem (c)
32         , n_samples (0)
33         , points_per_segment (16)
34         , curve_fill (Inside)
35 {
36 }
37
38 FramedCurve::FramedCurve (Item* parent)
39         : PolyItem (parent)
40         , n_samples (0)
41         , points_per_segment (16)
42         , curve_fill (Inside)
43 {
44 }
45
46 /** When rendering the curve, we will always draw a fixed number of straight
47  * line segments to span the x-axis extent of the curve. More segments:
48  * smoother visual rendering. Less rendering: closer to a visibily poly-line
49  * render.
50  */
51 void
52 FramedCurve::set_points_per_segment (uint32_t n)
53 {
54         /* this only changes our appearance rather than the bounding box, so we
55            just need to schedule a redraw rather than notify the parent of any
56            changes
57         */
58         points_per_segment = max (n, (uint32_t) 3);
59         interpolate ();
60         redraw ();
61 }
62
63 void
64 FramedCurve::compute_bounding_box () const
65 {
66         PolyItem::compute_bounding_box ();
67
68         /* possibly add extents of any point indicators here if we ever do that */
69 }
70
71 void
72 FramedCurve::set (Points const& p)
73 {
74         PolyItem::set (p);
75         interpolate ();
76 }
77
78 void
79 FramedCurve::interpolate ()
80 {
81         Points curve_points = _points;
82
83         if (curve_points.size()) {
84                 curve_points.erase (curve_points.begin());
85         }
86         samples.clear ();
87
88         if (_points.size() == 3) {
89                 samples.push_back (curve_points.front());
90                 samples.push_back (curve_points.back());
91                 n_samples = 2;
92         } else {
93
94                 InterpolatedCurve::interpolate (curve_points, points_per_segment, CatmullRomCentripetal, false, samples);
95                 n_samples = samples.size();
96         }
97 }
98
99 void
100 FramedCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
101 {
102         if (!_outline || _points.size() < 3 || !_bounding_box) {
103                 return;
104         }
105
106         Rect self = item_to_window (_bounding_box.get());
107         boost::optional<Rect> d = self.intersection (area);
108         assert (d);
109         Rect draw = d.get ();
110
111         /* Our approach is to always draw n_segments across our total size.
112          *
113          * This is very inefficient if we are asked to only draw a small
114          * section of the curve. For now we rely on cairo clipping to help
115          * with this.
116          */
117
118         /* x-axis limits of the curve, in window space coordinates */
119
120         Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
121         Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
122
123         /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
124
125         context->save ();
126         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
127         context->clip ();
128
129         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
130            they will still occur, but cairo's clipping will hide them.
131         */
132
133         draw = draw.expand (4.0);
134
135         /* now clip it to the actual points in the curve */
136
137         if (draw.x0 < w1.x) {
138                 draw.x0 = w1.x;
139         }
140
141         if (draw.x1 >= w2.x) {
142                 draw.x1 = w2.x;
143         }
144
145         setup_outline_context (context);
146
147         if (_points.size() == 3) {
148
149                 /* straight line */
150
151                 Duple window_space;
152                 Points::const_iterator it = _points.begin();
153                 window_space = item_to_window (*it);
154                 context->move_to (window_space.x, window_space.y);
155                 ++it;
156                 window_space = item_to_window (*it, false);
157                 context->line_to (window_space.x, window_space.y);
158                 window_space = item_to_window (_points.back(), false);
159                 context->line_to (window_space.x, window_space.y);
160
161                 switch (curve_fill) {
162                         case None:
163                                 context->stroke();
164                                 break;
165                         case Inside:
166                                 context->stroke_preserve ();
167                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
168                                 context->line_to (window_space.x, window_space.y);
169                                 window_space = item_to_window (Duple(_points.front().x, draw.height()));
170                                 context->line_to (window_space.x, window_space.y);
171                                 context->close_path();
172                                 setup_fill_context(context);
173                                 context->fill ();
174                                 break;
175                         case Outside:
176                                 context->stroke_preserve ();
177                                 window_space = item_to_window (Duple(_points.back().x, 0.0));
178                                 context->line_to (window_space.x, window_space.y);
179                                 window_space = item_to_window (Duple(_points.front().x, 0.0));
180                                 context->line_to (window_space.x, window_space.y);
181                                 context->close_path();
182                                 setup_fill_context(context);
183                                 context->fill ();
184                                 break;
185                 }
186         } else {
187
188                 /* curve of at least 3 points */
189
190                 /* find left and right-most sample */
191                 Duple window_space;
192                 Points::size_type left = 0;
193                 Points::size_type right = n_samples - 1;
194
195                 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
196                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
197                         if (window_space.x >= draw.x0) {
198                                 break;
199                         }
200                         left = idx;
201                 }
202
203                 for (Points::size_type idx = left; idx < n_samples - 1; ++idx) {
204                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
205                         if (window_space.x > draw.x1) {
206                                 right = idx;
207                                 break;
208                         }
209                 }
210
211                 const Duple first_sample = Duple (samples[left].x, samples[left].y);
212
213                 /* move to the first sample's x and the draw height */
214                 window_space = item_to_window (Duple (first_sample.x, draw.height()));
215                 context->move_to (window_space.x, window_space.y);
216
217                 /* draw line to first sample and then between samples */
218                 for (uint32_t idx = left; idx <= right; ++idx) {
219                         window_space = item_to_window (Duple (samples[idx].x, samples[idx].y), false);
220                         context->line_to (window_space.x, window_space.y);
221                 }
222
223                 /* a redraw may have been requested between the last sample and the last point.
224                    if so, draw a line to the last _point.
225                 */
226                 Duple last_sample = Duple (samples[right].x, samples[right].y);
227
228                 if (draw.x1 > last_sample.x) {
229                         last_sample = Duple (_points.back().x, _points.back().y);
230                         window_space = item_to_window (last_sample, false);
231                         context->line_to (window_space.x, window_space.y);
232                 }
233
234                 switch (curve_fill) {
235                         case None:
236                                 context->stroke();
237                                 break;
238                         case Inside:
239                                 context->stroke_preserve ();
240                                 /* close the frame, possibly using the last _point's x rather than samples[right].x */
241                                 window_space = item_to_window (Duple (last_sample.x, draw.height()));
242                                 context->line_to (window_space.x, window_space.y);
243                                 window_space = item_to_window (Duple (first_sample.x, draw.height()));
244                                 context->line_to (window_space.x, window_space.y);
245                                 context->close_path();
246                                 setup_fill_context(context);
247                                 context->fill ();
248                                 break;
249                         case Outside:
250                                 context->stroke_preserve ();
251                                 window_space = item_to_window (Duple (last_sample.x, 0.0));
252                                 context->line_to (window_space.x, window_space.y);
253                                 window_space = item_to_window (Duple (first_sample.x, 0.0));
254                                 context->line_to (window_space.x, window_space.y);
255                                 context->close_path();
256                                 setup_fill_context(context);
257                                 context->fill ();
258                                 break;
259                 }
260         }
261         context->restore ();
262
263 #if 0
264         /* add points */
265         setup_outline_context (context);
266         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
267                 Duple window_space (item_to_window (*p));
268                 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
269                 context->stroke ();
270         }
271 #endif
272 }
273
274 bool
275 FramedCurve::covers (Duple const & pc) const
276 {
277         Duple point = window_to_item (pc);
278
279         /* O(N) N = number of points, and not accurate */
280
281         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
282
283                 const Coord dx = point.x - (*p).x;
284                 const Coord dy = point.y - (*p).y;
285                 const Coord dx2 = dx * dx;
286                 const Coord dy2 = dy * dy;
287
288                 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {
289                         return true;
290                 }
291         }
292
293         return false;
294 }