refs #396 move Blackmisc Math classes into subfolder

This commit is contained in:
Roland Winklmeier
2015-04-02 01:37:22 +02:00
parent 608091912c
commit a6ca57265e
21 changed files with 132 additions and 139 deletions

View File

@@ -63,8 +63,8 @@ namespace BlackMisc
quint32 d = static_cast<quint32>(f / channelSpacingKHz);
frequency.switchUnit(CFrequencyUnit::MHz());
double f0 = frequency.valueRounded(CFrequencyUnit::MHz(), 3);
double f1 = CMath::round(d * (channelSpacingKHz / 1000.0), 3);
double f2 = CMath::round((d + 1) * (channelSpacingKHz / 1000.0), 3);
double f1 = CMathUtils::round(d * (channelSpacingKHz / 1000.0), 3);
double f2 = CMathUtils::round((d + 1) * (channelSpacingKHz / 1000.0), 3);
bool down = qAbs(f1 - f0) < qAbs(f2 - f0); // which is the closest value
frequency.setCurrentUnitValue(down ? f1 : f2);
}

View File

@@ -14,7 +14,7 @@
#include "blackmisc/propertyindex.h"
#include "blackmisc/aviation/avionicsbase.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/math/mathutils.h"
#include "blackmisc/blackmiscfreefunctions.h"
namespace BlackMisc
@@ -126,14 +126,14 @@ namespace BlackMisc
//! Set active frequency
virtual void setFrequencyActiveMHz(double frequencyMHz)
{
frequencyMHz = Math::CMath::round(frequencyMHz, 3);
frequencyMHz = Math::CMathUtils::round(frequencyMHz, 3);
this->m_frequencyActive = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz());
}
//! Set standby frequency
virtual void setFrequencyStandbyMHz(double frequencyMHz)
{
frequencyMHz = Math::CMath::round(frequencyMHz, 3);
frequencyMHz = Math::CMathUtils::round(frequencyMHz, 3);
this->m_frequencyStandby = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz());
}

View File

@@ -18,11 +18,6 @@
* \brief Base and utility classes available in all other projects.
*/
/*!
* \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.

View File

@@ -25,11 +25,13 @@ TRANSLATIONS += translations/blackmisc_i18n_de.ts \
HEADERS += *.h \
$$PWD/aviation/*.h \
$$PWD/math/*.h \
$$PWD/simulation/*.h \
SOURCES += *.cpp \
$$PWD/aviation/*.cpp \
$$PWD/math/*.cpp \
$$PWD/simulation/*.cpp \

View File

@@ -10,7 +10,7 @@
#include "blackmiscfreefunctions.h"
#include "blackmisc/aviation/aviation.h"
#include "pqallquantities.h"
#include "mathallclasses.h"
#include "math/math.h"
#include "geoallclasses.h"
#include "networkallclasses.h"
#include "audioallclasses.h"

View File

@@ -10,7 +10,7 @@
#include "blackmisc/coordinategeodetic.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include "blackmisc/propertyindex.h"
#include "mathematics.h"
#include "blackmisc/math/mathutils.h"
#include "variant.h"
#include <QtCore/qmath.h>
@@ -79,8 +79,8 @@ namespace BlackMisc
double x = qCos(lat1rad) * qSin(lat2rad) -
qSin(lat1rad) * qCos(lat2rad) * qCos(dLon);
double bearing = qAtan2(y, x);
bearing = CMath::rad2deg(bearing); // now in deg
bearing = CMath::normalizeDegrees(bearing); // normalize
bearing = CMathUtils::rad2deg(bearing); // now in deg
bearing = CMathUtils::normalizeDegrees(bearing); // normalize
return CAngle(bearing, CAngleUnit::deg());
}

View File

@@ -8,7 +8,7 @@
//! \file
#include "mathematics.h"
#include "blackmisc/math/mathutils.h"
#include "coordinategeodetic.h"
#include "range.h"
#include "iterator.h"
@@ -128,12 +128,12 @@ namespace BlackMisc
{
using namespace std;
using namespace BlackMisc::Math;
Q_ASSERT(lat >= -CMath::PIHALF() && lat <= CMath::PIHALF());
Q_ASSERT(lon >= -CMath::PI() && lon <= CMath::PI());
static const double ratio = Slices / CMath::PI();
Q_ASSERT(lat >= -CMathUtils::PIHALF() && lat <= CMathUtils::PIHALF());
Q_ASSERT(lon >= -CMathUtils::PI() && lon <= CMathUtils::PI());
static const double ratio = Slices / CMathUtils::PI();
Key x = qFloor(acos(cos(lat) * cos(lon)) * ratio);
Key y = qFloor(acos(cos(lat) * sin(lon)) * ratio);
Key z = qFloor( (lat + CMath::PIHALF()) * ratio);
Key z = qFloor( (lat + CMathUtils::PIHALF()) * ratio);
return (x << Xshift) | (y << Yshift) | (z << Zshift);
}

22
src/blackmisc/math/math.h Normal file
View File

@@ -0,0 +1,22 @@
/* 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_MATH_MATH_H
#define BLACKMISC_MATH_MATH_H
/*!
* \namespace BlackMisc::Math
* \brief Math classes such as vectors, matrices, and utility methods.
*/
#include "blackmisc/math/mathutils.h"
#endif // guard

View File

@@ -1,20 +1,22 @@
/* 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/. */
/* 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/mathematics.h"
#include <algorithm> // std::max
#include "blackmisc/math/mathutils.h"
#include <algorithm>
#include <cmath>
namespace BlackMisc
{
namespace Math
{
/*
* Hypotenuse
*/
double CMath::hypot(double x, double y)
double CMathUtils::hypot(double x, double y)
{
x = qAbs(x);
y = qAbs(y);
@@ -24,20 +26,14 @@ namespace BlackMisc
return max * sqrt(1 + r * r);
}
/*
* Real part of cubic root
*/
double CMath::cubicRootReal(double x)
double CMathUtils::cubicRootReal(double x)
{
double result;
result = std::pow(qAbs(x), (double)1.0 / 3.0);
return x < 0 ? -result : result;
}
/*
* Round utility method
*/
double CMath::round(double value, int digits)
double CMathUtils::round(double value, int digits)
{
// gosh, is there no Qt method for this??? It's year 2013
double fractpart, intpart;
@@ -49,10 +45,7 @@ namespace BlackMisc
return rv;
}
/*
* Round by given epsilon value
*/
double CMath::roundEpsilon(double value, double epsilon)
double CMathUtils::roundEpsilon(double value, double epsilon)
{
double fractpart, intpart;
fractpart = modf(value, &intpart);
@@ -62,35 +55,23 @@ namespace BlackMisc
return rv;
}
/*
* Equal, considering equal
*/
bool CMath::epsilonEqual(double v1, double v2, double epsilon)
bool CMathUtils::epsilonEqual(double v1, double v2, double epsilon)
{
if (v1 == v2) return true;
return qAbs(v1 - v2) <= epsilon;
}
/*
* To radians
*/
double CMath::deg2rad(double degree)
double CMathUtils::deg2rad(double degree)
{
return degree * CMath::PI() / 180.0;
return degree * CMathUtils::PI() / 180.0;
}
/*
* To radians
*/
double CMath::rad2deg(double radians)
double CMathUtils::rad2deg(double radians)
{
return radians * 180.0 / CMath::PI();
return radians * 180.0 / CMathUtils::PI();
}
/*
* Normalize degrees
*/
double CMath::normalizeDegrees(double degrees)
double CMathUtils::normalizeDegrees(double degrees)
{
double result = std::fmod(degrees, 360.0);
return (result >= 0.0) ? result : result + 360.0;

View File

@@ -1,10 +1,17 @@
/* 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/. */
/* 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_MATH_MATHUTILS_H
#define BLACKMISC_MATH_MATHUTILS_H
#ifndef BLACKMISC_MATHEMATICS_H
#define BLACKMISC_MATHEMATICS_H
#include <QtCore/qmath.h>
#include <cmath>
@@ -14,10 +21,13 @@ namespace BlackMisc
{
//! Math utils
class CMath
class CMathUtils
{
public:
//! No objects, just static
CMathUtils() = delete;
//! Calculates the hypotenuse of x and y without overflow
static double hypot(double x, double y);
@@ -87,12 +97,9 @@ namespace BlackMisc
//! Normalize: 0≤ degrees <360
static double normalizeDegrees(double degrees);
private:
//! No objects, just static
CMath();
};
} // namespace
} // namespace
#endif // guard

View File

@@ -1,14 +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_MATHALLCLASSES_H
#define BLACKMISC_MATHALLCLASSES_H
#include "blackmisc/mathmatrix1x3.h"
#include "blackmisc/mathmatrix3x3.h"
#include "blackmisc/mathmatrix3x1.h"
#include "blackmisc/mathvector3d.h"
#endif // guard

View File

@@ -24,12 +24,12 @@ namespace BlackMisc
double CAngle::piFactor() const
{
return BlackMisc::Math::CMath::round(this->value(CAngleUnit::rad()) / BlackMisc::Math::CMath::PI() , 6);
return BlackMisc::Math::CMathUtils::round(this->value(CAngleUnit::rad()) / BlackMisc::Math::CMathUtils::PI() , 6);
}
const double &CAngle::PI()
{
return BlackMisc::Math::CMath::PI();
return BlackMisc::Math::CMathUtils::PI();
}
}
}

View File

@@ -13,7 +13,7 @@
#define BLACKMISC_PQANGLE_H
#include "blackmisc/pqphysicalquantity.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/math/mathutils.h"
namespace BlackMisc
{

View File

@@ -10,7 +10,7 @@
//! \file
#include "blackmisc/pqbase.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/math/mathutils.h"
using namespace BlackMisc::Math;
@@ -60,7 +60,7 @@ namespace BlackMisc
double CMeasurementUnit::roundValue(double value, int digits) const
{
if (digits < 0) digits = this->m_displayDigits;
return CMath::round(value, digits);
return CMathUtils::round(value, digits);
}
/*
@@ -69,7 +69,7 @@ namespace BlackMisc
QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool /* i18n */) const
{
if (digits < 0) digits = this->m_displayDigits;
double v = CMath::round(value, digits);
double v = CMathUtils::round(value, digits);
QString s = QLocale::system().toString(v, 'f', digits);
return s;
}

