+
+
+void
+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());
+ callsign->setFontResolution(12, 12);
+ 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::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
+ // cast to int's, otherwise text comes out ugly
+ callsign->setPosition(osg::Vec3((int)pos.x(), (int)pos.y(), 0));
+ 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 << 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);
+}
+
+
+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 = fgGetDouble("/orientation/heading-deg");
+ 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;
+
+ bool draw_tcas = _radar_tcas_node->getBoolValue();
+ bool draw_absolute = _radar_absalt_node->getBoolValue();
+ bool draw_echoes = _radar_position_node->getBoolValue();
+ bool draw_symbols = _radar_symbol_node->getBoolValue();
+ bool draw_data = _radar_data_node->getBoolValue();
+ if (!draw_echoes && !draw_symbols && !draw_data)
+ return;
+
+ double user_lat = _user_lat_node->getDoubleValue();
+ double user_lon = _user_lon_node->getDoubleValue();
+ double user_alt = _user_alt_node->getDoubleValue();
+
+ float limit = _radar_coverage_node->getFloatValue();
+ if (limit > 180)
+ limit = 180;
+ else if (limit < 0)
+ limit = 0;
+ limit *= SG_DEGREES_TO_RADIANS;
+
+ int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
+
+ const SGPropertyNode *selected_ac = 0;
+ const SGPropertyNode *ai = fgGetNode("/ai/models", true);
+
+ for (int i = ai->nChildren() - 1; i >= -1; i--) {
+ const SGPropertyNode *model;
+
+ 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)&&
+ (!draw_tcas)) {
+ 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);
+
+ if (!isVisible)
+ continue;
+
+ if (!inRadarRange(sigma, range))
+ continue;
+
+ bearing *= SG_DEGREES_TO_RADIANS;
+ heading *= SG_DEGREES_TO_RADIANS;
+
+ float radius = range * _scale;
+ float angle = calcRelBearing(bearing, _view_heading);
+
+ if (angle > limit || angle < -limit)
+ continue;
+
+ bearing += _angle_offset;
+ heading += _angle_offset;
+
+ bool is_tcas_contact = false;
+ if (draw_tcas)
+ {
+ is_tcas_contact = update_tcas(model,range,user_alt,alt,bearing,radius,draw_absolute);
+ }
+
+ // pos mode
+ if (draw_echoes && (!is_tcas_contact)) {
+ 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);
+ addQuad(_vertices, _texCoords, m, texBase);
+ }
+
+ // data mode
+ if (draw_symbols && (!draw_tcas)) {
+ 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);
+ addQuad(_vertices, _texCoords, m, texBase);
+ }
+
+ if ((draw_data || i < 0)&& // selected one (i == -1) is always drawn
+ ((!draw_tcas)||(is_tcas_contact)||(draw_echoes)))
+ update_data(model, alt, heading, radius, bearing, i < 0);
+ }
+}
+
+/** Update TCAS display.
+ * Return true when processed as TCAS contact, false otherwise. */
+bool
+wxRadarBg::update_tcas(const SGPropertyNode *model,double range,double user_alt,double alt,
+ double bearing,double radius,bool absMode)
+{
+ int threatLevel=0;
+ {
+ // update TCAS symbol
+ osg::Vec2f texBase;
+ threatLevel = model->getIntValue("tcas/threat-level",-1);
+ if (threatLevel == -1)
+ {
+ // no TCAS information (i.e. no transponder) => not visible to TCAS
+ return false;
+ }
+ int row = 7 - threatLevel;
+ int col = 4;
+ double vspeed = model->getDoubleValue("velocities/vertical-speed-fps");
+ if (vspeed < -3.0) // descending
+ col+=1;
+ else
+ if (vspeed > 3.0) // climbing
+ col+=2;
+ texBase = osg::Vec2f(col*UNIT,row * UNIT);
+ float size = 200 * UNIT;
+ osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
+ * wxRotate(-bearing)
+ * osg::Matrixf::translate(0.0f, radius, 0.0f)
+ * wxRotate(bearing) * _centerTrans);
+ addQuad(_vertices, _texCoords, m, texBase);
+ }
+
+ {
+ // update TCAS data
+ osgText::Text *altStr = new osgText::Text;
+ altStr->setFont(_font.get());
+ altStr->setFontResolution(12, 12);
+ altStr->setCharacterSize(_font_size);
+ altStr->setColor(_tcas_colors[threatLevel]);
+ osg::Matrixf m(wxRotate(-bearing)
+ * 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
+ altStr->setLineSpacing(_font_spacing);
+
+ stringstream text;
+ altStr->setAlignment(osgText::Text::LEFT_CENTER);
+ int altDif = (alt-user_alt+50)/100;
+ char sign = 0;
+ int dy=0;
+ if (altDif>=0)
+ {
+ sign='+';
+ dy=2;
+ }
+ else
+ if (altDif<0)
+ {
+ sign='-';
+ altDif = -altDif;
+ dy=-30;
+ }
+ altStr->setPosition(osg::Vec3((int)pos.x()-30, (int)pos.y()+dy, 0));
+ if (absMode)
+ {
+ // absolute altitude display
+ text << setprecision(0) << fixed
+ << setw(3) << setfill('0') << alt/100 << endl;
+ }
+ else // relative altitude display
+ if (sign)
+ {
+ text << sign
+ << setprecision(0) << fixed
+ << setw(2) << setfill('0') << altDif << endl;
+ }
+
+ altStr->setText(text.str());
+ _textGeode->addDrawable(altStr);
+ }
+
+ return true;
+}
+
+void
+wxRadarBg::update_tacan()
+{
+ // draw TACAN symbol
+ int mode = _radar_mode_control_node->getIntValue();
+ bool inRange = _tacan_in_range_node->getBoolValue();
+
+ if (mode != 1 || !inRange)
+ return;
+
+ float size = 600 * UNIT;
+ float radius = _tacan_distance_node->getFloatValue() * _scale;
+ float angle = _tacan_bearing_node->getFloatValue() * SG_DEGREES_TO_RADIANS
+ + _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);
+ addQuad(_vertices, _texCoords, m, texBase);
+
+ //SG_LOG(SG_INSTR, SG_DEBUG, "Radar: drawing TACAN"
+ // << " dist=" << radius
+ // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
+ // << " bearing=" << angle * SG_RADIANS_TO_DEGREES
+ // << " x=" << x << " y="<< y
+ // << " size=" << size);
+}
+
+
+void
+wxRadarBg::update_heading_marker()
+{
+ if (!_radar_hdg_marker_node->getBoolValue())
+ return;
+
+ 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));
+
+ m *= _centerTrans;
+ addQuad(_vertices, _texCoords, m, texBase);
+
+ //SG_LOG(SG_INSTR, SG_DEBUG, "Radar: drawing heading marker"
+ // << " x,y " << x <<","<< y
+ // << " dist" << dist
+ // << " view_heading" << _view_heading * SG_RADIANS_TO_DEGREES
+ // << " heading " << iradarEcho->heading * SG_RADIANS_TO_DEGREES
+ // << " angle " << angle * SG_RADIANS_TO_DEGREES);
+}
+
+
+void
+wxRadarBg::center_map()
+{
+ _lat = _user_lat_node->getDoubleValue();
+ _lon = _user_lon_node->getDoubleValue();
+ _x_offset = _y_offset = 0;
+}
+
+
+void
+wxRadarBg::apply_map_offset()
+{
+ double lat = _user_lat_node->getDoubleValue();
+ double lon = _user_lon_node->getDoubleValue();
+ double bearing, distance, az2;
+ geo_inverse_wgs_84(_lat, _lon, lat, lon, &bearing, &az2, &distance);
+ distance *= SG_METER_TO_NM * _scale;
+ bearing *= SG_DEGREES_TO_RADIANS;
+ _x_offset += sin(bearing) * distance;
+ _y_offset += cos(bearing) * distance;
+ _lat = lat;
+ _lon = lon;
+}
+
+
+bool
+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 = _antenna_ht;
+
+ if (alt <= 0)
+ alt = 0; // to allow some vertical extent of target
+
+ double radarhorizon = 1.23 * (sqrt(alt) + sqrt(user_alt));
+// SG_LOG(SG_INSTR, SG_ALERT, "Radar: radar horizon " << radarhorizon);
+ return radarhorizon >= range_nm;
+}
+
+
+bool
+wxRadarBg::inRadarRange(double sigma, double range_nm)
+{
+ //The Radar Equation:
+ //
+ // MaxRange^4 = (TxPower * AntGain^2 * lambda^2 * sigma)/((constant) * MDS)
+ //
+ // Where (constant) = (4*pi)3 and MDS is the Minimum Detectable Signal power.
+ //
+ // For a given radar we can assume that the only variable is sigma,
+ // the target radar cross section.
+ //
+ // Here, we will use a normalised rcs (sigma) for a standard taget and assume that this
+ // will provide a maximum range of 35nm;
+ //
+ // TODO - make the maximum range adjustable at runtime
+
+ double constant = _radar_ref_rng;
+
+ if (constant <= 0)
+ constant = 35;
+
+ double maxrange = constant * pow(sigma, 0.25);
+ //SG_LOG(SG_INSTR, SG_DEBUG, "Radar: max range " << maxrange);
+ return maxrange >= range_nm;
+}
+
+
+void
+wxRadarBg::calcRangeBearing(double lat, double lon, double lat2, double lon2,
+ double &range, double &bearing) const
+{
+ // calculate the bearing and range of the second pos from the first
+ double az2, distance;
+ geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
+ range = distance *= SG_METER_TO_NM;
+}
+
+
+float
+wxRadarBg::calcRelBearing(float bearing, float heading)
+{
+ float angle = bearing - heading;
+
+ if (angle >= SG_PI)
+ angle -= 2.0 * SG_PI;
+
+ if (angle < -SG_PI)
+ angle += 2.0 * SG_PI;
+
+ 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()
+{
+ float red = _font_node->getFloatValue("color/red");
+ float green = _font_node->getFloatValue("color/green");
+ float blue = _font_node->getFloatValue("color/blue");
+ float alpha = _font_node->getFloatValue("color/alpha");
+ _font_color.set(red, green, blue, alpha);
+
+ _font_size = _font_node->getFloatValue("size");
+ _font_spacing = _font_size * _font_node->getFloatValue("line-spacing");
+ string path = _font_node->getStringValue("name", DEFAULT_FONT);
+
+ SGPath tpath;
+ if (path[0] != '/') {
+ tpath = globals->get_fg_root();
+ tpath.append("Fonts");
+ tpath.append(path);
+ } else {
+ tpath = path;
+ }
+
+#if (FG_OSG_VERSION >= 21000)
+ osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome");
+ osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str(), fontOptions.get());
+#else
+ osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str());
+#endif
+
+ if (font != 0) {
+ _font = font;
+ _font->setMinFilterHint(osg::Texture::NEAREST);
+ _font->setMagFilterHint(osg::Texture::NEAREST);
+ _font->setGlyphImageMargin(0);
+ _font->setGlyphImageMarginRatio(0);
+ }
+
+ for (int i=0;i<4;i++)
+ {
+ const float defaultColors[4][3] = {{0,1,1},{0,1,1},{1,0.5,0},{1,0,0}};
+ SGPropertyNode_ptr color_node = _font_node->getNode("tcas/color",i,true);
+ float red = color_node->getFloatValue("red",defaultColors[i][0]);
+ float green = color_node->getFloatValue("green",defaultColors[i][1]);
+ float blue = color_node->getFloatValue("blue",defaultColors[i][2]);
+ float alpha = color_node->getFloatValue("alpha",1);
+ _tcas_colors[i]=osg::Vec4(red, green, blue, alpha);
+ }
+}
+
+void
+wxRadarBg::valueChanged(SGPropertyNode*)
+{
+ updateFont();
+ _time = _interval;
+}
+