Skip to content

Commit

Permalink
Fix default value was not considered in Attribute::get_value when
Browse files Browse the repository at this point in the history
timecode != Defaut.
  • Loading branch information
syoyo committed May 7, 2024
1 parent 7615104 commit 1202c36
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/prim-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2477,9 +2477,9 @@ class Attribute {
}

template <typename T>
bool get_value(const double t, T *dst,
value::TimeSampleInterpolationType tinterp =
value::TimeSampleInterpolationType::Linear) const {
bool get(const double t, T *dst,
value::TimeSampleInterpolationType tinterp =
value::TimeSampleInterpolationType::Linear) const {
if (!dst) {
return false;
}
Expand All @@ -2498,9 +2498,19 @@ class Attribute {
return _var.get_interpolated_value(t, tinterp, dst);
}

return false;
// try to get 'defaut' value
return get_value(dst);
}

// TODO: Deprecate 'get_value' API
template <typename T>
bool get_value(const double t, T *dst,
value::TimeSampleInterpolationType tinterp =
value::TimeSampleInterpolationType::Linear) const {
return get(t, dst, tinterp);
}


const AttrMeta &metas() const { return _metas; }
AttrMeta &metas() { return _metas; }

Expand Down
37 changes: 37 additions & 0 deletions tests/unit/unit-timesamples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,42 @@ void timesamples_test(void) {
}
}

{
primvar::PrimVar pbar;
value::TimeSamples ts;
ts.add_sample(0, value::Value(0.0f));
ts.add_sample(1, value::Value(10.0f));
pbar.set_timesamples(ts);
pbar.set_value(2000.0f); // default value

Attribute attr;
attr.set_var(pbar);

{
float f;
TEST_CHECK(attr.get(value::TimeCode::Default(), &f, value::TimeSampleInterpolationType::Held));
// return the value of the first item(= timecode 0)
TEST_CHECK(math::is_close(f, 2000.0f));
}

// Linear interpolation
{
float f;
TEST_CHECK(attr.get(-10.0, &f, value::TimeSampleInterpolationType::Linear));
TEST_CHECK(math::is_close(f, 0.0f));

TEST_CHECK(attr.get(0.0, &f, value::TimeSampleInterpolationType::Linear));
TEST_CHECK(math::is_close(f, 0.0f));

TEST_CHECK(attr.get(0.5, &f, value::TimeSampleInterpolationType::Linear));
TEST_CHECK(math::is_close(f, 5.0f));

TEST_CHECK(attr.get(1.0, &f, value::TimeSampleInterpolationType::Linear));
TEST_CHECK(math::is_close(f, 10.0f));

TEST_CHECK(attr.get(value::TimeCode::Default(), &f, value::TimeSampleInterpolationType::Linear));
TEST_CHECK(math::is_close(f, 2000.0f));
}
}

}

0 comments on commit 1202c36

Please sign in to comment.