View File

@@ -13,7 +13,7 @@
#define BLACKMISC_PQBASE_H
#include "blackmisc/valueobject.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/math/mathutils.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QCoreApplication>
#include <QtDBus/QDBusArgument>
@@ -119,17 +119,17 @@ namespace BlackMisc
{
virtual double toDefault(double factor) const override
{
using BlackMisc::Math::CMath;
double part2 = CMath::fract(factor) * SubdivPolicy::fraction();
factor = CMath::trunc(factor) + part2 / SubdivPolicy::subfactor();
using BlackMisc::Math::CMathUtils;
double part2 = CMathUtils::fract(factor) * SubdivPolicy::fraction();
factor = CMathUtils::trunc(factor) + part2 / SubdivPolicy::subfactor();
return factor * FactorPolicy::factor();
}
virtual double fromDefault(double factor) const override
{
using BlackMisc::Math::CMath;
using BlackMisc::Math::CMathUtils;
factor /= FactorPolicy::factor();
double part2 = CMath::fract(factor) * SubdivPolicy::subfactor();
return CMath::trunc(factor) + part2 / SubdivPolicy::fraction();
double part2 = CMathUtils::fract(factor) * SubdivPolicy::subfactor();
return CMathUtils::trunc(factor) + part2 / SubdivPolicy::fraction();
}
};
@@ -143,19 +143,19 @@ namespace BlackMisc
{
virtual double toDefault(double factor) const override
{
using BlackMisc::Math::CMath;
double part2 = CMath::fract(factor) * SubdivPolicy::fraction();
double part3 = CMath::fract(part2) * SubdivPolicy::fraction();
factor = CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::subfactor()) / SubdivPolicy::subfactor();
using BlackMisc::Math::CMathUtils;
double part2 = CMathUtils::fract(factor) * SubdivPolicy::fraction();
double part3 = CMathUtils::fract(part2) * SubdivPolicy::fraction();
factor = CMathUtils::trunc(factor) + (CMathUtils::trunc(part2) + part3 / SubdivPolicy::subfactor()) / SubdivPolicy::subfactor();
return factor * FactorPolicy::factor();
}
virtual double fromDefault(double factor) const override
{
using BlackMisc::Math::CMath;
using BlackMisc::Math::CMathUtils;
factor /= FactorPolicy::factor();
double part2 = CMath::fract(factor) * SubdivPolicy::subfactor();
double part3 = CMath::fract(part2) * SubdivPolicy::subfactor();
return CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::fraction()) / SubdivPolicy::fraction();
double part2 = CMathUtils::fract(factor) * SubdivPolicy::subfactor();
double part3 = CMathUtils::fract(part2) * SubdivPolicy::subfactor();
return CMathUtils::trunc(factor) + (CMathUtils::trunc(part2) + part3 / SubdivPolicy::fraction()) / SubdivPolicy::fraction();
}
};

