Files
Seele/src/Engine/Math/Vector.cpp
T

56 lines
2.0 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "Vector.h"
2023-02-13 14:56:13 +01:00
#include "Serialization/ArchiveBuffer.h"
2024-06-09 12:20:04 +02:00
#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <regex>
#include <sstream>
2020-06-02 11:46:18 +02:00
2023-01-21 18:43:21 +01:00
using namespace Seele;
2024-06-09 12:20:04 +02:00
void to_json(nlohmann::json& j, const Vector& vec) { j = nlohmann::json{fmt::format("({}, {}, {})", vec.x, vec.y, vec.z)}; }
2023-01-21 18:43:21 +01:00
2024-06-09 12:20:04 +02:00
void from_json(const nlohmann::json& j, Vector& vec) {
2023-01-21 18:43:21 +01:00
std::string str;
j.get_to(str);
auto newEnd = std::remove(str.begin(), str.end(), ' ');
str.erase(newEnd);
std::stringstream stream(str);
std::string temp;
std::getline(stream, temp, ',');
vec.x = std::stof(temp);
std::getline(stream, temp, ',');
vec.y = std::stof(temp);
std::getline(stream, temp, ',');
vec.z = std::stof(temp);
}
2020-06-02 11:46:18 +02:00
2024-06-09 12:20:04 +02:00
std::ostream& operator<<(std::ostream& stream, const Vector2& vector) {
2024-07-05 12:02:46 +02:00
stream << "Vec2(" << vector.x << ", " << vector.y << ")";
2021-04-25 23:43:40 +02:00
return stream;
}
2024-06-09 12:20:04 +02:00
std::ostream& operator<<(std::ostream& stream, const Vector& vector) {
2024-07-05 12:02:46 +02:00
stream << "Vec3(" << vector.x << ", " << vector.y << ", " << vector.z << ")";
2021-04-25 23:43:40 +02:00
return stream;
}
2024-06-09 12:20:04 +02:00
std::ostream& operator<<(std::ostream& stream, const Vector4& vector) {
2024-07-05 12:02:46 +02:00
stream << "Vec4(" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << ")";
return stream;
}
std::ostream& operator<<(std::ostream& stream, const Quaternion& vector) {
stream << "Quat(" << vector.w << ", " << vector.x << ", " << vector.y << ", " << vector.z << ")";
2021-04-25 23:43:40 +02:00
return stream;
}
2024-06-09 12:20:04 +02:00
Vector Seele::parseVector(const char* str) {
// regex pattern consisting of 'float3(xComp, yComp, zComp)', more also matches for invalid floats, but that will throw later
2020-06-02 11:46:18 +02:00
std::regex pattern("float3\\(\\s*([0-9.]+f?)\\s*,\\s*([0-9.]+f?)\\s*,\\s*([0-9.]+f?)\\s*\\)");
std::cmatch base_match;
std::regex_match(str, base_match, pattern);
2024-06-09 12:20:04 +02:00
// match 0 is the whole expression
2020-06-02 11:46:18 +02:00
float x = std::stof(base_match[1].str());
float y = std::stof(base_match[2].str());
float z = std::stof(base_match[3].str());
return Vector(x, y, z);
}