]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/wxradar.cxx
Add Mach/TAS computation to the airspeed indicator.
[flightgear.git] / src / Instrumentation / wxradar.cxx
index a97a6fb738224ef6a2419b8ac29a9eef2556f0b4..8a27057fed894c78446c06fcce9c3ab7602b9380 100644 (file)
@@ -57,20 +57,16 @@ using std::setfill;
 #include <Main/globals.hxx>
 #include <Cockpit/panel.hxx>
 #include <Cockpit/hud.hxx>
-#include <AIModel/AIBase.hxx>
-#include <AIModel/AIManager.hxx>
-#include <AIModel/AIBallistic.hxx>
 
 #include <Include/general.hxx>
 #include "instrument_mgr.hxx"
 #include "od_gauge.hxx"
 #include "wxradar.hxx"
 
+#include <iostream>             // for cout, endl
 
-typedef list <osg::ref_ptr<FGAIBase> > radar_list_type;
-typedef radar_list_type::iterator radar_list_iterator;
-typedef radar_list_type::const_iterator radar_list_const_iterator;
-
+using std::cout;
+using std::endl;
 
 static const float UNIT = 1.0f / 8.0f;  // 8 symbols in a row/column in the texture
 static const char *DEFAULT_FONT = "typewriter.txf";
@@ -79,11 +75,12 @@ static const char *DEFAULT_FONT = "typewriter.txf";
 wxRadarBg::wxRadarBg(SGPropertyNode *node) :
     _name(node->getStringValue("name", "radar")),
     _num(node->getIntValue("number", 0)),
-    _interval(node->getDoubleValue("update-interval-sec", 1.0)),
     _time(0.0),
+    _interval(node->getDoubleValue("update-interval-sec", 1.0)),
     _sim_init_done(false),
     _odg(0),
     _last_switchKnob("off"),
+    _antenna_ht(node->getDoubleValue("antenna-ht-ft", 0.0)),
     _resultTexture(0),
     _wxEcho(0)
 {
@@ -123,12 +120,12 @@ wxRadarBg::init ()
 
     // texture name to use in 2D and 3D instruments
     _texture_path = _Instrument->getStringValue("radar-texture-path",
-            "Aircraft/Instruments/Textures/od_wxradar.rgb");
+        "Aircraft/Instruments/Textures/od_wxradar.rgb");
     _resultTexture = FGTextureManager::createTexture(_texture_path.c_str(), false);
 
     SGPath tpath(globals->get_fg_root());
     string path = _Instrument->getStringValue("echo-texture-path",
-            "Aircraft/Instruments/Textures/wxecho.rgb");
+        "Aircraft/Instruments/Textures/wxecho.rgb");
     tpath.append(path);
 
     // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
@@ -153,7 +150,6 @@ wxRadarBg::init ()
     _odg = (FGODGauge *)imgr->get_subsystem("od_gauge");
     _odg->setSize(512);
 
-    _ai = (FGAIManager*)globals->get_subsystem("ai_model");
     _ai_enabled_node = fgGetNode("/sim/ai/enabled", true);
 
     _user_lat_node = fgGetNode("/position/latitude-deg", true);
@@ -227,7 +223,7 @@ wxRadarBg::init ()
     pset->setDataVariance(osg::Object::DYNAMIC);
     _geom->addPrimitiveSet(pset);
     _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
-            osg::Vec3f(256.0f, 256.0f, 0.0f)));
+        osg::Vec3f(256.0f, 256.0f, 0.0f)));
     _radarGeode->addDrawable(_geom);
     _odg->allocRT();
     // Texture in the 2D panel system
@@ -289,8 +285,11 @@ wxRadarBg::update (double delta_time_sec)
         return;
     }
     _time += delta_time_sec;
-    if (_time < _interval)
+    if (_time < _interval){
+//        cout << "WXradar update too soon " << _time << endl;
         return;
+    }
+//        cout << "WXradar updating" << _time<< endl;
 
     _time = 0.0;
 
@@ -301,7 +300,9 @@ wxRadarBg::update (double delta_time_sec)
             center_map();
         }
     } else if (mode == "plan") {
-        _display_mode = PLAN;
+        _display_mode = PLAN;}
+    else if (mode == "bscan") {
+        _display_mode = BSCAN;
     } else {
         _display_mode = ARC;
     }
@@ -366,6 +367,8 @@ wxRadarBg::update (double delta_time_sec)
             if (_radar_rotate_node->getBoolValue()) {
                 _angle_offset = -_view_heading;
             }
+        } else if (_display_mode == BSCAN) {
+            _angle_offset = -_view_heading;
         } else {
             // rose
         }
