1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
30 #include "vogl_core.h"
32 #include "vogl_hash.h"
48 // up to, but not including right/bottom
49 inline rect(int left, int top, int right, int bottom)
51 set(left, top, right, bottom);
54 inline rect(const vec2I &lo, const vec2I &hi)
60 inline rect(const vec2I &point)
63 m_corner[1].set(point[0] + 1, point[1] + 1);
66 inline bool operator==(const rect &r) const
68 return (m_corner[0] == r.m_corner[0]) && (m_corner[1] == r.m_corner[1]);
71 inline bool operator<(const rect &r) const
73 for (uint i = 0; i < 2; i++)
75 if (m_corner[i] < r.m_corner[i])
77 else if (!(m_corner[i] == r.m_corner[i]))
90 inline void set(int left, int top, int right, int bottom)
92 m_corner[0].set(left, top);
93 m_corner[1].set(right, bottom);
96 inline void set(const vec2I &lo, const vec2I &hi)
102 inline void set(const vec2I &point)
105 m_corner[1].set(point[0] + 1, point[1] + 1);
108 inline uint get_width() const
110 return m_corner[1][0] - m_corner[0][0];
112 inline uint get_height() const
114 return m_corner[1][1] - m_corner[0][1];
117 inline int get_left() const
119 return m_corner[0][0];
121 inline int get_top() const
123 return m_corner[0][1];
125 inline int get_right() const
127 return m_corner[1][0];
129 inline int get_bottom() const
131 return m_corner[1][1];
134 inline bool is_empty() const
136 return (m_corner[1][0] <= m_corner[0][0]) || (m_corner[1][1] <= m_corner[0][1]);
139 inline uint get_dimension(uint axis) const
141 return m_corner[1][axis] - m_corner[0][axis];
143 inline uint get_area() const
145 return get_dimension(0) * get_dimension(1);
148 inline const vec2I &operator[](uint i) const
153 inline vec2I &operator[](uint i)
159 inline rect &translate(int x_ofs, int y_ofs)
161 m_corner[0][0] += x_ofs;
162 m_corner[0][1] += y_ofs;
163 m_corner[1][0] += x_ofs;
164 m_corner[1][1] += y_ofs;
168 inline rect &init_expand()
170 m_corner[0].set(INT_MAX);
171 m_corner[1].set(INT_MIN);
175 inline rect &expand(int x, int y)
177 m_corner[0][0] = math::minimum(m_corner[0][0], x);
178 m_corner[0][1] = math::minimum(m_corner[0][1], y);
179 m_corner[1][0] = math::maximum(m_corner[1][0], x + 1);
180 m_corner[1][1] = math::maximum(m_corner[1][1], y + 1);
184 inline rect &expand(const rect &r)
186 m_corner[0][0] = math::minimum(m_corner[0][0], r[0][0]);
187 m_corner[0][1] = math::minimum(m_corner[0][1], r[0][1]);
188 m_corner[1][0] = math::maximum(m_corner[1][0], r[1][0]);
189 m_corner[1][1] = math::maximum(m_corner[1][1], r[1][1]);
193 inline bool touches(const rect &r) const
195 for (uint i = 0; i < 2; i++)
197 if (r[1][i] <= m_corner[0][i])
199 else if (r[0][i] >= m_corner[1][i])
206 inline bool fully_within(const rect &r) const
208 for (uint i = 0; i < 2; i++)
210 if (m_corner[0][i] < r[0][i])
212 else if (m_corner[1][i] > r[1][i])
219 inline bool intersect(const rect &r)
227 for (uint i = 0; i < 2; i++)
229 m_corner[0][i] = math::maximum<int>(m_corner[0][i], r[0][i]);
230 m_corner[1][i] = math::minimum<int>(m_corner[1][i], r[1][i]);
236 inline bool contains(int x, int y) const
238 return (x >= m_corner[0][0]) && (x < m_corner[1][0]) &&
239 (y >= m_corner[0][1]) && (y < m_corner[1][1]);
242 inline bool contains(const vec2I &p) const
244 return contains(p[0], p[1]);
251 inline rect make_rect(uint width, uint height)
253 return rect(0, 0, width, height);