// Boost.Units - A C++ library for zero-overhead dimensional analysis and // unit/quantity manipulation and conversion // // Copyright (C) 2003-2008 Matthias Christian Schabel // Copyright (C) 2008 Steven Watanabe // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) /** \file \brief radar_beam_height.cpp \detailed Demonstrate library usage for user test cases suggested by Michael Fawcett. Output: @verbatim //[radar_beam_height_output radar range : 300 nmi earth radius : 6.37101e+06 m beam height 1 : 18169.7 m beam height 2 : 9.81085 nmi beam height 3 : 18169.7 m beam height 4 : 9.81085 nmi beam height approx : 59488.4 ft beam height approx : 18132.1 m //] @endverbatim **/ #include #include #include #include #include #include using boost::units::length_dimension; using boost::units::pow; using boost::units::root; using boost::units::quantity; using boost::units::unit; //[radar_beam_height_class_snippet_1 namespace nautical { struct length_base_unit : boost::units::base_unit { static std::string name() { return "nautical mile"; } static std::string symbol() { return "nmi"; } }; typedef boost::units::make_system::type system; /// unit typedefs typedef unit length; static const length mile,miles; } // namespace nautical // helper for conversions between nautical length and si length BOOST_UNITS_DEFINE_CONVERSION_FACTOR(nautical::length_base_unit, boost::units::si::meter_base_unit, double, 1.852e3); //] //[radar_beam_height_class_snippet_2 namespace imperial { struct length_base_unit : boost::units::base_unit { static std::string name() { return "foot"; } static std::string symbol() { return "ft"; } }; typedef boost::units::make_system::type system; /// unit typedefs typedef unit length; static const length foot,feet; } // imperial // helper for conversions between imperial length and si length BOOST_UNITS_DEFINE_CONVERSION_FACTOR(imperial::length_base_unit, boost::units::si::meter_base_unit, double, 1.0/3.28083989501312); //] // radar beam height functions //[radar_beam_height_function_snippet_1 template quantity,T> radar_beam_height(const quantity,T>& radar_range, const quantity,T>& earth_radius, T k = 4.0/3.0) { return quantity,T> (pow<2>(radar_range)/(2.0*k*earth_radius)); } //] //[radar_beam_height_function_snippet_2 template return_type radar_beam_height(const quantity,T>& radar_range, const quantity,T>& earth_radius, T k = 4.0/3.0) { // need to decide which system to use for calculation const return_type rr(radar_range), er(earth_radius); return return_type(pow<2>(rr)/(2.0*k*er)); } //] //[radar_beam_height_function_snippet_3 quantity radar_beam_height(const quantity& range) { return quantity (pow<2>(range/(1.23*nautical::miles/root<2>(imperial::feet)))); } //] int main(void) { using namespace boost::units; using namespace boost::units::si; using namespace nautical; //[radar_beam_height_snippet_1 const quantity radar_range(300.0*miles); const quantity earth_radius(6371.0087714*kilo*meters); const quantity beam_height_1(radar_beam_height(quantity(radar_range),earth_radius)); const quantity beam_height_2(radar_beam_height(radar_range,quantity(earth_radius))); const quantity beam_height_3(radar_beam_height< quantity >(radar_range,earth_radius)); const quantity beam_height_4(radar_beam_height< quantity >(radar_range,earth_radius)); //] std::cout << "radar range : " << radar_range << std::endl << "earth radius : " << earth_radius << std::endl << "beam height 1 : " << beam_height_1 << std::endl << "beam height 2 : " << beam_height_2 << std::endl << "beam height 3 : " << beam_height_3 << std::endl << "beam height 4 : " << beam_height_4 << std::endl << "beam height approx : " << radar_beam_height(radar_range) << std::endl << "beam height approx : " << quantity(radar_beam_height(radar_range)) << std::endl << std::endl; return 0; }