View File

@@ -15,7 +15,7 @@
#include "blackmisc/pqbase.h"
#include "blackmisc/pqunits.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/mathematics.h"
#include "blackmisc/math/mathutils.h"
#include "blackmisc/pqstring.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QtDBus/QDBusMetaType>

View File

@@ -13,7 +13,7 @@ namespace BlackMisc
{
namespace PhysicalQuantities
{
using BlackMisc::Math::CMath;
using BlackMisc::Math::CMathUtils;
/*
* Rounded to QString
@@ -26,9 +26,9 @@ namespace BlackMisc
{
digits -= 4;
Q_ASSERT(digits >= 0);
double de = CMath::trunc(value);
double mi = CMath::trunc((value - de) * 100.0);
double se = CMath::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
double de = CMathUtils::trunc(value);
double mi = CMathUtils::trunc((value - de) * 100.0);
double se = CMathUtils::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
const char *fmt = value < 0 ? "-%L1 %L2 %L3" : "%L1 %L2 %L3";
s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0');
@@ -37,8 +37,8 @@ namespace BlackMisc
{
digits -= 2;
Q_ASSERT(digits >= 0);
double de = CMath::trunc(value);
double mi = CMath::trunc((value - de) * 100.0);
double de = CMathUtils::trunc(value);
double mi = CMathUtils::trunc((value - de) * 100.0);
const char *fmt = value < 0 ? "-%L1 %L2" : "%L1 %L2";
s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', digits, '0');
@@ -61,9 +61,9 @@ namespace BlackMisc
{
digits -= 4;
Q_ASSERT(digits >= 0);
double hr = CMath::trunc(value);
double mi = CMath::trunc((value - hr) * 100.0);
double se = CMath::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
double hr = CMathUtils::trunc(value);
double mi = CMathUtils::trunc((value - hr) * 100.0);
double se = CMathUtils::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
const char *fmt = value < 0 ? "-%L1h%L2m%L3s" : "%L1h%L2m%L3s";
s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
s = s.arg(fabs(hr), 2, 'f', 0, '0').arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0');
@@ -72,8 +72,8 @@ namespace BlackMisc
{
digits -= 2;
Q_ASSERT(digits >= 0);
double hr = CMath::trunc(value);
double mi = CMath::trunc((value - hr) * 100.0);
double hr = CMathUtils::trunc(value);
double mi = CMathUtils::trunc((value - hr) * 100.0);
const char *fmt = value < 0 ? "-%L1h%L2m" : "%L1h%L2m";
s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
s = s.arg(fabs(hr), 2, 'f', 0, '0').arg(fabs(mi), 2, 'f', digits, '0');
@@ -82,8 +82,8 @@ namespace BlackMisc
{
digits -= 2;
Q_ASSERT(digits >= 0);
double mi = CMath::trunc(value);
double se = CMath::trunc((value - mi) * 100.0);
double mi = CMathUtils::trunc(value);
double se = CMathUtils::trunc((value - mi) * 100.0);
const char *fmt = value < 0 ? "-%L2m%L3s" : "%L2m%L3s";
s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
s = s.arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0');