refs #396 remove obsolete Matrix and Vector classes

This commit is contained in:
Roland Winklmeier
2015-04-02 01:33:17 +02:00
parent 78d3350bf3
commit b3daaa7ae2
21 changed files with 0 additions and 1551 deletions

View File

@@ -34,10 +34,6 @@
*/
void BlackMisc::Math::registerMetadata()
{
CMatrix3x3::registerMetadata();
CMatrix3x1::registerMetadata();
CMatrix1x3::registerMetadata();
CVector3D::registerMetadata();
}
/*

View File

@@ -12,7 +12,6 @@
#ifndef BLACKMISC_COORDINATEGEODETIC_H
#define BLACKMISC_COORDINATEGEODETIC_H
#include "blackmisc/mathvector3dbase.h"
#include "blackmisc/geolatitude.h"
#include "blackmisc/geolongitude.h"
#include "blackmisc/pqlength.h"

View File

@@ -1,48 +0,0 @@
/* Copyright (C) 2013
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_POSMATRIX1x3_H
#define BLACKMISC_POSMATRIX1x3_H
#include "blackmisc/mathmatrixbase.h"
#include "blackmisc/mathvector3d.h"
namespace BlackMisc
{
namespace Math
{
//! Matrix 1x3
class CMatrix1x3 : public CValueObject<CMatrix1x3, CMatrixBase<CMatrix1x3, 1, 3>>
{
public:
//! Constructor
CMatrix1x3() = default;
//! Init by fill value
explicit CMatrix1x3(double fillValue) : CValueObject(fillValue) {}
//! Constructor
CMatrix1x3(double c1, double c2, double c3)
{
this->m_matrix(0, 0) = c1;
this->m_matrix(0, 1) = c2;
this->m_matrix(0, 2) = c3;
}
};
}
}
Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix1x3)
#endif // guard

View File

@@ -1,65 +0,0 @@
/* Copyright (C) 2013
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_POSMATRIX3X1_H
#define BLACKMISC_POSMATRIX3X1_H
#include "blackmisc/mathmatrixbase.h"
#include "blackmisc/mathvector3d.h"
namespace BlackMisc
{
namespace Math
{
class CMatrix3x3;
//! Matrix 3x1
class CMatrix3x1 : public CValueObject<CMatrix3x1, CMatrixBase<CMatrix3x1, 3, 1>>
{
friend class CMatrix3x3;
public:
//! Constructor
CMatrix3x1() = default;
//! Constructor
CMatrix3x1(double r1, double r2, double r3)
{
this->m_matrix(0, 0) = r1;
this->m_matrix(1, 0) = r2;
this->m_matrix(2, 0) = r3;
}
//! Init by fill value
explicit CMatrix3x1(double fillValue) : CValueObject(fillValue) {}
//! Convert to vector
CVector3D toVector3D() const
{
return CVector3D(this->getElement(0, 0), this->getElement(1, 0), this->getElement(2, 0));
}
//! Convert from vector
void fromVector3D(const CVector3D &vector)
{
this->m_matrix(0, 0) = vector.i();
this->m_matrix(1, 0) = vector.j();
this->m_matrix(2, 0) = vector.k();
}
};
}
}
Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix3x1)
#endif // guard

View File

@@ -1,80 +0,0 @@
/* 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 "blackmisc/mathmatrix3x3.h"
namespace BlackMisc
{
namespace Math
{
/*
* Determinant
*/
double CMatrix3x3::determinant() const
{
return
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);
}
/*
* Inverse
*/
CMatrix3x3 CMatrix3x3::inverse(bool &o_invertible) const
{
CMatrix3x3 inverse;
double det = this->determinant();
if (det == 0)
{
o_invertible = false;
inverse.setZero();
return inverse;
}
double 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;
o_invertible = true;
return inverse;
}
/*
* Get a row
*/
CMatrix1x3 CMatrix3x3::getRow(int row) const
{
bool valid = row >= 0 && row <= 3;
Q_ASSERT_X(valid, "getRow", "invalid row");
if (!valid) throw std::range_error("invalid row");
return CMatrix1x3(this->getElement(row, 0), this->getElement(row, 1), this->getElement(row, 2));
}
/*
* Get a column
*/
CMatrix3x1 CMatrix3x3::getColumn(int column) const
{
bool valid = column >= 0 && column <= 3;
Q_ASSERT_X(valid, "getColumn", "invalid column");
if (!valid) throw new std::range_error("invalid column");
return CMatrix3x1(this->getElement(0, column), this->getElement(1, column), this->getElement(2, column));
}
} // namespace
} // namespace

