Skip to content

Commit a8b6ae1

Browse files
authored
Merge pull request #11 from clockj/shijie
update crossPoint function: not quit due to parallel line but just ou…
2 parents 963d5a2 + 6fec1f2 commit a8b6ae1

File tree

8 files changed

+121
-24
lines changed

8 files changed

+121
-24
lines changed

demo/python/demo_math.ipynb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
"data": {
1010
"text/plain": [
11-
"<pyOpenLPT.PythonStreamRedirector at 0x20e13232670>"
11+
"<pyOpenLPT.PythonStreamRedirector at 0x2f910d01cf0>"
1212
]
1313
},
1414
"execution_count": 1,
@@ -231,6 +231,47 @@
231231
"print(pt3d.norm())"
232232
]
233233
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": 3,
237+
"metadata": {},
238+
"outputs": [
239+
{
240+
"name": "stdout",
241+
"output_type": "stream",
242+
"text": [
243+
"True\n"
244+
]
245+
},
246+
{
247+
"name": "stderr",
248+
"output_type": "stream",
249+
"text": [
250+
"myMATH::crossPoint warning:The two lines are parallel!\n",
251+
"\n",
252+
"Matrix = \n",
253+
"(dim_row, dim_col) = (2,1)\n",
254+
"| 0.000e+00 |\n",
255+
"| 0.000e+00 |\n",
256+
"\n"
257+
]
258+
}
259+
],
260+
"source": [
261+
"pt2d_1 = pyOpenLPT.math.Pt2D(1,2)\n",
262+
"unit_1 = pyOpenLPT.math.Pt2D(1,1)\n",
263+
"line1 = pyOpenLPT.math.Line2D(pt2d_1,unit_1)\n",
264+
"\n",
265+
"pt2d_2 = pyOpenLPT.math.Pt2D(3,4)\n",
266+
"unit_2 = pyOpenLPT.math.Pt2D(1,1)\n",
267+
"line2 = pyOpenLPT.math.Line2D(pt2d_2,unit_2)\n",
268+
"\n",
269+
"pt_cross, is_parallel = pyOpenLPT.math.crossPoint(line1,line2)\n",
270+
"\n",
271+
"pt_cross.print(3)\n",
272+
"print(is_parallel)"
273+
]
274+
},
234275
{
235276
"cell_type": "markdown",
236277
"metadata": {},

inc/libMath/STBCommons.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct PixelRange
5050
{
5151
row_min = row;
5252
}
53-
}; // TODO: we can add a choice to increase search area
53+
};
5454

5555
// Note: before using SetRowRange or SetColRange
5656
// make sure it has been initialized!!!
@@ -64,7 +64,7 @@ struct PixelRange
6464
{
6565
col_min = col;
6666
}
67-
}; // TODO: we can add a choice to increase search area
67+
};
6868

6969
// Note: before using SetRowRange or SetColRange
7070
// make sure it has been initialized!!!

inc/libMath/myMATH.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void triangulation (Pt3D& pt_world, double& error,
151151

152152

153153
// Find cross points of two 2d lines
154-
Pt2D crossPoint (Line2D const& line1, Line2D const& line2);
154+
bool crossPoint (Pt2D& pt2d, Line2D const& line1, Line2D const& line2);
155155

156156

157157
// Create identity matrix

inc/libSTB/StereoMatch.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ class StereoMatch
154154
std::vector<std::vector<Tracer2D>> const& tr2d_list
155155
);
156156

157-
PixelRange findSearchRegion (int id, std::vector<Line2D> const& sight2D_list);
157+
// find search region for tracer
158+
// output: PixelRange, is_correct
159+
// if is_correct = true, the search region is valid (no parallel lines, able to find cross points)
160+
std::pair<PixelRange, bool> findSearchRegion (int id, std::vector<Line2D> const& sight2D_list);
158161

159162
void iterOnObjIDMap (
160163
int id,

src/pybind_OpenLPT/pymyMath.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ void init_myMath(py::module& m)
8282
return std::make_pair(pt_world_list, error_list);
8383
}, "Triangulate 3D points from all lists of 3D lines of sight");
8484

85-
m.def("crossPoint", &myMATH::crossPoint, "Find the cross point of two 2D lines");
85+
// m.def("crossPoint", &myMATH::crossPoint, "Find the cross point of two 2D lines");
86+
m.def("crossPoint", [](Line2D const& line1, Line2D const& line2){
87+
Pt2D pt2d;
88+
bool is_parallel = myMATH::crossPoint(pt2d, line1, line2);
89+
return std::make_pair(pt2d, is_parallel);
90+
}, "Find the cross point of two 2D lines");
8691

8792
m.def("eye", &myMATH::eye<double>, "Create an identity Matrix<double>");
8893

src/srcMath/myMATH.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,36 @@ void triangulation(Pt3D& pt_world, double& error,
299299
}
300300

301301
// Find the cross points of two 2d lines
302-
Pt2D crossPoint (Line2D const& line1, Line2D const& line2)
302+
bool crossPoint (Pt2D& pt2d, Line2D const& line1, Line2D const& line2)
303303
{
304304
double den = line1.unit_vector[0] * line2.unit_vector[1] - line1.unit_vector[1] * line2.unit_vector[0];
305-
if (std::fabs(den) < SMALLNUMBER)
305+
306+
// if (std::fabs(den) < SMALLNUMBER)
307+
// {
308+
// std::cerr << "myMATH::crossPoint: "
309+
// << "The two lines are parallel!"
310+
// << std::endl;
311+
// throw error_range;
312+
// }
313+
bool is_parallel = false;
314+
if (std::fabs(den) < 1e-10)
306315
{
307-
std::cerr << "myMATH::crossPoint: "
316+
std::cout << "myMATH::crossPoint warning:"
308317
<< "The two lines are parallel!"
309318
<< std::endl;
310-
throw error_range;
319+
is_parallel = true;
320+
pt2d[0] = 0;
321+
pt2d[1] = 0;
322+
return is_parallel;
311323
}
312324

313325
double num = line2.unit_vector[1] * (line2.pt[0] - line1.pt[0]) - line2.unit_vector[0] * (line2.pt[1] - line1.pt[1]);
326+
double factor = num / den;
314327

315-
Pt2D res(line1.unit_vector);
316-
res *= num / den;
317-
res += line1.pt;
328+
pt2d[0] = (line1.unit_vector[0]) * factor + line1.pt[0];
329+
pt2d[1] = (line1.unit_vector[1]) * factor + line1.pt[1];
318330

319-
return res;
331+
return is_parallel;
320332
}
321333

322334
// Polynomial fit

