|
|
@ -21,6 +21,7 @@ |
|
|
|
#include <vector> |
|
|
|
#include <iterator> |
|
|
|
#include <array> |
|
|
|
#include <numeric> |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
class Canvas2D { |
|
|
@ -30,6 +31,7 @@ class Canvas2D { |
|
|
|
|
|
|
|
public: |
|
|
|
static constexpr size_t dimension = 2; |
|
|
|
static constexpr bool layered = false; |
|
|
|
|
|
|
|
Canvas2D(size_t width, size_t height) |
|
|
|
: storage(height, Row(width, 0)) |
|
|
@ -47,12 +49,11 @@ public: |
|
|
|
const size_t width = size<0>(); |
|
|
|
const size_t height = size<1>(); |
|
|
|
|
|
|
|
const size_t c = 0.5*position[0]*width + width*.5; |
|
|
|
const size_t r = 0.5*position[1]*width + height*.5; |
|
|
|
const int c = 0.5*position[0]*width + width*.5; |
|
|
|
const int r = 0.5*position[1]*width + height*.5; |
|
|
|
|
|
|
|
if(c < width && r < height) { |
|
|
|
if(0 <= c && c < width && 0 <= r && r < height) |
|
|
|
storage[r][c]++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
size_t size() const { |
|
|
@ -213,6 +214,7 @@ template <typename Canvas> |
|
|
|
class LayeredCanvas { |
|
|
|
public: |
|
|
|
static constexpr size_t dimension = Canvas::dimension; |
|
|
|
static constexpr bool layered = true; |
|
|
|
|
|
|
|
LayeredCanvas(size_t width, size_t height, size_t layers) |
|
|
|
: canvae(layers, Canvas(width, height)) |
|
|
@ -230,6 +232,10 @@ public: |
|
|
|
return canvae.size(); |
|
|
|
} |
|
|
|
|
|
|
|
size_t size() const { |
|
|
|
return canvae.front().size(); |
|
|
|
} |
|
|
|
|
|
|
|
template <size_t N> |
|
|
|
size_t size() const { |
|
|
|
return canvae.front().size<N>(); |
|
|
@ -243,4 +249,26 @@ private: |
|
|
|
std::vector<Canvas> canvae; |
|
|
|
}; |
|
|
|
|
|
|
|
template <typename C> |
|
|
|
typename std::enable_if<!C::layered, bool>::type filled(C const & canvas, double threshold) { |
|
|
|
struct functor { |
|
|
|
size_t operator()(size_t x, size_t y){ |
|
|
|
if(y > 0) return ++x; |
|
|
|
else return x; |
|
|
|
} |
|
|
|
} f; |
|
|
|
size_t nonempty_pixels = std::accumulate(canvas.begin(), canvas.end(), 0ul, f); |
|
|
|
return nonempty_pixels > threshold * canvas.size(); |
|
|
|
} |
|
|
|
|
|
|
|
template <typename C> |
|
|
|
typename std::enable_if<C::layered, bool>::type filled(C const & canvas, double threshold) { |
|
|
|
for (unsigned int l = 0; l < canvas.layers(); ++l) { |
|
|
|
if (!filled(canvas[l], threshold)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|