1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
6 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
8 // This file was modified by Oracle on 2014.
9 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 #ifndef BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP
22 #define BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP
24 #include <geometry_test_common.hpp>
26 #include <boost/core/ignore_unused.hpp>
28 #include <boost/geometry/strategies/cartesian/distance_projected_point.hpp>
29 #include <boost/geometry/strategies/cartesian/distance_projected_point_ax.hpp>
30 #include <boost/geometry/strategies/concepts/distance_concept.hpp>
32 #include <boost/geometry/io/wkt/read.hpp>
34 #include <boost/geometry/geometries/point.hpp>
35 #include <boost/geometry/geometries/adapted/c_array.hpp>
36 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
37 #include <test_common/test_point.hpp>
39 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
40 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
43 template <typename P, typename PS, typename CalculationType>
47 bg::assign_values(p1, 0, 0);
48 bg::assign_values(p2, 0, 4);
51 bg::assign_values(p, 2, 0);
53 CalculationType const sqr_expected = 4;
54 CalculationType const expected = 2;
57 namespace bgsd = bg::strategy::distance;
58 namespace services = bg::strategy::distance::services;
61 // compile-check if there is a strategy for this type
62 typedef typename services::default_strategy
64 bg::point_tag, bg::segment_tag, P, PS
65 >::type projected_point_strategy_type;
67 typedef typename services::default_strategy
69 bg::segment_tag, bg::point_tag, PS, P
70 >::type reversed_tags_projected_point_strategy_type;
72 boost::ignore_unused<projected_point_strategy_type,
73 reversed_tags_projected_point_strategy_type>();
76 // 1: normal, calculate distance:
78 typedef bgsd::projected_point<CalculationType> strategy_type;
80 BOOST_CONCEPT_ASSERT( (bg::concepts::PointSegmentDistanceStrategy<strategy_type, P, PS>) );
82 typedef typename services::return_type<strategy_type, P, PS>::type return_type;
84 strategy_type strategy;
85 return_type result = strategy.apply(p, p1, p2);
86 BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
88 // 2: the strategy should return the same result if we reverse parameters
89 result = strategy.apply(p, p2, p1);
90 BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
93 // 3: "comparable" to construct a "comparable strategy" for P1/P2
94 // a "comparable strategy" is a strategy which does not calculate the exact distance, but
95 // which returns results which can be mutually compared (e.g. avoid sqrt)
97 // 3a: "comparable_type"
98 typedef typename services::comparable_type<strategy_type>::type comparable_type;
100 // 3b: "get_comparable"
101 comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
103 return_type c_result = comparable.apply(p, p1, p2);
104 BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
107 template <typename T1, typename T2>
108 void test_check_close(T1 const& v1, T2 const& v2, double f)
110 BOOST_CHECK_CLOSE(v1, v2, f);
113 template <typename T1, typename T2>
114 void test_check_close(bg::strategy::distance::detail::projected_point_ax_result<T1> const& v1,
115 bg::strategy::distance::detail::projected_point_ax_result<T2> const& v2,
118 BOOST_CHECK_CLOSE(v1.atd, v2.atd, f);
119 BOOST_CHECK_CLOSE(v1.xtd, v2.xtd, f);
122 template <typename P1, typename P2, typename T, typename Strategy, typename ComparableStrategy>
123 void test_2d(std::string const& wkt_p,
124 std::string const& wkt_sp1,
125 std::string const& wkt_sp2,
127 T expected_comparable_distance,
129 ComparableStrategy comparable_strategy)
133 bg::read_wkt(wkt_p, p);
134 bg::read_wkt(wkt_sp1, sp1);
135 bg::read_wkt(wkt_sp2, sp2);
139 (bg::concepts::PointSegmentDistanceStrategy<Strategy, P1, P2>)
143 (bg::concepts::PointSegmentDistanceStrategy<ComparableStrategy, P1, P2>)
147 typedef typename bg::strategy::distance::services::return_type<Strategy, P1, P2>::type return_type;
148 return_type d = strategy.apply(p, sp1, sp2);
149 test_check_close(d, expected_distance, 0.001);
152 // Test combination with the comparable strategy
154 typedef typename bg::strategy::distance::services::return_type<ComparableStrategy, P1, P2>::type return_type;
155 return_type d = comparable_strategy.apply(p, sp1, sp2);
156 test_check_close(d, expected_comparable_distance, 0.01);
161 template <typename P1, typename P2, typename T>
162 void test_2d(std::string const& wkt_p,
163 std::string const& wkt_sp1,
164 std::string const& wkt_sp2,
167 typedef bg::strategy::distance::projected_point<> strategy_type;
168 typedef bg::strategy::distance::projected_point
171 bg::strategy::distance::comparable::pythagoras<>
172 > comparable_strategy_type;
174 strategy_type strategy;
175 comparable_strategy_type comparable_strategy;
177 T expected_squared_distance = expected_distance * expected_distance;
178 test_2d<P1, P2>(wkt_p, wkt_sp1, wkt_sp2, expected_distance, expected_squared_distance, strategy, comparable_strategy);
181 #endif // BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP