1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/lib/rmg/PenroseTiling.cpp

196 lines
4.5 KiB
C++
Raw Normal View History

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"
#include "PenroseTiling.h"
2024-01-30 19:04:15 +02:00
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());
}
2024-02-02 15:27:32 +02:00
bool Point2D::operator < (const Point2D& other) const
{
2024-05-08 15:09:11 +02:00
if (x() != other.x())
return x() < other.x();
return y() < other.y();
2024-02-02 15:27:32 +02:00
}
2024-05-08 15:51:42 +02:00
bool Point2D::operator == (const Point2D& other) const
{
return vstd::isAlmostEqual(x(), other.x()) && vstd::isAlmostEqual(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;
}
}
}
Point2D Point2D::rotated(float radians) const
{
2024-03-24 18:25:30 +02:00
float cosAngle = cos(radians);
float sinAngle = sin(radians);
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-03-24 18:25:30 +02:00
return Point2D(newX, newY);
}
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)
{
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
{
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
{
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
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)
{
Point2D next = points[i].rotated(polyAngle);
2024-01-30 19:04:15 +02:00
points.push_back(next);
}
for (auto& p : points)
{
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 };
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 15:51:42 +02:00
// Remove duplicates
vstd::unique(points);
2024-05-08 15:51:42 +02:00
// Shift center to (0.5, 0.5)
for (auto & point : points)
{
2024-05-08 15:51:42 +02:00
point = point + Point2D(0.5f, 0.5f);
};
2024-01-30 19:04:15 +02:00
2024-05-08 15:51:42 +02:00
// For 8XM8 map, only 650 out of 15971 points are in the range
2024-05-08 15:51:42 +02:00
vstd::copy_if(points, vstd::set_inserter(finalPoints), [](const Point2D point)
2024-02-02 15:27:32 +02:00
{
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