Plot video and subtitle on one track and audio on the rest in the timeline.
[dcpomatic.git] / src / lib / rect.h
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef DCPOMATIC_RECT_H
21 #define DCPOMATIC_RECT_H
22
23 #include "position.h"
24 #include <boost/optional.hpp>
25 #include <algorithm>
26
27 /* Put this inside a namespace as Apple put a Rect in the global namespace */
28
29 namespace dcpomatic
30 {
31
32 /** @struct Rect
33  *  @brief A rectangle.
34  */
35 template <class T>
36 class Rect
37 {
38 public:
39
40         Rect ()
41                 : x (0)
42                 , y (0)
43                 , width (0)
44                 , height (0)
45         {}
46
47         Rect (Position<T> p, T w_, T h_)
48                 : x (p.x)
49                 , y (p.y)
50                 , width (w_)
51                 , height (h_)
52         {}
53
54         Rect (T x_, T y_, T w_, T h_)
55                 : x (x_)
56                 , y (y_)
57                 , width (w_)
58                 , height (h_)
59         {}
60
61         T x;
62         T y;
63         T width;
64         T height;
65
66         Position<T> position () const
67         {
68                 return Position<T> (x, y);
69         }
70
71         boost::optional<Rect<T> > intersection (Rect<T> const & other) const
72         {
73                 /* This isn't exactly the paragon of mathematical precision */
74
75                 T const tx = std::max (x, other.x);
76                 T const ty = std::max (y, other.y);
77
78                 Rect r (
79                         tx, ty,
80                         std::min (x + width, other.x + other.width) - tx,
81                         std::min (y + height, other.y + other.height) - ty
82                         );
83
84                 if (r.width < 0 || r.height < 0) {
85                         return boost::optional<Rect<T> > ();
86                 }
87
88                 return r;
89         }
90
91         void extend (Rect<T> const & other)
92         {
93                 x = std::min (x, other.x);
94                 y = std::min (y, other.y);
95                 width = std::max (x + width, other.x + other.width) - x;
96                 height = std::max (y + height, other.y + other.height) - y;
97         }
98
99         Rect<T> extended (T amount) const {
100                 Rect<T> c = *this;
101                 c.x -= amount;
102                 c.y -= amount;
103                 c.width += amount * 2;
104                 c.height += amount * 2;
105                 return c;
106         }
107
108         bool contains (Position<T> p) const
109         {
110                 return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));
111         }
112 };
113
114 }
115
116 #endif