CameraCalc.cc 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/****************************************************************************
 *
 *   (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>

const char* CameraCalc::_valueSetIsDistanceName =       "ValueSetIsDistance";
const char* CameraCalc::_distanceToSurfaceName =        "DistanceToSurface";
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";
24
const char* CameraCalc::_jsonCameraNameKey =            "CameraName";
25
const char* CameraCalc::_jsonCameraSpecTypeKey =        "CameraSpecType";
26 27 28 29 30

CameraCalc::CameraCalc(Vehicle* vehicle, QObject* parent)
    : CameraSpec                    (parent)
    , _vehicle                      (vehicle)
    , _dirty                        (false)
31
    , _cameraName                   (manualCameraName())
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    , _disableRecalc                (false)
    , _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);
52 53
    _adjustedFootprintSideFact.setMetaData      (_metaDataMap[_adjustedFootprintSideName],      true);
    _adjustedFootprintFrontalFact.setMetaData   (_metaDataMap[_adjustedFootprintFrontalName],   true);
54

55
    connect(this, &CameraCalc::cameraNameChanged, this, &CameraCalc::_recalcTriggerDistance);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    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);
    }
}

74
void CameraCalc::setCameraName(QString cameraName)
75
{
76 77 78 79 80 81 82 83 84
    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);
85 86
            }
        } else {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
            // 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;
116 117
        }

118
        emit cameraNameChanged(_cameraName);
119 120 121 122 123
    }
}

void CameraCalc::_recalcTriggerDistance(void)
{
124
    if (_disableRecalc || _cameraName == manualCameraName()) {
125 126 127 128 129 130 131 132 133 134 135 136 137 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
        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
{
167
    json[JsonHelper::jsonVersionKey] =      1;
168 169 170
    json[_adjustedFootprintSideName] =      _adjustedFootprintSideFact.rawValue().toDouble();
    json[_adjustedFootprintFrontalName] =   _adjustedFootprintFrontalFact.rawValue().toDouble();
    json[_distanceToSurfaceName] =  _distanceToSurfaceFact.rawValue().toDouble();
171
    json[_jsonCameraNameKey] = _cameraName;
172

173
    if (_cameraName != manualCameraName()) {
174 175 176 177 178
        CameraSpec::save(json);
        json[_valueSetIsDistanceName] = _valueSetIsDistanceFact.rawValue().toBool();
        json[_imageDensityName] =       _imageDensityFact.rawValue().toDouble();
        json[_frontalOverlapName] =     _frontalOverlapFact.rawValue().toDouble();
        json[_sideOverlapName] =        _sideOverlapFact.rawValue().toDouble();
179 180 181
    }
}

182
bool CameraCalc::load(const QJsonObject& json, QString& errorString)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
    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;
    }

207
    QList<JsonHelper::KeyValidateInfo> keyInfoList1 = {
208
        { _jsonCameraNameKey,               QJsonValue::String, true },
209 210 211
        { _adjustedFootprintSideName,       QJsonValue::Double, true },
        { _adjustedFootprintFrontalName,    QJsonValue::Double, true },
        { _distanceToSurfaceName,           QJsonValue::Double, true },
212
    };
213
    if (!JsonHelper::validateKeys(v1Json, keyInfoList1, errorString)) {
214 215 216
        return false;
    }

217
    _disableRecalc = true;
218

219 220 221 222
    setCameraName(v1Json[_jsonCameraNameKey].toString());
    _adjustedFootprintSideFact.setRawValue      (v1Json[_adjustedFootprintSideName].toDouble());
    _adjustedFootprintFrontalFact.setRawValue   (v1Json[_adjustedFootprintFrontalName].toDouble());
    _distanceToSurfaceFact.setRawValue          (v1Json[_distanceToSurfaceName].toDouble());
223

224
    if (_cameraName != manualCameraName()) {
225 226 227 228 229 230
        QList<JsonHelper::KeyValidateInfo> keyInfoList2 = {
            { _valueSetIsDistanceName,          QJsonValue::Bool,   true },
            { _imageDensityName,                QJsonValue::Double, true },
            { _frontalOverlapName,              QJsonValue::Double, true },
            { _sideOverlapName,                 QJsonValue::Double, true },
        };
231
        if (!JsonHelper::validateKeys(v1Json, keyInfoList2, errorString)) {
232
            return false;
233
            _disableRecalc = false;
234 235
        }

236 237 238 239
        _valueSetIsDistanceFact.setRawValue (v1Json[_valueSetIsDistanceName].toBool());
        _imageDensityFact.setRawValue       (v1Json[_imageDensityName].toDouble());
        _frontalOverlapFact.setRawValue     (v1Json[_frontalOverlapName].toDouble());
        _sideOverlapFact.setRawValue        (v1Json[_sideOverlapName].toDouble());
240

241
        if (!CameraSpec::load(v1Json, errorString)) {
242 243 244 245
            return false;
        }
    }

246
    _disableRecalc = false;
247 248 249

    return true;
}
250 251 252 253 254 255 256 257 258 259

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

QString CameraCalc::manualCameraName(void)
{
    return tr("Manual (no camera specs)");
}