dot_product.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2020.
  6. // Modifications copyright (c) 2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP
  14. #define BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP
  15. #include <cstddef>
  16. #include <boost/concept/requires.hpp>
  17. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  18. #include <boost/geometry/util/select_coordinate_type.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail
  23. {
  24. template <typename P1, typename P2, std::size_t Dimension, std::size_t DimensionCount>
  25. struct dot_product_maker
  26. {
  27. typedef typename select_coordinate_type<P1, P2>::type coordinate_type;
  28. static constexpr coordinate_type apply(P1 const& p1, P2 const& p2)
  29. {
  30. return get<Dimension>(p1) * get<Dimension>(p2)
  31. + dot_product_maker<P1, P2, Dimension+1, DimensionCount>::apply(p1, p2);
  32. }
  33. };
  34. template <typename P1, typename P2, std::size_t DimensionCount>
  35. struct dot_product_maker<P1, P2, DimensionCount, DimensionCount>
  36. {
  37. typedef typename select_coordinate_type<P1, P2>::type coordinate_type;
  38. static constexpr coordinate_type apply(P1 const& p1, P2 const& p2)
  39. {
  40. return get<DimensionCount>(p1) * get<DimensionCount>(p2);
  41. }
  42. };
  43. } // namespace detail
  44. #endif // DOXYGEN_NO_DETAIL
  45. /*!
  46. \brief Computes the dot product (or scalar product) of 2 vectors (points).
  47. \ingroup arithmetic
  48. \tparam Point1 \tparam_point
  49. \tparam Point2 \tparam_point
  50. \param p1 first point
  51. \param p2 second point
  52. \return the dot product
  53. \qbk{[heading Examples]}
  54. \qbk{[dot_product] [dot_product_output]}
  55. */
  56. template <typename Point1, typename Point2>
  57. // workaround for VS2015
  58. #if !defined(_MSC_VER) || (_MSC_VER >= 1910)
  59. constexpr
  60. #endif
  61. inline typename select_coordinate_type<Point1, Point2>::type dot_product(
  62. Point1 const& p1, Point2 const& p2)
  63. {
  64. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point1>) );
  65. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point2>) );
  66. return detail::dot_product_maker
  67. <
  68. Point1, Point2,
  69. 0, dimension<Point1>::type::value - 1
  70. >::apply(p1, p2);
  71. }
  72. }} // namespace boost::geometry
  73. #endif // BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP