fix crash when copy'ing latent plugins
[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 = 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         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         /* x-axis limits of the curve, in window space coordinates */
112
113         Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
114         Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
115
116         /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
117
118         context->save ();
119         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
120         context->clip ();
121
122         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
123            they will still occur, but cairo's clipping will hide them.
124         */
125
126         draw = draw.expand (4.0);
127
128         /* now clip it to the actual points in the curve */
129
130         if (draw.x0 < w1.x) {
131                 draw.x0 = w1.x;
132         }
133
134         if (draw.x1 >= w2.x) {
135                 draw.x1 = w2.x;
136         }
137
138         setup_outline_context (context);
139
140         if (_points.size() == 3) {
141
142                 /* straight line */
143
144                 Duple window_space;
145                 Points::const_iterator it = _points.begin();
146                 window_space = item_to_window (*it);
147                 context->move_to (window_space.x, window_space.y);
148                 ++it;
149                 window_space = item_to_window (*it, false);
150                 context->line_to (window_space.x, window_space.y);
151                 window_space = item_to_window (_points.back(), false);
152                 context->line_to (window_space.x, window_space.y);
153
154                 switch (curve_fill) {
155                         case None:
156                                 context->stroke();
157                                 break;
158                         case Inside:
159                                 context->stroke_preserve ();
160                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
161                                 context->line_to (window_space.x, window_space.y);
162                                 window_space = item_to_window (Duple(_points.front().x, draw.height()));
163                                 context->line_to (window_space.x, window_space.y);
164                                 context->close_path();
165                                 setup_fill_context(context);
166                                 context->fill ();
167                                 break;
168                         case Outside:
169                                 context->stroke_preserve ();
170                                 window_space = item_to_window (Duple(_points.back().x, 0.0));
171                                 context->line_to (window_space.x, window_space.y);
172                                 window_space = item_to_window (Duple(_points.front().x, 0.0));
173                                 context->line_to (window_space.x, window_space.y);
174                                 context->close_path();
175                                 setup_fill_context(context);
176                                 context->fill ();
177                                 break;
178                 }
179         } else {
180
181                 /* curve of at least 3 points */
182
183                 /* find left and right-most sample */
184                 Duple window_space;
185                 Points::size_type left = 0;
186                 Points::size_type right = n_samples - 1;
187
188                 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
189                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
190                         if (window_space.x >= draw.x0) {
191                                 break;
192                         }
193                         left = idx;
194                 }
195
196                 for (Points::size_type idx = left; idx < n_samples - 1; ++idx) {
197                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
198                         if (window_space.x > draw.x1) {
199                                 right = idx;
200                                 break;
201                         }
202                 }
203
204                 const Duple first_sample = Duple (samples[left].x, samples[left].y);
205
206                 /* move to the first sample's x and the draw height */
207                 window_space = item_to_window (Duple (first_sample.x, draw.height()));
208                 context->move_to (window_space.x, window_space.y);
209
210                 /* draw line to first sample and then between samples */
211                 for (uint32_t idx = left; idx <= right; ++idx) {
212                         window_space = item_to_window (Duple (samples[idx].x, samples[idx].y), false);
213                         context->line_to (window_space.x, window_space.y);
214                 }
215
216                 /* a redraw may have been requested between the last sample and the last point.
217                    if so, draw a line to the last _point.
218                 */
219                 Duple last_sample = Duple (samples[right].x, samples[right].y);
220
221                 if (draw.x1 > last_sample.x) {
222                         last_sample = Duple (_points.back().x, _points.back().y);
223                         window_space = item_to_window (last_sample, false);
224                         context->line_to (window_space.x, window_space.y);
225                 }
226
227                 switch (curve_fill) {
228                         case None:
229                                 context->stroke();
230                                 break;
231                         case Inside:
232                                 context->stroke_preserve ();
233                                 /* close the frame, possibly using the last _point's x rather than samples[right].x */
234                                 window_space = item_to_window (Duple (last_sample.x, draw.height()));
235                                 context->line_to (window_space.x, window_space.y);
236                                 window_space = item_to_window (Duple (first_sample.x, draw.height()));
237                                 context->line_to (window_space.x, window_space.y);
238                                 context->close_path();
239                                 setup_fill_context(context);
240                                 context->fill ();
241                                 break;
242                         case Outside:
243                                 context->stroke_preserve ();
244                                 window_space = item_to_window (Duple (last_sample.x, 0.0));
245                                 context->line_to (window_space.x, window_space.y);
246                                 window_space = item_to_window (Duple (first_sample.x, 0.0));
247                                 context->line_to (window_space.x, window_space.y);
248                                 context->close_path();
249                                 setup_fill_context(context);
250                                 context->fill ();
251                                 break;
252                 }
253         }
254         context->restore ();
255
256 #if 0
257         /* add points */
258         setup_outline_context (context);
259         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
260                 Duple window_space (item_to_window (*p));
261                 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
262                 context->stroke ();
263         }
264 #endif
265 }
266
267 bool
268 FramedCurve::covers (Duple const & pc) const
269 {
270         Duple point = window_to_item (pc);
271
272         /* O(N) N = number of points, and not accurate */
273
274         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
275
276                 const Coord dx = point.x - (*p).x;
277                 const Coord dy = point.y - (*p).y;
278                 const Coord dx2 = dx * dx;
279                 const Coord dy2 = dy * dy;
280
281                 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {
282                         return true;
283                 }
284         }
285
286         return false;
287 }