PenroseTiling.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * PenroseTiling.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. // Adapted from https://github.com/mpizzzle/penrose by Michael Percival
  11. #include "StdInc.h"
  12. #include "PenroseTiling.h"
  13. #include <vstd/RNG.h>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. Point2D Point2D::operator * (float scale) const
  16. {
  17. return Point2D(x() * scale, y() * scale);
  18. }
  19. Point2D Point2D::operator / (float scale) const
  20. {
  21. return Point2D(x() / scale, y() / scale);
  22. }
  23. Point2D Point2D::operator + (const Point2D& other) const
  24. {
  25. return Point2D(x() + other.x(), y() + other.y());
  26. }
  27. Point2D Point2D::operator - (const Point2D& other) const
  28. {
  29. return Point2D(x() - other.x(), y() - other.y());
  30. }
  31. bool Point2D::operator < (const Point2D& other) const
  32. {
  33. if (x() != other.x())
  34. return x() < other.x();
  35. return y() < other.y();
  36. }
  37. bool Point2D::operator == (const Point2D& other) const
  38. {
  39. return vstd::isAlmostEqual(x(), other.x()) && vstd::isAlmostEqual(y(), other.y());
  40. }
  41. std::string Point2D::toString() const
  42. {
  43. //Performance is important here
  44. std::string result = "(" +
  45. std::to_string(this->x()) + " " +
  46. std::to_string(this->y()) + ")";
  47. return result;
  48. }
  49. Triangle::Triangle(bool t_123, const TIndices & inds):
  50. tiling(t_123),
  51. indices(inds)
  52. {}
  53. Triangle::~Triangle()
  54. {
  55. for (auto * triangle : subTriangles)
  56. {
  57. if (triangle)
  58. {
  59. delete triangle;
  60. triangle = nullptr;
  61. }
  62. }
  63. }
  64. Point2D Point2D::rotated(float radians) const
  65. {
  66. float cosAngle = cos(radians);
  67. float sinAngle = sin(radians);
  68. // Apply rotation matrix transformation
  69. float newX = x() * cosAngle - y() * sinAngle;
  70. float newY = x() * sinAngle + y() * cosAngle;
  71. return Point2D(newX, newY);
  72. }
  73. void PenroseTiling::split(Triangle& p, std::vector<Point2D>& points,
  74. std::array<std::vector<uint32_t>, 5>& indices, uint32_t depth)
  75. {
  76. uint32_t s = points.size();
  77. TIndices& i = p.indices;
  78. const auto p2 = P2;
  79. if (depth > 0)
  80. {
  81. if (p.tiling ^ !p2)
  82. {
  83. points.push_back(Point2D((points[i[0]] * (1.0f - PHI) ) + (points[i[2]]) * PHI));
  84. points.push_back(Point2D((points[i[p2]] * (1.0f - PHI)) + (points[i[!p2]] * PHI)));
  85. auto * t1 = new Triangle(p2, TIndices({ i[(!p2) + 1], p2 ? i[2] : s, p2 ? s : i[1] }));
  86. auto * t2 = new Triangle(true, TIndices({ p2 ? i[1] : s, s + 1, p2 ? s : i[1] }));
  87. auto * t3 = new Triangle(false, TIndices({ s, s + 1, i[0] }));
  88. p.subTriangles = { t1, t2, t3 };
  89. }
  90. else
  91. {
  92. points.push_back(Point2D((points[i[p2 * 2]] * (1.0f - PHI)) + (points[i[!p2]]) * PHI));
  93. auto * t1 = new Triangle(true, TIndices({ i[2], s, i[1] }));
  94. auto * t2 = new Triangle(false, TIndices({ i[(!p2) + 1], s, i[0] }));
  95. p.subTriangles = { t1, t2 };
  96. }
  97. for (auto& t : p.subTriangles)
  98. {
  99. if (depth == 1)
  100. {
  101. for (uint32_t k = 0; k < 3; ++k)
  102. {
  103. if (k != (t->tiling ^ !p2 ? 2 : 1))
  104. {
  105. indices[indices.size() - 1].push_back(t->indices[k]);
  106. indices[indices.size() - 1].push_back(t->indices[((k + 1) % 3)]);
  107. }
  108. }
  109. indices[t->tiling + (p.tiling ? 0 : 2)].insert(indices[t->tiling + (p.tiling ? 0 : 2)].end(), t->indices.begin(), t->indices.end());
  110. }
  111. // Split recursively
  112. split(*t, points, indices, depth - 1);
  113. }
  114. }
  115. return;
  116. }
  117. std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, vstd::RNG * rand)
  118. {
  119. float scale = 173.2f / (numZones * 1.5f + 20);
  120. float polyAngle = (2 * PI_CONSTANT) / POLY;
  121. float randomAngle = rand->nextDouble(0.25 * PI_CONSTANT, 0.75 * PI_CONSTANT);
  122. std::vector<Point2D> points = { Point2D(0.0f, 0.0f), Point2D(0.0f, 1.0f).rotated(randomAngle) };
  123. std::array<std::vector<uint32_t>, 5> indices;
  124. for (uint32_t i = 1; i < POLY; ++i)
  125. {
  126. Point2D next = points[i].rotated(polyAngle);
  127. points.push_back(next);
  128. }
  129. for (auto& p : points)
  130. {
  131. p.x(p.x() * scale * BASE_SIZE);
  132. }
  133. std::set<Point2D> finalPoints;
  134. for (uint32_t i = 0; i < POLY; i++)
  135. {
  136. std::array<uint32_t, 2> p = { (i % (POLY + 1)) + 1, ((i + 1) % POLY) + 1 };
  137. Triangle t(true, TIndices({ 0, p[i & 1], p[!(i & 1)] }));
  138. split(t, points, indices, DEPTH);
  139. }
  140. // Remove duplicates
  141. vstd::unique(points);
  142. // Shift center to (0.5, 0.5)
  143. for (auto & point : points)
  144. {
  145. point = point + Point2D(0.5f, 0.5f);
  146. }
  147. // For 8XM8 map, only 650 out of 15971 points are in the range
  148. vstd::copy_if(points, vstd::set_inserter(finalPoints), [](const Point2D point)
  149. {
  150. return vstd::isbetween(point.x(), 0.f, 1.0f) && vstd::isbetween(point.y(), 0.f, 1.0f);
  151. });
  152. return finalPoints;
  153. }
  154. VCMI_LIB_NAMESPACE_END