54#include <initializer_list>
81template <
typename T, std::
size_t Rows, std::
size_t Cols>
84 static_assert(std::is_arithmetic_v<T>,
"Kernel2D requires arithmetic weights");
85 static_assert(Rows >= 3
and Cols >= 3,
"Kernel2D requires at least 3x3 weights");
86 static_assert(Rows % 2 == 1
and Cols % 2 == 1,
"Kernel2D dimensions must be odd");
87 static_assert(Rows == Cols,
"Kernel2D currently expects square kernels");
94 static constexpr std::size_t
rows_v = Rows;
96 static constexpr std::size_t
cols_v = Cols;
98 static constexpr std::size_t
size_v = Rows * Cols;
135 <<
"Kernel2D: expected " <<
size_v <<
" weights, got " <<
weights.size();
147 explicit Kernel2D(std::initializer_list<std::initializer_list<T>>
rows)
150 <<
"Kernel2D: expected " << Rows <<
" rows, got " <<
rows.size();
156 <<
"Kernel2D: expected " << Cols <<
" columns, got " <<
row.size();
158 for (
const auto &
w :
row)
174 <<
"Kernel2D::operator(): position (" <<
row <<
", " <<
col <<
") outside kernel";
190 or static_cast<std::size_t
>(c) >= Cols)
191 <<
"Kernel2D::weight_at_offset: offset (" <<
di <<
", " <<
dj <<
") outside radius "
193 return weights_[
index(
static_cast<std::size_t
>(
r),
static_cast<std::size_t
>(c))];
208 <<
"Kernel2D::neighbour_weight: index " <<
k <<
" outside [0, " <<
neighbour_count_v <<
")";
211 for (std::size_t
r = 0;
r < Rows; ++
r)
212 for (std::size_t c = 0; c < Cols; ++c)
214 if (
r == Rows / 2
and c == Cols / 2)
265 template <
typename State>
270 << neighbours.size();
275 for (std::size_t
r = 0;
r < Rows; ++
r)
276 for (std::size_t c = 0; c < Cols; ++c)
278 if (
r == Rows / 2
and c == Cols / 2)
298 auto rd = std::make_shared<IntRange>(
static_cast<int>(Rows));
299 auto cd = std::make_shared<IntRange>(
static_cast<int>(Cols));
301 for (std::size_t
r = 0;
r < Rows; ++
r)
302 for (std::size_t c = 0; c < Cols; ++c)
322template <
typename T =
double>
325 return Kernel2D<T, 3, 3>(std::array<T, 9>{
T{0},
T{1},
T{0},
T{1},
T{-4},
T{1},
T{0},
T{1},
T{0}});
342template <
typename T =
double>
346 T{-20} /
T{6},
T{4} /
T{6},
T{1} /
T{6},
T{4} /
T{6},
356template <
typename T =
double>
359 constexpr T w =
T{1} /
T{9};
360 return Kernel2D<T, 3, 3>(std::array<T, 9>{
w,
w,
w,
w,
w,
w,
w,
w,
w});
369template <
typename T =
double>
372 constexpr T w =
T{1} /
T{25};
374 std::array<T, 25>{
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w,
w});
383template <
typename T =
double>
386 return Kernel2D<T, 3, 3>(std::array<T, 9>{
T{-1},
T{0},
T{1},
T{-2},
T{0},
T{2},
T{-1},
T{0},
T{1}});
395template <
typename T =
double>
398 return Kernel2D<T, 3, 3>(std::array<T, 9>{
T{-1},
T{-2},
T{-1},
T{0},
T{0},
T{0},
T{1},
T{2},
T{1}});
407template <
typename T =
double>
411 T{4} /
T{16},
T{2} /
T{16},
T{1} /
T{16},
T{2} /
T{16},
421template <
typename T =
double>
425 T{1} /
T{256},
T{4} /
T{256},
T{6} /
T{256},
T{4} /
T{256},
T{1} /
T{256},
426 T{4} /
T{256},
T{16} /
T{256},
T{24} /
T{256},
T{16} /
T{256},
T{4} /
T{256},
427 T{6} /
T{256},
T{24} /
T{256},
T{36} /
T{256},
T{24} /
T{256},
T{6} /
T{256},
428 T{4} /
T{256},
T{16} /
T{256},
T{24} /
T{256},
T{16} /
T{256},
T{4} /
T{256},
429 T{1} /
T{256},
T{4} /
T{256},
T{6} /
T{256},
T{4} /
T{256},
T{1} /
T{256}});
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Integer domain classes for sparse data structures.
Sparse matrix with generic domains.
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
Dense odd-sized 2-D convolution kernel.
static constexpr std::size_t rows_v
Number of rows.
T neighbour_weight(const std::size_t k) const
Return a centre-skipping neighbour weight.
T weight_at_offset(const ca_index_t di, const ca_index_t dj) const
Return the weight at an offset from the centre.
static constexpr std::size_t index(const std::size_t row, const std::size_t col) noexcept
Kernel2D(std::initializer_list< std::initializer_list< T > > rows)
Construct a kernel from nested row initializer lists.
Matrix< int, int, T > to_matrix() const
Export the kernel weights as an Aleph::Matrix.
std::array< T, size_v > weights_
static constexpr std::size_t neighbour_count_v
Number of neighbour weights excluding the centre.
static constexpr std::size_t cols_v
Number of columns.
constexpr Kernel2D(const std::array< T, size_v > &weights)
Construct a kernel from row-major weights.
Kernel2D(std::initializer_list< T > weights)
Construct a kernel from a flat initializer list.
T apply(const State ¢er_value, Neighbor_View< State > neighbours) const
Apply the kernel to centre plus neighbour values.
static constexpr std::size_t radius_v
Chebyshev radius matched by Moore<2, radius_v>.
T operator()(const std::size_t row, const std::size_t col) const
Return a weight by matrix position.
constexpr const std::array< T, size_v > & weights() const noexcept
Return the raw row-major weight array.
constexpr T sum() const noexcept
Sum every kernel weight.
T value_type
Numeric weight type.
static constexpr std::size_t size_v
Total number of weights including the centre.
constexpr Kernel2D()=default
Construct a zero kernel.
constexpr T center() const noexcept
Return the centre weight.
Sparse matrix with generic row and column domains.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
constexpr Kernel2D< T, 3, 3 > sobel_y_kernel() noexcept
Return the vertical Sobel gradient kernel.
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
constexpr Kernel2D< T, 5, 5 > gaussian_5x5_kernel() noexcept
Return the separable 5x5 Gaussian blur kernel.
constexpr Kernel2D< T, 3, 3 > laplacian_5p_kernel() noexcept
Return the 5-point discrete Laplacian kernel.
constexpr Kernel2D< T, 5, 5 > mean_5x5_kernel() noexcept
Return a 5x5 mean filter.
constexpr Kernel2D< T, 3, 3 > laplacian_9p_kernel() noexcept
Return the isotropic 9-point discrete Laplacian kernel.
constexpr Kernel2D< T, 3, 3 > sobel_x_kernel() noexcept
Return the horizontal Sobel gradient kernel.
constexpr Kernel2D< T, 3, 3 > mean_3x3_kernel() noexcept
Return a 3x3 mean filter.
constexpr Kernel2D< T, 3, 3 > gaussian_3x3_kernel() noexcept
Return the separable 3x3 Gaussian blur kernel.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T