File: PenroseTiling.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (198 lines) | stat: -rw-r--r-- 4,611 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * PenroseTiling.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

// Adapted from https://github.com/mpizzzle/penrose by Michael Percival

#include "StdInc.h"
#include "PenroseTiling.h"

#include <vstd/RNG.h>

VCMI_LIB_NAMESPACE_BEGIN


Point2D Point2D::operator * (float scale) const
{
	return Point2D(x() * scale, y() * scale);
}

Point2D Point2D::operator / (float scale) const
{
	return Point2D(x() / scale, y() / scale);
}

Point2D Point2D::operator + (const Point2D& other) const
{
	return Point2D(x() + other.x(), y() + other.y());
}

Point2D Point2D::operator - (const Point2D& other) const
{
	return Point2D(x() - other.x(), y() - other.y());
}

bool Point2D::operator < (const Point2D& other) const
{
	if (x() != other.x())
		return x() < other.x();

	return y() < other.y();
}

bool Point2D::operator == (const Point2D& other) const
{
	return vstd::isAlmostEqual(x(), other.x()) && vstd::isAlmostEqual(y(), other.y());
}

std::string Point2D::toString() const
{
	//Performance is important here
	std::string result = "(" +
			std::to_string(this->x()) + " " +
			std::to_string(this->y()) + ")";

	return result;
}

Triangle::Triangle(bool t_123, const TIndices & inds):
	tiling(t_123),
	indices(inds)
{}

Triangle::~Triangle()
{
	for (auto * triangle : subTriangles)
	{
		if (triangle)
		{
			delete triangle;
			triangle = nullptr;
		}
	}
}

Point2D Point2D::rotated(float radians) const
{
	float cosAngle = cos(radians);
	float sinAngle = sin(radians);

	// Apply rotation matrix transformation
	float newX = x() * cosAngle - y() * sinAngle;
	float newY = x() * sinAngle + y() * cosAngle;

	return Point2D(newX, newY);
}

void PenroseTiling::split(Triangle& p, std::vector<Point2D>& points,
	std::array<std::vector<uint32_t>, 5>& indices, uint32_t depth) 
{
	uint32_t s = points.size();
	TIndices& i = p.indices;

	const auto p2 = P2;

	if (depth > 0)
	{
		if (p.tiling ^ !p2)
		{
			points.push_back(Point2D((points[i[0]] * (1.0f - PHI) ) + (points[i[2]]) * PHI));
			points.push_back(Point2D((points[i[p2]] * (1.0f - PHI)) + (points[i[!p2]] * PHI)));

			auto * t1 = new Triangle(p2, TIndices({ i[(!p2) + 1], p2 ? i[2] : s, p2 ? s : i[1] }));
			auto * t2 = new Triangle(true, TIndices({ p2 ? i[1] : s, s + 1, p2 ? s : i[1] }));
			auto * t3 = new Triangle(false, TIndices({ s, s + 1, i[0] }));

			p.subTriangles = { t1, t2, t3 };
		}
		else
		{
			points.push_back(Point2D((points[i[p2 * 2]] * (1.0f - PHI)) + (points[i[!p2]]) * PHI));

			auto * t1 = new Triangle(true, TIndices({ i[2], s, i[1] }));
			auto * t2 = new Triangle(false, TIndices({ i[(!p2) + 1], s, i[0] }));

			p.subTriangles = { t1, t2 };
		}

		for (auto& t : p.subTriangles)
		{
			if (depth == 1)
			{
				for (uint32_t k = 0; k < 3; ++k)
				{
					if (k != (t->tiling ^ !p2 ? 2 : 1))
					{
						indices[indices.size() - 1].push_back(t->indices[k]);
						indices[indices.size() - 1].push_back(t->indices[((k + 1) % 3)]);
					}
				}

				indices[t->tiling + (p.tiling ? 0 : 2)].insert(indices[t->tiling + (p.tiling ? 0 : 2)].end(), t->indices.begin(), t->indices.end());
			}

			// Split recursively
			split(*t, points, indices, depth - 1);
		}
	}

	return;
}

std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, vstd::RNG * rand)
{
	float scale = 173.2f / (numZones * 1.5f + 20);
	float polyAngle = (2 * PI_CONSTANT) / POLY;

	float randomAngle = rand->nextDouble(0.25 * PI_CONSTANT, 0.75 * PI_CONSTANT);

	std::vector<Point2D> points = { Point2D(0.0f, 0.0f), Point2D(0.0f, 1.0f).rotated(randomAngle) };
	std::array<std::vector<uint32_t>, 5> indices;

	for (uint32_t i = 1; i < POLY; ++i)
	{
		Point2D next = points[i].rotated(polyAngle);
		points.push_back(next);
	}

	for (auto& p : points)
	{
		p.x(p.x() * scale * BASE_SIZE);
	}

	std::set<Point2D> finalPoints;

	for (uint32_t i = 0; i < POLY; i++)
	{
		std::array<uint32_t, 2> p = { (i % (POLY + 1)) + 1, ((i + 1) % POLY) + 1 };

		Triangle t(true, TIndices({ 0, p[i & 1], p[!(i & 1)] }));

		split(t, points, indices, DEPTH);
	}
	// Remove duplicates
	vstd::unique(points);

	// Shift center to (0.5, 0.5)
	for (auto & point : points)
	{
		point = point + Point2D(0.5f, 0.5f);
	}

	// For 8XM8 map, only 650 out of 15971 points are in the range

	vstd::copy_if(points, vstd::set_inserter(finalPoints), [](const Point2D point)
	{
		return vstd::isbetween(point.x(), 0.f, 1.0f) && vstd::isbetween(point.y(), 0.f, 1.0f);
	});

	return finalPoints;
}

VCMI_LIB_NAMESPACE_END