Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / canvas / types.h
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 #ifndef __CANVAS_TYPES_H__
21 #define __CANVAS_TYPES_H__
22
23 #include <iostream>
24 #include <vector>
25 #include <stdint.h>
26 #include <boost/optional.hpp>
27
28 namespace ArdourCanvas
29 {
30
31 typedef double Coord;
32 typedef double Distance;
33 typedef uint32_t Color;
34
35 extern Coord const COORD_MAX;
36 extern Coord const CAIRO_MAX;
37
38 struct Duple
39 {
40         Duple ()
41                 : x (0)
42                 , y (0)
43         {}
44         
45         Duple (Coord x_, Coord y_)
46                 : x (x_)
47                 , y (y_)
48         {}
49                      
50         Coord x;
51         Coord y;
52
53         Duple translate (Duple) const;
54 };
55
56
57 extern Duple operator- (Duple const &);
58 extern Duple operator+ (Duple const &, Duple const &);
59 extern bool operator== (Duple const &, Duple const &);
60 extern Duple operator- (Duple const &, Duple const &);
61 extern Duple operator/ (Duple const &, double);
62 extern std::ostream & operator<< (std::ostream &, Duple const &);
63
64 struct Rect
65 {
66         Rect ()
67                 : x0 (0)
68                 , y0 (0)
69                 , x1 (0)
70                 , y1 (0)
71         {}
72         
73         Rect (Coord x0_, Coord y0_, Coord x1_, Coord y1_)
74                 : x0 (x0_)
75                 , y0 (y0_)
76                 , x1 (x1_)
77                 , y1 (y1_)
78         {}
79                 
80         Coord x0;
81         Coord y0;
82         Coord x1;
83         Coord y1;
84
85         boost::optional<Rect> intersection (Rect const &) const;
86         Rect extend (Rect const &) const;
87         Rect translate (Duple) const;
88         Rect expand (Distance) const;
89         bool contains (Duple) const;
90         Rect fix () const;
91
92         Distance width () const {
93                 return x1 - x0;
94         }
95
96         Distance height () const {
97                 return y1 - y0;
98         }
99 };
100
101 extern std::ostream & operator<< (std::ostream &, Rect const &);
102
103 typedef std::vector<Duple> Points;
104
105 }
106         
107 #endif