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