forked from varoudis/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpixelref.h
176 lines (160 loc) · 5.82 KB
/
pixelref.h
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
// sala - a component of the depthmapX - spatial network analysis platform
// Copyright (C) 2011-2012, Tasos Varoudis
// 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 3 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, see <http://www.gnu.org/licenses/>.
#pragma once
#include "genlib/p2dpoly.h"
#include <vector>
class PixelRef
{
public:
short x;
short y;
PixelRef( short ax = -1, short ay = -1 )
{ x = ax; y = ay; }
PixelRef( int i )
{ x = short(i >> 16); y = short(i & 0xffff); }
bool empty()
{ return x == -1 && y == -1; }
PixelRef up() const
{ return PixelRef(x, y + 1); }
PixelRef left() const
{ return PixelRef(x - 1, y); }
PixelRef right() const
{ return PixelRef(x + 1, y); }
PixelRef down() const
{ return PixelRef(x, y - 1); }
short& operator [] (int i)
{ return (i == XAXIS) ? x : y; }
bool within( const PixelRef bl, const PixelRef tr ) const
{ return (x >= bl.x && x <= tr.x && y >= bl.y && y <= tr.y); }
bool encloses( PixelRef testpoint ) const
{ return (testpoint.x >= 0 && testpoint.x < x && testpoint.y >= 0 && testpoint.y < y);}
// directions for the ngraph:
enum {NODIR = 0x00, HORIZONTAL = 0x01, VERTICAL = 0x02, POSDIAGONAL = 0x04, NEGDIAGONAL = 0x08, DIAGONAL = 0x0c, NEGHORIZONTAL = 0x10, NEGVERTICAL = 0x20};
short& row(char dir)
{ return (dir & VERTICAL) ? x : y; }
short& col(char dir)
{ return (dir & VERTICAL) ? y : x; }
const short& row(char dir) const
{ return (dir & VERTICAL) ? x : y; }
const short& col(char dir) const
{ return (dir & VERTICAL) ? y : x; }
PixelRef& move(char dir)
{ switch (dir)
{
case POSDIAGONAL: x++; y++; break;
case NEGDIAGONAL: x++; y--; break;
case HORIZONTAL: x++; break;
case VERTICAL: y++; break;
case NEGHORIZONTAL: x--; break;
case NEGVERTICAL: y--; break;
}
return *this; }
bool isodd() const
{ return x % 2 == 1 && y % 2 == 1; }
bool iseven() const
{ return x % 2 == 0 && y % 2 == 0; }
friend bool operator == (const PixelRef a, const PixelRef b);
friend bool operator != (const PixelRef a, const PixelRef b);
friend bool operator < (const PixelRef a, const PixelRef b);
friend bool operator > (const PixelRef a, const PixelRef b);
friend PixelRef operator + (const PixelRef a, const PixelRef b);
friend PixelRef operator - (const PixelRef a, const PixelRef b);
friend PixelRef operator / (const PixelRef a, const int factor);
friend double dist(const PixelRef a, const PixelRef b);
friend double angle(const PixelRef a, const PixelRef b, const PixelRef c);
operator int() const
{ return ((int(x) << 16) + (int(y) & 0xffff)); }
};
const PixelRef NoPixel( -1, -1 );
inline bool operator == (const PixelRef a, const PixelRef b)
{
return (a.x == b.x) && (a.y == b.y);
}
inline bool operator != (const PixelRef a, const PixelRef b)
{
return (a.x != b.x) || (a.y != b.y);
}
inline bool operator < (const PixelRef a, const PixelRef b)
{
return (a.x < b.x) || (a.x == b.x && a.y < b.y);
}
inline bool operator > (const PixelRef a, const PixelRef b)
{
return (a.x > b.x) || (a.x == b.x && a.y > b.y);
}
inline PixelRef operator + (const PixelRef a, const PixelRef b)
{
return PixelRef(a.x + b.x, a.y + b.y);
}
inline PixelRef operator - (const PixelRef a, const PixelRef b)
{
return PixelRef(a.x - b.x, a.y - b.y);
}
inline PixelRef operator / (const PixelRef a, const int factor)
{
return PixelRef(a.x / factor, a.y / factor);
}
inline double dist(const PixelRef a, const PixelRef b)
{
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
inline double angle(const PixelRef a, const PixelRef b, const PixelRef c)
{
if (c == NoPixel) {
return 0.0;
}
else {
// n.b. 1e-12 required for floating point error
return acos( double((a.x - b.x) * (b.x - c.x) + (a.y - b.y) * (b.y - c.y)) /
(sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)) * sqrt(sqr(b.x - c.x) + sqr(b.y - c.y)) + 1e-12) );
}
}
// Now sizeof(PixelRef) == sizeof(int) better stored directly:
typedef std::vector<PixelRef> PixelRefVector;
/////////////////////////////////////////////////////////////////////////////////////////////////
struct PixelRefPair
{
PixelRef a;
PixelRef b;
PixelRefPair(const PixelRef x = NoPixel, const PixelRef y = NoPixel)
{
a = x < y ? x : y;
b = x < y ? y : x;
}
friend bool operator == (const PixelRefPair& x, const PixelRefPair& y);
friend bool operator != (const PixelRefPair& x, const PixelRefPair& y);
friend bool operator < (const PixelRefPair& x, const PixelRefPair& y);
friend bool operator > (const PixelRefPair& x, const PixelRefPair& y);
};
// note: these are made with a is always less than b
inline bool operator == (const PixelRefPair& x, const PixelRefPair& y)
{
return (x.a == y.a && x.b == y.b);
}
inline bool operator != (const PixelRefPair& x, const PixelRefPair& y)
{
return (x.a != y.a || x.b != y.b);
}
inline bool operator < (const PixelRefPair& x, const PixelRefPair& y)
{
return ( (x.a == y.a) ? x.b < y.b : x.a < y.a );
}
inline bool operator > (const PixelRefPair& x, const PixelRefPair& y)
{
return ( (x.a == y.a) ? x.b > y.b : x.a > y.a );
}
struct hashPixelRef {
size_t operator()(const PixelRef &pixelRef) const{
return std::hash<int>()(int(pixelRef));
}
};