cross_product.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  3. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // This file was modified by Oracle on 2016-2020.
  6. // Modifications copyright (c) 2016-2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP
  12. #define BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP
  13. #include <cstddef>
  14. #include <type_traits>
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/make.hpp>
  17. #include <boost/geometry/core/coordinate_dimension.hpp>
  18. #include <boost/geometry/core/static_assert.hpp>
  19. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail
  24. {
  25. template <std::size_t Dimension>
  26. struct cross_product
  27. {
  28. // We define cross product only for 2d (see Wolfram) and 3d.
  29. // In Math, it is also well-defined for 7-dimension.
  30. // Generalisation of cross product to n-dimension is defined as
  31. // wedge product but it is not direct analogue to binary cross product.
  32. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  33. "Not implemented for this Dimension.",
  34. std::integral_constant<std::size_t, Dimension>);
  35. };
  36. template <>
  37. struct cross_product<2>
  38. {
  39. template <typename P1, typename P2, typename ResultP>
  40. static void apply(P1 const& p1, P2 const& p2, ResultP& result)
  41. {
  42. assert_dimension<P1, 2>();
  43. assert_dimension<P2, 2>();
  44. assert_dimension<ResultP, 2>();
  45. // For 2-dimensions, analog of the cross product U(x,y) and V(x,y) is
  46. // Ux * Vy - Uy * Vx
  47. // which is returned as 0-component (or X) of 2d vector, 1-component is undefined.
  48. set<0>(result, get<0>(p1) * get<1>(p2) - get<1>(p1) * get<0>(p2));
  49. }
  50. };
  51. template <>
  52. struct cross_product<3>
  53. {
  54. template <typename P1, typename P2, typename ResultP>
  55. static void apply(P1 const& p1, P2 const& p2, ResultP& result)
  56. {
  57. assert_dimension<P1, 3>();
  58. assert_dimension<P2, 3>();
  59. assert_dimension<ResultP, 3>();
  60. set<0>(result, get<1>(p1) * get<2>(p2) - get<2>(p1) * get<1>(p2));
  61. set<1>(result, get<2>(p1) * get<0>(p2) - get<0>(p1) * get<2>(p2));
  62. set<2>(result, get<0>(p1) * get<1>(p2) - get<1>(p1) * get<0>(p2));
  63. }
  64. template <typename ResultP, typename P1, typename P2>
  65. static constexpr ResultP apply(P1 const& p1, P2 const& p2)
  66. {
  67. assert_dimension<P1, 3>();
  68. assert_dimension<P2, 3>();
  69. assert_dimension<ResultP, 3>();
  70. return traits::make<ResultP>::apply(
  71. get<1>(p1) * get<2>(p2) - get<2>(p1) * get<1>(p2),
  72. get<2>(p1) * get<0>(p2) - get<0>(p1) * get<2>(p2),
  73. get<0>(p1) * get<1>(p2) - get<1>(p1) * get<0>(p2));
  74. }
  75. };
  76. } // namespace detail
  77. #endif // DOXYGEN_NO_DETAIL
  78. /*!
  79. \brief Computes the cross product of two vectors.
  80. \details All vectors should have the same dimension, 3 or 2.
  81. \ingroup arithmetic
  82. \param p1 first vector
  83. \param p2 second vector
  84. \return the cross product vector
  85. */
  86. template
  87. <
  88. typename ResultP, typename P1, typename P2,
  89. std::enable_if_t
  90. <
  91. dimension<ResultP>::value != 3
  92. || ! traits::make<ResultP>::is_specialized,
  93. int
  94. > = 0
  95. >
  96. inline ResultP cross_product(P1 const& p1, P2 const& p2)
  97. {
  98. BOOST_CONCEPT_ASSERT( (concepts::Point<ResultP>) );
  99. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<P1>) );
  100. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<P2>) );
  101. ResultP result;
  102. detail::cross_product<dimension<ResultP>::value>::apply(p1, p2, result);
  103. return result;
  104. }
  105. template
  106. <
  107. typename ResultP, typename P1, typename P2,
  108. std::enable_if_t
  109. <
  110. dimension<ResultP>::value == 3
  111. && traits::make<ResultP>::is_specialized,
  112. int
  113. > = 0
  114. >
  115. // workaround for VS2015
  116. #if !defined(_MSC_VER) || (_MSC_VER >= 1910)
  117. constexpr
  118. #endif
  119. inline ResultP cross_product(P1 const& p1, P2 const& p2)
  120. {
  121. BOOST_CONCEPT_ASSERT((concepts::Point<ResultP>));
  122. BOOST_CONCEPT_ASSERT((concepts::ConstPoint<P1>));
  123. BOOST_CONCEPT_ASSERT((concepts::ConstPoint<P2>));
  124. return detail::cross_product<3>::apply<ResultP>(p1, p2);
  125. }
  126. /*!
  127. \brief Computes the cross product of two vectors.
  128. \details All vectors should have the same dimension, 3 or 2.
  129. \ingroup arithmetic
  130. \param p1 first vector
  131. \param p2 second vector
  132. \return the cross product vector
  133. \qbk{[heading Examples]}
  134. \qbk{[cross_product] [cross_product_output]}
  135. */
  136. template
  137. <
  138. typename P,
  139. std::enable_if_t
  140. <
  141. dimension<P>::value != 3
  142. || ! traits::make<P>::is_specialized,
  143. int
  144. > = 0
  145. >
  146. inline P cross_product(P const& p1, P const& p2)
  147. {
  148. BOOST_CONCEPT_ASSERT((concepts::Point<P>));
  149. BOOST_CONCEPT_ASSERT((concepts::ConstPoint<P>));
  150. P result;
  151. detail::cross_product<dimension<P>::value>::apply(p1, p2, result);
  152. return result;
  153. }
  154. template
  155. <
  156. typename P,
  157. std::enable_if_t
  158. <
  159. dimension<P>::value == 3
  160. && traits::make<P>::is_specialized,
  161. int
  162. > = 0
  163. >
  164. // workaround for VS2015
  165. #if !defined(_MSC_VER) || (_MSC_VER >= 1910)
  166. constexpr
  167. #endif
  168. inline P cross_product(P const& p1, P const& p2)
  169. {
  170. BOOST_CONCEPT_ASSERT((concepts::Point<P>));
  171. BOOST_CONCEPT_ASSERT((concepts::ConstPoint<P>));
  172. return detail::cross_product<3>::apply<P>(p1, p2);
  173. }
  174. }} // namespace boost::geometry
  175. #endif // BOOST_GEOMETRY_ARITHMETIC_CROSS_PRODUCT_HPP