src/srcSTB/StereoMatch.hpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ void StereoMatch::findTracerMatch (
724724
Line3D sight3D;
725725
Pt2D pt2d_1;
726726
Pt2D pt2d_2;
727+
bool is_parallel;
727728
Pt2D unit2d;
728729
for (int i = 0; i < id; i ++)
729730
{
@@ -787,8 +788,18 @@ void StereoMatch::findTracerMatch (
787788
sight2D_axis.pt[1] = y_pixel;
788789

789790
// Get x_pixel (col id) from two lines
790-
pt2d_1 = myMATH::crossPoint(sight2D_axis, sight2D_plus);
791-
pt2d_2 = myMATH::crossPoint(sight2D_axis, sight2D_minus);
791+
// quit if the two lines are parallel
792+
is_parallel = myMATH::crossPoint(pt2d_1, sight2D_axis, sight2D_plus);
793+
if (is_parallel)
794+
{
795+
return;
796+
}
797+
is_parallel = myMATH::crossPoint(pt2d_2, sight2D_axis, sight2D_minus);
798+
if (is_parallel)
799+
{
800+
return;
801+
}
802+
792803
x_pixel_1 = pt2d_1[0];
793804
x_pixel_2 = pt2d_2[0];
794805
if (x_pixel_1 > x_pixel_2)
@@ -834,8 +845,18 @@ void StereoMatch::findTracerMatch (
834845
sight2D_axis.pt[1] = 0;
835846

836847
// Get y_pixel (row id) from two lines
837-
pt2d_1 = myMATH::crossPoint(sight2D_axis, sight2D_plus);
838-
pt2d_2 = myMATH::crossPoint(sight2D_axis, sight2D_minus);
848+
// quit if the two lines are parallel
849+
is_parallel = myMATH::crossPoint(pt2d_1, sight2D_axis, sight2D_plus);
850+
if (is_parallel)
851+
{
852+
return;
853+
}
854+
is_parallel = myMATH::crossPoint(pt2d_2, sight2D_axis, sight2D_minus);
855+
if (is_parallel)
856+
{
857+
return;
858+
}
859+
839860
y_pixel_1 = pt2d_1[1];
840861
y_pixel_2 = pt2d_2[1];
841862
if (y_pixel_1 > y_pixel_2)
@@ -876,9 +897,14 @@ void StereoMatch::findTracerMatch (
876897
else
877898
{
878899
// find search region
879-
PixelRange search_region = findSearchRegion(id, sight2D_list);
900+
std::pair<PixelRange, bool> search_output = findSearchRegion(id, sight2D_list);
901+
if (!search_output.second)
902+
{
903+
return;
904+
}
880905

881906
// iterate every pixel in the search region
907+
PixelRange search_region = search_output.first;
882908
for (int i = search_region.row_min; i < search_region.row_max; i ++)
883909
{
884910
for (int j = search_region.col_min; j < search_region.col_max; j ++)
@@ -1078,7 +1104,10 @@ void StereoMatch::iterOnObjIDMap (
10781104
// }
10791105

10801106

1081-
PixelRange StereoMatch::findSearchRegion (int id, std::vector<Line2D> const& sight2D_list)
1107+
// output: search_region, is_correct
1108+
// is_correct: true => do not have parallel lines
1109+
// false => have parallel lines
1110+
std::pair<PixelRange, bool> StereoMatch::findSearchRegion (int id, std::vector<Line2D> const& sight2D_list)
10821111
{
10831112
PixelRange search_region;
10841113

@@ -1089,11 +1118,12 @@ PixelRange StereoMatch::findSearchRegion (int id, std::vector<Line2D> const& sig
10891118
{
10901119
search_region.row_max = n_row;
10911120
search_region.col_max = n_col;
1092-
return search_region;
1121+
return std::pair(search_region, true);
10931122
}
10941123

10951124
// Find all crossing points
10961125
Pt2D pt_cross;
1126+
bool is_parallel;
10971127
Pt2D pt_temp;
10981128
Pt2D perp_unit_curr;
10991129
Pt2D perp_unit_test;
@@ -1108,7 +1138,11 @@ PixelRange StereoMatch::findSearchRegion (int id, std::vector<Line2D> const& sig
11081138
for (int j = i+1; j < id; j ++)
11091139
{
11101140
// find the crossing point
1111-
pt_cross = myMATH::crossPoint(sight2D_list[i], sight2D_list[j]);
1141+
is_parallel = myMATH::crossPoint(pt_cross, sight2D_list[i], sight2D_list[j]);
1142+
if (is_parallel)
1143+
{
1144+
return std::pair(search_region, false);
1145+
}
11121146

11131147
// cos
11141148
perp_unit_test[0] = sight2D_list[j].unit_vector[1];
@@ -1155,7 +1189,7 @@ PixelRange StereoMatch::findSearchRegion (int id, std::vector<Line2D> const& sig
11551189
search_region.col_min = std::max(search_region.col_min, 0);
11561190
search_region.col_max = std::min(search_region.col_max, n_col);
11571191

1158-
return search_region;
1192+
return std::pair(search_region, true);
11591193
}
11601194

11611195

test/test_myMATH.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ bool test_function_16 ()
530530
line2.pt = pt3;
531531
line2.unit_vector = myMATH::createUnitVector (pt3, pt4);
532532

533-
Pt2D cross_point = myMATH::crossPoint (line1, line2);
533+
Pt2D cross_point;
534+
bool is_parallel = myMATH::crossPoint (cross_point, line1, line2);
535+
std::cout << "is_parallel: " << is_parallel << std::endl;
534536

535537
if (cross_point != Pt2D(1,2))
536538
{

0 commit comments

Comments
 (0)