| 1 | // navigation display texture |
| 2 | // |
| 3 | // Written by James Turner, forked from wxradar code |
| 4 | // |
| 5 | // |
| 6 | // This program is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU General Public License as |
| 8 | // published by the Free Software Foundation; either version 2 of the |
| 9 | // License, or (at your option) any later version. |
| 10 | // |
| 11 | // This program is distributed in the hope that it will be useful, but |
| 12 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | // General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with this program; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | // |
| 20 | // |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include "NavDisplay.hxx" |
| 25 | |
| 26 | #include <cassert> |
| 27 | #include <algorithm> |
| 28 | |
| 29 | #include <osg/Array> |
| 30 | #include <osg/Geometry> |
| 31 | #include <osg/Matrixf> |
| 32 | #include <osg/PrimitiveSet> |
| 33 | #include <osg/StateSet> |
| 34 | #include <osg/LineWidth> |
| 35 | #include <osg/Version> |
| 36 | |
| 37 | #include <simgear/constants.h> |
| 38 | #include <simgear/misc/sg_path.hxx> |
| 39 | #include <simgear/scene/model/model.hxx> |
| 40 | #include <simgear/structure/exception.hxx> |
| 41 | #include <simgear/misc/sg_path.hxx> |
| 42 | #include <simgear/misc/strutils.hxx> |
| 43 | #include <simgear/math/sg_geodesy.hxx> |
| 44 | #include <simgear/scene/util/OsgUtils.hxx> |
| 45 | |
| 46 | #include <cstdio> |
| 47 | #include <sstream> |
| 48 | #include <iomanip> |
| 49 | #include <iostream> // for cout, endl |
| 50 | |
| 51 | using std::stringstream; |
| 52 | using std::endl; |
| 53 | using std::setprecision; |
| 54 | using std::fixed; |
| 55 | using std::setw; |
| 56 | using std::setfill; |
| 57 | using std::cout; |
| 58 | using std::endl; |
| 59 | using std::map; |
| 60 | using std::string; |
| 61 | |
| 62 | #include <Main/fg_props.hxx> |
| 63 | #include <Main/globals.hxx> |
| 64 | #include "panel.hxx" |
| 65 | #include <Navaids/routePath.hxx> |
| 66 | #include <Autopilot/route_mgr.hxx> |
| 67 | #include <Navaids/navrecord.hxx> |
| 68 | #include <Navaids/navlist.hxx> |
| 69 | #include <Navaids/fix.hxx> |
| 70 | #include <Airports/airport.hxx> |
| 71 | #include <Airports/runways.hxx> |
| 72 | #include "od_gauge.hxx" |
| 73 | |
| 74 | using namespace flightgear; |
| 75 | |
| 76 | static const char *DEFAULT_FONT = "typewriter.txf"; |
| 77 | |
| 78 | static |
| 79 | osg::Matrixf degRotation(float angle) |
| 80 | { |
| 81 | return osg::Matrixf::rotate(angle * SG_DEGREES_TO_RADIANS, 0.0f, 0.0f, -1.0f); |
| 82 | } |
| 83 | |
| 84 | static osg::Vec4 readColor(SGPropertyNode* colorNode, const osg::Vec4& c) |
| 85 | { |
| 86 | osg::Vec4 result; |
| 87 | result.r() = colorNode->getDoubleValue("red", c.r()); |
| 88 | result.g() = colorNode->getDoubleValue("green", c.g()); |
| 89 | result.b() = colorNode->getDoubleValue("blue", c.b()); |
| 90 | result.a() = colorNode->getDoubleValue("alpha", c.a()); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | static string formatPropertyValue(SGPropertyNode* nd, const string& format) |
| 95 | { |
| 96 | assert(nd); |
| 97 | static char buf[512]; |
| 98 | if (format.find('d') != string::npos) { |
| 99 | ::snprintf(buf, 512, format.c_str(), nd->getIntValue()); |
| 100 | return buf; |
| 101 | } |
| 102 | |
| 103 | if (format.find('s') != string::npos) { |
| 104 | ::snprintf(buf, 512, format.c_str(), nd->getStringValue().c_str()); |
| 105 | return buf; |
| 106 | } |
| 107 | |
| 108 | // assume it's a double/float |
| 109 | ::snprintf(buf, 512, format.c_str(), nd->getDoubleValue()); |
| 110 | return buf; |
| 111 | } |
| 112 | |
| 113 | static osg::Vec2 mult(const osg::Vec2& v, const osg::Matrixf& m) |
| 114 | { |
| 115 | osg::Vec3 r = m.preMult(osg::Vec3(v.x(), v.y(), 0.0)); |
| 116 | return osg::Vec2(r.x(), r.y()); |
| 117 | } |
| 118 | |
| 119 | class NavDisplay::CacheListener : public SGPropertyChangeListener |
| 120 | { |
| 121 | public: |
| 122 | CacheListener(NavDisplay *nd) : |
| 123 | _nd(nd) |
| 124 | {} |
| 125 | |
| 126 | void valueChanged (SGPropertyNode * prop) override |
| 127 | { |
| 128 | _nd->invalidatePositionedCache(); |
| 129 | } |
| 130 | private: |
| 131 | NavDisplay* _nd; |
| 132 | }; |
| 133 | |
| 134 | class NavDisplay::ForceUpdateListener : public SGPropertyChangeListener |
| 135 | { |
| 136 | public: |
| 137 | ForceUpdateListener(NavDisplay *nd) : |
| 138 | _nd(nd) |
| 139 | {} |
| 140 | |
| 141 | void valueChanged (SGPropertyNode * prop) override |
| 142 | { |
| 143 | _nd->forceUpdate(); |
| 144 | } |
| 145 | private: |
| 146 | NavDisplay* _nd; |
| 147 | }; |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | class SymbolRule |
| 152 | { |
| 153 | public: |
| 154 | SymbolRule() : enabled(false) |
| 155 | { |
| 156 | |
| 157 | } |
| 158 | |
| 159 | bool initFromNode(SGPropertyNode* node, NavDisplay* owner) |
| 160 | { |
| 161 | if (!node->getChild("type")) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | type = simgear::strutils::lowercase(node->getStringValue("type")); |
| 166 | SGPropertyNode* enableNode = node->getChild("enable"); |
| 167 | if (enableNode) { |
| 168 | enable.reset(sgReadCondition(fgGetNode("/"), enableNode)); |
| 169 | } |
| 170 | |
| 171 | int n=0; |
| 172 | while (node->hasChild("state", n)) { |
| 173 | string m = node->getChild("state", n++)->getStringValue(); |
| 174 | if (m[0] == '!') { |
| 175 | excluded_states.insert(m.substr(1)); |
| 176 | } else { |
| 177 | required_states.insert(m); |
| 178 | } |
| 179 | } // of matches parsing |
| 180 | |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | void setDefinition(SymbolDef* d) |
| 186 | { |
| 187 | definition = d; |
| 188 | } |
| 189 | |
| 190 | SymbolDef* getDefinition() const |
| 191 | { return definition; } |
| 192 | |
| 193 | bool matches(const string_set& states) const |
| 194 | { |
| 195 | for (auto s : required_states) { |
| 196 | if (states.count(s) == 0) { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | for (auto s : excluded_states) { |
| 202 | if (states.count(s) != 0) { |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | // return if the enabled state changed (needs a cache update) |
| 211 | bool checkEnabled() |
| 212 | { |
| 213 | if (enable.get()) { |
| 214 | bool wasEnabled = enabled; |
| 215 | enabled = enable->test(); |
| 216 | return (enabled != wasEnabled); |
| 217 | } else { |
| 218 | enabled = true; |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | bool enabled; // cached enabled state |
| 224 | std::string type; |
| 225 | |
| 226 | // record instances for limiting by count |
| 227 | int instanceCount; |
| 228 | private: |
| 229 | SymbolDef* definition = nullptr; |
| 230 | |
| 231 | std::unique_ptr<SGCondition> enable; |
| 232 | string_set required_states; |
| 233 | string_set excluded_states; |
| 234 | }; |
| 235 | |
| 236 | class SymbolDef |
| 237 | { |
| 238 | public: |
| 239 | SymbolDef() : limitCount(0) { } |
| 240 | |
| 241 | bool initFromNode(SGPropertyNode* node, NavDisplay* owner) |
| 242 | { |
| 243 | if (node->getChild("type")) { |
| 244 | SymbolRule* builtinRule = new SymbolRule; |
| 245 | builtinRule->initFromNode(node, owner); |
| 246 | builtinRule->setDefinition(this); |
| 247 | owner->addRule(builtinRule); |
| 248 | } |
| 249 | |
| 250 | if (node->hasChild("width")) { |
| 251 | float w = node->getFloatValue("width"); |
| 252 | float h = node->getFloatValue("height", w); |
| 253 | xy0.x() = -w * 0.5; |
| 254 | xy0.y() = -h * 0.5; |
| 255 | xy1.x() = w * 0.5; |
| 256 | xy1.y() = h * 0.5; |
| 257 | } else { |
| 258 | xy0.x() = node->getFloatValue("x0", 0.0); |
| 259 | xy0.y() = node->getFloatValue("y0", 0.0); |
| 260 | xy1.x() = node->getFloatValue("x1", 5); |
| 261 | xy1.y() = node->getFloatValue("y1", 5); |
| 262 | } |
| 263 | |
| 264 | double texSize = node->getFloatValue("texture-size", owner->textureSize()); |
| 265 | |
| 266 | uv0.x() = node->getFloatValue("u0", 0) / texSize; |
| 267 | uv0.y() = node->getFloatValue("v0", 0) / texSize; |
| 268 | uv1.x() = node->getFloatValue("u1", 1) / texSize; |
| 269 | uv1.y() = node->getFloatValue("v1", 1) / texSize; |
| 270 | |
| 271 | color = readColor(node->getChild("color"), osg::Vec4(1, 1, 1, 1)); |
| 272 | priority = node->getIntValue("priority", 0); |
| 273 | zOrder = node->getIntValue("zOrder", 0); |
| 274 | rotateToHeading = node->getBoolValue("rotate-to-heading", false); |
| 275 | roundPos = node->getBoolValue("round-position", true); |
| 276 | hasText = false; |
| 277 | if (node->hasChild("text")) { |
| 278 | hasText = true; |
| 279 | alignment = simgear::osgutils::mapAlignment(node->getStringValue("text-align")); |
| 280 | textTemplate = node->getStringValue("text"); |
| 281 | textOffset.x() = node->getFloatValue("text-offset-x", 0); |
| 282 | textOffset.y() = node->getFloatValue("text-offset-y", 0); |
| 283 | textColor = readColor(node->getChild("text-color"), color); |
| 284 | |
| 285 | SGPropertyNode* enableNode = node->getChild("text-enable"); |
| 286 | if (enableNode) { |
| 287 | textEnable.reset(sgReadCondition(fgGetNode("/"), enableNode)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | drawLine = node->getBoolValue("draw-line", false); |
| 292 | lineColor = readColor(node->getChild("line-color"), color); |
| 293 | drawRouteLeg = node->getBoolValue("draw-leg", false); |
| 294 | |
| 295 | stretchSymbol = node->getBoolValue("stretch-symbol", false); |
| 296 | if (stretchSymbol) { |
| 297 | stretchY2 = node->getFloatValue("y2"); |
| 298 | stretchY3 = node->getFloatValue("y3"); |
| 299 | stretchV2 = node->getFloatValue("v2") / texSize; |
| 300 | stretchV3 = node->getFloatValue("v3") / texSize; |
| 301 | } |
| 302 | |
| 303 | SGPropertyNode* limitNode = node->getChild("limit"); |
| 304 | if (limitNode) { |
| 305 | limitCount = limitNode->getIntValue(); |
| 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | osg::Vec2 xy0, xy1; |
| 312 | osg::Vec2 uv0, uv1; |
| 313 | osg::Vec4 color; |
| 314 | |
| 315 | int priority; |
| 316 | int zOrder; |
| 317 | bool rotateToHeading; |
| 318 | bool roundPos; ///< should position be rounded to integer values |
| 319 | bool hasText; |
| 320 | std::unique_ptr<SGCondition> textEnable; |
| 321 | bool textEnabled; ///< cache condition result |
| 322 | osg::Vec4 textColor; |
| 323 | osg::Vec2 textOffset; |
| 324 | osgText::Text::AlignmentType alignment; |
| 325 | string textTemplate; |
| 326 | |
| 327 | bool drawLine; |
| 328 | osg::Vec4 lineColor; |
| 329 | |
| 330 | // symbol stretching creates three quads (instead of one) - a start, |
| 331 | // middle and end quad, positioned along the line of the symbol. |
| 332 | // X (and U) axis values determined by the values above, so we only need |
| 333 | // to define the Y (and V) values to build the other quads. |
| 334 | bool stretchSymbol; |
| 335 | double stretchY2, stretchY3; |
| 336 | double stretchV2, stretchV3; |
| 337 | |
| 338 | bool drawRouteLeg; |
| 339 | |
| 340 | int limitCount, instanceCount; |
| 341 | }; |
| 342 | |
| 343 | class SymbolInstance |
| 344 | { |
| 345 | public: |
| 346 | SymbolInstance(const osg::Vec2& p, double h, SymbolDef* def, SGPropertyNode* vars) : |
| 347 | pos(p), |
| 348 | headingDeg(h), |
| 349 | definition(def), |
| 350 | props(vars) |
| 351 | { } |
| 352 | |
| 353 | osg::Vec2 pos; // projected position |
| 354 | osg::Vec2 endPos; |
| 355 | double headingDeg; |
| 356 | SymbolDef* definition; |
| 357 | SGPropertyNode_ptr props; |
| 358 | |
| 359 | string text() const |
| 360 | { |
| 361 | assert(definition->hasText); |
| 362 | string r; |
| 363 | size_t lastPos = 0; |
| 364 | |
| 365 | while (true) { |
| 366 | size_t pos = definition->textTemplate.find('{', lastPos); |
| 367 | if (pos == string::npos) { // no more replacements |
| 368 | r.append(definition->textTemplate.substr(lastPos)); |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | r.append(definition->textTemplate.substr(lastPos, pos - lastPos)); |
| 373 | |
| 374 | size_t endReplacement = definition->textTemplate.find('}', pos+1); |
| 375 | if (endReplacement <= pos) { |
| 376 | return "bad replacement"; |
| 377 | } |
| 378 | |
| 379 | string spec = definition->textTemplate.substr(pos + 1, endReplacement - (pos + 1)); |
| 380 | // look for formatter in spec |
| 381 | size_t colonPos = spec.find(':'); |
| 382 | if (colonPos == string::npos) { |
| 383 | // simple replacement |
| 384 | r.append(props->getStringValue(spec)); |
| 385 | } else { |
| 386 | string format = spec.substr(colonPos + 1); |
| 387 | string prop = spec.substr(0, colonPos); |
| 388 | r.append(formatPropertyValue(props->getNode(prop), format)); |
| 389 | } |
| 390 | |
| 391 | lastPos = endReplacement + 1; |
| 392 | } |
| 393 | |
| 394 | return r; |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | ////////////////////////////////////////////////////////////////// |
| 399 | |
| 400 | NavDisplay::NavDisplay(SGPropertyNode *node) : |
| 401 | _name(node->getStringValue("name", "nd")), |
| 402 | _num(node->getIntValue("number", 0)), |
| 403 | _time(0.0), |
| 404 | _updateInterval(node->getDoubleValue("update-interval-sec", 0.1)), |
| 405 | _forceUpdate(true), |
| 406 | _odg(0), |
| 407 | _scale(0), |
| 408 | _view_heading(0), |
| 409 | _font_size(0), |
| 410 | _font_spacing(0), |
| 411 | _rangeNm(0), |
| 412 | _maxSymbols(100) |
| 413 | { |
| 414 | _Instrument = fgGetNode(string("/instrumentation/" + _name).c_str(), _num, true); |
| 415 | _font_node = _Instrument->getNode("font", true); |
| 416 | |
| 417 | #define INITFONT(p, val, type) if (!_font_node->hasValue(p)) _font_node->set##type##Value(p, val) |
| 418 | INITFONT("name", DEFAULT_FONT, String); |
| 419 | INITFONT("size", 8, Float); |
| 420 | INITFONT("line-spacing", 0.25, Float); |
| 421 | INITFONT("color/red", 0, Float); |
| 422 | INITFONT("color/green", 0.8, Float); |
| 423 | INITFONT("color/blue", 0, Float); |
| 424 | INITFONT("color/alpha", 1, Float); |
| 425 | #undef INITFONT |
| 426 | |
| 427 | _textureSize = _Instrument->getNode("symbol-texture-size", true)->getIntValue(); |
| 428 | SGPropertyNode* symbolsNode = node->getNode("symbols"); |
| 429 | SGPropertyNode* symbol; |
| 430 | |
| 431 | map<string, SymbolDef*> definitionDict; |
| 432 | for (int i = 0; (symbol = symbolsNode->getChild("symbol", i)) != nullptr; ++i) { |
| 433 | SymbolDef* def = new SymbolDef; |
| 434 | if (!def->initFromNode(symbol, this)) { |
| 435 | delete def; |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | string id = symbol->getStringValue("id", ""); |
| 440 | if (!id.empty()) { |
| 441 | definitionDict[id] = def; |
| 442 | } |
| 443 | |
| 444 | _definitions.push_back(def); |
| 445 | } // of symbol definition parsing |
| 446 | |
| 447 | for (SGPropertyNode* rule : symbolsNode->getChildren("rule")) { |
| 448 | SymbolRule* r = new SymbolRule; |
| 449 | if (!r->initFromNode(rule, this)) { |
| 450 | delete r; |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | string id = rule->getStringValue("symbol", ""); |
| 455 | if (!id.empty() && (definitionDict.find(id) != definitionDict.end())) { |
| 456 | r->setDefinition(definitionDict[id]); |
| 457 | } else { |
| 458 | SG_LOG(SG_INSTR, SG_WARN, "symbol rule has missing/unknown definition id:" << id); |
| 459 | delete r; |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | addRule(r); |
| 464 | } |
| 465 | |
| 466 | } |
| 467 | |
| 468 | |
| 469 | NavDisplay::~NavDisplay() |
| 470 | { |
| 471 | delete _odg; |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | NavDisplay::init () |
| 476 | { |
| 477 | _cachedItemsValid = false; |
| 478 | _cacheListener.reset(new CacheListener(this)); |
| 479 | _forceUpdateListener.reset(new ForceUpdateListener(this)); |
| 480 | |
| 481 | _serviceable_node = _Instrument->getNode("serviceable", true); |
| 482 | _rangeNode = _Instrument->getNode("range", true); |
| 483 | if (!_rangeNode->hasValue()) { |
| 484 | _rangeNode->setDoubleValue(40.0); |
| 485 | } |
| 486 | _rangeNode->addChangeListener(_cacheListener.get()); |
| 487 | _rangeNode->addChangeListener(_forceUpdateListener.get()); |
| 488 | |
| 489 | _xCenterNode = _Instrument->getNode("x-center"); |
| 490 | if (!_xCenterNode->hasValue()) { |
| 491 | _xCenterNode->setDoubleValue(0.5); |
| 492 | } |
| 493 | _xCenterNode->addChangeListener(_forceUpdateListener.get()); |
| 494 | _yCenterNode = _Instrument->getNode("y-center"); |
| 495 | if (!_yCenterNode->hasValue()) { |
| 496 | _yCenterNode->setDoubleValue(0.5); |
| 497 | } |
| 498 | _yCenterNode->addChangeListener(_forceUpdateListener.get()); |
| 499 | |
| 500 | // texture name to use in 2D and 3D instruments |
| 501 | _texture_path = _Instrument->getStringValue("radar-texture-path", |
| 502 | "Aircraft/Instruments/Textures/od_wxradar.rgb"); |
| 503 | |
| 504 | string path = _Instrument->getStringValue("symbol-texture-path", |
| 505 | "Aircraft/Instruments/Textures/nd-symbols.png"); |
| 506 | SGPath tpath = globals->resolve_aircraft_path(path); |
| 507 | if (!tpath.exists()) { |
| 508 | SG_LOG(SG_INSTR, SG_WARN, "ND symbol texture not found:" << path); |
| 509 | } |
| 510 | |
| 511 | // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect |
| 512 | _symbolTexture = SGLoadTexture2D(tpath, nullptr, false, false); |
| 513 | |
| 514 | _odg = new FGODGauge; |
| 515 | _odg->setSize(_Instrument->getIntValue("texture-size", 512)); |
| 516 | |
| 517 | _route = globals->get_subsystem<FGRouteMgr>(); |
| 518 | |
| 519 | _navRadio1Node = fgGetNode("/instrumentation/nav[0]", true); |
| 520 | _navRadio2Node = fgGetNode("/instrumentation/nav[1]", true); |
| 521 | |
| 522 | _excessDataNode = _Instrument->getChild("excess-data", 0, true); |
| 523 | _excessDataNode->setBoolValue(false); |
| 524 | _testModeNode = _Instrument->getChild("test-mode", 0, true); |
| 525 | _testModeNode->setBoolValue(false); |
| 526 | |
| 527 | _viewHeadingNode = _Instrument->getChild("view-heading-deg", 0, true); |
| 528 | _userLatNode = _Instrument->getChild("user-latitude-deg", 0, true); |
| 529 | _userLonNode = _Instrument->getChild("user-longitude-deg", 0, true); |
| 530 | _userPositionEnable = _Instrument->getChild("user-position", 0, true); |
| 531 | |
| 532 | _customSymbols = _Instrument->getChild("symbols", 0, true); |
| 533 | |
| 534 | // OSG geometry setup |
| 535 | _radarGeode = new osg::Geode; |
| 536 | |
| 537 | _geom = new osg::Geometry; |
| 538 | _geom->setUseDisplayList(false); |
| 539 | |
| 540 | osg::StateSet *stateSet = _geom->getOrCreateStateSet(); |
| 541 | stateSet->setTextureAttributeAndModes(0, _symbolTexture.get()); |
| 542 | stateSet->setDataVariance(osg::Object::STATIC); |
| 543 | |
| 544 | // Initially allocate space for 128 quads |
| 545 | _vertices = new osg::Vec2Array; |
| 546 | _vertices->setDataVariance(osg::Object::DYNAMIC); |
| 547 | _vertices->reserve(128 * 4); |
| 548 | _geom->setVertexArray(_vertices); |
| 549 | _texCoords = new osg::Vec2Array; |
| 550 | _texCoords->setDataVariance(osg::Object::DYNAMIC); |
| 551 | _texCoords->reserve(128 * 4); |
| 552 | _geom->setTexCoordArray(0, _texCoords); |
| 553 | |
| 554 | _quadColors = new osg::Vec4Array; |
| 555 | _quadColors->setDataVariance(osg::Object::DYNAMIC); |
| 556 | _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
| 557 | _geom->setColorArray(_quadColors); |
| 558 | |
| 559 | _symbolPrimSet = new osg::DrawArrays(osg::PrimitiveSet::QUADS); |
| 560 | _symbolPrimSet->setDataVariance(osg::Object::DYNAMIC); |
| 561 | _geom->addPrimitiveSet(_symbolPrimSet); |
| 562 | |
| 563 | _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f), |
| 564 | osg::Vec3f(256.0f, 256.0f, 0.0f))); |
| 565 | |
| 566 | _radarGeode->addDrawable(_geom); |
| 567 | _odg->allocRT(); |
| 568 | // Texture in the 2D panel system |
| 569 | FGTextureManager::addTexture(_texture_path.c_str(), _odg->getTexture()); |
| 570 | |
| 571 | _lineGeometry = new osg::Geometry; |
| 572 | _lineGeometry->setUseDisplayList(false); |
| 573 | stateSet = _lineGeometry->getOrCreateStateSet(); |
| 574 | osg::LineWidth *lw = new osg::LineWidth(); |
| 575 | lw->setWidth(2.0); |
| 576 | stateSet->setAttribute(lw); |
| 577 | |
| 578 | _lineVertices = new osg::Vec2Array; |
| 579 | _lineVertices->setDataVariance(osg::Object::DYNAMIC); |
| 580 | _lineVertices->reserve(128 * 4); |
| 581 | _lineGeometry->setVertexArray(_lineVertices); |
| 582 | |
| 583 | |
| 584 | _lineColors = new osg::Vec4Array; |
| 585 | _lineColors->setDataVariance(osg::Object::DYNAMIC); |
| 586 | _lineGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
| 587 | _lineGeometry->setColorArray(_lineColors); |
| 588 | |
| 589 | _linePrimSet = new osg::DrawArrays(osg::PrimitiveSet::LINES); |
| 590 | _linePrimSet->setDataVariance(osg::Object::DYNAMIC); |
| 591 | _lineGeometry->addPrimitiveSet(_linePrimSet); |
| 592 | |
| 593 | _lineGeometry->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f), |
| 594 | osg::Vec3f(256.0f, 256.0f, 0.0f))); |
| 595 | |
| 596 | _radarGeode->addDrawable(_lineGeometry); |
| 597 | |
| 598 | _textGeode = new osg::Geode; |
| 599 | |
| 600 | osg::Camera *camera = _odg->getCamera(); |
| 601 | camera->addChild(_radarGeode.get()); |
| 602 | camera->addChild(_textGeode.get()); |
| 603 | osg::Texture2D* tex = _odg->getTexture(); |
| 604 | camera->setProjectionMatrixAsOrtho2D(0, tex->getTextureWidth(), |
| 605 | 0, tex->getTextureHeight()); |
| 606 | |
| 607 | updateFont(); |
| 608 | } |
| 609 | |
| 610 | void |
| 611 | NavDisplay::update (double delta_time_sec) |
| 612 | { |
| 613 | if (!fgGetBool("sim/sceneryloaded", false)) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | if (!_odg || !_serviceable_node->getBoolValue()) { |
| 618 | _Instrument->setStringValue("status", ""); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | if (_forceUpdate) { |
| 623 | _forceUpdate = false; |
| 624 | _time = 0.0; |
| 625 | } else { |
| 626 | _time += delta_time_sec; |
| 627 | if (_time < _updateInterval){ |
| 628 | return; |
| 629 | } |
| 630 | _time -= _updateInterval; |
| 631 | } |
| 632 | |
| 633 | _rangeNm = _rangeNode->getFloatValue(); |
| 634 | if (_testModeNode->getBoolValue()) { |
| 635 | _view_heading = 90; |
| 636 | } else if (_Instrument->getBoolValue("aircraft-heading-up", true)) { |
| 637 | _view_heading = fgGetDouble("/orientation/heading-deg"); |
| 638 | } else { |
| 639 | _view_heading = _Instrument->getFloatValue("heading-up-deg", 0.0); |
| 640 | } |
| 641 | _viewHeadingNode->setDoubleValue(_view_heading); |
| 642 | |
| 643 | double xCenterFrac = _xCenterNode->getDoubleValue(); |
| 644 | double yCenterFrac = _yCenterNode->getDoubleValue(); |
| 645 | int pixelSize = _odg->size(); |
| 646 | |
| 647 | int rangePixels = _Instrument->getIntValue("range-pixels", -1); |
| 648 | if (rangePixels < 0) { |
| 649 | // hacky - assume (as is very common) that x-frac doesn't vary, and |
| 650 | // y-frac is used to position the center at either the top or bottom of |
| 651 | // the pixel area. Measure from the center to the furthest edge (top or bottom) |
| 652 | rangePixels = pixelSize * std::max(fabs(1.0 - yCenterFrac), fabs(yCenterFrac)); |
| 653 | } |
| 654 | |
| 655 | _scale = rangePixels / _rangeNm; |
| 656 | _Instrument->setDoubleValue("scale", _scale); |
| 657 | |
| 658 | |
| 659 | _centerTrans = osg::Matrixf::translate(xCenterFrac * pixelSize, |
| 660 | yCenterFrac * pixelSize, 0.0); |
| 661 | |
| 662 | // scale from nm to display units, rotate so aircraft heading is up |
| 663 | // (as opposed to north), and compensate for centering |
| 664 | _projectMat = osg::Matrixf::scale(_scale, _scale, 1.0) * |
| 665 | degRotation(-_view_heading) * _centerTrans; |
| 666 | |
| 667 | if (_userPositionEnable->getBoolValue()) { |
| 668 | _pos = SGGeod::fromDeg(_userLonNode->getDoubleValue(), _userLatNode->getDoubleValue()); |
| 669 | } else { |
| 670 | _pos = globals->get_aircraft_position(); |
| 671 | } |
| 672 | |
| 673 | // invalidate the cache of positioned items, if we travelled more than 1nm |
| 674 | if (_cachedItemsValid) { |
| 675 | SGVec3d cartNow(SGVec3d::fromGeod(_pos)); |
| 676 | double movedNm = dist(_cachedPos, cartNow) * SG_METER_TO_NM; |
| 677 | _cachedItemsValid = (movedNm < 1.0); |
| 678 | } |
| 679 | |
| 680 | _vertices->clear(); |
| 681 | _lineVertices->clear(); |
| 682 | _lineColors->clear(); |
| 683 | _quadColors->clear(); |
| 684 | _texCoords->clear(); |
| 685 | _textGeode->removeDrawables(0, _textGeode->getNumDrawables()); |
| 686 | |
| 687 | for (SymbolInstance* si : _symbols) { |
| 688 | delete si; |
| 689 | } |
| 690 | _symbols.clear(); |
| 691 | |
| 692 | for (SymbolDef* d : _definitions) { |
| 693 | d->instanceCount = 0; |
| 694 | d->textEnabled = d->textEnable.get() ? d->textEnable->test() : true; |
| 695 | } |
| 696 | |
| 697 | bool enableChanged = false; |
| 698 | for (SymbolRule* r : _rules) { |
| 699 | enableChanged |= r->checkEnabled(); |
| 700 | } |
| 701 | |
| 702 | if (enableChanged) { |
| 703 | SG_LOG(SG_INSTR, SG_INFO, "NS rule enables changed, rebuilding cache"); |
| 704 | _cachedItemsValid = false; |
| 705 | } |
| 706 | |
| 707 | if (_testModeNode->getBoolValue()) { |
| 708 | addTestSymbols(); |
| 709 | } else { |
| 710 | processRoute(); |
| 711 | processNavRadios(); |
| 712 | processAI(); |
| 713 | processCustomSymbols(); |
| 714 | findItems(); |
| 715 | limitDisplayedSymbols(); |
| 716 | } |
| 717 | |
| 718 | addSymbolsToScene(); |
| 719 | |
| 720 | _symbolPrimSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size()); |
| 721 | _symbolPrimSet->dirty(); |
| 722 | _linePrimSet->set(osg::PrimitiveSet::LINES, 0, _lineVertices->size()); |
| 723 | _linePrimSet->dirty(); |
| 724 | } |
| 725 | |
| 726 | |
| 727 | void |
| 728 | NavDisplay::updateFont() |
| 729 | { |
| 730 | float red = _font_node->getFloatValue("color/red"); |
| 731 | float green = _font_node->getFloatValue("color/green"); |
| 732 | float blue = _font_node->getFloatValue("color/blue"); |
| 733 | float alpha = _font_node->getFloatValue("color/alpha"); |
| 734 | _font_color.set(red, green, blue, alpha); |
| 735 | |
| 736 | _font_size = _font_node->getFloatValue("size"); |
| 737 | _font_spacing = _font_size * _font_node->getFloatValue("line-spacing"); |
| 738 | string path = _font_node->getStringValue("name", DEFAULT_FONT); |
| 739 | |
| 740 | SGPath tpath; |
| 741 | if (path[0] != '/') { |
| 742 | tpath = globals->get_fg_root(); |
| 743 | tpath.append("Fonts"); |
| 744 | tpath.append(path); |
| 745 | } else { |
| 746 | tpath = path; |
| 747 | } |
| 748 | |
| 749 | osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome"); |
| 750 | osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.utf8Str(), fontOptions.get()); |
| 751 | |
| 752 | if (font != 0) { |
| 753 | _font = font; |
| 754 | _font->setMinFilterHint(osg::Texture::NEAREST); |
| 755 | _font->setMagFilterHint(osg::Texture::NEAREST); |
| 756 | |
| 757 | #if OSG_VERSION_LESS_THAN(3,5,6) |
| 758 | _font->setGlyphImageMargin(0); |
| 759 | _font->setGlyphImageMarginRatio(0); |
| 760 | #else |
| 761 | //OSG:TODO: need 3.5.7 version of this. |
| 762 | #endif |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void NavDisplay::addSymbolToScene(SymbolInstance* sym) |
| 767 | { |
| 768 | SymbolDef* def = sym->definition; |
| 769 | |
| 770 | osg::Vec2 verts[4]; |
| 771 | verts[0] = def->xy0; |
| 772 | verts[1] = osg::Vec2(def->xy1.x(), def->xy0.y()); |
| 773 | verts[2] = def->xy1; |
| 774 | verts[3] = osg::Vec2(def->xy0.x(), def->xy1.y()); |
| 775 | |
| 776 | if (def->rotateToHeading) { |
| 777 | osg::Matrixf m(degRotation(sym->headingDeg - _view_heading)); |
| 778 | for (int i=0; i<4; ++i) { |
| 779 | verts[i] = mult(verts[i], m); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | osg::Vec2 pos = sym->pos; |
| 784 | if (def->roundPos) { |
| 785 | pos = osg::Vec2((int) pos.x(), (int) pos.y()); |
| 786 | } |
| 787 | |
| 788 | _texCoords->push_back(def->uv0); |
| 789 | _texCoords->push_back(osg::Vec2(def->uv1.x(), def->uv0.y())); |
| 790 | _texCoords->push_back(def->uv1); |
| 791 | _texCoords->push_back(osg::Vec2(def->uv0.x(), def->uv1.y())); |
| 792 | |
| 793 | for (int i=0; i<4; ++i) { |
| 794 | _vertices->push_back(verts[i] + pos); |
| 795 | _quadColors->push_back(def->color); |
| 796 | } |
| 797 | |
| 798 | if (def->stretchSymbol) { |
| 799 | osg::Vec2 stretchVerts[4]; |
| 800 | stretchVerts[0] = osg::Vec2(def->xy0.x(), def->stretchY2); |
| 801 | stretchVerts[1] = osg::Vec2(def->xy1.x(), def->stretchY2); |
| 802 | stretchVerts[2] = osg::Vec2(def->xy1.x(), def->stretchY3); |
| 803 | stretchVerts[3] = osg::Vec2(def->xy0.x(), def->stretchY3); |
| 804 | |
| 805 | osg::Matrixf m(degRotation(sym->headingDeg - _view_heading)); |
| 806 | for (int i=0; i<4; ++i) { |
| 807 | stretchVerts[i] = mult(stretchVerts[i], m); |
| 808 | } |
| 809 | |
| 810 | // stretched quad |
| 811 | _vertices->push_back(verts[2] + pos); |
| 812 | _vertices->push_back(stretchVerts[1] + sym->endPos); |
| 813 | _vertices->push_back(stretchVerts[0] + sym->endPos); |
| 814 | _vertices->push_back(verts[3] + pos); |
| 815 | |
| 816 | _texCoords->push_back(def->uv1); |
| 817 | _texCoords->push_back(osg::Vec2(def->uv1.x(), def->stretchV2)); |
| 818 | _texCoords->push_back(osg::Vec2(def->uv0.x(), def->stretchV2)); |
| 819 | _texCoords->push_back(osg::Vec2(def->uv0.x(), def->uv1.y())); |
| 820 | |
| 821 | for (int i=0; i<4; ++i) { |
| 822 | _quadColors->push_back(def->color); |
| 823 | } |
| 824 | |
| 825 | // quad three, for the end portion |
| 826 | for (int i=0; i<4; ++i) { |
| 827 | _vertices->push_back(stretchVerts[i] + sym->endPos); |
| 828 | _quadColors->push_back(def->color); |
| 829 | } |
| 830 | |
| 831 | _texCoords->push_back(osg::Vec2(def->uv0.x(), def->stretchV2)); |
| 832 | _texCoords->push_back(osg::Vec2(def->uv1.x(), def->stretchV2)); |
| 833 | _texCoords->push_back(osg::Vec2(def->uv1.x(), def->stretchV3)); |
| 834 | _texCoords->push_back(osg::Vec2(def->uv0.x(), def->stretchV3)); |
| 835 | } |
| 836 | |
| 837 | if (def->drawLine) { |
| 838 | addLine(sym->pos, sym->endPos, def->lineColor); |
| 839 | } |
| 840 | |
| 841 | if (!def->hasText || !def->textEnabled) { |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | osgText::Text* t = new osgText::Text; |
| 846 | t->setFont(_font.get()); |
| 847 | t->setFontResolution(12, 12); |
| 848 | t->setCharacterSize(_font_size); |
| 849 | t->setLineSpacing(_font_spacing); |
| 850 | t->setColor(def->textColor); |
| 851 | t->setAlignment(def->alignment); |
| 852 | t->setText(sym->text()); |
| 853 | |
| 854 | |
| 855 | osg::Vec2 textPos = def->textOffset + pos; |
| 856 | // ensure we use ints here, or text visual quality goes bad |
| 857 | t->setPosition(osg::Vec3((int)textPos.x(), (int)textPos.y(), 0)); |
| 858 | _textGeode->addDrawable(t); |
| 859 | } |
| 860 | |
| 861 | class OrderByPriority |
| 862 | { |
| 863 | public: |
| 864 | bool operator()(SymbolInstance* a, SymbolInstance* b) |
| 865 | { |
| 866 | return a->definition->priority > b->definition->priority; |
| 867 | } |
| 868 | }; |
| 869 | |
| 870 | void NavDisplay::limitDisplayedSymbols() |
| 871 | { |
| 872 | // gloabl symbol limit |
| 873 | _maxSymbols= _Instrument->getIntValue("max-symbols", _maxSymbols); |
| 874 | if ((int) _symbols.size() <= _maxSymbols) { |
| 875 | _excessDataNode->setBoolValue(false); |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | std::sort(_symbols.begin(), _symbols.end(), OrderByPriority()); |
| 880 | _symbols.resize(_maxSymbols); |
| 881 | _excessDataNode->setBoolValue(true); |
| 882 | } |
| 883 | |
| 884 | class OrderByZ |
| 885 | { |
| 886 | public: |
| 887 | bool operator()(SymbolInstance* a, SymbolInstance* b) |
| 888 | { |
| 889 | return a->definition->zOrder > b->definition->zOrder; |
| 890 | } |
| 891 | }; |
| 892 | |
| 893 | void NavDisplay::addSymbolsToScene() |
| 894 | { |
| 895 | std::sort(_symbols.begin(), _symbols.end(), OrderByZ()); |
| 896 | for (SymbolInstance* sym : _symbols) { |
| 897 | addSymbolToScene(sym); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | void NavDisplay::addLine(osg::Vec2 a, osg::Vec2 b, const osg::Vec4& color) |
| 902 | { |
| 903 | _lineVertices->push_back(a); |
| 904 | _lineVertices->push_back(b); |
| 905 | _lineColors->push_back(color); |
| 906 | _lineColors->push_back(color); |
| 907 | } |
| 908 | |
| 909 | osg::Vec2 NavDisplay::projectBearingRange(double bearingDeg, double rangeNm) const |
| 910 | { |
| 911 | osg::Vec3 p(0, rangeNm, 0.0); |
| 912 | p = degRotation(bearingDeg).preMult(p); |
| 913 | p = _projectMat.preMult(p); |
| 914 | return osg::Vec2(p.x(), p.y()); |
| 915 | } |
| 916 | |
| 917 | osg::Vec2 NavDisplay::projectGeod(const SGGeod& geod) const |
| 918 | { |
| 919 | double rangeM, bearing, az2; |
| 920 | SGGeodesy::inverse(_pos, geod, bearing, az2, rangeM); |
| 921 | return projectBearingRange(bearing, rangeM * SG_METER_TO_NM); |
| 922 | } |
| 923 | |
| 924 | class Filter : public FGPositioned::Filter |
| 925 | { |
| 926 | public: |
| 927 | Filter(NavDisplay* nd) : _owner(nd) { } |
| 928 | |
| 929 | double minRunwayLengthFt; |
| 930 | |
| 931 | bool pass(FGPositioned* aPos) const override |
| 932 | { |
| 933 | if (aPos->type() == FGPositioned::FIX) { |
| 934 | string ident(aPos->ident()); |
| 935 | // ignore fixes which end in digits |
| 936 | if ((ident.size() > 4) && isdigit(ident[3]) && isdigit(ident[4])) { |
| 937 | return false; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if (aPos->type() == FGPositioned::AIRPORT) { |
| 942 | FGAirport* apt = (FGAirport*) aPos; |
| 943 | if (!apt->hasHardRunwayOfLengthFt(minRunwayLengthFt)) { |
| 944 | return false; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // check against current rule states |
| 949 | return _owner->isPositionedShown(aPos); |
| 950 | } |
| 951 | |
| 952 | FGPositioned::Type minType() const override |
| 953 | { |
| 954 | return FGPositioned::AIRPORT; |
| 955 | } |
| 956 | |
| 957 | FGPositioned::Type maxType() const override |
| 958 | { |
| 959 | return FGPositioned::OBSTACLE; |
| 960 | } |
| 961 | |
| 962 | private: |
| 963 | NavDisplay* _owner; |
| 964 | }; |
| 965 | |
| 966 | void NavDisplay::findItems() |
| 967 | { |
| 968 | if (!_cachedItemsValid) { |
| 969 | Filter filt(this); |
| 970 | filt.minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 2000); |
| 971 | bool wasTimeLimited; |
| 972 | _itemsInRange = FGPositioned::findClosestNPartial(_pos, _maxSymbols, _rangeNm, |
| 973 | &filt, wasTimeLimited); |
| 974 | _cachedItemsValid = true; |
| 975 | _cachedPos = SGVec3d::fromGeod(_pos); |
| 976 | |
| 977 | if (wasTimeLimited) { |
| 978 | // re-query next frame, to load incrementally |
| 979 | _cachedItemsValid = false; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | // sort by distance from pos, so symbol limits are accurate |
| 984 | FGPositioned::sortByRange(_itemsInRange, _pos); |
| 985 | for (FGPositioned* pos : _itemsInRange) { |
| 986 | foundPositionedItem(pos); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | void NavDisplay::processRoute() |
| 991 | { |
| 992 | _routeSources.clear(); |
| 993 | flightgear::FlightPlan* fp = _route->flightPlan(); |
| 994 | RoutePath path(fp); |
| 995 | int current = _route->currentIndex(); |
| 996 | |
| 997 | for (int l=0; l<fp->numLegs(); ++l) { |
| 998 | flightgear::FlightPlan::Leg* leg = fp->legAtIndex(l); |
| 999 | flightgear::WayptRef wpt(leg->waypoint()); |
| 1000 | _routeSources.insert(wpt->source()); |
| 1001 | |
| 1002 | string_set state; |
| 1003 | state.insert("on-active-route"); |
| 1004 | |
| 1005 | if (l < current) { |
| 1006 | state.insert("passed"); |
| 1007 | } |
| 1008 | |
| 1009 | if (l == current) { |
| 1010 | state.insert("current-wp"); |
| 1011 | } |
| 1012 | |
| 1013 | if (l > current) { |
| 1014 | state.insert("future"); |
| 1015 | } |
| 1016 | |
| 1017 | if (l == (current + 1)) { |
| 1018 | state.insert("next-wp"); |
| 1019 | } |
| 1020 | |
| 1021 | SymbolRuleVector rules; |
| 1022 | findRules("waypoint" , state, rules); |
| 1023 | if (rules.empty()) { |
| 1024 | return; // no rules matched, we can skip this item |
| 1025 | } |
| 1026 | |
| 1027 | SGGeod g = path.positionForIndex(l); |
| 1028 | SGPropertyNode* vars = _route->wayptNodeAtIndex(l); |
| 1029 | if (!vars) { |
| 1030 | continue; // shouldn't happen, but let's guard against it |
| 1031 | } |
| 1032 | |
| 1033 | double heading; |
| 1034 | computeWayptPropsAndHeading(wpt, g, vars, heading); |
| 1035 | |
| 1036 | osg::Vec2 projected = projectGeod(g); |
| 1037 | for (SymbolRule* r : rules) { |
| 1038 | addSymbolInstance(projected, heading, r->getDefinition(), vars); |
| 1039 | |
| 1040 | if (r->getDefinition()->drawRouteLeg) { |
| 1041 | SGGeodVec gv(path.pathForIndex(l)); |
| 1042 | if (!gv.empty()) { |
| 1043 | osg::Vec2 pr = projectGeod(gv[0]); |
| 1044 | for (unsigned int i=1; i<gv.size(); ++i) { |
| 1045 | osg::Vec2 p = projectGeod(gv[i]); |
| 1046 | addLine(pr, p, r->getDefinition()->lineColor); |
| 1047 | pr = p; |
| 1048 | } |
| 1049 | } |
| 1050 | } // of leg drawing enabled |
| 1051 | } // of matching rules iteration |
| 1052 | } // of waypoints iteration |
| 1053 | } |
| 1054 | |
| 1055 | void NavDisplay::computeWayptPropsAndHeading(flightgear::Waypt* wpt, const SGGeod& pos, SGPropertyNode* nd, double& heading) |
| 1056 | { |
| 1057 | double rangeM, az2; |
| 1058 | SGGeodesy::inverse(_pos, pos, heading, az2, rangeM); |
| 1059 | nd->setIntValue("radial", heading); |
| 1060 | nd->setDoubleValue("distance-nm", rangeM * SG_METER_TO_NM); |
| 1061 | |
| 1062 | heading = nd->getDoubleValue("leg-bearing-true-deg"); |
| 1063 | } |
| 1064 | |
| 1065 | void NavDisplay::processNavRadios() |
| 1066 | { |
| 1067 | _nav1Station = processNavRadio(_navRadio1Node); |
| 1068 | _nav2Station = processNavRadio(_navRadio2Node); |
| 1069 | |
| 1070 | foundPositionedItem(_nav1Station); |
| 1071 | foundPositionedItem(_nav2Station); |
| 1072 | } |
| 1073 | |
| 1074 | FGNavRecord* NavDisplay::processNavRadio(const SGPropertyNode_ptr& radio) |
| 1075 | { |
| 1076 | double mhz = radio->getDoubleValue("frequencies/selected-mhz", 0.0); |
| 1077 | FGNavRecord* nav = FGNavList::findByFreq(mhz, _pos, FGNavList::navFilter()); |
| 1078 | if (!nav || (nav->ident() != radio->getStringValue("nav-id"))) { |
| 1079 | // station was not found |
| 1080 | return nullptr; |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | return nav; |
| 1085 | } |
| 1086 | |
| 1087 | bool NavDisplay::anyRuleForType(const string& type) const |
| 1088 | { |
| 1089 | for (SymbolRule* r : _rules) { |
| 1090 | if (!r->enabled) { |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | if (r->type == type) { |
| 1095 | return true; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | void NavDisplay::findRules(const string& type, const string_set& states, SymbolRuleVector& rules) |
| 1103 | { |
| 1104 | for (SymbolRule* candidate : _rules) { |
| 1105 | if (!candidate->enabled || (candidate->type != type)) { |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | if (candidate->matches(states)) { |
| 1110 | rules.push_back(candidate); |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | bool NavDisplay::isPositionedShown(FGPositioned* pos) |
| 1116 | { |
| 1117 | SymbolRuleVector rules; |
| 1118 | isPositionedShownInner(pos, rules); |
| 1119 | return !rules.empty(); |
| 1120 | } |
| 1121 | |
| 1122 | void NavDisplay::isPositionedShownInner(FGPositioned* pos, SymbolRuleVector& rules) |
| 1123 | { |
| 1124 | string type = simgear::strutils::lowercase(FGPositioned::nameForType(pos->type())); |
| 1125 | if (!anyRuleForType(type)) { |
| 1126 | return; // not diplayed at all, we're done |
| 1127 | } |
| 1128 | |
| 1129 | string_set states; |
| 1130 | computePositionedState(pos, states); |
| 1131 | |
| 1132 | findRules(type, states, rules); |
| 1133 | } |
| 1134 | |
| 1135 | void NavDisplay::foundPositionedItem(FGPositioned* pos) |
| 1136 | { |
| 1137 | if (!pos) { |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | SymbolRuleVector rules; |
| 1142 | isPositionedShownInner(pos, rules); |
| 1143 | if (rules.empty()) { |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | SGPropertyNode_ptr vars(new SGPropertyNode); |
| 1148 | double heading; |
| 1149 | computePositionedPropsAndHeading(pos, vars, heading); |
| 1150 | |
| 1151 | osg::Vec2 projected = projectGeod(pos->geod()); |
| 1152 | if (pos->type() == FGPositioned::RUNWAY) { |
| 1153 | FGRunway* rwy = (FGRunway*) pos; |
| 1154 | projected = projectGeod(rwy->threshold()); |
| 1155 | } |
| 1156 | |
| 1157 | for (SymbolRule* r : rules) { |
| 1158 | SymbolInstance* ins = addSymbolInstance(projected, heading, r->getDefinition(), vars); |
| 1159 | if ((ins)&&(pos->type() == FGPositioned::RUNWAY)) { |
| 1160 | FGRunway* rwy = (FGRunway*) pos; |
| 1161 | ins->endPos = projectGeod(rwy->end()); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | void NavDisplay::computePositionedPropsAndHeading(FGPositioned* pos, SGPropertyNode* nd, double& heading) |
| 1167 | { |
| 1168 | nd->setStringValue("id", pos->ident()); |
| 1169 | nd->setStringValue("name", pos->name()); |
| 1170 | nd->setDoubleValue("elevation-ft", pos->elevation()); |
| 1171 | nd->setIntValue("heading-deg", 0); |
| 1172 | heading = 0.0; |
| 1173 | |
| 1174 | switch (pos->type()) { |
| 1175 | case FGPositioned::VOR: |
| 1176 | case FGPositioned::LOC: |
| 1177 | case FGPositioned::TACAN: { |
| 1178 | FGNavRecord* nav = static_cast<FGNavRecord*>(pos); |
| 1179 | nd->setDoubleValue("frequency-mhz", nav->get_freq()); |
| 1180 | |
| 1181 | if (pos == _nav1Station) { |
| 1182 | heading = _navRadio1Node->getDoubleValue("radials/target-radial-deg"); |
| 1183 | } else if (pos == _nav2Station) { |
| 1184 | heading = _navRadio2Node->getDoubleValue("radials/target-radial-deg"); |
| 1185 | } |
| 1186 | |
| 1187 | nd->setIntValue("heading-deg", heading); |
| 1188 | break; |
| 1189 | } |
| 1190 | |
| 1191 | case FGPositioned::AIRPORT: |
| 1192 | case FGPositioned::SEAPORT: |
| 1193 | case FGPositioned::HELIPORT: |
| 1194 | |
| 1195 | break; |
| 1196 | |
| 1197 | case FGPositioned::RUNWAY: { |
| 1198 | FGRunway* rwy = static_cast<FGRunway*>(pos); |
| 1199 | heading = rwy->headingDeg(); |
| 1200 | nd->setDoubleValue("heading-deg", heading); |
| 1201 | nd->setIntValue("length-ft", rwy->lengthFt()); |
| 1202 | nd->setStringValue("airport", rwy->airport()->ident()); |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | default: |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | void NavDisplay::computePositionedState(FGPositioned* pos, string_set& states) |
| 1212 | { |
| 1213 | if (_routeSources.count(pos) != 0) { |
| 1214 | states.insert("on-active-route"); |
| 1215 | } |
| 1216 | |
| 1217 | flightgear::FlightPlan* fp = _route->flightPlan(); |
| 1218 | switch (pos->type()) { |
| 1219 | case FGPositioned::VOR: |
| 1220 | case FGPositioned::LOC: |
| 1221 | if (pos == _nav1Station) { |
| 1222 | states.insert("tuned"); |
| 1223 | states.insert("nav1"); |
| 1224 | } |
| 1225 | |
| 1226 | if (pos == _nav2Station) { |
| 1227 | states.insert("tuned"); |
| 1228 | states.insert("nav2"); |
| 1229 | } |
| 1230 | break; |
| 1231 | |
| 1232 | case FGPositioned::AIRPORT: |
| 1233 | case FGPositioned::SEAPORT: |
| 1234 | case FGPositioned::HELIPORT: |
| 1235 | // mark alternates! |
| 1236 | // once the FMS system has some way to tell us about them, of course |
| 1237 | |
| 1238 | if (pos == fp->departureAirport()) { |
| 1239 | states.insert("departure"); |
| 1240 | } |
| 1241 | |
| 1242 | if (pos == fp->destinationAirport()) { |
| 1243 | states.insert("destination"); |
| 1244 | } |
| 1245 | break; |
| 1246 | |
| 1247 | case FGPositioned::RUNWAY: |
| 1248 | if (pos == fp->departureRunway()) { |
| 1249 | states.insert("departure"); |
| 1250 | } |
| 1251 | |
| 1252 | if (pos == fp->destinationRunway()) { |
| 1253 | states.insert("destination"); |
| 1254 | } |
| 1255 | break; |
| 1256 | |
| 1257 | case FGPositioned::OBSTACLE: |
| 1258 | #if 0 |
| 1259 | FGObstacle* obs = (FGObstacle*) pos; |
| 1260 | if (obj->isLit()) { |
| 1261 | states.insert("lit"); |
| 1262 | } |
| 1263 | |
| 1264 | if (obj->getHeightAGLFt() >= 1000) { |
| 1265 | states.insert("greater-1000-ft"); |
| 1266 | } |
| 1267 | #endif |
| 1268 | break; |
| 1269 | |
| 1270 | default: |
| 1271 | break; |
| 1272 | } // FGPositioned::Type switch |
| 1273 | } |
| 1274 | |
| 1275 | static string mapAINodeToType(SGPropertyNode* model) |
| 1276 | { |
| 1277 | // assume all multiplayer items are aircraft for the moment. Not ideal. |
| 1278 | if (model->getNameString() == "multiplayer") { |
| 1279 | return "ai-aircraft"; |
| 1280 | } |
| 1281 | |
| 1282 | return string("ai-") + model->getNameString(); |
| 1283 | } |
| 1284 | |
| 1285 | void NavDisplay::processAI() |
| 1286 | { |
| 1287 | SGPropertyNode *ai = fgGetNode("/ai/models", true); |
| 1288 | for (int i = ai->nChildren() - 1; i >= 0; i--) { |
| 1289 | SGPropertyNode *model = ai->getChild(i); |
| 1290 | if (!model->nChildren()) { |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
| 1294 | // prefix types with 'ai-', to avoid any chance of namespace collisions |
| 1295 | // with fg-positioned. |
| 1296 | string_set ss; |
| 1297 | computeAIStates(model, ss); |
| 1298 | SymbolRuleVector rules; |
| 1299 | findRules(mapAINodeToType(model), ss, rules); |
| 1300 | if (rules.empty()) { |
| 1301 | return; // no rules matched, we can skip this item |
| 1302 | } |
| 1303 | |
| 1304 | double heading = model->getDoubleValue("orientation/true-heading-deg"); |
| 1305 | SGGeod aiModelPos = SGGeod::fromDegFt(model->getDoubleValue("position/longitude-deg"), |
| 1306 | model->getDoubleValue("position/latitude-deg"), |
| 1307 | model->getDoubleValue("position/altitude-ft")); |
| 1308 | // compute some additional props |
| 1309 | int fl = (aiModelPos.getElevationFt() / 1000); |
| 1310 | model->setIntValue("flight-level", fl * 10); |
| 1311 | |
| 1312 | osg::Vec2 projected = projectGeod(aiModelPos); |
| 1313 | for (SymbolRule* r : rules) { |
| 1314 | addSymbolInstance(projected, heading, r->getDefinition(), (SGPropertyNode*) model); |
| 1315 | } |
| 1316 | } // of ai models iteration |
| 1317 | } |
| 1318 | |
| 1319 | void NavDisplay::computeAIStates(const SGPropertyNode* ai, string_set& states) |
| 1320 | { |
| 1321 | int threatLevel = ai->getIntValue("tcas/threat-level",-1); |
| 1322 | if (threatLevel < 1) |
| 1323 | threatLevel = 0; |
| 1324 | |
| 1325 | states.insert("tcas"); |
| 1326 | |
| 1327 | std::ostringstream os; |
| 1328 | os << "tcas-threat-level-" << threatLevel; |
| 1329 | states.insert(os.str()); |
| 1330 | |
| 1331 | double vspeed = ai->getDoubleValue("velocities/vertical-speed-fps"); |
| 1332 | if (vspeed < -3.0) { |
| 1333 | states.insert("descending"); |
| 1334 | } else if (vspeed > 3.0) { |
| 1335 | states.insert("climbing"); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | SymbolInstance* NavDisplay::addSymbolInstance(const osg::Vec2& proj, double heading, SymbolDef* def, SGPropertyNode* vars) |
| 1340 | { |
| 1341 | if (isProjectedClipped(proj)) { |
| 1342 | return nullptr; |
| 1343 | } |
| 1344 | |
| 1345 | if ((def->limitCount > 0) && (def->instanceCount >= def->limitCount)) { |
| 1346 | return nullptr; |
| 1347 | } |
| 1348 | |
| 1349 | ++def->instanceCount; |
| 1350 | SymbolInstance* sym = new SymbolInstance(proj, heading, def, vars); |
| 1351 | _symbols.push_back(sym); |
| 1352 | return sym; |
| 1353 | } |
| 1354 | |
| 1355 | bool NavDisplay::isProjectedClipped(const osg::Vec2& projected) const |
| 1356 | { |
| 1357 | double size = _odg->size(); |
| 1358 | return (projected.x() < 0.0) || |
| 1359 | (projected.y() < 0.0) || |
| 1360 | (projected.x() >= size) || |
| 1361 | (projected.y() >= size); |
| 1362 | } |
| 1363 | |
| 1364 | void NavDisplay::addTestSymbol(const std::string& type, const std::string& states, const SGGeod& pos, double heading, SGPropertyNode* vars) |
| 1365 | { |
| 1366 | string_set stateSet; |
| 1367 | for (auto s : simgear::strutils::split(states, ",")) { |
| 1368 | stateSet.insert(s); |
| 1369 | } |
| 1370 | |
| 1371 | SymbolRuleVector rules; |
| 1372 | findRules(type, stateSet, rules); |
| 1373 | if (rules.empty()) { |
| 1374 | return; // no rules matched, we can skip this item |
| 1375 | } |
| 1376 | |
| 1377 | osg::Vec2 projected = projectGeod(pos); |
| 1378 | for (SymbolRule* r : rules) { |
| 1379 | addSymbolInstance(projected, heading, r->getDefinition(), vars); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | void NavDisplay::addTestSymbols() |
| 1384 | { |
| 1385 | _pos = SGGeod::fromDeg(-122.3748889, 37.6189722); // KSFO |
| 1386 | |
| 1387 | SGGeod a1; |
| 1388 | double dummy; |
| 1389 | SGGeodesy::direct(_pos, 45.0, 20.0 * SG_NM_TO_METER, a1, dummy); |
| 1390 | |
| 1391 | addTestSymbol("airport", "", a1, 0.0, nullptr); |
| 1392 | |
| 1393 | SGGeodesy::direct(_pos, 95.0, 40.0 * SG_NM_TO_METER, a1, dummy); |
| 1394 | |
| 1395 | addTestSymbol("vor", "", a1, 0.0, nullptr); |
| 1396 | |
| 1397 | SGGeodesy::direct(_pos, 120, 80.0 * SG_NM_TO_METER, a1, dummy); |
| 1398 | |
| 1399 | addTestSymbol("airport", "destination", a1, 0.0, nullptr); |
| 1400 | |
| 1401 | SGGeodesy::direct(_pos, 80.0, 20.0 * SG_NM_TO_METER, a1, dummy); |
| 1402 | addTestSymbol("fix", "", a1, 0.0, nullptr); |
| 1403 | |
| 1404 | |
| 1405 | SGGeodesy::direct(_pos, 140.0, 20.0 * SG_NM_TO_METER, a1, dummy); |
| 1406 | addTestSymbol("fix", "", a1, 0.0, nullptr); |
| 1407 | |
| 1408 | SGGeodesy::direct(_pos, 110.0, 10.0 * SG_NM_TO_METER, a1, dummy); |
| 1409 | addTestSymbol("fix", "", a1, 0.0, nullptr); |
| 1410 | |
| 1411 | SGGeodesy::direct(_pos, 110.0, 5.0 * SG_NM_TO_METER, a1, dummy); |
| 1412 | addTestSymbol("fix", "", a1, 0.0, nullptr); |
| 1413 | } |
| 1414 | |
| 1415 | void NavDisplay::addRule(SymbolRule* r) |
| 1416 | { |
| 1417 | _rules.push_back(r); |
| 1418 | } |
| 1419 | |
| 1420 | void NavDisplay::computeCustomSymbolStates(const SGPropertyNode* sym, string_set& states) |
| 1421 | { |
| 1422 | for (SGPropertyNode* st : sym->getChildren("state")) { |
| 1423 | states.insert(st->getStringValue()); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | void NavDisplay::processCustomSymbols() |
| 1428 | { |
| 1429 | for (int i = _customSymbols->nChildren() - 1; i >= 0; i--) { |
| 1430 | SGPropertyNode *symNode = _customSymbols->getChild(i); |
| 1431 | if (!symNode->nChildren()) { |
| 1432 | continue; |
| 1433 | } |
| 1434 | string_set ss; |
| 1435 | computeCustomSymbolStates(symNode, ss); |
| 1436 | SymbolRuleVector rules; |
| 1437 | findRules(symNode->getNameString(), ss, rules); |
| 1438 | if (rules.empty()) { |
| 1439 | return; // no rules matched, we can skip this item |
| 1440 | } |
| 1441 | |
| 1442 | double heading = symNode->getDoubleValue("true-heading-deg", 0.0); |
| 1443 | SGGeod pos = SGGeod::fromDegFt(symNode->getDoubleValue("longitude-deg"), |
| 1444 | symNode->getDoubleValue("latitude-deg"), |
| 1445 | symNode->getDoubleValue("altitude-ft")); |
| 1446 | |
| 1447 | |
| 1448 | osg::Vec2 projected = projectGeod(pos); |
| 1449 | for (SymbolRule* r : rules) { |
| 1450 | addSymbolInstance(projected, heading, r->getDefinition(), symNode); |
| 1451 | } |
| 1452 | } // of custom symbols iteration |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | // Register the subsystem. |
| 1457 | #if 0 |
| 1458 | SGSubsystemMgr::Registrant<NavDisplay> registrantNavDisplay( |
| 1459 | SGSubsystemMgr::GENERAL, |
| 1460 | {{"route-manager", SGSubsystemMgr::Dependency::HARD}}); |
| 1461 | #endif |