File: widelands_geometry.h

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (177 lines) | stat: -rw-r--r-- 4,718 bytes parent folder | download | duplicates (2)
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
50
51
52
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2002-2004, 2006-2008 by the Widelands Development Team
 *
 * This program 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef WL_LOGIC_WIDELANDS_GEOMETRY_H
#define WL_LOGIC_WIDELANDS_GEOMETRY_H

#include <cmath>
#include <tuple>

#include <stdint.h>

namespace Widelands {

using MapIndex = uint32_t;

struct Extent {
	Extent(uint16_t const W, uint16_t const H) : w(W), h(H) {
	}
	uint16_t w, h;
};

/**
 * Structure used to store map coordinates
 */
struct Coords {
	Coords();
	Coords(int16_t nx, int16_t ny);

	/// Returns a special value indicating invalidity.
	static Coords null();

	/// Unhash coordinates so they can be gotten from a container
	static Coords unhash(uint32_t hash);

	/// Hash coordinates to use them as keys in a container
	uint32_t hash() const;

	bool operator==(const Coords& other) const;
	bool operator!=(const Coords& other) const;
	operator bool() const;

	// Ordering functor for use with std:: containers.
	struct OrderingFunctor {
		bool operator()(const Coords& a, const Coords& b) const {
			return std::forward_as_tuple(a.y, a.x) < std::forward_as_tuple(b.y, b.x);
		}
	};

	// Move the coords to the 'new_origin'.
	void reorigin(Coords new_origin, const Extent& extent);

	int16_t x;
	int16_t y;
};
static_assert(sizeof(Coords) == 4, "assert(sizeof(Coords) == 4) failed.");

template <typename _CoordsType = Coords, typename _RadiusType = uint16_t>
struct Area : public _CoordsType {
	using CoordsType = _CoordsType;
	using RadiusType = _RadiusType;
	Area() {
	}
	Area(const CoordsType center, const RadiusType Radius) : CoordsType(center), radius(Radius) {
	}

	bool operator==(const Area& other) const {
		return CoordsType::operator==(other) && radius == other.radius;
	}
	bool operator!=(const Area& other) const {
		return CoordsType::operator!=(other) || radius != other.radius;
	}

	RadiusType radius;
};

template <typename AreaType = Area<>> struct HollowArea : public AreaType {
	HollowArea(const AreaType area, const typename AreaType::RadiusType Hole_Radius)
	   : AreaType(area), hole_radius(Hole_Radius) {
	}

	bool operator==(const HollowArea& other) const {
		return AreaType::operator==(other) && hole_radius == other.hole_radius;
	}
	bool operator!=(const HollowArea& other) const {
		return !(*this == other);
	}

	typename AreaType::RadiusType hole_radius;
};

struct Field;

struct FCoords : public Coords {
	FCoords() : field(nullptr) {
	}
	FCoords(const Coords& nc, Field* const nf) : Coords(nc), field(nf) {
	}

	/**
	 * Used in RenderTarget::rendermap where this is first called, then the
	 * coordinates are normalized and after that field is set.
	 *
	 * \note You really want to use \ref Map::get_fcoords instead.
	 */
	explicit FCoords(const Coords& nc) : Coords(nc), field(nullptr) {
	}

	Field* field;
};

template <typename CoordsType = Coords> struct TCoords : public CoordsType {
	enum TriangleIndex { D, R, None };

	TCoords() : t() {
	}
	TCoords(const CoordsType C, const TriangleIndex T = None) : CoordsType(C), t(T) {
	}

	bool operator==(const TCoords& other) const {
		return CoordsType::operator==(other) && t == other.t;
	}
	bool operator!=(const TCoords& other) const {
		return CoordsType::operator!=(other) || t != other.t;
	}

	TriangleIndex t;
};

template <typename NodeCoordsType = Coords, typename TriangleCoordsType = Coords>
struct NodeAndTriangle {
	NodeAndTriangle() {
	}
	NodeAndTriangle(const NodeCoordsType Node, const TCoords<TriangleCoordsType>& Triangle)

	   : node(Node), triangle(Triangle) {
	}

	bool operator==(const NodeAndTriangle<>& other) const {
		return node == other.node && triangle == other.triangle;
	}
	bool operator!=(const NodeAndTriangle<>& other) const {
		return !(*this == other);
	}

	NodeCoordsType node;
	TCoords<TriangleCoordsType> triangle;
};

// A height interval.
struct HeightInterval {
	HeightInterval(const uint8_t Min, const uint8_t Max) : min(Min), max(Max) {
	}
	bool valid() const {
		return min <= max;
	}

	uint8_t min, max;
};
}

#endif  // end of include guard: WL_LOGIC_WIDELANDS_GEOMETRY_H