From 5bf308c54b011844347e4b964c566a244dc92706 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 18 Apr 2013 01:04:21 +0200 Subject: [PATCH] Initial refactoring of vector and matrix classes --- client.pro | 1 + samples/blackmiscquantities/main.cpp | 7 +- .../blackmiscquantities/samplesaviation.cpp | 5 + samples/blackmiscquantities/samplesaviation.h | 11 +- .../samplesphysicalquantities.cpp | 5 + .../samplesphysicalquantities.h | 11 +- samples/blackmiscvectorgeo/main.cpp | 17 ++ .../blackmiscvectorgeo/sample_vector_geo.pro | 30 ++ samples/blackmiscvectorgeo/samplesgeo.cpp | 38 +++ samples/blackmiscvectorgeo/samplesgeo.h | 33 ++ .../samplesvectormatrix.cpp | 29 ++ .../blackmiscvectorgeo/samplesvectormatrix.h | 29 ++ src/blackmisc/basestreamstringifier.h | 13 + src/blackmisc/blackmisc.h | 15 + src/blackmisc/blackmisc.pro | 24 +- src/blackmisc/coordinateecef.h | 92 ++++++ src/blackmisc/coordinategeodetic.h | 168 +++++++++++ src/blackmisc/coordinatened.h | 92 ++++++ src/blackmisc/geoearthangle.h | 168 +++++++++++ src/blackmisc/geolatitude.h | 54 ++++ src/blackmisc/geolatlonbase.cpp | 15 + src/blackmisc/geolongitude.h | 53 ++++ src/blackmisc/mathematics.cpp | 38 +++ src/blackmisc/mathematics.h | 64 ++++ src/blackmisc/mathmatrix3x1.h | 65 ++++ src/blackmisc/mathmatrix3x3.cpp | 57 ++++ src/blackmisc/mathmatrix3x3.h | 57 ++++ src/blackmisc/mathmatrixbase.cpp | 71 +++++ src/blackmisc/mathmatrixbase.h | 267 +++++++++++++++++ src/blackmisc/mathvector3d.h | 94 ++++++ src/blackmisc/mathvector3dbase.cpp | 80 +++++ src/blackmisc/mathvector3dbase.h | 283 ++++++++++++++++++ src/blackmisc/pqphysicalquantity.cpp | 2 +- src/blackmisc/pqphysicalquantity.h | 56 ++-- 34 files changed, 2004 insertions(+), 40 deletions(-) create mode 100644 samples/blackmiscvectorgeo/main.cpp create mode 100644 samples/blackmiscvectorgeo/sample_vector_geo.pro create mode 100644 samples/blackmiscvectorgeo/samplesgeo.cpp create mode 100644 samples/blackmiscvectorgeo/samplesgeo.h create mode 100644 samples/blackmiscvectorgeo/samplesvectormatrix.cpp create mode 100644 samples/blackmiscvectorgeo/samplesvectormatrix.h create mode 100644 src/blackmisc/coordinateecef.h create mode 100644 src/blackmisc/coordinategeodetic.h create mode 100644 src/blackmisc/coordinatened.h create mode 100644 src/blackmisc/geoearthangle.h create mode 100644 src/blackmisc/geolatitude.h create mode 100644 src/blackmisc/geolatlonbase.cpp create mode 100644 src/blackmisc/geolongitude.h create mode 100644 src/blackmisc/mathematics.cpp create mode 100644 src/blackmisc/mathematics.h create mode 100644 src/blackmisc/mathmatrix3x1.h create mode 100644 src/blackmisc/mathmatrix3x3.cpp create mode 100644 src/blackmisc/mathmatrix3x3.h create mode 100644 src/blackmisc/mathmatrixbase.cpp create mode 100644 src/blackmisc/mathmatrixbase.h create mode 100644 src/blackmisc/mathvector3d.h create mode 100644 src/blackmisc/mathvector3dbase.cpp create mode 100644 src/blackmisc/mathvector3dbase.h diff --git a/client.pro b/client.pro index 0f5af439c..eab19e0e9 100644 --- a/client.pro +++ b/client.pro @@ -48,6 +48,7 @@ equals(WITH_SAMPLES, ON) { SUBDIRS += samples/interpolator/sample_interpolator.pro SUBDIRS += samples/logging/sample_logging.pro SUBDIRS += samples/blackmiscquantities/sample_quantities_avionics.pro + SUBDIRS += samples/blackmiscvectorgeo/sample_vector_geo.pro } equals(WITH_UNITTESTS, ON) { diff --git a/samples/blackmiscquantities/main.cpp b/samples/blackmiscquantities/main.cpp index 8cc99d400..906cb356f 100644 --- a/samples/blackmiscquantities/main.cpp +++ b/samples/blackmiscquantities/main.cpp @@ -1,6 +1,11 @@ -#include +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "samplesphysicalquantities.h" #include "samplesaviation.h" +#include using namespace BlackMisc; using namespace BlackMiscTest; diff --git a/samples/blackmiscquantities/samplesaviation.cpp b/samples/blackmiscquantities/samplesaviation.cpp index ad495aa30..acba81946 100644 --- a/samples/blackmiscquantities/samplesaviation.cpp +++ b/samples/blackmiscquantities/samplesaviation.cpp @@ -1,3 +1,8 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "samplesaviation.h" using namespace BlackMisc::Aviation; diff --git a/samples/blackmiscquantities/samplesaviation.h b/samples/blackmiscquantities/samplesaviation.h index 61fe0377f..3cd0cafbf 100644 --- a/samples/blackmiscquantities/samplesaviation.h +++ b/samples/blackmiscquantities/samplesaviation.h @@ -1,9 +1,14 @@ -#ifndef BLACKMISC_SAMPLESAVIATION_H -#define BLACKMISC_SAMPLESAVIATION_H +/* Copyright (C) 2013 VATSIM Community / authors + * 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_SAMPLESAVIATION_H +#define BLACKMISCTEST_SAMPLESAVIATION_H + +#include "blackmisc/pqconstants.h" #include "blackmisc/avheading.h" #include "blackmisc/avverticalpositions.h" -#include "blackmisc/pqconstants.h" #include "blackmisc/aviocomsystem.h" #include "blackmisc/avionavsystem.h" #include "blackmisc/aviotransponder.h" diff --git a/samples/blackmiscquantities/samplesphysicalquantities.cpp b/samples/blackmiscquantities/samplesphysicalquantities.cpp index ed57baacd..b813f3bd2 100644 --- a/samples/blackmiscquantities/samplesphysicalquantities.cpp +++ b/samples/blackmiscquantities/samplesphysicalquantities.cpp @@ -1,3 +1,8 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "samplesphysicalquantities.h" using namespace BlackMisc::PhysicalQuantities; diff --git a/samples/blackmiscquantities/samplesphysicalquantities.h b/samples/blackmiscquantities/samplesphysicalquantities.h index 11a9b33d7..5fba7dd00 100644 --- a/samples/blackmiscquantities/samplesphysicalquantities.h +++ b/samples/blackmiscquantities/samplesphysicalquantities.h @@ -1,5 +1,10 @@ -#ifndef BLACKMISC_SAMPLESPHYSICALQUANTITIES_H -#define BLACKMISC_SAMPLESPHYSICALQUANTITIES_H +/* Copyright (C) 2013 VATSIM Community / authors + * 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_SAMPLESPHYSICALQUANTITIES_H +#define BLACKMISCTEST_SAMPLESPHYSICALQUANTITIES_H #include "blackmisc/pqconstants.h" #include "blackmisc/debug.h" @@ -20,4 +25,4 @@ public: }; } -#endif // BLACKMISC_SAMPLESPHYSICALQUANTITIES_H +#endif // guard diff --git a/samples/blackmiscvectorgeo/main.cpp b/samples/blackmiscvectorgeo/main.cpp new file mode 100644 index 000000000..626a10b38 --- /dev/null +++ b/samples/blackmiscvectorgeo/main.cpp @@ -0,0 +1,17 @@ +#include "samplesvectormatrix.h" +#include "samplesgeo.h" +#include + +/*! + * \brief Main entry + * \param argc + * \param argv + * \return + */ +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + BlackMiscTest::CSamplesVectorMatrix::samples(); + BlackMiscTest::CSamplesGeo::samples(); + return a.exec(); +} diff --git a/samples/blackmiscvectorgeo/sample_vector_geo.pro b/samples/blackmiscvectorgeo/sample_vector_geo.pro new file mode 100644 index 000000000..298a38e2a --- /dev/null +++ b/samples/blackmiscvectorgeo/sample_vector_geo.pro @@ -0,0 +1,30 @@ +QT += core + +TARGET = sample_vector_geo +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +DEPENDPATH += . ../../src +INCLUDEPATH += . ../../src + +win32-msvc* { + PRE_TARGETDEPS += ../../lib/blackmisc.lib + LIBS += ../../lib/blackmisc.lib +} + +!win32-msvc* { + PRE_TARGETDEPS += ../../lib/libblackmisc.a + LIBS += ../../lib/libblackmisc.a +} + +DESTDIR = ../../bin + +SOURCES += main.cpp \ + samplesvectormatrix.cpp \ + samplesgeo.cpp + +HEADERS += \ + samplesvectormatrix.h \ + samplesgeo.h diff --git a/samples/blackmiscvectorgeo/samplesgeo.cpp b/samples/blackmiscvectorgeo/samplesgeo.cpp new file mode 100644 index 000000000..dac8cb181 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesgeo.cpp @@ -0,0 +1,38 @@ +#include "samplesgeo.h" + +using namespace BlackMisc::Geo; +using namespace BlackMisc::PhysicalQuantities; + +namespace BlackMiscTest +{ + +/* + * Samples + */ +int CSamplesGeo::samples() +{ + CGeoLatitude lat1(20.0, CAngleUnit::deg()); + CGeoLatitude lat2 = lat1; + CGeoLatitude lat3 = lat1 - lat2; + + qDebug() << lat1 << lat2 << lat3; + qDebug() << (lat1 + lat2) << (lat1 - lat2); + + lat3 += lat1; + CGeoLongitude lon1(33.0, CAngleUnit::deg()); + qDebug() << lon1 << lat3; + +// lat3 += lon1; // must not work +// lat3 = lon1; //must not work +// CGeoLongitude lonx(lat2); // must notwork + + CCoordinateGeodetic gc(10.0, 20.0, 1000); + qDebug() << gc; + + // bye + qDebug() << "-----------------------------------------------"; + return 0; + +} + +} // namespace diff --git a/samples/blackmiscvectorgeo/samplesgeo.h b/samples/blackmiscvectorgeo/samplesgeo.h new file mode 100644 index 000000000..2f1671d12 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesgeo.h @@ -0,0 +1,33 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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_SAMPLESGEO_H +#define BLACKMISCTEST_SAMPLESGEO_H +#include "blackmisc/coordinategeodetic.h" + +namespace BlackMiscTest +{ + +/*! + * \brief Samples for vector / matrix + */ +class CSamplesGeo +{ +public: + /*! + * \brief Run the samples + */ + static int samples(); + +private: + /*! + * \brief Avoid init + */ + CSamplesGeo(); +}; +} // namespace + + +#endif // guard diff --git a/samples/blackmiscvectorgeo/samplesvectormatrix.cpp b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp new file mode 100644 index 000000000..cee116495 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp @@ -0,0 +1,29 @@ +#include "samplesvectormatrix.h" + +using namespace BlackMisc::Math; + +/*! + * Run the samples + */ +int BlackMiscTest::CSamplesVectorMatrix::samples() +{ + CVector3D v1; + CVector3D v2(1, 2, 3); + qDebug() << v1 << "value:" << v2[2] << v2.magnitude(); + v2 *= v2; // v2 * v2 + qDebug() << v2; + // v2 = v1 * v1; + + CMatrix3x3 m; + CMatrix3x3 mr = m; + qDebug() << m << mr; + mr.setRandom(); + CMatrix3x3 mi = mr.inverse(); + CMatrix3x3 mid = mr * mi; + qDebug() << mr << mi << mid; + + // bye + qDebug() << "-----------------------------------------------"; + return 0; + +} diff --git a/samples/blackmiscvectorgeo/samplesvectormatrix.h b/samples/blackmiscvectorgeo/samplesvectormatrix.h new file mode 100644 index 000000000..5e2495375 --- /dev/null +++ b/samples/blackmiscvectorgeo/samplesvectormatrix.h @@ -0,0 +1,29 @@ +#ifndef BLACKMISCTEST_SAMPLESVECTORMATRIX_H +#define BLACKMISCTEST_SAMPLESVECTORMATRIX_H + +#include "blackmisc/mathvector3d.h" +#include "blackmisc/mathmatrix3x3.h" + +namespace BlackMiscTest +{ + +/*! + * \brief Samples for vector / matrix + */ +class CSamplesVectorMatrix +{ +public: + /*! + * \brief Run the samples + */ + static int samples(); + +private: + /*! + * \brief Avoid init + */ + CSamplesVectorMatrix(); +}; +} // namespace + +#endif diff --git a/src/blackmisc/basestreamstringifier.h b/src/blackmisc/basestreamstringifier.h index c85374a13..45f5d8afc 100644 --- a/src/blackmisc/basestreamstringifier.h +++ b/src/blackmisc/basestreamstringifier.h @@ -27,6 +27,19 @@ template class CBaseStreamStringifier return debug; } + /*! + * \brief Stream operator << for QDataStream + * \param stream + * \param uc + * \return + */ + friend QDataStream operator<<(QDataStream stream, const UsingClass &uc) + { + const CBaseStreamStringifier &s = uc; + stream << s.stringForStreaming(); + return stream; + } + /*! * \brief Stream operator << for log messages * \param log diff --git a/src/blackmisc/blackmisc.h b/src/blackmisc/blackmisc.h index ba9974150..823a75453 100644 --- a/src/blackmisc/blackmisc.h +++ b/src/blackmisc/blackmisc.h @@ -9,11 +9,26 @@ * \brief Base and utility classes available in all other projects. */ +/*! + * \namespace BlackMisctest + * \brief Tests and samples for namespace BlackMisc. + */ + /*! * \namespace BlackMisc::Aviation * \brief Aviation and Avionics classes such as CHeading or CTransponder . */ +/*! + * \namespace BlackMisc::Math + * \brief Math classes such as vectors, matrices, and utility methods. + */ + +/*! + * \namespace BlackMisc::GeoPosition + * \brief Position handling as geo locations and coordinates systems. + */ + /*! * \namespace BlackMisc::PhysicalQuantities * \brief Classes for physical quantities and units such as length, mass, speed. diff --git a/src/blackmisc/blackmisc.pro b/src/blackmisc/blackmisc.pro index d02e53eb4..33707c2a3 100644 --- a/src/blackmisc/blackmisc.pro +++ b/src/blackmisc/blackmisc.pro @@ -10,8 +10,8 @@ DEPENDPATH += . .. #PRECOMPILED_HEADER = stdpch.h precompile_header:!isEmpty(PRECOMPILED_HEADER) { - DEFINES += USING_PCH - } + DEFINES += USING_PCH +} DEFINES += LOG_IN_FILE @@ -60,7 +60,19 @@ HEADERS += \ aviotransponder.h \ avioadfsystem.h \ aviation.h \ - basestreamstringifier.h + basestreamstringifier.h \ + mathvector3dbase.h \ + mathvector3d.h \ + mathmatrixbase.h \ + mathmatrix3x3.h \ + mathmatrix3x1.h \ + mathematics.h \ + geolatitude.h \ + geolongitude.h \ + coordinategeodetic.h \ + coordinateecef.h \ + coordinatened.h \ + geoearthangle.h SOURCES += \ logmessage.cpp \ @@ -88,6 +100,10 @@ SOURCES += \ avaltitude.cpp \ avverticalpositions.cpp \ aviomodulator.cpp \ - aviotransponder.cpp + aviotransponder.cpp \ + mathvector3dbase.cpp \ + mathmatrixbase.cpp \ + mathmatrix3x3.cpp \ + mathematics.cpp DESTDIR = ../../lib diff --git a/src/blackmisc/coordinateecef.h b/src/blackmisc/coordinateecef.h new file mode 100644 index 000000000..ce0f6e342 --- /dev/null +++ b/src/blackmisc/coordinateecef.h @@ -0,0 +1,92 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_COORDINATEECEF_H +#define BLACKMISC_COORDINATEECEF_H +#include "blackmisc/mathvector3dbase.h" + +namespace BlackMisc +{ +namespace Geo +{ +/*! + * \brief Earth centered, earth fixed position + */ +class CCoordinateEcef : public BlackMisc::Math::CVector3DBase +{ +public: + /*! + * \brief Default constructor + */ + CCoordinateEcef() : CVector3DBase() {} + + /*! + * \brief Constructor by values + * \param x + * \param y + * \param z + */ + CCoordinateEcef(qreal x, qreal y, qreal z) : CVector3DBase(x, y, z) {} + + /*! + * \brief x + * \return + */ + qreal x() const + { + return this->m_vector.x(); + } + + /*! + * \brief y + * \return + */ + qreal y() const + { + return this->m_vector.y(); + } + + /*! + * \brief z + * \return + */ + qreal z() const + { + return this->m_vector.z(); + } + + /*! + * \brief Set x + * \param x + */ + void setX(qreal x) + { + this->m_vector.setX(x); + } + + /*! + * \brief Set y + * \param y + */ + void setY(qreal y) + { + this->m_vector.setY(y); + } + + /*! + * \brief Set z + * \param z + */ + void setZ(qreal z) + { + this->m_vector.setZ(z); + } +}; + +} // namespace +} // namespace + + +#endif // guard diff --git a/src/blackmisc/coordinategeodetic.h b/src/blackmisc/coordinategeodetic.h new file mode 100644 index 000000000..698bf2989 --- /dev/null +++ b/src/blackmisc/coordinategeodetic.h @@ -0,0 +1,168 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_COORDINATEGEODETIC_H +#define BLACKMISC_COORDINATEGEODETIC_H +#include "blackmisc/mathvector3dbase.h" +#include "blackmisc/geolatitude.h" +#include "blackmisc/geolongitude.h" +#include "blackmisc/pqlength.h" + +namespace BlackMisc +{ +namespace Geo +{ +/*! + * \brief Geodetic coordinate + */ +class CCoordinateGeodetic : public CBaseStreamStringifier +{ + +private: + CGeoLatitude m_latitude; //!< Latitude + CGeoLongitude m_longitude; //!< Longitude + BlackMisc::PhysicalQuantities::CLength m_height; //!< height + +protected: + + /*! + * \brief String for converter + * \return + */ + virtual QString stringForConverter() const + { + QString s = "{%1, %2, %3}"; + return s.arg(this->m_latitude).arg(this->m_longitude).arg(this->height()); + } + +public: + /*! + * \brief Default constructor + */ + CCoordinateGeodetic() : m_latitude(), m_longitude(), m_height() {} + + /*! + * \brief Copy constructor + */ + CCoordinateGeodetic(const CCoordinateGeodetic &geoCoordinate) : + m_latitude(geoCoordinate.m_latitude), m_longitude(geoCoordinate.m_longitude), m_height(geoCoordinate.m_height) {} + + /*! + * \brief Constructor by values + * \param latitude + * \param longitude + * \param height + */ + CCoordinateGeodetic(CGeoLatitude latitude, CGeoLongitude longitude, BlackMisc::PhysicalQuantities::CLength height) : + m_latitude(latitude), m_longitude(longitude), m_height(height) {} + + /*! + * \brief Constructor by values + * \param latitudeDegrees + * \param longitudeDegrees + * \param heightMeters + */ + CCoordinateGeodetic(qreal latitudeDegrees, qreal longitudeDegrees, qreal heightMeters) : + m_latitude(latitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_longitude(longitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_height(heightMeters, BlackMisc::PhysicalQuantities::CLengthUnit::m()) {} + + + /*! + * \brief Latitude + * \return + */ + CGeoLatitude latitude() const + { + return this->m_latitude; + } + + /*! + * \brief Longitude + * \return + */ + CGeoLongitude longitude() const + { + return this->m_longitude; + } + + /*! + * \brief Height + * \return + */ + BlackMisc::PhysicalQuantities::CLength height() const + { + return this->m_height; + } + + /*! + * \brief Set latitude + * \param latitude + */ + void setLatitude(CGeoLatitude latitude) + { + this->m_latitude = latitude; + } + + /*! + * \brief Set longitude + * \param latitude + */ + void setLongitude(CGeoLongitude longitude) + { + this->m_longitude = longitude; + } + + /*! + * \brief Set height + * \param height + */ + void setHeight(BlackMisc::PhysicalQuantities::CLength height) + { + this->m_height = height; + } + + /*! + * \brief Equal operator == + * \param otherGeodetic + * \return + */ + bool operator ==(const CCoordinateGeodetic &otherGeodetic) const + { + if (this == &otherGeodetic) return true; + return this->m_height == otherGeodetic.m_height && + this->m_latitude == otherGeodetic.m_latitude && + this->m_longitude == otherGeodetic.m_longitude; + } + + /*! + * \brief Unequal operator != + * \param otherGeodetic + * \return + */ + bool operator !=(const CCoordinateGeodetic &otherGeodetic) const + { + if (this == &otherGeodetic) return false; + return !((*this) == otherGeodetic); + } + + /*! + * \brief Assigment operator = + * \param otherGeodetic + * \return + */ + CCoordinateGeodetic &operator =(const CCoordinateGeodetic &otherGeodetic) + { + if (this == &otherGeodetic) return *this; // Same object? + this->m_height = otherGeodetic.m_height; + this->m_latitude = otherGeodetic.m_latitude; + this->m_longitude = otherGeodetic.m_longitude; + return (*this); + } +}; + +} // namespace +} // namespace + + +#endif // guard diff --git a/src/blackmisc/coordinatened.h b/src/blackmisc/coordinatened.h new file mode 100644 index 000000000..40baa95d6 --- /dev/null +++ b/src/blackmisc/coordinatened.h @@ -0,0 +1,92 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_COORDINATENED_H +#define BLACKMISC_COORDINATENED_H +#include "blackmisc/mathvector3dbase.h" + +namespace BlackMisc +{ +namespace Geo +{ +/*! + * \brief North, East, Down + */ +class CCoordinateNed : public BlackMisc::Math::CVector3DBase +{ +public: + /*! + * \brief Default constructor + */ + CCoordinateNed() : CVector3DBase() {} + + /*! + * \brief Constructor by values + * \param north + * \param east + * \param down + */ + CCoordinateNed(qreal north, qreal east, qreal down) : CVector3DBase(north, east, down) {} + + /*! + * \brief North + * \return + */ + qreal north() const + { + return this->m_vector.x(); + } + + /*! + * \brief East + * \return + */ + qreal east() const + { + return this->m_vector.y(); + } + + /*! + * \brief Down + * \return + */ + qreal down() const + { + return this->m_vector.z(); + } + + /*! + * \brief Set north + * \param north + */ + void setNorth(qreal north) + { + this->m_vector.setX(north); + } + + /*! + * \brief Set east + * \param east + */ + void setEast(qreal east) + { + this->m_vector.setY(east); + } + + /*! + * \brief Set down + * \param down + */ + void setDown(qreal down) + { + this->m_vector.setZ(down); + } +}; + +} // namespace +} // namespace + + +#endif // guard diff --git a/src/blackmisc/geoearthangle.h b/src/blackmisc/geoearthangle.h new file mode 100644 index 000000000..ff9a2b4bf --- /dev/null +++ b/src/blackmisc/geoearthangle.h @@ -0,0 +1,168 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_GEOLATLONBASE_H +#define BLACKMISC_GEOLATLONBASE_H +#include "blackmisc/pqangle.h" + +namespace BlackMisc +{ +namespace Geo +{ +/*! + * \brief Base class for latitude / longitude + */ +template class CGeoEarthAngle : public BlackMisc::PhysicalQuantities::CAngle +{ +protected: + /*! + * \brief Default constructor + */ + CGeoEarthAngle() : CAngle() {} + + /*! + * \brief Copy constructor + * \param latOrLon + */ + CGeoEarthAngle(const LATorLON &latOrLon) : CAngle(latOrLon) { } + + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CGeoEarthAngle(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit): CAngle(value, unit) {} + +public: + + /*! + * \brief Virtual destructor + */ + virtual ~CGeoEarthAngle() {} + + /*! + * \brief Equal operator == + * \param latOrLon + * \return + */ + bool operator==(const LATorLON &latOrLon) const + { + return CAngle::operator ==(latOrLon); + } + + /*! + * \brief Not equal operator != + * \param latOrLon + * \return + */ + bool operator!=(const LATorLON &latOrLon) const + { + return CAngle::operator !=(latOrLon); + } + + /*! + * \brief Plus operator += + * \param latOrLon + * \return + */ + CGeoEarthAngle &operator +=(const CGeoEarthAngle &latOrLon) + { + CAngle::operator +=(latOrLon); + return (*this); + } + + /*! + * \brief Minus operator-= + * \param latOrLon + * \return + */ + CGeoEarthAngle &operator -=(const CGeoEarthAngle &latOrLon) + { + CAngle::operator -=(latOrLon); + return (*this); + } + + /*! + * \brief Greater operator > + * \param latOrLon + * \return + */ + bool operator >(const LATorLON &latOrLon) const + { + return CAngle::operator >(latOrLon); + } + + /*! + * \brief Less operator < + * \param latOrLon + * \return + */ + bool operator <(const LATorLON &latOrLon) const + { + return CAngle::operator >(latOrLon); + } + + /*! + * \brief Less equal operator <= + * \param latOrLon + * \return + */ + bool operator <=(const LATorLON &latOrLon) const + { + return CAngle::operator <=(latOrLon); + } + + /*! + * \brief Greater equal operator >= + * \param latOrLon + * \return + */ + bool operator >=(const LATorLON &latOrLon) const + { + return CAngle::operator >=(latOrLon); + } + + /*! + * \brief Assignment operator = + * \param latOrLon + * \return + */ + CGeoEarthAngle &operator =(const LATorLON &latOrLon) + { + CAngle::operator =(latOrLon); + return (*this); + } + + /*! + * \brief Plus operator + + * \param latOrLon + * \return + */ + LATorLON operator +(const LATorLON &latOrLon) const + { + LATorLON l(0.0, this->getUnit()); + l += (*this); + l += latOrLon; + return l; + } + + /*! + * \brief Minus operator - + * \param latOrLon + * \return + */ + LATorLON operator -(const LATorLON &latOrLon) const + { + LATorLON l(0.0, this->getUnit()); + l += (*this); + l -= latOrLon; + return l; + } +}; + +} // namespace +} // namespace + +#endif // guard diff --git a/src/blackmisc/geolatitude.h b/src/blackmisc/geolatitude.h new file mode 100644 index 000000000..478b2f41e --- /dev/null +++ b/src/blackmisc/geolatitude.h @@ -0,0 +1,54 @@ +#ifndef BLACKMISC_GEOLATITUDE_H +#define BLACKMISC_GEOLATITUDE_H +#include "blackmisc/geoearthangle.h" + +namespace BlackMisc +{ +namespace Geo +{ + +/*! + * \brief Latitude + */ +class CGeoLatitude : public CGeoEarthAngle +{ +protected: + /*! + * \brief Specific string representation + */ + virtual QString stringForConverter() const + { + QString s = "latitude "; + return s.append(CGeoEarthAngle::stringForConverter()); + } + +public: + /*! + * \brief Default constructor + */ + CGeoLatitude() : CGeoEarthAngle() {} + + /*! + * \brief Copy constructor + * \param latitude + */ + CGeoLatitude(const CGeoLatitude &latitude) : CGeoEarthAngle(latitude) {} + + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CGeoLatitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit): CGeoEarthAngle(value, unit) {} + + /*! + * \brief Virtual destructor + */ + virtual ~CGeoLatitude() {} +}; + +} // namespace +} // namespace + + +#endif // guard diff --git a/src/blackmisc/geolatlonbase.cpp b/src/blackmisc/geolatlonbase.cpp new file mode 100644 index 000000000..74d646c7d --- /dev/null +++ b/src/blackmisc/geolatlonbase.cpp @@ -0,0 +1,15 @@ +#include "geolatitude.h" + +namespace BlackMisc +{ +namespace Geo +{ + + +// see here for the reason of thess forward instantiations +// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html +// template class CGeoLatLonBase; + +} + +} diff --git a/src/blackmisc/geolongitude.h b/src/blackmisc/geolongitude.h new file mode 100644 index 000000000..3d72204dc --- /dev/null +++ b/src/blackmisc/geolongitude.h @@ -0,0 +1,53 @@ +#ifndef BLACKMISC_GEOLONGITUDE_H +#define BLACKMISC_GEOLONGITUDE_H +#include "blackmisc/geoearthangle.h" + +namespace BlackMisc +{ +namespace Geo +{ + +/*! + * \brief Longitude + */ +class CGeoLongitude : public CGeoEarthAngle +{ +protected: + /*! + * \brief Specific string representation + */ + virtual QString stringForConverter() const + { + QString s = "longitude "; + return s.append(CGeoEarthAngle::stringForConverter()); + } + +public: + /*! + * \brief Default constructor + */ + CGeoLongitude() : CGeoEarthAngle() {} + + /*! + * \brief Copy constructor + * \param Longitude + */ + CGeoLongitude(const CGeoLongitude &Longitude) : CGeoEarthAngle(Longitude) {} + + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CGeoLongitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit): CGeoEarthAngle(value, unit) {} + + /*! + * \brief Virtual destructor + */ + virtual ~CGeoLongitude() {} +}; + +} // namespace +} // namespace + +#endif // guard diff --git a/src/blackmisc/mathematics.cpp b/src/blackmisc/mathematics.cpp new file mode 100644 index 000000000..282d47ce5 --- /dev/null +++ b/src/blackmisc/mathematics.cpp @@ -0,0 +1,38 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "blackmisc/mathematics.h" +#include // std::max + +namespace BlackMisc +{ +namespace Math +{ + +/* + * Hypotenuse + */ +double CMath::hypot(double x, double y) +{ + x = abs(x); + y = abs(y); + double max = std::max(x, y); + double min = std::min(x, y); + double r = min / max; + return max * sqrt(1 + r * r); +} + +/* + * Real part of cubic root + */ +double CMath::cubicRootReal(const double x) +{ + double result; + result = std::pow(std::abs(x), (double)1 / 3); + return x < 0 ? -result : result; +} + +} // namespace +} // namespace diff --git a/src/blackmisc/mathematics.h b/src/blackmisc/mathematics.h new file mode 100644 index 000000000..b9d1dbeaa --- /dev/null +++ b/src/blackmisc/mathematics.h @@ -0,0 +1,64 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_MATHEMATICS_H +#define BLACKMISC_MATHEMATICS_H + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief Math utils + */ +class CMath +{ +public: + /*! + * \brief Calculates the hypotenuse of x and y without overflow + * \param x + * \param y + * \return + */ + static double hypot(double x, double y); + + /*! + * \brief Calculates the square of x + * \param x + * \return + */ + static inline double square(const double x) + { + return x * x; + } + + /*! + * \brief Calculates x to the power of three + * \param x + * \return + */ + static inline double cubic(const double x) + { + return x * x * x; + } + + /*! + * \brief Calculates the real cubic root + * \param x + * \return + */ + static double cubicRootReal(const double x); + +private: + /*! + * \brief Avoid object init + */ + CMath() {} +}; + +} // namespace +} // namespace +#endif // guard diff --git a/src/blackmisc/mathmatrix3x1.h b/src/blackmisc/mathmatrix3x1.h new file mode 100644 index 000000000..2c5200add --- /dev/null +++ b/src/blackmisc/mathmatrix3x1.h @@ -0,0 +1,65 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_POSMATRIX3X1_H +#define BLACKMISC_POSMATRIX3X1_H + +#include "blackmisc/mathmatrixbase.h" +#include "blackmisc/mathvector3d.h" + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief 3D matrix + */ +class CMatrix3x1 : public CMatrixBase +{ +public: + /*! + * \brief CMatrix3D + */ + CMatrix3x1() : CMatrixBase() {} + + /*! + * \brief init with value + * \param fillValue + */ + CMatrix3x1(qreal fillValue) : CMatrixBase(fillValue) {} + + /*! + * \brief Copy constructor + * \param other + */ + CMatrix3x1(const CMatrix3x1 &otherMatrix) : CMatrixBase(otherMatrix) {} + + /*! + * \brief Convert to vector + * \return + */ + CVector3D toVector3D() const + { + return CVector3D(this->getElement(0, 0), this->getElement(1, 0), this->getElement(2, 0)); + } + + /*! + * \brief Convert from vector + * \return + */ + void fromVector3D(const CVector3D &vector) + { + this->m_matrix[0][0] = vector.x(); + this->m_matrix[1][0] = vector.y(); + this->m_matrix[2][0] = vector.z(); + } +}; + +} // namespace + +} // namespace + +#endif // guard diff --git a/src/blackmisc/mathmatrix3x3.cpp b/src/blackmisc/mathmatrix3x3.cpp new file mode 100644 index 000000000..2f7d8d81a --- /dev/null +++ b/src/blackmisc/mathmatrix3x3.cpp @@ -0,0 +1,57 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "blackmisc/mathmatrix3x3.h" + +namespace BlackMisc +{ +namespace Math +{ + +/* + * Determinant + */ +qreal CMatrix3x3::determinant() const +{ + qreal determinant = + this->m_matrix(0, 0) * this->m_matrix(1, 1) * this->m_matrix(2, 2) + + this->m_matrix(0, 1) * this->m_matrix(1, 2) * this->m_matrix(2, 0) + + this->m_matrix(0, 2) * this->m_matrix(1, 0) * this->m_matrix(2, 1) - + this->m_matrix(0, 1) * this->m_matrix(1, 0) * this->m_matrix(2, 2) - + this->m_matrix(0, 2) * this->m_matrix(1, 1) * this->m_matrix(2, 0) - + this->m_matrix(0, 0) * this->m_matrix(1, 2) * this->m_matrix(2, 1); + + return determinant; +} + +/* + * Determinant + */ +CMatrix3x3 CMatrix3x3::inverse() const +{ + CMatrix3x3 inverse; + qreal det = determinant(); + + // should we throw an assert / error here? + if (det == 0) return inverse; + + qreal invdet = 1.0 / det; + + inverse.m_matrix(0, 0) = (this->m_matrix(1, 1) * this->m_matrix(2, 2) - this->m_matrix(1, 2) * this->m_matrix(2, 1)) * invdet; + inverse.m_matrix(0, 1) = (- this->m_matrix(0, 1) * this->m_matrix(2, 2) + this->m_matrix(0, 2) * this->m_matrix(2, 1)) * invdet; + inverse.m_matrix(0, 2) = (this->m_matrix(0, 1) * this->m_matrix(1, 2) - this->m_matrix(0, 2) * this->m_matrix(1, 1)) * invdet; + inverse.m_matrix(1, 0) = (- this->m_matrix(1, 0) * this->m_matrix(2, 2) + this->m_matrix(1, 2) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(1, 1) = (this->m_matrix(0, 0) * this->m_matrix(2, 2) - this->m_matrix(0, 2) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(1, 2) = (- this->m_matrix(0, 0) * this->m_matrix(1, 2) + this->m_matrix(0, 2) * this->m_matrix(1, 0)) * invdet; + inverse.m_matrix(2, 0) = (this->m_matrix(1, 0) * this->m_matrix(2, 1) - this->m_matrix(1, 1) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(2, 1) = (- this->m_matrix(0, 0) * this->m_matrix(2, 1) + this->m_matrix(0, 1) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(2, 2) = (this->m_matrix(0, 0) * this->m_matrix(1, 1) - this->m_matrix(0, 1) * this->m_matrix(1, 0)) * invdet; + + return inverse; +} + +} // namespace + +} // namespace diff --git a/src/blackmisc/mathmatrix3x3.h b/src/blackmisc/mathmatrix3x3.h new file mode 100644 index 000000000..86a28acc5 --- /dev/null +++ b/src/blackmisc/mathmatrix3x3.h @@ -0,0 +1,57 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_POSMATRIX3X3_H +#define BLACKMISC_POSMATRIX3X3_H + +#include "blackmisc/mathmatrixbase.h" + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief 3x1 matrix + */ +class CMatrix3x3 : public CMatrixBase +{ +public: + /*! + * \brief CMatrix3D + */ + CMatrix3x3() : CMatrixBase() {} + + /*! + * \brief init with value + * \param fillValue + */ + CMatrix3x3(qreal fillValue) : CMatrixBase(fillValue) {} + + /*! + * \brief Copy constructor + * \param other + */ + CMatrix3x3(const CMatrix3x3 &otherMatrix) : CMatrixBase(otherMatrix) {} + + /*! + * \brief Calculates the determinant of the matrix + * \return + */ + qreal determinant() const; + + /*! + * \brief Calculate the inverse + * \return + */ + CMatrix3x3 inverse() const; + +}; + +} // namespace + +} // namespace + +#endif // BLACKMISC_POSMATRIX3X3_H diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp new file mode 100644 index 000000000..d4b7ccf77 --- /dev/null +++ b/src/blackmisc/mathmatrixbase.cpp @@ -0,0 +1,71 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "blackmisc/mathmatrix3x3.h" +#include "blackmisc/mathmatrix3x1.h" + +namespace BlackMisc +{ +namespace Math +{ + +/* + * Get element by column / row + */ +template double CMatrixBase::getElement(size_t row, size_t column) const +{ + bool valid = (row >= 0 && column >= 0 && row < Rows && column < Columns); + Q_ASSERT_X(valid, "getElement()", "Row or column invalid"); + std::range_error("Row or column invalid"); + return this->m_matrix(row, column); +} + +/* + * All values to random value + */ +template void CMatrixBase::setRandom() +{ + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + this->m_matrix(r, c) = (qrand() % 101); // 0...100 + } + } +} + +/* + * Convert to string + */ +template QString CMatrixBase::stringForConverter() const +{ + QString s = "{"; + for (int r = 0; r < Rows; r++) + { + s = s.append("{"); + for (int c = 0; c < Columns; c++) + { + QString n = QString::number(this->m_matrix(r, c), 'f', 2); + if (c > 0) s = s.append(","); + s = s.append(n); + } + s = s.append("}"); + } + s = s.append("}"); + return s; +} + + +// see here for the reason of thess forward instantiations +// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html +template class CMatrixBase; +template class CMatrixBase; + +} // namespace + +} // namespace + +#include "mathmatrixbase.h" + diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h new file mode 100644 index 000000000..9d753808e --- /dev/null +++ b/src/blackmisc/mathmatrixbase.h @@ -0,0 +1,267 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_MATHMATRIXBASE_H +#define BLACKMISC_MATHMATRIXBASE_H + +#include "blackmisc/basestreamstringifier.h" +#include + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief Base functionality of a matrix + */ +template class CMatrixBase : + public BlackMisc::CBaseStreamStringifier +{ +protected: + QGenericMatrix m_matrix; //!< backing data + + /*! + * \brief Conversion to string + * \return + */ + QString stringForConverter() const; + +public: + /*! + * \brief Default constructor + */ + CMatrixBase() : m_matrix() {} + + /*! + * \brief Constructor with init value + * \param fillValue + */ + CMatrixBase(qreal fillValue) : m_matrix() { this->m_matrix.fill(fillValue);} + + /*! + * \brief Copy constructor + * \param other + */ + CMatrixBase(const CMatrixBase &otherMatrix) : m_matrix(otherMatrix.m_matrix) {} + + /*! + * \brief Virtual destructor + */ + virtual ~CMatrixBase() {} + + /*! + * \brief Equal operator == + * \param otherMatrix + * \return + */ + bool operator ==(const ImplMatrix &otherMatrix) const + { + if (this == &otherMatrix) return true; + return this->m_matrix == otherMatrix.m_matrix; + } + + /*! + * \brief Unequal operator != + * \param otherMatrix + * \return + */ + bool operator !=(const ImplMatrix &otherMatrix) const + { + if (this == &otherMatrix) return false; + return !((*this) == otherMatrix); + + } + + /*! + * \brief Assigment operator = + * \param multiply + * \return + */ + CMatrixBase &operator =(const CMatrixBase &otherMatrix) + { + if (this == &otherMatrix) return *this; // Same object? + this->m_matrix = otherMatrix.m_matrix; + return (*this); + } + + /*! + * \brief Operator *= + * \param multiply + * \return + */ + CMatrixBase &operator *=(const CMatrixBase &otherMatrix) + { + this->m_matrix = this->m_matrix * otherMatrix.m_matrix; + return (*this); + } + + /*! + * \brief Operator * + * \param multiply + * \return + */ + ImplMatrix operator *(const ImplMatrix &otherMatrix) const + { + ImplMatrix m(0.0); + m += (*this); + m *= otherMatrix; + return m; + } + + /*! + * \brief Operator *= + * \param factor + * \return + */ + CMatrixBase &operator *=(qreal factor) + { + this->m_matrix *= factor; + return (*this); + } + + /*! + * \brief Operator * + * \param factor + * \return + */ + CMatrixBase &operator *(qreal factor) + { + ImplMatrix m(0.0); + m += (*this); + m *= factor; + return m; + } + + /*! + * \brief Operator /= + * \param factor + * \return + */ + CMatrixBase &operator /=(qreal factor) + { + this->m_matrix /= factor; + return (*this); + } + + /*! + * \brief Operator / + * \param factor + * \return + */ + CMatrixBase &operator /(qreal factor) + { + ImplMatrix m(0.0); + m += (*this); + m /= factor; + return m; + } + + /*! + * \brief Operator += + * \param otherMatrix + * \return + */ + CMatrixBase &operator +=(const CMatrixBase &otherMatrix) + { + this->m_matrix += otherMatrix.m_matrix; + return (*this); + } + + /*! + * \brief Operator + + * \param otherMatrix + * \return + */ + ImplMatrix operator +(const ImplMatrix &otherMatrix) const + { + ImplMatrix m(0.0); + m += (*this); + m += otherMatrix; + return m; + } + + /*! + * \brief Operator -= + * \param otherMatrix + * \return + */ + CMatrixBase &operator -=(const CMatrixBase &otherMatrix) + { + this->m_matrix -= otherMatrix.m_matrix; + return (*this); + } + + /*! + * \brief Operator - + * \param otherMatrix + * \return + */ + ImplMatrix operator -(const ImplMatrix &otherMatrix) const + { + ImplMatrix m(0.0); + m += (*this); + m -= otherMatrix; + return m; + } + + /*! + * \brief Transposed matrix + * \return + */ + ImplMatrix transposed() const + { + ImplMatrix m(0.0); + m.m_matrix = this->m_matrix.transposed(); + return m; + } + + /*! + * \brief Is identity matrix? + * \return + */ + bool isIdentity() const + { + return this->m_matrix.isIdentity(); + } + + /*! + * \brief Set as identity matrix + * \return + */ + void setToIdentity() + { + this->m_matrix.setToIdentity(); + } + + /*! + * \brief Fills the matrix with random elements + */ + void setRandom(); + + /*! + * \brief All values to zero + */ + void setZero() { this->m_matrix.fill(0.0); } + + /*! + * \brief Set a dedicated value + * \param value + */ + void fill(qreal value) { this->m_matrix.fill(value); } + + /*! + * \brief Get element + * \param row + * \return + */ + double getElement(size_t row, size_t column) const; +}; + +} // namespace + +} // namespace + +#endif // guard diff --git a/src/blackmisc/mathvector3d.h b/src/blackmisc/mathvector3d.h new file mode 100644 index 000000000..ab5f5ab21 --- /dev/null +++ b/src/blackmisc/mathvector3d.h @@ -0,0 +1,94 @@ +#ifndef BLACKMISC_MATHVECTOR3D_H +#define BLACKMISC_MATHVECTOR3D_H + +#include "blackmisc/mathvector3dbase.h" + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief Concrete vector implementation + */ +class CVector3D : public CVector3DBase +{ +public: + /*! + * \brief Default constructor + */ + CVector3D() : CVector3DBase() {} + + /*! + * \brief Constructor by value + * \param i + * \param j + * \param k + */ + CVector3D(qreal i, qreal j, qreal k) : CVector3DBase(i, j, k) {} + + /*! + * \brief Constructor by value + * \param value + */ + CVector3D(qreal value) : CVector3DBase(value) {} + + /*! + * \brief i + * \return + */ + qreal i() const + { + return this->m_vector.x(); + } + + /*! + * \brief j + * \return + */ + qreal j() const + { + return this->m_vector.y(); + } + + /*! + * \brief k + * \return + */ + qreal k() const + { + return this->m_vector.z(); + } + + /*! + * \brief Set i + * \param i + */ + void setI(qreal i) + { + this->m_vector.setX(i); + } + + /*! + * \brief Set j + * \param j + */ + void setJ(qreal j) + { + this->m_vector.setY(j); + } + + /*! + * \brief Set k + * \param k + */ + void setK(qreal k) + { + this->m_vector.setZ(k); + } + +}; + +} // namespace +} // namespace +#endif // guard diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp new file mode 100644 index 000000000..447f283b0 --- /dev/null +++ b/src/blackmisc/mathvector3dbase.cpp @@ -0,0 +1,80 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 "blackmisc/mathvector3dbase.h" +#include "blackmisc/mathvector3d.h" +#include "blackmisc/coordinateecef.h" +#include "blackmisc/coordinatened.h" + +namespace BlackMisc +{ +namespace Math +{ + +/* + * Convert to string + */ +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()); + return s; +} + +/* + * Vector to zero + */ +template void CVector3DBase::setZero() +{ + this->fill(0.0); +} + +/* + * Vector to zero + */ +template void CVector3DBase::fill(qreal value) +{ + this->m_vector.setX(value); + this->m_vector.setY(value); + this->m_vector.setZ(value); +} + +/* + * Element + */ +template qreal CVector3DBase::getElement(size_t row) const +{ + bool validIndex = (row < 3 && row >= 0); + Q_ASSERT_X(validIndex, "getElement", "Wrong index"); + if (!validIndex) throw std::range_error("Invalid index vor 3D vector"); + double d; + switch (row) + { + case 0: + d = this->m_vector.x(); + break; + case 1: + d = this->m_vector.y(); + break; + case 2: + d = this->m_vector.z(); + break; + default: + Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); + throw std::range_error("Detected invalid index in 3D vector"); + break; + } + return d; +} + +// see here for the reason of thess forward instantiations +// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html +template class CVector3DBase; +template class CVector3DBase; +template class CVector3DBase; + +} // namespace + +} // namespace diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h new file mode 100644 index 000000000..a43af607a --- /dev/null +++ b/src/blackmisc/mathvector3dbase.h @@ -0,0 +1,283 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * 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 BLACKMISC_MATHVECTOR3DBASE_H +#define BLACKMISC_MATHVECTOR3DBASE_H + +#include "blackmisc/basestreamstringifier.h" +#include + +namespace BlackMisc +{ +namespace Math +{ + +/*! + * \brief 3D vector base (x, y, z) + */ +template class CVector3DBase : public CBaseStreamStringifier +{ +protected: + + QVector3D m_vector; //!< Vector data + + /*! + * \brief Default constructor + */ + CVector3DBase() : m_vector() {} + + /*! + * \brief Constructor by values + * \param i + * \param j + * \param k + */ + CVector3DBase(qreal i, qreal j, qreal k) : m_vector(i, j, k) {} + + /*! + * \brief Constructor by value + * \param value + */ + CVector3DBase(qreal value) : m_vector(value, value, value) {} + + /*! + * \brief String for converter + * \return + */ + virtual QString stringForConverter() const; + +public: + + // getter and setters are implemented in the derived classes + // as they have different names (x, i, north) + + /*! + * \brief Virtual destructor + */ + virtual ~CVector3DBase() {} + + /*! + * \brief Set zeros + */ + void setZero(); + + /*! + * \brief Set zeros + */ + void fill(qreal value); + + /*! + * \brief Get element + * \param row + * \return + */ + qreal getElement(size_t row) const; + + /*! + * \brief Operator [] + * \param row + * \return + */ + qreal operator[](size_t row) const { return this->getElement(row); } + + /*! + * \brief Equal operator == + * \param otherVector + * \return + */ + bool operator ==(const CVector3DBase &otherVector) const + { + if (this == &otherVector) return true; + return this->m_vector == otherVector.m_vector; + } + + /*! + * \brief Unequal operator != + * \param otherVector + * \return + */ + bool operator !=(const CVector3DBase &otherVector) const + { + if (this == &otherVector) return false; + return !((*this) == otherVector); + } + + /*! + * \brief Assigment operator = + * \param multiply + * \return + */ + CVector3DBase &operator =(const CVector3DBase &otherVector) + { + if (this == &otherVector) return *this; // Same object? + this->m_vector = otherVector.m_vector; + return (*this); + } + + /*! + * \brief Operator += + * \param otherVector + * \return + */ + CVector3DBase &operator +=(const CVector3DBase &otherVector) + { + this->m_vector += otherVector.m_vector; + return (*this); + } + + /*! + * \brief Operator + + * \param otherVector + * \return + */ + ImplClass operator +(const ImplClass &otherVector) const + { + ImplClass v; + v += (*this); + v += otherVector; + return v; + } + + /*! + * \brief Operator -= + * \param otherVector + * \return + */ + CVector3DBase &operator -=(const CVector3DBase &otherVector) + { + this->m_vector -= otherVector.m_vector; + return (*this); + } + + /*! + * \brief Operator + + * \param otherVector + * \return + */ + ImplClass operator -(const ImplClass &otherVector) const + { + ImplClass v; + v += (*this); + v -= otherVector; + return v; + } + + /*! + * \brief Operator *=, just x*x, y*y, z*z neither vector nor dot product + * \param otherVector + * \return + */ + CVector3DBase &operator *=(const CVector3DBase &otherVector) + { + this->m_vector *= otherVector.m_vector; + return (*this); + } + + /*! + * \brief Operator, just x*x, y*y, z*z neither vector nor dot product + * \param otherVector + * \return + */ + ImplClass operator *(const ImplClass &otherVector) const + { + ImplClass v; + v += (*this); + v *= otherVector; + return v; + } + + /*! + * \brief Operator /=, just x/x, y/y, z/z + * \param otherVector + * \return + */ + CVector3DBase &operator /=(const CVector3DBase &otherVector) + { + this->m_vector *= otherVector.reciprocalValues().m_vector; + return (*this); + } + + /*! + * \brief Operator, just x/x, y/y, z/z + * \param otherVector + * \return + */ + ImplClass operator /(const ImplClass &otherVector) const + { + ImplClass v; + v += (*this); + v /= otherVector; + return v; + } + + /*! + * \brief Dot product + * \param otherVector + * \return + */ + qreal dotProduct(const ImplClass &otherVector) const + { + return QVector3D::dotProduct(this->m_vector, otherVector.m_vector); + } + + /*! + * \brief Dot 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; + } + + /*! + * \brief Dot product + * \param otherVector + * \return + */ + 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()); + return v; + } + + /*! + * \brief Length + * \return + */ + qreal length()const + { + return this->m_vector.length(); + } + + /*! + * \brief Length squared + * \return + */ + qreal lengthSquared()const + { + return this->m_vector.lengthSquared(); + } + + /*! + * \brief Magnitude + * \return + */ + qreal magnitude() const + { + return sqrt(this->lengthSquared()); + } +}; + +} // namespace + +} // namespace + +#endif // guard diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index 2e11d08fa..72a224e98 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -79,7 +79,7 @@ template bool CPhysicalQuantity::operator ==(const template bool CPhysicalQuantity::operator !=(const CPhysicalQuantity &otherQuantity) const { if (this == &otherQuantity) return false; - return !(*this == otherQuantity); + return !((*this) == otherQuantity); } /* diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index 1c1dd3e62..59119ee38 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community +/* Copyright (C) 2013 VATSIM Community / authors * 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/. */ @@ -143,7 +143,7 @@ public: /*! * \brief Value in given unit * \param unit - * @return + * \return */ double value(const MU &unit) const; @@ -151,7 +151,7 @@ public: * \brief Rounded value in unit * \param unit * \param digits - * @return + * \return */ double valueRounded(const MU &unit, int digits = -1) const; @@ -159,13 +159,13 @@ public: * \brief Value to QString with unit, e.g. "5.00m" * \param unit * \param digits - * @return + * \return */ QString valueRoundedWithUnit(const MU &unit, int digits = -1) const; /*! * \brief Value a int - * @return + * \return */ qint32 unitValueToInteger() const { @@ -174,7 +174,7 @@ public: /*! * \brief Value a double - * @return + * \return */ double unitValueToDouble() const { @@ -184,13 +184,13 @@ public: /*! * \brief Value to QString with unit, e.g. "5.00m" * \param digits - * @return + * \return */ QString unitValueRoundedWithUnit(int digits = -1) const; /*! * \brief SI value to integer - * @return + * \return */ qint32 siBaseUnitValueToInteger() const { @@ -199,7 +199,7 @@ public: /*! * \brief SI value to double - * @return + * \return */ double siBaseUnitValueToDouble() const { @@ -209,14 +209,14 @@ public: /*! * \brief Rounded value by n digits * \param digits - * @return + * \return */ double unitValueToDoubleRounded(int digits = -1) const; /*! * \brief Rounded value by n digits * \param digits if no value is provided, unit rounding is taken - * @return + * \return */ QString unitValueToQStringRounded(int digits = -1) const; @@ -241,21 +241,21 @@ public: /*! * \brief Rounded SI value by n digits * \param digits - * @return + * \return */ double convertedSiValueToDoubleRounded(int digits = -1) const; /*! * \brief Rounded value by n digits * \param digits if no value is provided, unit rounding is taken - * @return + * \return */ QString convertedSiValueToQStringRounded(int digits = -1) const; /*! * \brief SI Base unit value rounded * \param digits - * @return + * \return */ QString convertedSiValueRoundedWithUnit(int digits = -1) const; @@ -291,98 +291,98 @@ public: /*! * \brief Divide operator /= * \param divide - * @return + * \return */ CPhysicalQuantity &operator /=(double divide); /*! * \brief Operator * * \param multiply - * @return + * \return */ PQ operator *(double multiply) const; /*! * \brief Operator / * \param divide - * @return + * \return */ PQ operator /(double divide) const; /*! * \brief Equal operator == * \param otherQuantity - * @return + * \return */ bool operator==(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Not equal operator != * \param otherQuantity - * @return + * \return */ bool operator!=(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Plus operator += * \param otherQuantity - * @return + * \return */ CPhysicalQuantity &operator +=(const CPhysicalQuantity &otherQuantity); /*! * \brief Minus operator-= * \param otherQuantity - * @return + * \return */ CPhysicalQuantity &operator -=(const CPhysicalQuantity &otherQuantity); /*! * \brief Greater operator > * \param otherQuantity - * @return + * \return */ bool operator >(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Less operator < * \param otherQuantity - * @return + * \return */ bool operator <(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Less equal operator <= * \param otherQuantity - * @return + * \return */ bool operator <=(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Greater equal operator >= * \param otherQuantity - * @return + * \return */ bool operator >=(const CPhysicalQuantity &otherQuantity) const; /*! * \brief Assignment operator = * \param otherQuantity - * @return + * \return */ CPhysicalQuantity &operator =(const CPhysicalQuantity &otherQuantity); /*! * \brief Plus operator + * \param otherQuantity - * @return + * \return */ PQ operator +(const PQ &otherQuantity) const; /*! * \brief Minus operator - * \param otherQuantity - * @return + * \return */ PQ operator -(const PQ &otherQuantity) const;