My Project
Util.h
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #ifndef UTIL_H
25 #define UTIL_H
26 
34 #include "Alvar.h"
35 #include "AlvarException.h"
36 
37 #include <cmath> //for abs
38 #include <iomanip>
39 #include <iostream>
40 #include <map>
41 #include <opencv2/core.hpp>
42 #include <sstream>
43 #include <vector>
44 
45 namespace alvar {
46 
47 const double PI = 3.14159265;
48 
52 template <class C>
53 inline int ALVAR_EXPORT
54 Sign(const C &v)
55 {
56  return (v < 0 ? -1 : 1);
57 }
58 
62 template <class C>
63 inline double ALVAR_EXPORT
64 Rad2Deg(const C &v)
65 {
66  return v * (180 / PI);
67 }
68 
72 template <class C>
73 inline double ALVAR_EXPORT
74 Deg2Rad(const C &v)
75 {
76  return v * (PI / 180);
77 }
78 
82 template <class C, class D = int>
83 struct ALVAR_EXPORT Point : public C
84 {
88  D val;
89 
90  Point(int vx = 0, int vy = 0)
91  {
92  C::x = vx;
93  C::y = vy;
94  }
95  Point(double vx, double vy)
96  {
97  C::x = vx;
98  C::y = vy;
99  }
100 };
101 
105 typedef ALVAR_EXPORT Point<cv::Point> PointInt;
106 
110 typedef ALVAR_EXPORT Point<cv::Point2d> PointDouble;
111 
117 template <class PointType>
118 double
119 PointSquaredDistance(PointType p1, PointType p2)
120 {
121  return ((p1.x - p2.x) * (p1.x - p2.x)) + ((p1.y - p2.y) * (p1.y - p2.y));
122 }
123 
124 //ttesis start
125 
130 int ALVAR_EXPORT dot(const cv::Point &A, const cv::Point &B, const cv::Point &C);
131 
137 int ALVAR_EXPORT cross(const cv::Point &A, const cv::Point &B, const cv::Point &C);
138 
143 double ALVAR_EXPORT distance(const cv::Point &A, const cv::Point &B);
144 
151 double ALVAR_EXPORT linePointDist(const cv::Point &A,
152  const cv::Point &B,
153  const cv::Point &C,
154  bool isSegment);
155 
164 double ALVAR_EXPORT angle(const cv::Point &A,
165  const cv::Point &B,
166  const cv::Point &C,
167  const cv::Point &D,
168  int isDirectionDependent);
169 
176 double ALVAR_EXPORT polyLinePointDist(const std::vector<cv::Point> &points,
177  const cv::Point & C,
178  int * index,
179  int isClosedPolygon);
180 //ttesis end
181 
187 void ALVAR_EXPORT FitCVEllipse(const std::vector<PointDouble> &points,
188  cv::RotatedRect & ellipse_box);
189 
190 int ALVAR_EXPORT exp_filt2(std::vector<double> &v, std::vector<double> &ret, bool clamp);
191 
198 template <class C>
199 inline int ALVAR_EXPORT
200 diff(const std::vector<C> &v, std::vector<C> &ret)
201 {
202  ret.clear();
203  if (v.size() == 1) {
204  ret.push_back(0);
205  } else if (v.size() > 1) {
206  ret.push_back(v.at(1) - v.at(0));
207  for (size_t i = 1; i < v.size(); ++i) {
208  ret.push_back(v.at(i) - v.at(i - 1));
209  }
210  }
211  return int(ret.size());
212 }
213 
221 int ALVAR_EXPORT find_zero_crossings(const std::vector<double> &v,
222  std::vector<int> & corners,
223  int offs = 20);
224 
228 void ALVAR_EXPORT out_matrix(const cv::Mat &m, const char *name);
229 
237 double ALVAR_EXPORT Limit(double val, double min_val, double max_val);
238 
246 struct ALVAR_EXPORT Index
247 {
249  std::vector<int> val;
251  Index(int a);
253  Index(int a, int b);
255  Index(int a, int b, int c);
257  bool operator<(const Index &index) const;
258 };
259 
263 class ALVAR_EXPORT Histogram
264 {
265 protected:
266  std::map<Index, int> bins;
267  std::vector<int> dim_binsize;
268  int DimIndex(int dim, double val);
269  double DimVal(int dim, int index);
270 
271 public:
274  void AddDimension(int binsize);
276  void Clear();
278  void Inc(double dim0, double dim1 = 0, double dim2 = 0);
282  int GetMax(double *dim0, double *dim1 = 0, double *dim2 = 0);
283 };
284 
288 class ALVAR_EXPORT HistogramSubpixel : public Histogram
289 {
290 protected:
291  std::map<Index, double> acc_dim0;
292  std::map<Index, double> acc_dim1;
293  std::map<Index, double> acc_dim2;
294 
295 public:
297  void Clear();
299  void Inc(double dim0, double dim1 = 0, double dim2 = 0);
304  int GetMax(double *dim0, double *dim1 = 0, double *dim2 = 0);
305 };
306 
307 #if (_MSC_VER >= 1400)
308 inline void
309 STRCPY(char *to, rsize_t size, const char *src)
310 {
311  strcpy_s(to, size, src);
312 }
313 #else
314 inline void
315 STRCPY(char *to, size_t size, const char *src)
316 {
317  strncpy(to, src, size - 1);
318 }
319 #endif
320 
321 #ifdef min
322 # undef min
323 #endif
324 
325 #ifdef max
326 # undef max
327 #endif
328 
370 class ALVAR_EXPORT Serialization
371 {
372 protected:
373  bool input;
374  std::string filename;
375  //std::iostream *stream;
376  std::ios *stream;
377  void * formatter_handle;
378  bool Output();
379  bool Input();
380  bool Descend(const char *id);
381  bool Ascend();
382 
383 public:
415  Serialization(std::string _filename);
417  Serialization(std::basic_iostream<char> &_stream);
419  Serialization(std::basic_istream<char> &_stream);
421  Serialization(std::basic_ostream<char> &_stream);
425  template <class C>
427  operator<<(C &serializable)
428  {
429  input = false;
430  if (!SerializeClass(serializable) || !Output()) {
431  throw(AlvarException("Serialization failure"));
432  }
433  return *this;
434  }
436  template <class C>
437  Serialization &
438  operator>>(C &serializable)
439  {
440  input = true;
441  if (!Input() || !SerializeClass(serializable)) {
442  throw(AlvarException("Serialization failure"));
443  }
444  return *this;
445  }
451  template <class C>
452  bool
453  SerializeClass(C &serializable)
454  {
455  std::string s = serializable.SerializeId();
456  if (!Descend(s.c_str()) || !serializable.Serialize(this) || !Ascend()) {
457  return false;
458  }
459  return true;
460  }
462  bool Serialize(int &data, const std::string &name);
464  bool Serialize(unsigned short &data, const std::string &name);
466  bool Serialize(unsigned long &data, const std::string &name);
468  bool Serialize(double &data, const std::string &name);
470  bool Serialize(std::string &data, const std::string &name);
472  bool Serialize(cv::Mat &data, const std::string &name);
474  bool
476  {
477  return input;
478  }
479 };
480 
481 } // namespace alvar
482 
483 #endif
This file defines library export definitions, version numbers and build information.
This file implements the ALVAR exception class.
ALVAR exception class.
Class for N-dimensional Histograms.
Definition: Util.h:264
void AddDimension(int binsize)
Add dimension with a binsize.
void Clear()
Clear the histogram.
void Inc(double dim0, double dim1=0, double dim2=0)
Increase the histogram for given dimensions.
int GetMax(double *dim0, double *dim1=0, double *dim2=0)
Get the maximum from the histogram This returns the value in the middle of the 'bin' instead of bin-n...
N-dimensional Histograms calculating also the subpixel average for max bin.
Definition: Util.h:289
void Clear()
Clear the histogram.
void Inc(double dim0, double dim1=0, double dim2=0)
Increase the histogram for given dimensions.
int GetMax(double *dim0, double *dim1=0, double *dim2=0)
Get the maximum from the histogram This finds the maximum bin(s) and averages the original values con...
Class for serializing class content to/from file or std::iostream.
Definition: Util.h:371
bool Serialize(cv::Mat &data, const std::string &name)
Method for serializing 'cv::Mat' data element. Used from your serializable class.
bool SerializeClass(C &serializable)
Method for serializing a serializable class. Used by operators << and >> .
Definition: Util.h:453
Serialization(std::string _filename)
Constructor for serializing to/from specified filename.
bool Serialize(std::string &data, const std::string &name)
Method for serializing 'std::string' data element. Used from your serializable class.
bool Serialize(unsigned long &data, const std::string &name)
Method for serializing 'int' data element. Used from your serializable class.
Serialization(std::basic_iostream< char > &_stream)
Constructor for serializing any iostream (e.g. std::stringstream)
bool Serialize(int &data, const std::string &name)
Method for serializing 'int' data element. Used from your serializable class.
bool IsInput()
Method for checking if we are inputting or outputting. Can be used from your serializable class.
Definition: Util.h:475
Serialization & operator>>(C &serializable)
Operator for reading a serializable class from the defined filename or std::iostream.
Definition: Util.h:438
bool Serialize(unsigned short &data, const std::string &name)
Method for serializing 'int' data element. Used from your serializable class.
bool Serialize(double &data, const std::string &name)
Method for serializing 'double' data element. Used from your serializable class.
Serialization(std::basic_istream< char > &_stream)
Constructor for serializing any istream (e.g. std::cin)
Serialization(std::basic_ostream< char > &_stream)
Constructor for serializing any ostream (e.g. std::cout)
~Serialization()
Destructor.
Main ALVAR namespace.
Definition: Alvar.h:174
void ALVAR_EXPORT out_matrix(const cv::Mat &m, const char *name)
Output OpenCV matrix for debug purposes.
int ALVAR_EXPORT find_zero_crossings(const std::vector< double > &v, std::vector< int > &corners, int offs=20)
Finds zero crossings of given vector elements (sequence).
double ALVAR_EXPORT distance(const cv::Point &A, const cv::Point &B)
Compute the distance from A to B.
double ALVAR_EXPORT linePointDist(const cv::Point &A, const cv::Point &B, const cv::Point &C, bool isSegment)
Computes the distance from point C to line (segment) AB.
double ALVAR_EXPORT Limit(double val, double min_val, double max_val)
Limits a number to between two values.
ALVAR_EXPORT Point< cv::Point > PointInt
The default integer point type.
Definition: Util.h:105
ALVAR_EXPORT Point< cv::Point2d > PointDouble
The default double point type.
Definition: Util.h:110
double ALVAR_EXPORT angle(const cv::Point &A, const cv::Point &B, const cv::Point &C, const cv::Point &D, int isDirectionDependent)
Computes the angle between lines AB and CD.
double PointSquaredDistance(PointType p1, PointType p2)
Returns the squared distance of two points.
Definition: Util.h:119
double ALVAR_EXPORT Deg2Rad(const C &v)
Converts an angle from degrees to radians.
Definition: Util.h:74
int ALVAR_EXPORT cross(const cv::Point &A, const cv::Point &B, const cv::Point &C)
Computes the cross product AB x AC.
int ALVAR_EXPORT dot(const cv::Point &A, const cv::Point &B, const cv::Point &C)
Computes dot product AB.BC.
double ALVAR_EXPORT polyLinePointDist(const std::vector< cv::Point > &points, const cv::Point &C, int *index, int isClosedPolygon)
Calculates minimum distance from Point C to Polygon whose points are in list PointList.
int ALVAR_EXPORT diff(const std::vector< C > &v, std::vector< C > &ret)
Calculates the difference between the consecutive vector elements.
Definition: Util.h:200
int ALVAR_EXPORT Sign(const C &v)
Returns the sign of a number.
Definition: Util.h:54
double ALVAR_EXPORT Rad2Deg(const C &v)
Converts an angle from radians to degrees.
Definition: Util.h:64
void ALVAR_EXPORT FitCVEllipse(const std::vector< PointDouble > &points, cv::RotatedRect &ellipse_box)
Uses OpenCV routine to fit ellipse to a vector of points.
Class for N-dimensional index to be used e.g. with STL maps.
Definition: Util.h:247
std::vector< int > val
The indices for each dimension are stored in val (last being the most significant)
Definition: Util.h:249
Index(int a)
Constructor for 1D index.
Index(int a, int b, int c)
Constructor for 3D index.
Index(int a, int b)
Constructor for 2D index.
bool operator<(const Index &index) const
Operator used for sorting the multidimensional indices (last dimension being the most significant)
Simple Point class meant to be inherited from OpenCV point-classes. For example: Point<cv::Point2d> p...
Definition: Util.h:84
D val
Additional value can be related to the point.
Definition: Util.h:88