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 
8 /** @file town_kdtree.h Declarations for accessing the k-d tree of towns */
9 
10 #ifndef VIEWPORT_KDTREE_H
11 #define VIEWPORT_KDTREE_H
12 
13 #include "core/kdtree.hpp"
14 #include "viewport_type.h"
15 #include "station_base.h"
16 #include "town_type.h"
17 #include "signs_base.h"
18 
19 struct ViewportSignKdtreeItem {
20 	enum ItemType : uint16 {
21 		VKI_STATION,
22 		VKI_WAYPOINT,
23 		VKI_TOWN,
24 		VKI_SIGN,
25 	};
26 	ItemType type;
27 	union {
28 		StationID station;
29 		TownID town;
30 		SignID sign;
31 	} id;
32 	int32 center;
33 	int32 top;
34 
35 	bool operator== (const ViewportSignKdtreeItem &other) const
36 	{
37 		if (this->type != other.type) return false;
38 		switch (this->type) {
39 			case VKI_STATION:
40 			case VKI_WAYPOINT:
41 				return this->id.station == other.id.station;
42 			case VKI_TOWN:
43 				return this->id.town == other.id.town;
44 			case VKI_SIGN:
45 				return this->id.sign == other.id.sign;
46 			default:
47 				NOT_REACHED();
48 		}
49 	}
50 
51 	bool operator< (const ViewportSignKdtreeItem &other) const
52 	{
53 		if (this->type != other.type) return this->type < other.type;
54 		switch (this->type) {
55 			case VKI_STATION:
56 			case VKI_WAYPOINT:
57 				return this->id.station < other.id.station;
58 			case VKI_TOWN:
59 				return this->id.town < other.id.town;
60 			case VKI_SIGN:
61 				return this->id.sign < other.id.sign;
62 			default:
63 				NOT_REACHED();
64 		}
65 	}
66 
67 	static ViewportSignKdtreeItem MakeStation(StationID id);
68 	static ViewportSignKdtreeItem MakeWaypoint(StationID id);
69 	static ViewportSignKdtreeItem MakeTown(TownID id);
70 	static ViewportSignKdtreeItem MakeSign(SignID id);
71 };
72 
Kdtree_ViewportSignXYFunc(const ViewportSignKdtreeItem & item,int dim)73 inline int32 Kdtree_ViewportSignXYFunc(const ViewportSignKdtreeItem &item, int dim)
74 {
75 	return (dim == 0) ? item.center : item.top;
76 }
77 
78 typedef Kdtree<ViewportSignKdtreeItem, decltype(&Kdtree_ViewportSignXYFunc), int32, int32> ViewportSignKdtree;
79 extern ViewportSignKdtree _viewport_sign_kdtree;
80 
81 void RebuildViewportKdtree();
82 
83 #endif
84