/usr/include/ETL/_rect.h is in etl-dev 1.2.1-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | /*! ========================================================================
** Extended Template Library
** Rectangle Basic Class Implementation
** $Id$
**
** Copyright (c) 2002 Adrian Bentley
**
** This package is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** This package 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
** General Public License for more details.
**
** === N O T E S ===========================================================
**
** This is an internal header file, included by other ETL headers.
** You should not attempt to use it directly.
**
** ========================================================================= */
/* === S T A R T =========================================================== */
#ifndef __ETL__RECT_H
#define __ETL__RECT_H
/* === H E A D E R S ======================================================= */
#include <functional>
#include <algorithm>
/* === M A C R O S ========================================================= */
/* === T Y P E D E F S ===================================================== */
/* === C L A S S E S & S T R U C T S ======================================= */
_ETL_BEGIN_NAMESPACE
template < typename T >
class rect
{
public: //type niceties
typedef T value_type;
public: //representation
value_type minx,maxx,miny,maxy;
public: //interface
rect() {}
rect(const value_type &x1,const value_type &y1)
{
set_point(x1,y1);
}
rect(const value_type &x1,const value_type &y1,
const value_type &x2,const value_type &y2)
{
set_point(x1,y1);
expand(x2,y2);
}
rect(const rect<T> &o)
:minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
{}
template < typename U >
rect(const rect<U> &o)
:minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
{}
void set_point(const value_type &x1,const value_type &y1)
{
minx = maxx = x1;
miny = maxy = y1;
}
void expand(const value_type &x1,const value_type &y1)
{
minx = std::min(minx,x1);
maxx = std::max(maxx,x1);
miny = std::min(miny,y1);
maxy = std::max(maxy,y1);
}
void set(const value_type &x1,const value_type &y1,
const value_type &x2,const value_type &y2)
{
minx = x1; maxx = x2;
miny = y1; maxy = y2;
}
//HACK HACK HACK (stupid compiler doesn't like default arguments of any type)
bool valid() const
{
return valid(std::less<T>());
}
template < typename F >
bool valid(const F & func) const
{
return func(minx,maxx) && func(miny,maxy);
}
};
template < typename T, typename F >
inline bool intersect(const rect<T> &r1, const rect<T> &r2, const F & func)
{
/* We wan to do the edge compare test
|-----|
|------| intersecting
|-----|
|-----| not intersecting
So we want to compare the mins of the one against the maxs of the other, and
visa versa
by default (exclude edge sharing) less will not be true if they are equal...
*/
return func(r1.minx,r2.maxx) &&
func(r2.minx,r1.maxx) &&
func(r1.miny,r2.maxy) &&
func(r2.miny,r1.maxy);
}
template < typename T >
inline bool intersect(const rect<T> &r1, const rect<T> &r2)
{
return intersect(r1,r2,std::less<T>());
}
template < typename T, typename F >
inline bool contains(const rect<T> &big, const rect<T> &small, const F & func)
{
return !func(small.minx, big.minx) &&
!func(big.maxx, small.maxx) &&
!func(small.miny, big.miny) &&
!func(big.maxy, small.maxy);
}
template < typename T >
inline bool contains(const rect<T> &big, const rect<T> &small)
{
return contains(big,small,std::less<T>());
}
template < typename T >
void set_intersect(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
{
//takes the intersection of the two rectangles
rout.minx = std::max(r1.minx,r2.minx);
rout.miny = std::max(r1.miny,r2.miny);
rout.maxx = std::min(r1.maxx,r2.maxx);
rout.maxy = std::min(r1.maxy,r2.maxy);
}
template < typename T >
void set_union(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
{
//takes the union of the two rectangles (bounds both... will contain extra info, but that's ok)
rout.set(
std::min(r1.minx,r2.minx),
std::min(r1.miny,r2.miny),
std::max(r1.maxx,r2.maxx),
std::max(r1.maxy,r2.maxy));
/*rect<T> local = r1;
rout.expand(r2.minx,r2.miny);
rout.expand(r2.maxx,r2.maxy);
rout = local;*/
}
template<typename List, typename T, typename F>
void rects_subtract(List &list, const rect<T> &r, const F &less)
{
typedef typename List::value_type Rect;
if (!r.valid(less)) return;
for(typename List::iterator i = list.begin(); i != list.end();)
{
if (intersect(*i, r))
{
Rect &x = *i;
Rect y;
y.minx = std::max(x.minx, r.maxx);
y.maxx = std::min(x.maxx, r.minx);
y.miny = std::max(x.miny, r.maxy);
y.maxy = std::min(x.maxy, r.miny);
T rects[][4] = {
{ x.minx, y.maxx, x.miny, x.maxy },
{ y.minx, x.maxx, x.miny, x.maxy },
{ y.minx, y.maxx, x.miny, y.maxy },
{ y.minx, y.maxx, y.miny, x.maxy }
};
const int count = sizeof(rects)/sizeof(rects[0]);
bool inserted = false;
for(int j = 0; j < count; ++j)
{
if ( less(rects[j][0], rects[j][1])
&& less(rects[j][2], rects[j][3]) )
{
Rect rr;
rr.minx = rects[j][0];
rr.maxx = rects[j][1];
rr.miny = rects[j][2];
rr.maxy = rects[j][3];
if (inserted)
i = list.insert(++i, rr);
else
*i = rr, inserted = true;
}
}
if (!inserted) { i = list.erase(i); continue; }
}
++i;
}
}
template<typename List, typename T>
void rects_subtract(List &list, const rect<T> &r)
{ rects_subtract(list, r, std::less<T>()); }
template<typename List, typename T, typename F>
void rects_add(List &list, const rect<T> &r, const F &less)
{
if (!r.valid(less)) return;
rects_subtract(list, r, less);
list.insert(list.end(), r);
}
template<typename List, typename T>
void rects_add(List &list, const rect<T> &r)
{ rects_add(list, r, std::less<T>()); }
template<typename List, typename F>
void rects_merge(List &list, const F &less)
{
for(typename List::iterator i = list.begin(); i != list.end();)
if (!i->valid(less)) i = list.erase(i); else ++i;
bool merged_any = true;
while(merged_any)
{
merged_any = false;
for(typename List::iterator i = list.begin(); i != list.end();)
{
bool merged_current = false;
for(typename List::iterator j = list.begin(); j != list.end(); ++j)
if ( !less(i->minx, j->minx) && !less(j->minx, i->minx)
&& !less(i->maxy, j->miny) && !less(j->miny, i->maxy) )
{
j->miny = i->miny;
i = list.erase(i);
merged_current = true;
break;
}
if (merged_current) merged_any = true; else ++i;
}
}
}
template<typename List>
void rects_merge(List &list)
{
typedef typename List::value_type R;
typedef typename R::value_type T;
rects_merge(list, std::less<T>());
}
_ETL_END_NAMESPACE
/* === E X T E R N S ======================================================= */
/* === E N D =============================================================== */
#endif
|