dot_product.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP
  11. #define BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP
  12. #include <cstddef>
  13. #include <boost/concept/requires.hpp>
  14. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  15. #include <boost/geometry/util/select_coordinate_type.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. #ifndef DOXYGEN_NO_DETAIL
  19. namespace detail
  20. {
  21. template <typename P1, typename P2, std::size_t Dimension, std::size_t DimensionCount>
  22. struct dot_product_maker
  23. {
  24. typedef typename select_coordinate_type<P1, P2>::type coordinate_type;
  25. static inline coordinate_type apply(P1 const& p1, P2 const& p2)
  26. {
  27. return get<Dimension>(p1) * get<Dimension>(p2)
  28. + dot_product_maker<P1, P2, Dimension+1, DimensionCount>::apply(p1, p2);
  29. }
  30. };
  31. template <typename P1, typename P2, std::size_t DimensionCount>
  32. struct dot_product_maker<P1, P2, DimensionCount, DimensionCount>
  33. {
  34. typedef typename select_coordinate_type<P1, P2>::type coordinate_type;
  35. static inline coordinate_type apply(P1 const& p1, P2 const& p2)
  36. {
  37. return get<DimensionCount>(p1) * get<DimensionCount>(p2);
  38. }
  39. };
  40. } // namespace detail
  41. #endif // DOXYGEN_NO_DETAIL
  42. /*!
  43. \brief Computes the dot product (or scalar product) of 2 vectors (points).
  44. \ingroup arithmetic
  45. \tparam Point1 \tparam_point
  46. \tparam Point2 \tparam_point
  47. \param p1 first point
  48. \param p2 second point
  49. \return the dot product
  50. \qbk{[heading Examples]}
  51. \qbk{[dot_product] [dot_product_output]}
  52. */
  53. template <typename Point1, typename Point2>
  54. inline typename select_coordinate_type<Point1, Point2>::type dot_product(
  55. Point1 const& p1, Point2 const& p2)
  56. {
  57. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point1>) );
  58. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point2>) );
  59. return detail::dot_product_maker
  60. <
  61. Point1, Point2,
  62. 0, dimension<Point1>::type::value - 1
  63. >::apply(p1, p2);
  64. }
  65. }} // namespace boost::geometry
  66. #endif // BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP