OpenTTD Source  1.11.2
station_kdtree.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef STATION_KDTREE_H
11 #define STATION_KDTREE_H
12 
13 #include "core/kdtree.hpp"
14 #include "core/math_func.hpp"
15 #include "station_base.h"
16 #include "map_func.h"
17 
18 inline uint16 Kdtree_StationXYFunc(StationID stid, int dim) { return (dim == 0) ? TileX(BaseStation::Get(stid)->xy) : TileY(BaseStation::Get(stid)->xy); }
19 typedef Kdtree<StationID, decltype(&Kdtree_StationXYFunc), uint16, int> StationKdtree;
20 extern StationKdtree _station_kdtree;
21 
28 template <typename Func>
29 void ForAllStationsRadius(TileIndex center, uint radius, Func func)
30 {
31  uint16 x1, y1, x2, y2;
32  x1 = (uint16)std::max<int>(0, TileX(center) - radius);
33  x2 = (uint16)std::min<int>(TileX(center) + radius + 1, MapSizeX());
34  y1 = (uint16)std::max<int>(0, TileY(center) - radius);
35  y2 = (uint16)std::min<int>(TileY(center) + radius + 1, MapSizeY());
36 
37  _station_kdtree.FindContained(x1, y1, x2, y2, [&](StationID id) {
38  func(Station::Get(id));
39  });
40 }
41 
42 #endif
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
Kdtree
K-dimensional tree, specialised for 2-dimensional space.
Definition: kdtree.hpp:37
math_func.hpp
map_func.h
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
Kdtree::FindContained
void FindContained(CoordT x1, CoordT y1, CoordT x2, CoordT y2, Outputter outputter) const
Find all items contained within the given rectangle.
Definition: kdtree.hpp:461
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
ForAllStationsRadius
void ForAllStationsRadius(TileIndex center, uint radius, Func func)
Call a function on all stations whose sign is within a radius of a center tile.
Definition: station_kdtree.h:29
station_base.h
BaseStation
Base class for all station-ish types.
Definition: base_station_base.h:52
kdtree.hpp