View File

@@ -1,120 +0,0 @@
/* Copyright (C) 2013
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_POSMATRIX3X3_H
#define BLACKMISC_POSMATRIX3X3_H
#include "blackmisc/mathmatrix1x3.h"
#include "blackmisc/mathmatrix3x1.h"
#include <stdexcept>
namespace BlackMisc
{
namespace Math
{
//! 3x3 matrix
class CMatrix3x3 : public CValueObject<CMatrix3x3, CMatrixBase<CMatrix3x3, 3, 3>>
{
public:
//! Constructor
CMatrix3x3() = default;
//! Init by fill value
explicit CMatrix3x3(double fillValue) : CValueObject(fillValue) {}
//! Stupid, but handy constructor by single row column values
explicit CMatrix3x3(double r1c1, double r1c2, double r1c3,
double r2c1, double r2c2, double r2c3,
double r3c1, double r3c2, double r3c3)
{
this->setElement(0, 0, r1c1);
this->setElement(0, 1, r1c2);
this->setElement(0, 2, r1c3);
this->setElement(1, 0, r2c1);
this->setElement(1, 1, r2c2);
this->setElement(1, 2, r2c3);
this->setElement(2, 0, r3c1);
this->setElement(2, 1, r3c2);
this->setElement(2, 2, r3c3);
}
//! Calculates the determinant of the matrix
double determinant() const;
//! Calculate the inverse and mark flag if successful
CMatrix3x3 inverse(bool &o_isInvertible) const;
//! Operator *=
CMatrix3x3 &operator *=(const CMatrix3x3 &other)
{
this->m_matrix = this->m_matrix * other.m_matrix;
return *this;
}
//! Operator *
CMatrix3x3 operator *(const CMatrix3x3 &other) const
{
CMatrix3x3 m(*this);
m *= other;
return m;
}
//! Operator *
CMatrix3x1 operator *(const CMatrix3x1 &other) const
{
CMatrix3x1 m;
m.m_matrix = this->m_matrix * other.m_matrix;
return m;
}
//! Multiply this matrix with vector
CVector3D operator *(const CVector3D &vector) const
{
return ((*this) * CMatrix3x1(vector.toMatrix3x1())).toVector3D();
}
//! Multiply with factor
CMatrix3x3 &operator *=(double factor)
{
this->CMatrixBase::operator *=(factor);
return *this;
}
//! Multiply with factor
CMatrix3x3 operator *(double factor) const
{
CMatrix3x3 m(*this);
m *= factor;
return m;
}
//! Transposed matrix
CMatrix3x3 transposed() const
{
CMatrix3x3 m(0);
m.m_matrix = this->m_matrix.transposed();
return m;
}
//! Get column
CMatrix3x1 getColumn(int column) const;
//! Get row
CMatrix1x3 getRow(int row) const;
};
}
}
Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix3x3)
#endif // guard

View File

@@ -1,241 +0,0 @@
/* 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"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QJsonArray>
#include <typeinfo>
namespace BlackMisc
{
namespace Math
{
/*
* Get element by column / row
*/
template<class ImplMatrix, int Rows, int Columns> double CMatrixBase<ImplMatrix, Rows, Columns>::getElement(int row, int column) const
{
this->checkRange(row, column);
return this->m_matrix(row, column);
}
/*
* Set element by column / row
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::setElement(int row, int column, double value)
{
this->checkRange(row, column);
this->m_matrix(row, column) = value;
}
/*
* Check range
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::checkRange(int row, int column) const
{
bool valid = (row < Rows && column < Columns);
Q_ASSERT_X(valid, "getElement()", "Row or column invalid");
if (!valid) throw std::range_error("Row or column invalid");
}
/*
* All values zero?
*/
template<class ImplMatrix, int Rows, int Columns> bool CMatrixBase<ImplMatrix, Rows, Columns>::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;
}
/*
* Set cell index
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::setCellIndex()
{
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
this->m_matrix(r, c) = r + 0.1 * c;
}
}
}
/*
* To list
*/
template<class ImplMatrix, int Rows, int Columns> QList<double> CMatrixBase<ImplMatrix, Rows, Columns>::toList() const
{
QList<double> list;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
list.append(this->m_matrix(r, c));
}
}
return list;
}
/*
* From list
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::fromList(const QList<double> &list)
{
Q_ASSERT_X(Rows * Columns == list.count(), "fromList()", "Mismatch of elements in list");
int ct = 0;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
this->m_matrix(r, c) = list.at(ct++);
}
}
}
/*
* Round all values
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::round()
{
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
this->m_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10);
}
}
}
/*
* Convert to string
*/
template <class ImplMatrix, int Rows, int Columns> QString CMatrixBase<ImplMatrix, Rows, Columns>::convertToQString(bool /* i18n */) 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;
}
/*
* Compare
*/
template <class ImplMatrix, int Rows, int Columns> int CMatrixBase<ImplMatrix, Rows, Columns>::compareImpl(const ImplMatrix &a, const ImplMatrix &b)
{
for (int r = 0; r < Rows; ++r)
{
for (int c = 0; c < Columns; ++c)
{
if (a.m_matrix(r, c) < b.m_matrix(r, c)) { return -1; }
if (a.m_matrix(r, c) > b.m_matrix(r, c)) { return 1; }
}
}
return 0;
}
/*
* Hash
*/
template <class ImplMatrix, int Rows, int Columns> uint CMatrixBase<ImplMatrix, Rows, Columns>::getValueHash() const
{
const QList<double> l = this->toList();
QList<uint> hashs;
// there is an issue with the signature of QList, so I use
// individual values
foreach(double v, l)
{
hashs << qHash(static_cast<long>(v));
}
return BlackMisc::calculateHash(hashs, "CMatrixBase");
}
/*
* To DBus
*/
template <class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::marshallToDbus(QDBusArgument &argument) const
{
const QList<double> l = this->toList();
// there is an issue with the signature of QList, so I use
// individual values
foreach(double v, l)
{
argument << v;
}
}
/*
* From DBus
*/
template <class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::unmarshallFromDbus(const QDBusArgument &argument)
{
QList<double> list;
double v;
while (!argument.atEnd())
{
argument >> v;
list.append(v);
}
this->fromList(list);
}
/*
* To JSON
*/
template <class ImplMatrix, int Rows, int Columns> QJsonObject CMatrixBase<ImplMatrix, Rows, Columns>::toJson() const
{
QJsonObject json;
QJsonArray jsonArray;
foreach(double v, this->toList())
{
jsonArray.append(QJsonValue(v));
}
json.insert("matrix", QJsonValue(jsonArray));
return json;
}
/*
* From Json
*/
template <class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::convertFromJson(const QJsonObject &json)
{
QJsonArray jsonArray = json.value("matrix").toArray();
QList<double> list;
for (auto i = jsonArray.begin(); i != jsonArray.end(); ++i)
{
list.append((*i).toDouble());
}
this->fromList(list);
}
// see here for the reason of these forward instantiations
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
template class CMatrixBase<CMatrix3x3, 3, 3>;
template class CMatrixBase<CMatrix3x1, 3, 1>;
template class CMatrixBase<CMatrix1x3, 1, 3>;
} // namespace
} // namespace

