Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
WeightedSquareGrid8.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <alib/tuple>
12
13#include "SquareGrid.hpp"
14
15namespace grid {
16
17template<typename TCoordinate, typename TEdge>
18class WeightedSquareGrid8 final : public SquareGrid<TCoordinate, TEdge> {
19// ---------------------------------------------------------------------------------------------------------------------
20
21 public:
22 using coordinate_type = TCoordinate;
23 using edge_type = TEdge;
26
27// ---------------------------------------------------------------------------------------------------------------------
28 protected:
29 typename edge_type::weight_type m_unit;
30
31// =====================================================================================================================
32// Constructor, Destructor, Operators
33 public:
34 explicit WeightedSquareGrid8(TCoordinate height, TCoordinate width, typename edge_type::weight_type unit = 1);
35
36// ---------------------------------------------------------------------------------------------------------------------
37
38 typename edge_type::weight_type getUnit() const;
39
40 void setUnit(typename edge_type::weight_type unit);
41
42// ---------------------------------------------------------------------------------------------------------------------
43
44// =====================================================================================================================
45// GridBase interface
46 public:
47 auto operator <=> (const WeightedSquareGrid8 &other) const {
48 return std::tie(this->m_obstacles, this->m_height, this->m_width) <=> std::tie(other.getObstacleList(), other.getHeight(), other.getWidth());
49 }
50
51 bool operator == (const WeightedSquareGrid8 &other) const {
52 return std::tie(this->m_obstacles, this->m_height, this->m_width) == std::tie(other.getObstacleList(), other.getHeight(), other.getWidth());
53 }
54
55
56// ---------------------------------------------------------------------------------------------------------------------
57// =====================================================================================================================
58// GridInterface interface
59
60 public:
61
62 bool isValidDirection(direction_type direction) const override;
63
64// ---------------------------------------------------------------------------------------------------------------------
65
66 protected:
67 TEdge createEdge(const node_type &a, const node_type &b) const override;
68
69// ---------------------------------------------------------------------------------------------------------------------
70// =====================================================================================================================
71// GraphInterface interface
72
73 public:
74 std::string name() const override;
75
76// ---------------------------------------------------------------------------------------------------------------------
77
78};
79
80// =====================================================================================================================
81
82template<typename TCoordinate, typename TEdge>
84 TCoordinate width,
85 typename TEdge::weight_type unit)
86 : SquareGrid<TCoordinate, TEdge>(height, width), m_unit(unit) {
87}
88
89// ---------------------------------------------------------------------------------------------------------------------
90
91template<typename TCoordinate, typename TEdge>
92typename TEdge::weight_type WeightedSquareGrid8<TCoordinate, TEdge>::getUnit() const {
93 return m_unit;
94}
95
96template<typename TCoordinate, typename TEdge>
97void WeightedSquareGrid8<TCoordinate, TEdge>::setUnit(typename TEdge::weight_type unit) {
98 if (unit > 0) m_unit = unit;
99}
100
101// ---------------------------------------------------------------------------------------------------------------------
102
103template<typename TCoordinate, typename TEdge>
105 const WeightedSquareGrid8::node_type &b) const {
107 return TEdge(a, b, M_SQRT2 * m_unit);
108 }
109
110 return TEdge(a, b, m_unit);
111}
112
113// ---------------------------------------------------------------------------------------------------------------------
114
115template<typename TCoordinate, typename TEdge>
117 return SQUARE_GRID_DIRECTIONS.find(direction) != SQUARE_GRID_DIRECTIONS.end();
118}
119
120// ---------------------------------------------------------------------------------------------------------------------
121
122template<typename TCoordinate, typename TEdge>
124 return "WeightedSquareGrid8";
125}
126
127// ---------------------------------------------------------------------------------------------------------------------
128
129} // namespace grid
130
131// =====================================================================================================================
132
133namespace core {
134
141template<typename TCoordinate, typename TEdge>
142struct normalize < grid::WeightedSquareGrid8 < TCoordinate, TEdge > > {
144 grid::WeightedSquareGrid8<> grid(value.getHeight(), value.getWidth(), value.getUnit());
145
146 // Copy obstacles
147 for (auto &&i: ext::make_mover(std::move(value).getObstacleList())) {
149 obstacle = graph::GraphNormalize::normalizeObstacle(std::move(i));
150
151 grid.addObstacle(std::move(obstacle));
152 }
153
154 return grid;
155 }
156};
157
158} // namespace grid
159
160// =====================================================================================================================
161
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
static ext::pair< DefaultCoordinateType, DefaultCoordinateType > normalizeObstacle(ext::pair< TCoordinate, TCoordinate > &&obstacle)
Definition: Normalize.hpp:82
static bool sqaureGridDirectionIsDiagonal(grid::SquareGridDirections direction)
Definition: GridFunction.cpp:40
static grid::SquareGridDirections squareGridDirection(const ext::pair< TCoordinate, TCoordinate > &a, const ext::pair< TCoordinate, TCoordinate > &b)
Definition: GridFunction.hpp:56
TCoordinate coordinate_type
Definition: GridInterface.hpp:23
TEdge edge_type
Definition: GridInterface.hpp:24
Definition: SquareGrid.hpp:29
TCoordinate getHeight() const
Definition: SquareGrid.hpp:155
TCoordinate getWidth() const
Definition: SquareGrid.hpp:160
TCoordinate m_height
Definition: SquareGrid.hpp:41
TCoordinate m_width
Definition: SquareGrid.hpp:42
ext::set< node_type > m_obstacles
Definition: SquareGrid.hpp:43
const ext::set< node_type > & getObstacleList() const &
Definition: SquareGrid.hpp:142
Definition: WeightedSquareGrid8.hpp:18
bool isValidDirection(direction_type direction) const override
Definition: WeightedSquareGrid8.hpp:116
SquareGridDirections direction_type
Definition: WeightedSquareGrid8.hpp:25
WeightedSquareGrid8(TCoordinate height, TCoordinate width, typename edge_type::weight_type unit=1)
Definition: WeightedSquareGrid8.hpp:83
bool operator==(const WeightedSquareGrid8 &other) const
Definition: WeightedSquareGrid8.hpp:51
ext::pair< TCoordinate, TCoordinate > node_type
Definition: WeightedSquareGrid8.hpp:24
void setUnit(typename edge_type::weight_type unit)
Definition: WeightedSquareGrid8.hpp:97
edge_type::weight_type m_unit
Definition: WeightedSquareGrid8.hpp:29
edge_type::weight_type getUnit() const
Definition: WeightedSquareGrid8.hpp:92
auto operator<=>(const WeightedSquareGrid8 &other) const
Definition: WeightedSquareGrid8.hpp:47
std::string name() const override
Definition: WeightedSquareGrid8.hpp:123
TEdge createEdge(const node_type &a, const node_type &b) const override
Definition: WeightedSquareGrid8.hpp:104
int i
Definition: AllEpsilonClosure.h:118
Definition: normalize.hpp:10
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
Definition: GridDirection.hpp:12
const ext::set< SquareGridDirections > SQUARE_GRID_DIRECTIONS
Definition: GridDirection.hpp:28
SquareGridDirections
Definition: GridDirection.hpp:16
static grid::WeightedSquareGrid8 eval(grid::WeightedSquareGrid8< TCoordinate, TEdge > &&value)
Definition: WeightedSquareGrid8.hpp:143
Definition: normalize.hpp:13