Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test approximation of two vector types (glm::vec3 assumed) #582

Open
alexpanter opened this issue Aug 31, 2023 · 1 comment
Open

Comments

@alexpanter
Copy link

Expected Behavior

I would like to be able to express a comparison between two glm::vec3 types (aka. 3d vectors).
It's a struct with x,y,z members, and has appropriate overloads for operator==, operator-, etc.

Actual Behavior

I keep getting a compiler error:

boost/ut.hpp:1029:55: error: no match for ‘operator<’ (operand types are ‘glm::vec<3, float, glm::packed_highp>’ and ‘float’)
 1029 |             return math::abs_diff(get(lhs), get(rhs)) < get(epsilon);

Steps to Reproduce the Problem

This is the code that I would like to get working, because it would be cool to actually get a print out of the compared vectors when the comparison fails:

// Define what ut needs
std::ostream& operator<<(std::ostream& s, const glm::vec3& v)
{
	return s << '[' << v.x << ',' << v.y << ',' << v.z << ']';
}
bool operator<(const glm::vec3& v1, const glm::vec3& v2)
{
	return (v1.x < v2.x) && (v1.y < v2.y) && (v1.z < v2.z);
}
constexpr bool operator<(glm::vec3 v, float f)
{
	return (v.x < f) && (v.y < f) && (v.z < f);
}
#include <boost/ut.hpp>

using namespace boost::ut;

As you can see, I have defined the appropriate operator which should satisfy glm::vec3 < float and glm::vec3 < glm::vec3.
But this test cannot compile, sadly:

void test_camera_default_lookat()
{
	Camera* cam = camera_create();
	glm::vec3 pos   {0.0f, 0.0f, 0.0f};
	glm::vec3 front {1.0f, 0.0f, 0.0f};
	glm::vec3 up    {0.0f, 0.0f, 1.0f};
	camera_set_lookat(cam, pos, front, up);

	"default_lookat"_test = [&pos, &front, &up, &cam]{
		"position"_test = [&pos, &cam]{
			glm::vec3 cpos   = camera_get_position(cam);
			expect(approx(pos, cpos, glm::epsilon<float>()));
		};
	};
}

Specifications

  • Version: BOOST_UT_VERSION 1'1'9
  • Platform: Ubuntu 23.04
  • Compiler: g++ 12.3.0
@heilkn
Copy link

heilkn commented Oct 23, 2023

Hi, if this is still relevant to you, then review the error message. The problem is not the missing operator, but the fact that it tries to compare a vector and a scalar. Remember your Calculus lessons: The abs function is supposed to return a float.

Kind regards

Konstantin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants