File: ecl_geom.cc

package info (click to toggle)
enigma 1.20-dfsg.1-2.2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 64,696 kB
  • sloc: xml: 153,614; cpp: 63,581; ansic: 31,088; sh: 4,825; makefile: 1,858; yacc: 288; perl: 84; sed: 16
file content (101 lines) | stat: -rw-r--r-- 3,085 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2002,2005 Daniel Heck
 *
 * 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; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "ecl_geom.hh"
#include <iostream>
#include <list>

using namespace std;
using namespace ecl;

/* -------------------- Rect -------------------- */

std::ostream& ecl::operator<<(std::ostream& os, const Rect& r) {
    return os << "Rect(" << r.x << " " << r.y << " " << r.w << " " << r.h << ")";
}

/* -------------------- RectList -------------------- */

RectList::RectList(const RectList& rl)
: m_rectangles(rl.m_rectangles)
{}

void RectList::swap(RectList &rl) {
    std::swap(m_rectangles, rl.m_rectangles);
}

RectList& RectList::operator=(const RectList& rl) {
    m_rectangles = rl.m_rectangles;
    return *this;
}

void RectList::push_back(const Rect& r) {
    if (r.w > 0 && r.h > 0)
        m_rectangles.push_back(r);
}

void RectList::pop_back() {
    return m_rectangles.pop_back();
}

void RectList::merge(const RectList& rl) {
    for (const_iterator i = rl.begin(); i != rl.end(); ++i)
        add(*i);
}

void RectList::intersect(const Rect& rect) {
    RectList rl;
    for (iterator i=begin(); i != end(); ++i) {
        int a = max(i->x, rect.x);
        int b = min(i->x + i->w, rect.x + rect.w);
        int c = max(i->y, rect.y);
        int d = min(i->y + i->h, rect.y + rect.h);
        rl.push_back(Rect(a, c, b-a, d-c));
    }
    swap(rl);
}

void RectList::add(const Rect& r) {
    RectList rl;
    rl.push_back(r);
    for (iterator i=begin(); i != end(); ++i)
        rl.sub(*i);
    copy(rl.begin(), rl.end(), back_inserter(*this));
}

/* Cut a region out of the rectangle list, i.e., remove all points
   that are _not_ covered by `rect'. */
void RectList::sub(const Rect& rect) {
    RectList rl;
    for (iterator i=begin(); i != end(); ++i) {
        int lft = max(i->x, rect.x);
        int top = max(i->y, rect.y);
        int rgt = min(i->x + i->w, rect.x + rect.w);
        int bot = min(i->y + i->h, rect.y + rect.h);

        if (rgt > lft && bot > top) {
            // the coordinates of the intersection rectangle
            rl.push_back(Rect(i->x, i->y, i->w,  top - i->y));
            rl.push_back(Rect(i->x, top, lft - i->x, bot - top));
            rl.push_back(Rect(rgt, top, i->x + i->w - rgt, bot - top));
            rl.push_back(Rect(i->x, bot, i->w, i->y + i->h - bot));
        } 
        else
            rl.push_back(*i);
    }
    swap(rl);
}