SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
image_ref.h
Go to the documentation of this file.
1 /*
2  This file is part of the CVD Library.
3 
4  Copyright (C) 2005 The Authors
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 //-*- c++ -*-
23 // //
24 // CVD::image.h //
25 // //
26 // Definitions for of template classes CVD::ImageRef and CVD::Image //
27 // //
28 // derived from IPRS_* developed by Tom Drummond //
29 // //
31 
32 #ifndef __CVD_IMAGE_REF_H__
33 #define __CVD_IMAGE_REF_H__
34 
35 #include <iostream>
36 #include <cctype>
37 #include "exceptions.h"
38 
39 namespace CVD {
40 
42 
44 // CVD::ImageRef //
47 class ImageRef
48 {
49 public:
50 
51  //Construction
53  inline ImageRef();
57  inline ImageRef(int xp, int yp);
60  inline ImageRef(std::istream& is);
61 
62  //Iteration
63 
68  inline bool next(const ImageRef& max);
73  inline bool prev(const ImageRef& max);
80  inline bool next(const ImageRef& min, const ImageRef& max);
87  inline bool prev(const ImageRef& min, const ImageRef& max);
88 
90  inline void home();
94  inline void end(const ImageRef& size);
95 
96 
97  //Operators
98 
101  inline ImageRef& operator=(const ImageRef& ref);
104  inline bool operator==(const ImageRef& ref) const;
107  inline bool operator!=(const ImageRef& ref) const;
109  inline ImageRef operator-() const;
112  inline ImageRef& operator*=(const double scale);
115  inline ImageRef& operator/=(const double scale);
118  inline ImageRef& operator+=(const ImageRef rhs);
121  inline ImageRef& operator-=(const ImageRef rhs);
124  inline ImageRef operator*(const double scale) const;
127  inline ImageRef operator/(const double scale) const;
130  inline ImageRef operator+(const ImageRef rhs) const;
133  inline ImageRef operator-(const ImageRef rhs) const;
136  inline ImageRef& operator<<=(int i);
139  inline ImageRef& operator>>=(int i);
142  inline ImageRef operator>>(int i) const;
145  inline ImageRef operator<<(int i) const;
151  inline bool operator<(const ImageRef & other) const;
155  inline bool operator>(const ImageRef & other) const;
156 
158  inline unsigned int mag_squared() const;
159 
161  inline int area() const;
162 
164  inline ImageRef dot_times(const ImageRef &ref) const;
165 
167  inline int& operator[](int i);
168 
170  inline int operator[](int i) const;
171 
172  //Why do these exist?
174  inline ImageRef shiftl(int i) const;
176  inline ImageRef shiftr(int i) const;
177 
178  // and now the data members (which are public!)
179  int x;
180  int y;
181 
182 };
183 
188 inline ImageRef operator*(const int scale, const ImageRef& ref);
189 
190 namespace Exceptions
191 {
194 }
195 
196 
197 
199  {
200  x=y=0;
201  }
202 
203  inline ImageRef::ImageRef(int xp, int yp)
204  :x(xp),y(yp)
205  {}
206 
207  inline ImageRef::ImageRef(std::istream& is)
208  {
209  is.read((char*)&x,sizeof(int));
210  is.read((char*)&y,sizeof(int));
211  }
212 
214  // the following cryptic pieces of rubbish are because inline functions //
215  // must have their one and only return function as the last call //
216  // so this code makes use of the fact that expressions to the right //
217  // of || are only evaluated when the left hand side is false //
219 
220  inline bool ImageRef::next(const ImageRef& max) // move on to the next value
221  {
222  return(++x < max.x || (x=0, ++y < max.y) || (y=0, false));
223  }
224 
225  inline bool ImageRef::next(const ImageRef& min, const ImageRef& max)
226  {
227  return (++x < max.x || (x=min.x, ++y < max.y) || (y=min.y, false));
228  }
229 
230  inline bool ImageRef::prev(const ImageRef& max) // move back to the previous value
231  {
232  return(--x > -1 || (x=max.x-1, --y > -1) || (y=max.y-1, false));
233  }
234 
235  inline bool ImageRef::prev(const ImageRef& min, const ImageRef& max)
236  {
237  return (--x > min.x-1 || (x=max.x-1, --y > min.y-1) || (y=max.y-1, false));
238  }
239 
240  inline void ImageRef::home()
241  {
242  x=y=0;
243  }
244 
245  inline void ImageRef::end(const ImageRef& size)
246  {
247  x=size.x-1;
248  y=size.y-1;
249  }
250 
252  {
253  x=ref.x;
254  y=ref.y;
255  return *this;
256  }
257 
258  inline bool ImageRef::operator ==(const ImageRef& ref) const
259  {
260  return (x==ref.x && y==ref.y);
261  }
262 
263  inline bool ImageRef::operator !=(const ImageRef& ref) const
264  {
265  return (x!=ref.x || y!=ref.y);
266  }
267 
269  {
270  ImageRef v(-x, -y);
271  return v;
272  }
273 
274  inline ImageRef& ImageRef::operator*=(const double scale)
275  {
276  x=(int)(x*scale);
277  y=(int)(y*scale);
278  return *this;
279  }
280 
281  inline ImageRef& ImageRef::operator/=(const double scale)
282  {
283  x=(int)(x/scale);
284  y=(int)(y/scale);
285  return *this;
286  }
287 
289  {
290  x+=rhs.x;
291  y+=rhs.y;
292  return *this;
293  }
294 
296  {
297  x-=rhs.x;
298  y-=rhs.y;
299  return *this;
300  }
301 
302  inline ImageRef ImageRef::operator*(const double scale) const
303  {
304  ImageRef v((int)(x*scale),(int)(y*scale));
305  return v;
306  }
307 
308  inline ImageRef ImageRef::operator/(const double scale) const
309  {
310  ImageRef v((int)(x/scale),(int)(y/scale));
311  return v;
312  }
313 
314  inline ImageRef ImageRef::operator+(const ImageRef rhs) const
315  {
316  ImageRef v(x+rhs.x, y+rhs.y);
317  return v;
318  }
319 
320  inline ImageRef ImageRef::operator-(const ImageRef rhs) const
321  {
322  ImageRef v(x-rhs.x, y-rhs.y);
323  return v;
324  }
325 
327  {
328  x = x << i;
329  y=y << i;
330  return *this;
331  }
332 
334  {
335  x = x >> i;
336  y=y >> i;
337  return *this;
338  }
339 
340  inline ImageRef ImageRef::shiftl(int i) const
341  {
342  ImageRef result;
343  result.x = x << i;
344  result.y=y << i;
345  return result;
346  }
347 
348  inline ImageRef ImageRef::shiftr(int i) const
349  {
350  ImageRef result;
351  result.x = x >> i;
352  result.y=y >> i;
353  return result;
354  }
355 
356  inline ImageRef ImageRef::operator<<(int i) const
357  {
358  return shiftl(i);
359  }
360 
361  inline ImageRef ImageRef::operator>>(int i) const
362  {
363  return shiftr(i);
364  }
365 
366 
367  inline ImageRef operator*(const int scale, const ImageRef& ref)
368  {
369  return ImageRef(ref.x*scale, ref.y*scale);
370  }
371 
372  inline bool ImageRef::operator<(const ImageRef & other) const
373  {
374  return y < other.y || ( y == other.y && x < other.x);
375  }
376 
377  inline bool ImageRef::operator>(const ImageRef & other) const
378  {
379  return y > other.y || ( y == other.y && x > other.x);
380  }
381 
382 
383  inline int& ImageRef::operator[](int i)
384  {
385  if(i==0)
386  return x;
387  if(i==1)
388  return y;
389  throw Exceptions::BadSubscript();
390  }
391 
392  inline int ImageRef::operator[](int i) const
393  {
394  if(i==0)
395  return x;
396  if(i==1)
397  return y;
398  throw Exceptions::BadSubscript();
399  }
400 
401  inline unsigned int ImageRef::mag_squared() const
402  {
403  typedef unsigned int uint;
404  return uint(x*x) + uint(y*y);
405  }
406 
407  inline int ImageRef::area() const
408  {
409  return x * y;
410  }
411 
412  inline ImageRef ImageRef::dot_times(const ImageRef &ref) const
413  {
414  return ImageRef(x * ref.x, y * ref.y);
415  }
416 
417 
418 // Streams stuff for ImageRef class //
419 
424 inline std::ostream& operator<<(std::ostream& os, const ImageRef& ref)
425 {
426  return os << "[" << ref.x << " " << ref.y << "]";
427 }
428 
431 inline std::istream& operator>>(std::istream& is, ImageRef& ref)
432 {
433  //Full parsing for ImageRefs, to allow it to accept the
434  //output produced above, as well as the older (x,y) format
435  is >> std::ws;
436 
437  unsigned char c = is.get();
438 
439  if(is.eof())
440  return is;
441 
442  if(c == '(' )
443  {
444  is >> std::ws >> ref.x >> std::ws;
445 
446  if(is.get() != ',')
447  goto bad;
448 
449  is >> std::ws >> ref.y >> std::ws;
450 
451  if(is.get() != ')')
452  goto bad;
453  }
454  else if(c == '[' )
455  {
456  is >> std::ws >> ref.x >> std::ws >> ref.y >> std::ws;
457  if(is.get() != ']')
458  goto bad;
459  }
460  else if(isdigit(c))
461  {
462  is.unget();
463  is >> ref.x >> ref.y;
464  }
465  else
466  goto bad;
467 
468  return is;
469 
470  bad:
471  is.setstate(std::ios_base::badbit);
472 
473  return is;
474 }
475 
478 const ImageRef ImageRef_zero(0, 0);
479 
480 
481 } //namespace CVD
482 
483 
484 #endif
ImageRef operator/(const double scale) const
Definition: image_ref.h:308
int y
The y co-ordinate.
Definition: image_ref.h:180
int x
The x co-ordinate.
Definition: image_ref.h:179
std::ostream & operator<<(std::ostream &os, const ImageRef &ref)
Definition: image_ref.h:424
ImageRef & operator+=(const ImageRef rhs)
Definition: image_ref.h:288
bool operator<(const ImageRef &other) const
Definition: image_ref.h:372
ImageRef & operator*=(const double scale)
Definition: image_ref.h:274
ImageRef operator*(const double scale) const
Definition: image_ref.h:302
ImageRef()
Construct an ImageRef initialised at (0,0)
Definition: image_ref.h:198
int area() const
Area (product of x and y; signed)
Definition: image_ref.h:407
ImageRef operator>>(int i) const
Definition: image_ref.h:361
Definition: abs.h:24
ImageRef & operator<<=(int i)
Definition: image_ref.h:326
unsigned int mag_squared() const
Magnitude-squared (x*x + y*y)
Definition: image_ref.h:401
bool prev(const ImageRef &max)
Definition: image_ref.h:230
bool next(const ImageRef &max)
Definition: image_ref.h:220
void end(const ImageRef &size)
Definition: image_ref.h:245
ImageRef dot_times(const ImageRef &ref) const
The equivalent of doing .* in matlab.
Definition: image_ref.h:412
ImageRef operator-() const
Unary minus. Negates both x and y components.
Definition: image_ref.h:268
ImageRef & operator=(const ImageRef &ref)
Definition: image_ref.h:251
ImageRef shiftr(int i) const
Definition: image_ref.h:348
ImageRef operator+(const ImageRef rhs) const
Definition: image_ref.h:314
bool operator==(const ImageRef &ref) const
Definition: image_ref.h:258
ImageRef & operator-=(const ImageRef rhs)
Definition: image_ref.h:295
ImageRef & operator>>=(int i)
Definition: image_ref.h:333
void home()
Resets the ImageRef to (0,0)
Definition: image_ref.h:240
ImageRef operator*(const int scale, const ImageRef &ref)
Definition: image_ref.h:367
Exception if subscript for [] is not 0 or 1.
Definition: image_ref.h:193
std::istream & operator>>(std::istream &is, ImageRef &ref)
Definition: image_ref.h:431
ImageRef & operator/=(const double scale)
Definition: image_ref.h:281
int & operator[](int i)
Square bracket subscripts for easy loops. 0=x 1=y other=error.
Definition: image_ref.h:383
ImageRef operator<<(int i) const
Definition: image_ref.h:356
bool operator>(const ImageRef &other) const
Definition: image_ref.h:377
bool operator!=(const ImageRef &ref) const
Definition: image_ref.h:263
ImageRef shiftl(int i) const
Definition: image_ref.h:340