mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 09:44:59 +01:00
fix: inverted clamp bounds give the upper bound again, as the old clamp did (#412)
* fix: inverted clamp bounds give the upper bound again, as the old clamp did The project's own clamp() of 26.05 applied the lower bound first and the upper one last, so bounds given the wrong way round always gave the upper one. Its calls became std::clamp (undefined for such bounds) and, where the bounds can cross, safe_clamp, which swapped them instead. Neither gives the old result. - safe_clamp is the old clamp again, min(max(value, lo), hi). This restores 26.05 at every place it is used, among them the AI's dynamic brake test (its demand was fAccThreshold x1.21, the swap made it x1.01), the generator voltage, the heavy cargo acceleration of the AI and the EP correction of the brake. - The cab camera is kept inside the cab with safe_clamp. The bounds come from the .mmd as written and some cabs have them reversed (the B cars of SA134 and 21xM, ET40, 754); with std::clamp the camera jumped between the two bounds every frame on MSVC, as its result is fed back. * fix: every call of the old clamp gives what it gave up to 26.05.1d1f16411(#99) made every call of the project's clamp() std::clamp. Where the bounds come from the data or are computed at runtime they can cross, and then std::clamp gives another result (undefined behaviour; MSVC gives the lower bound unless the value is above the upper one) where the old clamp gave the upper bound. These calls use safe_clamp, the old clamp, too: - Mover: the pantograph pressure (MaxPress), the compressor switch (CompressorListPosNo), the coupler distance (DmaxB, DmaxC), the engine revolutions, the eimic ramp (CtrlDelay), the eimic limit (eimic_max), the speed control ramp (PowerUpSpeed, PowerDownSpeed), the load (MaxLoad), the magnet of the security system (Dim.L); - DynObj: the coupler distance of the buffers (DmaxB), the axle distance (Dim.L); - Train: the brake handle positions (BrakeCtrlPosNo, the range of the handle); - the legacy renderer: the mouse pick (the window size). With the bounds in order nothing changes. Calls whose bounds the code keeps in order (constants, SplineFidelity 1-4, the season table, the guarded output calibration) give the same result with std::clamp and stay as they are. The load offset takes its arguments in the 26.05.1 order again;fa2ed089swapped them, which with the old clamp differs only for a negative load. The bump at a rail joint gives the old result again. Up to 26.05.1 its clamps had 0 in place of the value (since96f43e11and4d72d352), and the old clamp made that the headroom Vmax - (Vel + 0.32 Vmax), negative above 68% of the top speed, capped from above only;d464628aand03bfbb13kept it at 0 or above. Only damaged track has the bump: a damage flag in the .scn, a derailer set by an event, a track made on a derailment. The interior light of the cab in the 3.3 renderer is computed in double again, as befored1f16411. The call added since (stepped_uc_value,00955513) keeps what master does: bounds given the other way round are put in order. * fix: the alpha of a particle in the legacy renderer as up to 26.05.1, the NaN cases of the clamp tested Up to 26.05.1 the alpha was clamp<std::uint8_t>( particle.opacity * 255, 0, 255 ): the float was made a byte first, so the clamp did nothing and an opacity over 1 (a smoke max_opacity over 1) wrapped round, 1.2 giving 50. The move to std::clamp made it 255. It's made a byte as then again, through int: the byte the x86-64 compilers gave, without the undefined behaviour of that conversion (MSVC makes the same two instructions of both). openglparticles.cpp goes with feat/gl46-migration; its opengl33 counterpart, std::clamp( opacity, 0, 1 ), already gives what 26.05.1 gave. The tests pin what the old clamp gave with NaN: a NaN value stays NaN, a NaN bound doesn't clamp. The checks named after the AI braking test and the bump at a rail joint say they test the helper with the arguments of those calls, not the calls. * test: the includes of the clamp tests before the project's headers <cmath> and <limits> go up, after the test headers, so the change no longer touches the lines #416 adds its includes after. With both merged <cmath> is included twice, which is harmless. --------- Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
#include "doctest.h"
|
||||
#include "scratch.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/utilities.h"
|
||||
|
||||
@@ -681,12 +684,30 @@ TEST_CASE("utilities: the sign of a number, and a number rounded to a whole one"
|
||||
CHECK(Round(-2.5) == -2);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: a value is kept within bounds, given in any order") {
|
||||
TEST_CASE("utilities: a value is kept within bounds; inverted bounds give the upper one, as the old clamp did") {
|
||||
CHECK(safe_clamp(5, 0, 3) == 3);
|
||||
CHECK(safe_clamp(5, 3, 0) == 3);
|
||||
CHECK(safe_clamp(-1, 3, 0) == 0);
|
||||
CHECK(safe_clamp(2.0, 3.0, 1.0) == 2.0);
|
||||
CHECK(safe_clamp(-1, 0, 3) == 0);
|
||||
CHECK(safe_clamp(2, 0, 3) == 2);
|
||||
CHECK(safe_clamp(2.0, 2.0, 2.0) == 2.0);
|
||||
// inverted bounds: always the upper bound, whatever the value
|
||||
CHECK(safe_clamp(5, 3, 0) == 0);
|
||||
CHECK(safe_clamp(-1, 3, 0) == 0);
|
||||
CHECK(safe_clamp(2.0, 3.0, 1.0) == 1.0);
|
||||
// NaN as the old clamp had it: a NaN value stays NaN, a NaN bound doesn't clamp
|
||||
auto const notanumber{std::numeric_limits<double>::quiet_NaN()};
|
||||
CHECK(std::isnan(safe_clamp(notanumber, 0.0, 1.0)));
|
||||
CHECK(safe_clamp(2.0, 0.0, notanumber) == 2.0);
|
||||
CHECK(safe_clamp(-1.0, notanumber, 1.0) == -1.0);
|
||||
// what the helper gives for arguments in the order and of the signs of two calls (these checks don't call
|
||||
// them): the AI braking test, whose demand is the stronger of the two (negative) bounds, as up to 26.05.1
|
||||
double const threshold{-0.45};
|
||||
CHECK(safe_clamp(0.3, threshold * 1.01, threshold * 1.21) == doctest::Approx(threshold * 1.21));
|
||||
CHECK(safe_clamp(-0.6, threshold * 1.01, threshold * 1.21) == doctest::Approx(threshold * 1.21));
|
||||
// and the bump at a rail joint, 0 in place of the value as up to 26.05.1: the figure is capped from above
|
||||
// only, and above 68% of the top speed it is negative
|
||||
CHECK(safe_clamp(0.0, 160.0, 160.0 - (150.0 + 160.0 * 0.32)) == doctest::Approx(-41.2));
|
||||
CHECK(safe_clamp(0.0, 4.0, -0.5) == -0.5);
|
||||
CHECK(safe_clamp(0.0, 4.0, 5.0) == 4.0);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: a value is wrapped into a range, as an angle into the full circle") {
|
||||
|
||||
Reference in New Issue
Block a user