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/curve.h"
26 using namespace ArdourCanvas;
30 Curve::Curve (Canvas* c)
33 , points_per_segment (16)
34 , curve_type (CatmullRomCentripetal)
39 Curve::Curve (Item* parent)
42 , points_per_segment (16)
43 , curve_type (CatmullRomCentripetal)
48 /** When rendering the curve, we will always draw a fixed number of straight
49 * line segments to span the x-axis extent of the curve. More segments:
50 * smoother visual rendering. Less rendering: closer to a visibily poly-line
54 Curve::set_points_per_segment (uint32_t n)
56 /* this only changes our appearance rather than the bounding box, so we
57 just need to schedule a redraw rather than notify the parent of any
60 points_per_segment = n;
66 Curve::compute_bounding_box () const
68 PolyItem::compute_bounding_box ();
70 /* possibly add extents of any point indicators here if we ever do that */
74 Curve::set (Points const& p)
84 InterpolatedCurve::interpolate (_points, points_per_segment, CatmullRomCentripetal, false, samples);
85 n_samples = samples.size();
89 Curve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
91 if (!_outline || _points.size() < 2 || !_bounding_box) {
95 Rect self = item_to_window (_bounding_box.get());
96 boost::optional<Rect> d = self.intersection (area);
100 /* Our approach is to always draw n_segments across our total size.
102 * This is very inefficient if we are asked to only draw a small
103 * section of the curve. For now we rely on cairo clipping to help
108 setup_outline_context (context);
110 if (_points.size() == 2) {
116 window_space = item_to_window (_points.front());
117 context->move_to (window_space.x, window_space.y);
118 window_space = item_to_window (_points.back());
119 context->line_to (window_space.x, window_space.y);
122 switch (curve_fill) {
127 context->stroke_preserve ();
128 window_space = item_to_window (Duple(_points.back().x, draw.height()));
129 context->line_to (window_space.x, window_space.y);
130 window_space = item_to_window (Duple(_points.front().x, draw.height()));
131 context->line_to (window_space.x, window_space.y);
132 context->close_path();
133 setup_fill_context(context);
137 context->stroke_preserve ();
138 window_space = item_to_window (Duple(_points.back().x, 0.0));
139 context->line_to (window_space.x, window_space.y);
140 window_space = item_to_window (Duple(_points.front().x, 0.0));
141 context->line_to (window_space.x, window_space.y);
142 context->close_path();
143 setup_fill_context(context);
150 /* curve of at least 3 points */
152 /* x-axis limits of the curve, in window space coordinates */
154 Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
155 Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
157 /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
160 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
163 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
164 they will still occur, but cairo's clipping will hide them.
167 draw = draw.expand (4.0);
169 /* now clip it to the actual points in the curve */
171 if (draw.x0 < w1.x) {
175 if (draw.x1 >= w2.x) {
179 /* find left and right-most sample */
181 Points::size_type left = 0;
182 Points::size_type right = n_samples;
184 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
186 window_space = item_to_window (Duple (samples[idx].x, 0.0));
187 if (window_space.x >= draw.x0) break;
189 for (Points::size_type idx = n_samples; idx > left + 1; --idx) {
190 window_space = item_to_window (Duple (samples[idx].x, 0.0));
191 if (window_space.x <= draw.x1) break;
195 /* draw line between samples */
196 window_space = item_to_window (Duple (samples[left].x, samples[left].y));
197 context->move_to (window_space.x, window_space.y);
198 for (uint32_t idx = left + 1; idx < right; ++idx) {
199 window_space = item_to_window (Duple (samples[idx].x, samples[idx].y));
200 context->line_to (window_space.x, window_space.y);
203 switch (curve_fill) {
208 context->stroke_preserve ();
209 window_space = item_to_window (Duple (samples[right-1].x, draw.height()));
210 context->line_to (window_space.x, window_space.y);
211 window_space = item_to_window (Duple (samples[left].x, draw.height()));
212 context->line_to (window_space.x, window_space.y);
213 context->close_path();
214 setup_fill_context(context);
218 context->stroke_preserve ();
219 window_space = item_to_window (Duple (samples[right-1].x, 0.0));
220 context->line_to (window_space.x, window_space.y);
221 window_space = item_to_window (Duple (samples[left].x, 0.0));
222 context->line_to (window_space.x, window_space.y);
223 context->close_path();
224 setup_fill_context(context);
233 setup_outline_context (context);
234 for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
235 Duple window_space (item_to_window (*p));
236 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
243 Curve::covers (Duple const & pc) const
245 Duple point = window_to_item (pc);
247 /* O(N) N = number of points, and not accurate */
249 for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
251 const Coord dx = point.x - (*p).x;
252 const Coord dy = point.y - (*p).y;
253 const Coord dx2 = dx * dx;
254 const Coord dy2 = dy * dy;
256 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {