Using a settable tolerance in the comparison object for an STL set
I want a tool to remove duplicate nodes ("nodes" in the finite element sense, simply a point in space with certain coordinates). Basically, I want to be able to take a collection of nodes, and reduce it by eliminating extra nodes that are within a certain tolerance in distance of other nodes. I figure that an STL set of nodes sorted by coordinate will fit the bill nicely performance wise. My question is how to incorporate a tolerance that is settable at run time.
Consider the simple key comparison function for the 1D case (a single coordinate per node). I know my logic could be shortened; that isn't what my question is about.
struct NodeSorter_x{
//Stores Node objects sorted by x coordinate within tolerance TOL
bool operator()(const Node N1, const Node N2) const
{
//returns true if N1 < N2 and not within TOL distance
return ( N1.x < N2.x) && !( fabs( N1.x - N2.x ) < TOL );
}
};
And the set containing unique node objects (no duplicates)...
std::set <Node,NodeSorter_x> UniqueNodeSet;
So开发者_JAVA技巧, I want to be able to set TOL used in the comparison at run time. How do I go about doing so?
struct NodeSorter_x{
NodeSorter_x(double tol) : TOL(tol) {}
bool operator()(const Node N1, const Node N2) const
{
//returns true if N1 < N2 and not within TOL distance
return ( N1.x < N2.x) && !( fabs( N1.x - N2.x ) < TOL );
}
};
std::set <Node,NodeSorter_x> UniqueNodeSet( NodeSorter_x(0.1) );
精彩评论