@@ -379,25 +382,25 @@ wxRadarBg::update (double delta_time_sec)
 
 
         osg::DrawArrays *quadPSet
-                = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
+            = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
         quadPSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size());
         quadPSet->dirty();
 
         // erase what is out of sight of antenna
         /*
-            |\     /|
-            | \   / |
-            |  \ /  |
-            ---------
-            |       |
-            |       |
-            ---------
+        |\     /|
+        | \   / |
+        |  \ /  |
+        ---------
+        |       |
+        |       |
+        ---------
         */
 
         osg::DrawArrays *maskPSet
-                = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
+            = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
         osg::DrawArrays *trimaskPSet
-                = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(2));
+            = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(2));
 
         if (_display_mode == ARC) {
             float xOffset = 256.0f;
@@ -466,12 +469,12 @@ wxRadarBg::update (double delta_time_sec)
 void
 wxRadarBg::update_weather()
 {
-    string modeButton = _Instrument->getStringValue("mode", "wx");
+    string modeButton = _Instrument->getStringValue("mode", "WX");
     _radarEchoBuffer = *sgEnviro.get_radar_echo();
 
     // pretend we have a scan angle bigger then the FOV
     // TODO:check real fov, enlarge if < nn, and do clipping if > mm
-    const float fovFactor = 1.45f;
+//    const float fovFactor = 1.45f;
     _Instrument->setStringValue("status", modeButton.c_str());
 
     list_of_SGWxRadarEcho *radarEcho = &_radarEchoBuffer;
@@ -506,7 +509,7 @@ wxRadarBg::update_weather()
                     continue;
 
                 float angle = (iradarEcho->heading - _angle_offset) //* fovFactor
-                        + 0.5 * SG_PI;
+                    + 0.5 * SG_PI;
 
                 // Rotate echo into position, and rotate echo to have
                 // a constant orientation towards the
@@ -516,8 +519,8 @@ wxRadarBg::update_weather()
                 const osg::Vec2f texBase(col, (UNIT * (float) (4 + (cloudId & 3))));
 
                 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-                        * osg::Matrixf::translate(0.0f, radius, 0.0f)
-                        * wxRotate(angle) * _centerTrans);
+                    * osg::Matrixf::translate(0.0f, radius, 0.0f)
+                    * wxRotate(angle) * _centerTrans);
                 addQuad(_vertices, _texCoords, m, texBase);
 
                 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing clouds"
@@ -544,12 +547,12 @@ wxRadarBg::update_weather()
             float size = UNIT * 0.5f;
             float radius = iradarEcho->dist * _scale;
             float angle = iradarEcho->heading * SG_DEGREES_TO_RADIANS
-                    - _angle_offset;
+                - _angle_offset;
 
             osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-                    * wxRotate(-angle)
-                    * osg::Matrixf::translate(0.0f, radius, 0.0f)
-                    * wxRotate(angle) * _centerTrans);
+                * wxRotate(-angle)
+                * osg::Matrixf::translate(0.0f, radius, 0.0f)
+                * wxRotate(angle) * _centerTrans);
             addQuad(_vertices, _texCoords, m, texBase);
         }
     }
@@ -557,7 +560,8 @@ wxRadarBg::update_weather()
 
 
 void
