OpenTTD Source: src/station_kdtree.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef STATION_KDTREE_H
11#define STATION_KDTREE_H
12
13#include "core/kdtree.hpp"
14#include "station_base.h"
15#include "map_func.h"
16
17 struct Kdtree_StationXYFunc {
18 inline uint16_t operator()(StationID stid, int dim)
19 {
20 return (dim == 0) ? TileX(BaseStation::Get(stid)->xy) : TileY(BaseStation::Get(stid)->xy);
21 }
22};
23
24using StationKdtree = Kdtree<StationID, Kdtree_StationXYFunc, uint16_t, int>;
25extern StationKdtree _station_kdtree;
26
33template <typename Func>
34 void ForAllStationsRadius(TileIndex center, uint radius, Func func)
35{
36 uint16_t x1, y1, x2, y2;
37 x1 = (uint16_t)std::max<int>(0, TileX(center) - radius);
38 x2 = (uint16_t)std::min<int>(TileX(center) + radius + 1, Map::SizeX());
39 y1 = (uint16_t)std::max<int>(0, TileY(center) - radius);
40 y2 = (uint16_t)std::min<int>(TileY(center) + radius + 1, Map::SizeY());
41
42 _station_kdtree.FindContained(x1, y1, x2, y2, [&](StationID id) {
43 func(Station::Get(id));
44 });
45}
46
47#endif
Kdtree
K-dimensional tree, specialised for 2-dimensional space.
Definition kdtree.hpp:33
Kdtree::FindContained
void FindContained(CoordT x1, CoordT y1, CoordT x2, CoordT y2, const Outputter &outputter) const
Find all items contained within the given rectangle.
Definition kdtree.hpp:457
kdtree.hpp
K-d tree template specialised for 2-dimensional Manhattan geometry.
map_func.h
Functions related to maps.
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
station_base.h
Base classes/functions for stations.
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:34
Kdtree_StationXYFunc
Definition station_kdtree.h:17
Map::SizeX
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:272
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:281
PoolID< uint16_t, struct StationIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
SpecializedStation< Station, false >::Get
static Station * Get(auto index)
Gets station with given index.
Definition base_station_base.h:248
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >