/Src/Dependencies/Boost/boost/geometry/arithmetic/dot_product.hpp

http://hadesmem.googlecode.com/ · C++ Header · 82 lines · 46 code · 20 blank · 16 comment · 0 complexity · 252a16adf28d607cd317e7f3f7e581d4 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2008-2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2009-2011 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. \param p1 first point
  46. \param p2 second point
  47. \return the dot product
  48. */
  49. template <typename P1, typename P2>
  50. inline typename select_coordinate_type<P1, P2>::type dot_product(
  51. P1 const& p1, P2 const& p2)
  52. {
  53. BOOST_CONCEPT_ASSERT( (concept::ConstPoint<P1>) );
  54. BOOST_CONCEPT_ASSERT( (concept::ConstPoint<P2>) );
  55. return detail::dot_product_maker
  56. <
  57. P1, P2,
  58. 0, dimension<P1>::type::value - 1
  59. >::apply(p1, p2);
  60. }
  61. }} // namespace boost::geometry
  62. #endif // BOOST_GEOMETRY_ARITHMETIC_DOT_PRODUCT_HPP