summaryrefslogtreecommitdiff
path: root/src/base/Rect.h
blob: d6683384c03f241537f1655004b903748d8c8e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _Rect_H_
#define _Rect_H_

#include "../api.h"

#include "../base/GLMHelper.h"
#include "../glm/glm.hpp"

#include <algorithm>

namespace avg {

// Simple rectangle class.
// If NUM is an integer, contains all points from tl up to but not including 
// br.
template<class NUM>
class AVG_TEMPLATE_API Rect
{
public:
    typedef glm::detail::tvec2<NUM> Vec2;
    Vec2 tl;
    Vec2 br;

    Rect();
    Rect(NUM left, NUM top, NUM right, NUM bottom);
    Rect(const Vec2& TL, const Vec2& BR);
    template<class ORIGNUM> Rect(const Rect<ORIGNUM>& rc);

    bool operator ==(const Rect<NUM>& rect) const;
    bool operator !=(const Rect<NUM> & rect) const;
    NUM width() const;
    NUM height() const;
    Vec2 center() const;
    void setWidth(NUM width);
    void setHeight(NUM height);
    void setSize(const Vec2& size);
    bool contains(const Vec2& pt) const;
    bool contains(const Rect<NUM>& rect) const;
    bool intersects(const Rect<NUM>& rect) const;
    void expand(const Rect<NUM>& rect);
    void intersect(const Rect<NUM>& rect);
    Vec2 size() const;
    Vec2 cropPoint(const Vec2& pt) const;
};

template<class NUM>
std::ostream& operator<<( std::ostream& os, const Rect<NUM> &r)
{
    os << "(" << r.tl << "-" << r.br << ")";
    return os;
}


typedef Rect<float> FRect;
typedef Rect<int> IntRect;

template<class NUM>
Rect<NUM>::Rect()
{}

template<class NUM>
Rect<NUM>::Rect(const Vec2& TL, const Vec2& BR)
    : tl(TL), br(BR)
{}

template<class NUM>
Rect<NUM>::Rect(NUM left, NUM top, NUM right, NUM bottom) 
    : tl(left, top), 
      br(right, bottom)
{}

template<class NUM>
template<class ORIGNUM>
Rect<NUM>::Rect(const Rect<ORIGNUM>& rc)
    : tl (NUM(rc.tl.x), NUM(rc.tl.y)),
      br (NUM(rc.br.x), NUM(rc.br.y))
{
}

template<class NUM>
bool Rect<NUM>::operator ==(const Rect<NUM> & rect) const
{
  return (tl == rect.tl && br == rect.br);
}

template<class NUM>
bool Rect<NUM>::operator !=(const Rect<NUM> & rect) const
{
  return !(rect==*this);
}

template<class NUM>
NUM Rect<NUM>::width() const
{
  return br.x-tl.x;
}

template<class NUM>
NUM Rect<NUM>::height() const
{
  return br.y-tl.y;
}

template<class NUM>
glm::detail::tvec2<NUM> Rect<NUM>::center() const
{
    return Vec2(tl+br)/2;
}

template<class NUM>
void Rect<NUM>::setWidth(NUM width)
{
    br.x = tl.x+width;
}
 
template<class NUM>
void Rect<NUM>::setHeight(NUM height)
{
    br.y = tl.y+height;
}
    
template<class NUM>
void Rect<NUM>::setSize(const Vec2& size)
{
    setWidth(size.x);
    setHeight(size.y);
}

template<class NUM>
bool Rect<NUM>::contains(const Vec2& pt) const
{
    return (pt.x >= tl.x && pt.x < br.x &&
        pt.y >= tl.y && pt.y < br.y);
}

template<class NUM>
bool Rect<NUM>::contains(const Rect<NUM>& rect) const
{
    Vec2 brpt (rect.br.x-1, rect.br.y-1);
    return Contains(rect.tl) && Contains(brpt);
}

template<class NUM>
bool Rect<NUM>::intersects(const Rect<NUM>& rect) const
{   
    if (rect.br.x <= tl.x || rect.tl.x >= br.x ||
        rect.br.y <= tl.y || rect.tl.y >= br.y)
      return false;
    else
      return true;
}

template<class NUM>
void Rect<NUM>::expand(const Rect<NUM>& rect)
{
    tl.x = glm::min(tl.x, rect.tl.x);
    tl.y = glm::min(tl.y, rect.tl.y);
    br.x = glm::max(br.x, rect.br.x);
    br.y = glm::max(br.y, rect.br.y);
}

template<class NUM>
void Rect<NUM>::intersect(const Rect<NUM>& rect)
{
    tl.x = glm::max(tl.x, rect.tl.x);
    tl.y = glm::max(tl.y, rect.tl.y);
    br.x = glm::min(br.x, rect.br.x);
    br.y = glm::min(br.y, rect.br.y);
}

template<class NUM>
glm::detail::tvec2<NUM> Rect<NUM>::size() const
{
    return Vec2(width(), height());
}

template<class NUM>
glm::detail::tvec2<NUM> Rect<NUM>::cropPoint(const Vec2& pt) const
{
    Vec2 Result;
    Result.x = std::min(std::max(pt.x, tl.x), br.x-1);
    Result.y = std::min(std::max(pt.y, tl.y), br.y-1);
    return Result;
}

#undef min
#undef max

}

#endif