PenroseTiling.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. VCMI_LIB_NAMESPACE_BEGIN
  14. Point2D Point2D::operator * (float scale) const
  15. {
  16. return Point2D(x() * scale, y() * scale);
  17. }
  18. Point2D Point2D::operator / (float scale) const
  19. {
  20. return Point2D(x() / scale, y() / scale);
  21. }
  22. Point2D Point2D::operator + (const Point2D& other) const
  23. {
  24. return Point2D(x() + other.x(), y() + other.y());
  25. }
  26. Point2D Point2D::operator - (const Point2D& other) const
  27. {
  28. return Point2D(x() - other.x(), y() - other.y());
  29. }
  30. bool Point2D::operator < (const Point2D& other) const
  31. {
  32. if (x() < other.x())
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return y() < other.y();
  39. }
  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, CRandomGenerator * 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. std::set<Point2D> uniquePoints(points.begin(), points.end());
  141. std::vector<Point2D> uniquePointsVec(uniquePoints.begin(), uniquePoints.end());
  142. logGlobal->info("Generated %d vertices and %d unique vertices", points.size(), uniquePointsVec.size());
  143. // Find center of the mass, shift that center to (0.5, 0.5)
  144. Point2D center = Point2D(0.0f, 0.0f);
  145. for (const auto & point : uniquePointsVec)
  146. {
  147. center = center + point;
  148. };
  149. center = center / uniquePointsVec.size();
  150. // This center is very close to (0.0, 0.0), anyway
  151. logGlobal->info("Penrose tiling center: %s", center.toString().c_str());
  152. for (auto & point : uniquePointsVec)
  153. {
  154. point = point - center + Point2D(0.5f, 0.5f);
  155. };
  156. // For 8X8 map, only 650 out of 15971 points are in the range
  157. vstd::copy_if(uniquePointsVec, vstd::set_inserter(finalPoints), [](const Point2D point)
  158. {
  159. return vstd::isbetween(point.x(), 0.f, 1.0f) && vstd::isbetween(point.y(), 0.f, 1.0f);
  160. });
  161. return finalPoints;
  162. }
  163. VCMI_LIB_NAMESPACE_END