2018-11-21 14:21:57 +00:00
|
|
|
#ifndef MTUTILS_HPP
|
|
|
|
#define MTUTILS_HPP
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
#include <atomic> // for std::atomic_flag and memory orders
|
|
|
|
#include <mutex> // for std::lock_guard
|
|
|
|
#include <functional> // for std::function
|
|
|
|
#include <utility> // for std::forward
|
2019-06-18 14:24:30 +00:00
|
|
|
#include <algorithm>
|
2018-11-21 14:21:57 +00:00
|
|
|
|
2019-06-26 08:33:42 +00:00
|
|
|
#include "libslic3r.h"
|
|
|
|
#include "Point.hpp"
|
|
|
|
|
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-06-24 11:03:46 +00:00
|
|
|
/// An std compatible random access iterator which uses indices to the
|
|
|
|
/// source vector thus resistant to invalidation caused by relocations. It
|
|
|
|
/// also "knows" its container. No comparison is neccesary to the container
|
|
|
|
/// "end()" iterator. The template can be instantiated with a different
|
|
|
|
/// value type than that of the container's but the types must be
|
|
|
|
/// compatible. E.g. a base class of the contained objects is compatible.
|
2019-03-22 16:05:41 +00:00
|
|
|
///
|
|
|
|
/// For a constant iterator, one can instantiate this template with a value
|
|
|
|
/// type preceded with 'const'.
|
2019-06-24 11:03:46 +00:00
|
|
|
template<class Vector, // The container type, must be random access...
|
2019-03-22 16:05:41 +00:00
|
|
|
class Value = typename Vector::value_type // The value type
|
|
|
|
>
|
2019-06-24 11:03:46 +00:00
|
|
|
class IndexBasedIterator
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
static const size_t NONE = size_t(-1);
|
|
|
|
|
|
|
|
std::reference_wrapper<Vector> m_index_ref;
|
2019-06-24 11:03:46 +00:00
|
|
|
size_t m_idx = NONE;
|
2019-03-22 14:31:38 +00:00
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
public:
|
|
|
|
using value_type = Value;
|
|
|
|
using pointer = Value *;
|
|
|
|
using reference = Value &;
|
|
|
|
using difference_type = long;
|
2019-03-22 14:31:38 +00:00
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline explicit IndexBasedIterator(Vector &index, size_t idx)
|
|
|
|
: m_index_ref(index), m_idx(idx)
|
|
|
|
{}
|
2019-03-22 14:31:38 +00:00
|
|
|
|
|
|
|
// Post increment
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator operator++(int)
|
|
|
|
{
|
|
|
|
IndexBasedIterator cpy(*this);
|
|
|
|
++m_idx;
|
|
|
|
return cpy;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator operator--(int)
|
|
|
|
{
|
|
|
|
IndexBasedIterator cpy(*this);
|
|
|
|
--m_idx;
|
|
|
|
return cpy;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator &operator++()
|
|
|
|
{
|
|
|
|
++m_idx;
|
|
|
|
return *this;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator &operator--()
|
|
|
|
{
|
|
|
|
--m_idx;
|
|
|
|
return *this;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator &operator+=(difference_type l)
|
|
|
|
{
|
|
|
|
m_idx += size_t(l);
|
|
|
|
return *this;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator operator+(difference_type l)
|
|
|
|
{
|
|
|
|
auto cpy = *this;
|
|
|
|
cpy += l;
|
|
|
|
return cpy;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator &operator-=(difference_type l)
|
|
|
|
{
|
|
|
|
m_idx -= size_t(l);
|
|
|
|
return *this;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline IndexBasedIterator operator-(difference_type l)
|
|
|
|
{
|
|
|
|
auto cpy = *this;
|
|
|
|
cpy -= l;
|
|
|
|
return cpy;
|
2019-03-22 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
operator difference_type() { return difference_type(m_idx); }
|
|
|
|
|
2019-03-22 16:05:41 +00:00
|
|
|
/// Tesing the end of the container... this is not possible with std
|
|
|
|
/// iterators.
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool is_end() const
|
|
|
|
{
|
|
|
|
return m_idx >= m_index_ref.get().size();
|
|
|
|
}
|
2019-03-22 14:31:38 +00:00
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline Value &operator*() const
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
assert(m_idx < m_index_ref.get().size());
|
|
|
|
return m_index_ref.get().operator[](m_idx);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline Value *operator->() const
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
assert(m_idx < m_index_ref.get().size());
|
|
|
|
return &m_index_ref.get().operator[](m_idx);
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:05:41 +00:00
|
|
|
/// If both iterators point past the container, they are equal...
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator==(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
size_t e = m_index_ref.get().size();
|
|
|
|
return m_idx == other.m_idx || (m_idx >= e && other.m_idx >= e);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator!=(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator<=(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
return (m_idx < other.m_idx) || (*this == other);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator<(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
return m_idx < other.m_idx && (*this != other);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator>=(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
return m_idx > other.m_idx || *this == other;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
inline bool operator>(const IndexBasedIterator &other)
|
|
|
|
{
|
2019-03-22 14:31:38 +00:00
|
|
|
return m_idx > other.m_idx && *this != other;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-26 08:33:42 +00:00
|
|
|
// A shorter C++14 style form of the enable_if metafunction
|
|
|
|
template<bool B, class T>
|
|
|
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
|
|
|
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Type safe conversions to and from scaled and unscaled coordinates
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// A meta-predicate which is true for integers wider than or equal to coord_t
|
|
|
|
template<class I> struct is_scaled_coord
|
|
|
|
{
|
|
|
|
static const SLIC3R_CONSTEXPR bool value =
|
|
|
|
std::is_integral<I>::value &&
|
|
|
|
std::numeric_limits<I>::digits >=
|
|
|
|
std::numeric_limits<coord_t>::digits;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Meta predicates for floating, 'scaled coord' and generic arithmetic types
|
|
|
|
template<class T>
|
|
|
|
using FloatingOnly = enable_if_t<std::is_floating_point<T>::value, T>;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using ScaledCoordOnly = enable_if_t<is_scaled_coord<T>::value, T>;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using ArithmeticOnly = enable_if_t<std::is_arithmetic<T>::value, T>;
|
|
|
|
|
|
|
|
// A shorter form for a generic Eigen vector which is widely used in PrusaSlicer
|
|
|
|
template<class T, int N>
|
|
|
|
using EigenVec = Eigen::Matrix<T, N, 1, Eigen::DontAlign>;
|
|
|
|
|
|
|
|
// Semantics are the following:
|
|
|
|
// Upscaling (scaled()): only from floating point types (or Vec) to either
|
|
|
|
// floating point or integer 'scaled coord' coordinates.
|
2019-06-27 19:13:44 +00:00
|
|
|
// Downscaling (unscaled()): from arithmetic (or Vec) to floating point only
|
2019-06-26 08:33:42 +00:00
|
|
|
|
|
|
|
// Conversion definition from unscaled to floating point scaled
|
|
|
|
template<class Tout,
|
|
|
|
class Tin,
|
2019-07-17 12:34:28 +00:00
|
|
|
class = FloatingOnly<Tin>>
|
|
|
|
inline SLIC3R_CONSTEXPR FloatingOnly<Tout> scaled(const Tin &v) SLIC3R_NOEXCEPT
|
2019-06-26 08:33:42 +00:00
|
|
|
{
|
2019-06-27 19:13:44 +00:00
|
|
|
return Tout(v / Tin(SCALING_FACTOR));
|
2019-06-26 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion definition from unscaled to integer 'scaled coord'.
|
2019-06-27 19:13:44 +00:00
|
|
|
// TODO: is the rounding necessary? Here it is commented out to show that
|
|
|
|
// it can be different for integers but it does not have to be. Using
|
|
|
|
// std::round means loosing noexcept and constexpr modifiers
|
2019-06-26 08:33:42 +00:00
|
|
|
template<class Tout = coord_t, class Tin, class = FloatingOnly<Tin>>
|
|
|
|
inline SLIC3R_CONSTEXPR ScaledCoordOnly<Tout> scaled(const Tin &v) SLIC3R_NOEXCEPT
|
|
|
|
{
|
|
|
|
//return static_cast<Tout>(std::round(v / SCALING_FACTOR));
|
2019-06-27 19:13:44 +00:00
|
|
|
return Tout(v / Tin(SCALING_FACTOR));
|
2019-06-26 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion for Eigen vectors (N dimensional points)
|
|
|
|
template<class Tout = coord_t, class Tin, int N, class = FloatingOnly<Tin>>
|
|
|
|
inline EigenVec<ArithmeticOnly<Tout>, N> scaled(const EigenVec<Tin, N> &v)
|
|
|
|
{
|
2019-06-28 15:03:50 +00:00
|
|
|
return (v / SCALING_FACTOR).template cast<Tout>();
|
2019-06-26 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion from arithmetic scaled type to floating point unscaled
|
|
|
|
template<class Tout = double,
|
|
|
|
class Tin,
|
|
|
|
class = ArithmeticOnly<Tin>,
|
|
|
|
class = FloatingOnly<Tout>>
|
|
|
|
inline SLIC3R_CONSTEXPR Tout unscaled(const Tin &v) SLIC3R_NOEXCEPT
|
|
|
|
{
|
2019-06-27 19:13:44 +00:00
|
|
|
return Tout(v * Tout(SCALING_FACTOR));
|
2019-06-26 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unscaling for Eigen vectors. Input base type can be arithmetic, output base
|
|
|
|
// type can only be floating point.
|
|
|
|
template<class Tout = double,
|
|
|
|
class Tin,
|
|
|
|
int N,
|
|
|
|
class = ArithmeticOnly<Tin>,
|
|
|
|
class = FloatingOnly<Tout>>
|
|
|
|
inline SLIC3R_CONSTEXPR EigenVec<Tout, N> unscaled(
|
|
|
|
const EigenVec<Tin, N> &v) SLIC3R_NOEXCEPT
|
|
|
|
{
|
|
|
|
return v.template cast<Tout>() * SCALING_FACTOR;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:03:46 +00:00
|
|
|
} // namespace Slic3r
|
2018-11-21 14:21:57 +00:00
|
|
|
|
|
|
|
#endif // MTUTILS_HPP
|