ENH: x/y, y/x comparison operators for Vector2D

STYLE: init label variants with std::sqrt + double (compile-time invariant)
This commit is contained in:
Mark Olesen
2022-12-09 17:17:26 +01:00
parent 95a6e8b419
commit 1888bc62ab
16 changed files with 309 additions and 210 deletions

View File

@ -90,8 +90,8 @@ primitives/Vector/lists/vectorListIOList.C
primitives/Tensor2D/tensor2D/tensor2D.C
primitives/SphericalTensor2D/sphericalTensor2D/sphericalTensor2D.C
primitives/SymmTensor2D/symmTensor2D/symmTensor2D.C
primitives/Vector2D/labelVector2D/labelVector2D.C
primitives/Vector2D/vector2D/vector2D.C
primitives/Vector2D/floats/vector2D.C
primitives/Vector2D/ints/labelVector2D.C
primitives/complex/complex.C
primitives/globalIndexAndTransform/globalIndexAndTransform.C

View File

@ -69,13 +69,13 @@ const Foam::labelSphericalTensor Foam::labelSphericalTensor::vsType::min
template<>
const Foam::labelSphericalTensor Foam::labelSphericalTensor::vsType::rootMax
(
labelSphericalTensor::uniform(sqrt(scalar(labelMax)))
labelSphericalTensor::uniform(::sqrt(double(labelMax)))
);
template<>
const Foam::labelSphericalTensor Foam::labelSphericalTensor::vsType::rootMin
(
labelSphericalTensor::uniform(-sqrt(scalar(labelMax)))
labelSphericalTensor::uniform(-::sqrt(double(labelMax)))
);
template<>

View File

@ -67,13 +67,13 @@ const Foam::labelSymmTensor Foam::labelSymmTensor::vsType::min
template<>
const Foam::labelSymmTensor Foam::labelSymmTensor::vsType::rootMax
(
labelSymmTensor::uniform(sqrt(scalar(labelMax)))
labelSymmTensor::uniform(::sqrt(double(labelMax)))
);
template<>
const Foam::labelSymmTensor Foam::labelSymmTensor::vsType::rootMin
(
labelSymmTensor::uniform(-sqrt(scalar(labelMax)))
labelSymmTensor::uniform(-::sqrt(double(labelMax)))
);
template<>

View File

@ -68,13 +68,13 @@ const Foam::labelTensor Foam::labelTensor::vsType::min
template<>
const Foam::labelTensor Foam::labelTensor::vsType::rootMax
(
labelTensor::uniform(sqrt(scalar(labelMax)))
labelTensor::uniform(::sqrt(double(labelMax)))
);
template<>
const Foam::labelTensor Foam::labelTensor::vsType::rootMin
(
labelTensor::uniform(-sqrt(scalar(labelMax)))
labelTensor::uniform(-::sqrt(double(labelMax)))
);
template<>

View File

@ -171,7 +171,7 @@ public:
inline Vector<Cmpt> cross(const Vector<Cmpt>& v2) const;
// Comparision Operations
// Comparison Operations
//- Lexicographically compare \em a and \em b with order (x:y:z)
static inline bool less_xyz

View File

@ -170,7 +170,7 @@ Foam::Vector<Cmpt>::cross(const Vector<Cmpt>& v2) const
}
// * * * * * * * * * * * * * Comparision Operations * * * * * * * * * * * * //
// * * * * * * * * * * * * * Comparison Operations * * * * * * * * * * * * * //
template<class Cmpt>
inline bool

View File

