File size: 5,046 Bytes
9375c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (C) 2011  Davis E. King ([email protected])
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_BORDER_EnUMERATOR_H_
#define DLIB_BORDER_EnUMERATOR_H_

#include "border_enumerator_abstract.h"
#include "rectangle.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    class border_enumerator
    {
    public:
        border_enumerator(
        ) 
        {
            reset();
        }

        border_enumerator(
            const rectangle& rect_,
            unsigned long border_size
        ) : 
            rect(rect_),
            inner_rect(shrink_rect(rect_, border_size))
        {
            reset();
        }

        border_enumerator(
            const rectangle& rect_,
            const rectangle& non_border_region
        ) : 
            rect(rect_),
            inner_rect(non_border_region.intersect(rect))
        {
            reset();
        }

        void reset (
        ) 
        {
            // make the four rectangles that surround inner_rect and intersect them
            // with rect.
            bleft   = rect.intersect(rectangle(std::numeric_limits<long>::min(),
                                               std::numeric_limits<long>::min(),
                                               inner_rect.left()-1,
                                               std::numeric_limits<long>::max()));

            bright  = rect.intersect(rectangle(inner_rect.right()+1,
                                               std::numeric_limits<long>::min(),
                                               std::numeric_limits<long>::max(),
                                               std::numeric_limits<long>::max()));

            btop    = rect.intersect(rectangle(inner_rect.left(),
                                               std::numeric_limits<long>::min(),
                                               inner_rect.right(),
                                               inner_rect.top()-1));

            bbottom = rect.intersect(rectangle(inner_rect.left(),
                                               inner_rect.bottom()+1,
                                               inner_rect.right(),
                                               std::numeric_limits<long>::max()));

            p = bleft.tl_corner();
            p.x() -= 1;

            mode = atleft;
        }

        bool at_start (
        ) const
        {
            point temp = bleft.tl_corner();
            temp.x() -=1;
            return temp == p;
        }

        bool current_element_valid(
        ) const
        {
            return rect.contains(p);
        }

        bool move_next()
        {
            if (mode == atleft)
            {
                if (advance_point(bleft, p))
                    return true;
                    
                mode = attop;
                p = btop.tl_corner();
                p.x() -= 1;
            }
            if (mode == attop)
            {
                if (advance_point(btop, p))
                    return true;
                    
                mode = atright;
                p = bright.tl_corner();
                p.x() -= 1;
            }
            if (mode == atright)
            {
                if (advance_point(bright, p))
                    return true;
                    
                mode = atbottom;
                p = bbottom.tl_corner();
                p.x() -= 1;
            }

            if (advance_point(bbottom, p))
                return true;

            // put p outside rect since there are no more points to enumerate
            p = rect.br_corner();
            p.x() += 1;
                    
            return false;
        }

        size_t size (
        ) const
        {
            return rect.area() - inner_rect.area();
        }

        const point& element (
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(current_element_valid(),
                "\t point border_enumerator::element()"
                << "\n\t This function can't be called unless the element is valid."
                << "\n\t this: " << this
                );

            return p;
        }

    private:

        bool advance_point (
            const rectangle& r,
            point& p
        ) const
        {
            p.x() += 1;
            if (p.x() > r.right())
            {
                p.x() = r.left();
                p.y() += 1;
            }

            return r.contains(p);
        }

        point p;
        rectangle rect;
        rectangle inner_rect;  // the non-border regions of rect

        enum emode
        {
            atleft,
            atright,
            atbottom,
            attop
        };

        emode mode;

        rectangle btop, bleft, bright, bbottom;
    };

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_BORDER_EnUMERATOR_H_