diff --git a/libs/cg/CMakeLists.txt b/libs/cg/CMakeLists.txt index b6ebf0e4..6596be48 100644 --- a/libs/cg/CMakeLists.txt +++ b/libs/cg/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB_RECURSE PSEMEK_CG_HEADERS "include/*.hpp") -add_library(cg STATIC ${PSEMEK_CG_HEADERS}) +add_library(cg ${PSEMEK_CG_HEADERS}) target_include_directories(cg PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(cg PUBLIC geom) +target_link_libraries(cg PUBLIC geom util) set_target_properties(cg PROPERTIES LINKER_LANGUAGE CXX) diff --git a/libs/cg/include/psemek/cg/bbox.hpp b/libs/cg/include/psemek/cg/bbox.hpp index 93bdf7e6..a02da93e 100644 --- a/libs/cg/include/psemek/cg/bbox.hpp +++ b/libs/cg/include/psemek/cg/bbox.hpp @@ -2,15 +2,15 @@ #include -namespace cg +namespace psemek::cg { template - auto bbox (Iterator begin, Iterator end) + auto bbox(Iterator begin, Iterator end) { using point_type = std::decay_t; - box result; + geom::box result; for (; begin != end; ++begin) result |= *begin; return result; diff --git a/libs/cg/include/psemek/cg/clip.hpp b/libs/cg/include/psemek/cg/clip.hpp index c8b2f504..84025b5d 100644 --- a/libs/cg/include/psemek/cg/clip.hpp +++ b/libs/cg/include/psemek/cg/clip.hpp @@ -1,15 +1,15 @@ #pragma once -#include #include #include +#include -namespace cg +namespace psemek::cg { // TODO: this was written for Voronoi and is known to fail in some cases template - void clip (dcel, Edge, Face, Index> & dcel, cg::vector const & eq) + void clip(dcel, Edge, Face, Index> & dcel, geom::vector const & eq) { auto outer_face = dcel.face(0); diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp index 4e9a62a2..2e10c7f7 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp @@ -6,11 +6,11 @@ #include #include -namespace cg +namespace psemek::cg { template - OutIterator andrew_convex_hull (InIterator begin, InIterator end, OutIterator out) + OutIterator andrew_convex_hull(InIterator begin, InIterator end, OutIterator out) { // need to store iterators to sort them std::vector its(end - begin); @@ -25,7 +25,7 @@ namespace cg // find lower half of the hull for (auto it = its.begin() + 1; it != its.end(); ++it) { - while (hull.size() >= 2 && orientation(**(hull.end() - 2), **(hull.end() - 1), **it) != sign_t::positive) + while (hull.size() >= 2 && orientation(**(hull.end() - 2), **(hull.end() - 1), **it) != geom::sign_t::positive) hull.pop_back(); hull.push_back(*it); @@ -37,7 +37,7 @@ namespace cg if (*it == *(hull.end() - 2)) continue; - while (hull.size() >= 2 && orientation(**(hull.end() - 2), **(hull.end() - 1), **it) != sign_t::positive) + while (hull.size() >= 2 && orientation(**(hull.end() - 2), **(hull.end() - 1), **it) != geom::sign_t::positive) hull.pop_back(); hull.push_back(*it); diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp index 9eb7f0c9..9bf4f927 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp @@ -6,11 +6,11 @@ #include #include -namespace cg +namespace psemek::cg { template - OutIterator graham_convex_hull (InIterator begin, InIterator end, OutIterator out) + OutIterator graham_convex_hull(InIterator begin, InIterator end, OutIterator out) { // need to store iterators to sort them std::vector its(end - begin); @@ -27,9 +27,9 @@ namespace cg auto o = orientation(*its.front(), *i1, *i2); // carefully deal with parallel points - if (o == sign_t::positive) + if (o == geom::sign_t::positive) return true; - else if (o == sign_t::negative) + else if (o == geom::sign_t::negative) return false; return *i1 < *i2; @@ -41,7 +41,7 @@ namespace cg for (auto it = its.begin() + 1; it != its.end(); ++it) { - while (hull_end - its.begin() >= 2 && orientation(**(hull_end - 2), **(hull_end - 1), **it) != sign_t::positive) + while (hull_end - its.begin() >= 2 && orientation(**(hull_end - 2), **(hull_end - 1), **it) != geom::sign_t::positive) --hull_end; *hull_end++ = *it; diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp index f2217b61..36d30ecc 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp @@ -2,11 +2,11 @@ #include -namespace cg +namespace psemek::cg { template - OutIterator jarvis_convex_hull (InIterator begin, InIterator end, OutIterator out) + OutIterator jarvis_convex_hull(InIterator begin, InIterator end, OutIterator out) { auto first_hull_point = std::min_element(begin, end); auto last_hull_point = first_hull_point; @@ -26,7 +26,7 @@ namespace cg if (jt == it) continue; if (jt == last_hull_point) continue; - if (orientation(*last_hull_point, *it, *jt) != cg::sign_t::positive) + if (orientation(*last_hull_point, *it, *jt) != geom::sign_t::positive) { is_hull_edge = false; break; diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp index b7c01340..61bdfa84 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp @@ -6,14 +6,14 @@ #include #include -namespace cg +namespace psemek::cg { namespace detail { template - OutIterator quick_hull_recursive_helper (InIterator p1, InIterator p2, ItIterator begin, ItIterator end, OutIterator out) + OutIterator quick_hull_recursive_helper(InIterator p1, InIterator p2, ItIterator begin, ItIterator end, OutIterator out) { if (begin == end) return *out++ = p1; @@ -21,11 +21,11 @@ namespace cg // find point in [begin, end) furthest from segment (p1,p2) auto mid = *std::max_element(begin, end, [&](auto it1, auto it2){ // TODO: design a robust predicate - return orientation(*it2, (*it2) + ((*p2) - (*p1)), *it1) == sign_t::positive; + return orientation(*it2, (*it2) + ((*p2) - (*p1)), *it1) == geom::sign_t::positive; }); - auto end1 = std::partition(begin, end, [&](auto it){ return orientation(*p1, *it, *mid) == sign_t::positive; }); - auto end2 = std::partition(end1, end, [&](auto it){ return orientation(*mid, *it, *p2) == sign_t::positive; }); + auto end1 = std::partition(begin, end, [&](auto it){ return orientation(*p1, *it, *mid) == geom::sign_t::positive; }); + auto end2 = std::partition(end1, end, [&](auto it){ return orientation(*mid, *it, *p2) == geom::sign_t::positive; }); out = quick_hull_recursive_helper(p1, mid, begin, end1, out); out = quick_hull_recursive_helper(mid, p2, end1, end2, out); @@ -36,7 +36,7 @@ namespace cg } template - OutIterator quick_hull (InIterator begin, InIterator end, OutIterator out) + OutIterator quick_hull(InIterator begin, InIterator end, OutIterator out) { // need to store iterators to move sets of points around std::vector its(end - begin); @@ -52,7 +52,7 @@ namespace cg { auto p = std::find_if(its.begin() + 1, its.end(), [&](auto it){ return std::all_of(its.begin() + 1, its.end(), [&](auto jt){ - return it == jt || orientation(*its.front(), *it, *jt) == sign_t::negative; + return it == jt || orientation(*its.front(), *it, *jt) == geom::sign_t::negative; }); }); std::iter_swap(its.begin() + 1, p); diff --git a/libs/cg/include/psemek/cg/dcel.hpp b/libs/cg/include/psemek/cg/dcel.hpp index d1958b06..825ac312 100644 --- a/libs/cg/include/psemek/cg/dcel.hpp +++ b/libs/cg/include/psemek/cg/dcel.hpp @@ -1,12 +1,13 @@ #pragma once -#include +#include +#include #include #include #include -namespace cg +namespace psemek::cg { namespace detail @@ -36,21 +37,21 @@ namespace cg template struct handle_base { - handle_base ( ) + handle_base() : owner_(nullptr) , i_(static_cast(-1)) - { } + {} - handle_base (DCEL * owner, Index i) + handle_base(DCEL * owner, Index i) : owner_(owner) , i_(i) - { } + {} - DCEL & owner ( ) const { return *owner_; } + DCEL & owner() const { return *owner_; } - Index index ( ) const { return i_; } + Index index() const { return i_; } - explicit operator bool ( ) const { return i_ != static_cast(-1); } + explicit operator bool() const { return i_ != static_cast(-1); } friend bool operator == (handle_base const & h1, handle_base const & h2) { @@ -87,9 +88,9 @@ namespace cg Index i_; }; - struct point_tag { }; - struct edge_tag { }; - struct face_tag { }; + struct point_tag{}; + struct edge_tag{}; + struct face_tag{}; } @@ -118,14 +119,14 @@ namespace cg { using detail::handle_base::handle_base; - Point & data ( ) const; + Point & data() const; - edge_handle edge ( ) const; + edge_handle edge() const; - void edge (edge_handle h); + void edge(edge_handle h); protected: - point_rec & get ( ) const + point_rec & get() const { assert(this->owner_ != nullptr); assert(this->i_ < this->owner_->points.size()); @@ -137,20 +138,20 @@ namespace cg { using detail::handle_base::handle_base; - Edge & data ( ) const; + Edge & data() const; - point_handle origin ( ) const; - edge_handle next ( ) const; - edge_handle twin ( ) const; - face_handle face ( ) const; + point_handle origin() const; + edge_handle next() const; + edge_handle twin() const; + face_handle face() const; - void origin (point_handle h); - void next (edge_handle h); - void twin (edge_handle h); - void face (face_handle h); + void origin(point_handle h); + void next(edge_handle h); + void twin(edge_handle h); + void face(face_handle h); protected: - edge_rec & get ( ) const + edge_rec & get() const { assert(this->owner_ != nullptr); assert(this->i_ < this->owner_->edges.size()); @@ -162,14 +163,14 @@ namespace cg { using detail::handle_base::handle_base; - Face & data ( ) const; + Face & data() const; - edge_handle edge ( ) const; + edge_handle edge() const; - void edge (edge_handle h); + void edge(edge_handle h); protected: - face_rec & get ( ) const + face_rec & get() const { assert(this->owner_ != nullptr); assert(this->i_ < this->owner_->faces.size()); @@ -177,43 +178,43 @@ namespace cg } }; - point_handle point (Index i) + point_handle point(Index i) { return {this, i}; } - edge_handle edge (Index i) + edge_handle edge(Index i) { return {this, i}; } - face_handle face (Index i) + face_handle face(Index i) { return {this, i}; } - point_handle push_point (Point data = {}) + point_handle push_point(Point data = {}) { auto i = static_cast(points.size()); points.push_back({{std::move(data)}, null}); return point(i); } - edge_handle push_edge (Edge data = {}) + edge_handle push_edge(Edge data = {}) { auto i = static_cast(edges.size()); edges.push_back({{std::move(data)}, null, null, null, null}); return edge(i); } - face_handle push_face (Face data = {}) + face_handle push_face(Face data = {}) { auto i = static_cast(faces.size()); faces.push_back({{std::move(data)}, null}); return face(i); } - point_handle insert_point (Index i, Point data = {}) + point_handle insert_point(Index i, Point data = {}) { points.insert(points.begin() + i, {{std::move(data)}, null}); for (auto & e : edges) @@ -224,7 +225,7 @@ namespace cg return point(i); } - edge_handle insert_edge (Index i, Edge data = {}) + edge_handle insert_edge(Index i, Edge data = {}) { edges.insert(edges.begin() + i, {{std::move(data)}, null, null, null, null}); for (auto & p : points) @@ -247,7 +248,7 @@ namespace cg return edge(i); } - face_handle insert_face (Index i, Face data = {}) + face_handle insert_face(Index i, Face data = {}) { faces.insert(faces.begin() + i, {{std::move(data)}, null}); for (auto & e : edges) @@ -258,7 +259,7 @@ namespace cg return face(i); } - void remove_point (point_handle h) + void remove_point(point_handle h) { points.erase(points.begin() + h.index()); for (auto & e : edges) @@ -268,7 +269,7 @@ namespace cg } } - void remove_edge (edge_handle h) + void remove_edge(edge_handle h) { edges.erase(edges.begin() + h.index()); for (auto & p : points) @@ -290,7 +291,7 @@ namespace cg } } - void remove_face (face_handle h) + void remove_face(face_handle h) { faces.erase(faces.begin() + h.index()); for (auto & e : edges) @@ -435,7 +436,7 @@ namespace cg } template - auto polygon_dcel (Iterator begin, Iterator end) + auto polygon_dcel(Iterator begin, Iterator end) { using point_type = std::decay_t; diff --git a/libs/cg/include/psemek/cg/dual.hpp b/libs/cg/include/psemek/cg/dual.hpp index c371534b..28474eb6 100644 --- a/libs/cg/include/psemek/cg/dual.hpp +++ b/libs/cg/include/psemek/cg/dual.hpp @@ -2,11 +2,11 @@ #include -namespace cg +namespace psemek::cg { template - dcel dual (dcel const & d) + dcel dual(dcel const & d) { dcel result; diff --git a/libs/cg/include/psemek/cg/segment_intersection.hpp b/libs/cg/include/psemek/cg/segment_intersection.hpp index 0103ba9e..5e3381e5 100644 --- a/libs/cg/include/psemek/cg/segment_intersection.hpp +++ b/libs/cg/include/psemek/cg/segment_intersection.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -11,11 +10,11 @@ #include #include -namespace cg +namespace psemek::cg { template - OutIterator segment_intersection (InIterator begin, InIterator end, OutIterator out) + OutIterator segment_intersection(InIterator begin, InIterator end, OutIterator out) { using segment = std::decay_t; using point = typename segment::point_type; @@ -39,7 +38,7 @@ namespace cg struct event_comparator { - bool operator() (event const & e1, event const & e2) const + bool operator()(event const & e1, event const & e2) const { return e1.p > e2.p; } @@ -71,7 +70,7 @@ namespace cg { scalar & sweep_line; - bool operator() (InIterator it1, InIterator it2) const + bool operator()(InIterator it1, InIterator it2) const { scalar const eps = 1e-4; scalar const y1 = (*it1)[0][1] + ((*it1)[1][1] -(*it1)[0][1]) * (sweep_line + eps - (*it1)[0][0]) / ((*it1)[1][0] - (*it1)[0][0]); diff --git a/libs/cg/include/psemek/cg/triangulation/delaunay.hpp b/libs/cg/include/psemek/cg/triangulation/delaunay.hpp index 4f6c2e00..e21fa522 100644 --- a/libs/cg/include/psemek/cg/triangulation/delaunay.hpp +++ b/libs/cg/include/psemek/cg/triangulation/delaunay.hpp @@ -5,11 +5,11 @@ #include -namespace cg +namespace psemek::cg { template - auto delaunay (InputIterator begin, InputIterator end) + auto delaunay(InputIterator begin, InputIterator end) { std::vector edge_queue; @@ -58,7 +58,7 @@ namespace cg auto p3 = e.twin().next().next().origin(); // decide if a flip is needed - if (in_circle(*p0.data(), *p1.data(), *p2.data(), *p3.data()) != sign_t::positive) continue; + if (in_circle(*p0.data(), *p1.data(), *p2.data(), *p3.data()) != geom::sign_t::positive) continue; auto f0 = e.face(); auto f1 = twin.face(); diff --git a/libs/cg/include/psemek/cg/triangulation/triangulation.hpp b/libs/cg/include/psemek/cg/triangulation/triangulation.hpp index 0a37b5d6..047b29d8 100644 --- a/libs/cg/include/psemek/cg/triangulation/triangulation.hpp +++ b/libs/cg/include/psemek/cg/triangulation/triangulation.hpp @@ -1,20 +1,20 @@ #pragma once +#include #include #include -#include #include #include -namespace cg +namespace psemek::cg { namespace detail { template - auto triangulate (Iterator begin, Iterator end, Callback && callback) + auto triangulate(Iterator begin, Iterator end, Callback && callback) { using point_type = std::decay_t; static_assert(point_type::dimension == 2); @@ -93,7 +93,7 @@ namespace cg bool degenerate = false; // find first hull edge visible from new point - while (orientation(**it, *hp0.data(), *hp1.data()) != sign_t::positive) + while (orientation(**it, *hp0.data(), *hp1.data()) != geom::sign_t::positive) { move_hull_edge(); if (cur_hull_edge == hull_start) @@ -192,7 +192,7 @@ namespace cg } // until current edge is visible - while (orientation(**it, *hp0.data(), *hp1.data()) == sign_t::positive) + while (orientation(**it, *hp0.data(), *hp1.data()) == geom::sign_t::positive) { // fill new triangle @@ -254,7 +254,7 @@ namespace cg } template - auto triangulate (InputIterator begin, InputIterator end) + auto triangulate(InputIterator begin, InputIterator end) { return detail::triangulate(begin, end, util::nop); } diff --git a/libs/cg/include/psemek/cg/utility.hpp b/libs/cg/include/psemek/cg/utility.hpp deleted file mode 100644 index ab4e1754..00000000 --- a/libs/cg/include/psemek/cg/utility.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once - -#include -#include - -namespace cg::util -{ - - struct empty{}; - - template - struct overload_impl - : Fs ... - { - using Fs::operator() ...; - }; - - template - auto overload (Fs && ... fs) - { - return overload_impl{ std::forward(fs) ... }; - } - - template - decltype(auto) match (Variant && v, Fs && ... fs) - { - return std::visit(overload(std::forward(fs)...), std::forward(v)); - } - - constexpr auto id = [](auto const & x){ return x; }; - - constexpr auto nop = [](auto const & ...){}; - - namespace detail - { - - template - struct ebo_helper_impl : T - { - T & data() { return *this; } - T const & data() const { return *this; } - - template - ebo_helper_impl (Args && ... args) - : T(std::forward(args)...) - { } - }; - - template - struct ebo_helper_impl - { - T value; - - T & data() { return value; } - T const & data() const { return value; } - - template - ebo_helper_impl (Args && ... args) - : value(std::forward(args)...) - { } - }; - - } - - template - using ebo_helper = detail::ebo_helper_impl && !(std::is_final_v)>; - -} diff --git a/libs/cg/include/psemek/cg/voronoi.hpp b/libs/cg/include/psemek/cg/voronoi.hpp index 0f1abd1a..47d1eb51 100644 --- a/libs/cg/include/psemek/cg/voronoi.hpp +++ b/libs/cg/include/psemek/cg/voronoi.hpp @@ -1,17 +1,17 @@ #pragma once +#include #include #include -#include #include -namespace cg +namespace psemek::cg { // Point #0 of the resulting dcel corresponds to the outer face, // i.e. the point at infinity, and has unspecified coordinates. template - auto voronoi (Iterator begin, Iterator end) + auto voronoi(Iterator begin, Iterator end) { using point_type = std::decay_t; @@ -35,8 +35,8 @@ namespace cg using T = typename point_type::scalar_type; - matrix m; - vector b; + geom::matrix m; + geom::vector b; for (std::size_t i = 0; i < 3; ++i) { @@ -47,7 +47,7 @@ namespace cg b[i] = q[i][0] * q[i][0] + q[i][1] * q[i][1]; } - gauss(m, b); + geom::gauss(m, b); auto newp = result.push_point(point_type{b[0] / 2, b[1] / 2}); newp.edge(result.edge(p.edge().index())); @@ -61,7 +61,7 @@ namespace cg // Replaces the #0 infinite point of a dcel with a proper outer face template - void remove_infinite_point (dcel & dcel) + void remove_infinite_point(dcel & dcel) { // insert outer face at position #0 auto outer_face = dcel.insert_face(0); @@ -117,7 +117,7 @@ namespace cg // iteratively removes degenerate edges template - void remove_degenerate_edges (dcel & dcel) + void remove_degenerate_edges(dcel & dcel) { std::vector delete_points; std::vector delete_edges; @@ -160,7 +160,7 @@ namespace cg // Outputs 4 points such that the finite faces of the voronoi diagram of (anything // inside the convex hull of input + the output points) covers the convex hull of input template - OutputIterator bounded_voronoi_extra_points (InputIterator begin, InputIterator end, OutputIterator out) + OutputIterator bounded_voronoi_extra_points(InputIterator begin, InputIterator end, OutputIterator out) { using point_type = std::decay_t; @@ -183,7 +183,7 @@ namespace cg // Compute a clipped voronoi tessellation of (begin, end - 4) // Last 4 points will be used for temporary data template - auto bounded_voronoi (InputIterator begin, InputIterator end) + auto bounded_voronoi(InputIterator begin, InputIterator end) { bounded_voronoi_extra_points(begin, end - 4, end - 4); auto dcel = cg::voronoi(begin, end); diff --git a/libs/geom/CMakeLists.txt b/libs/geom/CMakeLists.txt index 6a09ff48..2493f342 100644 --- a/libs/geom/CMakeLists.txt +++ b/libs/geom/CMakeLists.txt @@ -4,6 +4,6 @@ find_package(GMP REQUIRED) file(GLOB_RECURSE PSEMEK_GEOM_HEADERS "include/*.hpp") file(GLOB_RECURSE PSEMEK_GEOM_SOURCES "source/*.cpp") -add_library(geom STATIC ${PSEMEK_GEOM_HEADERS} ${PSEMEK_GEOM_SOURCES}) +add_library(geom ${PSEMEK_GEOM_HEADERS} ${PSEMEK_GEOM_SOURCES}) target_include_directories(geom PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(geom PUBLIC Boost::boost GMP) +target_link_libraries(geom PUBLIC util Boost::boost GMP) diff --git a/libs/geom/include/psemek/geom/bezier.hpp b/libs/geom/include/psemek/geom/bezier.hpp index c50a24d0..8b1610e7 100644 --- a/libs/geom/include/psemek/geom/bezier.hpp +++ b/libs/geom/include/psemek/geom/bezier.hpp @@ -11,7 +11,7 @@ namespace psemek::geom template struct bezier { - bezier (std::vector points) + bezier(std::vector points) : points_(std::move(points)) { if (points_.empty()) @@ -20,7 +20,7 @@ namespace psemek::geom } template - auto operator() (T const & t) const + auto operator()(T const & t) const { // In-place de Casteljau's algorithm temp_ = points_; diff --git a/libs/geom/include/psemek/geom/box.hpp b/libs/geom/include/psemek/geom/box.hpp index 4f668ece..bc2d29d5 100644 --- a/libs/geom/include/psemek/geom/box.hpp +++ b/libs/geom/include/psemek/geom/box.hpp @@ -14,18 +14,18 @@ namespace psemek::geom using point_type = point; using vector_type = vector; - interval & operator [] (std::size_t i) + interval & operator[](std::size_t i) { return axes[i]; } - interval const & operator [] (std::size_t i) const + interval const & operator[](std::size_t i) const { return axes[i]; } // singleton box - static box singleton (point_type const & p) + static box singleton(point_type const & p) { box b; for (std::size_t i = 0; i < N; ++i) @@ -33,7 +33,7 @@ namespace psemek::geom return b; } - bool empty ( ) const + bool empty() const { for (auto const & i : axes) if (i.empty()) @@ -41,7 +41,7 @@ namespace psemek::geom return false; } - T size ( ) const + T size() const { T result = T{1}; for (auto const & i : axes) diff --git a/libs/geom/include/psemek/geom/contains.hpp b/libs/geom/include/psemek/geom/contains.hpp index aa482a53..c59eacff 100644 --- a/libs/geom/include/psemek/geom/contains.hpp +++ b/libs/geom/include/psemek/geom/contains.hpp @@ -10,13 +10,13 @@ namespace psemek::geom { template - bool contains (interval const & i, T const & x) + bool contains(interval const & i, T const & x) { return (i.min <= x) && (x <= i.max); } template - bool contains (box const & b, point const & p) + bool contains(box const & b, point const & p) { for (std::size_t i = 0; i < N; ++i) if (!contains(b[i], p[i])) @@ -25,7 +25,7 @@ namespace psemek::geom } template - bool contains (triangle> const & t, point const & p) + bool contains(triangle> const & t, point const & p) { return true && orientation(t[0], t[1], p) != sign_t::negative diff --git a/libs/geom/include/psemek/geom/gauss.hpp b/libs/geom/include/psemek/geom/gauss.hpp index 2d1f28b3..479d5de2 100644 --- a/libs/geom/include/psemek/geom/gauss.hpp +++ b/libs/geom/include/psemek/geom/gauss.hpp @@ -16,15 +16,15 @@ namespace psemek::geom struct gauss_helper; template - gauss_helper (X &) -> gauss_helper; + gauss_helper(X &) -> gauss_helper; template struct gauss_helper> { vector & v; - static constexpr std::size_t columns ( ) { return 1; } - T & at (std::size_t row, std::size_t) { return v[row]; } + static constexpr std::size_t columns() { return 1; } + T & at(std::size_t row, std::size_t) { return v[row]; } }; template @@ -32,12 +32,12 @@ namespace psemek::geom { matrix & m; - static constexpr std::size_t columns ( ) { return M; } - T & at (std::size_t row, std::size_t col) { return m[row][col]; } + static constexpr std::size_t columns() { return M; } + T & at(std::size_t row, std::size_t col) { return m[row][col]; } }; template - void for_each (F && f, Args & ... args) + void for_each(F && f, Args & ... args) { (f(args), ...); } @@ -45,7 +45,7 @@ namespace psemek::geom } template - T det (matrix m) + T det(matrix m) { using std::abs; @@ -101,7 +101,7 @@ namespace psemek::geom // the set of equations (m * x = rhs)... is solved simultaneously column-wise // returns false is the matrix is degenerate template - bool gauss (matrix m, RHS & ... rhs) + bool gauss(matrix m, RHS & ... rhs) { using std::abs; @@ -198,7 +198,7 @@ namespace psemek::geom } template - std::optional> inverse (matrix m) + std::optional> inverse(matrix m) { matrix r = matrix::identity(); if (!gauss(m, r)) @@ -208,7 +208,7 @@ namespace psemek::geom // Least-squares solution of m*x=b with full-rank m template - std::optional> least_squares (matrix const & m, vector const & b) + std::optional> least_squares(matrix const & m, vector const & b) { if constexpr (N == M) { diff --git a/libs/geom/include/psemek/geom/incircle.hpp b/libs/geom/include/psemek/geom/incircle.hpp index c401fc6e..7491724f 100644 --- a/libs/geom/include/psemek/geom/incircle.hpp +++ b/libs/geom/include/psemek/geom/incircle.hpp @@ -13,9 +13,10 @@ namespace psemek::geom template std::enable_if_t, sign_t> - in_circle (point const & p0, point const & p1, point const & p2, point const & p3) + in_circle(point const & p0, point const & p1, point const & p2, point const & p3) { - auto proj = [](point const & p){ + auto proj = [](point const & p) + { auto const x = p[0]; auto const y = p[1]; return point{ x, y, x*x + y*y }; @@ -26,7 +27,7 @@ namespace psemek::geom template std::enable_if_t, sign_t> - in_circle (point const & p0, point const & p1, point const & p2, point const & p3) + in_circle(point const & p0, point const & p1, point const & p2, point const & p3) { constexpr T error = std::numeric_limits::epsilon() * T(29) / T(2); diff --git a/libs/geom/include/psemek/geom/intersection.hpp b/libs/geom/include/psemek/geom/intersection.hpp index 3d4790cc..81519de3 100644 --- a/libs/geom/include/psemek/geom/intersection.hpp +++ b/libs/geom/include/psemek/geom/intersection.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -10,17 +12,8 @@ namespace psemek::geom { - // denotes empty intersection - struct empty { }; - - template - Stream & operator << (Stream & os, empty) - { - return os << "empty"; - } - template > - bool intersect (segment const & s0, segment const & s1) + bool intersect(segment const & s0, segment const & s1) { auto const o00 = orientation(s0[0], s0[1], s1[0]); auto const o01 = orientation(s0[0], s0[1], s1[1]); @@ -32,7 +25,7 @@ namespace psemek::geom // TODO: robust implementation template > - std::variant> intersection (segment s0, segment s1) + std::variant> intersection(segment s0, segment s1) { auto const a0 = -det(s1[0] - s0[0], s1[1] - s1[0]); auto const a1 = det(s0[1] - s0[0], s1[0] - s0[0]); @@ -46,7 +39,7 @@ namespace psemek::geom auto const t1 = a1 / b; if (t0 < 0 || t0 > 1 || t1 < 0 || t1 > 1) - return empty{}; + return util::empty{}; return s0[0] + t0 * (s0[1] - s0[0]); } @@ -57,7 +50,7 @@ namespace psemek::geom if (a0 != 0) { // segments do not lie on the same line: no intersection - return empty{}; + return util::empty{}; } // if segments are not Y-axis aligned, safe to use X-coordinates to sort them (k = 0) @@ -74,7 +67,7 @@ namespace psemek::geom auto const r1 = std::min(s0[1][k], s1[1][k]); if (r0 > r1) - return empty{}; + return util::empty{}; bool const s0_is_first = s0[0][k] < s1[0][k]; diff --git a/libs/geom/include/psemek/geom/interval.hpp b/libs/geom/include/psemek/geom/interval.hpp index 51f2569e..a4cd4550 100644 --- a/libs/geom/include/psemek/geom/interval.hpp +++ b/libs/geom/include/psemek/geom/interval.hpp @@ -11,7 +11,7 @@ namespace psemek::geom template struct limits { - static constexpr T min ( ) + static constexpr T min() { if constexpr (std::is_floating_point_v) { @@ -23,7 +23,7 @@ namespace psemek::geom } } - static constexpr T max ( ) + static constexpr T max() { if constexpr (std::is_floating_point_v) { @@ -42,15 +42,15 @@ namespace psemek::geom { T value; - T operator* () const { return value; } + T operator*() const { return value; } - interval_iterator & operator++ () + interval_iterator & operator++() { ++value; return *this; } - interval_iterator operator++ (int) + interval_iterator operator++(int) { auto copy = *this; ++value; @@ -74,17 +74,17 @@ namespace psemek::geom T min = limits::max(); T max = limits::min(); - static interval singleton (T const & value) + static interval singleton(T const & value) { return {value, value}; } - bool empty ( ) const + bool empty() const { return min > max; } - T length ( ) const + T length() const { return empty() ? T{} : max - min; } @@ -208,7 +208,7 @@ namespace psemek::geom } template - T clamp (T x, interval const & i) + T clamp(T x, interval const & i) { return std::max(i.min, std::min(i.max, x)); } diff --git a/libs/geom/include/psemek/geom/matrix.hpp b/libs/geom/include/psemek/geom/matrix.hpp index 2f7f236b..0d2a6d4b 100644 --- a/libs/geom/include/psemek/geom/matrix.hpp +++ b/libs/geom/include/psemek/geom/matrix.hpp @@ -17,12 +17,12 @@ namespace psemek::geom T coords[R * C]; - T * operator [] (std::size_t i) + T * operator[](std::size_t i) { return coords + C * i; } - T const * operator [] (std::size_t i) const + T const * operator[](std::size_t i) const { return coords + C * i; } @@ -33,14 +33,14 @@ namespace psemek::geom matrix & operator += (matrix const & v); matrix & operator -= (matrix const & v); - static matrix zero ( ); - static matrix identity ( ); - static matrix scalar (T const & s); + static matrix zero(); + static matrix identity(); + static matrix scalar(T const & s); }; template - matrix matrix::zero ( ) + matrix matrix::zero() { matrix m; for (std::size_t i = 0; i < R * C; ++i) @@ -49,13 +49,13 @@ namespace psemek::geom } template - matrix matrix::identity ( ) + matrix matrix::identity() { return scalar(T{1}); } template - matrix matrix::scalar (T const & s) + matrix matrix::scalar(T const & s) { matrix m = zero(); for (std::size_t i = 0; i < std::min(R, C); ++i) @@ -101,7 +101,7 @@ namespace psemek::geom } template - matrix cast (matrix const & m) + matrix cast(matrix const & m) { matrix r; for (std::size_t i = 0; i < R * C; ++i) @@ -226,7 +226,7 @@ namespace psemek::geom } template - matrix transpose (matrix const & m) + matrix transpose(matrix const & m) { matrix r; for (std::size_t i = 0; i < R; ++i) diff --git a/libs/geom/include/psemek/geom/orientation.hpp b/libs/geom/include/psemek/geom/orientation.hpp index 158a05e4..41d9a452 100644 --- a/libs/geom/include/psemek/geom/orientation.hpp +++ b/libs/geom/include/psemek/geom/orientation.hpp @@ -15,7 +15,7 @@ namespace psemek::geom // TODO: generic implementation template std::enable_if_t, sign_t> - orientation (point const & p0, point const & p1, point const & p2) + orientation(point const & p0, point const & p1, point const & p2) { T const d = det(p1 - p0, p2 - p0); @@ -29,7 +29,7 @@ namespace psemek::geom template std::enable_if_t, sign_t> - orientation (point const & p0, point const & p1, point const & p2) + orientation(point const & p0, point const & p1, point const & p2) { constexpr T error = std::numeric_limits::epsilon() * T(5) / T(2); @@ -52,7 +52,7 @@ namespace psemek::geom } template - sign_t orientation (point const & p0, point const & p1, point const & p2, point const & p3) + sign_t orientation(point const & p0, point const & p1, point const & p2, point const & p3) { T const d = det(p0 - p3, p1 - p3, p2 - p3); diff --git a/libs/geom/include/psemek/geom/orthographic.hpp b/libs/geom/include/psemek/geom/orthographic.hpp index ed87fd65..e9e303c4 100644 --- a/libs/geom/include/psemek/geom/orthographic.hpp +++ b/libs/geom/include/psemek/geom/orthographic.hpp @@ -12,10 +12,10 @@ namespace psemek::geom template struct orthographic { - using vector_type = vector; - using point_type = point; - using homogeneous_matrix_type = matrix; - using box_type = box; + using vector_type = geom::vector; + using point_type = geom::point; + using homogeneous_matrix_type = geom::matrix; + using box_type = geom::box; orthographic(); orthographic(box_type r); @@ -46,7 +46,7 @@ namespace psemek::geom template orthographic::orthographic(box_type r) : r_(r) - { } + {} template box orthographic::box() const @@ -89,4 +89,20 @@ namespace psemek::geom return m; } + template + orthographic orthographic::inverse() const + { + auto inv = [](interval const & i) + { + auto const s = i.length(); + auto const d = i.max - i.min; + return interval{ (- s - 2) / d, (- s + 2) / d }; + }; + + box_type inv_box; + for (std::size_t d = 0; d < D; ++d) + inv_box[d] = inv(r_[d]); + return {inv_box}; + } + } diff --git a/libs/geom/include/psemek/geom/permutation.hpp b/libs/geom/include/psemek/geom/permutation.hpp index 8752f6dd..a12f0efe 100644 --- a/libs/geom/include/psemek/geom/permutation.hpp +++ b/libs/geom/include/psemek/geom/permutation.hpp @@ -10,13 +10,13 @@ namespace psemek::geom template struct swap { - using vector_type = vector; - using point_type = point; - using matrix_type = matrix; + using vector_type = geom::vector; + using point_type = geom::point; + using matrix_type = geom::matrix; swap(std::size_t i, std::size_t j); - vector_type operator() (vector_type v) const; + vector_type operator()(vector_type v) const; matrix_type matrix() const; @@ -27,13 +27,13 @@ namespace psemek::geom }; template - swap::swap (std::size_t i, std::size_t j) + swap::swap(std::size_t i, std::size_t j) : i_(i) , j_(j) - { } + {} template - vector swap::operator() (vector_type v) const + vector swap::operator()(vector_type v) const { std::swap(v[i_], v[j_]); return v; diff --git a/libs/geom/include/psemek/geom/perspective.hpp b/libs/geom/include/psemek/geom/perspective.hpp index a2c77c24..b1626de0 100644 --- a/libs/geom/include/psemek/geom/perspective.hpp +++ b/libs/geom/include/psemek/geom/perspective.hpp @@ -16,9 +16,9 @@ namespace psemek::geom using homogeneous_matrix_type = matrix; // fov are in radians - perspective (scalar_type fov_x, scalar_type fov_y, scalar_type near, scalar_type far); + perspective(scalar_type fov_x, scalar_type fov_y, scalar_type near, scalar_type far); - perspective (scalar_type left, scalar_type right, scalar_type bottom, scalar_type top, scalar_type near, scalar_type far); + perspective(scalar_type left, scalar_type right, scalar_type bottom, scalar_type top, scalar_type near, scalar_type far); homogeneous_matrix_type homogeneous_matrix() const; diff --git a/libs/geom/include/psemek/geom/point.hpp b/libs/geom/include/psemek/geom/point.hpp index 1ba89db0..8771e0c3 100644 --- a/libs/geom/include/psemek/geom/point.hpp +++ b/libs/geom/include/psemek/geom/point.hpp @@ -14,27 +14,27 @@ namespace psemek::geom T coords[N]; - point ( ) = default; - point (point const &) = default; - point (point &) = default; - point (point &&) = default; + point() = default; + point(point const &) = default; + point(point &) = default; + point(point &&) = default; point & operator = (point const &) = default; point & operator = (point &) = default; point & operator = (point &&) = default; template - point (Args && ... args) + point(Args && ... args) : coords{ std::forward(args)... } { static_assert(sizeof...(Args) == N); } - T & operator[] (std::size_t i) + T & operator[](std::size_t i) { return coords[i]; } - T const & operator[] (std::size_t i) const + T const & operator[](std::size_t i) const { return coords[i]; } @@ -44,7 +44,7 @@ namespace psemek::geom }; template - point (Args && ...) -> point, sizeof...(Args)>; + point(Args && ...) -> point, sizeof...(Args)>; template bool operator == (point const & p1, point const & p2) @@ -86,7 +86,7 @@ namespace psemek::geom } template - point cast (point const & p) + point cast(point const & p) { point r; for (std::size_t i = 0; i < N; ++i) @@ -143,19 +143,19 @@ namespace psemek::geom } template - point lerp (point const & p0, point const & p1, T const & t) + point lerp(point const & p0, point const & p1, T const & t) { return p0 + t * (p1 - p0); } template - T distance_sqr (point const & p1, point const & p2) + T distance_sqr(point const & p1, point const & p2) { return length_sqr(p2 - p1); } template - T distance (point const & p1, point const & p2) + T distance(point const & p1, point const & p2) { return length(p2 - p1); } diff --git a/libs/geom/include/psemek/geom/rotation.hpp b/libs/geom/include/psemek/geom/rotation.hpp index 54f2f50d..82e3324a 100644 --- a/libs/geom/include/psemek/geom/rotation.hpp +++ b/libs/geom/include/psemek/geom/rotation.hpp @@ -22,7 +22,7 @@ namespace psemek::geom scalar_type angle() const; scalar_type angle(scalar_type a); - vector_type operator() (vector_type v) const; + vector_type operator()(vector_type v) const; matrix_type matrix() const; @@ -52,7 +52,7 @@ namespace psemek::geom scalar_type angle() const; scalar_type angle(scalar_type a); - vector_type operator() (vector_type v) const; + vector_type operator()(vector_type v) const; matrix_type matrix() const; @@ -66,7 +66,7 @@ namespace psemek::geom template plane_rotation::plane_rotation(std::size_t i, std::size_t j, T angle) : i_(i), j_(j), angle_(angle) - { } + {} template T plane_rotation::angle() const @@ -83,7 +83,7 @@ namespace psemek::geom } template - vector plane_rotation::operator() (vector_type v) const + vector plane_rotation::operator()(vector_type v) const { T vi = v[i_] * std::cos(angle_) - v[j_] * std::sin(angle_); T vj = v[i_] * std::sin(angle_) + v[j_] * std::cos(angle_); @@ -112,13 +112,13 @@ namespace psemek::geom template axis_rotation::axis_rotation() : axis_rotation(vector_type{T(0), T(0), T(1)}, T(0)) - { } + {} template axis_rotation::axis_rotation(vector_type axis, scalar_type angle) : axis_(axis) , angle_(angle) - { } + {} template vector axis_rotation::axis() const @@ -135,7 +135,7 @@ namespace psemek::geom } template - T axis_rotation::angle ( ) const + T axis_rotation::angle() const { return angle_; } @@ -149,7 +149,7 @@ namespace psemek::geom } template - vector axis_rotation::operator() (vector_type v) const + vector axis_rotation::operator()(vector_type v) const { return matrix() * v; } diff --git a/libs/geom/include/psemek/geom/scale.hpp b/libs/geom/include/psemek/geom/scale.hpp index 0181b82c..4a5feadb 100644 --- a/libs/geom/include/psemek/geom/scale.hpp +++ b/libs/geom/include/psemek/geom/scale.hpp @@ -10,9 +10,9 @@ namespace psemek::geom template struct scale { - using vector_type = vector; - using point_type = point; - using matrix_type = matrix; + using vector_type = geom::vector; + using point_type = geom::point; + using matrix_type = geom::matrix; scale(); scale(T v); @@ -21,7 +21,7 @@ namespace psemek::geom vector_type vector() const; vector_type vector(vector_type v); - vector_type operator() (vector_type v) const; + vector_type operator()(vector_type v) const; matrix_type matrix() const; @@ -48,7 +48,7 @@ namespace psemek::geom template scale::scale(vector_type v) : v_(v) - { } + {} template vector scale::vector() const @@ -65,7 +65,7 @@ namespace psemek::geom } template - vector scale::operator() (vector_type v) const + vector scale::operator()(vector_type v) const { for (std::size_t i = 0; i < D; ++i) v[i] *= v_[i]; diff --git a/libs/geom/include/psemek/geom/simplex.hpp b/libs/geom/include/psemek/geom/simplex.hpp index a9f10522..977548d5 100644 --- a/libs/geom/include/psemek/geom/simplex.hpp +++ b/libs/geom/include/psemek/geom/simplex.hpp @@ -14,35 +14,35 @@ namespace psemek::geom point_type points[K + 1]; - point_type & operator[] (std::size_t i) + point_type & operator[](std::size_t i) { return points[i]; } - point_type const & operator[] (std::size_t i) const + point_type const & operator[](std::size_t i) const { return points[i]; } }; template - simplex (Args ...) -> simplex, sizeof...(Args) - 1>; + simplex(Args ...) -> simplex, sizeof...(Args) - 1>; template struct segment : simplex - { }; + {}; template - segment (Point, Point) -> segment; + segment(Point, Point) -> segment; template struct triangle : simplex - { }; + {}; template - triangle (Point, Point, Point) -> triangle; + triangle(Point, Point, Point) -> triangle; template Stream & operator << (Stream & os, simplex const & s) diff --git a/libs/geom/include/psemek/geom/translation.hpp b/libs/geom/include/psemek/geom/translation.hpp index 562d7d3e..7fbebe7f 100644 --- a/libs/geom/include/psemek/geom/translation.hpp +++ b/libs/geom/include/psemek/geom/translation.hpp @@ -20,8 +20,8 @@ namespace psemek::geom vector_type vector() const; vector_type vector(vector_type v); - vector_type operator() (vector_type v) const; - point_type operator() (point_type p) const; + vector_type operator()(vector_type v) const; + point_type operator()(point_type p) const; homogeneous_matrix_type homogeneous_matrix() const; @@ -34,12 +34,12 @@ namespace psemek::geom template translation::translation() : translation(vector_type::zero()) - { } + {} template translation::translation(vector_type v) : v_(v) - { } + {} template vector translation::vector() const @@ -56,13 +56,13 @@ namespace psemek::geom } template - vector translation::operator() (vector_type v) const + vector translation::operator()(vector_type v) const { return v; } template - point translation::operator() (point_type p) const + point translation::operator()(point_type p) const { return p + v_; } diff --git a/libs/geom/include/psemek/geom/vector.hpp b/libs/geom/include/psemek/geom/vector.hpp index 3229920b..516730a3 100644 --- a/libs/geom/include/psemek/geom/vector.hpp +++ b/libs/geom/include/psemek/geom/vector.hpp @@ -17,27 +17,27 @@ namespace psemek::geom T coords[N]; - vector ( ) = default; - vector (vector const &) = default; - vector (vector &) = default; - vector (vector &&) = default; + vector() = default; + vector(vector const &) = default; + vector(vector &) = default; + vector(vector &&) = default; vector & operator = (vector const &) = default; vector & operator = (vector &) = default; vector & operator = (vector &&) = default; template - vector (Args && ... args) + vector(Args && ... args) : coords{ std::forward(args)... } { static_assert(sizeof...(Args) == N); } - T & operator[] (std::size_t i) + T & operator[](std::size_t i) { return coords[i]; } - T const & operator[] (std::size_t i) const + T const & operator[](std::size_t i) const { return coords[i]; } @@ -52,7 +52,7 @@ namespace psemek::geom }; template - vector (Args && ...) -> vector, sizeof...(Args)>; + vector(Args && ...) -> vector, sizeof...(Args)>; template bool operator == (vector const & v1, vector const & v2) @@ -91,7 +91,7 @@ namespace psemek::geom } template - vector cast (vector const & v) + vector cast(vector const & v) { vector r; for (std::size_t i = 0; i < N; ++i) @@ -187,7 +187,7 @@ namespace psemek::geom } template - T dot (vector const & v1, vector const & v2) + T dot(vector const & v1, vector const & v2) { T r{}; for (std::size_t i = 0; i < N; ++i) @@ -196,32 +196,32 @@ namespace psemek::geom } template - T length_sqr (vector const & v) + T length_sqr(vector const & v) { return dot(v, v); } template - T length (vector const & v) + T length(vector const & v) { return std::sqrt(length_sqr(v)); } template - vector normalized (vector const & v) + vector normalized(vector const & v) { return v / length(v); } // TODO: generic implementation template - T det (vector const & v0, vector const & v1) + T det(vector const & v0, vector const & v1) { return v0[0] * v1[1] - v0[1] * v1[0]; } template - T det (vector const & v0, vector const & v1, vector const & v2) + T det(vector const & v0, vector const & v1, vector const & v2) { return + v0[0] * v1[1] * v2[2] @@ -234,7 +234,7 @@ namespace psemek::geom } template - vector cross (vector const & v0, vector const & v1) + vector cross(vector const & v0, vector const & v1) { return vector{ v0[1] * v1[2] - v0[2] * v1[1], @@ -244,13 +244,13 @@ namespace psemek::geom } template - T lerp (T const & x0, T const & x1, T const & t) + T lerp(T const & x0, T const & x1, T const & t) { return x0 * (T(1) - t) + x1 * t; } template - vector lerp (vector const & v0, vector const & v1, T const & t) + vector lerp(vector const & v0, vector const & v1, T const & t) { return v0 * (T(1) - t) + v1 * t; } diff --git a/libs/pcg/include/psemek/pcg/perlin.hpp b/libs/pcg/include/psemek/pcg/perlin.hpp index 12ac55e5..a3c91ed0 100644 --- a/libs/pcg/include/psemek/pcg/perlin.hpp +++ b/libs/pcg/include/psemek/pcg/perlin.hpp @@ -12,7 +12,7 @@ namespace psemek::pcg perlin(util::basic_pixmap> grad_map); perlin(perlin &&) = default; - perlin & operator= (perlin &&) = default; + perlin & operator = (perlin &&) = default; std::size_t width() const { @@ -26,7 +26,7 @@ namespace psemek::pcg // x \in [0.0 .. 1.0] // y \in [0.0 .. 1.0] - float operator() (float x, float y) const; + float operator()(float x, float y) const; private: util::basic_pixmap> grad_map_; diff --git a/libs/pcg/source/perlin.cpp b/libs/pcg/source/perlin.cpp index 0cfedda2..fc3f130d 100644 --- a/libs/pcg/source/perlin.cpp +++ b/libs/pcg/source/perlin.cpp @@ -22,7 +22,7 @@ namespace psemek::pcg return step(x0, x1, s); } - float perlin::operator() (float x, float y) const + float perlin::operator()(float x, float y) const { assert(x >= 0.f); assert(y >= 0.f); diff --git a/libs/util/include/psemek/util/assert.hpp b/libs/util/include/psemek/util/assert.hpp index d4c43f96..1a6a8d24 100644 --- a/libs/util/include/psemek/util/assert.hpp +++ b/libs/util/include/psemek/util/assert.hpp @@ -13,6 +13,6 @@ namespace psemek::util { - [[noreturn]] bool assert_handler (char const * expression, char const * file, int line); + [[noreturn]] bool assert_handler(char const * expression, char const * file, int line); } diff --git a/libs/util/include/psemek/util/blob.hpp b/libs/util/include/psemek/util/blob.hpp index b27f12c1..2eafccab 100644 --- a/libs/util/include/psemek/util/blob.hpp +++ b/libs/util/include/psemek/util/blob.hpp @@ -20,7 +20,7 @@ namespace psemek::util blob & operator = (blob const & other); blob & operator = (blob && other); - ~ blob() = default; + ~blob() = default; char * data() { return data_.get(); } char const * data() const { return data_.get(); } @@ -39,8 +39,8 @@ namespace psemek::util char * end() { return data() + size(); } char const * end() const { return data() + size(); } - char & operator[] (std::size_t i) { return data()[i]; } - char const & operator[] (std::size_t i) const { return data()[i]; } + char & operator[](std::size_t i) { return data()[i]; } + char const & operator[](std::size_t i) const { return data()[i]; } std::string string() const; std::string_view string_view() const; diff --git a/libs/util/include/psemek/util/clock.hpp b/libs/util/include/psemek/util/clock.hpp index 2ba74f2f..112672ab 100644 --- a/libs/util/include/psemek/util/clock.hpp +++ b/libs/util/include/psemek/util/clock.hpp @@ -14,17 +14,17 @@ namespace psemek::util typedef Clock clock_type; typedef typename clock_type::time_point time_point_type; - clock ( ) + clock() { restart(); } - time_point_type now ( ) const + time_point_type now() const { return clock_type::now(); } - duration_type restart ( ) + duration_type restart() { auto t = now(); auto delta = t - start_; @@ -32,12 +32,12 @@ namespace psemek::util return std::chrono::duration_cast(delta); } - duration_type duration ( ) const + duration_type duration() const { return std::chrono::duration_cast(now() - start_); } - rep_type count ( ) const + rep_type count() const { return duration().count(); } diff --git a/libs/util/include/psemek/util/ebo.hpp b/libs/util/include/psemek/util/ebo.hpp new file mode 100644 index 00000000..a6fd654f --- /dev/null +++ b/libs/util/include/psemek/util/ebo.hpp @@ -0,0 +1,40 @@ +#pragma once + +namespace psemek::util +{ + + namespace detail + { + + template + struct ebo_helper_impl : T + { + T & data() { return *this; } + T const & data() const { return *this; } + + template + ebo_helper_impl(Args && ... args) + : T(std::forward(args)...) + {} + }; + + template + struct ebo_helper_impl + { + T value; + + T & data() { return value; } + T const & data() const { return value; } + + template + ebo_helper_impl(Args && ... args) + : value(std::forward(args)...) + {} + }; + + } + + template + using ebo_helper = detail::ebo_helper_impl && !(std::is_final_v)>; + +} diff --git a/libs/util/include/psemek/util/empty.hpp b/libs/util/include/psemek/util/empty.hpp new file mode 100644 index 00000000..b84d4264 --- /dev/null +++ b/libs/util/include/psemek/util/empty.hpp @@ -0,0 +1,14 @@ +#pragma once + +namespace psemek::util +{ + + struct empty{}; + + template + Stream & operator << (Stream & os, empty) + { + return os << "empty"; + } + +} diff --git a/libs/util/include/psemek/util/flat_list.hpp b/libs/util/include/psemek/util/flat_list.hpp index 8c00093d..7ec30713 100644 --- a/libs/util/include/psemek/util/flat_list.hpp +++ b/libs/util/include/psemek/util/flat_list.hpp @@ -10,16 +10,16 @@ namespace psemek::util template struct flat_list { - flat_list ( ); - explicit flat_list (std::size_t count); - flat_list (std::size_t count, T const & value); - flat_list (flat_list const &); - flat_list (flat_list &&); - flat_list (std::initializer_list init); + flat_list(); + explicit flat_list(std::size_t count); + flat_list(std::size_t count, T const & value); + flat_list(flat_list const &); + flat_list(flat_list &&); + flat_list(std::initializer_list init); - ~ flat_list ( ); + ~flat_list(); - std::size_t size ( ) const; + std::size_t size() const; private: using item = std::aligned_storage_t; @@ -33,22 +33,22 @@ namespace psemek::util }; template - flat_list::flat_list ( ) = default; + flat_list::flat_list() = default; template - flat_list::flat_list (std::size_t count) + flat_list::flat_list(std::size_t count) : data_(count) , size_(count) - { } + {} template - flat_list::flat_list (std::size_t count, T const & value) + flat_list::flat_list(std::size_t count, T const & value) : data_(count, value) , size_(count) - { } + {} template - flat_list::flat_list (flat_list const & other) = default; + flat_list::flat_list(flat_list const & other) = default; template flat_list::flat_list (flat_list && other) @@ -61,16 +61,16 @@ namespace psemek::util } template - flat_list::flat_list (std::initializer_list init) + flat_list::flat_list(std::initializer_list init) : data_(std::move(init)) , size_(data_.size()) - { } + {} template - flat_list::~flat_list ( ) = default; + flat_list::~flat_list() = default; template - std::size_t flat_list::size ( ) const + std::size_t flat_list::size() const { return size_; } diff --git a/libs/util/include/psemek/util/fmap.hpp b/libs/util/include/psemek/util/fmap.hpp index 73d81957..ba26114b 100644 --- a/libs/util/include/psemek/util/fmap.hpp +++ b/libs/util/include/psemek/util/fmap.hpp @@ -16,7 +16,7 @@ namespace psemek::util F f; template - auto operator() (std::optional && x) + auto operator()(std::optional && x) { using R = decltype(f(*x)); @@ -30,7 +30,7 @@ namespace psemek::util } template - auto fmap (F f) + auto fmap(F f) { return detail::fmap{std::move(f)}; } diff --git a/libs/util/include/psemek/util/functional.hpp b/libs/util/include/psemek/util/functional.hpp index e0ebb61e..dd6fcfd7 100644 --- a/libs/util/include/psemek/util/functional.hpp +++ b/libs/util/include/psemek/util/functional.hpp @@ -8,13 +8,13 @@ namespace psemek::util constexpr auto id = [](auto && x) -> decltype(auto) { return x; }; template - auto constant (T const & x) + auto constant(T const & x) { return [x](auto const & ...){ return x; }; } template - auto bind_and (F1 && f1, F2 && f2) + auto bind_and(F1 && f1, F2 && f2) { return [=](auto const &... args){ return f1(args...) && f2(args...); diff --git a/libs/util/include/psemek/util/lazy_range.hpp b/libs/util/include/psemek/util/lazy_range.hpp index 89f02646..afe60ece 100644 --- a/libs/util/include/psemek/util/lazy_range.hpp +++ b/libs/util/include/psemek/util/lazy_range.hpp @@ -10,9 +10,9 @@ namespace psemek::util { Gen generator; - lazy_range (Gen gen) + lazy_range(Gen gen) : generator(std::move(gen)) - { } + {} using value_type = decltype(generator()); @@ -24,21 +24,21 @@ namespace psemek::util value_type value; - value_type const & operator * () + value_type const & operator*() { return value; } - iterator & operator ++ () + iterator & operator++() { value = range.generator(); return *this; } }; - iterator begin ( ) + iterator begin() { - return { *this, generator() }; + return {*this, generator()}; } struct sentinel @@ -46,7 +46,7 @@ namespace psemek::util using iterator_category = std::input_iterator_tag; }; - sentinel end ( ) + sentinel end() { return {}; } diff --git a/libs/util/include/psemek/util/movable_function.hpp b/libs/util/include/psemek/util/movable_function.hpp index 43b8eeca..efe8b20a 100644 --- a/libs/util/include/psemek/util/movable_function.hpp +++ b/libs/util/include/psemek/util/movable_function.hpp @@ -14,9 +14,9 @@ namespace psemek::util template struct movable_function_node_base { - virtual R call (Args ...) = 0; + virtual R call(Args ...) = 0; - virtual ~ movable_function_node_base ( ) = default; + virtual ~movable_function_node_base() = default; }; template @@ -28,18 +28,18 @@ namespace psemek::util { F f; - movable_function_node (F && f) + movable_function_node(F && f) : f(std::move(f)) - { } + {} - R call (Args ... args) override + R call(Args ... args) override { return f(args...); } }; // Implemented in cpp to prevent dependency on - [[noreturn]] void bad_function_call ( ); + [[noreturn]] void bad_function_call(); } @@ -51,12 +51,12 @@ namespace psemek::util { using signature = R(Args...); - movable_function ( ) = default; + movable_function() = default; template - movable_function (F f) - : p { std::make_unique>>(std::move(f)) } - { } + movable_function(F f) + : p {std::make_unique>>(std::move(f))} + {} movable_function (movable_function &&) = default; movable_function & operator = (movable_function &&) = default; @@ -64,12 +64,12 @@ namespace psemek::util movable_function (movable_function const &) = delete; movable_function & operator = (movable_function const &) = delete; - explicit operator bool ( ) const + explicit operator bool() const { return static_cast(p); } - R operator() (Args ... args) const + R operator()(Args ... args) const { if (!p) detail::bad_function_call(); diff --git a/libs/util/include/psemek/util/moving_average.hpp b/libs/util/include/psemek/util/moving_average.hpp index e3e62fcf..a3d99885 100644 --- a/libs/util/include/psemek/util/moving_average.hpp +++ b/libs/util/include/psemek/util/moving_average.hpp @@ -8,21 +8,21 @@ namespace psemek::util template struct moving_average { - moving_average (std::size_t max) + moving_average(std::size_t max) : data_(max, T()) , begin_(0) , size_(0) , sum_(T()) - { } + {} - void clear ( ) + void clear() { begin_ = 0; size_ = 0; sum_ = T(0); } - void push (T x) + void push(T x) { if (size_ >= data_.size()) { @@ -38,17 +38,17 @@ namespace psemek::util begin_ = 0; } - T sum ( ) const + T sum() const { return sum_; } - std::size_t count ( ) const + std::size_t count() const { return size_; } - T average ( ) const + T average() const { return sum() / count(); } diff --git a/libs/util/include/psemek/util/noncopyable.hpp b/libs/util/include/psemek/util/noncopyable.hpp deleted file mode 100644 index cfd90573..00000000 --- a/libs/util/include/psemek/util/noncopyable.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -namespace psemek::util -{ - - struct noncopyable - { - noncopyable (noncopyable const &) = delete; - noncopyable & operator = (noncopyable const &) = delete; - - noncopyable ( ) = default; - noncopyable (noncopyable &&) = default; - noncopyable & operator = (noncopyable &&) = default; - }; - -} diff --git a/libs/util/include/psemek/util/not_implemented.hpp b/libs/util/include/psemek/util/not_implemented.hpp index 530ef7ba..365492fb 100644 --- a/libs/util/include/psemek/util/not_implemented.hpp +++ b/libs/util/include/psemek/util/not_implemented.hpp @@ -3,6 +3,6 @@ namespace psemek::util { - void not_implemented ( ); + void not_implemented(); } diff --git a/libs/util/include/psemek/util/overload.hpp b/libs/util/include/psemek/util/overload.hpp index c5b29b4b..36ef9f65 100644 --- a/libs/util/include/psemek/util/overload.hpp +++ b/libs/util/include/psemek/util/overload.hpp @@ -12,9 +12,9 @@ namespace psemek::util struct overload_impl : Fs... { - overload_impl (Fs ... fs) + overload_impl(Fs ... fs) : Fs(fs)... - { } + {} using Fs::operator()...; }; @@ -22,7 +22,7 @@ namespace psemek::util } template - auto overload (Fs ... fs) + auto overload(Fs ... fs) { return detail::overload_impl{std::move(fs)...}; } diff --git a/libs/util/include/psemek/util/pimpl.hpp b/libs/util/include/psemek/util/pimpl.hpp index e18ee25d..187da9b9 100644 --- a/libs/util/include/psemek/util/pimpl.hpp +++ b/libs/util/include/psemek/util/pimpl.hpp @@ -14,22 +14,22 @@ namespace psemek::util::pimpl { protected: - using impl_type = ::util::pimpl::impl; + using impl_type = pimpl::impl; - impl_type * pimpl ( ) { return reinterpret_cast(std::addressof(storage_)); } - impl_type & impl ( ) { return *pimpl(); } + impl_type * pimpl() { return reinterpret_cast(std::addressof(storage_)); } + impl_type & impl() { return *pimpl(); } - impl_type const * pimpl ( ) const { return reinterpret_cast(std::addressof(storage_)); } - impl_type const & impl ( ) const { return *pimpl(); } + impl_type const * pimpl() const { return reinterpret_cast(std::addressof(storage_)); } + impl_type const & impl() const { return *pimpl(); } template - in_place (Args && ... args) + in_place(Args && ... args) { static_assert(sizeof(impl_type) <= Size, "impl storage size too small"); new (pimpl()) impl_type (std::forward(args)...); } - ~ in_place ( ) + ~in_place() { impl().~impl_type(); } @@ -44,21 +44,21 @@ namespace psemek::util::pimpl { protected: - using impl_type = ::util::pimpl::impl; + using impl_type = pimpl::impl; - impl_type * pimpl ( ) { return pointer_.get(); } - impl_type & impl ( ) { return *pimpl(); } + impl_type * pimpl() { return pointer_.get(); } + impl_type & impl() { return *pimpl(); } - impl_type const * pimpl ( ) const { return pointer_.get(); } - impl_type const & impl ( ) const { return *pimpl(); } + impl_type const * pimpl() const { return pointer_.get(); } + impl_type const & impl() const { return *pimpl(); } template - dynamic (Args && ... args) + dynamic(Args && ... args) { pointer_.reset(new impl_type(std::forward(args)...)); } - ~ dynamic ( ) = default; + ~dynamic() = default; private: diff --git a/libs/util/include/psemek/util/pixmap.hpp b/libs/util/include/psemek/util/pixmap.hpp index ac95eba8..8ac38617 100644 --- a/libs/util/include/psemek/util/pixmap.hpp +++ b/libs/util/include/psemek/util/pixmap.hpp @@ -28,8 +28,8 @@ namespace psemek::util void resize(std::size_t width, std::size_t height); void resize(std::size_t width, std::size_t height, Pixel value); - Pixel * operator[] (std::size_t row); - Pixel const * operator[] (std::size_t row) const; + Pixel * operator[](std::size_t row); + Pixel const * operator[](std::size_t row) const; Pixel * data() { return data_.get(); } Pixel const * data() const { return data_.get(); } @@ -124,13 +124,13 @@ namespace psemek::util } template - Pixel * basic_pixmap::operator[] (std::size_t row) + Pixel * basic_pixmap::operator[](std::size_t row) { return data() + row * width(); } template - Pixel const * basic_pixmap::operator[] (std::size_t row) const + Pixel const * basic_pixmap::operator[](std::size_t row) const { return data() + row * width(); } diff --git a/libs/util/include/psemek/util/pretty_print.hpp b/libs/util/include/psemek/util/pretty_print.hpp index 9708f625..4bde9c97 100644 --- a/libs/util/include/psemek/util/pretty_print.hpp +++ b/libs/util/include/psemek/util/pretty_print.hpp @@ -16,7 +16,7 @@ namespace psemek::util UpTo up_to; }; - void pretty_print_time (std::ostream & o, std::int64_t d, std::int64_t up_to); + void pretty_print_time(std::ostream & o, std::int64_t d, std::int64_t up_to); template std::ostream & operator << (std::ostream & o, pretty_print_time_wrapper t) @@ -30,13 +30,13 @@ namespace psemek::util } template - auto pretty (std::chrono::duration d, UpTo up_to) + auto pretty(std::chrono::duration d, UpTo up_to) { return detail::pretty_print_time_wrapper, UpTo>{d, up_to}; } template - auto pretty (std::chrono::duration d) + auto pretty(std::chrono::duration d) { return pretty(d, std::chrono::seconds{1}); } diff --git a/libs/util/include/psemek/util/profiler.hpp b/libs/util/include/psemek/util/profiler.hpp index 2cf28c03..ae93547a 100644 --- a/libs/util/include/psemek/util/profiler.hpp +++ b/libs/util/include/psemek/util/profiler.hpp @@ -10,11 +10,11 @@ namespace psemek::util struct profiler { - profiler (std::string name) + profiler(std::string name) : name_(std::move(name)) - { } + {} - ~ profiler ( ) + ~ profiler() { std::cout << name_ << ": " << util::pretty(clock_.duration(), std::chrono::microseconds{1}) << "\n"; } diff --git a/libs/util/include/psemek/util/range.hpp b/libs/util/include/psemek/util/range.hpp index 7dd9f56a..2ca91146 100644 --- a/libs/util/include/psemek/util/range.hpp +++ b/libs/util/include/psemek/util/range.hpp @@ -10,14 +10,14 @@ namespace psemek::util { template - auto begin_helper (Container & x) + auto begin_helper(Container & x) { using std::begin; return begin(x); } template - auto end_helper (Container & x) + auto end_helper(Container & x) { using std::end; return end(x); @@ -26,13 +26,13 @@ namespace psemek::util } template - auto begin (Container & x) + auto begin(Container & x) { return detail::begin_helper(x); } template - auto end (Container & x) + auto end(Container & x) { return detail::end_helper(x); } @@ -68,13 +68,13 @@ namespace psemek::util }; template - auto reversed (Range const & r) + auto reversed(Range const & r) { auto it1 = begin(r); auto it2 = end(r); using ReverseIterator = std::reverse_iterator; - return range{ std::make_reverse_iterator(it2), std::make_reverse_iterator(it1) }; + return range{std::make_reverse_iterator(it2), std::make_reverse_iterator(it1)}; } } diff --git a/libs/util/include/psemek/util/recursive.hpp b/libs/util/include/psemek/util/recursive.hpp index d7e47fa9..486810ac 100644 --- a/libs/util/include/psemek/util/recursive.hpp +++ b/libs/util/include/psemek/util/recursive.hpp @@ -12,7 +12,7 @@ namespace psemek::util F f; template - auto operator() (Args && ... args) -> decltype(auto) + auto operator()(Args && ... args) -> decltype(auto) { return f(*this, std::forward(args)...); } @@ -21,7 +21,7 @@ namespace psemek::util } template - auto recursive (F f) + auto recursive(F f) { return detail::recursive_impl{std::move(f)}; } diff --git a/libs/util/include/psemek/util/shared_blob.hpp b/libs/util/include/psemek/util/shared_blob.hpp index 3670ca99..68261ec7 100644 --- a/libs/util/include/psemek/util/shared_blob.hpp +++ b/libs/util/include/psemek/util/shared_blob.hpp @@ -42,8 +42,8 @@ namespace psemek::util char * end() { return data() + size(); } char const * end() const { return data() + size(); } - char & operator[] (std::size_t i) { return data()[i]; } - char const & operator[] (std::size_t i) const { return data()[i]; } + char & operator[](std::size_t i) { return data()[i]; } + char const & operator[](std::size_t i) const { return data()[i]; } std::string string() const; std::string_view string_view() const; @@ -83,7 +83,7 @@ namespace psemek::util , size_{size} {} - inline shared_blob & shared_blob::operator=(shared_blob const & other) + inline shared_blob & shared_blob::operator = (shared_blob const & other) { if (this != &other) { @@ -93,7 +93,7 @@ namespace psemek::util return *this; } - inline shared_blob & shared_blob::operator=(shared_blob && other) + inline shared_blob & shared_blob::operator = (shared_blob && other) { if (this != &other) { @@ -104,7 +104,7 @@ namespace psemek::util return *this; } - inline shared_blob & shared_blob::operator=(blob && other) + inline shared_blob & shared_blob::operator = (blob && other) { size_ = other.size(); data_ = other.release(); diff --git a/libs/util/include/psemek/util/synchronyzed_queue.hpp b/libs/util/include/psemek/util/synchronyzed_queue.hpp index 0d1b9876..5cef8d4b 100644 --- a/libs/util/include/psemek/util/synchronyzed_queue.hpp +++ b/libs/util/include/psemek/util/synchronyzed_queue.hpp @@ -13,32 +13,32 @@ namespace psemek::util template struct synchronized_queue { - synchronized_queue (std::size_t max_size = std::numeric_limits::max()) noexcept + synchronized_queue(std::size_t max_size = std::numeric_limits::max()) noexcept : max_size_(max_size) - { } + {} - std::size_t max_size ( ) const noexcept + std::size_t max_size() const noexcept { return max_size_; } - void push (T const & x); - void push (T && x); - T pop ( ); + void push(T const & x); + void push(T && x); + T pop(); - bool try_push (T const & x); + bool try_push(T const & x); template - bool try_push (T const & x, std::chrono::duration const & timeout); + bool try_push(T const & x, std::chrono::duration const & timeout); - std::optional try_pop ( ); + std::optional try_pop(); template - std::optional try_pop (std::chrono::duration const & timeout); + std::optional try_pop(std::chrono::duration const & timeout); - void clear ( ); + void clear(); // Wait for the queue to become empty // e.g. when no new items are going to be pushed - void wait ( ); + void wait(); private: std::mutex mutex; @@ -48,9 +48,9 @@ namespace psemek::util }; template - void synchronized_queue::push (T const & x) + void synchronized_queue::push(T const & x) { - std::unique_lock lock { mutex }; + std::unique_lock lock{mutex}; push_cv.wait(lock, [this]{ return queue.size() < max_size(); }); queue.push_back(x); pop_cv.notify_one(); @@ -59,16 +59,16 @@ namespace psemek::util template void synchronized_queue::push (T && x) { - std::unique_lock lock { mutex }; + std::unique_lock lock{mutex}; push_cv.wait(lock, [this]{ return queue.size() < max_size(); }); queue.push_back(std::move(x)); pop_cv.notify_one(); } template - T synchronized_queue::pop ( ) + T synchronized_queue::pop() { - std::unique_lock lock { mutex }; + std::unique_lock lock{mutex}; pop_cv.wait(lock, [this]{ return !queue.empty(); }); T x = std::move(queue.front()); queue.pop_front(); @@ -77,10 +77,10 @@ namespace psemek::util } template - bool synchronized_queue::try_push (T const & x) + bool synchronized_queue::try_push(T const & x) { - std::lock_guard lock { mutex }; - if (queue.size() >= max_size()) + std::lock_guard lock{mutex}; + if (queue.size() >= max_size()) return false; queue.push_back(x); @@ -90,10 +90,10 @@ namespace psemek::util template template - bool synchronized_queue::try_push (T const & x, std::chrono::duration const & timeout) + bool synchronized_queue::try_push(T const & x, std::chrono::duration const & timeout) { - std::unique_lock lock { mutex }; - if (push_cv.wait_for(lock, timeout, [this]{ return queue.size() < max_size(); })) + std::unique_lock lock{mutex}; + if (push_cv.wait_for(lock, timeout, [this]{ return queue.size() < max_size(); })) { queue.push_back(std::move(x)); pop_cv.notify_one(); @@ -103,9 +103,9 @@ namespace psemek::util } template - std::optional synchronized_queue::try_pop ( ) + std::optional synchronized_queue::try_pop() { - std::lock_guard lock { mutex }; + std::lock_guard lock{mutex}; if (queue.empty()) return std::nullopt; T x = std::move(queue.front()); @@ -116,31 +116,31 @@ namespace psemek::util template template - std::optional synchronized_queue::try_pop (std::chrono::duration const & timeout) + std::optional synchronized_queue::try_pop(std::chrono::duration const & timeout) { - std::unique_lock lock { mutex }; + std::unique_lock lock{mutex}; if (pop_cv.wait_for(lock, timeout, [this]{ return !queue.empty(); })) { T x = std::move(queue.front()); queue.pop_front(); push_cv.notify_one(); - return { std::move(x) }; + return {std::move(x)}; } return std::nullopt; } template - void synchronized_queue::clear ( ) + void synchronized_queue::clear() { - std::lock_guard lock { mutex }; + std::lock_guard lock{mutex}; queue.clear(); push_cv.notify_all(); } template - void synchronized_queue::wait ( ) + void synchronized_queue::wait() { - std::unique_lock lock { mutex }; + std::unique_lock lock{mutex}; push_cv.wait(lock, [this]{ return queue.empty(); }); } diff --git a/libs/util/include/psemek/util/thread.hpp b/libs/util/include/psemek/util/thread.hpp index 1082d064..c567bac6 100644 --- a/libs/util/include/psemek/util/thread.hpp +++ b/libs/util/include/psemek/util/thread.hpp @@ -9,13 +9,13 @@ namespace psemek::util : std::thread { template - thread (Args && ... args) + thread(Args && ... args) : std::thread(std::forward(args)...) - { } + {} - thread (thread &&) = default; + thread(thread &&) = default; - ~ thread ( ) + ~ thread() { if (joinable()) join(); diff --git a/libs/util/include/psemek/util/threadpool.hpp b/libs/util/include/psemek/util/threadpool.hpp index b33b4eb0..2ee490b4 100644 --- a/libs/util/include/psemek/util/threadpool.hpp +++ b/libs/util/include/psemek/util/threadpool.hpp @@ -12,26 +12,26 @@ namespace psemek::util struct threadpool { - threadpool ( ) + threadpool() : threadpool(std::max(1u, std::thread::hardware_concurrency())) - { } + {} - threadpool (std::size_t thread_count) + threadpool(std::size_t thread_count) { start(thread_count); } - ~ threadpool ( ) + ~ threadpool() { stop(); } template - auto dispatch (F && f) + auto dispatch(F && f) { using R = decltype(f()); - std::packaged_task task { std::forward(f) }; + std::packaged_task task{std::forward(f)}; auto result = task.get_future(); @@ -40,11 +40,11 @@ namespace psemek::util return result; } - void start (std::size_t thread_count); + void start(std::size_t thread_count); - void stop ( ); + void stop(); - void wait ( ) + void wait() { tasks_queue.wait(); } diff --git a/libs/util/include/psemek/util/timer.hpp b/libs/util/include/psemek/util/timer.hpp index 36f89ec4..8d883706 100644 --- a/libs/util/include/psemek/util/timer.hpp +++ b/libs/util/include/psemek/util/timer.hpp @@ -9,11 +9,11 @@ namespace psemek::util struct timer : clock { - timer (Duration duration) + timer(Duration duration) : duration_(duration) - { } + {} - explicit operator bool ( ) + explicit operator bool() { if (this->duration() >= duration_) { diff --git a/libs/util/include/psemek/util/to_string.hpp b/libs/util/include/psemek/util/to_string.hpp index 42699fb8..2573d01f 100644 --- a/libs/util/include/psemek/util/to_string.hpp +++ b/libs/util/include/psemek/util/to_string.hpp @@ -10,7 +10,7 @@ namespace psemek::util { template , typename ... Args> - std::basic_string to_string (Args const & ... args) + std::basic_string to_string(Args const & ... args) { std::basic_ostringstream oss; @@ -22,25 +22,25 @@ namespace psemek::util } template - std::string to_string (Args const & ... args) + std::string to_string(Args const & ... args) { return detail::to_string(args...); } template - std::wstring to_wstring (Args const & ... args) + std::wstring to_wstring(Args const & ... args) { return detail::to_string(args...); } template - std::u32string to_u32string (Args const & ... args) + std::u32string to_u32string(Args const & ... args) { return detail::to_string(args...); } template - T from_string (std::basic_string const & s) + T from_string(std::basic_string const & s) { std::basic_istringstream iss(s); T x; @@ -51,7 +51,7 @@ namespace psemek::util } template - T from_string (Char const * s) + T from_string(Char const * s) { return from_string>(s); } diff --git a/libs/util/include/psemek/util/unicode.hpp b/libs/util/include/psemek/util/unicode.hpp index 93f3f394..e1198772 100644 --- a/libs/util/include/psemek/util/unicode.hpp +++ b/libs/util/include/psemek/util/unicode.hpp @@ -5,7 +5,7 @@ namespace psemek::util { - std::string to_utf8 (std::u32string const & str); - std::u32string from_utf8 (std::string const & str); + std::string to_utf8(std::u32string const & str); + std::u32string from_utf8(std::string const & str); } diff --git a/libs/util/source/assert.cpp b/libs/util/source/assert.cpp index 84d3967c..7950ab5a 100644 --- a/libs/util/source/assert.cpp +++ b/libs/util/source/assert.cpp @@ -6,7 +6,7 @@ namespace psemek::util { - [[noreturn]] bool assert_handler (char const * expression, char const * file, int line) + [[noreturn]] bool assert_handler(char const * expression, char const * file, int line) { throw std::runtime_error(to_string(file, ":", line, " Assertion failed: ", expression)); } diff --git a/libs/util/source/movable_function.cpp b/libs/util/source/movable_function.cpp index 0f4dc69f..26fb51e5 100644 --- a/libs/util/source/movable_function.cpp +++ b/libs/util/source/movable_function.cpp @@ -8,9 +8,9 @@ namespace psemek::util namespace detail { - void bad_function_call ( ) + void bad_function_call() { - throw std::bad_function_call(); + throw std::bad_function_call{}; } } diff --git a/libs/util/source/not_implemented.cpp b/libs/util/source/not_implemented.cpp index f9a73a13..10c39152 100644 --- a/libs/util/source/not_implemented.cpp +++ b/libs/util/source/not_implemented.cpp @@ -5,7 +5,7 @@ namespace psemek::util { - void not_implemented ( ) + void not_implemented() { throw std::runtime_error("Not implemented"); } diff --git a/libs/util/source/pretty_print_time.cpp b/libs/util/source/pretty_print_time.cpp index 99de1cc1..bf373997 100644 --- a/libs/util/source/pretty_print_time.cpp +++ b/libs/util/source/pretty_print_time.cpp @@ -6,7 +6,7 @@ namespace psemek::util namespace detail { - void pretty_print_time (std::ostream & o, std::int64_t d, std::int64_t up_to) + void pretty_print_time(std::ostream & o, std::int64_t d, std::int64_t up_to) { static constexpr const std::int64_t durations[8] = { 604800 * 1000000000ull, diff --git a/libs/util/source/threadpool.cpp b/libs/util/source/threadpool.cpp index 72c67a78..ff69ce1b 100644 --- a/libs/util/source/threadpool.cpp +++ b/libs/util/source/threadpool.cpp @@ -3,7 +3,7 @@ namespace psemek::util { - void threadpool::start (std::size_t thread_count) + void threadpool::start(std::size_t thread_count) { for (std::size_t th = 0; th < thread_count; ++th) { @@ -22,7 +22,7 @@ namespace psemek::util } } - void threadpool::stop ( ) + void threadpool::stop() { tasks_queue.clear(); for (auto const & thread: threads) diff --git a/libs/util/source/unicode.cpp b/libs/util/source/unicode.cpp index 1a503195..175ff5a2 100644 --- a/libs/util/source/unicode.cpp +++ b/libs/util/source/unicode.cpp @@ -8,14 +8,14 @@ namespace psemek::util using converter = std::wstring_convert, char32_t>; - std::string to_utf8 (std::u32string const & str) + std::string to_utf8(std::u32string const & str) { - return converter().to_bytes(str); + return converter{}.to_bytes(str); } - std::u32string from_utf8 (std::string const & str) + std::u32string from_utf8(std::string const & str) { - return converter().from_bytes(str); + return converter{}.from_bytes(str); } } diff --git a/todo.md b/todo.md index 5d75430b..ae2d8f74 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ -* Use the same code style everywhere (mostly about spaces after function names) * Make sure program & shaders are deleted properly if program creation fails * Remove gfx::vertex, setup mesh using attributes directly * Design affine transforms in geom & use them instead of matrices when appropriate