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 #include <vector>
37 #include <iostream>
38 #include <iomanip>
39 #include <sstream>
40 #include <cxcore.h>
41 #include <cv.h>
42 #include <cmath> //for abs
43 #include <map>
44 #include <opencv2/calib3d/calib3d_c.h> //Compatibility with OpenCV 3.x
45 
46 namespace alvar {
47 
48 const double PI = 3.14159265;
49 
53 template<class C> inline
54 int ALVAR_EXPORT Sign(const C& v)
55 {
56  return (v<0?-1:1);
57 }
58 
62 template<class C> inline
63 double ALVAR_EXPORT Rad2Deg(const C& v)
64 {
65  return v*(180/PI);
66 }
67 
71 template<class C> inline
72 double ALVAR_EXPORT Deg2Rad(const C& v)
73 {
74  return v*(PI/180);
75 }
76 
80 template<class C, class D = int>
81 struct ALVAR_EXPORT Point : public C
82 {
86  D val;
87 
88  Point(int vx=0, int vy=0)
89  {
90  C::x = vx;
91  C::y = vy;
92  }
93  Point(double vx, double vy)
94  {
95  C::x = vx;
96  C::y = vy;
97  }
98 };
99 
104 
109 
115 template<class PointType>
116 double PointSquaredDistance(PointType p1, PointType p2) {
117  return ((p1.x-p2.x)*(p1.x-p2.x)) +
118  ((p1.y-p2.y)*(p1.y-p2.y));
119 }
120 
121 
122 //ttesis start
123 
124 
129 int ALVAR_EXPORT dot(CvPoint *A, CvPoint *B, CvPoint *C);
130 
136 int ALVAR_EXPORT cross(CvPoint *A,CvPoint *B, CvPoint *C);
137 
138 
143 double ALVAR_EXPORT distance(CvPoint *A,CvPoint *B);
144 
151 double ALVAR_EXPORT linePointDist(CvPoint *A,CvPoint *B,CvPoint *C, bool isSegment);
152 
153 
162 double ALVAR_EXPORT angle(CvPoint *A,CvPoint *B, CvPoint *C,CvPoint *D, int isDirectionDependent);
163 
164 
171 double ALVAR_EXPORT polyLinePointDist(CvPoint *PointList, int nPnts,CvPoint *C, int *index, int isClosedPolygon);
172 
173 //ttesis end
174 
175 
181 void ALVAR_EXPORT FitCVEllipse(const std::vector<PointDouble> &points, CvBox2D& ellipse_box);
182 
183 int ALVAR_EXPORT exp_filt2(std::vector<double> &v,std:: vector<double> &ret, bool clamp);
184 
191 template<class C> inline
192 int ALVAR_EXPORT diff(const std::vector<C> &v, std::vector<C> &ret)
193 {
194  ret.clear();
195  if (v.size() == 1) {
196  ret.push_back(0);
197  } else if (v.size() > 1) {
198  ret.push_back(v.at(1)-v.at(0));
199  for(size_t i = 1; i < v.size(); ++i)
200  {
201  ret.push_back(v.at(i)-v.at(i-1));
202  }
203  }
204  return int(ret.size());
205 }
206 
214 int ALVAR_EXPORT find_zero_crossings(const std::vector<double>& v, std::vector<int> &corners, int offs = 20);
215 
219 void ALVAR_EXPORT out_matrix(const CvMat *m, const char *name);
220 
228 double ALVAR_EXPORT Limit(double val, double min_val, double max_val);
229 
239  std::vector<int> val;
241  Index(int a);
243  Index(int a, int b);
245  Index(int a, int b, int c);
247  bool operator<(const Index &index) const;
248 };
249 
254 protected:
255  std::map<Index, int> bins;
256  std::vector<int> dim_binsize;
257  int DimIndex(int dim, double val);
258  double DimVal(int dim, int index);
259 public:
262  void AddDimension(int binsize);
264  void Clear();
266  void Inc(double dim0, double dim1=0, double dim2=0);
270  int GetMax(double *dim0, double *dim1=0, double *dim2=0);
271 };
272 
277 protected:
278  std::map<Index, double> acc_dim0;
279  std::map<Index, double> acc_dim1;
280  std::map<Index, double> acc_dim2;
281 public:
283  void Clear();
285  void Inc(double dim0, double dim1=0, double dim2=0);
290  int GetMax(double *dim0, double *dim1=0, double *dim2=0);
291 };
292 
293 #if (_MSC_VER >= 1400)
294  inline void STRCPY(char *to, rsize_t size, const char *src) {
295  strcpy_s(to,size,src);
296  }
297 #else
298  inline void STRCPY(char *to, size_t size, const char *src) {
299  strncpy(to,src,size-1);
300  }
301 #endif
302 
303 #ifdef min
304 #undef min
305 #endif
306 
307 #ifdef max
308 #undef max
309 #endif
310 
353 protected:
354  bool input;
355  std::string filename;
356  //std::iostream *stream;
357  std::ios *stream;
359  bool Output();
360  bool Input();
361  bool Descend(const char *id);
362  bool Ascend();
363 public:
395  Serialization(std::string _filename);
397  Serialization(std::basic_iostream<char> &_stream);
399  Serialization(std::basic_istream<char> &_stream);
401  Serialization(std::basic_ostream<char> &_stream);
403  ~Serialization();
405  template <class C>
406  Serialization& operator<<(C &serializable) {
407  input=false;
408  if (!SerializeClass(serializable) || !Output()) {
409  throw(AlvarException("Serialization failure"));
410  }
411  return *this;
412  }
414  template <class C>
415  Serialization& operator>>(C &serializable) {
416  input=true;
417  if (!Input() || !SerializeClass(serializable)) {
418  throw(AlvarException("Serialization failure"));
419  }
420  return *this;
421  }
427  template <class C>
428  bool SerializeClass(C &serializable) {
429  std::string s = serializable.SerializeId();
430  if (!Descend(s.c_str()) || !serializable.Serialize(this) || !Ascend()) {
431  return false;
432  }
433  return true;
434  }
436  bool Serialize(int &data, const std::string &name);
438  bool Serialize(unsigned short &data, const std::string &name);
440  bool Serialize(unsigned long &data, const std::string &name);
442  bool Serialize(double &data, const std::string &name);
444  bool Serialize(std::string &data, const std::string &name);
446  bool Serialize(CvMat &data, const std::string &name);
448  bool IsInput() { return input; }
449 };
450 
451 } // namespace alvar
452 
453 #endif
Main ALVAR namespace.
Definition: Alvar.h:174
double ALVAR_EXPORT distance(CvPoint *A, CvPoint *B)
Compute the distance from A to B.
Definition: Util.cpp:54
std::ios * stream
Definition: Util.h:357
Serialization & operator<<(C &serializable)
Operator for outputting a serializable class into the defined filename or std::iostream.
Definition: Util.h:406
int ALVAR_EXPORT exp_filt2(std::vector< double > &v, std::vector< double > &ret, bool clamp)
int ALVAR_EXPORT cross(CvPoint *A, CvPoint *B, CvPoint *C)
Computes the cross product AB x AC.
Definition: Util.cpp:44
void ALVAR_EXPORT FitCVEllipse(const std::vector< PointDouble > &points, CvBox2D &ellipse_box)
Uses OpenCV routine to fit ellipse to a vector of points.
Output
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).
bool IsInput()
Method for checking if we are inputting or outputting. Can be used from your serializable class...
Definition: Util.h:448
ALVAR_EXPORT Point< CvPoint > PointInt
The default integer point type.
Definition: Util.h:103
double ALVAR_EXPORT polyLinePointDist(CvPoint *PointList, int nPnts, CvPoint *C, int *index, int isClosedPolygon)
Calculates minimum distance from Point C to Polygon whose points are in list PointList.
Definition: Util.cpp:89
XmlRpcServer s
int ALVAR_EXPORT dot(CvPoint *A, CvPoint *B, CvPoint *C)
Computes dot product AB.BC.
Definition: Util.cpp:34
Input
double ALVAR_EXPORT Deg2Rad(const C &v)
Converts an angle from degrees to radians.
Definition: Util.h:72
N-dimensional Histograms calculating also the subpixel average for max bin.
Definition: Util.h:276
void ALVAR_EXPORT out_matrix(const CvMat *m, const char *name)
Output OpenCV matrix for debug purposes.
Definition: Util.cpp:216
std::map< Index, double > acc_dim0
Definition: Util.h:278
int ALVAR_EXPORT Sign(const C &v)
Returns the sign of a number.
Definition: Util.h:54
double ALVAR_EXPORT linePointDist(CvPoint *A, CvPoint *B, CvPoint *C, bool isSegment)
Computes the distance from point C to line (segment) AB.
Definition: Util.cpp:61
Point(double vx, double vy)
Definition: Util.h:93
D val
Additional value can be related to the point.
Definition: Util.h:86
double ALVAR_EXPORT Rad2Deg(const C &v)
Converts an angle from radians to degrees.
Definition: Util.h:63
std::map< Index, double > acc_dim1
Definition: Util.h:279
ALVAR exception class.
Point(int vx=0, int vy=0)
Definition: Util.h:88
Class for N-dimensional index to be used e.g. with STL maps.
Definition: Util.h:237
double PointSquaredDistance(PointType p1, PointType p2)
Returns the squared distance of two points.
Definition: Util.h:116
bool SerializeClass(C &serializable)
Method for serializing a serializable class. Used by operators << and >> .
Definition: Util.h:428
const double PI
Definition: Util.h:48
#define ALVAR_EXPORT
Definition: Alvar.h:168
std::map< Index, double > acc_dim2
Definition: Util.h:280
std::vector< int > dim_binsize
Definition: Util.h:256
void * formatter_handle
Definition: Util.h:358
Simple Point class meant to be inherited from OpenCV point-classes. For example: Point<CvPoint2D64f> ...
Definition: Util.h:81
ALVAR_EXPORT Point< CvPoint2D64f > PointDouble
The default double point type.
Definition: Util.h:108
void STRCPY(char *to, size_t size, const char *src)
Definition: Util.h:298
This file implements the ALVAR exception class.
std::map< Index, int > bins
Definition: Util.h:255
Class for serializing class content to/from file or std::iostream.
Definition: Util.h:352
Class for N-dimensional Histograms.
Definition: Util.h:253
Serialization & operator>>(C &serializable)
Operator for reading a serializable class from the defined filename or std::iostream.
Definition: Util.h:415
This file defines library export definitions, version numbers and build information.
std::vector< int > val
The indices for each dimension are stored in val (last being the most significant) ...
Definition: Util.h:239
double ALVAR_EXPORT Limit(double val, double min_val, double max_val)
Limits a number to between two values.
Definition: Util.cpp:241
std::string filename
Definition: Util.h:355
int ALVAR_EXPORT diff(const std::vector< C > &v, std::vector< C > &ret)
Calculates the difference between the consecutive vector elements.
Definition: Util.h:192
double ALVAR_EXPORT angle(CvPoint *A, CvPoint *B, CvPoint *C, CvPoint *D, int isDirectionDependent)
Computes the angle between lines AB and CD.
Definition: Util.cpp:72


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04