test_intersection.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <gtest/gtest.h>
31 
33 
34 TEST(IntersectionTests, Intersects)
35 {
36  cv::Vec2d c;
38  cv::Vec2d(0, 5),
39  cv::Vec2d(10, 5),
40  cv::Vec2d(5, 0),
41  cv::Vec2d(5, 10),
42  c));
43 
44  EXPECT_FLOAT_EQ(5.0, c[0]);
45  EXPECT_FLOAT_EQ(5.0, c[1]);
46 
48  cv::Vec2d(0, 6),
49  cv::Vec2d(10, 6),
50  cv::Vec2d(5, 0),
51  cv::Vec2d(5, 10),
52  c));
53 
54  EXPECT_FLOAT_EQ(5.0, c[0]);
55  EXPECT_FLOAT_EQ(6.0, c[1]);
56 
58  cv::Vec2d(10, 6),
59  cv::Vec2d(0, 6),
60  cv::Vec2d(5, 10),
61  cv::Vec2d(5, 0),
62  c));
63 
64  EXPECT_FLOAT_EQ(5.0, c[0]);
65  EXPECT_FLOAT_EQ(6.0, c[1]);
66 
68  cv::Vec2d(5, 10),
69  cv::Vec2d(5, 0),
70  cv::Vec2d(10, 6),
71  cv::Vec2d(0, 6),
72  c));
73 
74  EXPECT_FLOAT_EQ(5.0, c[0]);
75  EXPECT_FLOAT_EQ(6.0, c[1]);
76 
78  cv::Vec2d(10, 10),
79  cv::Vec2d(0, 0),
80  cv::Vec2d(0, 10),
81  cv::Vec2d(10, 0),
82  c));
83 
84  EXPECT_FLOAT_EQ(5.0, c[0]);
85  EXPECT_FLOAT_EQ(5.0, c[1]);
86 
88  cv::Vec2d(-10, -10),
89  cv::Vec2d(0, 0),
90  cv::Vec2d(0, -10),
91  cv::Vec2d(-10, 0),
92  c));
93 
94  EXPECT_FLOAT_EQ(-5.0, c[0]);
95  EXPECT_FLOAT_EQ(-5.0, c[1]);
96 
98  cv::Vec2d(0, 5),
99  cv::Vec2d(-10, 5),
100  cv::Vec2d(5, 0),
101  cv::Vec2d(5, -10),
102  c));
103 
104  EXPECT_FLOAT_EQ(5.0, c[0]);
105  EXPECT_FLOAT_EQ(5.0, c[1]);
106 }
107 
108 TEST(IntersectionTests, Parallel)
109 {
110  cv::Vec2d c;
112  cv::Vec2d(0, 0),
113  cv::Vec2d(0, 0),
114  cv::Vec2d(0, 0),
115  cv::Vec2d(0, 0),
116  c));
117 
119  cv::Vec2d(0, 0),
120  cv::Vec2d(10, 0),
121  cv::Vec2d(0, 0),
122  cv::Vec2d(10, 0),
123  c));
124 
126  cv::Vec2d(0, 0),
127  cv::Vec2d(10, 0),
128  cv::Vec2d(25, 0),
129  cv::Vec2d(15, 0),
130  c));
131 
133  cv::Vec2d(0, 0),
134  cv::Vec2d(10, 0),
135  cv::Vec2d(0, -1),
136  cv::Vec2d(10, -1),
137  c));
138 }
139 
140 TEST(IntersectionTests, SegmentsIntersect)
141 {
142  cv::Vec2d c;
144  cv::Vec2d(0, 5),
145  cv::Vec2d(10, 5),
146  cv::Vec2d(5, 0),
147  cv::Vec2d(5, 10),
148  c));
149 
150  EXPECT_FLOAT_EQ(5.0, c[0]);
151  EXPECT_FLOAT_EQ(5.0, c[1]);
152 
154  cv::Vec2d(0, 6),
155  cv::Vec2d(10, 6),
156  cv::Vec2d(5, 0),
157  cv::Vec2d(5, 10),
158  c));
159 
160  EXPECT_FLOAT_EQ(5.0, c[0]);
161  EXPECT_FLOAT_EQ(6.0, c[1]);
162 
164  cv::Vec2d(10, 6),
165  cv::Vec2d(0, 6),
166  cv::Vec2d(5, 10),
167  cv::Vec2d(5, 0),
168  c));
169 
170  EXPECT_FLOAT_EQ(5.0, c[0]);
171  EXPECT_FLOAT_EQ(6.0, c[1]);
172 
174  cv::Vec2d(5, 10),
175  cv::Vec2d(5, 0),
176  cv::Vec2d(10, 6),
177  cv::Vec2d(0, 6),
178  c));
179 
180  EXPECT_FLOAT_EQ(5.0, c[0]);
181  EXPECT_FLOAT_EQ(6.0, c[1]);
182 
184  cv::Vec2d(10, 10),
185  cv::Vec2d(0, 0),
186  cv::Vec2d(0, 10),
187  cv::Vec2d(10, 0),
188  c));
189 
190  EXPECT_FLOAT_EQ(5.0, c[0]);
191  EXPECT_FLOAT_EQ(5.0, c[1]);
192 
194  cv::Vec2d(-10, -10),
195  cv::Vec2d(0, 0),
196  cv::Vec2d(0, -10),
197  cv::Vec2d(-10, 0),
198  c));
199 
200  EXPECT_FLOAT_EQ(-5.0, c[0]);
201  EXPECT_FLOAT_EQ(-5.0, c[1]);
202 
203 
205  cv::Vec2d(0, 2),
206  cv::Vec2d(2, 0),
207  cv::Vec2d(0, 0),
208  cv::Vec2d(2, 2),
209  c));
210 
211  EXPECT_FLOAT_EQ(1.0, c[0]);
212  EXPECT_FLOAT_EQ(1.0, c[1]);
213 
215  cv::Vec2d(0, 2),
216  cv::Vec2d(2, 0),
217  cv::Vec2d(0, 0),
218  cv::Vec2d(2, 0),
219  c));
220 
221  EXPECT_FLOAT_EQ(2.0, c[0]);
222  EXPECT_FLOAT_EQ(0.0, c[1]);
223 
225  cv::Vec2d(3, 0),
226  cv::Vec2d(0, 0),
227  cv::Vec2d(0, 2),
228  cv::Vec2d(2, 0),
229  c));
230  EXPECT_FLOAT_EQ(2.0, c[0]);
231  EXPECT_FLOAT_EQ(0.0, c[1]);
232 
234  cv::Vec2d(0, 2),
235  cv::Vec2d(2, 0),
236  cv::Vec2d(0, 0),
237  cv::Vec2d(3, 0),
238  c));
239  EXPECT_FLOAT_EQ(2.0, c[0]);
240  EXPECT_FLOAT_EQ(0.0, c[1]);
241 
243  cv::Vec2d(0, 2),
244  cv::Vec2d(2, 0),
245  cv::Vec2d(3, 0),
246  cv::Vec2d(0, 0),
247  c));
248  EXPECT_FLOAT_EQ(2.0, c[0]);
249  EXPECT_FLOAT_EQ(0.0, c[1]);
250 
252  cv::Vec2d(0, 2),
253  cv::Vec2d(2, 0),
254  cv::Vec2d(2, 0),
255  cv::Vec2d(0, 0),
256  c));
257  EXPECT_FLOAT_EQ(2.0, c[0]);
258  EXPECT_FLOAT_EQ(0.0, c[1]);
259 
261  cv::Vec2d(0, 2),
262  cv::Vec2d(2, 0),
263  cv::Vec2d(0, 2),
264  cv::Vec2d(0, 0),
265  c));
266 
267  EXPECT_FLOAT_EQ(0.0, c[0]);
268  EXPECT_FLOAT_EQ(2.0, c[1]);
269 
271  cv::Vec2d(0, 2),
272  cv::Vec2d(2, 0),
273  cv::Vec2d(1, 1),
274  cv::Vec2d(0, 0),
275  c));
276 
277  EXPECT_FLOAT_EQ(1.0, c[0]);
278  EXPECT_FLOAT_EQ(1.0, c[1]);
279 
281  cv::Vec2d(200, 0),
282  cv::Vec2d(0, 0),
283  cv::Vec2d(33.3, 0),
284  cv::Vec2d(10, 10),
285  c));
286  EXPECT_FLOAT_EQ(33.3, c[0]);
287  EXPECT_FLOAT_EQ(0.0, c[1]);
288 
290  cv::Vec2d(0, 0),
291  cv::Vec2d(0, 0),
292  cv::Vec2d(0, 0),
293  cv::Vec2d(0, 0),
294  c));
295  EXPECT_FLOAT_EQ(0.0, c[0]);
296  EXPECT_FLOAT_EQ(0.0, c[1]);
297 
299  cv::Vec2d(0, 2),
300  cv::Vec2d(2, 0),
301  cv::Vec2d(1, 1),
302  cv::Vec2d(1, 1),
303  c));
304  EXPECT_FLOAT_EQ(1.0, c[0]);
305  EXPECT_FLOAT_EQ(1.0, c[1]);
306 
308  cv::Vec2d(1, 1),
309  cv::Vec2d(1, 1),
310  cv::Vec2d(0, 2),
311  cv::Vec2d(2, 0),
312  c));
313  EXPECT_FLOAT_EQ(1.0, c[0]);
314  EXPECT_FLOAT_EQ(1.0, c[1]);
315 
317  cv::Vec2d(1065.8687582537791058712173253,-1053.2999883795632740657310933),
318  cv::Vec2d(1065.5686875431010776082985103,-1051.8000590902413478033849970),
319  cv::Vec2d(1066.1686875431009866588283330,-1053.0000590902413932781200856),
320  cv::Vec2d(1065.5686875431010776082985103,-1051.8000590902413478033849970),
321  c));
322  EXPECT_FLOAT_EQ(1065.5686875431010776082985103, c[0]);
323  EXPECT_FLOAT_EQ(-1051.8000590902413478033849970, c[1]);
324 }
325 
326 TEST(IntersectionTests, SegmentsDontIntersect)
327 {
328  cv::Vec2d c;
330  cv::Vec2d(0, 5),
331  cv::Vec2d(-10, 5),
332  cv::Vec2d(5, 0),
333  cv::Vec2d(5, -10),
334  c));
335 
337  cv::Vec2d(0, 2),
338  cv::Vec2d(2, 0),
339  cv::Vec2d(2, 1),
340  cv::Vec2d(5, 2),
341  c));
342 
344  cv::Vec2d(0, 2),
345  cv::Vec2d(2, 0),
346  cv::Vec2d(0, 0),
347  cv::Vec2d(-5, -5),
348  c));
349 
351  cv::Vec2d(0, 2),
352  cv::Vec2d(2, 0),
353  cv::Vec2d(0.999, 0.999),
354  cv::Vec2d(0, 0),
355  c));
356 
358  cv::Vec2d(200, 0),
359  cv::Vec2d(0, 0),
360  cv::Vec2d(33.3, 0.0000001),
361  cv::Vec2d(10, 10),
362  c));
363 
365  cv::Vec2d(1.00001, 1.00001),
366  cv::Vec2d(1.00001, 1.00001),
367  cv::Vec2d(0, 2),
368  cv::Vec2d(2, 0),
369  c));
370 
372  cv::Vec2d(0, 2),
373  cv::Vec2d(2, 0),
374  cv::Vec2d(1.00001, 1.00001),
375  cv::Vec2d(1.00001, 1.00001),
376  c));
377 }
378 
379 TEST(IntersectionTests, ParallelSegments)
380 {
381  cv::Vec2d c;
383  cv::Vec2d(0, 0),
384  cv::Vec2d(10, 0),
385  cv::Vec2d(0, 0),
386  cv::Vec2d(10, 0),
387  c));
388  EXPECT_FLOAT_EQ(0.0, c[0]);
389  EXPECT_FLOAT_EQ(0.0, c[1]);
390 
392  cv::Vec2d(0, 0),
393  cv::Vec2d(10, 0),
394  cv::Vec2d(10, 0),
395  cv::Vec2d(0, 0),
396  c));
397  EXPECT_FLOAT_EQ(0.0, c[0]);
398  EXPECT_FLOAT_EQ(0.0, c[1]);
399 
401  cv::Vec2d(10, 0),
402  cv::Vec2d(0, 0),
403  cv::Vec2d(10, 0),
404  cv::Vec2d(0, 0),
405  c));
406  EXPECT_FLOAT_EQ(10.0, c[0]);
407  EXPECT_FLOAT_EQ(0.0, c[1]);
408 
410  cv::Vec2d(10, 0),
411  cv::Vec2d(0, 0),
412  cv::Vec2d(0, 0),
413  cv::Vec2d(10, 0),
414  c));
415  EXPECT_FLOAT_EQ(10.0, c[0]);
416  EXPECT_FLOAT_EQ(0.0, c[1]);
417 
419  cv::Vec2d(10, 0),
420  cv::Vec2d(0, 0),
421  cv::Vec2d(10, 0),
422  cv::Vec2d(20, 0),
423  c));
424  EXPECT_FLOAT_EQ(10.0, c[0]);
425  EXPECT_FLOAT_EQ(0.0, c[1]);
426 
428  cv::Vec2d(10, 0),
429  cv::Vec2d(0, 0),
430  cv::Vec2d(5, 0),
431  cv::Vec2d(10, 0),
432  c));
433  EXPECT_FLOAT_EQ(10.0, c[0]);
434  EXPECT_FLOAT_EQ(0.0, c[1]);
435 
437  cv::Vec2d(10, 0),
438  cv::Vec2d(0, 0),
439  cv::Vec2d(10, 0),
440  cv::Vec2d(5, 0),
441  c));
442  EXPECT_FLOAT_EQ(10.0, c[0]);
443  EXPECT_FLOAT_EQ(0.0, c[1]);
444 
446  cv::Vec2d(0, 0),
447  cv::Vec2d(10, 0),
448  cv::Vec2d(5, 0),
449  cv::Vec2d(10, 0),
450  c));
451  EXPECT_FLOAT_EQ(5.0, c[0]);
452  EXPECT_FLOAT_EQ(0.0, c[1]);
453 
455  cv::Vec2d(0, 0),
456  cv::Vec2d(10, 0),
457  cv::Vec2d(10, 0),
458  cv::Vec2d(5, 0),
459  c));
460  EXPECT_FLOAT_EQ(5.0, c[0]);
461  EXPECT_FLOAT_EQ(0.0, c[1]);
462 
464  cv::Vec2d(0, 0),
465  cv::Vec2d(10, 0),
466  cv::Vec2d(5, 0),
467  cv::Vec2d(6, 0),
468  c));
469  EXPECT_FLOAT_EQ(5.0, c[0]);
470  EXPECT_FLOAT_EQ(0.0, c[1]);
471 
473  cv::Vec2d(0, 0),
474  cv::Vec2d(10, 0),
475  cv::Vec2d(6, 0),
476  cv::Vec2d(5, 0),
477  c));
478  EXPECT_FLOAT_EQ(5.0, c[0]);
479  EXPECT_FLOAT_EQ(0.0, c[1]);
480 
482  cv::Vec2d(10, 0),
483  cv::Vec2d(0, 0),
484  cv::Vec2d(5, 0),
485  cv::Vec2d(6, 0),
486  c));
487  EXPECT_FLOAT_EQ(6.0, c[0]);
488  EXPECT_FLOAT_EQ(0.0, c[1]);
489 
491  cv::Vec2d(10, 0),
492  cv::Vec2d(0, 0),
493  cv::Vec2d(6, 0),
494  cv::Vec2d(5, 0),
495  c));
496  EXPECT_FLOAT_EQ(6.0, c[0]);
497  EXPECT_FLOAT_EQ(0.0, c[1]);
498 
500  cv::Vec2d(0, 0),
501  cv::Vec2d(10, 0),
502  cv::Vec2d(0, 1),
503  cv::Vec2d(10, 1),
504  c));
505 
507  cv::Vec2d(0, 0),
508  cv::Vec2d(10, 0),
509  cv::Vec2d(0, .00001),
510  cv::Vec2d(10, .00001),
511  c));
512 
514  cv::Vec2d(0, 0),
515  cv::Vec2d(10, 0),
516  cv::Vec2d(11, 0),
517  cv::Vec2d(20, 0),
518  c));
519 
521  cv::Vec2d(0, 0),
522  cv::Vec2d(10, 0),
523  cv::Vec2d(10.00001, 0),
524  cv::Vec2d(20, 0),
525  c));
526 }
527 
528 // Run all the tests that were declared with TEST()
529 int main(int argc, char **argv)
530 {
531  testing::InitGoogleTest(&argc, argv);
532 
533  return RUN_ALL_TESTS();
534 }
bool LineSegmentIntersection(const cv::Vec2d &p1, const cv::Vec2d &p2, const cv::Vec2d &p3, const cv::Vec2d &p4, cv::Vec2d &c)
TEST(IntersectionTests, Intersects)
int main(int argc, char **argv)
bool LineIntersection(const cv::Vec2d &p1, const cv::Vec2d &p2, const cv::Vec2d &p3, const cv::Vec2d &p4, cv::Vec2d &c)


swri_geometry_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:05:39