From fa4a5e7a64838a84379330c6bfe967dc2c5a1fa5 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 18 Jan 2016 00:04:05 -0500 Subject: [PATCH] Better types for eye/target position --- src/Viewer/viewer.cxx | 87 ++++++++++++++++++++++++++++++------------- src/Viewer/viewer.hxx | 38 ++++++++++++++----- 2 files changed, 91 insertions(+), 34 deletions(-) diff --git a/src/Viewer/viewer.cxx b/src/Viewer/viewer.cxx index 36e7fa967..c3098b22d 100644 --- a/src/Viewer/viewer.cxx +++ b/src/Viewer/viewer.cxx @@ -176,7 +176,7 @@ View* View::createFromProperties(SGPropertyNode_ptr config) target_x_offset_m, target_y_offset_m, target_z_offset_m, near_m, internal ); if (!from_model) { - v->_targetProperties = PositionAttitudeProperties(config, "target-"); + v->_targetProperties.init(config, "target-"); } } else { v = new View ( FG_LOOKFROM, from_model, from_model_index, @@ -188,7 +188,7 @@ View* View::createFromProperties(SGPropertyNode_ptr config) } if (!from_model) { - v->_eyeProperties = PositionAttitudeProperties(config, "eye-"); + v->_eyeProperties.init(config, "eye-"); } v->_name = config->getParent()->getStringValue("name"); @@ -306,17 +306,17 @@ View::setInternal ( bool internal ) } void -View::setPosition (double lon_deg, double lat_deg, double alt_ft) +View::setPosition (const SGGeod& geod) { _dirty = true; - _position = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft); + _position = geod; } void -View::setTargetPosition (double lon_deg, double lat_deg, double alt_ft) +View::setTargetPosition (const SGGeod& geod) { _dirty = true; - _target = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft); + _target = geod; } void @@ -769,10 +769,9 @@ void View::updateData() { if (!_from_model) { - SGVec3d pos = _eyeProperties.position(); + SGGeod pos = _eyeProperties.position(); SGVec3d att = _eyeProperties.attitude(); - - setPosition(pos.x(), pos.y(), pos.z()); + setPosition(pos); setOrientation(att[2], att[1], att[0]); } else { set_dirty(); @@ -781,9 +780,9 @@ View::updateData() // if lookat (type 1) then get target data... if (getType() == FG_LOOKAT) { if (!_from_model) { - SGVec3d pos = _targetProperties.position(); + SGGeod pos = _targetProperties.position(); SGVec3d att = _targetProperties.attitude(); - setTargetPosition(pos.x(), pos.y(), pos.z()); + setTargetPosition(pos); setTargetOrientation(att[2], att[1], att[0]); } else { set_dirty(); @@ -881,26 +880,64 @@ View::PositionAttitudeProperties::PositionAttitudeProperties() { } -View::PositionAttitudeProperties::PositionAttitudeProperties(SGPropertyNode_ptr parent, const std::string& prefix) +View::PositionAttitudeProperties::~PositionAttitudeProperties() +{ +} + +void View::PositionAttitudeProperties::init(SGPropertyNode_ptr parent, const std::string& prefix) +{ + _lonPathProp = parent->getNode(prefix + "lon-deg-path", true); + _latPathProp = parent->getNode(prefix + "lat-deg-path", true); + _altPathProp = parent->getNode(prefix + "alt-ft-path", true); + _headingPathProp = parent->getNode(prefix + "heading-deg-path", true); + _pitchPathProp = parent->getNode(prefix + "pitch-deg-path", true); + _rollPathProp = parent->getNode(prefix + "roll-deg-path", true); + + // update the real properties now + valueChanged(NULL); + + _lonPathProp->addChangeListener(this); + _latPathProp->addChangeListener(this); + _altPathProp->addChangeListener(this); + _headingPathProp->addChangeListener(this); + _pitchPathProp->addChangeListener(this); + _rollPathProp->addChangeListener(this); +} + +void View::PositionAttitudeProperties::valueChanged(SGPropertyNode* node) { - _lonProp = parent->getNode(prefix + "lon-deg-path", 0, true); - _latProp = parent->getNode(prefix + "lat-deg-path", 0, true); - _altProp = parent->getNode(prefix + "alt-ft-path", 0, true); - _headingProp = parent->getNode(prefix + "heading-deg-path", 0, true); - _pitchProp = parent->getNode(prefix + "pitch-deg-path", 0, true); - _rollProp = parent->getNode(prefix + "roll-deg-path", 0, true); + _lonProp = resolvePathProperty(_lonPathProp); + _latProp = resolvePathProperty(_latPathProp); + _altProp = resolvePathProperty(_altPathProp); + _headingProp = resolvePathProperty(_headingPathProp); + _pitchProp = resolvePathProperty(_pitchPathProp); + _rollProp = resolvePathProperty(_rollPathProp); +} + +SGPropertyNode_ptr View::PositionAttitudeProperties::resolvePathProperty(SGPropertyNode_ptr p) +{ + if (!p) + return SGPropertyNode_ptr(); + + std::string path = p->getStringValue(); + if (path.empty()) + return SGPropertyNode_ptr(); + + return fgGetNode(path, true); } -SGVec3d View::PositionAttitudeProperties::position() const +SGGeod View::PositionAttitudeProperties::position() const { - return SGVec3d(_lonProp->getDoubleValue(), - _latProp->getDoubleValue(), - _altProp->getDoubleValue()); + double lon = _lonProp ? _lonProp->getDoubleValue() : 0.0; + double lat = _latProp ? _latProp->getDoubleValue() : 0.0; + double alt = _altProp ? _altProp->getDoubleValue() : 0.0; + return SGGeod::fromDegFt(lon, lat, alt); } SGVec3d View::PositionAttitudeProperties::attitude() const { - return SGVec3d(_headingProp->getDoubleValue(), - _pitchProp->getDoubleValue(), - _rollProp->getDoubleValue()); + double heading = _headingProp ? _headingProp->getDoubleValue() : 0.0; + double pitch = _pitchProp ? _pitchProp->getDoubleValue() : 0.0; + double roll = _rollProp ? _rollProp->getDoubleValue() : 0.0; + return SGVec3d(heading, pitch, roll); } diff --git a/src/Viewer/viewer.hxx b/src/Viewer/viewer.hxx index 97f92ff69..3258b323c 100644 --- a/src/Viewer/viewer.hxx +++ b/src/Viewer/viewer.hxx @@ -92,11 +92,11 @@ public: // pilot view, model in model view). // FIXME: the model view position (ie target positions) // should be in the model class. - void setPosition (double lon_deg, double lat_deg, double alt_ft); + + const SGGeod& getPosition() const { return _position; } // Reference geodetic target position... - void setTargetPosition (double lon_deg, double lat_deg, double alt_ft); const SGGeod& getTargetPosition() const { return _target; } @@ -249,6 +249,9 @@ private: void setHeadingOffset_deg_property (double heading_offset_deg); void setPitchOffset_deg_property(double pitch_offset_deg); void setRollOffset_deg_property(double roll_offset_deg); + + void setPosition (const SGGeod& geod); + void setTargetPosition (const SGGeod& geod); ////////////////////////////////////////////////////////////////// // private data // @@ -327,22 +330,39 @@ private: // multiplied into the aspect_ratio to get the actual vertical fov double _aspect_ratio_multiplier; - class PositionAttitudeProperties + class PositionAttitudeProperties : public SGPropertyChangeListener { public: PositionAttitudeProperties(); - PositionAttitudeProperties(SGPropertyNode_ptr parent, const std::string& prefix); + void init(SGPropertyNode_ptr parent, const std::string& prefix); + + virtual ~PositionAttitudeProperties(); - SGVec3d position() const; + SGGeod position() const; SGVec3d attitude() const; // as heading pitch roll + + protected: + virtual void valueChanged(SGPropertyNode* prop); + private: +// disable copy + PositionAttitudeProperties(const PositionAttitudeProperties&); + + SGPropertyNode_ptr resolvePathProperty(SGPropertyNode_ptr p); + SGPropertyNode_ptr _lonProp, _latProp, - _altProp, - _headingProp, - _pitchProp, - _rollProp; + _altProp, + _headingProp, + _pitchProp, + _rollProp; + SGPropertyNode_ptr _lonPathProp, + _latPathProp, + _altPathProp, + _headingPathProp, + _pitchPathProp, + _rollPathProp; }; PositionAttitudeProperties _eyeProperties; -- 2.39.5