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

Make pinhole.hpp robust against min/max preprocessor macros (typically from windows.h) #5432

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion rerun_cpp/src/rerun/archetypes/pinhole.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rerun_cpp/src/rerun/archetypes/pinhole_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ namespace rerun {
/// Assumes the principal point to be in the middle of the sensor.
static Pinhole from_fov_and_aspect_ratio(float fov_y, float aspect_ratio) {
const float EPSILON = std::numeric_limits<float>::epsilon();
const float focal_length_y = 0.5f / std::tan(std::max(fov_y * 0.5f, EPSILON));
// `max` has explicit template args to avoid preprocessor replacement when <windows.h> is included without NOMINMAX.
const float focal_length_y = 0.5f / std::tan(std::max<float>(fov_y * 0.5f, EPSILON));
return from_focal_length_and_resolution(
{focal_length_y, focal_length_y},
{aspect_ratio, 1.0}
Expand Down
10 changes: 10 additions & 0 deletions rerun_cpp/tests/windows_min_max_robustness.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

// `<windows.h>` by default defined min and max as macros, which can cause issues with std::min and std::max.
// This can be easily turned off by the user with `#define NOMINMAX` before including `<windows.h>`.
// However, users might not know about this or forget about it and we want to be robust against it.
//
// This test checks that rerun.h is robust against this issue by explicitly setting `min`/`max` macros and including rerun.hpp.
#define min(a, b) (this does not compile)
#define max(a, b) (this does not compile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


#include <rerun.hpp>