201 lines
5.3 KiB
C++
201 lines
5.3 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include <string>
|
||
|
#include <random>
|
||
|
#include <functional>
|
||
|
#include <typeindex>
|
||
|
#include <algorithm>
|
||
|
#include <vector>
|
||
|
#include <fstream>
|
||
|
#include <glm/glm.hpp>
|
||
|
#include <glm/gtc/quaternion.hpp>
|
||
|
#include <map>
|
||
|
|
||
|
#include "log.hpp"
|
||
|
|
||
|
namespace Utility {
|
||
|
inline uint32_t Align(uint32_t val, uint32_t align)
|
||
|
{
|
||
|
return (val + align - 1) & ~(align - 1);
|
||
|
}
|
||
|
|
||
|
inline void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = ",")
|
||
|
{
|
||
|
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
|
||
|
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
|
||
|
|
||
|
while (std::string::npos != pos || std::string::npos != lastPos)
|
||
|
{
|
||
|
tokens.push_back(str.substr(lastPos, pos - lastPos));
|
||
|
|
||
|
lastPos = str.find_first_not_of(delimiters, pos);
|
||
|
pos = str.find_first_of(delimiters, lastPos);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
inline std::string Vec3ToString(glm::vec3 vector)
|
||
|
{
|
||
|
return std::string(std::to_string(vector.x) + "," + std::to_string(vector.y) + "," + std::to_string(vector.z));
|
||
|
}
|
||
|
|
||
|
inline std::string Vec2ToString(glm::vec2 vector)
|
||
|
{
|
||
|
return std::string(std::to_string(vector.x) + "," + std::to_string(vector.y));
|
||
|
}
|
||
|
|
||
|
inline std::string QuatToString(glm::quat quaternion)
|
||
|
{
|
||
|
return std::string(std::to_string(quaternion.w) + "," + std::to_string(quaternion.x) + "," + std::to_string(quaternion.y) + "," + std::to_string(quaternion.z));
|
||
|
}
|
||
|
|
||
|
inline glm::vec3 StringToVec3(const std::string value)
|
||
|
{
|
||
|
std::vector<std::string> float_list;
|
||
|
|
||
|
Tokenize(value, float_list);
|
||
|
|
||
|
float x = std::stof(float_list[0]);
|
||
|
float y = std::stof(float_list[1]);
|
||
|
float z = std::stof(float_list[2]);
|
||
|
|
||
|
return glm::vec3(x, y, z);
|
||
|
}
|
||
|
|
||
|
inline glm::vec2 StringToVec2(const char* value)
|
||
|
{
|
||
|
std::string vec2_string = std::string(value);
|
||
|
std::vector<std::string> float_list;
|
||
|
|
||
|
Tokenize(vec2_string, float_list);
|
||
|
|
||
|
float x = std::stof(float_list[0]);
|
||
|
float y = std::stof(float_list[1]);
|
||
|
|
||
|
return glm::vec2(x, y);
|
||
|
}
|
||
|
|
||
|
inline glm::quat StringToQuat(const char* value)
|
||
|
{
|
||
|
std::string quat_string = std::string(value);
|
||
|
std::vector<std::string> float_list;
|
||
|
|
||
|
Tokenize(quat_string, float_list);
|
||
|
|
||
|
float w = std::stof(float_list[0]);
|
||
|
float x = std::stof(float_list[1]);
|
||
|
float y = std::stof(float_list[2]);
|
||
|
float z = std::stof(float_list[3]);
|
||
|
|
||
|
return glm::quat(w, x, y, z);
|
||
|
}
|
||
|
|
||
|
inline std::string RemoveToLastSlash(const std::string& str)
|
||
|
{
|
||
|
std::size_t found = str.find_last_of("/\\");
|
||
|
return str.substr(0, found);
|
||
|
}
|
||
|
|
||
|
inline uint32_t Random(uint32_t min, uint32_t max)
|
||
|
{
|
||
|
std::default_random_engine eng((std::random_device())());
|
||
|
std::uniform_int_distribution<uint32_t> dis(min, max);
|
||
|
|
||
|
return dis(eng);
|
||
|
}
|
||
|
|
||
|
inline bool IsInteger(const std::string& str)
|
||
|
{
|
||
|
return str.find_first_of("0123456789") == std::string::npos;
|
||
|
}
|
||
|
|
||
|
inline bool IsFloat(const std::string& str)
|
||
|
{
|
||
|
return str.find_first_not_of("0123456789.") == std::string::npos;
|
||
|
}
|
||
|
|
||
|
inline std::string GetTypeName(std::type_index type)
|
||
|
{
|
||
|
std::string n = std::string(type.name());
|
||
|
|
||
|
n.erase(std::remove_if(n.begin(), n.end(), &isdigit), n.end());
|
||
|
|
||
|
//MSVC has the prefix "class " so we need to remove that
|
||
|
#ifdef OS_WIN
|
||
|
n = n.erase(0, 6);
|
||
|
#endif
|
||
|
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
inline float Lerp(float a, float b, float f)
|
||
|
{
|
||
|
return a + f * (b - a);
|
||
|
}
|
||
|
|
||
|
inline std::vector<char> ReadFile(const std::string& filename)
|
||
|
{
|
||
|
std::ifstream file_stream(filename, std::ios::ate | std::ios::binary);
|
||
|
|
||
|
if(!file_stream.is_open())
|
||
|
return std::vector<char>();
|
||
|
|
||
|
size_t file_size = static_cast<size_t>(file_stream.tellg());
|
||
|
std::vector<char> buffer(file_size);
|
||
|
|
||
|
file_stream.seekg(0);
|
||
|
file_stream.read(buffer.data(), file_size);
|
||
|
|
||
|
file_stream.close();
|
||
|
|
||
|
return buffer;
|
||
|
}
|
||
|
|
||
|
inline std::string GetFilename(const std::string& path)
|
||
|
{
|
||
|
return path.substr(path.find_last_of("/") + 1, path.length());
|
||
|
}
|
||
|
|
||
|
inline std::string RemoveFileExtension(const std::string& path)
|
||
|
{
|
||
|
return path.substr(0, path.find_last_of("."));
|
||
|
}
|
||
|
|
||
|
inline std::string RemoveSubstrings(const std::string& original, std::vector<std::string> substrings)
|
||
|
{
|
||
|
std::string modified = original;
|
||
|
|
||
|
for(unsigned int i = 0; i < substrings.size(); i++)
|
||
|
{
|
||
|
std::string::size_type pos = original.find(substrings[i]);
|
||
|
|
||
|
if (pos != std::string::npos)
|
||
|
modified.erase(pos, substrings[i].length());
|
||
|
}
|
||
|
|
||
|
return modified;
|
||
|
}
|
||
|
|
||
|
inline int Clamp(int value, int lower, int upper)
|
||
|
{
|
||
|
return value <= lower ? lower : value >= upper ? upper : value;
|
||
|
}
|
||
|
|
||
|
inline float Clip(float n, float lower, float upper)
|
||
|
{
|
||
|
return std::max(lower, std::min(n, upper));
|
||
|
}
|
||
|
|
||
|
template<class A, class B>
|
||
|
inline bool RemoveFromMap(std::map<A, B>& map, const A& value)
|
||
|
{
|
||
|
auto itr = map.find(value);
|
||
|
if(itr != map.end())
|
||
|
{
|
||
|
map.erase(itr);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|