dot_product.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. */
  51. template <typename Point1, typename Point2>
  52. inline typename select_coordinate_type<Point1, Point2>::type dot_product(
  53. Point1 const& p1, Point2 const& p2)
  54. {
  55. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point1>) );
  56. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point2>) );
  57. return detail::dot_product_maker
  58. <
  59. Point1, Point2,
  60. 0, dimension<Point1>::type::value - 1
  61. >::apply(p1, p2);
  62. }
  63. }} // namespace boost::geometry
  64. #endif // BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP