diff --git a/samples/Geodetic2Ecef/main.cpp b/samples/Geodetic2Ecef/main.cpp index 21fec1a5e..e43891c34 100644 --- a/samples/Geodetic2Ecef/main.cpp +++ b/samples/Geodetic2Ecef/main.cpp @@ -14,40 +14,40 @@ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); BlackMisc::CApplicationContext myApplicationContext; - - QElapsedTimer timer; - qint64 duration; + + QElapsedTimer timer; + qint64 duration; double lat = 27.999999, lon = 86.999999, h = 8820.999999; // Mt Everest - - BlackCore::CEcef mediumvec; - BlackCore::CVectorGeo startVec(lat, lon, h); - startVec.print(); - - cout << std::endl; - cout << std::endl; - - BlackCore::CVectorGeo endVec; - timer.start(); - mediumvec = startVec.toCartesian(); - - duration = timer.nsecsElapsed(); - - mediumvec.print(); - - cout << std::endl; - cout << std::endl; - - cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl << std::endl; - - timer.restart(); - endVec = mediumvec.toGeodetic(); - duration = timer.nsecsElapsed(); - - endVec.print(); - - cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl << std::endl; - + BlackCore::CEcef mediumvec; + BlackCore::CVectorGeo startVec(lat, lon, h); + startVec.print(); + + cout << std::endl; + cout << std::endl; + + BlackCore::CVectorGeo endVec; + + timer.start(); + mediumvec = startVec.toCartesian(); + + duration = timer.nsecsElapsed(); + + mediumvec.print(); + + cout << std::endl; + cout << std::endl; + + cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl << std::endl; + + timer.restart(); + endVec = mediumvec.toGeodetic(); + duration = timer.nsecsElapsed(); + + endVec.print(); + + cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl << std::endl; + return a.exec(); } diff --git a/samples/blackmiscvectorgeo/main.cpp b/samples/blackmiscvectorgeo/main.cpp index 626a10b38..ff7bd5026 100644 --- a/samples/blackmiscvectorgeo/main.cpp +++ b/samples/blackmiscvectorgeo/main.cpp @@ -1,5 +1,6 @@ #include "samplesvectormatrix.h" #include "samplesgeo.h" +#include "samplesgeodetictoecef.h" #include /*! @@ -13,5 +14,6 @@ int main(int argc, char *argv[]) QCoreApplication a(argc, argv); BlackMiscTest::CSamplesVectorMatrix::samples(); BlackMiscTest::CSamplesGeo::samples(); + BlackMiscTest::CSamplesGeodeticToEcef::samples(); return a.exec(); } diff --git a/samples/blackmiscvectorgeo/sample_vector_geo.pro b/samples/blackmiscvectorgeo/sample_vector_geo.pro index 298a38e2a..94e455f70 100644 --- a/samples/blackmiscvectorgeo/sample_vector_geo.pro +++ b/samples/blackmiscvectorgeo/sample_vector_geo.pro @@ -23,8 +23,10 @@ DESTDIR = ../../bin SOURCES += main.cpp \ samplesvectormatrix.cpp \ - samplesgeo.cpp + samplesgeo.cpp \ + samplesgeodetictoecef.cpp HEADERS += \ samplesvectormatrix.h \ - samplesgeo.h + samplesgeo.h \ + samplesgeodetictoecef.h diff --git a/samples/blackmiscvectorgeo/samplesgeo.cpp b/samples/blackmiscvectorgeo/samplesgeo.cpp index 54b32b717..578f9b0cf 100644 --- a/samples/blackmiscvectorgeo/samplesgeo.cpp +++ b/samples/blackmiscvectorgeo/samplesgeo.cpp @@ -1,3 +1,8 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include "samplesgeo.h" using namespace BlackMisc::Geo; diff --git a/samples/blackmiscvectorgeo/samplesgeodetictoecef.cpp b/samples/blackmiscvectorgeo/samplesgeodetictoecef.cpp new file mode 100644 index 000000000..f16c12fa1 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesgeodetictoecef.cpp @@ -0,0 +1,44 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "samplesgeodetictoecef.h" +#include + +using namespace BlackMisc::Geo; + +namespace BlackMiscTest +{ + +/* + * Samples + */ +int CSamplesGeodeticToEcef::samples() +{ + + QElapsedTimer timer; + qint64 duration; + + double lat = 27.999999, lon = 86.999999, h = 8820.999999; // Mt Everest + CCoordinateGeodetic startVec(lat, lon, h); + std::cout << startVec << std::endl; + + timer.start(); + CCoordinateEcef mediumvec = CCoordinateTransformation::toEcef(startVec); + duration = timer.nsecsElapsed(); + std::cout << mediumvec << " "; + std::cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl; + + timer.restart(); + CCoordinateGeodetic endVec = CCoordinateTransformation::toGeodetic(mediumvec); + duration = timer.nsecsElapsed(); + + std::cout << endVec << " "; + std::cout << "Needed " << duration << " nanoseconds for the calculation!" << std::endl; + + std::cout << "-----------------------------------------------" << std::endl; + return 0; +} + +} // namespace diff --git a/samples/blackmiscvectorgeo/samplesgeodetictoecef.h b/samples/blackmiscvectorgeo/samplesgeodetictoecef.h new file mode 100644 index 000000000..6fe8a67e0 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesgeodetictoecef.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKMISCTEST_SAMPLESGEO2ECEF_H +#define BLACKMISCTEST_SAMPLESGEO2ECEF_H +#include "blackmisc/coordinatetransformation.h" + +namespace BlackMiscTest +{ + +/*! + * \brief Samples for vector / matrix + */ +class CSamplesGeodeticToEcef +{ +public: + /*! + * \brief Run the samples + */ + static int samples(); + +private: + /*! + * \brief Avoid init + */ + CSamplesGeodeticToEcef() {} +}; +} // namespace + +#endif // guard diff --git a/src/blackmisc/basestreamstringifier.h b/src/blackmisc/basestreamstringifier.h index 45f5d8afc..02a528430 100644 --- a/src/blackmisc/basestreamstringifier.h +++ b/src/blackmisc/basestreamstringifier.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace BlackMisc { @@ -20,7 +21,7 @@ template class CBaseStreamStringifier * \param uc * \return */ - friend QDebug operator<<(QDebug debug, const UsingClass &uc) + friend QDebug &operator<<(QDebug &debug, const UsingClass &uc) { const CBaseStreamStringifier &s = uc; debug << s.stringForStreaming(); @@ -33,7 +34,7 @@ template class CBaseStreamStringifier * \param uc * \return */ - friend QDataStream operator<<(QDataStream stream, const UsingClass &uc) + friend QDataStream &operator<<(QDataStream &stream, const UsingClass &uc) { const CBaseStreamStringifier &s = uc; stream << s.stringForStreaming(); @@ -46,13 +47,26 @@ template class CBaseStreamStringifier * \param uc * \return */ - friend CLogMessage operator<<(CLogMessage log, const UsingClass &uc) + friend CLogMessage &operator<<(CLogMessage &log, const UsingClass &uc) { const CBaseStreamStringifier &s = uc; log << s.stringForStreaming(); return log; } + /*! + * \brief Stream operator << for std::cout + * \param ostr + * \param uc + * \return + */ + friend std::ostream &operator<<(std::ostream &ostr, const UsingClass &uc) + { + const CBaseStreamStringifier &s = uc; + ostr << s.stringForStreaming().toStdString(); + return ostr; + } + public: /*! * \brief Virtual destructor diff --git a/src/blackmisc/coordinateecef.h b/src/blackmisc/coordinateecef.h index 51324b31b..fa1af2594 100644 --- a/src/blackmisc/coordinateecef.h +++ b/src/blackmisc/coordinateecef.h @@ -28,7 +28,7 @@ public: * \param y * \param z */ - CCoordinateEcef(qreal x, qreal y, qreal z) : CVector3DBase(x, y, z) {} + CCoordinateEcef(double x, double y, double z) : CVector3DBase(x, y, z) {} /*! * \brief Constructor by math vector @@ -40,54 +40,54 @@ public: * \brief x * \return */ - qreal x() const + double x() const { - return this->m_vector.x(); + return this->m_i; } /*! * \brief y * \return */ - qreal y() const + double y() const { - return this->m_vector.y(); + return this->m_j; } /*! * \brief z * \return */ - qreal z() const + double z() const { - return this->m_vector.z(); + return this->m_k; } /*! * \brief Set x * \param x */ - void setX(qreal x) + void setX(double x) { - this->m_vector.setX(x); + this->m_i = x; } /*! * \brief Set y * \param y */ - void setY(qreal y) + void setY(double y) { - this->m_vector.setY(y); + this->m_j = y; } /*! * \brief Set z * \param z */ - void setZ(qreal z) + void setZ(double z) { - this->m_vector.setZ(z); + this->m_k = z; } /*! @@ -107,7 +107,10 @@ protected: virtual QString stringForConverter() const { QString s = "ECEF: {x %1, y %2, z %3}"; - return s.arg(this->x()).arg(this->y()).arg(this->z()); + s = s.arg(QString::number(this->x(), 'f', 6)). + arg(QString::number(this->y(), 'f', 6)). + arg(QString::number(this->z(), 'f', 6)); + return s; } }; diff --git a/src/blackmisc/coordinategeodetic.h b/src/blackmisc/coordinategeodetic.h index cd37ba128..c2cf1b9a3 100644 --- a/src/blackmisc/coordinategeodetic.h +++ b/src/blackmisc/coordinategeodetic.h @@ -34,7 +34,7 @@ protected: virtual QString stringForConverter() const { QString s = "Geodetic: {%1, %2, %3}"; - return s.arg(this->m_latitude.unitValueRoundedWithUnit()).arg(this->m_longitude.unitValueRoundedWithUnit()).arg(this->m_height.unitValueRoundedWithUnit()); + return s.arg(this->m_latitude.unitValueRoundedWithUnit(6)).arg(this->m_longitude.unitValueRoundedWithUnit(6)).arg(this->m_height.unitValueRoundedWithUnit()); } public: @@ -64,7 +64,7 @@ public: * \param longitudeDegrees * \param heightMeters */ - CCoordinateGeodetic(qreal latitudeDegrees, qreal longitudeDegrees, qreal heightMeters) : + CCoordinateGeodetic(double latitudeDegrees, double longitudeDegrees, double heightMeters) : m_latitude(latitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_longitude(longitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_height(heightMeters, BlackMisc::PhysicalQuantities::CLengthUnit::m()) {} /*! diff --git a/src/blackmisc/coordinatened.h b/src/blackmisc/coordinatened.h index b0fda2603..5c9fe7c65 100644 --- a/src/blackmisc/coordinatened.h +++ b/src/blackmisc/coordinatened.h @@ -31,7 +31,10 @@ protected: virtual QString stringForConverter() const { QString s = "NED: {N %1, E %2, D %3}"; - return s.arg(this->north()).arg(this->east()).arg(this->down()); + s = s.arg(QString::number(this->north(), 'f', 6)). + arg(QString::number(this->east(), 'f', 6)). + arg(QString::number(this->down(), 'f', 6)); + return s; } public: @@ -53,7 +56,7 @@ public: * \param east * \param down */ - CCoordinateNed(const CCoordinateGeodetic &referencePosition, qreal north, qreal east, qreal down) : CVector3DBase(north, east, down), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} + CCoordinateNed(const CCoordinateGeodetic &referencePosition, double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} /*! * \brief Copy constructor @@ -135,54 +138,54 @@ public: * \brief North * \return */ - qreal north() const + double north() const { - return this->m_vector.x(); + return this->m_i; } /*! * \brief East * \return */ - qreal east() const + double east() const { - return this->m_vector.y(); + return this->m_j; } /*! * \brief Down * \return */ - qreal down() const + double down() const { - return this->m_vector.z(); + return this->m_k; } /*! * \brief Set north * \param north */ - void setNorth(qreal north) + void setNorth(double north) { - this->m_vector.setX(north); + this->m_i = north; } /*! * \brief Set east * \param east */ - void setEast(qreal east) + void setEast(double east) { - this->m_vector.setY(east); + this->m_j = east; } /*! * \brief Set down * \param down */ - void setDown(qreal down) + void setDown(double down) { - this->m_vector.setZ(down); + this->m_k = down; } /*! diff --git a/src/blackmisc/coordinatetransformation.cpp b/src/blackmisc/coordinatetransformation.cpp index 0fc53f4b2..113c5b425 100644 --- a/src/blackmisc/coordinatetransformation.cpp +++ b/src/blackmisc/coordinatetransformation.cpp @@ -68,8 +68,8 @@ CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateGeodetic &geo CLatitude lat = geo.latitude(); CLongitude lon = geo.longitude(); - qreal latDeg = lat.value(CAngleUnit::deg()); - qreal lonDeg = lon.value(CAngleUnit::deg()); + double latDeg = lat.value(CAngleUnit::deg()); + double lonDeg = lon.value(CAngleUnit::deg()); double phi = lat.value(CAngleUnit::rad()); double lambdaRad = lon.value(CAngleUnit::rad()); @@ -90,7 +90,6 @@ CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateGeodetic &geo double Y = X * slambda; X *= clambda; double Z = (e2m() * n + h) * sphi; - CCoordinateEcef result(X, Y, Z); return result; } @@ -246,6 +245,7 @@ CCoordinateGeodetic CCoordinateTransformation::toGeodetic(const CCoordinateEcef CLatitude(latRad, CAngleUnit::rad()), CLongitude(lonRad, CAngleUnit::rad()), CLength(h, CLengthUnit::m())); + result.switchUnit(CAngleUnit::deg()); return result; } diff --git a/src/blackmisc/coordinatetransformation.h b/src/blackmisc/coordinatetransformation.h index 4b5c73c31..26afb12aa 100644 --- a/src/blackmisc/coordinatetransformation.h +++ b/src/blackmisc/coordinatetransformation.h @@ -34,9 +34,9 @@ private: * \brief Equatorial radius of WGS84 ellipsoid (6378137 m) * \return */ - static const qreal &EarthRadiusMeters() + static const double &EarthRadiusMeters() { - static qreal erm = 6378137.0; + static double erm = 6378137.0; return erm; } @@ -44,9 +44,9 @@ private: * \brief Flattening of WGS84 ellipsoid (1/298.257223563) * \return */ - static const qreal &Flattening() + static const double &Flattening() { - static qreal f = 1 / 298.257223563; + static double f = 1 / 298.257223563; return f; } @@ -54,9 +54,9 @@ private: * \brief First eccentricity squared * \return */ - static const qreal &e2() + static const double &e2() { - static qreal e2 = (Flattening() * (2 - Flattening())); + static double e2 = (Flattening() * (2 - Flattening())); return e2; } @@ -64,9 +64,9 @@ private: * \brief First eccentricity to power of four * \return */ - static const qreal &e4() + static const double &e4() { - static qreal e4 = BlackMisc::Math::CMath::square(e2()); + static double e4 = BlackMisc::Math::CMath::square(e2()); return e4; } @@ -74,9 +74,9 @@ private: * \brief First eccentricity squared absolute * \return */ - static const qreal &e2abs() + static const double &e2abs() { - static qreal e2abs = abs(e2()); + static double e2abs = abs(e2()); return e2abs; } @@ -84,9 +84,9 @@ private: * \brief Eccentricity e2m * \return */ - static const qreal &e2m() + static const double &e2m() { - static qreal e2m = BlackMisc::Math::CMath::square(1 - Flattening()); + static double e2m = BlackMisc::Math::CMath::square(1 - Flattening()); return e2m; } diff --git a/src/blackmisc/geoearthangle.h b/src/blackmisc/geoearthangle.h index 4494ee982..1c33b8c53 100644 --- a/src/blackmisc/geoearthangle.h +++ b/src/blackmisc/geoearthangle.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -20,7 +20,7 @@ protected: /*! * \brief Default constructor */ - CEarthAngle() : CAngle() {} + CEarthAngle() : CAngle(0.0, BlackMisc::PhysicalQuantities::CAngleUnit::deg()) {} /*! * \brief Copy constructor @@ -35,6 +35,15 @@ protected: */ CEarthAngle(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit): CAngle(value, unit) {} + /*! + * \brief String for converter and streaming + * \return + */ + virtual QString stringForConverter() const + { + return this->unitValueRoundedWithUnit(6); + } + public: /*! diff --git a/src/blackmisc/mathmatrix1x3.h b/src/blackmisc/mathmatrix1x3.h index bcf4c2f6b..53694d32e 100644 --- a/src/blackmisc/mathmatrix1x3.h +++ b/src/blackmisc/mathmatrix1x3.h @@ -29,7 +29,7 @@ public: * \brief init with value * \param fillValue */ - CMatrix1x3(qreal fillValue) : CMatrixBase(fillValue) {} + CMatrix1x3(double fillValue) : CMatrixBase(fillValue) {} /*! * \brief Copy constructor @@ -43,7 +43,7 @@ public: * \param c2 * \param c3 */ - CMatrix1x3(qreal c1, qreal c2, qreal c3) : CMatrixBase() + CMatrix1x3(double c1, double c2, double c3) : CMatrixBase() { this->m_matrix(0, 0) = c1; this->m_matrix(0, 1) = c2; diff --git a/src/blackmisc/mathmatrix3x3.cpp b/src/blackmisc/mathmatrix3x3.cpp index 6c28903bf..d3e51f3d4 100644 --- a/src/blackmisc/mathmatrix3x3.cpp +++ b/src/blackmisc/mathmatrix3x3.cpp @@ -74,8 +74,6 @@ CMatrix3x1 CMatrix3x3::getColumn(int column) const return CMatrix3x1(this->getElement(0, column), this->getElement(1, column), this->getElement(2, column)); } - - } // namespace } // namespace diff --git a/src/blackmisc/mathmatrix3x3.h b/src/blackmisc/mathmatrix3x3.h index e3bf25a50..7fb438049 100644 --- a/src/blackmisc/mathmatrix3x3.h +++ b/src/blackmisc/mathmatrix3x3.h @@ -28,7 +28,7 @@ public: * \brief init with value * \param fillValue */ - CMatrix3x3(qreal fillValue) : CMatrixBase(fillValue) {} + CMatrix3x3(double fillValue) : CMatrixBase(fillValue) {} /*! * \brief Copy constructor @@ -40,7 +40,7 @@ public: * \brief Calculates the determinant of the matrix * \return */ - qreal determinant() const; + double determinant() const; /*! * \brief Calculate the inverse @@ -88,7 +88,8 @@ public: * \param vector * \return */ - CVector3D operator *(const CVector3D &vector) const { + CVector3D operator *(const CVector3D &vector) const + { CVector3D v(vector); v.matrixMultiplication(*this); return v; diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index 4413b474b..df0fa3012 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -14,7 +14,7 @@ namespace Math /* * Get element by column / row */ -template qreal CMatrixBase::getElement(size_t row, size_t column) const +template double CMatrixBase::getElement(size_t row, size_t column) const { this->checkRange(row, column); return this->m_matrix(row, column); @@ -23,7 +23,7 @@ template qreal CMatrixBase void CMatrixBase::setElement(size_t row, size_t column, qreal value) +template void CMatrixBase::setElement(size_t row, size_t column, double value) { this->checkRange(row, column); this->m_matrix(row, column) = value; @@ -53,6 +53,21 @@ template void CMatrixBase bool CMatrixBase::isZero() const +{ + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + if (this->m_matrix(r, c) != 0) return false; + } + } + return true; +} + /* * Convert to string */ diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index 73a585671..0fffb1e4d 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -23,7 +23,7 @@ template class CMatrixBase : public Bla protected: // no bug, Qt expects columns rows - QGenericMatrix m_matrix; //!< backing data + QGenericMatrix m_matrix; //!< backing data /*! * \brief Conversion to string @@ -41,7 +41,7 @@ public: * \brief Constructor with init value * \param fillValue */ - CMatrixBase(qreal fillValue) : m_matrix() { this->m_matrix.fill(fillValue);} + CMatrixBase(double fillValue) : m_matrix() { this->m_matrix.fill(fillValue);} /*! * \brief Copy constructor @@ -93,7 +93,7 @@ public: * \param factor * \return */ - CMatrixBase &operator *=(qreal factor) + CMatrixBase &operator *=(double factor) { this->m_matrix *= factor; return (*this); @@ -104,7 +104,7 @@ public: * \param factor * \return */ - CMatrixBase &operator *(qreal factor) + CMatrixBase &operator *(double factor) { ImplMatrix m(0.0); m += (*this); @@ -124,7 +124,7 @@ public: * \param factor * \return */ - CMatrixBase &operator /=(qreal factor) + CMatrixBase &operator /=(double factor) { this->m_matrix /= factor; return (*this); @@ -135,7 +135,7 @@ public: * \param factor * \return */ - CMatrixBase &operator /(qreal factor) + CMatrixBase &operator /(double factor) { ImplMatrix m(0.0); m += (*this); @@ -219,11 +219,17 @@ public: */ void setZero() { this->m_matrix.fill(0.0); } + /*! + * \brief isZero + * \return + */ + bool isZero() const; + /*! * \brief Set a dedicated value * \param value */ - void fill(qreal value) { this->m_matrix.fill(value); } + void fill(double value) { this->m_matrix.fill(value); } /*! * \brief Get element @@ -231,7 +237,7 @@ public: * \param column * \return */ - qreal getElement(size_t row, size_t column) const; + double getElement(size_t row, size_t column) const; /*! * \brief Get element @@ -239,14 +245,14 @@ public: * \param column * \param value */ - void setElement(size_t row, size_t column, qreal value); + void setElement(size_t row, size_t column, double value); /*! * \brief Get element by () * \param column * \return */ - qreal &operator()(size_t row, size_t column) + double &operator()(size_t row, size_t column) { this->checkRange(row, column); return this->m_matrix(row, column); @@ -257,7 +263,7 @@ public: * \param column * \return */ - qreal operator()(size_t row, size_t column) const + double operator()(size_t row, size_t column) const { return this->getElement(row, column); } diff --git a/src/blackmisc/mathvector3d.h b/src/blackmisc/mathvector3d.h index ab5f5ab21..00ae8d8c2 100644 --- a/src/blackmisc/mathvector3d.h +++ b/src/blackmisc/mathvector3d.h @@ -25,66 +25,72 @@ public: * \param j * \param k */ - CVector3D(qreal i, qreal j, qreal k) : CVector3DBase(i, j, k) {} + CVector3D(double i, double j, double k) : CVector3DBase(i, j, k) {} /*! * \brief Constructor by value * \param value */ - CVector3D(qreal value) : CVector3DBase(value) {} + CVector3D(double value) : CVector3DBase(value) {} + + /*! + * \brief Copy constructor + * \param otherVector + */ + CVector3D(const CVector3D &otherVector) : CVector3DBase(otherVector) {} /*! * \brief i * \return */ - qreal i() const + double i() const { - return this->m_vector.x(); + return this->m_i; } /*! * \brief j * \return */ - qreal j() const + double j() const { - return this->m_vector.y(); + return this->m_j; } /*! * \brief k * \return */ - qreal k() const + double k() const { - return this->m_vector.z(); + return this->m_k; } /*! * \brief Set i * \param i */ - void setI(qreal i) + void setI(double i) { - this->m_vector.setX(i); + this->m_i = i; } /*! * \brief Set j * \param j */ - void setJ(qreal j) + void setJ(double j) { - this->m_vector.setY(j); + this->m_j = j; } /*! * \brief Set k * \param k */ - void setK(qreal k) + void setK(double k) { - this->m_vector.setZ(k); + this->m_k = k; } }; diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp index d4ee19fb9..813d308f2 100644 --- a/src/blackmisc/mathvector3dbase.cpp +++ b/src/blackmisc/mathvector3dbase.cpp @@ -19,7 +19,9 @@ namespace Math template QString CVector3DBase::stringForConverter() const { QString s = ("{%1, %2, %3}"); - s = s.arg(this->m_vector.x()).arg(this->m_vector.y()).arg(this->m_vector.z()); + s = s.arg(QString::number(this->m_i, 'f')). + arg(QString::number(this->m_j, 'f')). + arg(QString::number(this->m_k, 'f')); return s; } @@ -34,29 +36,29 @@ template void CVector3DBase::setZero() /* * Vector to zero */ -template void CVector3DBase::fill(qreal value) +template void CVector3DBase::fill(double value) { - this->m_vector.setX(value); - this->m_vector.setY(value); - this->m_vector.setZ(value); + this->m_i = value; + this->m_j = value; + this->m_k = value; } /* * Element */ -template qreal CVector3DBase::getElement(size_t row) const +template double CVector3DBase::getElement(size_t row) const { - qreal d; + double d; switch (row) { case 0: - d = this->m_vector.x(); + d = this->m_i; break; case 1: - d = this->m_vector.y(); + d = this->m_j; break; case 2: - d = this->m_vector.z(); + d = this->m_k; break; default: Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); @@ -69,18 +71,18 @@ template qreal CVector3DBase::getElement(size_t row /* * Set given element */ -template void CVector3DBase::setElement(size_t row, qreal value) +template void CVector3DBase::setElement(size_t row, double value) { switch (row) { case 0: - this->m_vector.setX(value); + this->m_i = value; break; case 1: - this->m_vector.setY(value); + this->m_j = value; break; case 2: - this->m_vector.setZ(value); + this->m_k = value; break; default: Q_ASSERT_X(true, "setElement", "Detected invalid index in 3D vector"); @@ -89,15 +91,36 @@ template void CVector3DBase::setElement(size_t row, } } +/* + * Corss product + */ +template ImplClass CVector3DBase::crossProduct(const ImplClass &otherVector) const +{ + ImplClass v(otherVector); + v.m_i = this->m_j * otherVector.m_k - this->m_k * otherVector.m_j; + v.m_j = this->m_k * otherVector.m_i - this->m_j * otherVector.m_k; + v.m_k = this->m_i * otherVector.m_j - this->m_j * otherVector.m_i; + return v; +} + +/* + * Cross product + */ +template double CVector3DBase::dotProduct(const ImplClass &otherVector) const +{ + return this->m_i * otherVector.m_i + this->m_j * otherVector.m_j + this->m_k * otherVector.m_k; +} + + /* * Multiply with matrix */ template void CVector3DBase::matrixMultiplication(const CMatrix3x3 &matrix) { CMatrix3x1 m = matrix * (this->toMatrix3x1()); - this->m_vector.setX(m(0,0)); - this->m_vector.setY(m(1,0)); - this->m_vector.setZ(m(2,0)); + this->m_i = m(0, 0); + this->m_j = m(1, 0); + this->m_k = m(2, 0); } /* @@ -105,7 +128,7 @@ template void CVector3DBase::matrixMultiplication(c */ template CMatrix3x1 CVector3DBase::toMatrix3x1() const { - return CMatrix3x1(this->m_vector.x(), this->m_vector.y(), this->m_vector.z()); + return CMatrix3x1(this->m_i, this->m_j, this->m_k); } // see here for the reason of thess forward instantiations diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index 99adde0a5..dd93c2c14 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -7,7 +7,6 @@ #define BLACKMISC_MATHVECTOR3DBASE_H #include "blackmisc/basestreamstringifier.h" -#include namespace BlackMisc { @@ -25,12 +24,15 @@ template class CVector3DBase : public CBaseStreamStringifiergetElement(row); } + double operator[](size_t row) const { return this->getElement(row); } /*! @@ -99,7 +107,7 @@ public: * \param column * \return */ - qreal operator()(size_t row) const { return this->getElement(row); } + double operator()(size_t row) const { return this->getElement(row); } /*! * \brief Equal operator == @@ -109,7 +117,9 @@ public: bool operator ==(const CVector3DBase &otherVector) const { if (this == &otherVector) return true; - return this->m_vector == otherVector.m_vector; + return this->m_i == otherVector.m_i && + this->m_j == otherVector.m_j && + this->m_k == otherVector.m_k; } /*! @@ -130,8 +140,10 @@ public: */ CVector3DBase &operator =(const CVector3DBase &otherVector) { - if (this == &otherVector) return *this; // Same object? - this->m_vector = otherVector.m_vector; + if (this == &otherVector) return *this; // Same object? + this->m_i = otherVector.m_i; + this->m_j = otherVector.m_j; + this->m_k = otherVector.m_k; return (*this); } @@ -142,7 +154,9 @@ public: */ CVector3DBase &operator +=(const CVector3DBase &otherVector) { - this->m_vector += otherVector.m_vector; + this->m_i += otherVector.m_i; + this->m_j += otherVector.m_j; + this->m_k += otherVector.m_k; return (*this); } @@ -166,12 +180,14 @@ public: */ CVector3DBase &operator -=(const CVector3DBase &otherVector) { - this->m_vector -= otherVector.m_vector; + this->m_i -= otherVector.m_i; + this->m_j -= otherVector.m_j; + this->m_k -= otherVector.m_k; return (*this); } /*! - * \brief Operator + + * \brief Operator - * \param otherVector * \return */ @@ -190,7 +206,9 @@ public: */ CVector3DBase &operator *=(const CVector3DBase &otherVector) { - this->m_vector *= otherVector.m_vector; + this->m_i *= otherVector.m_i; + this->m_j *= otherVector.m_j; + this->m_k *= otherVector.m_k; return (*this); } @@ -214,7 +232,9 @@ public: */ CVector3DBase &operator /=(const CVector3DBase &otherVector) { - this->m_vector *= otherVector.reciprocalValues().m_vector; + this->m_i /= otherVector.m_i; + this->m_j /= otherVector.m_j; + this->m_k /= otherVector.m_k; return (*this); } @@ -236,22 +256,14 @@ public: * \param otherVector * \return */ - qreal dotProduct(const ImplClass &otherVector) const - { - return QVector3D::dotProduct(this->m_vector, otherVector.m_vector); - } + double dotProduct(const ImplClass &otherVector) const; /*! * \brief Cross product * \param otherVector * \return */ - ImplClass crossProduct(const ImplClass &otherVector) const - { - ImplClass v; - v.m_vector = QVector3D::crossProduct(this->m_vector, otherVector.m_vector); - return v; - } + ImplClass crossProduct(const ImplClass &otherVector) const; /*! * \brief Matrix * this vector @@ -268,9 +280,9 @@ public: ImplClass reciprocalValues() const { ImplClass v; - v.m_vector.setX(1 / this->m_vector.x()); - v.m_vector.setY(1 / this->m_vector.y()); - v.m_vector.setZ(1 / this->m_vector.z()); + v.m_i = (1 / this->m_i); + v.m_j = (1 / this->m_j); + v.m_k = (1 / this->m_j); return v; } @@ -278,18 +290,18 @@ public: * \brief Length * \return */ - qreal length()const + double length()const { - return this->m_vector.length(); + return this->m_i * this->m_j + this->m_k; } /*! * \brief Length squared * \return */ - qreal lengthSquared()const + double lengthSquared()const { - return this->m_vector.lengthSquared(); + return this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k; } /*! @@ -302,7 +314,7 @@ public: * \brief Magnitude * \return */ - qreal magnitude() const + double magnitude() const { return sqrt(this->lengthSquared()); } diff --git a/tests/blackmisc/test_blackmisc.pro b/tests/blackmisc/test_blackmisc.pro index 893844d2d..74a17a3d9 100644 --- a/tests/blackmisc/test_blackmisc.pro +++ b/tests/blackmisc/test_blackmisc.pro @@ -1,5 +1,4 @@ QT += core testlib -QT -= gui TARGET = test_blackmisc TEMPLATE = app @@ -11,12 +10,14 @@ DEPENDPATH += . ../../src INCLUDEPATH += . ../../src SOURCES += main.cpp testmain.cpp \ testphysicalquantitiesbase.cpp \ - testaviationbase.cpp + testaviationbase.cpp \ + testvectormatrix.cpp HEADERS += testmain.h \ testphysicalquantitiesbase.h \ blackmisctest.h \ - testaviationbase.h + testaviationbase.h \ + testvectormatrix.h win32-msvc* { PRE_TARGETDEPS += ../../lib/blackmisc.lib diff --git a/tests/blackmisc/testaviationbase.h b/tests/blackmisc/testaviationbase.h index 1959ef09b..f7f62e530 100644 --- a/tests/blackmisc/testaviationbase.h +++ b/tests/blackmisc/testaviationbase.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -14,7 +14,8 @@ #include "blackmisc/aviotransponder.h" #include -namespace BlackMiscTest { +namespace BlackMiscTest +{ /*! * \brief Aviation classes basic tests @@ -52,4 +53,4 @@ private slots: } // namespace -#endif // BLACKMISCTEST_TESTAVIATIONBASE_H +#endif // guard diff --git a/tests/blackmisc/testmain.cpp b/tests/blackmisc/testmain.cpp index a655c7f98..e8e070df7 100644 --- a/tests/blackmisc/testmain.cpp +++ b/tests/blackmisc/testmain.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -16,9 +16,11 @@ int CTestMain::unitMain(int argc, char *argv[]) { CTestPhysicalQuantitiesBase pqBaseTests ; CTestAviationBase avBaseTests; + CTestVectorMatrix vmTests; status |= QTest::qExec(&pqBaseTests, argc, argv); status |= QTest::qExec(&avBaseTests, argc, argv); + status |= QTest::qExec(&vmTests, argc, argv); } return status; } -} +} // namespace diff --git a/tests/blackmisc/testmain.h b/tests/blackmisc/testmain.h index 442a81580..4ce4681e0 100644 --- a/tests/blackmisc/testmain.h +++ b/tests/blackmisc/testmain.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -8,9 +8,11 @@ #include "testphysicalquantitiesbase.h" #include "testaviationbase.h" +#include "testvectormatrix.h" #include -namespace BlackMiscTest{ +namespace BlackMiscTest +{ /*! * Class firing of all unit tests in this namespace. diff --git a/tests/blackmisc/testphysicalquantitiesbase.h b/tests/blackmisc/testphysicalquantitiesbase.h index 081d0d9af..bc7cd7290 100644 --- a/tests/blackmisc/testphysicalquantitiesbase.h +++ b/tests/blackmisc/testphysicalquantitiesbase.h @@ -1,3 +1,8 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #ifndef BLACKMISCTEST_TESTPHYSICALQUANTITIESBASE_H #define BLACKMISCTEST_TESTPHYSICALQUANTITIESBASE_H diff --git a/tests/blackmisc/testvectormatrix.cpp b/tests/blackmisc/testvectormatrix.cpp new file mode 100644 index 000000000..7036c501e --- /dev/null +++ b/tests/blackmisc/testvectormatrix.cpp @@ -0,0 +1,35 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "testvectormatrix.h" + +using namespace BlackMisc::Math; + +namespace BlackMiscTest +{ + +/* + * Basic tests vector + */ +void CTestVectorMatrix::vectorBasics() +{ + +} + +/* + * Matrix tests + */ +void CTestVectorMatrix::matrixBasics() +{ + CMatrix3x3 m1; + CMatrix3x3 m2 = m1 - m1; + QVERIFY2(m1.isIdentity(), "Default matrix should be identity"); + QVERIFY2(m2.isZero(), "Matrix should be zero"); +} + +} // namespace + +#include "testvectormatrix.h" + diff --git a/tests/blackmisc/testvectormatrix.h b/tests/blackmisc/testvectormatrix.h new file mode 100644 index 000000000..f88ed7ce2 --- /dev/null +++ b/tests/blackmisc/testvectormatrix.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKMISCTEST_TESTVECTORMATRIX_H +#define BLACKMISCTEST_TESTVECTORMATRIX_H + +#include "blackmisc/mathmatrix3x3.h" +#include + +namespace BlackMiscTest +{ + +/*! + * \brief Vector and Matrix classes basic tests + */ +class CTestVectorMatrix : public QObject +{ + Q_OBJECT + +public: + /*! + * \brief Standard test case constructor + * \param parent + */ + explicit CTestVectorMatrix(QObject *parent = 0) : QObject(parent) {} + +private slots: + /*! + * \brief Basic unit tests for physical units + */ + void vectorBasics(); + /*! + * \brief Vertical positions + */ + void matrixBasics(); +}; + +} // namespace + +#endif // guard