-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
utility.hpp
to store utility functions
- Loading branch information
Showing
5 changed files
with
52 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,39 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <dpp/message.h> | ||
#include <filesystem> | ||
#include <random> | ||
#define cache_channel 1176057541013282896 | ||
|
||
class image { | ||
dpp::snowflake id{}; | ||
cv::Mat img{}; | ||
public: | ||
bool update(cv::Mat new_img = cv::Mat()) { | ||
cv::String path() { | ||
return cv::String(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id))); | ||
} | ||
std::string raw() { | ||
return dpp::utility::read_file(this->path().c_str()); | ||
} | ||
bool image_write(cv::Mat new_img = cv::Mat()) { | ||
if (not new_img.empty()) this->img = new_img; | ||
return cv::imwrite(cv::String(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id))), this->img); | ||
return cv::imwrite(this->path(), this->img); | ||
} | ||
/* @param dem y, x e.g. { 100, 100 } */ | ||
image(dpp::snowflake id, std::vector<int> dem) { | ||
this->id = id; | ||
update(cv::Mat::zeros(dem[0], dem[1], CV_8UC3) + cv::Scalar(0, 0, 0, 240)); | ||
} | ||
std::string raw() { | ||
return dpp::utility::read_file(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id))); | ||
} | ||
std::string path() { | ||
return std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id)); | ||
image_write(cv::Mat::zeros(dem[0], dem[1], CV_8UC3) + cv::Scalar(0, 0, 0, 240)); | ||
} | ||
image& background(std::string file_name) { | ||
cv::Mat image = cv::imread(cv::String(this->path())); | ||
cv::Mat image = cv::imread(this->path()); | ||
cv::Mat background = cv::imread(cv::String(std::format(".\\cache\\{0}.jpg", file_name))); | ||
cv::resize(image, image, background.size()); | ||
cv::addWeighted(image, 0.5, background, (1.0 - 0.5), 0.0, this->img); | ||
update(); | ||
} | ||
image& add_line(std::vector<int> pt1, std::vector<int> pt2, std::vector<double> RGBA, int thickness = 1) { | ||
if (RGBA.size() == 3) RGBA[4] = 240; | ||
cv::line(this->img, cv::Point(pt1[0], pt1[1]), cv::Point(pt2[0], pt2[1]), cv::Scalar(RGBA[0], RGBA[1], RGBA[2], RGBA[3]), thickness); | ||
update(); | ||
} | ||
~image() { | ||
/* this deletes the physical copy and keeps the cloud copy (via discord) */ | ||
std::filesystem::remove(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id))); | ||
std::filesystem::remove(std::filesystem::path(this->path())); | ||
} | ||
}; | ||
|
||
|
||
std::unique_ptr<std::vector<std::string>> index(const std::string& source, const char& find) | ||
{ | ||
std::unique_ptr<std::vector<std::string>> i = std::make_unique<std::vector<std::string>>(); | ||
std::string_view preview(source); | ||
size_t pos = 0; | ||
while ((pos = preview.find(find)) not_eq -1) { | ||
if (pos not_eq 0) i->emplace_back(std::string(preview.substr(0, pos))); | ||
preview.remove_prefix(pos + 1); | ||
} | ||
if (not preview.empty()) i->emplace_back(std::string(preview)); | ||
return std::move(i); | ||
} | ||
template<typename T> T rand(T min, T max) { | ||
std::uniform_int_distribution<T> integer(min, max); | ||
std::default_random_engine random; | ||
return integer(random); | ||
} | ||
time_t string_to_time(std::string str) { | ||
try | ||
{ | ||
time_t t = time(0); | ||
str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); | ||
std::unique_ptr<std::vector<std::string>> is = index(str, ','); // TODO: make it so ',' are not needed. 1h, 30m -> 1h 30m | ||
for (std::string& i : std::move(*is)) { | ||
if (std::ranges::find(i | std::views::reverse, 's') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)); | ||
if (std::ranges::find(i | std::views::reverse, 'm') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60; | ||
if (std::ranges::find(i | std::views::reverse, 'h') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60; | ||
if (std::ranges::find(i | std::views::reverse, 'd') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60 * 24; | ||
} | ||
return t; | ||
} | ||
catch (...) { | ||
return time(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <random> // random engine | ||
#include <dpp/stringops.h> // dpp::rtrim() | ||
#include <ranges> // std::ranges:: | ||
|
||
std::unique_ptr<std::vector<std::string>> index(const std::string& source, const char& find) | ||
{ | ||
std::unique_ptr<std::vector<std::string>> i = std::make_unique<std::vector<std::string>>(); | ||
std::string_view preview(source); | ||
size_t pos = 0; | ||
while ((pos = preview.find(find)) not_eq -1) { | ||
if (pos not_eq 0) i->emplace_back(std::string(preview.substr(0, pos))); | ||
preview.remove_prefix(pos + 1); | ||
} | ||
if (not preview.empty()) i->emplace_back(std::string(preview)); | ||
return std::move(i); | ||
} | ||
template<typename T> T rand(T min, T max) { | ||
static thread_local std::default_random_engine random(std::random_device{}()); | ||
return std::uniform_int_distribution<T>(min, max)(random); | ||
} | ||
time_t string_to_time(std::string str) { | ||
try | ||
{ | ||
time_t t = time(0); | ||
str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); | ||
std::unique_ptr<std::vector<std::string>> is = index(str, ','); // TODO: make it so ',' are not needed. 1h, 30m -> 1h 30m | ||
for (std::string& i : std::move(*is)) { | ||
if (std::ranges::find(i | std::views::reverse, 's') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)); | ||
if (std::ranges::find(i | std::views::reverse, 'm') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60; | ||
if (std::ranges::find(i | std::views::reverse, 'h') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60; | ||
if (std::ranges::find(i | std::views::reverse, 'd') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60 * 24; | ||
} | ||
return t; | ||
} | ||
catch (...) { | ||
return time(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters