14#include <unordered_map>
34 return {
x + v2.
x,
y + v2.
y };
39 return {
x - v2.
x,
y - v2.
y };
44 return x * v2.
x +
y * v2.
y;
49 return {
x * s,
y * s };
54 return {
x / s,
y / s };
59 return sqrt(
x *
x +
y *
y);
64 float norm = !(*this);
65 return {
x / norm,
y / norm };
70 float norm1 = !(*this);
72 return acos(((*
this) * v2) / (norm1 * norm2));
79 float dotProd = (*this) * v2;
80 return (*
this) *= (dotProd / norm2);
89 return {
x + v2.
x,
y + v2.
y,
z + v2.
z };
94 return {
x - v2.
x,
y - v2.
y,
z - v2.
z };
99 return x * v2.
x +
y * v2.
y +
z * v2.
z;
104 return {
x * s,
y * s,
z * s };
109 return {
x / s,
y / s,
z / s };
114 return {
y * v2.
z -
z * v2.
y,
z * v2.
x -
x * v2.
z,
x * v2.
y -
y * v2.
x };
119 return sqrt(
x *
x +
y *
y +
z *
z);
124 float norm = !(*this);
125 return {
x / norm,
y / norm,
z / norm };
130 float norm1 = !(*this);
132 return acos(((*
this) * v2) / (norm1 * norm2));
139 float dotProd = (*this) * v2;
140 return (*
this) *= (dotProd / norm2);
146 os <<
"{ x: " << v.
x <<
", y: " << v.
y <<
"}";
152 os <<
"{ x: " << v.
x <<
", y: " << v.
y <<
", z: " << v.
z <<
"}";
157#define ECS_Types int, float, std::string, bool, Vector2, Vector3
160using dataUnMap = std::unordered_map<std::string, std::pair<std::string, std::variant<ECS_Types>>>;
163using dataVector = std::vector<std::pair<std::string, std::variant<ECS_Types>>>;
171std::variant<ECS_Types>
strToType(std::string type);
180std::variant<ECS_Types>
valueToType(
const nlohmann::json& value, std::string type);
194void valueToStream(std::ostream& stream, std::variant<ECS_Types> value);
202inline std::variant<ECS_Types>
strToType(std::string type) {
207 type[0] = tolower(
static_cast<unsigned char>(type[0]));
209 if (type ==
"integer" || type ==
"int") {
212 else if (type ==
"float") {
215 else if (type ==
"string" || type ==
"str") {
218 else if (type ==
"boolean" || type ==
"bool") {
221 else if (type ==
"vector2") {
222 return Vector2({ 0.0f, 0.0f });
224 else if (type ==
"vector3") {
225 return Vector3({ 0.0f, 0.0f, 0.0f });
228 throw std::runtime_error(
"Error : invalid type \"" + type +
"\".");
231inline std::variant<ECS_Types>
valueToType(
const nlohmann::json& value, std::string type) {
233 type[0] = tolower(
static_cast<unsigned char>(type[0]));
235 if (type ==
"integer" || type ==
"int") {
236 return value.get<
int>();
238 else if (type ==
"float") {
239 return value.get<
float>();
241 else if (type ==
"string" || type ==
"str") {
242 return value.get<std::string>();
244 else if (type ==
"boolean" || type ==
"bool") {
245 return value.get<
bool>();
247 else if (type ==
"vector2") {
248 return Vector2{ value[0].get<
float>(), value[1].get<float>() };
250 else if (type ==
"vector3") {
251 return Vector3{ value[0].get<
float>(), value[1].get<float>(), value[2].get<
float>() };
254 throw std::runtime_error(
"Error : invalid type \"" + type +
"\".");
258 nlohmann::ordered_json dict = nlohmann::ordered_json::object();
260 for (
const auto& [key, valuePair] : dataMap) {
261 const auto& data = valuePair.second;
264 std::visit([&](
auto&& val) {
265 using T = std::decay_t<
decltype(val)>;
267 if constexpr (std::is_same_v<T, int> ||
268 std::is_same_v<T, float> ||
269 std::is_same_v<T, std::string> ||
270 std::is_same_v<T, bool>) {
273 else if constexpr (std::is_same_v<T, Vector2>) {
274 dict[key] = { val.x, val.y };
276 else if constexpr (std::is_same_v<T, Vector3>) {
277 dict[key] = { val.x, val.y, val.z };
284inline void valueToStream(std::ostream& stream, std::variant<ECS_Types> value) {
286 std::visit([&](
auto&& val) {
292 std::vector<std::string> result;
295 for (
const auto& file : std::filesystem::recursive_directory_iterator(directory)) {
296 if (std::filesystem::is_regular_file(file.path())) {
297 result.push_back(file.path().string());
301 catch (
const std::filesystem::filesystem_error& e) {
302 std::cerr << e.what() << std::endl;
Vector2 structure and operators.
Definition TM_Tools.h:30
float x
Definition TM_Tools.h:30
float operator%(const Vector2 v2) const
Angle.
Definition TM_Tools.h:69
float y
Definition TM_Tools.h:30
Vector2 operator>>(const Vector2 v2) const
Projection.
Definition TM_Tools.h:76
Vector2 operator+(const Vector2 v2) const
Addition.
Definition TM_Tools.h:33
float operator!() const
Norm.
Definition TM_Tools.h:58
float operator*(const Vector2 v2) const
Dot product.
Definition TM_Tools.h:43
Vector2 operator-(const Vector2 v2) const
Soustraction.
Definition TM_Tools.h:38
Vector2 operator*=(const float s) const
Scalar product.
Definition TM_Tools.h:48
Vector2 operator/=(const float s) const
Scalar division.
Definition TM_Tools.h:53
Vector2 operator~() const
Normalization.
Definition TM_Tools.h:63
Vector3 structure and operators.
Definition TM_Tools.h:85
float operator*(const Vector3 v2) const
Dot product.
Definition TM_Tools.h:98
Vector3 operator/=(const float s) const
Scalar division.
Definition TM_Tools.h:108
Vector3 operator+(const Vector3 v2) const
Addition.
Definition TM_Tools.h:88
Vector3 operator^(const Vector3 v2) const
Cross product.
Definition TM_Tools.h:113
float operator%(const Vector3 v2) const
Angle.
Definition TM_Tools.h:129
float x
Definition TM_Tools.h:85
float y
Definition TM_Tools.h:85
Vector3 operator>>(const Vector3 v2) const
Projection.
Definition TM_Tools.h:136
Vector3 operator-(const Vector3 v2) const
Soustraction.
Definition TM_Tools.h:93
float z
Definition TM_Tools.h:85
float operator!() const
Norm.
Definition TM_Tools.h:118
Vector3 operator*=(const float s) const
Scalar product.
Definition TM_Tools.h:103
Vector3 operator~() const
Normalization.
Definition TM_Tools.h:123