2018-11-21 14:21:57 +00:00
|
|
|
#ifndef MTUTILS_HPP
|
|
|
|
#define MTUTILS_HPP
|
|
|
|
|
2018-11-21 14:33:30 +00:00
|
|
|
#include <atomic> // for std::atomic_flag and memory orders
|
2018-11-21 14:21:57 +00:00
|
|
|
#include <mutex> // for std::lock_guard
|
|
|
|
#include <functional> // for std::function
|
|
|
|
#include <utility> // for std::forward
|
2019-06-18 09:24:50 +00:00
|
|
|
#include <vector>
|
2019-06-18 14:24:30 +00:00
|
|
|
#include <algorithm>
|
2019-06-18 09:24:50 +00:00
|
|
|
#include <cmath>
|
2018-11-21 14:21:57 +00:00
|
|
|
|
2019-06-26 08:33:42 +00:00
|
|
|
#include "libslic3r.h"
|
|
|
|
|
2018-11-21 14:21:57 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
/// Handy little spin mutex for the cached meshes.
|
|
|
|
/// Implements the "Lockable" concept
|
2019-06-24 11:03:46 +00:00
|
|
|
class SpinMutex
|
|
|
|
{
|
|
|
|
std::atomic_flag m_flg;
|
2018-11-21 14:21:57 +00:00
|
|
|
static const /*constexpr*/ auto MO_ACQ = std::memory_order_acquire;
|
|
|
|
static const /*constexpr*/ auto MO_REL = std::memory_order_release;
|
2019-06-24 11:03:46 +00:00
|
|
|
|
2018-11-21 14:21:57 +00:00
|
|
|
public:
|
|
|
|
inline SpinMutex() { m_flg.clear(MO_REL); }
|
2019-06-24 11:03:46 +00:00
|
|
|
inline void lock() { while (m_flg.test_and_set(MO_ACQ)) ; }
|
2018-11-21 14:21:57 +00:00
|
|
|
inline bool try_lock() { return !m_flg.test_and_set(MO_ACQ); }
|
|
|
|
inline void unlock() { m_flg.clear(MO_REL); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A wrapper class around arbitrary object that needs thread safe caching.
|
2019-06-24 11:03:46 +00:00
|
|
|
template<class T> class CachedObject
|
|
|
|
{
|
2018-11-21 14:21:57 +00:00
|
|
|
public:
|
|
|
|
// Method type which refreshes the object when it has been invalidated
|
2019-06-24 11:03:46 +00:00
|
|
|
using Setter = std::function<void(T &)>;
|
|
|
|
|
2018-11-21 14:21:57 +00:00
|
|
|
private:
|
2019-06-24 11:03:46 +00:00
|
|
|
T m_obj; // the object itself
|
|
|
|
bool m_valid; // invalidation flag
|
|
|
|
SpinMutex m_lck; // to make the caching thread safe
|
2018-11-21 14:21:57 +00:00
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
// the setter will be called just before the object's const value is
|
|
|
|
// about to be retrieved.
|
|
|
|
std::function<void(T &)> m_setter;
|
2018-11-21 14:21:57 +00:00
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
public:
|
2018-11-21 14:21:57 +00:00
|
|
|
// Forwarded constructor
|
2019-06-24 11:03:46 +00:00
|
|
|
template<class... Args>
|
|
|
|
inline CachedObject(Setter fn, Args &&... args)
|
|
|
|
: m_obj(std::forward<Args>(args)...), m_valid(false), m_setter(fn)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// invalidate the value of the object. The object will be refreshed at
|
|
|
|
// the next retrieval (Setter will be called). The data that is used in
|
|
|
|
// the setter function should be guarded as well during modification so
|
|
|
|
// the modification has to take place in fn.
|
|
|
|
inline void invalidate(std::function<void()> fn)
|
|
|
|
{
|
|
|
|
std::lock_guard<SpinMutex> lck(m_lck);
|
|
|
|
fn();
|
|
|
|
m_valid = false;
|
2018-11-21 14:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the const object properly updated.
|
2019-06-24 11:03:46 +00:00
|
|
|
inline const T &get()
|
|
|
|
{
|
2018-11-21 14:21:57 +00:00
|
|
|
std::lock_guard<SpinMutex> lck(m_lck);
|
2019-06-24 11:03:46 +00:00
|
|
|
if (!m_valid) {
|
|
|
|
m_setter(m_obj);
|
|
|
|
m_valid = true;
|
|
|
|
}
|
2018-11-21 14:21:57 +00:00
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-22 16:05:41 +00:00
|
|
|
/// A very simple range concept implementation with iterator-like objects.
|
2019-06-24 11:03:46 +00:00
|
|
|
template<class It> class Range
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
It from, to;
|
2019-03-22 16:05:41 +00:00
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
public:
|
2019-03-22 16:05:41 +00:00
|
|
|
// The class is ready for range based for loops.
|
2019-03-22 14:31:38 +00:00
|
|
|
It begin() const { return from; }
|
|
|
|
It end() const { return to; }
|
2019-03-22 16:05:41 +00:00
|
|
|
|
|
|
|
// The iterator type can be obtained this way.
|
2019-03-22 14:31:38 +00:00
|
|
|
using Type = It;
|
|
|
|
|
|
|
|
Range() = default;
|
2019-06-24 11:03:46 +00:00
|
|
|
Range(It &&b, It &&e)
|
|
|
|
: from(std::forward<It>(b)), to(std::forward<It>(e))
|
|
|
|
{}
|
2019-03-22 14:31:38 +00:00
|
|
|
|
2019-03-22 16:05:41 +00:00
|
|
|
// Some useful container-like methods...
|
2019-03-22 14:31:38 +00:00
|
|
|
inline size_t size() const { return end() - begin(); }
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool empty() const { return size() == 0; }
|
2019-03-22 14:31:38 +00:00
|
|
|
};
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
template<class C> bool all_of(const C &container)
|
|
|
|
{
|
2019-06-18 14:24:30 +00:00
|
|
|
return std::all_of(container.begin(),
|
|
|
|
container.end(),
|
|
|
|
[](const typename C::value_type &v) {
|
|
|
|
return static_cast<bool>(v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:42:59 +00:00
|
|
|
template<class T> struct remove_cvref
|
2019-06-18 09:24:50 +00:00
|
|
|
{
|
|
|
|
using type =
|
|
|
|
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
|
|
|
};
|
|
|
|
|
2019-06-28 13:42:59 +00:00
|
|
|
template<class T> using remove_cvref_t = typename remove_cvref<T>::type;
|
2019-06-18 09:24:50 +00:00
|
|
|
|
2020-01-24 13:26:05 +00:00
|
|
|
/// Exactly like Matlab https://www.mathworks.com/help/matlab/ref/linspace.html
|
2020-04-13 10:31:37 +00:00
|
|
|
template<class T, class I, class = IntegerOnly<I>>
|
2020-01-24 13:26:05 +00:00
|
|
|
inline std::vector<T> linspace_vector(const ArithmeticOnly<T> &start,
|
|
|
|
const T &stop,
|
2020-04-13 10:31:37 +00:00
|
|
|
const I &n)
|
2020-01-24 13:26:05 +00:00
|
|
|
{
|
|
|
|
std::vector<T> vals(n, T());
|
|
|
|
|
|
|
|
T stride = (stop - start) / n;
|
|
|
|
size_t i = 0;
|
|
|
|
std::generate(vals.begin(), vals.end(), [&i, start, stride] {
|
|
|
|
return start + i++ * stride;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N, class T>
|
|
|
|
inline std::array<ArithmeticOnly<T>, N> linspace_array(const T &start, const T &stop)
|
|
|
|
{
|
|
|
|
std::array<T, N> vals = {T()};
|
|
|
|
|
|
|
|
T stride = (stop - start) / N;
|
|
|
|
size_t i = 0;
|
|
|
|
std::generate(vals.begin(), vals.end(), [&i, start, stride] {
|
|
|
|
return start + i++ * stride;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A set of equidistant values starting from 'start' (inclusive), ending
|
|
|
|
/// in the closest multiple of 'stride' less than or equal to 'end' and
|
|
|
|
/// leaving 'stride' space between each value.
|
|
|
|
/// Very similar to Matlab [start:stride:end] notation.
|
|
|
|
template<class T>
|
|
|
|
inline std::vector<ArithmeticOnly<T>> grid(const T &start,
|
|
|
|
const T &stop,
|
|
|
|
const T &stride)
|
|
|
|
{
|
|
|
|
std::vector<T> vals(size_t(std::ceil((stop - start) / stride)), T());
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
std::generate(vals.begin(), vals.end(), [&i, start, stride] {
|
|
|
|
return start + i++ * stride;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
} // namespace Slic3r
|
2018-11-21 14:21:57 +00:00
|
|
|
|
|
|
|
#endif // MTUTILS_HPP
|