34 int dot(CvPoint *A, CvPoint *B, CvPoint *C){
40 int dot = AB.x * BC.x + AB.y * BC.y;
44 int cross(CvPoint *A,CvPoint *B, CvPoint *C){
50 int cross = AB.x * AC.y - AB.y * AC.x;
55 double d1 = A->x - B->x;
56 double d2 = A->y - B->y;
57 return sqrt(d1*d1+d2*d2);
64 int dot1 =
dot(A,B,C);
66 int dot2 =
dot(B,A,C);
72 double angle(CvPoint *A,CvPoint *B, CvPoint *C,CvPoint *D,
int isDirectionDependent){
74 double a = B->x - A->x;
75 double b = B->y - A->y;
76 double c = D->x - C->x;
77 double d = D->y - C->y;
78 angle = acos( ((a * c) + (b * d) )/(sqrt(a*a + b*b) * sqrt(c*c + d*d)));
79 if(isDirectionDependent){
89 double polyLinePointDist(CvPoint *PointList,
int nPnts,CvPoint *C,
int *index,
int isClosedPolygon){
97 for(
int i=0; i < nPnts-1; i++){
99 if (mindist == -1 || dist<mindist){
117 void FitCVEllipse(
const vector<PointDouble> &points, CvBox2D& ellipse_box)
119 if(points.size() < 8)
return;
121 CvMat* vector = cvCreateMat(1,
int(points.size()), CV_64FC2);
122 for(
size_t i = 0; i < points.size(); ++i)
124 CV_MAT_ELEM(*vector, CvPoint2D64f, 0, i) = (CvPoint2D64f)points[i];
126 ellipse_box = cvFitEllipse2(vector);
127 cvReleaseMat(&vector);
130 int exp_filt2(vector<double> &v, vector<double> &ret,
bool clamp)
133 int n = (int)v.size();
135 double a = pow(0.01, 8.0/n);
139 vector<double> yp(n);
142 for(
int i = 0; i < n; ++i)
145 y *= 1.0 / (1.0-pow(a,n));
147 for(
int i = 0; i < n; ++i)
154 vector<double> ym(n);
157 for(
int i = n-1; i >= 0; --i)
160 y *= 1.0 / (1.0-pow(a,n));
162 for(
int i = n-1; i >= 0; --i)
170 for(
int i = 0; i < n; ++i)
172 ret[i] = (k/2.0) * (yp[i] + ym[i] - v[i]);
175 return int(ret.size());
181 int len = (int)v.size();
184 if(
Sign(v.at(0)) == 1)
190 for(
int i = 0; i < len+offs; ++i)
197 int s =
Sign(v.at(ind));
198 if(state == 1 && s == -1)
200 if(state == 2 && s == 1)
204 for(
unsigned j = 0; j < corners.size(); ++j)
205 if(corners.at(j) == ind)
209 corners.push_back(ind);
213 return int(corners.size());
218 std::cout<<name<<
" = [";
219 for (
int j=0; j<m->rows; j++) {
220 std::cout<<
" "<<cvGet2D(m, j, 0).val[0];
222 std::cout<<
"]^T"<<std::endl;
223 }
else if (m->rows == 1) {
224 std::cout<<name<<
" = [";
225 for (
int i=0; i<m->cols; i++) {
226 std::cout<<
" "<<cvGet2D(m, 0, i).val[0];
228 std::cout<<
"]^T"<<std::endl;
230 std::cout<<name<<
" = ["<<std::endl;
231 for (
int j=0; j<m->rows; j++) {
232 for (
int i=0; i<m->cols; i++) {
233 std::cout<<
" "<<cvGet2D(m, j, i).val[0];
235 std::cout<<std::endl;
237 std::cout<<
"]"<<std::endl;
241 double Limit(
double val,
double min_val,
double max_val) {
242 return max(min_val,
min(max_val, val));
245 Index::Index(
int a) { val.push_back(a); }
247 Index::Index(
int a,
int b) {
252 Index::Index(
int a,
int b,
int c) {
258 bool Index::operator<(
const Index &index)
const {
262 while ((d < val.size()) || (d < index.
val.size())) {
263 int v0 = (d < val.size() ? val[
d] : 0);
264 int v1 = (d < index.
val.size() ? index.
val[
d] : 0);
266 else if (v1<v0) comp=1;
269 if (comp == -1)
return true;
273 int Histogram::DimIndex(
int dim,
double val) {
274 int binsize = (dim < int(dim_binsize.size()) ? dim_binsize[dim] : 1);
275 if (val >= 0)
return int(val+(binsize/2))/binsize;
276 return int(val-(binsize/2))/binsize;
278 double Histogram::DimVal(
int dim,
int index) {
279 int binsize = (dim < int(dim_binsize.size()) ? dim_binsize[dim] : 1);
280 return (index * binsize);
282 void Histogram::AddDimension(
int binsize) {
283 dim_binsize.push_back(binsize);
285 void Histogram::Clear() {
288 void Histogram::Inc(
double dim0,
double dim1,
double dim2) {
289 Index index(DimIndex(0,dim0), DimIndex(1,dim1), DimIndex(2,dim2));
290 if (bins.find(index) != bins.end()) {
296 int Histogram::GetMax(
double *dim0,
double *dim1,
double *dim2) {
297 map<Index, int>::const_iterator iter, max_iter;
299 for (max_iter = iter = bins.begin(); iter != bins.end(); iter++) {
300 if (iter->second > max) {
306 *dim0 = DimVal(0, max_iter->first.val[0]);
307 if (dim1) *dim1 = DimVal(1, max_iter->first.val[1]);
308 if (dim2) *dim2 = DimVal(2, max_iter->first.val[2]);
312 void HistogramSubpixel::Clear() {
318 void HistogramSubpixel::Inc(
double dim0,
double dim1,
double dim2) {
319 Index index(DimIndex(0,dim0), DimIndex(1,dim1), DimIndex(2,dim2));
320 if (bins.find(index) != bins.end()) {
322 acc_dim0[index] += dim0;
323 acc_dim1[index] += dim1;
324 acc_dim2[index] += dim2;
327 acc_dim0[index] = dim0;
328 acc_dim1[index] = dim1;
329 acc_dim2[index] = dim2;
332 int HistogramSubpixel::GetMax(
double *dim0,
double *dim1,
double *dim2) {
333 map<Index, int>::const_iterator iter;
336 for (iter = bins.begin(); iter != bins.end(); iter++) {
337 if (iter->second > max) {
338 divider = max = iter->second;
339 *dim0 = acc_dim0[iter->first];
340 if (dim1) *dim1 = acc_dim1[iter->first];
341 if (dim2) *dim2 = acc_dim2[iter->first];
342 }
else if (iter->second == max) {
343 divider += iter->second;
344 *dim0 += acc_dim0[iter->first];
345 if (dim1) *dim1 += acc_dim1[iter->first];
346 if (dim2) *dim2 += acc_dim2[iter->first];
351 if (dim1) *dim1 /= divider;
352 if (dim2) *dim2 /= divider;
363 bool Serialization::Output() {
367 xml->
document.InsertBeforeChild (xml->
document.RootElement(), TiXmlDeclaration(
"1.0",
"UTF-8",
"no"));
371 std::basic_ostream<char> *os =
dynamic_cast<std::basic_ostream<char> *
>(stream);
378 bool Serialization::Input() {
386 node = (TiXmlElement*)xml->
document.LinkEndChild(
new TiXmlElement(
"root"));
388 std::basic_istream<char> *is =
dynamic_cast<std::basic_istream<char> *
>(stream);
395 bool Serialization::Descend(
const char *
id) {
416 bool Serialization::Ascend() {
422 Serialization::Serialization(std::string _filename) {
424 formatter_handle = xml;
429 Serialization::Serialization(std::basic_iostream<char> &_stream)
432 formatter_handle = xml;
436 Serialization::Serialization(std::basic_istream<char> &_stream) {
438 formatter_handle = xml;
442 Serialization::Serialization(std::basic_ostream<char> &_stream) {
444 formatter_handle = xml;
448 Serialization::~Serialization() {
453 bool Serialization::Serialize(
int &data,
const std::string &name) {
457 if (input) ret = (xml->
xml_current->QueryIntAttribute(name, &data) == TIXML_SUCCESS);
462 bool Serialization::Serialize(
unsigned short &data,
const std::string &name) {
464 bool ret = Serialize(i, name);
469 bool Serialization::Serialize(
unsigned long &data,
const std::string &name) {
472 bool ret = Serialize(i, name);
477 bool Serialization::Serialize(
double &data,
const std::string &name) {
480 if (input) ret = (xml->
xml_current->QueryDoubleAttribute(name, &data) == TIXML_SUCCESS);
481 else xml->
xml_current->SetDoubleAttribute(name.c_str(), data);
485 bool Serialization::Serialize(std::string &data,
const std::string &name) {
489 const char *tmp = xml->
xml_current->Attribute(name.c_str());
490 if (tmp == NULL) ret=
false;
493 else xml->
xml_current->SetAttribute(name.c_str(), data.c_str());
497 bool Serialization::Serialize(CvMat &data,
const std::string &name) {
501 TiXmlElement *xml_matrix = (TiXmlElement*)xml->
xml_current->FirstChild(name);
502 if (xml_matrix == NULL)
return false;
503 if (!FileFormatUtils::parseXMLMatrix(xml_matrix, &data))
return false;
506 xml->
xml_current->LinkEndChild(FileFormatUtils::createXMLMatrix(name.c_str(), &data));
TFSIMD_FORCE_INLINE Vector3 cross(const Vector3 &v) const
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.
int ALVAR_EXPORT dot(CvPoint *A, CvPoint *B, CvPoint *C)
Computes dot product AB.BC.
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
void ALVAR_EXPORT out_matrix(const CvMat *m, const char *name)
Output OpenCV matrix for debug purposes.
int find_zero_crossings(const vector< double > &v, vector< int > &corners, int offs)
int ALVAR_EXPORT Sign(const C &v)
Returns the sign of a number.
double ALVAR_EXPORT linePointDist(CvPoint *A, CvPoint *B, CvPoint *C, bool isSegment)
Computes the distance from point C to line (segment) AB.
int exp_filt2(vector< double > &v, vector< double > &ret, bool clamp)
Class for N-dimensional index to be used e.g. with STL maps.
This file implements generic utility functions and a serialization interface.
void FitCVEllipse(const vector< PointDouble > &points, CvBox2D &ellipse_box)
std::vector< int > val
The indices for each dimension are stored in val (last being the most significant) ...
double ALVAR_EXPORT Limit(double val, double min_val, double max_val)
Limits a number to between two values.
double ALVAR_EXPORT angle(CvPoint *A, CvPoint *B, CvPoint *C, CvPoint *D, int isDirectionDependent)
Computes the angle between lines AB and CD.