From dd6588d4f03f1a6bc365c92000427642731cfaa5 Mon Sep 17 00:00:00 2001
From: adrian <adrian@localhost.com>
Date: Sat, 26 Nov 2011 18:51:33 +0200
Subject: [PATCH] Use landcover to determine ground clutter path loss for LOS 
 modified:   src/Radio/itm.cpp 	modified:   src/Radio/radio.cxx 
 modified:   src/Radio/radio.hxx

---
 src/Radio/itm.cpp   |  32 +++++--
 src/Radio/radio.cxx | 223 ++++++++++++++++++++++++++++++++++++++++++--
 src/Radio/radio.hxx |   8 +-
 3 files changed, 248 insertions(+), 15 deletions(-)

diff --git a/src/Radio/itm.cpp b/src/Radio/itm.cpp
index 9c2f010fc..79360a201 100644
--- a/src/Radio/itm.cpp
+++ b/src/Radio/itm.cpp
@@ -1519,6 +1519,8 @@ void point_to_point(double elev[],
                     double rel,                // 0.01 .. .99, Fractions of time
                     double &dbloss,
                     char *strmode,
+                    int &p_mode,				// propagation mode selector
+                    double (&horizons)[2],			// horizon distances
                     int &errnum)
 {
 	// radio_climate: 1-Equatorial, 2-Continental Subtropical, 3-Maritime Tropical,
@@ -1568,22 +1570,38 @@ void point_to_point(double elev[],
 	fs = 32.45 + 20.0 * log10(frq_mhz) + 20.0 * log10(prop.d / 1000.0);
 	q = prop.d - prop.d_L;
 
+	horizons[0] = 0.0;
+	horizons[1] = 0.0;
 	if (int(q) < 0.0) {
 		strcpy(strmode, "Line-Of-Sight Mode");
+		p_mode = 0;
 	} else {
-		if (int(q) == 0.0)
+		if (int(q) == 0.0) {
 			strcpy(strmode, "Single Horizon");
+			horizons[0] = prop.d_Lj[0];
+			p_mode = 1;
+		}
 
-		else
-			if (int(q) > 0.0)
+		else {
+			if (int(q) > 0.0) {
 				strcpy(strmode, "Double Horizon");
+				horizons[0] = prop.d_Lj[0];
+				horizons[1] = prop.d_Lj[1];
+				p_mode = 1;
+			}
+		}
 
-		if (prop.d <= prop.d_Ls || prop.d <= prop.dx)
+		if (prop.d <= prop.d_Ls || prop.d <= prop.dx) {
 			strcat(strmode, ", Diffraction Dominant");
+			p_mode = 1;
+		}
 
-		else
-		if (prop.d > prop.dx)
-			strcat(strmode, ", Troposcatter Dominant");
+		else {
+			if (prop.d > prop.dx) {
+				strcat(strmode, ", Troposcatter Dominant");
+				p_mode = 2;
+			}
+		}
 	}
 
 	dbloss = avar(zr, 0.0, zc, prop, propv) + fs;
diff --git a/src/Radio/radio.cxx b/src/Radio/radio.cxx
index fb73dffed..72fdb15d8 100644
--- a/src/Radio/radio.cxx
+++ b/src/Radio/radio.cxx
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <deque>
 #include "radio.hxx"
+#include <simgear/scene/material/mat.hxx>
 #include <Scenery/scenery.hxx>
 
 #define WITH_POINT_TO_POINT 1
@@ -213,8 +214,11 @@ double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq, int transmiss
 	double rel = 0.90;	
 	double dbloss;
 	char strmode[150];
+	int p_mode = 0; // propgation mode selector: 0 LOS, 1 diffraction dominant, 2 troposcatter
+	double horizons[2];
 	int errnum;
 	
+	double clutter_loss; 	// loss due to vegetation and urban
 	double tx_pow = _transmitter_power;
 	double ant_gain = _antenna_gain;
 	double signal = 0.0;
@@ -278,6 +282,8 @@ double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq, int transmiss
 		
 	double max_points = distance_m / point_distance;
 	deque<double> _elevations;
+	deque<string> materials;
+	
 
 	double elevation_under_pilot = 0.0;
 	if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
@@ -306,23 +312,39 @@ double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq, int transmiss
 	while (_elevations.size() <= e_size) {
 		probe_distance += point_distance;
 		SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
-		
+		const SGMaterial *mat = 0;
 		double elevation_m = 0.0;
 	
-		if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
+		if (scenery->get_elevation_m( probe, elevation_m, &mat )) {
 			if((transmission_type == 3) || (transmission_type == 4)) {
 				_elevations.push_back(elevation_m);
+				if(mat) {
+					const std::vector<string> mat_names = mat->get_names();
+					materials.push_back(mat_names[0]);
+				}
+				else {
+					materials.push_back("None");
+				}
 			}
 			else {
 				 _elevations.push_front(elevation_m);
+				 if(mat) {
+				 	 const std::vector<string> mat_names = mat->get_names();
+				 	 materials.push_front(mat_names[0]);
+				}
+				else {
+					materials.push_front("None");
+				}
 			}
 		}
 		else {
 			if((transmission_type == 3) || (transmission_type == 4)) {
-				_elevations.push_back(elevation_m);
+				_elevations.push_back(0.0);
+				materials.push_back("None");
 			}
 			else {
-			_elevations.push_front(0.0);
+				_elevations.push_front(0.0);
+				materials.push_front("None");
 			}
 		}
 	}
@@ -367,25 +389,205 @@ double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq, int transmiss
 		// the sender and receiver roles are switched
 		point_to_point(itm_elev, receiver_height, transmitter_height,
 			eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
-			pol, conf, rel, dbloss, strmode, errnum);
+			pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
 		
 	}
 	else {
 		point_to_point(itm_elev, transmitter_height, receiver_height,
 			eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
-			pol, conf, rel, dbloss, strmode, errnum);
+			pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
 	}
 	SG_LOG(SG_GENERAL, SG_BULK,
 			"ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
 	cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
 	
+	clutterLoss(frq_mhz, distance_m, itm_elev, materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
+	cerr << "Clutter loss: " << clutter_loss << endl;
 	//if (errnum == 4)	// if parameters are outside sane values for lrprop, the alternative method is used
 	//	return -1;
-	signal = link_budget - dbloss;
+	signal = link_budget - dbloss - clutter_loss;
 	return signal;
 
 }
 
+/*** Calculate losses due to vegetation and urban clutter (WIP)
+*	 We are only worried about clutter loss, terrain influence 
+*	 on the first Fresnel zone is calculated in the ITM functions
+***/
+void FGRadio::clutterLoss(double freq, double distance_m, double itm_elev[], deque<string> materials,
+	double transmitter_height, double receiver_height, int p_mode,
+	double horizons[], double &clutter_loss) {
+	
+	if (p_mode == 0) {	// LOS: take each point and see how clutter height affects first Fresnel zone
+		int j=1; // first point is TX elevation, last is RX elevation
+		for (int k=3;k < (int)itm_elev[0];k++) {
+			
+			double clutter_height = 0.0;	// clutter height hard-coded to 15 for now
+			double clutter_density = 0.0;	// percent of reflected wave
+			get_material_properties(materials[j-1], clutter_height, clutter_density);
+			//cerr << "Clutter:: material: " << materials[j-1] << " height: " << clutter_height << ", density: " << clutter_density << endl;
+			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) );
+			//cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
+			//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);
+			double d1 = j * itm_elev[1];
+			if (fabs(min_elev - itm_elev[2]) <= 0.0001) 
+				d1 = (itm_elev[0] - j) * itm_elev[1];
+			double ray_height = (grad * d1) + min_elev;
+			//cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
+			double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad;		
+			double intrusion = fabs(clearance);
+			//cerr << "Clutter:: clearance: " << clearance << endl;
+			if (clearance >= 0) {
+				clutter_loss +=0.0;
+			}
+			else if (clearance < 0 && (intrusion < clutter_height)) {
+				
+				clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
+			}
+			else if (clearance < 0 && (intrusion > clutter_height)) {
+				clutter_loss += clutter_density * (clutter_height / (frs_rad *2 ) ) * freq/100;
+			}
+			else {
+				clutter_loss += 0.0;
+			}
+			j++;
+		}
+		
+	}
+	else if (p_mode == 1) {		// diffraction
+		
+		if (horizons[1] == 0.0) {	//	single horizon: same as above, except pass twice using the highest point
+			
+		}
+		else {	// double horizon: same as single horizon, except there are 3 segments
+			
+		}
+	}
+	else if (p_mode == 2) {		//	troposcatter: use the first smooth earth horizon as mid point
+		
+	}
+	
+}
+
+/*** 	Material properties database
+*		height: median clutter height
+*		density: radiowave attenuation factor
+***/
+void FGRadio::get_material_properties(string mat_name, double &height, double &density) {
+	
+	if(mat_name == "Landmass") {
+		height = 15.0;
+		density = 0.2;
+	}
+
+	else if(mat_name == "SomeSort") {
+		height = 15.0;
+		density = 0.2;
+	}
+
+	else if(mat_name == "Island") {
+		height = 15.0;
+		density = 0.2;
+	}
+	else if(mat_name == "Default") {
+		height = 15.0;
+		density = 0.2;
+	}
+	else if(mat_name == "EvergreenBroadCover") {
+		height = 20.0;
+		density = 0.2;
+	}
+	else if(mat_name == "EvergreenForest") {
+		height = 20.0;
+		density = 0.2;
+	}
+	else if(mat_name == "DeciduousBroadCover") {
+		height = 15.0;
+		density = 0.3;
+	}
+	else if(mat_name == "DeciduousForest") {
+		height = 15.0;
+		density = 0.3;
+	}
+	else if(mat_name == "MixedForestCover") {
+		height = 20.0;
+		density = 0.25;
+	}
+	else if(mat_name == "MixedForest") {
+		height = 15.0;
+		density = 0.25;
+	}
+	else if(mat_name == "RainForest") {
+		height = 25.0;
+		density = 0.55;
+	}
+	else if(mat_name == "EvergreenNeedleCover") {
+		height = 15.0;
+		density = 0.2;
+	}
+	else if(mat_name == "WoodedTundraCover") {
+		height = 5.0;
+		density = 0.15;
+	}
+	else if(mat_name == "DeciduousNeedleCover") {
+		height = 5.0;
+		density = 0.2;
+	}
+	else if(mat_name == "ScrubCover") {
+		height = 3.0;
+		density = 0.15;
+	}
+	else if(mat_name == "BuiltUpCover") {
+		height = 30.0;
+		density = 0.7;
+	}
+	else if(mat_name == "Urban") {
+		height = 30.0;
+		density = 0.7;
+	}
+	else if(mat_name == "Construction") {
+		height = 30.0;
+		density = 0.7;
+	}
+	else if(mat_name == "Industrial") {
+		height = 30.0;
+		density = 0.7;
+	}
+	else if(mat_name == "Port") {
+		height = 30.0;
+		density = 0.7;
+	}
+	else if(mat_name == "Town") {
+		height = 10.0;
+		density = 0.5;
+	}
+	else if(mat_name == "SubUrban") {
+		height = 10.0;
+		density = 0.5;
+	}
+	else if(mat_name == "CropWoodCover") {
+		height = 10.0;
+		density = 0.1;
+	}
+	else if(mat_name == "CropWood") {
+		height = 10.0;
+		density = 0.1;
+	}
+	else if(mat_name == "AgroForest") {
+		height = 10.0;
+		density = 0.1;
+	}
+	else {
+		height = 0.0;
+		density = 0.0;
+	}
+	
+}
+
 /*** implement simple LOS propagation model (WIP)
 ***/
 double FGRadio::LOS_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
