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

Add utility functions for comparison and rounding #1623

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 28 additions & 5 deletions src/opentime/rationalTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ class RationalTime
return fabs(value_rescaled_to(other._rate) - other._value) <= delta;
}

// Return whether the value and rate are equal to another RationalTime.
// This is different from the operator "==" that rescales the time before
// comparison.
constexpr bool exactly_equal(RationalTime other) const noexcept
darbyjohnston marked this conversation as resolved.
Show resolved Hide resolved
{
return _value == other._value && _rate == other._rate;
}

// Return a RationalTime with the largest integer value not greater than
// this value.
RationalTime floor() const
darbyjohnston marked this conversation as resolved.
Show resolved Hide resolved
{
return RationalTime{ std::floor(_value), _rate };
}

// Return a RationalTime with the smallest integer value not less than
// this value.
RationalTime ceil() const
{
return RationalTime{ std::ceil(_value), _rate };
}

// Return a RationalTime with the nearest integer value to this value.
RationalTime round() const
{
return RationalTime{ std::round(_value), _rate };
}

static RationalTime constexpr duration_from_start_end_time(
RationalTime start_time,
RationalTime end_time_exclusive) noexcept
Expand Down Expand Up @@ -263,11 +291,6 @@ class RationalTime
static RationalTime _invalid_time;
static constexpr double _invalid_rate = -1;

RationalTime _floor() const noexcept
{
return RationalTime{ floor(_value), _rate };
}

darbyjohnston marked this conversation as resolved.
Show resolved Hide resolved
friend class TimeTransform;
friend class TimeRange;

Expand Down
2 changes: 1 addition & 1 deletion src/opentime/timeRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TimeRange
if ((et - _start_time.rescaled_to(_duration))._value > 1)
{
return _duration._value != floor(_duration._value)
? et._floor()
? et.floor()
: et - RationalTime(1, _duration._rate);
}
else
Expand Down
22 changes: 22 additions & 0 deletions tests/test_opentime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ main(int argc, char** argv)
assertEqual(t1, t1);
otime::RationalTime t2(30.2);
assertEqual(t1, t2);
otime::RationalTime t3(60.4, 2.0);
assertEqual(t1, t3);
});

tests.add_test("test_inequality", [] {
Expand All @@ -38,6 +40,26 @@ main(int argc, char** argv)
assertFalse(t1 != t3);
});

tests.add_test("test_exact_equality", [] {
otime::RationalTime t1(30.2);
assertTrue(t1.exactly_equal(t1));
otime::RationalTime t2(30.2);
assertTrue(t1.exactly_equal(t2));
otime::RationalTime t3(60.4, 2.0);
assertFalse(t1.exactly_equal(t3));
});

tests.add_test("test_rounding", [] {
otime::RationalTime t1(30.2);
assertEqual(t1.floor(), otime::RationalTime(30.0));
assertEqual(t1.ceil(), otime::RationalTime(31.0));
assertEqual(t1.round(), otime::RationalTime(30.0));
otime::RationalTime t2(30.8);
assertEqual(t2.floor(), otime::RationalTime(30.0));
assertEqual(t2.ceil(), otime::RationalTime(31.0));
assertEqual(t2.round(), otime::RationalTime(31.0));
});

tests.add_test("test_from_time_string", [] {
std::string time_string = "0:12:04";
auto t = otime::RationalTime(24 * (12 * 60 + 4), 24);
Expand Down