Tempo ramps - add Canvas::FramedCurve and use it in the tempo marker bar.
[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 (None)
35 {
36 }
37
38 FramedCurve::FramedCurve (Item* parent)
39         : PolyItem (parent)
40         , n_samples (0)
41         , points_per_segment (16)
42         , curve_fill (None)
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 = n;
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         InterpolatedCurve::interpolate (curve_points, points_per_segment, CatmullRomCentripetal, false, samples);
89         n_samples = samples.size();
90 }
91
92 void
93 FramedCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
94 {
95         if (!_outline || _points.size() < 3 || !_bounding_box) {
96                 return;
97         }
98
99         Rect self = item_to_window (_bounding_box.get());
100         boost::optional<Rect> d = self.intersection (area);
101         assert (d);
102         Rect draw = d.get ();
103
104         /* Our approach is to always draw n_segments across our total size.
105          *
106          * This is very inefficient if we are asked to only draw a small
107          * section of the curve. For now we rely on cairo clipping to help
108          * with this.
109          */
110
111
112         setup_outline_context (context);
113
114         if (_points.size() == 3) {
115
116                 /* straight line */
117
118                 Duple window_space;
119                 Points::const_iterator it = _points.begin();
120                 window_space = item_to_window (*it);
121                 context->move_to (window_space.x, window_space.y);
122                 ++it;
123                 window_space = item_to_window (*it);
124                 context->line_to (window_space.x, window_space.y);
125                 window_space = item_to_window (_points.back());
126                 context->line_to (window_space.x, window_space.y);
127
128                 switch (curve_fill) {
129                         case None:
130                                 context->stroke();
131                                 break;
132                         case Inside:
133                                 context->stroke_preserve ();
134                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
135                                 context->line_to (window_space.x, window_space.y);
136                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
137                                 context->line_to (window_space.x, window_space.y);
138                                 context->close_path();
139                                 setup_fill_context(context);
140                                 context->fill ();
141                                 break;
142                         case Outside:
143                                 context->stroke_preserve ();
144                                 window_space = item_to_window (Duple(_points.back().x, 0.0));
145                                 context->line_to (window_space.x, window_space.y);
146                                 window_space = item_to_window (Duple(_points.front().x, 0.0));
147                                 context->line_to (window_space.x, window_space.y);
148                                 context->close_path();
149                                 setup_fill_context(context);
150                                 context->fill ();
151                                 break;
152                 }
153
154         } else {
155
156                 /* curve of at least 3 points */
157
158                 /* x-axis limits of the curve, in window space coordinates */
159
160                 Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
161                 Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
162
163                 /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
164
165                 context->save ();
166                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
167                 context->clip ();
168
169                 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
170                    they will still occur, but cairo's clipping will hide them.
171                  */
172
173                 draw = draw.expand (4.0);
174
175                 /* now clip it to the actual points in the curve */
176
177                 if (draw.x0 < w1.x) {
178                         draw.x0 = w1.x;
179                 }
180
181                 if (draw.x1 >= w2.x) {
182                         draw.x1 = w2.x;
183                 }
184
185                 /* find left and right-most sample */
186                 Duple window_space;
187                 Points::size_type left = 0;
188                 Points::size_type right = n_samples;
189
190                 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
191                         left = idx;
192                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
193                         if (window_space.x >= draw.x0) break;
194                 }
195                 for (Points::size_type idx = n_samples; idx > left + 1; --idx) {
196                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
197                         if (window_space.x <= draw.x1) break;
198                         right = idx;
199                 }
200
201                 /* draw line between samples */
202                 window_space = item_to_window (Duple (samples[left].x, samples[left].y));
203                 context->move_to (window_space.x, window_space.y);
204                 for (uint32_t idx = left + 1; idx < right; ++idx) {
205                         window_space = item_to_window (Duple (samples[idx].x, samples[idx].y), false);
206                         context->line_to (window_space.x, window_space.y);
207                 }
208
209                 switch (curve_fill) {
210                         case None:
211                                 context->stroke();
212                                 break;
213                         case Inside:
214                                 context->stroke_preserve ();
215                                 window_space = item_to_window (Duple (samples[right-1].x, draw.height()));
216                                 context->line_to (window_space.x, window_space.y);
217                                 window_space = item_to_window (Duple (samples[left].x, draw.height()));
218                                 context->line_to (window_space.x, window_space.y);
219                                 context->close_path();
220                                 setup_fill_context(context);
221                                 context->fill ();
222                                 break;
223                         case Outside:
224                                 context->stroke_preserve ();
225                                 window_space = item_to_window (Duple (samples[right-1].x, 0.0));
226                                 context->line_to (window_space.x, window_space.y);
227                                 window_space = item_to_window (Duple (samples[left].x, 0.0));
228                                 context->line_to (window_space.x, window_space.y);
229                                 context->close_path();
230                                 setup_fill_context(context);
231                                 context->fill ();
232                                 break;
233                 }
234                 context->restore ();
235         }
236
237 #if 0
238         /* add points */
239         setup_outline_context (context);
240         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
241                 Duple window_space (item_to_window (*p));
242                 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
243                 context->stroke ();
244         }
245 #endif
246 }
247
248 bool
249 FramedCurve::covers (Duple const & pc) const
250 {
251         Duple point = window_to_item (pc);
252
253         /* O(N) N = number of points, and not accurate */
254
255         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
256
257                 const Coord dx = point.x - (*p).x;
258                 const Coord dy = point.y - (*p).y;
259                 const Coord dx2 = dx * dx;
260                 const Coord dy2 = dy * dy;
261
262                 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {
263                         return true;
264                 }
265         }
266
267         return false;
268 }