1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
4 // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
6 
7 // This file was modified by Oracle on 2016.
8 // Modifications copyright (c) 2016, Oracle and/or its affiliates.
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 
15 #ifndef BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP
16 #define BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP
17 
18 
19 #include <cstddef>
20 
21 #include <boost/mpl/assert.hpp>
22 #include <boost/mpl/size_t.hpp>
23 
24 #include <boost/geometry/core/access.hpp>
25 #include <boost/geometry/core/coordinate_dimension.hpp>
26 
27 #include <boost/geometry/geometries/concepts/point_concept.hpp>
28 
29 
30 namespace boost { namespace geometry
31 {
32 
33 #ifndef DOXYGEN_NO_DETAIL
34 namespace detail
35 {
36 
37 template <std::size_t Dimension>
38 struct cross_product
39 {
40     // We define cross product only for 2d (see Wolfram) and 3d.
41     // In Math, it is also well-defined for 7-dimension.
42     // Generalisation of cross product to n-dimension is defined as
43     // wedge product but it is not direct analogue to binary cross product.
44     BOOST_MPL_ASSERT_MSG((false),
45                          NOT_IMPLEMENTED_FOR_THIS_DIMENSION,
46                          (mpl::size_t<Dimension>));
47 };
48 
49 template <>
50 struct cross_product<2>
51 {
52     template <typename P1, typename P2, typename ResultP>
applyboost::geometry::detail::cross_product53     static inline void apply(P1 const& p1, P2 const& p2, ResultP& result)
54     {
55         assert_dimension<P1, 2>();
56         assert_dimension<P2, 2>();
57         assert_dimension<ResultP, 2>();
58 
59         // For 2-dimensions, analog of the cross product U(x,y) and V(x,y) is
60         // Ux * Vy - Uy * Vx
61         // which is returned as 0-component (or X) of 2d vector, 1-component is undefined.
62         set<0>(result, get<0>(p1) * get<1>(p2) - get<1>(p1) * get<0>(p2));
63     }
64 };
65 
66 template <>
67 struct cross_product<3>
68 {
69     template <typename P1, typename P2, typename ResultP>
applyboost::geometry::detail::cross_product70     static inline void apply(P1 const& p1, P2 const& p2, ResultP& result)
71     {
72         assert_dimension<P1, 3>();
73         assert_dimension<P2, 3>();
74         assert_dimension<ResultP, 3>();
75 
76         set<0>(result, get<1>(p1) * get<2>(p2) - get<2>(p1) * get<1>(p2));
77         set<1>(result, get<2>(p1) * get<0>(p2) - get<0>(p1) * get<2>(p2));
78         set<2>(result, get<0>(p1) * get<1>(p2) - get<1>(p1) * get<0>(p2));
79     }
80 };
81 
82 } // namespace detail
83 #endif // DOXYGEN_NO_DETAIL
84 
85 
86 /*!
87 \brief Computes the cross product of two vectors.
88 \details All vectors should have the same dimension, 3 or 2.
89 \ingroup arithmetic
90 \param p1 first vector
91 \param p2 second vector
92 \return the cross product vector
93  */
94 template <typename ResultP, typename P1, typename P2>
cross_product(P1 const & p1,P2 const & p2)95 inline ResultP cross_product(P1 const& p1, P2 const& p2)
96 {
97     BOOST_CONCEPT_ASSERT( (concepts::Point<ResultP>) );
98     BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<P1>) );
99     BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<P2>) );
100 
101     ResultP result;
102     detail::cross_product<dimension<ResultP>::value>::apply(p1, p2, result);
103     return result;
104 }
105 
106 /*!
107 \brief Computes the cross product of two vectors.
108 \details All vectors should have the same dimension, 3 or 2.
109 \ingroup arithmetic
110 \param p1 first vector
111 \param p2 second vector
112 \return the cross product vector
113 */
114 template <typename P>
cross_product(P const & p1,P const & p2)115 inline P cross_product(P const& p1, P const& p2)
116 {
117     BOOST_CONCEPT_ASSERT((concepts::Point<P>));
118     BOOST_CONCEPT_ASSERT((concepts::ConstPoint<P>));
119 
120     P result;
121     detail::cross_product<dimension<P>::value>::apply(p1, p2, result);
122     return result;
123 }
124 
125 
126 }} // namespace boost::geometry
127 
128 #endif // BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP
129