]> git.mxchange.org Git - flightgear.git/blob - src/Radio/antenna.cxx
Clean-up some SGMath dependencies.
[flightgear.git] / src / Radio / antenna.cxx
1 // antenna.cxx -- implementation of FGRadioAntenna
2 // Class to represent a virtual radio antenna properties 
3 // Written by Adrian Musceac YO8RZZ, started December 2011.
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <math.h>
25 #include <iostream>
26 #include <stdlib.h>
27 #include <fstream>
28 #include <Scenery/scenery.hxx>
29 #include "antenna.hxx"
30
31 using namespace std;
32
33 FGRadioAntenna::FGRadioAntenna(string type) {
34         
35         _mirror_y = 1;  // normally we want to mirror these axis because the pattern is simetric
36         _mirror_z = 1;
37         _invert_ground = 0;             // TODO: use for inverting the antenna ground, for instance aircraft body reflection
38         load_NEC_antenna_pattern(type);
39 }
40
41 FGRadioAntenna::~FGRadioAntenna() {
42         for (unsigned i =0; i < _pattern.size(); i++) {
43                 AntennaGain *point_gain = _pattern[i];
44                 delete point_gain;
45         }
46         _pattern.clear();
47 }
48
49 // WIP
50 double FGRadioAntenna::calculate_gain(double bearing, double angle) {
51         
52         // TODO: what if the pattern is assimetric?
53         bearing = fabs(bearing);
54         if (bearing > 180)
55                 bearing = 360 - bearing;
56         // for plots with 2 degrees resolution:
57         int azimuth = (int)floor(bearing);
58         azimuth += azimuth % 2;
59         int elevation = (int)floor(angle);
60         elevation += elevation % 2;
61         //cerr << "Bearing: " << bearing << " angle: " << angle << " azimuth: " << azimuth << " elevation: " << elevation << endl;
62         for (unsigned i =0; i < _pattern.size(); i++) {
63                 AntennaGain *point_gain = _pattern[i];
64                 
65                 if ( (azimuth == point_gain->azimuth) && (elevation == point_gain->elevation)) {
66                         return point_gain->gain;
67                 }
68         }
69                 
70         return 0;
71 }
72
73
74 void FGRadioAntenna::load_NEC_antenna_pattern(string type) {
75         
76         //SGPath pattern_file(fgGetString("/sim/fg-home"));
77         SGPath pattern_file(globals->get_fg_root());
78         pattern_file.append("Navaids/Antennas");
79         pattern_file.append(type + ".txt");
80         if (!pattern_file.exists()) {
81                 return;
82         }
83         ifstream file_in(pattern_file.c_str());
84         int heading, elevation;
85         double gain;
86         while(!file_in.eof()) {
87                 file_in >> heading >> elevation >> gain;
88                 if( (_mirror_y == 1) && (heading > 180) ) {
89                         continue;
90                 }
91                 if ( (_mirror_z == 1) && (elevation < 0) ) {
92                         continue;
93                 }
94                 //cerr << "head: " << heading << " elev: " << elevation << " gain: " << gain << endl;
95                 AntennaGain *datapoint = new AntennaGain;
96                 datapoint->azimuth = heading;
97                 datapoint->elevation = 90.0 - abs(elevation);
98                 datapoint->gain = gain;
99                 _pattern.push_back(datapoint);
100         }
101 }