-wxRadarBg::update_data(FGAIBase *ac, double radius, double bearing, bool selected)
+wxRadarBg::update_data(const SGPropertyNode *ac, double altitude, double heading,
+                       double radius, double bearing, bool selected)
 {
     osgText::Text *callsign = new osgText::Text;
     callsign->setFont(_font.get());
@@ -565,8 +569,8 @@ wxRadarBg::update_data(FGAIBase *ac, double radius, double bearing, bool selecte
     callsign->setCharacterSize(_font_size);
     callsign->setColor(selected ? osg::Vec4(1, 1, 1, 1) : _font_color);
     osg::Matrixf m(wxRotate(-bearing)
-            * osg::Matrixf::translate(0.0f, radius, 0.0f)
-            * wxRotate(bearing) * _centerTrans);
+        * osg::Matrixf::translate(0.0f, radius, 0.0f)
+        * wxRotate(bearing) * _centerTrans);
 
     osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
     // cast to int's, otherwise text comes out ugly
@@ -574,12 +578,16 @@ wxRadarBg::update_data(FGAIBase *ac, double radius, double bearing, bool selecte
     callsign->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE);
     callsign->setLineSpacing(_font_spacing);
 
+    const char *identity = ac->getStringValue("transponder-id");
+    if (!identity[0])
+        identity = ac->getStringValue("callsign");
+
     stringstream text;
-    text << ac->_getCallsign() << endl
-            << setprecision(0) << fixed
-            << setw(3) << setfill('0') << ac->_getHeading() << "\xB0 "
-            << setw(0) << ac->_getAltitude() << "ft" << endl
-            << ac->_getSpeed() << "kts";
+    text << identity << endl
+        << setprecision(0) << fixed
+        << setw(3) << setfill('0') << heading * SG_RADIANS_TO_DEGREES << "\xB0 "
+        << setw(0) << altitude << "ft" << endl
+        << ac->getDoubleValue("velocities/true-airspeed-kt") << "kts";
 
     callsign->setText(text.str());
     _textGeode->addDrawable(callsign);
@@ -589,6 +597,75 @@ wxRadarBg::update_data(FGAIBase *ac, double radius, double bearing, bool selecte
 void
 wxRadarBg::update_aircraft()
 {
+    double diff;
+    double age_factor = 1.0;
+    double test_rng;
+    double test_brg;
+    double range;
+    double bearing;
+    float echo_radius;
+    double angle;
+
+    if (!ground_echoes.empty()){
+        ground_echoes_iterator = ground_echoes.begin();
+
+        while(ground_echoes_iterator != ground_echoes.end()) {
+            diff = _elapsed_time - (*ground_echoes_iterator)->elapsed_time;
+
+            if( diff > _persistance) {
+                ground_echoes.erase(ground_echoes_iterator);
+            } else {
+//                double test_brg = (*ground_echoes_iterator)->bearing;
+//                double bearing = test_brg * SG_DEGREES_TO_RADIANS;
+//                float angle = calcRelBearing(bearing, _view_heading);
+                double bumpinessFactor  = (*ground_echoes_iterator)->bumpiness;
+                float heading = get_heading();
+                if ( _display_mode == BSCAN ){
+                    test_rng = (*ground_echoes_iterator)->elevation * 6;
+                    test_brg = (*ground_echoes_iterator)->bearing;
+                    angle = calcRelBearingDeg(test_brg, heading) * 6;
+                    range = sqrt(test_rng * test_rng + angle * angle);
+                    bearing = atan2(angle, test_rng);
+                    //cout << "angle " << angle <<" bearing "
+                    //    << bearing / SG_DEGREES_TO_RADIANS <<  endl;
+                    echo_radius = (0.1 + (1.9 * bumpinessFactor)) * 240 * age_factor;
+                } else {
+                    test_rng = (*ground_echoes_iterator)->range;
+                    range = test_rng * SG_METER_TO_NM;
+                    test_brg = (*ground_echoes_iterator)->bearing;
+                    bearing = test_brg * SG_DEGREES_TO_RADIANS;
+                    echo_radius = (0.1 + (1.9 * bumpinessFactor)) * 120 * age_factor;
+                    bearing += _angle_offset;
+                }
+
+                float radius = range * _scale;
+                //double heading = 90 * SG_DEGREES_TO_RADIANS;
+                //heading += _angle_offset;
+
+                age_factor = 1;
+
+                if (diff != 0)
+                    age_factor = 1 - (0.5 * diff/_persistance);
+
+                float size = echo_radius * UNIT;
+
+                const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
+                osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
+                    * osg::Matrixf::translate(0.0f, radius, 0.0f)
+                    * wxRotate(bearing) * _centerTrans);
+                addQuad(_vertices, _texCoords, m, texBase);
+
+                ++ground_echoes_iterator;
+
+                //cout << "test bearing " << test_brg 
+                //<< " test_rng " << test_rng * SG_METER_TO_NM
+                //<< " persistance " << _persistance
+                //<< endl;
+            }
+
+        }
+
+    }
     if (!_ai_enabled_node->getBoolValue())
         return;
 
@@ -598,14 +675,6 @@ wxRadarBg::update_aircraft()
     if (!draw_echoes && !draw_symbols && !draw_data)
         return;
 
-    radar_list_type radar_list = _ai->get_ai_list();
-    //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: AI submodel list size" << radar_list.size());
-    if (radar_list.empty())
-        return;
-
-    //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: Loading AI submodels ");
-    const double echo_radii[] = {0, 1, 1.5, 1.5, 0.001, 0.1, 1.5, 2, 1.5, 1.5, 1.5};
-
     double user_lat = _user_lat_node->getDoubleValue();
     double user_lon = _user_lon_node->getDoubleValue();
     double user_alt = _user_alt_node->getDoubleValue();
@@ -617,39 +686,61 @@ wxRadarBg::update_aircraft()
         limit = 0;
     limit *= SG_DEGREES_TO_RADIANS;
 
-    radar_list_iterator it = radar_list.begin();
-    radar_list_iterator end = radar_list.end();
-    FGAIBase *selected_ac = 0;
-    double selected_radius = 0;
-    double selected_bearing = 0;
     int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
 
-    for (; it != end; ++it) {
-        FGAIBase *ac = (*it).get();
-        int type       = ac->getType();
-        double lat     = ac->_getLatitude();
-        double lon     = ac->_getLongitude();
-        double alt     = ac->_getAltitude();
-        double heading = ac->_getHeading();
+    const SGPropertyNode *selected_ac = 0;
+    const SGPropertyNode *ai = fgGetNode("/ai/models", true);
 
-        double range, bearing;
-        calcRangeBearing(user_lat, user_lon, lat, lon, range, bearing);
+    for (int i = ai->nChildren() - 1; i >= -1; i--) {
+        const SGPropertyNode *model;
 
-        //SG_LOG(SG_GENERAL, SG_DEBUG,
-        /*        "Radar: ID=" << ac->getID() << "(" << radar_list.size() << ")"
-                << " type=" << type
-                << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
-                << " alt=" << alt
-                << " heading=" << heading
-                << " range=" << range
-                << " bearing=" << bearing);*/
+        if (i < 0) { // last iteration: selected model
+            model = selected_ac;
+        } else {
+            model = ai->getChild(i);
+            if (!model->nChildren())
+                continue;
+            if (model->getIntValue("id") == selected_id) {
+                selected_ac = model;  // save selected model for last iteration
+                continue;
+            }
+        }
+        if (!model)
+            continue;
 
+        double echo_radius, sigma;
+        const string name = model->getName();
+
+        //cout << "name "<<name << endl;
+        if (name == "aircraft" || name == "tanker")
+            echo_radius = 1, sigma = 1;
+        else if (name == "multiplayer" || name == "wingman" || name == "static")
+            echo_radius = 1.5, sigma = 1;
+        else if (name == "ship" || name == "carrier" || name == "escort" ||name == "storm")
+            echo_radius = 1.5, sigma = 100;
+        else if (name == "thermal")
+            echo_radius = 2, sigma = 100;
+        else if (name == "rocket")
+            echo_radius = 0.1, sigma = 0.1;
+        else if (name == "ballistic")
+            echo_radius = 0.001, sigma = 0.001;
+        else
+            continue;
+
+        double lat = model->getDoubleValue("position/latitude-deg");
+        double lon = model->getDoubleValue("position/longitude-deg");
+        double alt = model->getDoubleValue("position/altitude-ft");
+        double heading = model->getDoubleValue("orientation/true-heading-deg");
+
+        double range, bearing;
+        calcRangeBearing(user_lat, user_lon, lat, lon, range, bearing);
+        //cout << _antenna_ht << _interval<< endl;
         bool isVisible = withinRadarHorizon(user_alt, alt, range);
-        //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: visible " << isVisible);
+
         if (!isVisible)
             continue;
 
-        if (!inRadarRange(type, range))
+        if (!inRadarRange(sigma, range))
             continue;
 
         bearing *= SG_DEGREES_TO_RADIANS;
@@ -666,20 +757,13 @@ wxRadarBg::update_aircraft()
 
         // pos mode
         if (draw_echoes) {
-            float echo_radius = echo_radii[type] * 120;
-            float size = echo_radius * UNIT;
+            float size = echo_radius * 120 * UNIT;
 
             const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
             osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-                    * osg::Matrixf::translate(0.0f, radius, 0.0f)
-                    * wxRotate(bearing) * _centerTrans);
+                * osg::Matrixf::translate(0.0f, radius, 0.0f)
+                * wxRotate(bearing) * _centerTrans);
             addQuad(_vertices, _texCoords, m, texBase);
-
-            //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar:    drawing AI"
-                //<< " ID=" << ac->getID()
-                //<< " type=" << type
-            //        << " radius=" << radius
-            //        << " angle=" << angle * SG_RADIANS_TO_DEGREES);
         }
 
         // data mode
@@ -687,29 +771,14 @@ wxRadarBg::update_aircraft()
             const osg::Vec2f texBase(0, 3 * UNIT);
             float size = 600 * UNIT;
             osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-                    * wxRotate(heading - bearing)
-                    * osg::Matrixf::translate(0.0f, radius, 0.0f)
-                    * wxRotate(bearing) * _centerTrans);
+                * wxRotate(heading - bearing)
+                * osg::Matrixf::translate(0.0f, radius, 0.0f)
+                * wxRotate(bearing) * _centerTrans);
             addQuad(_vertices, _texCoords, m, texBase);
