]> git.mxchange.org Git - flightgear.git/blobdiff - src/Radio/radio.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / Radio / radio.cxx
index e8133b0aabe4c2ec0392b80e0f8f0dd93a021c5b..169cd322f19bb650c25806fde84f76d8fde3d46b 100644 (file)
@@ -29,6 +29,7 @@
 #include "radio.hxx"
 #include <simgear/scene/material/mat.hxx>
 #include <Scenery/scenery.hxx>
+#include <boost/scoped_array.hpp>
 
 #define WITH_POINT_TO_POINT 1
 #include "itm.cpp"
@@ -210,7 +211,8 @@ void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, in
 double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
 
        
-       
+       if((freq < 40.0) || (freq > 20000.0))   // frequency out of recommended range 
+               return -1;
        /** ITM default parameters 
                TODO: take them from tile materials (especially for sea)?
        **/
@@ -288,7 +290,7 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i
        
                
        int max_points = (int)floor(distance_m / point_distance);
-       double delta_last = fmod(distance_m, point_distance);
+       //double delta_last = fmod(distance_m, point_distance);
        
        deque<double> elevations;
        deque<string*> materials;
@@ -382,8 +384,7 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i
        elevations.push_front(num_points -1);
 
        int size = elevations.size();
-       double *itm_elev;
-       itm_elev = new double[size];
+        boost::scoped_array<double> itm_elev( new double[size] );
 
        for(int i=0;i<size;i++) {
                itm_elev[i]=elevations[i];
@@ -391,18 +392,18 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i
        
        if((transmission_type == 3) || (transmission_type == 4)) {
                // the sender and receiver roles are switched
-               ITM::point_to_point(itm_elev, receiver_height, transmitter_height,
+               ITM::point_to_point(itm_elev.get(), receiver_height, transmitter_height,
                        eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
                        pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
                if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
-                       calculate_clutter_loss(frq_mhz, itm_elev, materials, receiver_height, transmitter_height, p_mode, horizons, clutter_loss);
+                       calculate_clutter_loss(frq_mhz, itm_elev.get(), materials, receiver_height, transmitter_height, p_mode, horizons, clutter_loss);
        }
        else {
-               ITM::point_to_point(itm_elev, transmitter_height, receiver_height,
+               ITM::point_to_point(itm_elev.get(), transmitter_height, receiver_height,
                        eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
                        pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
                if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
-                       calculate_clutter_loss(frq_mhz, itm_elev, materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
+                       calculate_clutter_loss(frq_mhz, itm_elev.get(), materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
        }
        
        double pol_loss = 0.0;
@@ -457,7 +458,6 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i
        //_root_node->setDoubleValue("station[0]/tx-pattern-gain", tx_pattern_gain);
        //_root_node->setDoubleValue("station[0]/rx-pattern-gain", rx_pattern_gain);
 
-       delete[] itm_elev;
        for (unsigned i =0; i < materials.size(); i++) {
                delete materials[i];
        }
@@ -480,7 +480,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                        
                        double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                        double clutter_density = 0.0;   // percent of reflected wave
-                       if((unsigned)mat >= mat_size) {
+                       if((unsigned)mat >= mat_size) { //this tends to happen when the model interferes with the antenna (obstructs)
+                               //cerr << "Array index out of bounds 0-0: " << mat << " size: " << mat_size << endl;
                                break;
                        }
                        get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -488,7 +489,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                        double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
                        // First Fresnel radius
                        double frs_rad = 548 * sqrt( (j * itm_elev[1] * (itm_elev[0] - j) * itm_elev[1] / 1000000) / (  distance_m * freq / 1000) );
-                       
+                       if (frs_rad <= 0.0) {   //this tends to happen when the model interferes with the antenna (obstructs)
+                               //cerr << "Frs rad 0-0: " << frs_rad << endl;
+                               continue;
+                       }
                        //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
                        
                        double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
@@ -535,7 +539,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                                double clutter_density = 0.0;   // percent of reflected wave
                                
-                               if((unsigned)mat >= mat_size) {
+                               if((unsigned)mat >= mat_size) {         
+                                       //cerr << "Array index out of bounds 1-1: " << mat << " size: " << mat_size << endl;
                                        break;
                                }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -543,7 +548,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
                                // First Fresnel radius
                                double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / ( num_points_1st * itm_elev[1] * freq / 1000) );
-                               
+                               if (frs_rad <= 0.0) {   
+                                       //cerr << "Frs rad 1-1: " << frs_rad << endl;
+                                       continue;
+                               }
                                //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
                                
                                double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
@@ -583,7 +591,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                                double clutter_density = 0.0;   // percent of reflected wave
                                
-                               if((unsigned)mat >= mat_size) {
+                               if((unsigned)mat >= mat_size) {         
+                                       //cerr << "Array index out of bounds 1-2: " << mat << " size: " << mat_size << endl;
                                        break;
                                }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -591,7 +600,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
                                // First Fresnel radius
                                double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / (  num_points_2nd * itm_elev[1] * freq / 1000) );
-                               
+                               if (frs_rad <= 0.0) {   
+                                       //cerr << "Frs rad 1-2: " << frs_rad << " numpoints2 " << num_points_2nd << " j: " << j << endl;
+                                       continue;
+                               }
                                //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
                                
                                double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
@@ -638,7 +650,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                        break;
                                double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                                double clutter_density = 0.0;   // percent of reflected wave
-                               if((unsigned)mat >= mat_size) {
+                               if((unsigned)mat >= mat_size) {         
+                                       //cerr << "Array index out of bounds 2-1: " << mat << " size: " << mat_size << endl;
                                        break;
                                }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -646,7 +659,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
                                // First Fresnel radius
                                double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / (  num_points_1st * itm_elev[1] * freq / 1000) );
-                               
+                               if (frs_rad <= 0.0) {           
+                                       //cerr << "Frs rad 2-1: " << frs_rad << " numpoints1 " << num_points_1st << " j: " << j << endl;
+                                       continue;
+                               }
                                //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
                                
                                double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
@@ -685,7 +701,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                        break;
                                double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                                double clutter_density = 0.0;   // percent of reflected wave
-                               if((unsigned)mat >= mat_size) {
+                               if((unsigned)mat >= mat_size) {         
+                                       //cerr << "Array index out of bounds 2-2: " << mat << " size: " << mat_size << endl;
                                        break;
                                }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -693,7 +710,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) / distance_m;
                                // First Fresnel radius
                                double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / (  num_points_2nd * itm_elev[1] * freq / 1000) );
-                               
+                               if (frs_rad <= 0.0) {   
+                                       //cerr << "Frs rad 2-2: " << frs_rad << " numpoints2 " << num_points_2nd << " j: " << j << endl;
+                                       continue;
+                               }
                                //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
                                
                                double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[num_points_1st + num_points_2nd +2] + clutter_height);
@@ -732,7 +752,8 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                        break;
                                double clutter_height = 0.0;    // mean clutter height for a certain terrain type
                                double clutter_density = 0.0;   // percent of reflected wave
-                               if((unsigned)mat >= mat_size) {
+                               if((unsigned)mat >= mat_size) {         
+                                       //cerr << "Array index out of bounds 2-3: " << mat << " size: " << mat_size << endl;
                                        break;
                                }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
@@ -740,7 +761,10 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                double grad = fabs(itm_elev[last2+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
                                // First Fresnel radius
                                double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_3rd - j) * itm_elev[1] / 1000000) / (  num_points_3rd * itm_elev[1] * freq / 1000) );
-                               
+                               if (frs_rad <= 0.0) {           
+                                       //cerr << "Frs rad 2-3: " << frs_rad << " numpoints3 " << num_points_3rd << " j: " << j << endl;
+                                       continue;
+                               }
                                
                                //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3