fix crash when copy'ing latent plugins
[ardour.git] / libs / canvas / utils.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21 #include <cmath>
22 #include <stdint.h>
23 #include <cairomm/context.h>
24
25 #include "canvas/utils.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 void
31 ArdourCanvas::set_source_rgba (Cairo::RefPtr<Cairo::Context> context, Color color)
32 {
33         context->set_source_rgba (
34                 ((color >> 24) & 0xff) / 255.0,
35                 ((color >> 16) & 0xff) / 255.0,
36                 ((color >>  8) & 0xff) / 255.0,
37                 ((color >>  0) & 0xff) / 255.0
38                 );
39 }
40
41 void
42 ArdourCanvas::set_source_rgb_a (Cairo::RefPtr<Cairo::Context> context, Color color, float alpha)
43 {
44         context->set_source_rgba (
45                 ((color >> 24) & 0xff) / 255.0,
46                 ((color >> 16) & 0xff) / 255.0,
47                 ((color >>  8) & 0xff) / 255.0,
48                 alpha
49                 );
50 }
51
52 void
53 ArdourCanvas::set_source_rgba (cairo_t *cr, Color color)
54 {
55         cairo_set_source_rgba ( cr,
56                 ((color >> 24) & 0xff) / 255.0,
57                 ((color >> 16) & 0xff) / 255.0,
58                 ((color >>  8) & 0xff) / 255.0,
59                 ((color >>  0) & 0xff) / 255.0
60                 );
61 }
62
63 void
64 ArdourCanvas::set_source_rgb_a (cairo_t *cr, Color color, float alpha)
65 {
66         cairo_set_source_rgba ( cr,
67                 ((color >> 24) & 0xff) / 255.0,
68                 ((color >> 16) & 0xff) / 255.0,
69                 ((color >>  8) & 0xff) / 255.0,
70                 alpha
71                 );
72 }
73
74 ArdourCanvas::Distance
75 ArdourCanvas::distance_to_segment_squared (Duple const & p, Duple const & p1, Duple const & p2, double& t, Duple& at)
76 {
77         static const double kMinSegmentLenSquared = 0.00000001;  // adjust to suit.  If you use float, you'll probably want something like 0.000001f
78         static const double kEpsilon = 1.0E-14;  // adjust to suit.  If you use floats, you'll probably want something like 1E-7f
79         double dx = p2.x - p1.x;
80         double dy = p2.y - p1.y;
81         double dp1x = p.x - p1.x;
82         double dp1y = p.y - p1.y;
83         const double segLenSquared = (dx * dx) + (dy * dy);
84
85         if (segLenSquared >= -kMinSegmentLenSquared && segLenSquared <= kMinSegmentLenSquared) {
86                 // segment is a point.
87                 at = p1;
88                 t = 0.0;
89                 return ((dp1x * dp1x) + (dp1y * dp1y));
90         }
91
92
93         // Project a line from p to the segment [p1,p2].  By considering the line
94         // extending the segment, parameterized as p1 + (t * (p2 - p1)),
95         // we find projection of point p onto the line.
96         // It falls where t = [(p - p1) . (p2 - p1)] / |p2 - p1|^2
97
98         t = ((dp1x * dx) + (dp1y * dy)) / segLenSquared;
99
100         if (t < kEpsilon) {
101                 // intersects at or to the "left" of first segment vertex (p1.x, p1.y).  If t is approximately 0.0, then
102                 // intersection is at p1.  If t is less than that, then there is no intersection (i.e. p is not within
103                 // the 'bounds' of the segment)
104                 if (t > -kEpsilon) {
105                         // intersects at 1st segment vertex
106                         t = 0.0;
107                 }
108                 // set our 'intersection' point to p1.
109                 at = p1;
110                 // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
111                 // we were doing PointLineDistanceSquared, then qx would be (p1.x + (t * dx)) and qy would be (p1.y + (t * dy)).
112
113         } else if (t > (1.0 - kEpsilon)) {
114                 // intersects at or to the "right" of second segment vertex (p2.x, p2.y).  If t is approximately 1.0, then
115                 // intersection is at p2.  If t is greater than that, then there is no intersection (i.e. p is not within
116                 // the 'bounds' of the segment)
117                 if (t < (1.0 + kEpsilon)) {
118                         // intersects at 2nd segment vertex
119                         t = 1.0;
120                 }
121                 // set our 'intersection' point to p2.
122                 at = p2;
123                 // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
124                 // we were doing PointLineDistanceSquared, then qx would be (p1.x + (t * dx)) and qy would be (p1.y + (t * dy)).
125         } else {
126                 // The projection of the point to the point on the segment that is perpendicular succeeded and the point
127                 // is 'within' the bounds of the segment.  Set the intersection point as that projected point.
128                 at = Duple (p1.x + (t * dx), p1.y + (t * dy));
129         }
130
131         // return the squared distance from p to the intersection point.  Note that we return the squared distance
132         // as an optimization because many times you just need to compare relative distances and the squared values
133         // works fine for that.  If you want the ACTUAL distance, just take the square root of this value.
134         double dpqx = p.x - at.x;
135         double dpqy = p.y - at.y;
136
137         return ((dpqx * dpqx) + (dpqy * dpqy));
138 }
139