Skip to content

Commit

Permalink
Add set_indices() and get_indices() to GeomPrimvar.
Browse files Browse the repository at this point in the history
Add API to query an sample exists at specified time(TimeSamples,
TypedTimeSamples)
  • Loading branch information
syoyo committed May 11, 2024
1 parent b052341 commit d22bf14
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/math-util.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "prim-types.hh"
//#include "prim-types.hh"
#include "value-types.hh"

namespace tinyusdz {
Expand Down
32 changes: 32 additions & 0 deletions src/prim-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "primvar.hh"
//
#include "value-eval-util.hh"
#include "math-util.inc"

namespace tinyusdz {

Expand Down Expand Up @@ -1310,6 +1311,37 @@ struct TypedTimeSamples {
_dirty = true;
}

bool has_sample_at(const double t) const {
if (_dirty) {
update();
}

const auto it = std::find_if(_samples.begin(), _samples.end(), [&t](const Sample &s) {
return tinyusdz::math::is_close(t, s.t);
});

return (it != _samples.end());
}

bool get_sample_at(const double t, Sample **dst) {
if (!dst) {
return false;
}

if (_dirty) {
update();
}

const auto it = std::find_if(_samples.begin(), _samples.end(), [&t](const Sample &sample) {
return math::is_close(t, sample.t);
});

if (it != _samples.end()) {
(*dst) = &(*it);
}
return false;
}

const std::vector<Sample> &get_samples() const {
if (_dirty) {
update();
Expand Down
35 changes: 35 additions & 0 deletions src/usdGeom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,41 @@ bool GeomPrimvar::flatten_with_indices(const double t, std::vector<T> *dest, con
return false;
}

std::vector<int32_t> GeomPrimvar::get_indices(const double t) const {
if (value::TimeCode(t).is_default()) {
if (has_default_indices()) {
return get_default_indices();
}
}

if (has_timesampled_indices()) {
std::vector<int32_t> indices;
if (get_timesampled_indices().get(&indices, t)) {
return indices;
}
}

if (has_default_indices()) {
return get_default_indices();
}

return std::vector<int32_t>();
}

void GeomPrimvar::set_indices(const std::vector<int32_t> &indices, const double t) {
if (value::TimeCode(t).is_default()) {
_indices = indices;
} else {
TypedTimeSamples<std::vector<int32_t>>::Sample *psample{nullptr};
if (_ts_indices.get_sample_at(t, &psample)) {
// overwrite content
psample->value = indices;
} else {
_ts_indices.add_sample(t, indices);
}
}
}

template <typename T>
bool GeomPrimvar::flatten_with_indices(std::vector<T> *dest, std::string *err) const {
return flatten_with_indices(value::TimeCode::Default(), dest, value::TimeSampleInterpolationType::Linear, err);
Expand Down
10 changes: 10 additions & 0 deletions src/usdGeom.hh
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ class GeomPrimvar {
_unauthoredValuesIndex = n;
}

///
/// Get indices at specified timecode.
/// Returns empty when appropriate indices does not exist for timecode 't'.
///
std::vector<int32_t> get_indices(const double t = value::TimeCode::Default()) const;

const std::vector<int32_t> &get_default_indices() const {
return _indices;
Expand Down Expand Up @@ -266,6 +271,11 @@ class GeomPrimvar {

void set_name(const std::string &name) { _name = name; }

// Set indices for specified timecode 't'
// indices will be replaced when there is an indices at timecode 't'.
void set_indices(const std::vector<int32_t> &indices, const double t = value::TimeCode::Default());


void set_default_indices(const std::vector<int32_t> &indices) {
_indices = indices;
}
Expand Down
32 changes: 32 additions & 0 deletions src/value-types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

//
#include "common-macros.inc"
#include "math-util.inc"

// For compile-time map
// Another candidate is frozen: https://github.com/serge-sans-paille/frozen
Expand Down Expand Up @@ -1075,5 +1076,36 @@ bool FlexibleTypeCast(const value::Value &src, value::Value &dst) {
}
#endif

bool TimeSamples::has_sample_at(const double t) const {
if (_dirty) {
update();
}

const auto it = std::find_if(_samples.begin(), _samples.end(), [&t](const Sample &s) {
return math::is_close(t, s.t);
});

return (it != _samples.end());
}

bool TimeSamples::get_sample_at(const double t, Sample **dst) {
if (!dst) {
return false;
}

if (_dirty) {
update();
}

const auto it = std::find_if(_samples.begin(), _samples.end(), [&t](const Sample &sample) {
return math::is_close(t, sample.t);
});

if (it != _samples.end()) {
(*dst) = &(*it);
}
return false;
}

} // namespace value
} // namespace tinyusdz
3 changes: 3 additions & 0 deletions src/value-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,9 @@ struct TimeSamples {
_dirty = false;
}

bool has_sample_at(const double t) const;
bool get_sample_at(const double t, Sample **s);

nonstd::optional<double> get_time(size_t idx) const {
if (idx >= _samples.size()) {
return nonstd::nullopt;
Expand Down

0 comments on commit d22bf14

Please sign in to comment.