/* 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 namespace BlackMisc { namespace Math { /* * Get element by column / row */ template double CMatrixBase::getElement(size_t row, size_t column) const { this->checkRange(row, column); return this->m_matrix(row, column); } /* * Set element by column / row */ template void CMatrixBase::setElement(size_t row, size_t column, double value) { this->checkRange(row, column); this->m_matrix(row, column) = value; } /* * Check range */ template void CMatrixBase::checkRange(size_t row, size_t 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 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; } /* * Set cell index */ template void CMatrixBase::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 QList CMatrixBase::toList() const { QList 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 void CMatrixBase::fromList(const QList &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 void CMatrixBase::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 QString CMatrixBase::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; } /* * metaTypeId */ template int CMatrixBase::getMetaTypeId() const { return qMetaTypeId(); } /* * is a */ template bool CMatrixBase::isA(int metaTypeId) const { if (metaTypeId == qMetaTypeId()) { return true; } return this->CValueObject::isA(metaTypeId); } /* * Compare */ template int CMatrixBase::compareImpl(const CValueObject &otherBase) const { const auto &other = static_cast(otherBase); for (int r = 0; r < Rows; ++r) { for (int c = 0; c < Columns; ++c) { if (this->m_matrix(r, c) < other.m_matrix(r, c)) { return -1; } if (this->m_matrix(r, c) > other.m_matrix(r, c)) { return 1; } } } return 0; } /* * Hash */ template uint CMatrixBase::getValueHash() const { const QList l = this->toList(); QList hashs; // there is an issue with the signature of QList, so I use // individual values foreach(double v, l) { hashs << qHash(static_cast(v)); } return BlackMisc::calculateHash(hashs, "CMatrixBase"); } /* * To DBus */ template void CMatrixBase::marshallToDbus(QDBusArgument &argument) const { const QList 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 void CMatrixBase::unmarshallFromDbus(const QDBusArgument &argument) { QList list; double v; while (!argument.atEnd()) { argument >> v; list.append(v); } this->fromList(list); } /* * Register metadata */ template void CMatrixBase::registerMetadata() { qRegisterMetaType(); qDBusRegisterMetaType(); } // 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; template class CMatrixBase; } // namespace } // namespace