radar_beam_height.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. /**
  11. \file
  12. \brief radar_beam_height.cpp
  13. \details
  14. Demonstrate library usage for user test cases suggested by Michael Fawcett.
  15. Output:
  16. @verbatim
  17. //[radar_beam_height_output
  18. radar range : 300 nmi
  19. earth radius : 6.37101e+06 m
  20. beam height 1 : 18169.7 m
  21. beam height 2 : 9.81085 nmi
  22. beam height 3 : 18169.7 m
  23. beam height 4 : 9.81085 nmi
  24. beam height approx : 59488.4 ft
  25. beam height approx : 18132.1 m
  26. //]
  27. @endverbatim
  28. **/
  29. #include <iostream>
  30. #include <boost/units/conversion.hpp>
  31. #include <boost/units/io.hpp>
  32. #include <boost/units/pow.hpp>
  33. #include <boost/units/systems/si.hpp>
  34. #include <boost/units/systems/si/prefixes.hpp>
  35. using boost::units::length_dimension;
  36. using boost::units::pow;
  37. using boost::units::root;
  38. using boost::units::quantity;
  39. using boost::units::unit;
  40. //[radar_beam_height_class_snippet_1
  41. namespace nautical {
  42. struct length_base_unit :
  43. boost::units::base_unit<length_base_unit, length_dimension, 1>
  44. {
  45. static std::string name() { return "nautical mile"; }
  46. static std::string symbol() { return "nmi"; }
  47. };
  48. typedef boost::units::make_system<length_base_unit>::type system;
  49. /// unit typedefs
  50. typedef unit<length_dimension,system> length;
  51. static const length mile,miles;
  52. } // namespace nautical
  53. // helper for conversions between nautical length and si length
  54. BOOST_UNITS_DEFINE_CONVERSION_FACTOR(nautical::length_base_unit,
  55. boost::units::si::meter_base_unit,
  56. double, 1.852e3);
  57. //]
  58. //[radar_beam_height_class_snippet_2
  59. namespace imperial {
  60. struct length_base_unit :
  61. boost::units::base_unit<length_base_unit, length_dimension, 2>
  62. {
  63. static std::string name() { return "foot"; }
  64. static std::string symbol() { return "ft"; }
  65. };
  66. typedef boost::units::make_system<length_base_unit>::type system;
  67. /// unit typedefs
  68. typedef unit<length_dimension,system> length;
  69. static const length foot,feet;
  70. } // imperial
  71. // helper for conversions between imperial length and si length
  72. BOOST_UNITS_DEFINE_CONVERSION_FACTOR(imperial::length_base_unit,
  73. boost::units::si::meter_base_unit,
  74. double, 1.0/3.28083989501312);
  75. //]
  76. // radar beam height functions
  77. //[radar_beam_height_function_snippet_1
  78. template<class System,typename T>
  79. constexpr
  80. quantity<unit<boost::units::length_dimension,System>,T>
  81. radar_beam_height(const quantity<unit<length_dimension,System>,T>& radar_range,
  82. const quantity<unit<length_dimension,System>,T>& earth_radius,
  83. T k = 4.0/3.0)
  84. {
  85. return quantity<unit<length_dimension,System>,T>
  86. (pow<2>(radar_range)/(2.0*k*earth_radius));
  87. }
  88. //]
  89. //[radar_beam_height_function_snippet_2
  90. template<class return_type,class System1,class System2,typename T>
  91. constexpr
  92. return_type
  93. radar_beam_height(const quantity<unit<length_dimension,System1>,T>& radar_range,
  94. const quantity<unit<length_dimension,System2>,T>& earth_radius,
  95. T k = 4.0/3.0)
  96. {
  97. // need to decide which system to use for calculation
  98. return pow<2>(static_cast<return_type>(radar_range))
  99. / (2.0*k*static_cast<return_type>(earth_radius));
  100. }
  101. //]
  102. //[radar_beam_height_function_snippet_3
  103. constexpr
  104. quantity<imperial::length>
  105. radar_beam_height(const quantity<nautical::length>& range)
  106. {
  107. return quantity<imperial::length>
  108. (pow<2>(range/(1.23*nautical::miles/root<2>(imperial::feet))));
  109. }
  110. //]
  111. int main(void)
  112. {
  113. using namespace boost::units;
  114. using namespace boost::units::si;
  115. using namespace nautical;
  116. //[radar_beam_height_snippet_1
  117. const quantity<nautical::length> radar_range(300.0*miles);
  118. const quantity<si::length> earth_radius(6371.0087714*kilo*meters);
  119. const quantity<si::length> beam_height_1(radar_beam_height(quantity<si::length>(radar_range),earth_radius));
  120. const quantity<nautical::length> beam_height_2(radar_beam_height(radar_range,quantity<nautical::length>(earth_radius)));
  121. const quantity<si::length> beam_height_3(radar_beam_height< quantity<si::length> >(radar_range,earth_radius));
  122. const quantity<nautical::length> beam_height_4(radar_beam_height< quantity<nautical::length> >(radar_range,earth_radius));
  123. //]
  124. std::cout << "radar range : " << radar_range << std::endl
  125. << "earth radius : " << earth_radius << std::endl
  126. << "beam height 1 : " << beam_height_1 << std::endl
  127. << "beam height 2 : " << beam_height_2 << std::endl
  128. << "beam height 3 : " << beam_height_3 << std::endl
  129. << "beam height 4 : " << beam_height_4 << std::endl
  130. << "beam height approx : " << radar_beam_height(radar_range)
  131. << std::endl
  132. << "beam height approx : "
  133. << quantity<si::length>(radar_beam_height(radar_range))
  134. << std::endl << std::endl;
  135. return 0;
  136. }