View File

@@ -1,252 +0,0 @@
/* 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 BLACKMISC_MATHMATRIXBASE_H
#define BLACKMISC_MATHMATRIXBASE_H
//! \file
#include "blackmisc/valueobject.h"
#include "blackmisc/mathvector3dbase.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QGenericMatrix>
namespace BlackMisc
{
namespace Math { template <class, int, int> class CMatrixBase; }
//! \private
template <class ImplMatrix, int Rows, int Columns> struct CValueObjectPolicy<Math::CMatrixBase<ImplMatrix, Rows, Columns>> : public CValueObjectPolicy<>
{
using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own;
};
namespace Math
{
/*!
* \brief Base functionality of a matrix
*/
template<class ImplMatrix, int Rows, int Columns> class CMatrixBase : public CValueObject<CMatrixBase<ImplMatrix, Rows, Columns>>
{
//! \copydoc CValueObject::compare
friend int compare(const ImplMatrix &a, const ImplMatrix &b) { return compareImpl(a, b); }
public:
//! \brief Default constructor
CMatrixBase() = default;
//! \brief Fill with value
explicit CMatrixBase(double fillValue)
{
this->fill(fillValue);
}
//! \brief List of values
QList<double> toList() const;
//! \brief List of values
void fromList(const QList<double> &list);
//! \copydoc CValueObject::getValueHash
virtual uint getValueHash() const override;
//! \brief Equal operator ==
bool operator ==(const ImplMatrix &other) const
{
if (this == &other) return true;
return this->m_matrix == other.m_matrix;
}
//! \brief Unequal operator !=
bool operator !=(const ImplMatrix &other) const
{
return !((*this) == other);
}
//! \brief Operator *=
CMatrixBase &operator *=(double factor)
{
this->m_matrix *= factor;
return *this;
}
//! \brief Operator *
ImplMatrix operator *(double factor) const
{
ImplMatrix m = *derived();
m *= factor;
return m;
}
//! \brief Operator to support commutative scalar multiplication
friend ImplMatrix operator *(double factor, const ImplMatrix &other)
{
return other * factor;
}
//! \brief Operator /=
CMatrixBase &operator /=(double factor)
{
this->m_matrix /= factor;
return *this;
}
//! \brief Operator /
ImplMatrix operator /(double factor) const
{
ImplMatrix m = *derived();
m /= factor;
return m;
}
//! \brief Operator +=
CMatrixBase &operator +=(const CMatrixBase &other)
{
this->m_matrix += other.m_matrix;
return *this;
}
//! \brief Operator +
ImplMatrix operator +(const ImplMatrix &other) const
{
ImplMatrix m = *derived();
m += other;
return m;
}
//! \brief Operator -=
CMatrixBase &operator -=(const CMatrixBase &other)
{
this->m_matrix -= other.m_matrix;
return *this;
}
//! \brief Operator -
ImplMatrix operator -(const ImplMatrix &other) const
{
ImplMatrix m = *derived();
m -= other;
return m;
}
//! \brief Is identity matrix?
bool isIdentity() const
{
return this->m_matrix.isIdentity();
}
//! \brief Is identity matrix? Epsilon considered.
bool isIdentityEpsilon() const
{
ImplMatrix m = *derived();
m.round();
return m.isIdentity();
}
//! \brief Set as identity matrix
void setToIdentity()
{
this->m_matrix.setToIdentity();
}
//! \brief All values to zero
void setZero() { this->m_matrix.fill(0.0); }
//! \brief Is zero?
bool isZero() const;
//! \brief Is zero matrix? Epsilon considered.
bool isZeroEpsilon() const
{
ImplMatrix m = *derived();
m.round();
return m.isZero();
}
//! \brief Each cell gets a unique index (used primarily for testing)
void setCellIndex();
//! \brief Set all elements the same
void fill(double value) { this->m_matrix.fill(value); }
//! \brief Round all values
void round();
//! \brief Return a rounded matrix
ImplMatrix roundedMatrix() const
{
ImplMatrix m = *derived();
m.round();
return m;
}
//! \brief Get element
double getElement(int row, int column) const;
//! \brief Set element
void setElement(int row, int column, double value);
//! \brief Get element by operator () modifying
double &operator()(int row, int column)
{
this->checkRange(row, column);
return this->m_matrix(row, column);
}
//! \brief Get element by operator () read only
double operator()(int row, int column) const
{
return this->getElement(row, column);
}
//! \copydoc CValueObject::toJson
virtual QJsonObject toJson() const override;
//! \copydoc CValueObject::convertFromJson
virtual void convertFromJson(const QJsonObject &json) override;
protected:
// no bug, Qt expects columns rows
QGenericMatrix<Columns, Rows, double> m_matrix; //!< backing data
//! \copydoc CValueObject::marshallToDbus
virtual void marshallToDbus(QDBusArgument &argument) const override;
//! \copydoc CValueObject::unmarshallFromDbus
virtual void unmarshallFromDbus(const QDBusArgument &argument) override;
//! \copydoc CValueObject::convertToQString
virtual QString convertToQString(bool i18n = false) const override;
private:
//! \brief Easy access to derived class (CRTP template parameter)
ImplMatrix const *derived() const
{
return static_cast<ImplMatrix const *>(this);
}
//! \brief Easy access to derived class (CRTP template parameter)
ImplMatrix *derived()
{
return static_cast<ImplMatrix *>(this);
}
//! \brief Check range of row / column
void checkRange(int row, int column) const;
//! Implementation of compare
static int compareImpl(const ImplMatrix &, const ImplMatrix &);
};
} // namespace
} // namespace
#endif // guard

View File

@@ -1,108 +0,0 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#ifndef BLACKMISC_MATHVECTOR3D_H
#define BLACKMISC_MATHVECTOR3D_H
//! \file
#include "blackmisc/mathvector3dbase.h"
namespace BlackMisc
{
namespace Math { class CVector3D; }
//! \private
template <> struct CValueObjectPolicy<Math::CVector3D> : public CValueObjectPolicy<>
{
using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own;
};
namespace Math
{
/*!
* \brief Concrete vector implementation
*/
class CVector3D : public CValueObject<CVector3D, CVector3DBase<CVector3D>>
{
public:
/*!
* \brief Default constructor
*/
CVector3D() = default;
/*!
* \brief Constructor by value
*/
CVector3D(double i, double j, double k) : CValueObject(i, j, k) {}
/*!
* \brief Constructor by value
*/
explicit CVector3D(double value) : CValueObject(value) {}
/*!
* \brief i
*/
double i() const
{
return this->m_i;
}
/*!
* \brief j
*/
double j() const
{
return this->m_j;
}
/*!
* \brief k
*/
double k() const
{
return this->m_k;
}
/*!
* \brief Set i
*/
void setI(double i)
{
this->m_i = i;
}
/*!
* \brief Set j
*/
void setJ(double j)
{
this->m_j = j;
}
/*!
* \brief Set k
*/
void setK(double k)
{
this->m_k = k;
}
};
}
}
Q_DECLARE_METATYPE(BlackMisc::Math::CVector3D)
#endif // guard

