mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 01:45:38 +08:00
refs #396 move Blackmisc Math classes into subfolder
This commit is contained in:
22
src/blackmisc/math/math.h
Normal file
22
src/blackmisc/math/math.h
Normal 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
|
||||
81
src/blackmisc/math/mathutils.cpp
Normal file
81
src/blackmisc/math/mathutils.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/* 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/math/mathutils.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Math
|
||||
{
|
||||
|
||||
double CMathUtils::hypot(double x, double y)
|
||||
{
|
||||
x = qAbs(x);
|
||||
y = qAbs(y);
|
||||
double max = std::max(x, y);
|
||||
double min = std::min(x, y);
|
||||
double r = min / max;
|
||||
return max * sqrt(1 + r * r);
|
||||
}
|
||||
|
||||
double CMathUtils::cubicRootReal(double x)
|
||||
{
|
||||
double result;
|
||||
result = std::pow(qAbs(x), (double)1.0 / 3.0);
|
||||
return x < 0 ? -result : result;
|
||||
}
|
||||
|
||||
double CMathUtils::round(double value, int digits)
|
||||
{
|
||||
// gosh, is there no Qt method for this??? It's year 2013
|
||||
double fractpart, intpart;
|
||||
fractpart = modf(value, &intpart);
|
||||
if (fractpart == 0) return value; // do not mess any "integers" to the worse
|
||||
double m = pow(10.0, digits);
|
||||
qint64 ri = qRound64(value * m); // do not loose any range here
|
||||
double rv = double(ri) / m;
|
||||
return rv;
|
||||
}
|
||||
|
||||
double CMathUtils::roundEpsilon(double value, double epsilon)
|
||||
{
|
||||
double fractpart, intpart;
|
||||
fractpart = modf(value, &intpart);
|
||||
if (fractpart == 0) return value; // do not mess any "integers" to the worse
|
||||
qint64 ri = qRound(value / epsilon);
|
||||
double rv = double(ri) * epsilon; // do not loose any range here
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool CMathUtils::epsilonEqual(double v1, double v2, double epsilon)
|
||||
{
|
||||
if (v1 == v2) return true;
|
||||
return qAbs(v1 - v2) <= epsilon;
|
||||
}
|
||||
|
||||
double CMathUtils::deg2rad(double degree)
|
||||
{
|
||||
return degree * CMathUtils::PI() / 180.0;
|
||||
}
|
||||
|
||||
double CMathUtils::rad2deg(double radians)
|
||||
{
|
||||
return radians * 180.0 / CMathUtils::PI();
|
||||
}
|
||||
|
||||
double CMathUtils::normalizeDegrees(double degrees)
|
||||
{
|
||||
double result = std::fmod(degrees, 360.0);
|
||||
return (result >= 0.0) ? result : result + 360.0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
105
src/blackmisc/math/mathutils.h
Normal file
105
src/blackmisc/math/mathutils.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/* 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
|
||||
|
||||
#include <QtCore/qmath.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Math
|
||||
{
|
||||
|
||||
//! Math utils
|
||||
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);
|
||||
|
||||
//! Calculates the square of x
|
||||
static inline double square(double x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
//! Calculates x to the power of three
|
||||
static inline double cubic(const double x)
|
||||
{
|
||||
return x * x * x;
|
||||
}
|
||||
|
||||
//! Calculates the real cubic root
|
||||
static double cubicRootReal(double x);
|
||||
|
||||
//! Utility round method
|
||||
static double round(double value, int digits);
|
||||
|
||||
//! Round by given epsilon
|
||||
static double roundEpsilon(double value, double epsilon);
|
||||
|
||||
//! Epsilon safe equal
|
||||
static bool epsilonEqual(double v1, double v2, double epsilon = 1E-06);
|
||||
|
||||
//! Nearest integer not greater in magnitude than value, correcting for epsilon
|
||||
static inline double trunc(double value, double epsilon = 1e-10)
|
||||
{
|
||||
return value < 0 ? ceil(value - epsilon) : floor(value + epsilon);
|
||||
}
|
||||
|
||||
//! Fractional part of value
|
||||
static inline double fract(double value)
|
||||
{
|
||||
double unused;
|
||||
return modf(value, &unused);
|
||||
}
|
||||
|
||||
//! PI / 2
|
||||
static const double &PIHALF()
|
||||
{
|
||||
static double pi = 2.0 * qAtan(1.0);
|
||||
return pi;
|
||||
}
|
||||
|
||||
//! PI
|
||||
static const double &PI()
|
||||
{
|
||||
static double pi = 4.0 * qAtan(1.0);
|
||||
return pi;
|
||||
}
|
||||
|
||||
//! PI * 2
|
||||
static const double &PI2()
|
||||
{
|
||||
static double pi2 = 8.0 * qAtan(1.0);
|
||||
return pi2;
|
||||
}
|
||||
|
||||
//! Degrees to radians
|
||||
static double deg2rad(double degree);
|
||||
|
||||
//! Radians to degrees
|
||||
static double rad2deg(double radians);
|
||||
|
||||
//! Normalize: 0≤ degrees <360
|
||||
static double normalizeDegrees(double degrees);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user