2 Copyright (C) 2013 Paul Davis
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.
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.
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.
24 #include "canvas/framed_curve.h"
26 using namespace ArdourCanvas;
30 FramedCurve::FramedCurve (Canvas* c)
33 , points_per_segment (16)
38 FramedCurve::FramedCurve (Item* parent)
41 , points_per_segment (16)
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
52 FramedCurve::set_points_per_segment (uint32_t n)
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
58 points_per_segment = max (n, (uint32_t) 3);
64 FramedCurve::compute_bounding_box () const
66 PolyItem::compute_bounding_box ();
68 /* possibly add extents of any point indicators here if we ever do that */
72 FramedCurve::set (Points const& p)
79 FramedCurve::interpolate ()
81 Points curve_points = _points;
83 if (curve_points.size()) {
84 curve_points.erase (curve_points.begin());
88 if (_points.size() == 3) {
89 samples.push_back (curve_points.front());
90 samples.push_back (curve_points.back());
94 InterpolatedCurve::interpolate (curve_points, points_per_segment, CatmullRomCentripetal, false, samples);
95 n_samples = samples.size();
100 FramedCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
102 if (!_outline || _points.size() < 3 || !_bounding_box) {
106 Rect self = item_to_window (_bounding_box.get());
107 boost::optional<Rect> d = self.intersection (area);
109 Rect draw = d.get ();
111 /* Our approach is to always draw n_segments across our total size.
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
118 /* x-axis limits of the curve, in window space coordinates */
120 Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
121 Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
123 /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
126 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
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.
133 draw = draw.expand (4.0);
135 /* now clip it to the actual points in the curve */
137 if (draw.x0 < w1.x) {
141 if (draw.x1 >= w2.x) {
145 setup_outline_context (context);
147 if (_points.size() == 3) {
152 Points::const_iterator it = _points.begin();
153 window_space = item_to_window (*it);
154 context->move_to (window_space.x, window_space.y);
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);
161 switch (curve_fill) {
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);
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);
188 /* curve of at least 3 points */
190 /* find left and right-most sample */
192 Points::size_type left = 0;
193 Points::size_type right = n_samples - 1;
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) {
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) {
211 const Duple first_sample = Duple (samples[left].x, samples[left].y);
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);
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);
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.
226 Duple last_sample = Duple (samples[right].x, samples[right].y);
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);
234 switch (curve_fill) {
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);
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);
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);
275 FramedCurve::covers (Duple const & pc) const
277 Duple point = window_to_item (pc);
279 /* O(N) N = number of points, and not accurate */
281 for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
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;
288 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {