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
|
/*
* QuadTree.h - QuadTree, a data structure for fast fuzzy rectangle merging
*
* Copyright (c) 2010-2013 Tobias Doerffel <tobydox/at/users/dot/sf/dot/net>
*
* This file is part of iTALC - http://italc.sourceforge.net
*
* This program 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 program 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.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#ifndef QUAD_TREE_H
#define QUAD_TREE_H
#include <stdint.h>
#include <QtCore/QList>
#include <QtCore/QRect>
#include <QtCore/QVector>
typedef QList<QRect> RectList;
struct QuadTreeRect
{
QuadTreeRect( uint16_t x1 = 0, uint16_t y1 = 0, uint16_t x2 = 0, uint16_t y2 = 0 ) :
m_x1( x1 ),
m_x2( x2 ),
m_y1( y1 ),
m_y2( y2 )
{
}
uint16_t x1() const
{
return m_x1;
}
uint16_t y1() const
{
return m_y1;
}
uint16_t x2() const
{
return m_x2;
}
uint16_t y2() const
{
return m_y2;
}
uint16_t m_x1, m_x2, m_y1, m_y2;
} ;
class QuadTree
{
public:
QuadTree( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t level, bool topLevel = true ) :
m_x1( x1 ),
m_y1( y1 ),
m_x2( x2 ),
m_y2( y2 ),
m_level( level ),
m_topLevel( topLevel ),
m_marked( false )
{
if( m_level > 0 )
{
uint16_t wh = (m_x2-m_x1+1)/2;
uint16_t hh = (m_y2-m_y1+1)/2;
m_subRects[0][0] = new QuadTree( x1, y1, x1+wh-1, y1+hh-1, level-1, false );
m_subRects[1][0] = new QuadTree( x1+wh, y1, x2, y1+hh-1, level-1, false );
m_subRects[0][1] = new QuadTree( x1, y1+hh, x1+wh-1, y2, level-1, false );
m_subRects[1][1] = new QuadTree( x1+wh, y1+hh, x2, y2, level-1, false );
}
}
~QuadTree()
{
if( m_level > 0 )
{
delete m_subRects[0][0];
delete m_subRects[0][1];
delete m_subRects[1][0];
delete m_subRects[1][1];
}
}
void reset()
{
m_marked = false;
if( m_level > 0 )
{
m_subRects[0][0]->reset();
m_subRects[0][1]->reset();
m_subRects[1][0]->reset();
m_subRects[1][1]->reset();
}
}
bool isMarked() const
{
return m_marked;
}
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
bool intersects( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 ) const
{
if ( m_x1 > x2 || x1 > m_x2 ) return false;
if ( m_y1 > y2 || y1 > m_y2 ) return false;
return true;
}
void addRects( const QList<QRect> &qrects )
{
foreach( const QRect &r, qrects )
{
addRect( r.x(), r.y(), r.x()+r.width()-1, r.y()+r.height()-1 );
}
}
bool addRect( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
{
if( isMarked() )
{
return true;
}
else if( intersects( x1, y1, x2, y2 ) )
{
if( likely( m_level > 0 ) )
{
bool x = true;
x &= m_subRects[0][0]->addRect( x1, y1, x2, y2 );
x &= m_subRects[0][1]->addRect( x1, y1, x2, y2 );
x &= m_subRects[1][0]->addRect( x1, y1, x2, y2 );
x &= m_subRects[1][1]->addRect( x1, y1, x2, y2 );
m_marked = x;
}
else
{
//printf("mark %d:%d - %d:%d\n", m_x1, m_y1, m_x2, m_y2 );
m_marked = true;
}
}
return isMarked();
}
int numMarkedAllSubRects() const
{
if( unlikely( m_level == 0 ) )
{
return isMarked() ? 1 : 0;
}
return m_subRects[0][0]->numMarkedAllSubRects() +
m_subRects[0][1]->numMarkedAllSubRects() +
m_subRects[1][0]->numMarkedAllSubRects() +
m_subRects[1][1]->numMarkedAllSubRects();
}
int numMarkedSubRects() const
{
if( unlikely( m_level == 0 ) )
{
return 0;
}
return ( m_subRects[0][0]->isMarked() ? 1 : 0 ) +
( m_subRects[0][1]->isMarked() ? 1 : 0 ) +
( m_subRects[1][0]->isMarked() ? 1 : 0 ) +
( m_subRects[1][1]->isMarked() ? 1 : 0 );
}
inline int totalSubRects() const
{
int n = 1 << m_level;
return n*n;
}
operator QuadTreeRect() const
{
return QuadTreeRect( m_x1, m_y1, m_x2, m_y2 );
}
QVector<QuadTreeRect> rects() const
{
if( isMarked() )
{
return QVector<QuadTreeRect>( 1 , (QuadTreeRect)*this);
}
else if( m_level == 0 )
{
return QVector<QuadTreeRect>();
}
const int x = 1<<(m_level+1);
if( numMarkedAllSubRects() >= totalSubRects()*(x-1)/x )
{
return QVector<QuadTreeRect>( 1, (QuadTreeRect)*this );
}
if( numMarkedSubRects() == 2 )
{
for( int i = 0; i < 2; ++i )
{
// subrects at same row?
if( m_subRects[0][i]->isMarked() && m_subRects[1][i]->isMarked() )
{
QVector<QuadTreeRect> l( m_subRects[0][i ? 0 : 1]->rects() +
m_subRects[1][i ? 0 : 1]->rects() );
l += QuadTreeRect( m_x1, m_subRects[0][i]->m_y1,
m_x2, m_subRects[0][i]->m_y2 );
return l;
}
// subrects at same col?
if( m_subRects[i][0]->isMarked() && m_subRects[i][1]->isMarked() )
{
QVector<QuadTreeRect> l( m_subRects[i ? 0 : 1][0]->rects() +
m_subRects[i ? 0 : 1][1]->rects() );
l += QuadTreeRect( m_subRects[i][0]->m_x1, m_y1,
m_subRects[i][0]->m_x2, m_y2 );
return l;
}
}
}
QVector<QuadTreeRect> l( m_subRects[0][0]->rects() +
m_subRects[0][1]->rects() +
m_subRects[1][0]->rects() +
m_subRects[1][1]->rects() );
// merge all adjacent rectangles
if( m_topLevel )
{
bool found = true;
while( found )
{
found = false;
for( QVector<QuadTreeRect>::Iterator it = l.begin(); it != l.end(); ++it )
{
for( QVector<QuadTreeRect>::Iterator jt = it+1; jt != l.end(); )
{
if( ( it->y1() == jt->y1() && it->y2() == jt->y2() &&
( it->x2()+1 == jt->x1() || it->x1() == jt->x2()+1 ) ) ||
( it->x1() == jt->x1() && it->x2() == jt->x2() &&
( it->y2()+1 == jt->y1() || it->y1() == jt->y2()+1 ) ) )
{
*it = QuadTreeRect( qMin( it->x1(), jt->x1() ), qMin( it->y1(), jt->y1() ),
qMax( it->x2(), jt->x2() ), qMax( it->y2(), jt->y2() ) );
jt = l.erase( jt );
found = true;
}
else
{
++jt;
}
}
}
}
}
return l;
}
private:
const uint16_t m_x1, m_y1, m_x2, m_y2;
const uint8_t m_level;
const bool m_topLevel;
bool m_marked;
QuadTree *m_subRects[2][2];
} ;
#endif
|