normalize.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2016, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_ARITHMETIC_NORMALIZE_HPP
  8. #define BOOST_GEOMETRY_ARITHMETIC_NORMALIZE_HPP
  9. #include <boost/geometry/core/coordinate_type.hpp>
  10. #include <boost/geometry/arithmetic/arithmetic.hpp>
  11. #include <boost/geometry/arithmetic/dot_product.hpp>
  12. #include <boost/geometry/util/math.hpp>
  13. namespace boost { namespace geometry
  14. {
  15. #ifndef DOXYGEN_NO_DETAIL
  16. namespace detail
  17. {
  18. template <typename Point>
  19. inline typename coordinate_type<Point>::type vec_length_sqr(Point const& pt)
  20. {
  21. return dot_product(pt, pt);
  22. }
  23. template <typename Point>
  24. inline typename coordinate_type<Point>::type vec_length(Point const& pt)
  25. {
  26. // NOTE: hypot() could be used instead of sqrt()
  27. return math::sqrt(dot_product(pt, pt));
  28. }
  29. template <typename Point>
  30. inline bool vec_normalize(Point & pt, typename coordinate_type<Point>::type & len)
  31. {
  32. typedef typename coordinate_type<Point>::type coord_t;
  33. coord_t const c0 = 0;
  34. len = vec_length(pt);
  35. if (math::equals(len, c0))
  36. {
  37. return false;
  38. }
  39. divide_value(pt, len);
  40. return true;
  41. }
  42. template <typename Point>
  43. inline bool vec_normalize(Point & pt)
  44. {
  45. typedef typename coordinate_type<Point>::type coord_t;
  46. coord_t len;
  47. return vec_normalize(pt, len);
  48. }
  49. } // namespace detail
  50. #endif // DOXYGEN_NO_DETAIL
  51. }} // namespace boost::geometry
  52. #endif // BOOST_GEOMETRY_ARITHMETIC_NORMALIZE_HPP