-
-            //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar:    drawing data"
-            //        << " x=" << x <<" y="<< y
-            //        << " bearing=" << angle * SG_RADIANS_TO_DEGREES
-            //        << " radius=" << radius);
         }
 
-        if (draw_data) {
-            if (ac->getID() == selected_id) {
-                selected_ac = ac;
-                selected_radius = radius;
-                selected_bearing = bearing;
-            } else {
-                update_data(ac, radius, bearing, false);
-            }
-        }
-    }
-    if (selected_ac) {
-        update_data(selected_ac, selected_radius, selected_bearing, true);
+        if (draw_data || i < 0)  // selected one (i == -1) is always drawn
+            update_data(model, alt, heading, radius, bearing, i < 0);
     }
 }
 
@@ -727,13 +796,13 @@ wxRadarBg::update_tacan()
     float size = 600 * UNIT;
     float radius = _tacan_distance_node->getFloatValue() * _scale;
     float angle = _tacan_bearing_node->getFloatValue() * SG_DEGREES_TO_RADIANS
-            + _angle_offset;
+        + _angle_offset;
 
     const osg::Vec2f texBase(1 * UNIT, 3 * UNIT);
     osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-            * wxRotate(-angle)
-            * osg::Matrixf::translate(0.0f, radius, 0.0f)
-            * wxRotate(angle) * _centerTrans);
+        * wxRotate(-angle)
+        * osg::Matrixf::translate(0.0f, radius, 0.0f)
+        * wxRotate(angle) * _centerTrans);
     addQuad(_vertices, _texCoords, m, texBase);
 
     //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar:     drawing TACAN"
