CameraCalc.cc 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "CameraCalc.h"
#include "JsonHelper.h"
#include "Vehicle.h"
#include "CameraMetaData.h"

#include <QQmlEngine>

17 18 19 20 21 22 23 24 25 26
const char* CameraCalc::_valueSetIsDistanceName =           "ValueSetIsDistance";
const char* CameraCalc::_distanceToSurfaceName =            "DistanceToSurface";
const char* CameraCalc::_distanceToSurfaceRelativeName =    "DistanceToSurfaceRelative";
const char* CameraCalc::_imageDensityName =                 "ImageDensity";
const char* CameraCalc::_frontalOverlapName =               "FrontalOverlap";
const char* CameraCalc::_sideOverlapName =                  "SideOverlap";
const char* CameraCalc::_adjustedFootprintFrontalName =     "AdjustedFootprintFrontal";
const char* CameraCalc::_adjustedFootprintSideName =        "AdjustedFootprintSide";
const char* CameraCalc::_jsonCameraNameKey =                "CameraName";
const char* CameraCalc::_jsonCameraSpecTypeKey =            "CameraSpecType";
27 28 29 30 31

CameraCalc::CameraCalc(Vehicle* vehicle, QObject* parent)
    : CameraSpec                    (parent)
    , _vehicle                      (vehicle)
    , _dirty                        (false)
32
    , _cameraName                   (manualCameraName())
33
    , _disableRecalc                (false)
34
    , _distanceToSurfaceRelative    (true)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    , _metaDataMap                  (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/CameraCalc.FactMetaData.json"), this))
    , _valueSetIsDistanceFact       (0, _valueSetIsDistanceName,        FactMetaData::valueTypeBool)
    , _distanceToSurfaceFact        (0, _distanceToSurfaceName,         FactMetaData::valueTypeDouble)
    , _imageDensityFact             (0, _imageDensityName,              FactMetaData::valueTypeDouble)
    , _frontalOverlapFact           (0, _frontalOverlapName,            FactMetaData::valueTypeDouble)
    , _sideOverlapFact              (0, _sideOverlapName,               FactMetaData::valueTypeDouble)
    , _adjustedFootprintSideFact    (0, _adjustedFootprintSideName,     FactMetaData::valueTypeDouble)
    , _adjustedFootprintFrontalFact (0, _adjustedFootprintFrontalName,  FactMetaData::valueTypeDouble)
    , _imageFootprintSide           (0)
    , _imageFootprintFrontal        (0)
    , _knownCameraList              (_vehicle->staticCameraList())
{
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);

    _valueSetIsDistanceFact.setMetaData         (_metaDataMap[_valueSetIsDistanceName],         true /* setDefaultFromMetaData */);
    _distanceToSurfaceFact.setMetaData          (_metaDataMap[_distanceToSurfaceName],          true);
    _imageDensityFact.setMetaData               (_metaDataMap[_imageDensityName],               true);
    _frontalOverlapFact.setMetaData             (_metaDataMap[_frontalOverlapName],             true);
    _sideOverlapFact.setMetaData                (_metaDataMap[_sideOverlapName],                true);
54 55
    _adjustedFootprintSideFact.setMetaData      (_metaDataMap[_adjustedFootprintSideName],      true);
    _adjustedFootprintFrontalFact.setMetaData   (_metaDataMap[_adjustedFootprintFrontalName],   true);
56

57 58 59 60 61 62 63 64 65 66
    connect(&_valueSetIsDistanceFact,       &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_distanceToSurfaceFact,        &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_imageDensityFact,             &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_frontalOverlapFact,           &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_sideOverlapFact,              &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_adjustedFootprintSideFact,    &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(&_adjustedFootprintFrontalFact, &Fact::valueChanged,                            this, &CameraCalc::_setDirty);
    connect(this,                           &CameraCalc::cameraNameChanged,                 this, &CameraCalc::_setDirty);
    connect(this,                           &CameraCalc::distanceToSurfaceRelativeChanged,  this, &CameraCalc::_setDirty);

67
    connect(this, &CameraCalc::cameraNameChanged, this, &CameraCalc::_recalcTriggerDistance);
68
    connect(this, &CameraCalc::cameraNameChanged, this, &CameraCalc::_adjustDistanceToSurfaceRelative);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    connect(&_distanceToSurfaceFact,    &Fact::rawValueChanged, this, &CameraCalc::_recalcTriggerDistance);
    connect(&_imageDensityFact,         &Fact::rawValueChanged, this, &CameraCalc::_recalcTriggerDistance);
    connect(&_frontalOverlapFact,       &Fact::rawValueChanged, this, &CameraCalc::_recalcTriggerDistance);
    connect(&_sideOverlapFact,          &Fact::rawValueChanged, this, &CameraCalc::_recalcTriggerDistance);
    connect(landscape(),                &Fact::rawValueChanged, this, &CameraCalc::_recalcTriggerDistance);

    _recalcTriggerDistance();
}

void CameraCalc::setDirty(bool dirty)
{
    if (_dirty != dirty) {
        _dirty = dirty;
        emit dirtyChanged(_dirty);
    }
}

87
void CameraCalc::setCameraName(QString cameraName)
88
{
89 90 91 92 93 94 95 96 97
    if (cameraName != _cameraName) {
        _cameraName = cameraName;

        if (_cameraName == manualCameraName() || _cameraName == customCameraName()) {
            // These values are unknown for these types
            fixedOrientation()->setRawValue(false);
            minTriggerInterval()->setRawValue(0);
            if (_cameraName == manualCameraName()) {
                valueSetIsDistance()->setRawValue(false);
98 99
            }
        } else {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            // This should be a known camera name. Update camera specs to new camera

            bool foundKnownCamera = false;
            CameraMetaData* cameraMetaData = NULL;
            for (int cameraIndex=0; cameraIndex<_knownCameraList.count(); cameraIndex++) {
                cameraMetaData = _knownCameraList[cameraIndex].value<CameraMetaData*>();
                if (cameraName == cameraMetaData->name()) {
                    foundKnownCamera = true;
                    break;
                }
            }

            _disableRecalc = true;
            if (foundKnownCamera) {
                sensorWidth()->setRawValue          (cameraMetaData->sensorWidth());
                sensorHeight()->setRawValue         (cameraMetaData->sensorHeight());
                imageWidth()->setRawValue           (cameraMetaData->imageWidth());
                imageHeight()->setRawValue          (cameraMetaData->imageHeight());
                focalLength()->setRawValue          (cameraMetaData->focalLength());
                landscape()->setRawValue            (cameraMetaData->landscape());
                fixedOrientation()->setRawValue     (cameraMetaData->fixedOrientation());
                minTriggerInterval()->setRawValue   (cameraMetaData->minTriggerInterval());
            } else {
                // We don't know this camera, switch back to custom
                _cameraName = customCameraName();
                fixedOrientation()->setRawValue(false);
                minTriggerInterval()->setRawValue(0);
            }
            _disableRecalc = false;
129 130
        }

131
        emit cameraNameChanged(_cameraName);
132 133 134 135 136
    }
}

void CameraCalc::_recalcTriggerDistance(void)
{
137
    if (_disableRecalc || _cameraName == manualCameraName()) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        return;
    }

    _disableRecalc = true;

    double focalLength =    this->focalLength()->rawValue().toDouble();
    double sensorWidth =    this->sensorWidth()->rawValue().toDouble();
    double sensorHeight =   this->sensorHeight()->rawValue().toDouble();
    double imageWidth =     this->imageWidth()->rawValue().toDouble();
    double imageHeight =    this->imageHeight()->rawValue().toDouble();
    double imageDensity =   _imageDensityFact.rawValue().toDouble();

    if (focalLength <= 0 || sensorWidth <= 0 || sensorHeight <= 0 || imageWidth <= 0 || imageHeight <= 0 || imageDensity <= 0) {
        return;
    }

    if (_valueSetIsDistanceFact.rawValue().toBool()) {
        _imageDensityFact.setRawValue((_distanceToSurfaceFact.rawValue().toDouble() * sensorWidth * 100.0) / (imageWidth * focalLength));
    } else {
        _distanceToSurfaceFact.setRawValue((imageWidth * _imageDensityFact.rawValue().toDouble() * focalLength) / (sensorWidth * 100.0));
    }

    imageDensity = _imageDensityFact.rawValue().toDouble();

    if (landscape()->rawValue().toBool()) {
        _imageFootprintSide =       (imageWidth  * imageDensity) / 100.0;
        _imageFootprintFrontal =    (imageHeight * imageDensity) / 100.0;
    } else {
        _imageFootprintSide  =      (imageHeight * imageDensity) / 100.0;
        _imageFootprintFrontal =    (imageWidth  * imageDensity) / 100.0;
    }
    _adjustedFootprintSideFact.setRawValue      (_imageFootprintSide * ((100.0 - _sideOverlapFact.rawValue().toDouble()) / 100.0));
    _adjustedFootprintFrontalFact.setRawValue   (_imageFootprintFrontal * ((100.0 - _frontalOverlapFact.rawValue().toDouble()) / 100.0));

    emit imageFootprintSideChanged      (_imageFootprintSide);
    emit imageFootprintFrontalChanged   (_imageFootprintFrontal);

    _disableRecalc = false;
}

void CameraCalc::save(QJsonObject& json) const
{
180
    json[JsonHelper::jsonVersionKey] =      1;
181 182
    json[_adjustedFootprintSideName] =      _adjustedFootprintSideFact.rawValue().toDouble();
    json[_adjustedFootprintFrontalName] =   _adjustedFootprintFrontalFact.rawValue().toDouble();
183 184 185
    json[_distanceToSurfaceName] =          _distanceToSurfaceFact.rawValue().toDouble();
    json[_distanceToSurfaceRelativeName] =  _distanceToSurfaceRelative;
    json[_jsonCameraNameKey] =              _cameraName;
186

187
    if (_cameraName != manualCameraName()) {
188 189 190 191 192
        CameraSpec::save(json);
        json[_valueSetIsDistanceName] = _valueSetIsDistanceFact.rawValue().toBool();
        json[_imageDensityName] =       _imageDensityFact.rawValue().toDouble();
        json[_frontalOverlapName] =     _frontalOverlapFact.rawValue().toDouble();
        json[_sideOverlapName] =        _sideOverlapFact.rawValue().toDouble();
193 194 195
    }
}

196
bool CameraCalc::load(const QJsonObject& json, QString& errorString)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
{
    QJsonObject v1Json = json;

    if (!v1Json.contains(JsonHelper::jsonVersionKey)) {
        // Version 0 file. Differences from Version 1 for conversion:
        //  JsonHelper::jsonVersionKey not stored
        //  _jsonCameraSpecTypeKey stores CameraSpecType
        //  _jsonCameraNameKey only set if CameraSpecKnown
        int cameraSpec = v1Json[_jsonCameraSpecTypeKey].toInt(CameraSpecNone);
        if (cameraSpec == CameraSpecCustom) {
            v1Json[_jsonCameraNameKey] = customCameraName();
        } else if (cameraSpec == CameraSpecNone) {
            v1Json[_jsonCameraNameKey] = manualCameraName();
        }
        v1Json.remove(_jsonCameraSpecTypeKey);
        v1Json[JsonHelper::jsonVersionKey] = 1;
    }

    int version = v1Json[JsonHelper::jsonVersionKey].toInt();
    if (version != 1) {
        errorString = tr("CameraCalc section version %1 not supported").arg(version);
        return false;
    }

221
    QList<JsonHelper::KeyValidateInfo> keyInfoList1 = {
222
        { _jsonCameraNameKey,               QJsonValue::String, true },
223 224 225
        { _adjustedFootprintSideName,       QJsonValue::Double, true },
        { _adjustedFootprintFrontalName,    QJsonValue::Double, true },
        { _distanceToSurfaceName,           QJsonValue::Double, true },
226
        { _distanceToSurfaceRelativeName,   QJsonValue::Bool, true },
227
    };
228
    if (!JsonHelper::validateKeys(v1Json, keyInfoList1, errorString)) {
229 230 231
        return false;
    }

232
    _disableRecalc = true;
233

234 235 236 237
    setCameraName(v1Json[_jsonCameraNameKey].toString());
    _adjustedFootprintSideFact.setRawValue      (v1Json[_adjustedFootprintSideName].toDouble());
    _adjustedFootprintFrontalFact.setRawValue   (v1Json[_adjustedFootprintFrontalName].toDouble());
    _distanceToSurfaceFact.setRawValue          (v1Json[_distanceToSurfaceName].toDouble());
238

239 240
    _distanceToSurfaceRelative = v1Json[_distanceToSurfaceRelativeName].toBool();

241
    if (_cameraName != manualCameraName()) {
242 243 244 245 246 247
        QList<JsonHelper::KeyValidateInfo> keyInfoList2 = {
            { _valueSetIsDistanceName,          QJsonValue::Bool,   true },
            { _imageDensityName,                QJsonValue::Double, true },
            { _frontalOverlapName,              QJsonValue::Double, true },
            { _sideOverlapName,                 QJsonValue::Double, true },
        };
248
        if (!JsonHelper::validateKeys(v1Json, keyInfoList2, errorString)) {
249
            return false;
250
            _disableRecalc = false;
251 252
        }

253 254 255 256
        _valueSetIsDistanceFact.setRawValue (v1Json[_valueSetIsDistanceName].toBool());
        _imageDensityFact.setRawValue       (v1Json[_imageDensityName].toDouble());
        _frontalOverlapFact.setRawValue     (v1Json[_frontalOverlapName].toDouble());
        _sideOverlapFact.setRawValue        (v1Json[_sideOverlapName].toDouble());
257

258
        if (!CameraSpec::load(v1Json, errorString)) {
259 260 261 262
            return false;
        }
    }

263
    _disableRecalc = false;
264 265 266

    return true;
}
267 268 269 270 271 272 273 274 275 276

QString CameraCalc::customCameraName(void)
{
    return tr("Custom Camera");
}

QString CameraCalc::manualCameraName(void)
{
    return tr("Manual (no camera specs)");
}
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

void CameraCalc::_adjustDistanceToSurfaceRelative(void)
{
    if (!isManualCamera()) {
        setDistanceToSurfaceRelative(true);
    }
}

void CameraCalc::setDistanceToSurfaceRelative(bool distanceToSurfaceRelative)
{
    if (distanceToSurfaceRelative != _distanceToSurfaceRelative) {
        _distanceToSurfaceRelative = distanceToSurfaceRelative;
        emit distanceToSurfaceRelativeChanged(distanceToSurfaceRelative);
    }
}
292 293 294 295 296

void CameraCalc::_setDirty(void)
{
    setDirty(true);
}