libcvd
image_ref_implementation.hh
1 constexpr inline ImageRef::ImageRef()
2  :x(0),y(0)
3 {
4 }
5 
6 constexpr inline ImageRef::ImageRef(int xp, int yp)
7  :x(xp),y(yp)
8 {}
9 
10 inline ImageRef::ImageRef(std::istream& is)
11 {
12  is.read((char*)&x,sizeof(int));
13  is.read((char*)&y,sizeof(int));
14 }
15 
17 // the following cryptic pieces of rubbish are because inline functions //
18 // must have their one and only return function as the last call //
19 // so this code makes use of the fact that expressions to the right //
20 // of || are only evaluated when the left hand side is false //
22 
23 inline bool ImageRef::next(const ImageRef& max) // move on to the next value
24 {
25  return(++x < max.x || (x=0, ++y < max.y) || (y=0, false));
26 }
27 
28 inline bool ImageRef::next(const ImageRef& min, const ImageRef& max)
29 {
30  return (++x < max.x || (x=min.x, ++y < max.y) || (y=min.y, false));
31 }
32 
33 inline bool ImageRef::prev(const ImageRef& max) // move back to the previous value
34 {
35  return(--x > -1 || (x=max.x-1, --y > -1) || (y=max.y-1, false));
36 }
37 
38 inline bool ImageRef::prev(const ImageRef& min, const ImageRef& max)
39 {
40  return (--x > min.x-1 || (x=max.x-1, --y > min.y-1) || (y=max.y-1, false));
41 }
42 
43 inline void ImageRef::home()
44 {
45  x=y=0;
46 }
47 
48 inline void ImageRef::end(const ImageRef& size)
49 {
50  x=size.x-1;
51  y=size.y-1;
52 }
53 
54 constexpr inline ImageRef& ImageRef::operator=(const ImageRef& ref)
55 {
56  x=ref.x;
57  y=ref.y;
58  return *this;
59 }
60 
61 constexpr inline bool ImageRef::operator ==(const ImageRef& ref) const
62 {
63  return (x==ref.x && y==ref.y);
64 }
65 
66 constexpr inline bool ImageRef::operator !=(const ImageRef& ref) const
67 {
68  return (x!=ref.x || y!=ref.y);
69 }
70 
71 constexpr inline ImageRef ImageRef::operator-() const
72 {
73  ImageRef v(-x, -y);
74  return v;
75 }
76 
77 constexpr inline ImageRef& ImageRef::operator*=(const double scale)
78 {
79  x=(int)(x*scale);
80  y=(int)(y*scale);
81  return *this;
82 }
83 
84 constexpr inline ImageRef& ImageRef::operator/=(const double scale)
85 {
86  x=(int)(x/scale);
87  y=(int)(y/scale);
88  return *this;
89 }
90 
91 constexpr inline ImageRef& ImageRef::operator+=(const ImageRef rhs)
92 {
93  x+=rhs.x;
94  y+=rhs.y;
95  return *this;
96 }
97 
98 constexpr inline ImageRef& ImageRef::operator-=(const ImageRef rhs)
99 {
100  x-=rhs.x;
101  y-=rhs.y;
102  return *this;
103 }
104 
105 constexpr inline ImageRef ImageRef::operator*(const double scale) const
106 {
107  ImageRef v((int)(x*scale),(int)(y*scale));
108  return v;
109 }
110 
111 constexpr inline ImageRef ImageRef::operator/(const double scale) const
112 {
113  ImageRef v((int)(x/scale),(int)(y/scale));
114  return v;
115 }
116 
117 constexpr inline ImageRef ImageRef::operator+(const ImageRef rhs) const
118 {
119  ImageRef v(x+rhs.x, y+rhs.y);
120  return v;
121 }
122 
123 constexpr inline ImageRef ImageRef::operator-(const ImageRef rhs) const
124 {
125  ImageRef v(x-rhs.x, y-rhs.y);
126  return v;
127 }
128 
129 constexpr inline ImageRef& ImageRef::operator<<=(int i)
130 {
131  x = x << i;
132  y=y << i;
133  return *this;
134 }
135 
136 constexpr inline ImageRef& ImageRef::operator>>=(int i)
137 {
138  x = x >> i;
139  y=y >> i;
140  return *this;
141 }
142 
143 constexpr inline ImageRef ImageRef::shiftl(int i) const
144 {
145  ImageRef result;
146  result.x = x << i;
147  result.y=y << i;
148  return result;
149 }
150 
151 constexpr inline ImageRef ImageRef::shiftr(int i) const
152 {
153  ImageRef result;
154  result.x = x >> i;
155  result.y=y >> i;
156  return result;
157 }
158 
159 constexpr inline ImageRef ImageRef::operator<<(int i) const
160 {
161  return shiftl(i);
162 }
163 
164 constexpr inline ImageRef ImageRef::operator>>(int i) const
165 {
166  return shiftr(i);
167 }
168 
169 
170 constexpr inline ImageRef operator*(const int scale, const ImageRef& ref)
171 {
172  return ImageRef(ref.x*scale, ref.y*scale);
173 }
174 
175 constexpr inline bool ImageRef::operator<(const ImageRef & other) const
176 {
177  return y < other.y || ( y == other.y && x < other.x);
178 }
179 
180 constexpr inline bool ImageRef::operator>(const ImageRef & other) const
181 {
182  return y > other.y || ( y == other.y && x > other.x);
183 }
184 
185 
186 constexpr inline int& ImageRef::operator[](int i)
187 {
188  if(i==0)
189  return x;
190  else if(i==1)
191  return y;
192  else
193  throw Exceptions::BadSubscript();
194 }
195 
196 constexpr inline int ImageRef::operator[](int i) const
197 {
198  if(i==0)
199  return x;
200  else if(i==1)
201  return y;
202  else
203  throw Exceptions::BadSubscript();
204 }
205 
206 constexpr inline unsigned int ImageRef::mag_squared() const
207 {
208  typedef unsigned int uint;
209  return uint(x*x) + uint(y*y);
210 }
211 
212 constexpr inline int ImageRef::area() const
213 {
214  return x * y;
215 }
216 
217 constexpr inline ImageRef ImageRef::dot_times(const ImageRef &ref) const
218 {
219  return ImageRef(x * ref.x, y * ref.y);
220 }
constexpr ImageRef operator/(const double scale) const
Divide both x and y co-ordinates by a scalar.
Definition: image_ref.h:112
constexpr ImageRef shiftl(int i) const
Definition: image_ref.h:144
constexpr ImageRef shiftr(int i) const
Definition: image_ref.h:152
constexpr int area() const
Area (product of x and y; signed)
Definition: image_ref.h:213
constexpr ImageRef & operator*=(const double scale)
Multiply both x and y co-ordinates by a scalar.
Definition: image_ref.h:78
constexpr ImageRef operator+(const ImageRef rhs) const
Add an offset to the co-ordinate.
Definition: image_ref.h:118
constexpr ImageRef()
Construct an ImageRef initialised at (0,0)
Definition: image_ref.h:2
constexpr bool operator>(const ImageRef &other) const
An ImageRef is greater than another ImageRef if it is earlier in the standard horizontal scan-line or...
Definition: image_ref.h:181
constexpr ImageRef & operator<<=(int i)
Bitwise left-shift operator.
Definition: image_ref.h:130
constexpr unsigned int mag_squared() const
Magnitude-squared (x*x + y*y)
Definition: image_ref.h:207
bool next(const ImageRef &max)
Step to the next co-ordinate in the image (in horizontal scanline order).
Definition: image_ref.h:24
constexpr ImageRef operator>>(int i) const
Bitwise right-shift operator.
Definition: image_ref.h:165
constexpr ImageRef operator*(const double scale) const
Multiply both x and y co-ordinates by a scalar.
Definition: image_ref.h:106
bool prev(const ImageRef &max)
Step to the previous co-ordinate in the image (in horizontal scanline order).
Definition: image_ref.h:34
constexpr ImageRef & operator>>=(int i)
Bitwise right-shift operator.
Definition: image_ref.h:137
constexpr ImageRef & operator=(const ImageRef &ref)
Assigment.
Definition: image_ref.h:55
constexpr ImageRef & operator+=(const ImageRef rhs)
Add an offset to the co-ordinate.
Definition: image_ref.h:92
int x
The x co-ordinate.
Definition: image_ref.h:172
constexpr ImageRef operator-() const
Unary minus. Negates both x and y components.
Definition: image_ref.h:72
constexpr bool operator<(const ImageRef &other) const
An ImageRef is less than another ImageRef if it is earlier in the standard horizontal scan-line order...
Definition: image_ref.h:176
constexpr bool operator!=(const ImageRef &ref) const
Logical not equals.
Definition: image_ref.h:67
void home()
Resets the ImageRef to (0,0)
Definition: image_ref.h:44
constexpr bool operator==(const ImageRef &ref) const
Logical equals.
Definition: image_ref.h:62
constexpr int & operator[](int i)
Square bracket subscripts for easy loops. 0=x 1=y other=error.
Definition: image_ref.h:187
void end(const ImageRef &size)
Resets the ImageRef to the maximum co-ordinate in the image i.e.
Definition: image_ref.h:49
constexpr ImageRef & operator-=(const ImageRef rhs)
Subtract an offset from the co-ordinate.
Definition: image_ref.h:99
constexpr ImageRef operator<<(int i) const
Bitwise left-shift operator.
Definition: image_ref.h:160
constexpr ImageRef dot_times(const ImageRef &ref) const
The equivalent of doing .* in matlab.
Definition: image_ref.h:218
constexpr ImageRef & operator/=(const double scale)
Divide both x and y co-ordinates by a scalar.
Definition: image_ref.h:85