PageRenderTime 58ms CodeModel.GetById 18ms app.highlight 35ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/strategies/segment_intersection_collinear.cpp

http://hadesmem.googlecode.com/
C++ | 252 lines | 161 code | 43 blank | 48 comment | 5 complexity | f74ee11350f373f4b8ce3d05b34d6ea1 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  6// Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  7
  8// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
 10
 11// Use, modification and distribution is subject to the Boost Software License,
 12// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
 13// http://www.boost.org/LICENSE_1_0.txt)
 14
 15
 16#include <geometry_test_common.hpp>
 17
 18#include <boost/geometry/algorithms/assign.hpp>
 19
 20#include <boost/geometry/strategies/cartesian/cart_intersect.hpp>
 21#include <boost/geometry/strategies/intersection_result.hpp>
 22
 23#include <boost/geometry/policies/relate/intersection_points.hpp>
 24#include <boost/geometry/policies/relate/direction.hpp>
 25#include <boost/geometry/policies/relate/de9im.hpp>
 26#include <boost/geometry/policies/relate/tupled.hpp>
 27
 28#include <boost/geometry/algorithms/intersection.hpp>
 29
 30
 31#include <boost/geometry/geometries/point.hpp>
 32#include <boost/geometry/geometries/segment.hpp>
 33
 34template <typename IntersectionPoints>
 35static int check(IntersectionPoints const& is,
 36                std::size_t index, double expected_x, double expected_y)
 37{
 38    if (expected_x != -99 && expected_y != -99 && is.count > index)
 39    {
 40        double x = bg::get<0>(is.intersections[index]);
 41        double y = bg::get<1>(is.intersections[index]);
 42
 43        BOOST_CHECK_CLOSE(x, expected_x, 0.001);
 44        BOOST_CHECK_CLOSE(y, expected_y, 0.001);
 45        return 1;
 46    }
 47    return 0;
 48}
 49
 50
 51template <typename P>
 52static void test_segment_intersection(std::string const& case_id,
 53                int x1, int y1, int x2, int y2,
 54                int x3, int y3, int x4, int y4,
 55                char expected_how, bool expected_opposite,
 56                int expected_arrival1, int expected_arrival2,
 57                int expected_x1, int expected_y1,
 58                int expected_x2 = -99, int expected_y2 = -99)
 59
 60{
 61
 62//#ifdef REPORT
 63    std::cout << "Case: " << case_id << std::endl;
 64//#endif
 65
 66    typedef typename bg::coordinate_type<P>::type coordinate_type;
 67    typedef bg::model::referring_segment<const P> segment_type;
 68
 69    P p1, p2, p3, p4;
 70    bg::assign_values(p1, x1, y1);
 71    bg::assign_values(p2, x2, y2);
 72    bg::assign_values(p3, x3, y3);
 73    bg::assign_values(p4, x4, y4);
 74
 75    segment_type s12(p1,p2);
 76    segment_type s34(p3,p4);
 77
 78    // Get the intersection point (or two points)
 79    bg::segment_intersection_points<P> is
 80        = bg::strategy::intersection::relate_cartesian_segments
 81        <
 82            bg::policies::relate::segments_intersection_points
 83                <
 84                    segment_type,
 85                    segment_type,
 86                    bg::segment_intersection_points<P>
 87                >
 88        >::apply(s12, s34);
 89
 90    // Get just a character for Left/Right/intersects/etc, purpose is more for debugging
 91    bg::policies::relate::direction_type dir
 92        = bg::strategy::intersection::relate_cartesian_segments
 93        <
 94            bg::policies::relate::segments_direction
 95                <
 96                    segment_type,
 97                    segment_type
 98                >
 99        >::apply(s12, s34);
100
101    int expected_count =
102        check(is, 0, expected_x1, expected_y1)
103        + check(is, 1, expected_x2, expected_y2);
104
105    BOOST_CHECK_EQUAL(is.count, expected_count);
106    BOOST_CHECK_EQUAL(dir.how, expected_how);
107    BOOST_CHECK_EQUAL(dir.opposite, expected_opposite);
108    BOOST_CHECK_EQUAL(dir.arrival[0], expected_arrival1);
109    BOOST_CHECK_EQUAL(dir.arrival[1], expected_arrival2);
110}
111
112
113template <typename P>
114void test_all()
115{
116    // Collinear - non opposite
117
118    //       a1---------->a2
119    // b1--->b2
120    test_segment_intersection<P>("n1",
121        2, 0, 6, 0,
122        0, 0, 2, 0,
123        'a', false, -1, 0,
124        2, 0);
125
126    //       a1---------->a2
127    //    b1--->b2
128    test_segment_intersection<P>("n2",
129        2, 0, 6, 0,
130        1, 0, 3, 0,
131        'c', false, -1, 1,
132        2, 0, 3, 0);
133
134    //       a1---------->a2
135    //       b1--->b2
136    test_segment_intersection<P>("n3",
137        2, 0, 6, 0,
138        2, 0, 4, 0,
139        'c', false, -1, 1,
140        2, 0, 4, 0);
141
142    //       a1---------->a2
143    //          b1--->b2
144    test_segment_intersection<P>("n4",
145        2, 0, 6, 0,
146        3, 0, 5, 0,
147        'c', false, -1, 1,
148        3, 0, 5, 0);
149
150    //       a1---------->a2
151    //              b1--->b2
152    test_segment_intersection<P>("n5",
153        2, 0, 6, 0,
154        4, 0, 6, 0,
155        'c', false, 0, 0,
156        4, 0, 6, 0);
157
158    //       a1---------->a2
159    //                 b1--->b2
160    test_segment_intersection<P>("n6",
161        2, 0, 6, 0,
162        5, 0, 7, 0,
163        'c', false, 1, -1,
164        5, 0, 6, 0);
165
166    //       a1---------->a2
167    //                    b1--->b2
168    test_segment_intersection<P>("n7",
169        2, 0, 6, 0,
170        6, 0, 8, 0,
171        'a', false, 0, -1,
172        6, 0);
173
174    // Collinear - opposite
175    //       a1---------->a2
176    // b2<---b1
177    test_segment_intersection<P>("o1",
178        2, 0, 6, 0,
179        2, 0, 0, 0,
180        'f', true, -1, -1,
181        2, 0);
182
183    //       a1---------->a2
184    //    b2<---b1
185    test_segment_intersection<P>("o2",
186        2, 0, 6, 0,
187        3, 0, 1, 0,
188        'c', true, -1, -1,
189        2, 0, 3, 0);
190
191    //       a1---------->a2
192    //       b2<---b1
193    test_segment_intersection<P>("o3",
194        2, 0, 6, 0,
195        4, 0, 2, 0,
196        'c', true, -1, 0,
197        2, 0, 4, 0);
198
199    //       a1---------->a2
200    //           b2<---b1
201    test_segment_intersection<P>("o4",
202        2, 0, 6, 0,
203        5, 0, 3, 0,
204        'c', true, -1, 1,
205        3, 0, 5, 0);
206
207    //       a1---------->a2
208    //              b2<---b1
209    test_segment_intersection<P>("o5",
210        2, 0, 6, 0,
211        6, 0, 4, 0,
212        'c', true, 0, 1,
213        4, 0, 6, 0);
214
215    //       a1---------->a2
216    //                b2<---b1
217    test_segment_intersection<P>("o6",
218        2, 0, 6, 0,
219        7, 0, 5, 0,
220        'c', true, 1, 1,
221        5, 0, 6, 0);
222
223    //       a1---------->a2
224    //                    b2<---b1
225    test_segment_intersection<P>("o7",
226        2, 0, 6, 0,
227        8, 0, 6, 0,
228        't', true, 0, 0,
229        6, 0);
230
231    //   a1---------->a2
232    //   b1---------->b2
233    test_segment_intersection<P>("e1",
234        2, 0, 6, 0,
235        2, 0, 6, 0,
236        'e', false, 0, 0,
237        2, 0, 6, 0);
238
239    //   a1---------->a2
240    //   b2<----------b1
241    test_segment_intersection<P>("e1",
242        2, 0, 6, 0,
243        6, 0, 2, 0,
244        'e', true, 0, 0,
245        2, 0, 6, 0);
246}
247
248int test_main(int, char* [])
249{
250    test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
251    return 0;
252}