// Boost.Polygon library polygon_segment_test.cpp file // Copyright Andrii Sydorchuk 2012. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org for updates, documentation, and revision history. #include #include #include #include using namespace boost::polygon; void segment_data_test() { typedef point_data point_type; typedef segment_data segment_type; point_type point1(1, 2); point_type point2(3, 4); segment_type segment1(point1, point2); segment_type segment2; segment2 = segment1; BOOST_TEST(segment1.low() == point1); BOOST_TEST(segment1.high() == point2); BOOST_TEST(segment1.get(LOW) == point1); BOOST_TEST(segment1.get(HIGH) == point2); BOOST_TEST(segment1 == segment2); BOOST_TEST(!(segment1 != segment2)); BOOST_TEST(!(segment1 < segment2)); BOOST_TEST(!(segment1 > segment1)); BOOST_TEST(segment1 <= segment2); BOOST_TEST(segment1 >= segment2); segment1.low(point2); segment1.high(point1); BOOST_TEST(segment1.low() == point2); BOOST_TEST(segment1.high() == point1); BOOST_TEST(!(segment1 == segment2)); BOOST_TEST(segment1 != segment2); segment2.set(LOW, point2); segment2.set(HIGH, point1); BOOST_TEST(segment1 == segment2); } void segment_traits_test() { typedef point_data point_type; typedef segment_data segment_type; point_type point1(1, 2); point_type point2(3, 4); segment_type segment = segment_mutable_traits::construct(point1, point2); BOOST_TEST(segment_traits::get(segment, LOW) == point1); BOOST_TEST(segment_traits::get(segment, HIGH) == point2); segment_mutable_traits::set(segment, LOW, point2); segment_mutable_traits::set(segment, HIGH, point1); BOOST_TEST(segment_traits::get(segment, LOW) == point2); BOOST_TEST(segment_traits::get(segment, HIGH) == point1); } template struct Segment { typedef T coordinate_type; typedef point_data point_type; point_type p0; point_type p1; }; namespace boost { namespace polygon { template struct geometry_concept< Segment > { typedef segment_concept type; }; template struct segment_traits< Segment > { typedef T coordinate_type; typedef point_data point_type; static point_type get(const Segment& segment, direction_1d dir) { return dir.to_int() ? segment.p1 : segment.p0; } }; template struct segment_mutable_traits< Segment > { typedef T coordinate_type; typedef point_data point_type; static void set( Segment& segment, direction_1d dir, const point_type& point) { dir.to_int() ? segment.p1 = point : segment.p0 = point;; } static Segment construct( const point_type& point1, const point_type& point2) { Segment segment; segment.p0 = point1; segment.p1 = point2; return segment; } }; } } void segment_concept_test1() { typedef point_data point_type; typedef Segment segment_type; point_type point1(1, 2); point_type point2(3, 4); point_type point3(2, 3); segment_type segment1 = construct(point1, point2); BOOST_TEST(segment1.p0 == point1); BOOST_TEST(segment1.p1 == point2); BOOST_TEST(get(segment1, LOW) == point1); BOOST_TEST(low(segment1) == point1); BOOST_TEST(get(segment1, HIGH) == point2); BOOST_TEST(high(segment1) == point2); BOOST_TEST(center(segment1) == point3); set(segment1, LOW, point2); set(segment1, HIGH, point1); BOOST_TEST(segment1.p0 == point2); BOOST_TEST(segment1.p1 == point1); BOOST_TEST(get(segment1, LOW) == point2); BOOST_TEST(get(segment1, HIGH) == point1); low(segment1, point1); high(segment1, point2); BOOST_TEST(segment1.p0 == point1); BOOST_TEST(segment1.p1 == point2); segment_data segment2 = copy_construct< segment_data >(segment1); BOOST_TEST(segment1.p0 == segment2.low()); BOOST_TEST(segment1.p1 == segment2.high()); BOOST_TEST(equivalence(segment1, segment2)); segment_data segment3 = construct< segment_data >(point2, point1); assign(segment1, segment3); BOOST_TEST(segment1.p0 == point2); BOOST_TEST(segment1.p1 == point1); BOOST_TEST(!equivalence(segment1, segment2)); } void segment_concept_test2() { typedef point_data point_type; typedef Segment segment_type; point_type point1(1, 2); point_type point2(2, 4); point_type point3(0, 0); point_type point4(5, 10); point_type point5(1, 3); point_type point6(2, 3); point_type point7(100, 201); point_type point8(100, 200); point_type point9(100, 199); segment_type segment1 = construct(point1, point2); segment_type segment2 = construct(point2, point1); segment_type segment3 = construct(point1, point5); BOOST_TEST(orientation(segment1, point1) == 0); BOOST_TEST(orientation(segment1, point2) == 0); BOOST_TEST(orientation(segment1, point3) == 0); BOOST_TEST(orientation(segment1, point4) == 0); BOOST_TEST(orientation(segment1, point5) == 1); BOOST_TEST(orientation(segment2, point5) == -1); BOOST_TEST(orientation(segment1, point6) == -1); BOOST_TEST(orientation(segment2, point6) == 1); BOOST_TEST(orientation(segment1, point7) == 1); BOOST_TEST(orientation(segment2, point7) == -1); BOOST_TEST(orientation(segment1, point8) == 0); BOOST_TEST(orientation(segment1, point9) == -1); BOOST_TEST(orientation(segment2, point9) == 1); BOOST_TEST(orientation(segment3, point6) == -1); BOOST_TEST(orientation(segment3, point3) == 1); } void segment_concept_test3() { typedef point_data point_type; typedef Segment segment_type; segment_type segment1 = construct( point_type(0, 0), point_type(1, 2)); segment_type segment2 = construct( point_type(0, 0), point_type(2, 4)); segment_type segment3 = construct( point_type(0, 0), point_type(2, 3)); segment_type segment4 = construct( point_type(0, 0), point_type(2, 5)); segment_type segment5 = construct( point_type(0, 2), point_type(2, 0)); BOOST_TEST(orientation(segment1, segment2) == 0); BOOST_TEST(orientation(segment1, segment3) == -1); BOOST_TEST(orientation(segment3, segment1) == 1); BOOST_TEST(orientation(segment1, segment4) == 1); BOOST_TEST(orientation(segment4, segment1) == -1); BOOST_TEST(orientation(segment1, segment5) == -1); BOOST_TEST(orientation(segment5, segment1) == 1); } void segment_concept_test4() { typedef point_data point_type; typedef Segment segment_type; point_type point1(1, 2); point_type point2(3, 6); point_type point3(2, 4); point_type point4(4, 8); point_type point5(0, 0); segment_type segment = construct(point1, point2); BOOST_TEST(contains(segment, point1, true)); BOOST_TEST(contains(segment, point2, true)); BOOST_TEST(!contains(segment, point1, false)); BOOST_TEST(!contains(segment, point2, false)); BOOST_TEST(contains(segment, point3, false)); BOOST_TEST(!contains(segment, point4, true)); BOOST_TEST(!contains(segment, point5, true)); } void segment_concept_test5() { typedef point_data point_type; typedef Segment segment_type; point_type point1(0, 0); point_type point2(10, 0); point_type point3(5, 0); point_type point4(-1, 0); point_type point5(11, 0); segment_type segment = construct(point1, point2); BOOST_TEST(contains(segment, point1, true)); BOOST_TEST(contains(segment, point2, true)); BOOST_TEST(!contains(segment, point1, false)); BOOST_TEST(!contains(segment, point2, false)); BOOST_TEST(contains(segment, point3, false)); BOOST_TEST(!contains(segment, point4, true)); BOOST_TEST(!contains(segment, point5, true)); } void segment_concept_test6() { typedef point_data point_type; typedef Segment segment_type; point_type point1(0, 0); point_type point2(1, 2); point_type point3(2, 4); point_type point4(3, 6); point_type point5(4, 8); point_type point6(5, 10); segment_type segment1 = construct(point2, point5); segment_type segment2 = construct(point3, point4); segment_type segment3 = construct(point1, point3); segment_type segment4 = construct(point4, point6); BOOST_TEST(contains(segment1, segment2, false)); BOOST_TEST(!contains(segment2, segment1, true)); BOOST_TEST(!contains(segment1, segment3, true)); BOOST_TEST(!contains(segment1, segment4, true)); BOOST_TEST(contains(segment1, segment1, true)); BOOST_TEST(!contains(segment1, segment1, false)); } template struct Transformer { void scale(T& x, T& y) const { x *= 2; y *= 2; } void transform(T& x, T& y) const { T tmp = x; x = y; y = tmp; } }; void segment_concept_test7() { typedef point_data point_type; typedef Segment segment_type; point_type point1(1, 2); point_type point2(4, 6); segment_type segment1 = construct(point1, point2); scale_up(segment1, 3); BOOST_TEST(low(segment1) == point_type(3, 6)); BOOST_TEST(high(segment1) == point_type(12, 18)); scale_down(segment1, 3); BOOST_TEST(low(segment1) == point1); BOOST_TEST(high(segment1) == point2); BOOST_TEST(length(segment1) == 5); move(segment1, HORIZONTAL, 1); move(segment1, VERTICAL, 2); BOOST_TEST(low(segment1) == point_type(2, 4)); BOOST_TEST(high(segment1) == point_type(5, 8)); BOOST_TEST(length(segment1) == 5); convolve(segment1, point_type(1, 2)); BOOST_TEST(low(segment1) == point_type(3, 6)); BOOST_TEST(high(segment1) == point_type(6, 10)); deconvolve(segment1, point_type(2, 4)); BOOST_TEST(low(segment1) == point1); BOOST_TEST(high(segment1) == point2); scale(segment1, Transformer()); BOOST_TEST(low(segment1) == point_type(2, 4)); BOOST_TEST(high(segment1) == point_type(8, 12)); transform(segment1, Transformer()); BOOST_TEST(low(segment1) == point_type(4, 2)); BOOST_TEST(high(segment1) == point_type(12, 8)); } void segment_concept_test8() { typedef point_data point_type; typedef Segment segment_type; segment_type segment1 = construct( point_type(0, 0), point_type(1, 2)); segment_type segment2 = construct( point_type(1, 2), point_type(2, 4)); segment_type segment3 = construct( point_type(2, 4), point_type(0, 4)); segment_type segment4 = construct( point_type(0, 4), point_type(0, 0)); BOOST_TEST(abuts(segment1, segment2, HIGH)); BOOST_TEST(abuts(segment2, segment3, HIGH)); BOOST_TEST(abuts(segment3, segment4, HIGH)); BOOST_TEST(abuts(segment4, segment1, HIGH)); BOOST_TEST(!abuts(segment1, segment2, LOW)); BOOST_TEST(!abuts(segment2, segment3, LOW)); BOOST_TEST(!abuts(segment3, segment4, LOW)); BOOST_TEST(!abuts(segment4, segment1, LOW)); BOOST_TEST(abuts(segment2, segment1)); BOOST_TEST(abuts(segment3, segment2)); BOOST_TEST(abuts(segment4, segment3)); BOOST_TEST(abuts(segment1, segment4)); BOOST_TEST(!abuts(segment1, segment3)); BOOST_TEST(!abuts(segment2, segment4)); } void segment_concept_test9() { typedef point_data point_type; typedef Segment segment_type; segment_type segment1 = construct( point_type(0, 0), point_type(2, 2)); segment_type segment2 = construct( point_type(1, 1), point_type(3, 3)); segment_type segment3 = construct( point_type(2, 2), point_type(-1, -1)); segment_type segment4 = construct( point_type(1, 3), point_type(3, 1)); segment_type segment5 = construct( point_type(2, 2), point_type(1, 3)); BOOST_TEST(intersects(segment1, segment2, false)); BOOST_TEST(intersects(segment1, segment2, true)); BOOST_TEST(intersects(segment1, segment3, false)); BOOST_TEST(intersects(segment1, segment3, true)); BOOST_TEST(intersects(segment2, segment3, false)); BOOST_TEST(intersects(segment2, segment3, true)); BOOST_TEST(intersects(segment4, segment3, false)); BOOST_TEST(intersects(segment4, segment3, true)); BOOST_TEST(intersects(segment4, segment2, false)); BOOST_TEST(intersects(segment4, segment2, true)); BOOST_TEST(!intersects(segment3, segment5, false)); BOOST_TEST(intersects(segment3, segment5, true)); } void segment_concept_test10() { typedef point_data point_type; typedef Segment segment_type; segment_type segment1 = construct( point_type(0, 0), point_type(0, 2)); segment_type segment2 = construct( point_type(0, 1), point_type(0, 3)); segment_type segment3 = construct( point_type(0, 1), point_type(0, 2)); segment_type segment4 = construct( point_type(0, 2), point_type(0, 3)); segment_type segment5 = construct( point_type(0, 2), point_type(2, 2)); segment_type segment6 = construct( point_type(0, 1), point_type(1, 1)); BOOST_TEST(intersects(segment1, segment1, false)); BOOST_TEST(intersects(segment1, segment1, true)); BOOST_TEST(intersects(segment1, segment2, false)); BOOST_TEST(intersects(segment1, segment2, true)); BOOST_TEST(intersects(segment1, segment3, false)); BOOST_TEST(intersects(segment1, segment3, true)); BOOST_TEST(intersects(segment2, segment3, false)); BOOST_TEST(intersects(segment2, segment3, true)); BOOST_TEST(!intersects(segment1, segment4, false)); BOOST_TEST(intersects(segment1, segment4, true)); BOOST_TEST(!intersects(segment1, segment5, false)); BOOST_TEST(intersects(segment1, segment5, true)); BOOST_TEST(intersects(segment1, segment6, false)); BOOST_TEST(intersects(segment1, segment6, true)); } void segment_concept_test11() { typedef point_data point_type; typedef Segment segment_type; point_type point1(1, 2); point_type point2(7, 10); segment_type segment1 = construct(point1, point2); BOOST_TEST(euclidean_distance(segment1, point1) == 0.0); BOOST_TEST(euclidean_distance(segment1, point2) == 0.0); BOOST_TEST(euclidean_distance(segment1, point_type(10, 14)) == 5.0); BOOST_TEST(euclidean_distance(segment1, point_type(-3, -1)) == 5.0); BOOST_TEST(euclidean_distance(segment1, point_type(0, 9)) == 5.0); BOOST_TEST(euclidean_distance(segment1, point_type(8, 3)) == 5.0); } void segment_concept_test12() { typedef point_data point_type; typedef Segment segment_type; segment_type segment1 = construct( point_type(0, 0), point_type(3, 4)); segment_type segment2 = construct( point_type(2, 0), point_type(0, 2)); segment_type segment3 = construct( point_type(1, -7), point_type(10, 5)); segment_type segment4 = construct( point_type(7, 7), point_type(10, 11)); BOOST_TEST(euclidean_distance(segment1, segment2) == 0.0); BOOST_TEST(euclidean_distance(segment1, segment3) == 5.0); BOOST_TEST(euclidean_distance(segment1, segment4) == 5.0); } int main() { segment_data_test(); segment_traits_test(); segment_concept_test1(); segment_concept_test2(); segment_concept_test3(); segment_concept_test4(); segment_concept_test5(); segment_concept_test6(); segment_concept_test7(); segment_concept_test8(); segment_concept_test9(); segment_concept_test10(); segment_concept_test11(); segment_concept_test12(); return boost::report_errors(); }