From 2297e09fcb436caee2df447530234bf5e9a0722c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Wed, 8 May 2024 15:51:42 +0200 Subject: [PATCH] Clean up excessive operations and logs --- Global.h | 9 +++++++++ lib/rmg/PenroseTiling.cpp | 31 ++++++++++++------------------- lib/rmg/PenroseTiling.h | 1 + 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Global.h b/Global.h index 2b43c7d34..6e4c15e06 100644 --- a/Global.h +++ b/Global.h @@ -523,6 +523,15 @@ namespace vstd } } + // Removes all duplicate elements from the vector + template + void unique(std::vector &vec) + { + std::sort(vec.begin(), vec.end()); + auto newEnd = std::unique(vec.begin(), vec.end()); + vec.erase(newEnd, vec.end()); + } + template OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred) { diff --git a/lib/rmg/PenroseTiling.cpp b/lib/rmg/PenroseTiling.cpp index 32dc26645..80395573f 100644 --- a/lib/rmg/PenroseTiling.cpp +++ b/lib/rmg/PenroseTiling.cpp @@ -44,6 +44,11 @@ bool Point2D::operator < (const Point2D& other) const 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 @@ -169,30 +174,18 @@ std::set PenroseTiling::generatePenroseTiling(size_t numZones, CRandomG split(t, points, indices, DEPTH); } + // Remove duplicates + vstd::unique(points); - std::set uniquePoints(points.begin(), points.end()); - std::vector uniquePointsVec(uniquePoints.begin(), uniquePoints.end()); - logGlobal->info("Generated %d vertices and %d unique vertices", points.size(), uniquePointsVec.size()); - - // Find center of the mass, shift that center to (0.5, 0.5) - Point2D center = Point2D(0.0f, 0.0f); - for (const auto & point : uniquePointsVec) + // Shift center to (0.5, 0.5) + for (auto & point : points) { - center = center + point; - }; - center = center / uniquePointsVec.size(); - - // This center is very close to (0.0, 0.0), anyway - logGlobal->info("Penrose tiling center: %s", center.toString().c_str()); - - for (auto & point : uniquePointsVec) - { - point = point - center + Point2D(0.5f, 0.5f); + point = point + Point2D(0.5f, 0.5f); }; - // For 8X8 map, only 650 out of 15971 points are in the range + // For 8XM8 map, only 650 out of 15971 points are in the range - vstd::copy_if(uniquePointsVec, vstd::set_inserter(finalPoints), [](const Point2D point) + 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); }); diff --git a/lib/rmg/PenroseTiling.h b/lib/rmg/PenroseTiling.h index f008fecd1..8b6ace8ed 100644 --- a/lib/rmg/PenroseTiling.h +++ b/lib/rmg/PenroseTiling.h @@ -34,6 +34,7 @@ public: Point2D rotated(float radians) const; bool operator < (const Point2D& other) const; + bool operator == (const Point2D& other) const; std::string toString() const; };