View File

@@ -1,173 +0,0 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/mathvector3d.h"
#include "blackmisc/mathmatrix3x3.h"
#include "blackmisc/coordinateecef.h"
#include "blackmisc/coordinatened.h"
#include "blackmisc/blackmiscfreefunctions.h"
namespace BlackMisc
{
namespace Math
{
/*
* Convert to string
*/
template <class ImplVector> QString CVector3DBase<ImplVector>::convertToQString(bool /* i18n */) const
{
QString s = ("{%1, %2, %3}");
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;
}
/*
* Vector to zero
*/
template <class ImplVector> void CVector3DBase<ImplVector>::setZero()
{
this->fill(0.0);
}
/*
* Vector to zero
*/
template <class ImplVector> void CVector3DBase<ImplVector>::fill(double value)
{
this->m_i = value;
this->m_j = value;
this->m_k = value;
}
/*
* Element (return by reference)
*/
template <class ImplVector> double &CVector3DBase<ImplVector>::getElement(int row)
{
switch (row)
{
case 0:
return this->m_i;
case 1:
return this->m_j;
case 2:
return this->m_k;
default:
Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector");
throw std::range_error("Detected invalid index in 3D vector");
}
}
/*
* Element
*/
template <class ImplVector> double CVector3DBase<ImplVector>::getElement(int row) const
{
return const_cast<CVector3DBase<ImplVector>*>(this)->getElement(row);
}
/*
* Set given element
*/
template <class ImplVector> void CVector3DBase<ImplVector>::setElement(int row, double value)
{
switch (row)
{
case 0:
this->m_i = value;
break;
case 1:
this->m_j = value;
break;
case 2:
this->m_k = value;
break;
default:
Q_ASSERT_X(true, "setElement", "Detected invalid index in 3D vector");
throw std::range_error("Detected invalid index in 3D vector");
break;
}
}
/*
* Cross product
*/
template <class ImplVector> ImplVector CVector3DBase<ImplVector>::crossProduct(const ImplVector &other) const
{
ImplVector v(other);
v.m_i = this->m_j * other.m_k - this->m_k * other.m_j;
v.m_j = this->m_k * other.m_i - this->m_i * other.m_k;
v.m_k = this->m_i * other.m_j - this->m_j * other.m_i;
return v;
}
/*
* Cross product
*/
template <class ImplVector> double CVector3DBase<ImplVector>::dotProduct(const ImplVector &other) const
{
return this->m_i * other.m_i + this->m_j * other.m_j + this->m_k * other.m_k;
}
/*
* Convert to matrix
*/
template <class ImplVector> CMatrix3x1 CVector3DBase<ImplVector>::toMatrix3x1() const
{
return CMatrix3x1(this->m_i, this->m_j, this->m_k);
}
/*
* getValueHash()
*/
template <class ImplVector> uint CVector3DBase<ImplVector>::getValueHash() const
{
QList<uint> hashs;
hashs << qHash(static_cast<long>(this->m_i));
hashs << qHash(static_cast<long>(this->m_j));
hashs << qHash(static_cast<long>(this->m_k));
return BlackMisc::calculateHash(hashs, "CVector3DBase");
}
/*
* To JSON
*/
template <class ImplVector> QJsonObject CVector3DBase<ImplVector>::toJson() const
{
QJsonObject json;
QJsonArray jsonArray;
jsonArray.append(QJsonValue(this->m_i));
jsonArray.append(QJsonValue(this->m_j));
jsonArray.append(QJsonValue(this->m_k));
json.insert("vector", QJsonValue(jsonArray));
return json;
}
/*
* From Json
*/
template <class ImplVector> void CVector3DBase<ImplVector>::convertFromJson(const QJsonObject &json)
{
QJsonArray jsonArray = json.value("vector").toArray();
this->m_i = jsonArray.at(0).toDouble();
this->m_i = jsonArray.at(1).toDouble();
this->m_i = jsonArray.at(2).toDouble();
}
// 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<CVector3D>;
template class CVector3DBase<BlackMisc::Geo::CCoordinateEcef>;
template class CVector3DBase<BlackMisc::Geo::CCoordinateNed>;
} // namespace
} // namespace

