add headers to all canvas .cc and .h files
[ardour.git] / libs / canvas / types.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 <cfloat>
22 #include <cassert>
23 #include "canvas/types.h"
24
25 using namespace std;
26 using namespace ArdourCanvas;
27
28 Coord const ArdourCanvas::COORD_MAX = DBL_MAX;
29 /* XXX: empirically arrived at */
30 Coord const ArdourCanvas::CAIRO_MAX = 65536;
31
32 static inline Coord
33 safe_add (Coord a, Coord b)
34 {
35         if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) {
36                 return COORD_MAX;
37         }
38
39         return a + b;
40 }
41
42 Duple
43 Duple::translate (Duple t) const
44 {
45         Duple d;
46
47         d.x = safe_add (x, t.x);
48         d.y = safe_add (y, t.y);
49         
50         return d;
51 }
52
53 boost::optional<Rect>
54 Rect::intersection (Rect const & o) const
55 {
56         Rect i;
57         
58         i.x0 = max (x0, o.x0);
59         i.y0 = max (y0, o.y0);
60         i.x1 = min (x1, o.x1);
61         i.y1 = min (y1, o.y1);
62
63         if (i.x0 > i.x1 || i.y0 > i.y1) {
64                 return boost::optional<Rect> ();
65         }
66         
67         return boost::optional<Rect> (i);
68 }
69
70 Rect
71 Rect::translate (Duple t) const
72 {
73         Rect r;
74
75         r.x0 = safe_add (x0, t.x);
76         r.y0 = safe_add (y0, t.y);
77         r.x1 = safe_add (x1, t.x);
78         r.y1 = safe_add (y1, t.y);
79         return r;
80 }
81
82 Rect
83 Rect::extend (Rect const & o) const
84 {
85         Rect r;
86         r.x0 = min (x0, o.x0);
87         r.y0 = min (y0, o.y0);
88         r.x1 = max (x1, o.x1);
89         r.y1 = max (y1, o.y1);
90         return r;
91 }
92
93 Rect
94 Rect::expand (Distance amount) const
95 {
96         Rect r;
97         r.x0 = x0 - amount;
98         r.y0 = y0 - amount;
99         r.x1 = safe_add (x1, amount);
100         r.y1 = safe_add (y1, amount);
101         return r;
102 }
103
104 bool
105 Rect::contains (Duple point) const
106 {
107         return point.x >= x0 && point.x <= x1 && point.y >= y0 && point.y <= y1;
108 }
109
110 Rect
111 Rect::fix () const
112 {
113         Rect r;
114         
115         r.x0 = min (x0, x1);
116         r.y0 = min (y0, y1);
117         r.x1 = max (x0, x1);
118         r.y1 = max (y0, y1);
119
120         return r;
121 }
122
123 Duple
124 ArdourCanvas::operator- (Duple const & o)
125 {
126         return Duple (-o.x, -o.y);
127 }
128
129 Duple
130 ArdourCanvas::operator+ (Duple const & a, Duple const & b)
131 {
132         return Duple (safe_add (a.x, b.x), safe_add (a.y, b.y));
133 }
134
135 Duple
136 ArdourCanvas::operator- (Duple const & a, Duple const & b)
137 {
138         return Duple (a.x - b.x, a.y - b.y);
139 }
140
141 Duple
142 ArdourCanvas::operator/ (Duple const & a, double b)
143 {
144         return Duple (a.x / b, a.y / b);
145 }
146
147 ostream &
148 ArdourCanvas::operator<< (ostream & s, Duple const & r)
149 {
150         s << "(" << r.x << ", " << r.y << ")";
151         return s;
152 }
153
154 ostream &
155 ArdourCanvas::operator<< (ostream & s, Rect const & r)
156 {
157         s << "[(" << r.x0 << ", " << r.y0 << "), (" << r.x1 << ", " << r.y1 << ") " << r.width() << " x " << r.height() << "]";
158         return s;
159 }
160