forked from ryanpeach/openscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.hpp
157 lines (133 loc) · 3.08 KB
/
support.hpp
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
/**
* support.hpp
*
* @date Oct 31, 2015
* @author Ryan Peach
* @version v0.1
*/
#ifndef SUPPORT
#define SUPPORT
#include <vector>
#include "opencv2/core.hpp"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
using namespace cv;
typedef vector<Point> cnt;
typedef vector<Point> Points; //This is an unordered cnt.
struct Cnts {
vector<cnt> contours;
vector<Vec4i> heirarchy;
Cnts(vector<cnt> polys, vector<Vec4i> heir);
Cnts();
bool empty();
int size();
};
double distTol(cnt poly, double distRatio);
bool matEq(Mat m1, Mat m2);
/**
* Shifts all cells of lst, vec, or contour right by one cell, moves the last cell first.
* @param Any list/vector/cnt you wish to rotate.
* @return The rotated list/vector/contour.
* @complexity O(?)
* @tested untested
*/
template <typename Container>
Container rotateVec (Container vec){
Container out;
for (unsigned int i = 1; i < vec.size(); i++) {
out.push_back(vec[i]);
}
out.push_back(vec[0]);
return out;
}
/**
* Tests whether or not item is within lst.
* @param lst: Any list or vector.
* @return True/False item is inside lst.
* @complexity O(?)
* @tested untested
*/
template <typename Container, typename T>
bool contains(Container lst, T item) {
return find(lst.begin(),lst.end(),item)!=lst.end();
}
/**
* Tests whether or not item is within vec.
* @param lst: Any list or vector.
* @return first index of item inside lst.
* @return -1 if none found.
* @complexity O(n)
* @tested untested
*/
template <typename Container, typename T>
int index(Container lst, T item) {
for (unsigned int i = 0; i<lst.size(); i++){
if (lst[i]==item) {
return i;
}
}
return -1;
}
/**
* Converts any type into a formatted string, vstring does the same for vectors
* @param Any compatible type or vector of types to convert to string.
* @return Display string
* @complexity O(1), O(N)
* @tested works
*/
string tostr(double p);
string tostr(Point p);
string tostr(cnt contour);
template <typename Container>
string vtostr(Container vec) {
stringstream out;
out << "{";
for (auto v : vec) {
out << tostr(v);
out << ", ";
}
out << "}";
return out.str();
}
template<> string vtostr(Cnts vec);
/**
* Returns the sum of a vector of items which support addition.
* @param vector of numbers
* @return sum of numers
* @complexity O(N)
* @tested works
*/
double sum(vector<double> nums);
Point sum(Points pts);
/**
* Returns the mean of a vector of summable and divisible items.
* @param vector
* @return mean of vector
* @complexity O(N)
* @tested works
*/
template <typename T>
T mean(vector<T> nums){
return sum(nums) / nums.size();
}
/**
* Rounds n to the nearest odd number.
* @param any int
* @return none, modifies the int directly via pointers
* @complexity O(1)
* @tested untested
*/
void Odd(int *n);
int toOdd(int n);
/**
* Returns whether a == b given a certain tolerance.
* @param any two numbers
* @return whether abs(a-b)<=tol.
* @complexity O(1)
* @tested works
*/
bool tolEq(double a, double b, double tol);
bool tolEq(int a, int b, int tol);
#endif