cross_product.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  4. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <geometry_test_common.hpp>
  9. #include <boost/core/ignore_unused.hpp>
  10. #include <boost/geometry/arithmetic/cross_product.hpp>
  11. #include <boost/geometry/algorithms/assign.hpp>
  12. #include <boost/geometry/geometries/point.hpp>
  13. #include <boost/geometry/geometries/adapted/c_array.hpp>
  14. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  15. #include <test_common/test_point.hpp>
  16. template <typename P>
  17. void test_2d()
  18. {
  19. P p1;
  20. bg::assign_values(p1, 20, 30);
  21. P p2;
  22. bg::assign_values(p2, 45, 70);
  23. P c = bg::cross_product(p1, p2);
  24. typedef typename bg::coordinate_type<P>::type scalar_type;
  25. BOOST_CHECK_EQUAL(bg::get<0>(c), scalar_type(50));
  26. }
  27. template <typename P>
  28. void test_3d()
  29. {
  30. P p1;
  31. bg::assign_values(p1, 20, 30, 10);
  32. P p2;
  33. bg::assign_values(p2, 45, 70, 20);
  34. P c = bg::cross_product(p1, p2);
  35. typedef typename bg::coordinate_type<P>::type scalar_type;
  36. BOOST_CHECK_EQUAL(bg::get<0>(c), scalar_type(-100));
  37. BOOST_CHECK_EQUAL(bg::get<1>(c), scalar_type(50));
  38. BOOST_CHECK_EQUAL(bg::get<2>(c), scalar_type(50));
  39. }
  40. #ifdef TEST_FAIL_CROSS_PRODUCT
  41. template <typename P>
  42. void test_4d()
  43. {
  44. P p1;
  45. bg::assign_values(p1, 20, 30, 10);
  46. bg::set<3>(p1, 15);
  47. P p2;
  48. bg::assign_values(p2, 45, 70, 20);
  49. bg::set<3>(p2, 35);
  50. P c = bg::cross_product(p1, p2);
  51. boost::ignore_unused(c);
  52. }
  53. #endif
  54. int test_main(int, char* [])
  55. {
  56. test_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
  57. test_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
  58. test_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
  59. test_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
  60. test_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
  61. test_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  62. #ifdef TEST_FAIL_CROSS_PRODUCT
  63. test_4d<bg::model::point<int, 4, bg::cs::cartesian> >();
  64. test_4d<bg::model::point<float, 4, bg::cs::cartesian> >();
  65. test_4d<bg::model::point<double, 4, bg::cs::cartesian> >();
  66. #endif
  67. return 0;
  68. }