# std::set and Point3

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a polygon class where I like to insert points on the fly using an add function. I might happen that my calculations create a point that already exists and therefore should not be inserted again. My idea was to use a std::set since it seems the right tool for in this case. How do I define a comparison for a Point3 class such that equal points are rejected? Ordering is not important so can I pass std::equal_to in the constructor? Maybe I need a "fuzzy" compare later where the points are nearly equal - how would I implement this and what is a good "epsilon" value here? A short code example for illustrations:
class Point3
{
float x, y, z;
}

class Polygon
{
public:
void Add( const Point3& p )
{
m_VtxCoords.insert( p );
// This should fail if "p" was already inserted before!
}

private:
std::set<Point3, Comparison > m_VtxCoords;
}

class comparison
{
// What goes here?
}

Best regards,

Dirk



##### Share on other sites
It depends on what you define as "equal points". It varies from application to application. Depending on how often you add points to the set, you could use a squared distance (no need to bother with the root), where if two points are closer than a certain epsilon, they're considered the same point. That's personally what I'd do, or you could use a rough taxi approximation (which is basically the sum of all the component deltas). As for a good epsilon value, I can't tell you what would be good. 0.001 or 0.0001 could both be winners.

##### Share on other sites
class comparison{public:    bool operator () ( const Point3& a, const Point3& b ) const    {        return a.x != b.x || a.y != b.y || a.z != b.z;    }};