Line.cpp
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 #include "ar_track_alvar/Line.h"
25 
26 using namespace std;
27 
28 namespace alvar {
29 using namespace std;
30 
31 Line::Line(float params[4])
32 {
33  c.x = params[2];
34  c.y = params[3];
35  s.x = params[0];
36  s.y = params[1];
37 }
38 
39 void FitLines(vector<Line>& lines)
40 {
41 
42 }
43 
44 int FitLines(vector<Line> &lines,
45  const vector<int>& corners,
46  const vector<PointInt >& edge,
47  IplImage *grey /*=0*/) // grey image for future sub pixel accuracy
48 {
49  lines.clear();
50  for(unsigned j = 0; j < corners.size(); ++j)
51  {
52  int start, end, first;
53  int size = (int)edge.size();
54 
55  first = corners[0];
56  start = corners[j];
57 
58  if(j < corners.size()-1)
59  end = corners[j+1];
60  else
61  end = first;
62 
63  int len = 0;
64 
65  if(start < end)
66  len = end-start+1;
67  else
68  len = size-start+end+1;
69 
70  int ind;
71  double* data = new double[2*len];
72 
73  // OpenCV routine...
74  CvMat* line_data = cvCreateMat(1, len, CV_32FC2);
75  for(int i = 0; i < len; ++i)
76  {
77  ind = i + start;
78  if(ind >= size)
79  ind = ind-size;
80 
81  double px = double(edge[ind].x);
82  double py = double(edge[ind].y);
83  CV_MAT_ELEM(*line_data, CvPoint2D32f, 0, i) = cvPoint2D32f(px, py);
84  }
85 
86  float params[4] = {0};
87  cvFitLine(line_data, CV_DIST_L2, 0, 0.01, 0.01, params);
88  lines.push_back(Line(params));
89 
90  delete [] data;
91  cvReleaseMat(&line_data);
92 
93  }
94 
95  return lines.size();
96 }
97 
98 PointDouble Intersection(const Line& l1, const Line& l2)
99 {
100 
101  double vx = l1.s.x;
102  double vy = l1.s.y;
103  double ux = l2.s.x;
104  double uy = l2.s.y;
105  double wx = l2.c.x-l1.c.x;
106  double wy = l2.c.y-l1.c.y;
107 
108  double s, px, py;
109  double tmp = vx*uy-vy*ux;
110  if(tmp==0) tmp = 1;
111 
112  //if(/*tmp <= 1.f && tmp >= -1.f && */tmp != 0.f && ang > 0.1)
113  {
114  s = (vy*wx-vx*wy) / (tmp);
115  px = l2.c.x+s*ux;
116  py = l2.c.y+s*uy;
117  }
118 
119  return PointDouble(px, py);
120 }
121 
122 } // namespace alvar
Main ALVAR namespace.
Definition: Alvar.h:174
PointDouble s
Direction vector.
Definition: Line.h:58
This file implements a parametrized line.
int FitLines(vector< Line > &lines, const vector< int > &corners, const vector< PointInt > &edge, IplImage *grey)
Definition: Line.cpp:44
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
Struct representing a line. The line is parametrized by its center and direction vector.
Definition: Line.h:41
TFSIMD_FORCE_INLINE const tfScalar & x() const
PointDouble ALVAR_EXPORT Intersection(const Line &l1, const Line &l2)
Calculates an intersection point of two lines.
Definition: Line.cpp:98
ALVAR_EXPORT Point< CvPoint2D64f > PointDouble
The default double point type.
Definition: Util.h:108
PointDouble c
Line center.
Definition: Line.h:54


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 19:27:23