Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// WeightedSquareGrid8.hpp
//
// Created on: 31. 01. 2018
// Author: Jan Uhlik
// Modified by:
//
// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
// Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library
#ifndef ALIB2_WEIGHTEDSQUAREGRID8_HPP
#define ALIB2_WEIGHTEDSQUAREGRID8_HPP
#include <alib/tuple>
#include <edge/weighted/WeightedEdge.hpp>
#include "SquareGrid.hpp"
namespace grid {
template<typename TCoordinate, typename TEdge>
class WeightedSquareGrid8 final : public SquareGrid<TCoordinate, TEdge> {
// ---------------------------------------------------------------------------------------------------------------------
public:
using coordinate_type = TCoordinate;
using edge_type = TEdge;
using node_type = ext::pair<TCoordinate, TCoordinate>;
using direction_type = SquareGridDirections;
// ---------------------------------------------------------------------------------------------------------------------
protected:
typename edge_type::weight_type m_unit;
// =====================================================================================================================
// Constructor, Destructor, Operators
public:
explicit WeightedSquareGrid8(TCoordinate height, TCoordinate width, typename edge_type::weight_type unit = 1);
// ---------------------------------------------------------------------------------------------------------------------
typename edge_type::weight_type getUnit() const;
void setUnit(typename edge_type::weight_type unit);
// ---------------------------------------------------------------------------------------------------------------------
// =====================================================================================================================
// ObjectBase interface
public:
GridBase *clone() const & override;
GridBase *clone() && override;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
int compare(const object::ObjectBase &other) const override;
virtual int compare(const WeightedSquareGrid8 &other) const;
object::ObjectBase *inc() &&override;
// ---------------------------------------------------------------------------------------------------------------------
// =====================================================================================================================
// GridInterface interface
public:
bool isValidDirection(direction_type direction) const override;
// ---------------------------------------------------------------------------------------------------------------------
protected:
TEdge createEdge(const node_type &a, const node_type &b) const override;
// ---------------------------------------------------------------------------------------------------------------------
// =====================================================================================================================
// GraphInterface interface
public:
std::string name() const override;
// ---------------------------------------------------------------------------------------------------------------------
};
// =====================================================================================================================
template<typename TCoordinate, typename TEdge>
WeightedSquareGrid8<TCoordinate, TEdge>::WeightedSquareGrid8(TCoordinate height,
TCoordinate width,
typename TEdge::weight_type unit)
: SquareGrid<TCoordinate, TEdge>(height, width), m_unit(unit) {
}
// ---------------------------------------------------------------------------------------------------------------------
template<typename TCoordinate, typename TEdge>
typename TEdge::weight_type WeightedSquareGrid8<TCoordinate, TEdge>::getUnit() const {
return m_unit;
}
template<typename TCoordinate, typename TEdge>
void WeightedSquareGrid8<TCoordinate, TEdge>::setUnit(typename TEdge::weight_type unit) {
if (unit > 0) m_unit = unit;
}
// ---------------------------------------------------------------------------------------------------------------------
template<typename TCoordinate, typename TEdge>
TEdge WeightedSquareGrid8<TCoordinate, TEdge>::createEdge(const WeightedSquareGrid8::node_type &a,
const WeightedSquareGrid8::node_type &b) const {
if (GridFunction::sqaureGridDirectionIsDiagonal(GridFunction::squareGridDirection(a, b))) {
return TEdge(a, b, M_SQRT2 * m_unit);
}
return TEdge(a, b, m_unit);
}
// ---------------------------------------------------------------------------------------------------------------------
template<typename TCoordinate, typename TEdge>
bool WeightedSquareGrid8<TCoordinate, TEdge>::isValidDirection(WeightedSquareGrid8::direction_type direction) const {
return SQUARE_GRID_DIRECTIONS.find(direction) != SQUARE_GRID_DIRECTIONS.end();
}
// ---------------------------------------------------------------------------------------------------------------------
template<typename TCoordinate, typename TEdge>
std::string WeightedSquareGrid8<TCoordinate, TEdge>::name() const {
return "WeightedSquareGrid8";
}
// ---------------------------------------------------------------------------------------------------------------------
template<typename TCoordinate, typename TEdge>
GridBase *WeightedSquareGrid8<TCoordinate, TEdge>::clone() const & {
return new WeightedSquareGrid8(*this);
}
template<typename TCoordinate, typename TEdge>
GridBase *WeightedSquareGrid8<TCoordinate, TEdge>::clone() && {
return new WeightedSquareGrid8(std::move(*this));
}
template<typename TCoordinate, typename TEdge>
int WeightedSquareGrid8<TCoordinate, TEdge>::compare(const object::ObjectBase &other) const {
if (ext::type_index(typeid(*this)) == ext::type_index(typeid(other))) return this->compare((decltype(*this)) other);
return ext::type_index(typeid(*this)) - ext::type_index(typeid(other));
}
template<typename TCoordinate, typename TEdge>
int WeightedSquareGrid8<TCoordinate, TEdge>::compare(const WeightedSquareGrid8 &other) const {
auto first = ext::tie(this->m_obstacles, this->m_height, this->m_width);
TCoordinate height = other.getHeight();
TCoordinate width = other.getWidth();
auto second = ext::tie(other.getObstacleList(), height, width);
static ext::compare<decltype(first)> comp;
return comp(first, second);
}
template<typename TCoordinate, typename TEdge>
object::ObjectBase *WeightedSquareGrid8<TCoordinate, TEdge>::inc() &&{
return new object::UniqueObject(object::Object(std::move(*this)), 0);
}
// ---------------------------------------------------------------------------------------------------------------------
} // namespace grid
// =====================================================================================================================
namespace core {
/**
* Helper for normalisation of types specified by templates used as internal datatypes of symbols and states.
*
* \returns new instance of the graph with default template parameters or unmodified instance if the template parameters were already the default ones
*/
template<typename TCoordinate, typename TEdge>
struct normalize < grid::WeightedSquareGrid8 < TCoordinate, TEdge > > {
static grid::WeightedSquareGrid8<> eval(grid::WeightedSquareGrid8<TCoordinate, TEdge> &&value) {
grid::WeightedSquareGrid8<> grid(value.getHeight(), value.getWidth(), value.getUnit());
// Copy obstacles
for (auto &&i: ext::make_mover(std::move(value).getObstacleList())) {
DefaultSquareGridNodeType
obstacle = common::Normalize::normalizeObstacle(std::move(i));
grid.addObstacle(std::move(obstacle));
}
return grid;
}
};
} // namespace grid
// =====================================================================================================================
#endif //ALIB2_WEIGHTEDSQUAREGRID8_HPP