Commit 8a51401e authored by Philip Trettner's avatar Philip Trettner
Browse files

added median and order statistic

parent c3fc93a6
......@@ -285,6 +285,27 @@ auto smart_range<this_t, ElementT>::geometric_mean(FuncT &&f) const -> tmp::deca
return this->f_mean(ff, ff_inv);
}
template <class this_t, class ElementT>
template <class FuncT>
auto smart_range<this_t, ElementT>::median(FuncT &&f) const -> tmp::decayed_result_type_of<FuncT, ElementT>
{
return this->order_statistic(0.5f, f);
}
template <class this_t, class ElementT>
template <class FuncT>
auto smart_range<this_t, ElementT>::order_statistic(float p, FuncT &&f) const -> tmp::decayed_result_type_of<FuncT, ElementT>
{
auto vals = this->to_vector();
assert(!vals.empty() && "requires non-empty range");
auto n = (int)(std::roundf(vals.size() * p));
if (n < 0)
n = 0;
if (n >= (int)vals.size())
n = (int)vals.size() - 1;
return std::nth_element(vals.begin(), vals.begin() + n, vals.end());
}
template <class this_t, class ElementT>
template <class FuncT>
auto smart_range<this_t, ElementT>::aabb(FuncT &&f) const -> polymesh::aabb<tmp::decayed_result_type_of<FuncT, ElementT>>
......
......@@ -104,6 +104,16 @@ struct smart_range
template <class FuncT = tmp::identity>
auto geometric_mean(FuncT&& f = {}) const -> tmp::decayed_result_type_of<FuncT, ElementT>;
/// computes the median
/// NOTE: constructs an intermediate vector!
template <class FuncT = tmp::identity>
auto median(FuncT&& f = {}) const -> tmp::decayed_result_type_of<FuncT, ElementT>;
/// computes the p-order statistic (p in 0..1)
/// p: 0 is min, 0.5 is median, 1 is max
/// NOTE: constructs an intermediate vector!
template <class FuncT = tmp::identity>
auto order_statistic(float p, FuncT&& f = {}) const -> tmp::decayed_result_type_of<FuncT, ElementT>;
/// calculates the aabb (min and max) of f(e) over all elements
/// undefined behavior if range is empty
/// works for std::min/max and everything reachable by ADL (calls min/max(_, _))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment