如何在stl算法中使用glm的运算符==?

发布于 2024-09-27 00:31:31 字数 263 浏览 0 评论 0原文

是否可以在 stl 算法中使用 glm::gtx::comparison 中定义的运算符?

具体来说,我有这段代码:

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

默认情况下会失败,因为找不到运算符==。

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== can't be found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼中杀气 2024-10-04 00:31:31

虽然晚了几年,但我想分享我的解决方案。
我需要 std::map 和 std::set 的比较器函数。

经过一番修补后,我找到了

#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

在头文件 util_glm.hpp 中包含以下代码的解决方案,并将其包含在需要比较器的位置,

#include "util_glm.hpp"

我确信可以为 glm::ivec3 完成类似的解决方案

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

青春有你 2024-10-04 00:31:31

显然,这是一个未解决的错误

That's an open bug apparently.

把人绕傻吧 2024-10-04 00:31:31

您可以在 virtrev/ 目录中#include equal_operator.hpp。

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

猥琐帝 2024-10-04 00:31:31

为了能够在 std::set 中使用 glm::vec3,我在 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;
    }

不幸的是我不知道如何在不更改glm代码的情况下实现它。

该实现认为 x 轴比 y 轴更相关,并且 y 轴比 z 更相关,关于小于。更改代码以使任何其他轴更相关非常简单。

== 运算符已实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文