@@ -453,3 +655,10 @@ double FGRadio::LOS_calculate_attenuation(SGGeod pos, double freq, int transmiss
 	return signal;
 	
 }
+
+/*** Material properties database
+***/
+void FGRadio::set_material_properties() {
+	
+	
+}
diff --git a/src/Radio/radio.hxx b/src/Radio/radio.hxx
index 6f37a5201..d6cc77e82 100644
--- a/src/Radio/radio.hxx
+++ b/src/Radio/radio.hxx
@@ -23,7 +23,7 @@
 
 #include <simgear/compiler.h>
 #include <simgear/structure/subsystem_mgr.hxx>
-
+#include <deque>
 #include <Main/fg_props.hxx>
 
 #include <simgear/math/sg_geodesy.hxx>
@@ -43,10 +43,16 @@ private:
 	double _receiver_sensitivity;
 	double _transmitter_power;
 	double _antenna_gain;
+	std::map<string, double[2]> _mat_database;
 	
 	int _propagation_model; /// 0 none, 1 round Earth, 2 ITM
 	double ITM_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air);
 	double LOS_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air);
+	void clutterLoss(double freq, double distance_m, double itm_elev[], std::deque<string> materials,
+			double transmitter_height, double receiver_height, int p_mode,
+			double horizons[], double &clutter_loss);
+	void set_material_properties();
+	void get_material_properties(string mat_name, double &height, double &density);
 	
 public: