]> 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 2c983285fd8754f1500025a46e4d521deb0d8736..169cd322f19bb650c25806fde84f76d8fde3d46b 100644 (file)
@@ -1,6 +1,6 @@
 // radio.cxx -- implementation of FGRadio
 // Class to manage radio propagation using the ITM model
-// Written by Adrian Musceac, started August 2011.
+// Written by Adrian Musceac YO8RZZ, started August 2011.
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -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,10 +290,10 @@ 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;
+       deque<string*> materials;
        
 
        double elevation_under_pilot = 0.0;
@@ -329,42 +331,48 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i
                                elevations.push_back(elevation_m);
                                if(mat) {
                                        const std::vector<string> mat_names = mat->get_names();
-                                       materials.push_back(mat_names[0]);
+                                       string* name = new string(mat_names[0]);
+                                       materials.push_back(name);
                                }
                                else {
-                                       materials.push_back("None");
+                                       string* no_material = new string("None"); 
+                                       materials.push_back(no_material);
                                }
                        }
                        else {
                                 elevations.push_front(elevation_m);
                                 if(mat) {
                                         const std::vector<string> mat_names = mat->get_names();
-                                        materials.push_front(mat_names[0]);
+                                        string* name = new string(mat_names[0]);
+                                        materials.push_front(name);
                                }
                                else {
-                                       materials.push_front("None");
+                                       string* no_material = new string("None"); 
+                                       materials.push_front(no_material);
                                }
                        }
                }
                else {
                        if((transmission_type == 3) || (transmission_type == 4)) {
                                elevations.push_back(0.0);
-                               materials.push_back("None");
+                               string* no_material = new string("None"); 
+                               materials.push_back(no_material);
                        }
                        else {
+                               string* no_material = new string("None"); 
                                elevations.push_front(0.0);
-                               materials.push_front("None");
+                               materials.push_front(no_material);
                        }
                }
        }
        if((transmission_type == 3) || (transmission_type == 4)) {
                elevations.push_front(elevation_under_pilot);
-               if (delta_last > (point_distance / 2) )                 // only add last point if it's farther than half point_distance
+               //if (delta_last > (point_distance / 2) )                       // only add last point if it's farther than half point_distance
                        elevations.push_back(elevation_under_sender);
        }
        else {
                elevations.push_back(elevation_under_pilot);
-               if (delta_last > (point_distance / 2) )
+               //if (delta_last > (point_distance / 2) )
                        elevations.push_front(elevation_under_sender);
        }
        
@@ -376,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];
@@ -385,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
-               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 {
-               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;
@@ -451,19 +458,21 @@ 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];
+       }
+       
        return signal;
 
 }
 
 
-void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[], deque<string> &materials,
+void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[], deque<string*> &materials,
        double transmitter_height, double receiver_height, int p_mode,
        double horizons[], double &clutter_loss) {
        
        double distance_m = itm_elev[0] * itm_elev[1]; // only consider elevation points
-       
+       unsigned mat_size = materials.size();
        if (p_mode == 0) {      // LOS: take each point and see how clutter height affects first Fresnel zone
                int mat = 0;
                int j=1; 
@@ -471,12 +480,19 @@ 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) { //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);
                        
                        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);
@@ -522,12 +538,20 @@ 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) {         
+                                       //cerr << "Array index out of bounds 1-1: " << mat << " size: " << mat_size << endl;
+                                       break;
+                               }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
                                
                                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);
@@ -566,12 +590,20 @@ 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) {         
+                                       //cerr << "Array index out of bounds 1-2: " << mat << " size: " << mat_size << endl;
+                                       break;
+                               }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
                                
                                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);
@@ -618,12 +650,19 @@ 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) {         
+                                       //cerr << "Array index out of bounds 2-1: " << mat << " size: " << mat_size << endl;
+                                       break;
+                               }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
                                
                                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);
@@ -650,6 +689,7 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
                                        // no losses
                                }
                                j++;
+                               mat++;
                                last = k;
                        }
                        mat +=1;
@@ -661,12 +701,19 @@ 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) {         
+                                       //cerr << "Array index out of bounds 2-2: " << mat << " size: " << mat_size << endl;
+                                       break;
+                               }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
                                
                                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);
@@ -705,12 +752,19 @@ 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) {         
+                                       //cerr << "Array index out of bounds 2-3: " << mat << " size: " << mat_size << endl;
+                                       break;
+                               }
                                get_material_properties(materials[mat], clutter_height, clutter_density);
                                
                                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
                                
@@ -751,107 +805,110 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[],
 }
 
 
-void FGRadioTransmission::get_material_properties(string mat_name, double &height, double &density) {
+void FGRadioTransmission::get_material_properties(string* mat_name, double &height, double &density) {
+       
+       if(!mat_name)
+               return;
        
-       if(mat_name == "Landmass") {
+       if(*mat_name == "Landmass") {
                height = 15.0;
                density = 0.2;
        }
 
-       else if(mat_name == "SomeSort") {
+       else if(*mat_name == "SomeSort") {
                height = 15.0;
                density = 0.2;
        }
 
-       else if(mat_name == "Island") {
+       else if(*mat_name == "Island") {
                height = 15.0;
                density = 0.2;
        }
-       else if(mat_name == "Default") {
+       else if(*mat_name == "Default") {
                height = 15.0;
                density = 0.2;
        }
-       else if(mat_name == "EvergreenBroadCover") {
+       else if(*mat_name == "EvergreenBroadCover") {
                height = 20.0;
                density = 0.2;
        }
-       else if(mat_name == "EvergreenForest") {
+       else if(*mat_name == "EvergreenForest") {
                height = 20.0;
                density = 0.2;
        }
-       else if(mat_name == "DeciduousBroadCover") {
+       else if(*mat_name == "DeciduousBroadCover") {
                height = 15.0;
                density = 0.3;
        }
-       else if(mat_name == "DeciduousForest") {
+       else if(*mat_name == "DeciduousForest") {
                height = 15.0;
                density = 0.3;
        }
-       else if(mat_name == "MixedForestCover") {
+       else if(*mat_name == "MixedForestCover") {
                height = 20.0;
                density = 0.25;
        }
-       else if(mat_name == "MixedForest") {
+       else if(*mat_name == "MixedForest") {
                height = 15.0;
                density = 0.25;
        }
-       else if(mat_name == "RainForest") {
+       else if(*mat_name == "RainForest") {
                height = 25.0;
                density = 0.55;
        }
-       else if(mat_name == "EvergreenNeedleCover") {
+       else if(*mat_name == "EvergreenNeedleCover") {
                height = 15.0;
                density = 0.2;
        }
-       else if(mat_name == "WoodedTundraCover") {
+       else if(*mat_name == "WoodedTundraCover") {
                height = 5.0;
                density = 0.15;
        }
-       else if(mat_name == "DeciduousNeedleCover") {
+       else if(*mat_name == "DeciduousNeedleCover") {
                height = 5.0;
                density = 0.2;
        }
-       else if(mat_name == "ScrubCover") {
+       else if(*mat_name == "ScrubCover") {
                height = 3.0;
                density = 0.15;
        }
-       else if(mat_name == "BuiltUpCover") {
+       else if(*mat_name == "BuiltUpCover") {
                height = 30.0;
                density = 0.7;
        }
-       else if(mat_name == "Urban") {
+       else if(*mat_name == "Urban") {
                height = 30.0;
                density = 0.7;
        }
-       else if(mat_name == "Construction") {
+       else if(*mat_name == "Construction") {
                height = 30.0;
                density = 0.7;
        }
-       else if(mat_name == "Industrial") {
+       else if(*mat_name == "Industrial") {
                height = 30.0;
                density = 0.7;
        }
-       else if(mat_name == "Port") {
+       else if(*mat_name == "Port") {
                height = 30.0;
                density = 0.7;
        }
-       else if(mat_name == "Town") {
+       else if(*mat_name == "Town") {
                height = 10.0;
                density = 0.5;
        }
-       else if(mat_name == "SubUrban") {
+       else if(*mat_name == "SubUrban") {
                height = 10.0;
                density = 0.5;
        }
-       else if(mat_name == "CropWoodCover") {
+       else if(*mat_name == "CropWoodCover") {
                height = 10.0;
                density = 0.1;
        }
-       else if(mat_name == "CropWood") {
+       else if(*mat_name == "CropWood") {
                height = 10.0;
                density = 0.1;
        }
-       else if(mat_name == "AgroForest") {
+       else if(*mat_name == "AgroForest") {
                height = 10.0;
                density = 0.1;
        }