View File

@@ -1,257 +0,0 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_MATHVECTOR3DBASE_H
#define BLACKMISC_MATHVECTOR3DBASE_H
#include "blackmisc/valueobject.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/blackmiscfreefunctions.h"
namespace BlackMisc
{
namespace Math { template <class> class CVector3DBase; }
//! \private
template <class ImplVector> struct CValueObjectPolicy<Math::CVector3DBase<ImplVector>> : public CValueObjectPolicy<>
{
using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None;
using Hash = Policy::Hash::Own;
using Json = Policy::Json::Own;
};
namespace Math
{
class CMatrix3x1;
//! 3D vector base (x, y, z)
template <class ImplVector> class CVector3DBase : public CValueObject<CVector3DBase<ImplVector>>
{
public:
// getter and setters are implemented in the derived classes
// as they have different names (x, i, north)
//! Set zeros
void setZero();
//! Is zero?
bool isZero() const
{
return this->m_i == 0 && this->m_j == 0 && this->m_k == 0;
}
//! Is zero, epsilon considered.
bool isZeroEpsilon() const
{
ImplVector v;
v += *this;
v.round();
return v.isZero();
}
//! Set all elements the same
void fill(double value);
//! Get element
double getElement(int row) const;
//! Set element
void setElement(int row, double value);
//! Operator []
double operator[](int row) const { return this->getElement(row); }
//! Operator [], mutable reference
double &operator[](int row) { return this->getElement(row); }
//! Equal operator ==
bool operator ==(const CVector3DBase &other) const
{
if (this == &other) return true;
return
CMath::epsilonEqual(this->m_i, other.m_i, 1E-9) &&
CMath::epsilonEqual(this->m_j, other.m_j, 1E-9) &&
CMath::epsilonEqual(this->m_k, other.m_k, 1E-9);
}
//! Unequal operator !=
bool operator !=(const CVector3DBase &other) const
{
return !((*this) == other);
}
//! Operator +=
CVector3DBase &operator +=(const CVector3DBase &other)
{
this->m_i += other.m_i;
this->m_j += other.m_j;
this->m_k += other.m_k;
return *this;
}
//! Operator +
ImplVector operator +(const ImplVector &other) const
{
ImplVector v = *derived();
v += other;
return v;
}
//! Operator -=
CVector3DBase &operator -=(const CVector3DBase &other)
{
this->m_i -= other.m_i;
this->m_j -= other.m_j;
this->m_k -= other.m_k;
return *this;
}
//! Operator -
ImplVector operator -(const ImplVector &other) const
{
ImplVector v = *derived();
v -= other;
return v;
}
//! Multiply with scalar
CVector3DBase &operator *=(double factor)
{
this->m_i *= factor;
this->m_j *= factor;
this->m_k *= factor;
return *this;
}
//! Multiply with scalar
ImplVector operator *(double factor) const
{
ImplVector v = *derived();
v *= factor;
return v;
}
//! Operator to support commutative multiplication
friend ImplVector operator *(double factor, const ImplVector &other)
{
return other * factor;
}
//! Divide by scalar
CVector3DBase &operator /=(double divisor)
{
this->m_i /= divisor;
this->m_j /= divisor;
this->m_k /= divisor;
return *this;
}
//! Divide by scalar
ImplVector operator /(double divisor) const
{
ImplVector v = *derived();
v /= divisor;
return v;
}
//! Dot product
double dotProduct(const ImplVector &other) const;
//! Cross product
ImplVector crossProduct(const ImplVector &other) const;
//! Reciprocal value
ImplVector reciprocalValues() const
{
ImplVector v(1 / this->m_i, 1 / this->m_j, 1 / this->m_j);
return v;
}
//! Converted to matrix
CMatrix3x1 toMatrix3x1() const;
//! \copydoc CValueObject::getValueHash
virtual uint getValueHash() const override;
//! length / magnitude
double length() const
{
return sqrt(this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k);
}
//! Round this vector
void round()
{
const double epsilon = 1E-10;
this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon);
this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon);
this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon);
}
//! Rounded vector
ImplVector rounded() const
{
ImplVector v = *derived();
v.round();
return v;
}
//! \copydoc CValueObject::toJson
virtual QJsonObject toJson() const override;
//! \copydoc CValueObject::convertFromJson
virtual void convertFromJson(const QJsonObject &json) override;
protected:
// using own value since Qt QVector3D stores internally as float
double m_i; //!< Vector data i
double m_j; //!< Vector data j
double m_k; //!< Vector data k
//! Default constructor
CVector3DBase() : m_i(0.0), m_j(0.0), m_k(0.0) {}
//! Constructor by values
CVector3DBase(double i, double j, double k) : m_i(i), m_j(j), m_k(k) {}
//! Constructor by value
explicit CVector3DBase(double value) : m_i(value), m_j(value), m_k(value) {}
//! Get mutable element
double &getElement(int row);
//! \copydoc CValueObject::convertToQString
virtual QString convertToQString(bool i18n = false) const override;
private:
BLACK_ENABLE_TUPLE_CONVERSION(CVector3DBase)
//! Easy access to derived class (CRTP template parameter)
ImplVector const *derived() const
{
return static_cast<ImplVector const *>(this);
}
//! Easy access to derived class (CRTP template parameter)
ImplVector *derived()
{
return static_cast<ImplVector *>(this);
}
};
}
}
BLACK_DECLARE_TUPLE_CONVERSION_TEMPLATE(BlackMisc::Math::CVector3DBase, (o.m_i, o.m_j, o.m_k))
#endif // guard