diff --git a/include/light/ltface.hh b/include/light/ltface.hh index 86b82b04..7c7320d3 100644 --- a/include/light/ltface.hh +++ b/include/light/ltface.hh @@ -67,6 +67,10 @@ struct lightgrid_sample_t qvec3b round_to_int() const; float brightness() const; + /** + * - if !used, style and color are ignored for equality + * - if a color component is nan, nan is considered equal to nan for the purposes of this comparison + */ bool operator==(const lightgrid_sample_t &other) const; }; diff --git a/light/ltface.cc b/light/ltface.cc index 7d9a108e..0b3591be 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -3555,7 +3555,29 @@ float lightgrid_sample_t::brightness() const bool lightgrid_sample_t::operator==(const lightgrid_sample_t &other) const { - return used == other.used && style == other.style && color == other.color; + if (used != other.used) + return false; + + if (!used) { + // if unused, style and color don't matter + return true; + } + + if (style != other.style) + return false; + + // color check requires special handling for nan + for (int i=0; i<3; ++i) { + if (std::isnan(color[i])) { + if (!std::isnan(other.color[i])) + return false; + } else { + if (color[i] != other.color[i]) + return false; + } + } + + return true; } int lightgrid_samples_t::used_styles() const diff --git a/tests/test_ltface.cc b/tests/test_ltface.cc index 78011016..1f346aed 100644 --- a/tests/test_ltface.cc +++ b/tests/test_ltface.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -103,6 +104,48 @@ testresults_t QbspVisLight_Q2( return QbspVisLight_Common(name, {"-q2bsp"}, extra_light_args, run_vis); } +TEST_CASE("lightgrid_sample_t equality") +{ + SUBCASE("style equality") { + lightgrid_sample_t a {.used = true, .style = 4, .color = {}}; + lightgrid_sample_t b = a; + CHECK(a == b); + + b.style = 6; + CHECK(a != b); + } + + SUBCASE("color equality") { + lightgrid_sample_t a {.used = true, .style = 4, .color = {1,2,3}}; + lightgrid_sample_t b = a; + CHECK(a == b); + + b.color = {6,5,4}; + CHECK(a != b); + } + + SUBCASE("nan colors") { + lightgrid_sample_t a {.used = true, .style = 4, .color = {std::numeric_limits::quiet_NaN(), 1.0, 1.0}}; + lightgrid_sample_t b = a; + CHECK(a == b); + + b.color = { 0,0,0}; + CHECK(a != b); + } + + SUBCASE("unused equality doesn't consider other attributes") { + lightgrid_sample_t a, b; + CHECK(!a.used); + CHECK(a == b); + + b.style = 5; + CHECK(a == b); + + b.color = {1, 0, 0}; + CHECK(a == b); + } +} + TEST_CASE("-world_units_per_luxel, -lightgrid") { auto [bsp, bspx] = QbspVisLight_Q2("q2_lightmap_custom_scale.map", {"-lightgrid"});