From dea31e9b4a07bbfe605ec8c209a50c48239989c7 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 14 Jul 2022 12:58:18 +0200 Subject: [PATCH] ENH: consistent member access for triFace / triangle etc. - can access the vertices/points as a(), b(), c() --- applications/test/edges/Test-edges.C | 13 ++- src/OpenFOAM/meshes/meshShapes/edge/edge.H | 41 +++++--- src/OpenFOAM/meshes/meshShapes/edge/edgeI.H | 23 ----- .../meshes/meshShapes/triFace/triFace.H | 27 +++++- .../meshes/primitiveShapes/line/line.H | 93 ++++++++++++++----- .../meshes/primitiveShapes/line/lineI.H | 20 +++- src/OpenFOAM/primitives/tuples/PairI.H | 8 +- src/fileFormats/stl/STLtriangle.H | 18 ++-- src/fileFormats/stl/STLtriangleI.H | 44 ++++----- 9 files changed, 179 insertions(+), 108 deletions(-) diff --git a/applications/test/edges/Test-edges.C b/applications/test/edges/Test-edges.C index bb38f33f82..fb6e3fd409 100644 --- a/applications/test/edges/Test-edges.C +++ b/applications/test/edges/Test-edges.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -82,8 +82,7 @@ int main(int argc, char *argv[]) Info<< nl << "hash-like functionality" << nl; - // doesn't work e4 = -1; - e4.start() = e4.end() = -1; + e4.clear(); printInfo(e4); for (label i : {2, -1, 2, 1, 4, 1, 2, 3}) @@ -93,24 +92,24 @@ int main(int argc, char *argv[]) printInfo(e4); } - e4.start() = e4.end() = -1; + e4.clear(); Info<< "insert from list\n"; labelHashSet newIndices({2, -1, 2, 1, 4, 1, 2, 3}); e4.insert(newIndices.toc()); printInfo(e4); - e4.start() = e4.end() = -1; + e4.clear(); Info<< "insert from list\n"; e4.insert({0, 5, 2, -1, 2, 1, 4, 1, 2, 3}); printInfo(e4); FixedList otherIndices{12, 2, -1, 1, 4, 1, 2, 3}; - e4.start() = e4.end() = -1; + e4.clear(); Info<< "insert from list: " << otherIndices << nl; e4.insert(otherIndices); printInfo(e4); - e4.start() = e4.end(); + e4.a() = e4.b(); Info<< "erase from list: " << otherIndices << nl; Info<< "removed " << e4.erase(otherIndices) << " values" << nl; printInfo(e4); diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edge.H b/src/OpenFOAM/meshes/meshShapes/edge/edge.H index a30a723593..5d299d08bb 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edge.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edge.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -107,27 +107,40 @@ public: // Access - //- Return first vertex label + //- The first vertex + label a() const { return labelPair::first(); } + + //- The second vertex + label b() const { return labelPair::second(); } + + //- The first vertex + label& a() { return labelPair::first(); } + + //- The second vertex + label& b() { return labelPair::second(); } + + + //- The first vertex label using labelPair::first; - //- Return last (second) vertex label - using labelPair::last; - - //- Return second (last) vertex label + //- The second (last) vertex label using labelPair::second; + //- The last (second) vertex label + using labelPair::last; - //- Return start (first) vertex label - inline label start() const; + //- The start (first) vertex label + label start() const { return labelPair::first(); } - //- Return start (first) vertex label - inline label& start(); + //- The end (last/second) vertex label + label end() const { return labelPair::second(); } - //- Return end (last/second) vertex label - inline label end() const; + //- The start (first) vertex label + label& start() { return labelPair::first(); } + + //- The end (last/second) vertex label + label& end() { return labelPair::second(); } - //- Return end (last/second) vertex label - inline label& end(); //- Return reverse edge as copy. // No special handling of negative point labels. diff --git a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H index 93fb48307d..31b7a65e0f 100644 --- a/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H +++ b/src/OpenFOAM/meshes/meshShapes/edge/edgeI.H @@ -92,29 +92,6 @@ inline Foam::edge::edge(Istream& is) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline Foam::label Foam::edge::start() const -{ - return first(); -} - -inline Foam::label& Foam::edge::start() -{ - return first(); -} - - -inline Foam::label Foam::edge::end() const -{ - return second(); -} - - -inline Foam::label& Foam::edge::end() -{ - return second(); -} - - inline Foam::label Foam::edge::minVertex() const { return (first() < second() ? first() : second()); diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index 4444dc9587..2a7489e5a0 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -102,16 +102,35 @@ public: // Access - //- Return first vertex label + //- The first vertex + label a() const { return operator[](0); } + + //- The second vertex + label b() const { return operator[](1); } + + //- The third vertex + label c() const { return operator[](2); } + + //- The first vertex + label& a() { return operator[](0); } + + //- The second vertex + label& b() { return operator[](1); } + + //- The third vertex + label& c() { return operator[](2); } + + + //- The first vertex label using FixedList::first; - //- Return last (third) vertex label + //- The last (third) vertex label using FixedList::last; - //- Return second vertex label + //- The second vertex label label& second() { return operator[](1); } - //- Return second vertex label + //- The second vertex label label second() const { return operator[](1); } diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/line.H b/src/OpenFOAM/meshes/primitiveShapes/line/line.H index e4efe0acbc..e00dfcdfca 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/line/line.H +++ b/src/OpenFOAM/meshes/primitiveShapes/line/line.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,6 +43,7 @@ SourceFiles #include "vector.H" #include "PointHit.H" #include "FixedList.H" +#include "Pair.H" #include "UList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -67,6 +68,55 @@ inline Ostream& operator<<(Ostream& os, const line& l); typedef line linePointRef; +/*---------------------------------------------------------------------------*\ + Class linePoints Declaration +\*---------------------------------------------------------------------------*/ + +//- Line point storage. Default constructable (line is not) +class linePoints +: + public Pair +{ +public: + + // Generated Methods + + //- Default construct + linePoints() = default; + + //- Inherit constructors + using Pair::Pair; + + + // Constructors + + //- Construct from point references + inline explicit linePoints(const linePointRef& pts); + + //- Copy construct from subset of points + inline linePoints + ( + const UList& points, + const FixedList& indices + ); + + + // Member Functions + + //- The first vertex + const point& a() const { return Pair::first(); } + + //- The second vertex + const point& b() const { return Pair::second(); } + + //- The first vertex + point& a() { return Pair::first(); } + + //- The second vertex + point& b() { return Pair::second(); } +}; + + /*---------------------------------------------------------------------------*\ Class line Declaration \*---------------------------------------------------------------------------*/ @@ -106,20 +156,26 @@ public: // Access - //- Return first point - inline PointRef first() const noexcept { return a_; } + //- The first point + PointRef a() const noexcept { return a_; } - //- Return second (last) point - inline PointRef second() const noexcept { return b_; } + //- The second point + PointRef b() const noexcept { return b_; } - //- Return last (second) point - inline PointRef last() const noexcept { return b_; } + //- The first point + PointRef first() const noexcept { return a_; } - //- Return start (first) point - inline PointRef start() const noexcept { return a_; } + //- The second (last) point + PointRef second() const noexcept { return b_; } - //- Return end (second) point - inline PointRef end() const noexcept { return b_; } + //- The last (second) point + PointRef last() const noexcept { return b_; } + + //- The start (first) point + PointRef start() const noexcept { return a_; } + + //- The end (second) point + PointRef end() const noexcept { return b_; } // Properties @@ -151,19 +207,10 @@ public: ) const; - // IOstream operators + // IOstream Operators - friend Istream& operator>> - ( - Istream& is, - line& l - ); - - friend Ostream& operator<< - ( - Ostream& os, - const line& l - ); + friend Istream& operator>> (Istream&, line&); + friend Ostream& operator<< (Ostream&, const line&); }; diff --git a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H index 6a25dfa6f3..217d0dee31 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H +++ b/src/OpenFOAM/meshes/primitiveShapes/line/lineI.H @@ -31,6 +31,22 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // +inline Foam::linePoints::linePoints(const linePointRef& pts) +: + Pair(pts.a(), pts.b()) +{} + + +inline Foam::linePoints::linePoints +( + const UList& points, + const FixedList& indices +) +: + Pair(points[indices.first()], points[indices.last()]) +{} + + template inline Foam::line::line(const Point& from, const Point& to) : @@ -46,8 +62,8 @@ inline Foam::line::line const FixedList& indices ) : - a_(points[indices[0]]), - b_(points[indices[1]]) + a_(points[indices.first()]), + b_(points[indices.last()]) {} diff --git a/src/OpenFOAM/primitives/tuples/PairI.H b/src/OpenFOAM/primitives/tuples/PairI.H index 738fdbb4d9..f8b49c84a5 100644 --- a/src/OpenFOAM/primitives/tuples/PairI.H +++ b/src/OpenFOAM/primitives/tuples/PairI.H @@ -131,7 +131,7 @@ inline T& Foam::Pair::second() noexcept template -inline const T& Foam::Pair::other(const T& a) const +inline const T& Foam::Pair::other(const T& val) const { if (first() == second()) { @@ -139,15 +139,15 @@ inline const T& Foam::Pair::other(const T& a) const << "Call to other only valid for Pair with differing elements:" << *this << abort(FatalError); } - else if (a == first()) + else if (val == first()) { return second(); } - else if (a != second()) + else if (val != second()) { FatalErrorInFunction << "Pair " << *this - << " does not contain " << a << abort(FatalError); + << " does not contain " << val << abort(FatalError); } return first(); diff --git a/src/fileFormats/stl/STLtriangle.H b/src/fileFormats/stl/STLtriangle.H index 0072db268a..d723713433 100644 --- a/src/fileFormats/stl/STLtriangle.H +++ b/src/fileFormats/stl/STLtriangle.H @@ -86,9 +86,9 @@ public: inline STLtriangle ( const STLpoint& normal, - const STLpoint& a, - const STLpoint& b, - const STLpoint& c, + const STLpoint& p0, + const STLpoint& p1, + const STLpoint& p2, uint16_t attrib ); @@ -127,18 +127,18 @@ public: ( Ostream& os, const vector& norm, - const point& pt0, - const point& pt1, - const point& pt2 + const point& p0, + const point& p1, + const point& p2 ); //- Write components to Ostream (ASCII), calculating the normal inline static void write ( Ostream& os, - const point& pt0, - const point& pt1, - const point& pt2 + const point& p0, + const point& p1, + const point& p2 ); diff --git a/src/fileFormats/stl/STLtriangleI.H b/src/fileFormats/stl/STLtriangleI.H index 8348c9546d..6ecf80f68e 100644 --- a/src/fileFormats/stl/STLtriangleI.H +++ b/src/fileFormats/stl/STLtriangleI.H @@ -33,16 +33,16 @@ License inline Foam::STLtriangle::STLtriangle ( const STLpoint& normal, - const STLpoint& a, - const STLpoint& b, - const STLpoint& c, + const STLpoint& p0, + const STLpoint& p1, + const STLpoint& p2, uint16_t attrib ) : normal_(normal), - a_(a), - b_(b), - c_(c), + a_(p0), + b_(p1), + c_(p2), attrib_(attrib) {} @@ -88,17 +88,17 @@ inline void Foam::STLtriangle::write ( Ostream& os, const vector& norm, - const point& pt0, - const point& pt1, - const point& pt2 + const point& p0, + const point& p1, + const point& p2 ) { os << " facet normal " << norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl << " outer loop" << nl - << " vertex " << pt0.x() << ' ' << pt0.y() << ' ' << pt0.z() << nl - << " vertex " << pt1.x() << ' ' << pt1.y() << ' ' << pt1.z() << nl - << " vertex " << pt2.x() << ' ' << pt2.y() << ' ' << pt2.z() << nl + << " vertex " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl + << " vertex " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl + << " vertex " << p2.x() << ' ' << p2.y() << ' ' << p2.z() << nl << " endloop" << nl << " endfacet" << nl; } @@ -107,15 +107,15 @@ inline void Foam::STLtriangle::write inline void Foam::STLtriangle::write ( Ostream& os, - const point& pt0, - const point& pt1, - const point& pt2 + const point& p0, + const point& p1, + const point& p2 ) { // Calculate the normal ourselves - const vector norm = triPointRef(pt0, pt1, pt2).unitNormal(); + const vector norm = triPointRef(p0, p1, p2).unitNormal(); - write(os, norm, pt0, pt1, pt2); + write(os, norm, p0, p1, p2); } @@ -123,11 +123,11 @@ inline void Foam::STLtriangle::write inline Foam::Ostream& Foam::operator<<(Ostream& os, const STLtriangle& tri) { - os << tri.normal_ << token::SPACE - << tri.a_ << token::SPACE - << tri.b_ << token::SPACE - << tri.c_ << token::SPACE - << tri.attrib_; + os << tri.normal() << token::SPACE + << tri.a() << token::SPACE + << tri.b() << token::SPACE + << tri.c() << token::SPACE + << tri.attrib(); return os; }