@@ -754,7 +823,7 @@ wxRadarBg::update_heading_marker()
     const osg::Vec2f texBase(2 * UNIT, 3 * UNIT);
     float size = 600 * UNIT;
     osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
-            * wxRotate(_view_heading + _angle_offset));
+        * wxRotate(_view_heading + _angle_offset));
 
     m *= _centerTrans;
     addQuad(_vertices, _texCoords, m, texBase);
@@ -798,21 +867,23 @@ wxRadarBg::withinRadarHorizon(double user_alt, double alt, double range_nm)
 {
     // Radar Horizon  = 1.23(ht^1/2 + hr^1/2),
     //don't allow negative altitudes (an approximation - yes altitudes can be negative)
+    // Allow antenna ht to be set, but only on ground
+    _antenna_ht = _Instrument->getDoubleValue("antenna-ht-ft");
 
-    if (user_alt < 0)
-        user_alt = 0;
+    if (user_alt <= 0)
+        user_alt = _antenna_ht;
 
-    if (alt < 0)
-        alt = 0;
+    if (alt <= 0)
+        alt = 0; // to allow some vertical extent of target
 
     double radarhorizon = 1.23 * (sqrt(alt) + sqrt(user_alt));
-    //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: horizon " << radarhorizon);
+//    SG_LOG(SG_GENERAL, SG_ALERT, "Radar: radar horizon " << radarhorizon);
     return radarhorizon >= range_nm;
 }
 
 
 bool
-wxRadarBg::inRadarRange(int type, double range_nm)
+wxRadarBg::inRadarRange(double sigma, double range_nm)
 {
     //The Radar Equation:
     //
@@ -828,13 +899,12 @@ wxRadarBg::inRadarRange(int type, double range_nm)
     //
     // TODO - make the maximum range adjustable at runtime
 
-    const double sigma[] = {0, 1, 100, 100, 0.001, 0.1, 100, 100, 1, 1, 1};
     double constant = _radar_ref_rng;
 
     if (constant <= 0)
         constant = 35;
 
-    double maxrange = constant * pow(sigma[type], 0.25);
+    double maxrange = constant * pow(sigma, 0.25);
     //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: max range " << maxrange);
     return maxrange >= range_nm;
 }
@@ -842,7 +912,7 @@ wxRadarBg::inRadarRange(int type, double range_nm)
 
 void
 wxRadarBg::calcRangeBearing(double lat, double lon, double lat2, double lon2,
-        double &range, double &bearing) const
+                            double &range, double &bearing) const
 {
     // calculate the bearing and range of the second pos from the first
     double az2, distance;
@@ -865,6 +935,20 @@ wxRadarBg::calcRelBearing(float bearing, float heading)
     return angle;
 }
 
+float
+wxRadarBg::calcRelBearingDeg(float bearing, float heading)
+{
+    float angle = bearing - heading;
+
+    if (angle >= 180)
+        return angle -= 360;
+
+    if (angle < -180)
+        return angle += 360;
+
+    return angle;
+}
+
 
 void
 wxRadarBg::updateFont()