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