1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014-2017 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2017-2021.
9 // Modifications copyright (c) 2017-2021 Oracle and/or its affiliates.
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_COLLECT_VECTORS_HPP
20 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_COLLECT_VECTORS_HPP
23 #include <boost/numeric/conversion/cast.hpp>
25 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
26 #include <boost/geometry/algorithms/detail/normalize.hpp>
27 #include <boost/geometry/algorithms/not_implemented.hpp>
29 #include <boost/geometry/core/cs.hpp>
30 #include <boost/geometry/core/interior_rings.hpp>
31 #include <boost/geometry/core/tags.hpp>
33 #include <boost/geometry/formulas/spherical.hpp>
35 #include <boost/geometry/geometries/concepts/check.hpp>
37 #include <boost/geometry/util/math.hpp>
38 #include <boost/geometry/util/range.hpp>
40 #include <boost/geometry/views/detail/closed_clockwise_view.hpp>
42 #include <boost/geometry/strategies/spherical/ssf.hpp>
43 #include <boost/geometry/strategies/normalize.hpp>
46 namespace boost { namespace geometry
51 struct collected_vector_cartesian
55 inline collected_vector_cartesian()
58 inline collected_vector_cartesian(T const& px, T const& py,
59 T const& pdx, T const& pdy)
68 template <typename Point>
69 inline collected_vector_cartesian(Point const& p1, Point const& p2)
80 T magnitude = math::sqrt(boost::numeric_cast<T>(dx * dx + dy * dy));
82 // NOTE: shouldn't here math::equals() be called?
94 inline bool operator<(collected_vector_cartesian const& other) const
96 if (math::equals(x, other.x))
98 if (math::equals(y, other.y))
100 if (math::equals(dx, other.dx))
102 return dy < other.dy;
104 return dx < other.dx;
111 inline bool next_is_collinear(collected_vector_cartesian const& other) const
113 return same_direction(other);
117 inline bool operator==(collected_vector_cartesian const& other) const
119 return math::equals(x, other.x)
120 && math::equals(y, other.y)
121 && same_direction(other);
125 inline bool same_direction(collected_vector_cartesian const& other) const
127 // For high precision arithmetic, we have to be
128 // more relaxed then using ==
129 // Because 2/sqrt( (0,0)<->(2,2) ) == 1/sqrt( (0,0)<->(1,1) )
130 // is not always true (at least, not for some user defined types)
131 return math::equals_with_epsilon(dx, other.dx)
132 && math::equals_with_epsilon(dy, other.dy);
140 // Compatible with spherical_side_formula which currently
141 // is the default spherical_equatorial and geographic strategy
142 // so CSTag is spherical_equatorial_tag or geographic_tag
143 template <typename T, typename Point>
144 struct collected_vector_spherical
148 typedef model::point<T, 3, cs::cartesian> vector_type;
150 collected_vector_spherical()
153 collected_vector_spherical(Point const& p1, Point const& p2)
154 : origin(get<0>(p1), get<1>(p1))
156 origin = detail::return_normalized<Point>(
158 strategy::normalize::spherical_point());
160 using namespace geometry::formula;
161 prev = sph_to_cart3d<vector_type>(p1);
162 next = sph_to_cart3d<vector_type>(p2);
163 cross = direction = cross_product(prev, next);
168 T const magnitude_sqr = dot_product(direction, direction);
170 // NOTE: shouldn't here math::equals() be called?
171 if (magnitude_sqr > 0)
173 divide_value(direction, math::sqrt(magnitude_sqr));
180 bool operator<(collected_vector_spherical const& other) const
182 if (math::equals(get<0>(origin), get<0>(other.origin)))
184 if (math::equals(get<1>(origin), get<1>(other.origin)))
186 if (math::equals(get<0>(direction), get<0>(other.direction)))
188 if (math::equals(get<1>(direction), get<1>(other.direction)))
190 return get<2>(direction) < get<2>(other.direction);
192 return get<1>(direction) < get<1>(other.direction);
194 return get<0>(direction) < get<0>(other.direction);
196 return get<1>(origin) < get<1>(other.origin);
198 return get<0>(origin) < get<0>(other.origin);
201 // For consistency with side and intersection strategies used by relops
202 // IMPORTANT: this method should be called for previous vector
203 // and next vector should be passed as parameter
204 bool next_is_collinear(collected_vector_spherical const& other) const
206 return formula::sph_side_value(cross, other.next) == 0;
210 bool operator==(collected_vector_spherical const& other) const
212 return math::equals(get<0>(origin), get<0>(other.origin))
213 && math::equals(get<1>(origin), get<1>(other.origin))
214 && is_collinear(other);
218 // For consistency with side and intersection strategies used by relops
219 // NOTE: alternative would be to equal-compare direction's coordinates
220 // or to check if dot product of directions is equal to 1.
221 bool is_collinear(collected_vector_spherical const& other) const
223 return formula::sph_side_value(cross, other.prev) == 0
224 && formula::sph_side_value(cross, other.next) == 0;
227 Point origin; // used for sorting and equality check
228 vector_type direction; // used for sorting, only in operator<
229 vector_type cross; // used for sorting, used for collinearity check
230 vector_type prev; // used for collinearity check, only in operator==
231 vector_type next; // used for collinearity check
234 // Version for spherical polar
235 template <typename T, typename Point>
236 struct collected_vector_polar
237 : public collected_vector_spherical<T, Point>
240 using base_point_type
241 = model::point<T, 2, cs::spherical_equatorial<geometry::degree>>;
242 using base_type = collected_vector_spherical<T, base_point_type>;
244 collected_vector_polar() {}
246 collected_vector_polar(Point const& p1, Point const& p2)
247 : base_type(to_equatorial(p1), to_equatorial(p2))
251 static base_point_type to_equatorial(Point const& p)
253 using coord_type = typename coordinate_type<Point>::type;
254 using constants = math::detail::constants_on_spheroid
257 typename coordinate_system<Point>::type::units
260 constexpr coord_type pi_2 = constants::half_period() / 2;
263 set<0>(res, get<0>(p));
264 set<1>(res, pi_2 - get<1>(p));
269 // TODO: implement collected_vector type for geographic
272 #ifndef DOXYGEN_NO_DETAIL
273 namespace detail { namespace collect_vectors
277 template <typename Range, typename Collection>
278 struct range_collect_vectors
280 typedef typename boost::range_value<Collection>::type item_type;
281 typedef typename item_type::type calculation_type;
283 static inline void apply(Collection& collection, Range const& range)
285 apply_impl(collection,
286 detail::closed_clockwise_view<Range const>(range));
290 template <typename ClosedClockwiseRange>
291 static inline void apply_impl(Collection& collection, ClosedClockwiseRange const& range)
293 if (boost::size(range) < 2)
298 auto c_old_size = boost::size(collection);
299 bool is_first = true;
300 auto it = boost::begin(range);
302 for (auto prev = it++; it != boost::end(range); prev = it++)
304 typename boost::range_value<Collection>::type v(*prev, *it);
306 // Normalize the vector -> this results in points+direction
307 // and is comparible between geometries
308 // Avoid non-duplicate points (AND division by zero)
311 // Avoid non-direction changing points
312 if (is_first || ! collection.back().next_is_collinear(v))
314 collection.push_back(v);
320 // If first one has same direction as last one, remove first one
321 if (boost::size(collection) > c_old_size + 1)
323 auto first = range::pos(collection, c_old_size);
324 if (collection.back().next_is_collinear(*first))
326 //collection.erase(first);
327 // O(1) instead of O(N)
328 *first = collection.back();
329 collection.pop_back();
336 // Default version (cartesian)
337 template <typename Box, typename Collection, typename CSTag = typename cs_tag<Box>::type>
338 struct box_collect_vectors
340 // Calculate on coordinate type, but if it is integer,
342 typedef typename boost::range_value<Collection>::type item_type;
343 typedef typename item_type::type calculation_type;
345 static inline void apply(Collection& collection, Box const& box)
347 typename point_type<Box>::type lower_left, lower_right,
348 upper_left, upper_right;
349 geometry::detail::assign_box_corners(box, lower_left, lower_right,
350 upper_left, upper_right);
352 typedef typename boost::range_value<Collection>::type item;
354 collection.push_back(item(get<0>(lower_left), get<1>(lower_left), 0, 1));
355 collection.push_back(item(get<0>(upper_left), get<1>(upper_left), 1, 0));
356 collection.push_back(item(get<0>(upper_right), get<1>(upper_right), 0, -1));
357 collection.push_back(item(get<0>(lower_right), get<1>(lower_right), -1, 0));
361 // NOTE: This is not fully correct because Box in spherical and geographic
362 // cordinate systems cannot be seen as Polygon
363 template <typename Box, typename Collection>
364 struct box_collect_vectors<Box, Collection, spherical_equatorial_tag>
366 static inline void apply(Collection& collection, Box const& box)
368 typename point_type<Box>::type lower_left, lower_right,
369 upper_left, upper_right;
370 geometry::detail::assign_box_corners(box, lower_left, lower_right,
371 upper_left, upper_right);
373 typedef typename boost::range_value<Collection>::type item;
375 collection.push_back(item(lower_left, upper_left));
376 collection.push_back(item(upper_left, upper_right));
377 collection.push_back(item(upper_right, lower_right));
378 collection.push_back(item(lower_right, lower_left));
382 template <typename Box, typename Collection>
383 struct box_collect_vectors<Box, Collection, spherical_polar_tag>
384 : box_collect_vectors<Box, Collection, spherical_equatorial_tag>
387 template <typename Box, typename Collection>
388 struct box_collect_vectors<Box, Collection, geographic_tag>
389 : box_collect_vectors<Box, Collection, spherical_equatorial_tag>
393 template <typename Polygon, typename Collection>
394 struct polygon_collect_vectors
396 static inline void apply(Collection& collection, Polygon const& polygon)
398 typedef typename geometry::ring_type<Polygon>::type ring_type;
400 typedef range_collect_vectors<ring_type, Collection> per_range;
401 per_range::apply(collection, exterior_ring(polygon));
403 typename interior_return_type<Polygon const>::type
404 rings = interior_rings(polygon);
405 for (typename detail::interior_iterator<Polygon const>::type
406 it = boost::begin(rings); it != boost::end(rings); ++it)
408 per_range::apply(collection, *it);
414 template <typename MultiGeometry, typename Collection, typename SinglePolicy>
415 struct multi_collect_vectors
417 static inline void apply(Collection& collection, MultiGeometry const& multi)
419 for (typename boost::range_iterator<MultiGeometry const>::type
420 it = boost::begin(multi);
421 it != boost::end(multi);
424 SinglePolicy::apply(collection, *it);
430 }} // namespace detail::collect_vectors
431 #endif // DOXYGEN_NO_DETAIL
435 #ifndef DOXYGEN_NO_DISPATCH
446 struct collect_vectors
448 static inline void apply(Collection&, Geometry const&)
453 template <typename Collection, typename Box>
454 struct collect_vectors<box_tag, Collection, Box>
455 : detail::collect_vectors::box_collect_vectors<Box, Collection>
460 template <typename Collection, typename Ring>
461 struct collect_vectors<ring_tag, Collection, Ring>
462 : detail::collect_vectors::range_collect_vectors<Ring, Collection>
466 template <typename Collection, typename LineString>
467 struct collect_vectors<linestring_tag, Collection, LineString>
468 : detail::collect_vectors::range_collect_vectors<LineString, Collection>
472 template <typename Collection, typename Polygon>
473 struct collect_vectors<polygon_tag, Collection, Polygon>
474 : detail::collect_vectors::polygon_collect_vectors<Polygon, Collection>
478 template <typename Collection, typename MultiPolygon>
479 struct collect_vectors<multi_polygon_tag, Collection, MultiPolygon>
480 : detail::collect_vectors::multi_collect_vectors
484 detail::collect_vectors::polygon_collect_vectors
486 typename boost::range_value<MultiPolygon>::type,
494 } // namespace dispatch
499 \ingroup collect_vectors
500 \tparam Collection Collection type, should be e.g. std::vector<>
501 \tparam Geometry geometry type
502 \param collection the collection of vectors
503 \param geometry the geometry to make collect_vectors
505 template <typename Collection, typename Geometry>
506 inline void collect_vectors(Collection& collection, Geometry const& geometry)
508 concepts::check<Geometry const>();
510 dispatch::collect_vectors
512 typename tag<Geometry>::type,
515 >::apply(collection, geometry);
519 }} // namespace boost::geometry
522 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_COLLECT_VECTORS_HPP