2024-01-30 19:04:15 +02:00
|
|
|
/*
|
2024-02-02 15:27:32 +02:00
|
|
|
* 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
|
|
|
|
*
|
2024-01-30 19:04:15 +02:00
|
|
|
*/
|
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
// Adapted from https://github.com/mpizzzle/penrose by Michael Percival
|
2024-01-30 19:04:15 +02:00
|
|
|
|
|
|
|
#include "StdInc.h"
|
2024-02-02 11:37:08 +02:00
|
|
|
#include "PenroseTiling.h"
|
2024-01-30 19:04:15 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
2024-02-02 11:37:08 +02:00
|
|
|
Point2D Point2D::operator * (float scale) const
|
|
|
|
{
|
|
|
|
return Point2D(x() * scale, y() * scale);
|
|
|
|
}
|
|
|
|
|
2024-05-08 12:16:42 +02:00
|
|
|
Point2D Point2D::operator / (float scale) const
|
|
|
|
{
|
|
|
|
return Point2D(x() / scale, y() / scale);
|
|
|
|
}
|
|
|
|
|
2024-02-02 11:37:08 +02:00
|
|
|
Point2D Point2D::operator + (const Point2D& other) const
|
|
|
|
{
|
|
|
|
return Point2D(x() + other.x(), y() + other.y());
|
|
|
|
}
|
|
|
|
|
2024-05-08 12:16:42 +02:00
|
|
|
Point2D Point2D::operator - (const Point2D& other) const
|
|
|
|
{
|
|
|
|
return Point2D(x() - other.x(), y() - other.y());
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
bool Point2D::operator < (const Point2D& other) const
|
|
|
|
{
|
|
|
|
if (x() < other.x())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return y() < other.y();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 18:25:30 +02:00
|
|
|
std::string Point2D::toString() const
|
|
|
|
{
|
|
|
|
//Performance is important here
|
|
|
|
std::string result = "(" +
|
|
|
|
std::to_string(this->x()) + " " +
|
|
|
|
std::to_string(this->y()) + ")";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-01-30 19:04:15 +02:00
|
|
|
Triangle::Triangle(bool t_123, const TIndices & inds):
|
|
|
|
tiling(t_123),
|
|
|
|
indices(inds)
|
|
|
|
{}
|
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
Triangle::~Triangle()
|
|
|
|
{
|
|
|
|
for (auto * triangle : subTriangles)
|
|
|
|
{
|
|
|
|
if (triangle)
|
|
|
|
{
|
|
|
|
delete triangle;
|
|
|
|
triangle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 11:37:08 +02:00
|
|
|
Point2D Point2D::rotated(float radians) const
|
|
|
|
{
|
2024-03-24 18:25:30 +02:00
|
|
|
float cosAngle = cos(radians);
|
|
|
|
float sinAngle = sin(radians);
|
2024-02-02 11:37:08 +02:00
|
|
|
|
2024-03-24 18:25:30 +02:00
|
|
|
// Apply rotation matrix transformation
|
|
|
|
float newX = x() * cosAngle - y() * sinAngle;
|
|
|
|
float newY = x() * sinAngle + y() * cosAngle;
|
2024-02-02 11:37:08 +02:00
|
|
|
|
2024-03-24 18:25:30 +02:00
|
|
|
return Point2D(newX, newY);
|
2024-02-02 11:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PenroseTiling::split(Triangle& p, std::vector<Point2D>& points,
|
2024-01-30 19:04:15 +02:00
|
|
|
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)
|
|
|
|
{
|
2024-02-02 11:37:08 +02:00
|
|
|
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)));
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
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] }));
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
p.subTriangles = { t1, t2, t3 };
|
2024-01-30 19:04:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-02 11:37:08 +02:00
|
|
|
points.push_back(Point2D((points[i[p2 * 2]] * (1.0f - PHI)) + (points[i[!p2]]) * PHI));
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
auto * t1 = new Triangle(true, TIndices({ i[2], s, i[1] }));
|
|
|
|
auto * t2 = new Triangle(false, TIndices({ i[(!p2) + 1], s, i[0] }));
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
p.subTriangles = { t1, t2 };
|
2024-01-30 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, CRandomGenerator * rand)
|
2024-01-30 19:04:15 +02:00
|
|
|
{
|
2024-04-14 07:25:52 +02:00
|
|
|
float scale = 173.2f / (numZones * 1.5f + 20);
|
2024-02-02 15:27:32 +02:00
|
|
|
float polyAngle = (2 * PI_CONSTANT) / POLY;
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-04-14 07:25:52 +02:00
|
|
|
float randomAngle = rand->nextDouble(0.25 * PI_CONSTANT, 0.75 * PI_CONSTANT);
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
std::vector<Point2D> points = { Point2D(0.0f, 0.0f), Point2D(0.0f, 1.0f).rotated(randomAngle) };
|
2024-01-30 19:04:15 +02:00
|
|
|
std::array<std::vector<uint32_t>, 5> indices;
|
|
|
|
|
|
|
|
for (uint32_t i = 1; i < POLY; ++i)
|
|
|
|
{
|
2024-02-02 11:37:08 +02:00
|
|
|
Point2D next = points[i].rotated(polyAngle);
|
2024-01-30 19:04:15 +02:00
|
|
|
points.push_back(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& p : points)
|
|
|
|
{
|
2024-02-02 11:37:08 +02:00
|
|
|
p.x(p.x() * scale * BASE_SIZE);
|
2024-01-30 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
std::set<Point2D> finalPoints;
|
|
|
|
|
2024-01-30 19:04:15 +02:00
|
|
|
for (uint32_t i = 0; i < POLY; i++)
|
|
|
|
{
|
|
|
|
std::array<uint32_t, 2> p = { (i % (POLY + 1)) + 1, ((i + 1) % POLY) + 1 };
|
|
|
|
|
2024-02-02 11:37:08 +02:00
|
|
|
Triangle t(true, TIndices({ 0, p[i & 1], p[!(i & 1)] }));
|
2024-01-30 19:04:15 +02:00
|
|
|
|
|
|
|
split(t, points, indices, DEPTH);
|
|
|
|
}
|
2024-05-08 12:16:42 +02:00
|
|
|
// Find center of the mass, shift that center to (0.5, 0.5)
|
|
|
|
|
|
|
|
Point2D center = Point2D(0.0f, 0.0f);
|
|
|
|
for (auto & point : points)
|
|
|
|
{
|
|
|
|
center = center + point;
|
|
|
|
};
|
|
|
|
center = center / points.size();
|
|
|
|
|
|
|
|
for (auto & point : points)
|
|
|
|
{
|
|
|
|
point = point - center + Point2D(0.5f, 0.5f);
|
|
|
|
};
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
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);
|
|
|
|
});
|
2024-01-30 19:04:15 +02:00
|
|
|
|
2024-02-02 15:27:32 +02:00
|
|
|
return finalPoints;
|
2024-01-30 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|