@ -32,47 +32,54 @@ License
template<>
const char* const Foam::labelVector::vsType::typeName = "labelVector";
template<>
const char* const Foam::labelVector::vsType::componentNames[] =
{
"x", "y", "z"
};
#undef defineTraits
#define defineTraits(Type, Prefix) \
\
template<> \
const char* const Foam::Vector<Type>::vsType::componentNames[] = \
{ \
"x", "y", "z" \
}; \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::zero \
( \
Vector<Type>::uniform(0) \
); \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::one \
( \
Vector<Type>::uniform(1) \
); \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::max \
( \
Vector<Type>::uniform(Prefix##Max) \
); \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::min \
( \
Vector<Type>::uniform(-Prefix##Max) \
); \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::rootMax \
( \
Vector<Type>::uniform(::sqrt(double(Prefix##Max))) \
); \
\
template<> \
const Foam::Vector<Type> Foam::Vector<Type>::vsType::rootMin \
( \
Vector<Type>::uniform(-::sqrt(double(Prefix##Max))) \
);
template<>
const Foam::labelVector Foam::labelVector::vsType::zero
(
labelVector::uniform(0)
);
template<>
const Foam::labelVector Foam::labelVector::vsType::one
(
labelVector::uniform(1)
);
template<>
const Foam::labelVector Foam::labelVector::vsType::max
(
labelVector::uniform(labelMax)
);
template<>
const Foam::labelVector Foam::labelVector::vsType::min
(
labelVector::uniform(-labelMax)
);
template<>
const Foam::labelVector Foam::labelVector::vsType::rootMax
(
labelVector::uniform(sqrt(scalar(labelMax)))
);
template<>
const Foam::labelVector Foam::labelVector::vsType::rootMin
(
labelVector::uniform(-sqrt(scalar(labelMax)))
);
defineTraits(Foam::label, label);
#undef defineTraits
// ************************************************************************* //

View File

@ -35,8 +35,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef labelVector_H
#define labelVector_H
#ifndef Foam_labelVector_H
#define Foam_labelVector_H
#include "label.H"
#include "Vector.H"

View File

@ -153,6 +153,23 @@ public:
const Vector2D<Cmpt>& b,
const scalar tol = 1e-10
) const;
// Comparison Operations
//- Lexicographically compare \em a and \em b with order (x:y)
static inline bool less_xy
(
const Vector2D<Cmpt>& a,
const Vector2D<Cmpt>& b
);
//- Lexicographically compare \em a and \em b with order (y:x)
static inline bool less_yx
(
const Vector2D<Cmpt>& a,
const Vector2D<Cmpt>& b
);
};

View File

@ -126,12 +126,38 @@ Foam::Vector2D<Cmpt>::removeCollinear(const Vector2D<Cmpt>& unitVec)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Comparison Operations * * * * * * * * * * * * * //
template<class Cmpt>
inline bool
Foam::Vector2D<Cmpt>::less_xy(const Vector2D<Cmpt>& a, const Vector2D<Cmpt>& b)
{
return
(
(a.x() < b.x())
|| (!(b.x() < a.x()) && (a.y() < b.y()))
);
}
template<class Cmpt>
inline bool
Foam::Vector2D<Cmpt>::less_yx(const Vector2D<Cmpt>& a, const Vector2D<Cmpt>& b)
{
return
(
(a.y() < b.y())
|| (!(b.y() < a.y()) && (a.x() < b.x()))
);
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Cmpt>
inline Cmpt operator&(const Vector2D<Cmpt>& v1, const Vector2D<Cmpt>& v2)

View File

@ -0,0 +1,105 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "vector2D.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
#if defined(WM_DP)
template<>
const char* const Foam::Vector2D<float>::vsType::typeName = "floatVector2D";
template<>
const char* const Foam::Vector2D<double>::vsType::typeName = "vector2D";
#else
// WM_SP, WM_SPDP
template<>
const char* const Foam::Vector2D<float>::vsType::typeName = "vector2D";
template<>
const char* const Foam::Vector2D<double>::vsType::typeName = "doubleVector2D";
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#undef defineTraits
#define defineTraits(Type, Prefix) \
\
template<> \
const char* const Foam::Vector2D<Type>::vsType::componentNames[] = \
{ \
"x", "y" \
}; \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::zero \
( \
Vector2D<Type>::uniform(0) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::one \
( \
Vector2D<Type>::uniform(1) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::max \
( \
Vector2D<Type>::uniform(Prefix##VGREAT) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::min \
( \
Vector2D<Type>::uniform(-Prefix##VGREAT) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::rootMax \
( \
Vector2D<Type>::uniform(Prefix##ROOTVGREAT) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::rootMin \
( \
Vector2D<Type>::uniform(-Prefix##ROOTVGREAT) \
);
defineTraits(float, floatScalar);
defineTraits(double, doubleScalar);
#undef defineTraits
// ************************************************************************* //

View File

@ -35,8 +35,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef vector2D_H
#define vector2D_H
#ifndef Foam_vector2D_H
#define Foam_vector2D_H
#include "scalar.H"
#include "Vector2D.H"
@ -48,6 +48,15 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//! \class Foam::floatVector2D
//! \brief A Vector2D of values with float precision
typedef Vector2D<float> floatVector2D;
//! \class Foam::doubleVector2D
//! \brief A Vector2D of values with double precision
typedef Vector2D<double> doubleVector2D;
// With float or double precision (depending on compilation)
typedef Vector2D<scalar> vector2D;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,88 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "labelVector2D.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<>
const char* const Foam::labelVector2D::vsType::typeName = "labelVector2D";
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#undef defineTraits
#define defineTraits(Type, Prefix) \
\
template<> \
const char* const Foam::Vector2D<Type>::vsType::componentNames[] = \
{ \
"x", "y" \
}; \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::zero \
( \
Vector2D<Type>::uniform(0) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::one \
( \
Vector2D<Type>::uniform(1) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::max \
( \
Vector2D<Type>::uniform(Prefix##Max) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::min \
( \
Vector2D<Type>::uniform(-Prefix##Max) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::rootMax \
( \
Vector2D<Type>::uniform(::sqrt(double(Prefix##Max))) \
); \
\
template<> \
const Foam::Vector2D<Type> Foam::Vector2D<Type>::vsType::rootMin \
( \
Vector2D<Type>::uniform(-::sqrt(double(Prefix##Max))) \
);
defineTraits(Foam::label, label);
#undef defineTraits
// ************************************************************************* //

View File

@ -34,8 +34,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef labelVector2D_H
#define labelVector2D_H
#ifndef Foam_labelVector2D_H
#define Foam_labelVector2D_H
#include "label.H"
#include "Vector2D.H"

View File

@ -1,75 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "labelVector2D.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<>
const char* const Foam::labelVector2D::vsType::typeName = "labelVector2D";
template<>
const char* const Foam::labelVector2D::vsType::componentNames[] = { "x", "y" };
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::zero
(
labelVector2D::uniform(0)
);
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::one
(
labelVector2D::uniform(1)
);
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::max
(
labelVector2D::uniform(labelMax)
);
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::min
(
labelVector2D::uniform(-labelMax)
);
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::rootMax
(
labelVector2D::uniform(sqrt(scalar(labelMax)))
);
template<>
const Foam::labelVector2D Foam::labelVector2D::vsType::rootMin
(
labelVector2D::uniform(-sqrt(scalar(labelMax)))
);
// ************************************************************************* //

View File

@ -1,78 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Vector2D of scalars.
\*---------------------------------------------------------------------------*/
#include "vector2D.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<>
const char* const Foam::vector2D::vsType::typeName = "vector2D";
template<>
const char* const Foam::vector2D::vsType::componentNames[] = {"x", "y"};
template<>
const Foam::vector2D Foam::vector2D::vsType::vsType::zero
(
vector2D::uniform(0)
);
template<>
const Foam::vector2D Foam::vector2D::vsType::one
(
vector2D::uniform(1)
);
template<>
const Foam::vector2D Foam::vector2D::vsType::max
(
vector2D::uniform(VGREAT)
);
template<>
const Foam::vector2D Foam::vector2D::vsType::min
(
vector2D::uniform(-VGREAT)
);
template<>
const Foam::vector2D Foam::vector2D::vsType::rootMax
(
vector2D::uniform(ROOTVGREAT)
);
template<>
const Foam::vector2D Foam::vector2D::vsType::rootMin
(
vector2D::uniform(-ROOTVGREAT)
);
// ************************************************************************* //