ut icon indicating copy to clipboard operation
ut copied to clipboard

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

Open alexpanter opened this issue 2 years ago • 1 comments

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

alexpanter avatar Aug 31 '23 09:08 alexpanter

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

heilkn avatar Oct 23 '23 21:10 heilkn