-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.cpp
115 lines (94 loc) · 2.95 KB
/
Line.cpp
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
/*
* This file is part of ALVAR, A Library for Virtual and Augmented Reality.
*
* Copyright 2007-2012 VTT Technical Research Centre of Finland
*
* Contact: VTT Augmented Reality Team <[email protected]>
* <http://www.vtt.fi/multimedia/alvar.html>
*
* ALVAR is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ALVAR; if not, see
* <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
*/
#include "Line.h"
using namespace std;
namespace alvar {
using namespace std;
/*****************************************************************************
*** class Line
****************************************************************************/
PointDouble Line::Intersection(const Line& l1, const Line& l2)
{
double vx = l1.s.x;
double vy = l1.s.y;
double ux = l2.s.x;
double uy = l2.s.y;
double wx = l2.c.x - l1.c.x;
double wy = l2.c.y - l1.c.y;
double s =0.0;
double px = 0.0;
double py = 0.0;
double tmp = vx * uy - vy * ux;
if (tmp == 0.0)
tmp = 1.0;
//if(/*tmp <= 1.f && tmp >= -1.f && */tmp != 0.f && ang > 0.1)
{
s = (vy * wx - vx * wy) / tmp;
px = l2.c.x + s * ux;
py = l2.c.y + s * uy;
}
return (PointDouble(px, py));
}
int Line::FitLines(std::vector<Line> &lines,
const vector<int>& corners,
const vector<PointInt>& edge,
cv::Mat& /* grey */) // grey image for future sub pixel accuracy
{
lines.clear();
for (size_t j = 0 ; j < corners.size() ; ++j)
{
int start, end;
int size = (int)edge.size();
start = corners[j];
if (j < (corners.size() - 1))
{
end = corners[j + 1];
} // end if
else
{
end = corners[0];
} // end else
int len = end - start + 1;
if (start >= end)
{
len += size;
} // end if
// OpenCV routine...
// Change to std::vector. TTC!!
cv::Mat line_data(1, len, CV_32FC2);
for (int i = 0 ; i < len ; ++i)
{
int ind = i + start;
if (ind >= size)
ind = ind - size;
double px = double(edge[ind].x);
double py = double(edge[ind].y);
line_data.at<cv::Point2f>(0, i) = cv::Point2f(px, py);
}
cv::Vec4f params;
cv::fitLine(line_data, params, CV_DIST_L2, 0.0, 0.01, 0.01);
lines.push_back(Line(params));
}
return (lines.size());
}
} // namespace alvar