ENH: more noexcept methods for edge, avoid some intermediates

STYLE: document edge parameters as 'vertex' (not point)

- edge is also used for graph edges or connection edges etc.
This commit is contained in:
Mark Olesen
2023-02-27 13:01:08 +01:00
parent 0fc2c817e1
commit 00f499d714
8 changed files with 125 additions and 117 deletions

View File

@ -28,13 +28,13 @@ Class
Foam::edge
Description
An edge is a list of two point labels. The functionality it provides
supports the discretisation on a 2-D flat mesh.
An edge is a list of two vertex labels.
This can correspond to a directed graph edge or an edge on a mesh.
The edge is implemented as a Pair/FixedList of labels.
As well as geometrically relevant methods, it also provides methods
similar to HashSet for additional convenience.
Valid point labels are always non-negative (since they correspond to
Valid vertex labels are always non-negative (eg, since they correspond to
addressing within the mesh). The value '-1' is used to tag invalid
point labels that correspond conceptually to open 'slots', which
can be filled with a HashSet-like functionality.
@ -69,30 +69,31 @@ public:
// Static Data Members
//- The typeName ("edge")
static const char* const typeName;
// Constructors
//- Default construct, with invalid point labels (-1)
//- Default construct, with invalid vertex labels (-1)
inline edge();
//- Construct from two point labels
//- Construct from two vertex labels
inline edge(const label from, const label to);
//- Construct from pair of point labels
//- Construct from pair of vertex labels
inline edge(const labelPair& pair);
//- Construct from list of point labels
//- Construct from list of vertex labels
inline edge(const FixedList<label, 2>& list);
//- Construct from two point labels, sorted with first less-than second
//- Construct from two vertex labels, sorted with first less-than second
inline edge(const label from, const label to, const bool doSort);
//- Construct from list, sorted with first less-than second
inline edge(const FixedList<label, 2>& list, const bool doSort);
//- Copy construct from a subset of point labels
//- Copy construct from a subset of vertex labels
inline edge
(
const UList<label>& list,
@ -133,53 +134,50 @@ public:
//- Return reverse edge as copy.
// No special handling of negative point labels.
// No special handling of negative vertex labels.
inline edge reverseEdge() const;
// Queries
//- Return the smallest point label used by the edge
// No special handling of negative point labels.
inline label minVertex() const;
//- Return the smallest vertex label used by the edge
// No special handling of negative vertex labels.
inline label minVertex() const noexcept;
//- Return the largest point label used by the edge
// No special handling of negative point labels.
inline label maxVertex() const;
//- Return the largest vertex label used by the edge
// No special handling of negative vertex labels.
inline label maxVertex() const noexcept;
//- True if the vertices are unique and non-negative.
inline bool good() const noexcept;
//- True if the vertices are unique and non-negative.
bool valid() const noexcept { return good(); }
//- Return true if the vertex label is contained in the edge.
// Always false for a negative vertex label.
inline bool contains(const label vertex) const noexcept;
//- Return true if the point label is contained in the edge.
// Always false for a negative label.
inline bool contains(const label pointLabel) const;
//- Return local index (0,1) of point label in edge -1 on failure
// Always return -1 for a negative label.
inline label which(const label pointLabel) const;
//- Return local index (0,1) of vertex label in edge -1 on failure
// Always return -1 for a negative vertex label.
inline label which(const label vertex) const;
//- True if the edge has at least one vertex in common with other
inline bool connected(const edge& other) const;
//- Return vertex common with other edge or -1 on failure
// Negative point labels are never considered common between edges.
// Negative vertex labels are never considered common between edges.
inline label commonVertex(const edge& other) const;
//- Given the point label for one vertex, return the other one.
// No special treatment for negative point labels.
inline label otherVertex(const label pointLabel) const;
//- Given one vertex label, return the other one.
// No special treatment for negative vertex labels.
inline label otherVertex(const label vertex) const;
//- Do the edges share a common vertex index?
// Negative point labels never connect.
// Negative vertex labels never connect.
bool connects(const edge& other) const { return connected(other); }
// Editing
//- 'Collapse' edge by marking duplicate point labels as '-1',
//- 'Collapse' edge by marking duplicate vertex labels as '-1',
//- the lower vertex is retained.
// Return the effective size after collapsing.
inline label collapse();
@ -187,21 +185,22 @@ public:
// Hash-like Functions
//- Return the number of unique, valid (non -1) point labels.
//- Return the number of unique, valid (non -1) vertex labels.
// Similar to a HashTable::size().
inline label count() const;
//- Return true if edge has no valid point labels.
inline bool empty() const;
//- Return true if edge has no valid vertex labels.
inline bool empty() const noexcept;
//- 'Clears' edge by setting both ends to invalid point labels.
//- 'Clears' edge by setting both ends to invalid vertex labels.
inline void clear();
//- Fill any open slot with the index if it did not previously exist.
// Returns true on success. A negative label never inserts.
//- Fill any open slot with the vertex label
//- (if not previously contained in the edge).
// Returns true on success. A negative vertex label never inserts.
// Similar to a HashTable::insert().
inline bool insert(const label index);
inline bool insert(const label vertex);
//- Insert values, using begin/end iterators.
template<class InputIterator>
@ -227,10 +226,11 @@ public:
inline label insert(const labelUList& list);
//- Remove an existing index from the edge and set its location to '-1'.
// Returns the number of changes. A negative label never removes.
//- Remove an existing vertex from the edge and set its location
//- to '-1'. A negative vertex label never removes.
// Returns the number of changes.
// Similar to a HashTable::erase().
inline label erase(const label index);
inline label erase(const label vertex);
//- Remove values, using begin/end iterators.
template<class InputIterator>
@ -253,26 +253,30 @@ public:
// Geometric Functions
//- Return centre point (centroid) of the edge.
// No special handling of negative point labels.
// No special handling of negative vertex labels.
inline point centre(const UList<point>& pts) const;
//- Return the vector (end - start)
// No special handling of negative point labels.
//- Return the vector (from first to second).
// No special handling of negative vertex labels.
inline vector vec(const UList<point>& pts) const;
//- Return the unit vector (end - start)
// No special handling of negative point labels.
//- Return the unit vector (from first to second).
// No special handling of negative vertex labels.
inline vector unitVec(const UList<point>& pts) const;
//- Return scalar magnitude of the edge.
// No special handling of negative point labels.
//- The length (L2-norm) of the edge vector.
// No special handling of negative vertex labels.
inline scalar mag(const UList<point>& pts) const;
//- The length (L2-norm) squared of the edge vector.
// No special handling of negative vertex labels.
inline scalar magSqr(const UList<point>& pts) const;
//- The enclosing (bounding) box for the edge
inline Pair<point> box(const UList<point>& pts) const;
//- Return edge line
// No special handling of negative point labels.
// No special handling of negative vertex labels.
inline linePointRef line(const UList<point>& pts) const;
@ -327,6 +331,9 @@ public:
//- Same as contains()
bool found(label vertex) const { return contains(vertex); }
//- Same as good()
bool valid() const noexcept { return good(); }
//- Deprecated(2021-04) hashing functor. Use hasher()
// \deprecated(2021-04) - use hasher() functor
template<class Unused=bool>

View File

@ -92,15 +92,15 @@ inline Foam::edge::edge(Istream& is)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline Foam::label Foam::edge::minVertex() const
inline Foam::label Foam::edge::minVertex() const noexcept
{
return (first() < second() ? first() : second());
}
inline Foam::label Foam::edge::maxVertex() const
inline Foam::label Foam::edge::maxVertex() const noexcept
{
return (first() > second() ? first() : second());
return (second() < first() ? first() : second());
}
@ -110,27 +110,27 @@ inline bool Foam::edge::good() const noexcept
}
inline bool Foam::edge::contains(const label pointLabel) const
inline bool Foam::edge::contains(const label vertex) const noexcept
{
// -1: always false
return
(
pointLabel >= 0
&& (pointLabel == first() || pointLabel == second())
vertex >= 0
&& (vertex == first() || vertex == second())
);
}
inline Foam::label Foam::edge::which(const label pointLabel) const
inline Foam::label Foam::edge::which(const label vertex) const
{
// -1: always false
if (pointLabel >= 0)
if (vertex >= 0)
{
if (pointLabel == first())
if (vertex == first())
{
return 0;
}
if (pointLabel == second())
if (vertex == second())
{
return 1;
}
@ -162,13 +162,13 @@ inline Foam::label Foam::edge::commonVertex(const edge& other) const
}
inline Foam::label Foam::edge::otherVertex(const label pointLabel) const
inline Foam::label Foam::edge::otherVertex(const label vertex) const
{
if (pointLabel == first())
if (vertex == first())
{
return second();
}
if (pointLabel == second())
if (vertex == second())
{
return first();
}
@ -228,35 +228,35 @@ inline Foam::label Foam::edge::count() const
}
inline bool Foam::edge::empty() const
inline bool Foam::edge::empty() const noexcept
{
return (first() < 0 && second() < 0);
}
inline bool Foam::edge::insert(const label index)
inline bool Foam::edge::insert(const label vertex)
{
if (index < 0)
if (vertex < 0)
{
// Cannot insert invalid point labels (use direct assignment for that)
// Cannot insert invalid vertex labels (use direct assignment for that)
return false;
}
if (first() < 0)
{
// Store at first, if not duplicate of second
if (index != second())
if (vertex != second())
{
first() = index;
first() = vertex;
return true;
}
}
else if (second() < 0)
{
// Store at second, if not duplicate of first
if (index != first())
if (vertex != first())
{
second() = index;
second() = vertex;
return true;
}
}
@ -308,23 +308,23 @@ inline Foam::label Foam::edge::insert(const labelUList& list)
}
inline Foam::label Foam::edge::erase(const label index)
inline Foam::label Foam::edge::erase(const label vertex)
{
if (index < 0)
if (vertex < 0)
{
// Can never remove invalid point labels!
return 0;
}
label n = 0;
if (index == first())
if (vertex == first())
{
first() = -1;
++n;
}
// Automatically handle duplicates, which should not have been there anyhow
if (index == second())
if (vertex == second())
{
second() = -1;
++n;
@ -426,7 +426,13 @@ inline Foam::vector Foam::edge::unitVec(const UList<point>& pts) const
inline Foam::scalar Foam::edge::mag(const UList<point>& pts) const
{
return ::Foam::mag(vec(pts));
return pts[first()].dist(pts[second()]);
}
inline Foam::scalar Foam::edge::magSqr(const UList<point>& pts) const
{
return pts[first()].distSqr(pts[second()]);
}

View File

@ -776,8 +776,8 @@ Foam::edgeList Foam::face::edges() const
edgeList theEdges(nVerts);
// Last edge closes the polygon
theEdges.last().first() = verts.last();
theEdges.last().second() = verts[0];
theEdges.back().first() = verts.back();
theEdges.back().second() = verts.front();
for (label verti = 0; verti < nVerts - 1; ++verti)
{
@ -797,8 +797,8 @@ Foam::edgeList Foam::face::rcEdges() const
edgeList theEdges(nVerts);
// First edge closes the polygon
theEdges.first().first() = verts[0];
theEdges.first().second() = verts.last();
theEdges.front().first() = verts.front();
theEdges.front().second() = verts.back();
for (label verti = 1; verti < nVerts; ++verti)
{
@ -878,12 +878,12 @@ Foam::label Foam::face::longestEdge(const UList<point>& pts) const
// Last edge closes the polygon. Use it to initialize loop
label longest = nVerts - 1;
scalar longestLen = Foam::edge(verts.first(), verts.last()).mag(pts);
scalar longestLen = pts[verts.front()].distSqr(pts[verts.back()]);
// Examine other edges
for (label edgei = 0; edgei < nVerts - 1; ++edgei)
{
scalar edgeLen = Foam::edge(verts[edgei], verts[edgei + 1]).mag(pts);
scalar edgeLen = pts[verts[edgei]].distSqr(pts[verts[edgei+1]]);
if (longestLen < edgeLen)
{

View File

@ -136,26 +136,26 @@ public:
//- Default construct
constexpr face() noexcept = default;
//- Construct given size, with invalid point labels (-1)
//- Construct given size, with invalid vertex labels (-1)
inline explicit face(const label sz);
//- Copy construct from list of point labels
//- Copy construct from list of vertex labels
inline explicit face(const labelUList& list);
//- Move construct from list of point labels
//- Move construct from list of vertex labels
inline explicit face(labelList&& list);
//- Copy construct from an initializer list of point labels
//- Copy construct from an initializer list of vertex labels
inline explicit face(std::initializer_list<label> list);
//- Copy construct from list of point labels
//- Copy construct from list of vertex labels
template<unsigned N>
inline explicit face(const FixedList<label, N>& list);
//- Copy construct from subset of point labels
//- Copy construct from subset of vertex labels
inline face(const labelUList& list, const labelUList& indices);
//- Copy construct from subset of point labels
//- Copy construct from subset of vertex labels
template<unsigned N>
inline face(const labelUList& list, const FixedList<label, N>& indices);
@ -168,12 +168,12 @@ public:
// Member Functions
//- Collapse face by removing duplicate point labels
//- Collapse face by removing duplicate vertex labels
// \return the collapsed size
label collapse();
//- Flip the face in-place.
// The starting points of the original and flipped face are identical.
// The starting vertex of the original and flipped face are identical.
void flip();
//- Return the points corresponding to this face
@ -211,15 +211,15 @@ public:
inline Pair<point> box(const UList<point>& pts) const;
//- Return face with reverse direction
// The starting points of the original and reverse face are identical.
// The starting vertex of the original and reverse face are identical.
face reverseFace() const;
// Navigation through face vertices
//- Find local index on face for the point label, same as find()
//- Find local vertex on face for the vertex label, same as find()
// \return position in face (0,1,2,...) or -1 if not found.
inline label which(const label pointLabel) const;
inline label which(const label vertex) const;
//- The vertex on face - identical to operator[], but with naming
//- similar to nextLabel(), prevLabel()

View File

@ -175,9 +175,9 @@ inline Foam::vector Foam::face::rcEdge
}
inline Foam::label Foam::face::which(const label pointLabel) const
inline Foam::label Foam::face::which(const label vertex) const
{
return labelList::find(pointLabel);
return labelList::find(vertex);
}

View File

@ -107,12 +107,7 @@ Foam::pointHit Foam::face::ray
// Miss distance is the distance between the plane intersection
// point and the nearest point of the triangle
scalar missDist =
Foam::mag
(
p + curHit.distance()*n
- curHit.point()
);
scalar missDist = curHit.point().dist(p + curHit.distance()*n);
if (missDist < nearestMissDist)
{

View File

@ -84,19 +84,19 @@ public:
// Constructors
//- Default construct, with invalid point labels (-1)
//- Default construct, with invalid vertex labels (-1)
inline triFace();
//- Construct from three point labels
//- Construct from three vertex labels
inline triFace(const label p0, const label p1, const label p2) noexcept;
//- Construct from an initializer list of three point labels
//- Construct from an initializer list of three vertex labels
inline explicit triFace(std::initializer_list<label> list);
//- Copy construct from a list of three point labels.
//- Copy construct from a list of three vertex labels.
inline explicit triFace(const labelUList& list);
//- Copy construct from a subset of point labels
//- Copy construct from a subset of vertex labels
inline triFace
(
const labelUList& list,
@ -135,20 +135,17 @@ public:
//- True if vertices are unique and non-negative.
inline bool good() const noexcept;
//- True if vertices are unique and non-negative.
bool valid() const noexcept { return good(); }
// Other
//- 'Collapse' face by marking duplicate point labels.
// Duplicates point labels are marked with '-1'
//- 'Collapse' face by marking duplicate vertex labels.
// Duplicates vertex labels are marked with '-1'
// (the lower vertex is retained).
// Return the collapsed size.
inline label collapse();
//- Flip the face in-place.
// The starting points of the original and flipped face are identical.
// The starting vertex of the original and flipped face are identical.
inline void flip();
//- Return the points corresponding to this face
@ -191,12 +188,12 @@ public:
inline label nTriangles() const noexcept;
//- Return face with reverse direction
// The starting points of the original and reverse face are identical.
// The starting vertex of the original and reverse face are identical.
inline triFace reverseFace() const;
//- Find local index on face for the point label, same as find()
//- Find local vertex on face for the vertex label, same as find()
// \return position in face (0,1,2) or -1 if not found.
inline label which(const label pointLabel) const;
inline label which(const label vertex) const;
//- Next vertex on face
inline label nextLabel(const label i) const;
@ -378,6 +375,9 @@ public:
// Housekeeping
//- Same as good()
bool valid() const noexcept { return good(); }
//- Identical to edge()
Foam::edge faceEdge(label edgei) const { return this->edge(edgei); }
};

View File

@ -221,9 +221,9 @@ inline Foam::triFace Foam::triFace::reverseFace() const
}
inline Foam::label Foam::triFace::which(const label pointLabel) const
inline Foam::label Foam::triFace::which(const label vertex) const
{
return FixedList<label, 3>::find(pointLabel);
return FixedList<label, 3>::find(vertex);
}