开发者

how to use glm's operator== in stl algorithms?

开发者 https://www.devze.com 2023-01-19 11:15 出处:网络
Is it possible to use the operators defined in glm::gtx::comparison in stl algorithms? Specifically i have this code:

Is it possible to use the operators defined in glm::gtx::comparison in stl algorithms?

Specifically i have this code:

std::vector<glm::ivec3> vecA, vecB;    // vectors with content
bool result = std::equal(vecA.begin(), vecA.end(), vecB.begin());

This by default fails cause operator== ca开发者_如何学JAVAn't be found.


Only a few years late, but I wanted to share my fix. I needed a comparator function for std::map and std::set.

After a bit of tinkering, I found the solution to have the following code

#ifndef __UTIL_GLM__
#define __UTIL_GLM__
#include "glm/vec2.hpp"
namespace glm{
  template <typename T, precision P>
  bool operator<(const tvec2<T, P>& a,const tvec2<T, P>& b)
  {
    return (a.x < b.x || (a.x == b.x && a.y < b.y));
  }
};
#endif

in a header file util_glm.hpp and include it where ever the comparator was needed with

#include "util_glm.hpp"

I am sure a similar solution can be done for glm::ivec3


That's an open bug apparently.


You can #include equal_operator.hpp, in the virtrev/ directory.


To be able to use glm::vec3 in a std::set<>, I implemented the following overload in the type_vec3.inl:

template <typename T> 
    GLM_FUNC_QUALIFIER bool operator<
    (
        tvec3<T> const & v1, 
        tvec3<T> const & v2
    )
    {
        if(v1.x == v2.x && v1.y == v2.y && v1.z < v2.z) return true;
        if(v1.x == v2.x && v1.y < v2.y) return true;
        if(v1.x < v2.x) return true;
        return false;
    }

Unfortunately I don't know how to implement it without change the glm code.

This implementation consider x axis more relevant than y axis and y axis more relevant then z concerning smaller than. It's very simple to change the code to make any other axis more relevant.

The == operator is already implemented.

0

精彩评论

暂无评论...
验证码 换一张
取 消