Jump to content
  • Advertisement
Sign in to follow this  
Dirk Gregorius

std::set and Point3

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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;
}
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!