简体   繁体   中英

The best way to find intersection two std::vectors of 3D points

I was wondering if there is a way, using standard library, to find an intersection of two vectors of 3D points. 3D point is a glm::vec3 with x, y and z. x, y and z are floats.

I know that we can use a std::set_intersection on 1D arrays.

Just to be clear I have 2 vectors:

std::vector<Point> v1;
std::vector<Point> v2;

where Point is:

struct Point {
    glm::vec3 m_position;
    glm::vec2 m_texCoord;
    glm::vec3 m_normal;

    Point() {}

    Point(glm::vec3& pos, glm::vec2& tex, glm::vec3& norm) {
        m_position = pos;
        m_normal = norm;
        m_texCoord = tex;
    }

    Point(glm::vec3& pos, glm::vec3& norm) {
        m_position = pos;
        m_normal = norm;
    }

    Point(glm::vec3& pos) {
        m_position = pos;
    }
};

I would like to find an intersection of v1 and v2 by Point.m_position.

Thank you for your help.

In the documentation of std::set_intersection() it's mentioned that

1) Elements are compared using operator< and the ranges must be sorted with respect to the same.

So basically you need to provide an overloaded operator<() for Point , and sort those vectors before calling std::set_intersection() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM