From e9d126b3a4e9ba96984e7ce4f1f6538b1610f341 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 6 Mar 2016 01:54:13 +0100 Subject: [PATCH] Math: Add serialization specialization for all types Former-commit-id: 7e45c50d3b4eabfc581736f290fc208592d3d46c --- include/Nazara/Math/BoundingVolume.hpp | 4 + include/Nazara/Math/BoundingVolume.inl | 54 +++++++++++ include/Nazara/Math/Box.hpp | 4 + include/Nazara/Math/Box.inl | 62 +++++++++++++ include/Nazara/Math/EulerAngles.hpp | 4 + include/Nazara/Math/EulerAngles.inl | 44 +++++++++ include/Nazara/Math/Frustum.hpp | 4 + include/Nazara/Math/Frustum.inl | 50 ++++++++++ include/Nazara/Math/Matrix4.hpp | 4 + include/Nazara/Math/Matrix4.inl | 38 ++++++++ include/Nazara/Math/OrientedBox.hpp | 4 + include/Nazara/Math/OrientedBox.inl | 36 ++++++++ include/Nazara/Math/Plane.hpp | 4 + include/Nazara/Math/Plane.inl | 38 ++++++++ include/Nazara/Math/Quaternion.hpp | 4 + include/Nazara/Math/Quaternion.inl | 51 +++++++++++ include/Nazara/Math/Ray.hpp | 4 + include/Nazara/Math/Ray.inl | 38 ++++++++ include/Nazara/Math/Rect.hpp | 4 + include/Nazara/Math/Rect.inl | 50 ++++++++++ include/Nazara/Math/Sphere.hpp | 4 + include/Nazara/Math/Sphere.inl | 50 ++++++++++ include/Nazara/Math/Vector2.hpp | 4 + include/Nazara/Math/Vector2.inl | 40 +++++++- include/Nazara/Math/Vector3.hpp | 4 + include/Nazara/Math/Vector3.inl | 122 +++++++++---------------- include/Nazara/Math/Vector4.hpp | 4 + include/Nazara/Math/Vector4.inl | 50 ++++++++++ 28 files changed, 699 insertions(+), 80 deletions(-) diff --git a/include/Nazara/Math/BoundingVolume.hpp b/include/Nazara/Math/BoundingVolume.hpp index e0ca2f556..b074b4a72 100644 --- a/include/Nazara/Math/BoundingVolume.hpp +++ b/include/Nazara/Math/BoundingVolume.hpp @@ -5,6 +5,7 @@ #ifndef NAZARA_BOUNDINGVOLUME_HPP #define NAZARA_BOUNDINGVOLUME_HPP +#include #include #include #include @@ -66,6 +67,9 @@ namespace Nz typedef BoundingVolume BoundingVolumed; typedef BoundingVolume BoundingVolumef; + + template bool Serialize(SerializationContext& context, const BoundingVolume& boundingVolume); + template bool Unserialize(SerializationContext& context, BoundingVolume* boundingVolume); } template diff --git a/include/Nazara/Math/BoundingVolume.inl b/include/Nazara/Math/BoundingVolume.inl index 4047769dc..b5743b058 100644 --- a/include/Nazara/Math/BoundingVolume.inl +++ b/include/Nazara/Math/BoundingVolume.inl @@ -545,6 +545,60 @@ namespace Nz return volume; } + + /*! + * \brief Serializes a BoundingVolume + * \return true if successfully serialized + * + * \param context Serialization context + * \param boundingVolume Input bounding volume + * + * \remark Does not save OBB corners + */ + template + bool Serialize(SerializationContext& context, const BoundingVolume& boundingVolume) + { + if (!Serialize(context, static_cast(boundingVolume.extend))) + return false; + + if (!Serialize(context, boundingVolume.aabb)) + return false; + + if (!Serialize(context, boundingVolume.obb)) + return false; + + return true; + } + + /*! + * \brief Unserializes a BoundingVolume + * \return true if successfully unserialized + * + * \param context Serialization context + * \param boundingVolume Output bounding volume + * + * \remark The resulting oriented box corners will *not* be updated, a call to Update is required + */ + template + bool Unserialize(SerializationContext& context, BoundingVolume* boundingVolume) + { + UInt8 extend; + if (!Unserialize(context, &extend)) + return false; + + if (extend > Extend_Max) + return false; + + boundingVolume->extend = extend; + + if (!Unserialize(context, &boundingVolume->aabb)) + return false; + + if (!Unserialize(context, &boundingVolume->obb)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Box.hpp b/include/Nazara/Math/Box.hpp index d8de5bdab..a7bf6523e 100644 --- a/include/Nazara/Math/Box.hpp +++ b/include/Nazara/Math/Box.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_BOX_HPP #define NAZARA_BOX_HPP +#include #include #include #include @@ -96,6 +97,9 @@ namespace Nz typedef Box Boxui; typedef Box Boxi32; typedef Box Boxui32; + + template bool Serialize(SerializationContext& context, const Box& box); + template bool Unserialize(SerializationContext& context, Box* box); } template diff --git a/include/Nazara/Math/Box.inl b/include/Nazara/Math/Box.inl index 8c8d655ef..2c8b0e5d5 100644 --- a/include/Nazara/Math/Box.inl +++ b/include/Nazara/Math/Box.inl @@ -920,6 +920,68 @@ namespace Nz return box; } + + /*! + * \brief Serializes a Box + * \return true if successfully serialized + * + * \param context Serialization context + * \param box Input Box + */ + template + bool Serialize(SerializationContext& context, const Box& box) + { + if (!Serialize(context, box.x)) + return false; + + if (!Serialize(context, box.y)) + return false; + + if (!Serialize(context, box.z)) + return false; + + if (!Serialize(context, box.width)) + return false; + + if (!Serialize(context, box.height)) + return false; + + if (!Serialize(context, box.depth)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Box + * \return true if successfully unserialized + * + * \param context Serialization context + * \param box Output Box + */ + template + bool Unserialize(SerializationContext& context, Box* box) + { + if (!Unserialize(context, &box->x)) + return false; + + if (!Unserialize(context, &box->y)) + return false; + + if (!Unserialize(context, &box->z)) + return false; + + if (!Unserialize(context, &box->width)) + return false; + + if (!Unserialize(context, &box->height)) + return false; + + if (!Unserialize(context, &box->depth)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/EulerAngles.hpp b/include/Nazara/Math/EulerAngles.hpp index 3c9a6b684..544ebff5a 100644 --- a/include/Nazara/Math/EulerAngles.hpp +++ b/include/Nazara/Math/EulerAngles.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_EULERANGLES_HPP #define NAZARA_EULERANGLES_HPP +#include #include #include #include @@ -61,6 +62,9 @@ namespace Nz typedef EulerAngles EulerAnglesd; typedef EulerAngles EulerAnglesf; + + template bool Serialize(SerializationContext& context, const EulerAngles& eulerAngles); + template bool Unserialize(SerializationContext& context, EulerAngles* eulerAngles); } template std::ostream& operator<<(std::ostream& out, const Nz::EulerAngles& angles); diff --git a/include/Nazara/Math/EulerAngles.inl b/include/Nazara/Math/EulerAngles.inl index 5c442f6c9..8b4011e80 100644 --- a/include/Nazara/Math/EulerAngles.inl +++ b/include/Nazara/Math/EulerAngles.inl @@ -330,6 +330,50 @@ namespace Nz return angles; } + + /*! + * \brief Serializes a EulerAngles + * \return true if successfully serialized + * + * \param context Serialization context + * \param angles Input euler angles + */ + template + bool Serialize(SerializationContext& context, const EulerAngles& angles) + { + if (!Serialize(context, angles.pitch)) + return false; + + if (!Serialize(context, angles.yaw)) + return false; + + if (!Serialize(context, angles.roll)) + return false; + + return true; + } + + /*! + * \brief Unserializes a EulerAngles + * \return true if successfully unserialized + * + * \param context Serialization context + * \param angles Output euler angles + */ + template + bool Unserialize(SerializationContext& context, EulerAngles* angles) + { + if (!Unserialize(context, &angles->pitch)) + return false; + + if (!Unserialize(context, &angles->yaw)) + return false; + + if (!Unserialize(context, &angles->roll)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Frustum.hpp b/include/Nazara/Math/Frustum.hpp index 44c3bc0e7..ab0447351 100644 --- a/include/Nazara/Math/Frustum.hpp +++ b/include/Nazara/Math/Frustum.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_FRUSTUM_HPP #define NAZARA_FRUSTUM_HPP +#include #include #include #include @@ -60,6 +61,9 @@ namespace Nz typedef Frustum Frustumd; typedef Frustum Frustumf; + + template bool Serialize(SerializationContext& context, const Frustum& frustum); + template bool Unserialize(SerializationContext& context, Frustum* frustum); } template diff --git a/include/Nazara/Math/Frustum.inl b/include/Nazara/Math/Frustum.inl index 4ff2b8f1b..6889f92f4 100644 --- a/include/Nazara/Math/Frustum.inl +++ b/include/Nazara/Math/Frustum.inl @@ -674,6 +674,56 @@ namespace Nz << " Right: " << m_planes[FrustumPlane_Right].ToString() << "\n" << " Top: " << m_planes[FrustumPlane_Top].ToString() << ")\n"; } + + /*! + * \brief Serializes a Frustum + * \return true if successfully serialized + * + * \param context Serialization context + * \param matrix Input frustum + */ + template + bool Serialize(SerializationContext& context, const Frustum& frustum) + { + for (unsigned int i = 0; i <= BoxCorner_Max; ++i) + { + if (!Serialize(context, m_corners[i])) + return false; + } + + for (unsigned int i = 0; i <= FrustumPlane_Max; ++i) + { + if (!Serialize(context, m_planes[i])) + return false; + } + + return true; + } + + /*! + * \brief Unserializes a Matrix4 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param matrix Output matrix + */ + template + bool Unserialize(SerializationContext& context, Frustum* frustum) + { + for (unsigned int i = 0; i <= BoxCorner_Max; ++i) + { + if (!Unserialize(context, &m_corners[i])) + return false; + } + + for (unsigned int i = 0; i <= FrustumPlane_Max; ++i) + { + if (!Unserialize(context, &m_planes[i])) + return false; + } + + return true; + } } /*! diff --git a/include/Nazara/Math/Matrix4.hpp b/include/Nazara/Math/Matrix4.hpp index 211e8eedb..687eb4e0d 100644 --- a/include/Nazara/Math/Matrix4.hpp +++ b/include/Nazara/Math/Matrix4.hpp @@ -9,6 +9,7 @@ ///FIXME: Matrices column-major, difficile de bosser avec (Tout passer en row-major et transposer dans les shaders ?) +#include #include #include @@ -138,6 +139,9 @@ namespace Nz typedef Matrix4 Matrix4d; typedef Matrix4 Matrix4f; + + template bool Serialize(SerializationContext& context, const Matrix4& matrix); + template bool Unserialize(SerializationContext& context, Matrix4* matrix); } template std::ostream& operator<<(std::ostream& out, const Nz::Matrix4& matrix); diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index 6d84c241e..161ba96ad 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -1761,6 +1761,44 @@ namespace Nz return matrix; } + + /*! + * \brief Serializes a Matrix4 + * \return true if successfully serialized + * + * \param context Serialization context + * \param matrix Input matrix + */ + template + bool Serialize(SerializationContext& context, const Matrix4& matrix) + { + for (unsigned int i = 0; i < 16; ++i) + { + if (!Serialize(context, matrix[i])) + return false; + } + + return true; + } + + /*! + * \brief Unserializes a Matrix4 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param matrix Output matrix + */ + template + bool Unserialize(SerializationContext& context, Matrix4* matrix) + { + for (unsigned int i = 0; i < 16; ++i) + { + if (!Unserialize(context, &matrix[i])) + return false; + } + + return true; + } } /*! diff --git a/include/Nazara/Math/OrientedBox.hpp b/include/Nazara/Math/OrientedBox.hpp index 627f4cbe9..6ec640de2 100644 --- a/include/Nazara/Math/OrientedBox.hpp +++ b/include/Nazara/Math/OrientedBox.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_ORIENTEDBOX_HPP #define NAZARA_ORIENTEDBOX_HPP +#include #include #include #include @@ -67,6 +68,9 @@ namespace Nz typedef OrientedBox OrientedBoxd; typedef OrientedBox OrientedBoxf; + + template bool Serialize(SerializationContext& context, const OrientedBox& obb); + template bool Unserialize(SerializationContext& context, OrientedBox* obb); } template diff --git a/include/Nazara/Math/OrientedBox.inl b/include/Nazara/Math/OrientedBox.inl index 188b6d8fc..e6f42b286 100644 --- a/include/Nazara/Math/OrientedBox.inl +++ b/include/Nazara/Math/OrientedBox.inl @@ -430,6 +430,42 @@ namespace Nz return orientedBox; } + + /*! + * \brief Serializes a OrientedBox + * \return true if successfully serialized + * + * \param context Serialization context + * \param obb Input oriented box + * + * \remark Does not save OBB corners + */ + template + bool Serialize(SerializationContext& context, const OrientedBox& obb) + { + if (!Serialize(context, obb.localBox)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Matrix4 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param obb Output oriented box + * + * \remark The resulting oriented box corners will *not* be updated, a call to Update is required + */ + template + bool Unserialize(SerializationContext& context, OrientedBox* obb) + { + if (!Unserialize(context, &obb->localBox)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Plane.hpp b/include/Nazara/Math/Plane.hpp index 70bc06051..fbc1a3f12 100644 --- a/include/Nazara/Math/Plane.hpp +++ b/include/Nazara/Math/Plane.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_PLANE_HPP #define NAZARA_PLANE_HPP +#include #include #include @@ -57,6 +58,9 @@ namespace Nz typedef Plane Planed; typedef Plane Planef; + + template bool Serialize(SerializationContext& context, const Plane& plane); + template bool Unserialize(SerializationContext& context, Plane* plane); } template diff --git a/include/Nazara/Math/Plane.inl b/include/Nazara/Math/Plane.inl index 29bd829b2..6dccba3d7 100644 --- a/include/Nazara/Math/Plane.inl +++ b/include/Nazara/Math/Plane.inl @@ -424,6 +424,44 @@ namespace Nz return plane; } + + /*! + * \brief Serializes a Vector2 + * \return true if successfully serialized + * + * \param context Serialization context + * \param plane Input Vector2 + */ + template + bool Serialize(SerializationContext& context, const Plane& plane) + { + if (!Serialize(context, plane.normal)) + return false; + + if (!Serialize(context, plane.distance)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Plane + * \return true if successfully unserialized + * + * \param context Serialization context + * \param plane Output Plane + */ + template + bool Unserialize(SerializationContext& context, Plane* plane) + { + if (!Unserialize(context, &plane->normal)) + return false; + + if (!Unserialize(context, &plane->distance)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Quaternion.hpp b/include/Nazara/Math/Quaternion.hpp index f33dec0b6..b984cea69 100644 --- a/include/Nazara/Math/Quaternion.hpp +++ b/include/Nazara/Math/Quaternion.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_QUATERNION_HPP #define NAZARA_QUATERNION_HPP +#include #include namespace Nz @@ -88,6 +89,9 @@ namespace Nz typedef Quaternion Quaterniond; typedef Quaternion Quaternionf; + + template bool Serialize(SerializationContext& context, const Quaternion& quat); + template bool Unserialize(SerializationContext& context, Quaternion* quat); } template std::ostream& operator<<(std::ostream& out, const Nz::Quaternion& quat); diff --git a/include/Nazara/Math/Quaternion.inl b/include/Nazara/Math/Quaternion.inl index 75e3966d3..012c3578a 100644 --- a/include/Nazara/Math/Quaternion.inl +++ b/include/Nazara/Math/Quaternion.inl @@ -831,6 +831,57 @@ namespace Nz return quaternion; } + + + /*! + * \brief Serializes a Quaternion + * \return true if successfully serialized + * + * \param context Serialization context + * \param quat Input Quaternion + */ + template + bool Serialize(SerializationContext& context, const Quaternion& quat) + { + if (!Serialize(context, quat.x)) + return false; + + if (!Serialize(context, quat.y)) + return false; + + if (!Serialize(context, quat.z)) + return false; + + if (!Serialize(context, quat.w)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Quaternion + * \return true if successfully unserialized + * + * \param context Serialization context + * \param quat Output Quaternion + */ + template + bool Unserialize(SerializationContext& context, Quaternion* quat) + { + if (!Unserialize(context, &quat->x)) + return false; + + if (!Unserialize(context, &quat->y)) + return false; + + if (!Unserialize(context, &quat->z)) + return false; + + if (!Unserialize(context, &quat->w)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Ray.hpp b/include/Nazara/Math/Ray.hpp index c4c53664d..22c520dfc 100644 --- a/include/Nazara/Math/Ray.hpp +++ b/include/Nazara/Math/Ray.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_RAY_HPP #define NAZARA_RAY_HPP +#include #include #include #include @@ -73,6 +74,9 @@ namespace Nz typedef Ray Rayd; typedef Ray Rayf; + + template bool Serialize(SerializationContext& context, const Ray& ray); + template bool Unserialize(SerializationContext& context, Ray* ray); } template std::ostream& operator<<(std::ostream& out, const Nz::Ray& vec); diff --git a/include/Nazara/Math/Ray.inl b/include/Nazara/Math/Ray.inl index e83c39488..c601f018b 100644 --- a/include/Nazara/Math/Ray.inl +++ b/include/Nazara/Math/Ray.inl @@ -762,6 +762,44 @@ namespace Nz { return Ray(Nz::Vector3::Lerp(from.origin, to.origin, interpolation), Nz::Vector3::Lerp(from.direction, to.direction, interpolation)); } + + /*! + * \brief Serializes a Ray + * \return true if successfully serialized + * + * \param context Serialization context + * \param ray Input Ray + */ + template + bool Serialize(SerializationContext& context, const Ray& ray) + { + if (!Serialize(context, ray.origin)) + return false; + + if (!Serialize(context, ray.direction)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Ray + * \return true if successfully unserialized + * + * \param context Serialization context + * \param ray Output Ray + */ + template + bool Unserialize(SerializationContext& context, Ray* ray) + { + if (!Unserialize(context, &ray->origin)) + return false; + + if (!Unserialize(context, &ray->direction)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Rect.hpp b/include/Nazara/Math/Rect.hpp index ec203848f..f51e88c8a 100644 --- a/include/Nazara/Math/Rect.hpp +++ b/include/Nazara/Math/Rect.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_RECT_HPP #define NAZARA_RECT_HPP +#include #include #include #include @@ -90,6 +91,9 @@ namespace Nz typedef Rect Rectui; typedef Rect Recti32; typedef Rect Rectui32; + + template bool Serialize(SerializationContext& context, const Rect& rect); + template bool Unserialize(SerializationContext& context, Rect* rect); } template diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 9a2737a02..2f72b30ec 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -816,6 +816,56 @@ namespace Nz return rect; } + + /*! + * \brief Serializes a Rect + * \return true if successfully serialized + * + * \param context Serialization context + * \param rect Input Rect + */ + template + bool Serialize(SerializationContext& context, const Rect& rect) + { + if (!Serialize(context, rect.x)) + return false; + + if (!Serialize(context, rect.y)) + return false; + + if (!Serialize(context, rect.width)) + return false; + + if (!Serialize(context, rect.height)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Rect + * \return true if successfully unserialized + * + * \param context Serialization context + * \param rect Output Rect + */ + template + bool Unserialize(SerializationContext& context, Rect* rect) + { + if (!Unserialize(context, &rect->x)) + return false; + + if (!Unserialize(context, &rect->y)) + return false; + + if (!Unserialize(context, &rect->width)) + return false; + + if (!Unserialize(context, &rect->height)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Sphere.hpp b/include/Nazara/Math/Sphere.hpp index 65203c4b0..e2d766744 100644 --- a/include/Nazara/Math/Sphere.hpp +++ b/include/Nazara/Math/Sphere.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_SPHERE_HPP #define NAZARA_SPHERE_HPP +#include #include #include @@ -80,6 +81,9 @@ namespace Nz typedef Sphere Sphered; typedef Sphere Spheref; + + template bool Serialize(SerializationContext& context, const Sphere& sphere); + template bool Unserialize(SerializationContext& context, Sphere* sphere); } template diff --git a/include/Nazara/Math/Sphere.inl b/include/Nazara/Math/Sphere.inl index eac46d76c..4dca542fb 100644 --- a/include/Nazara/Math/Sphere.inl +++ b/include/Nazara/Math/Sphere.inl @@ -674,6 +674,56 @@ namespace Nz return sphere; } + + /*! + * \brief Serializes a Sphere + * \return true if successfully serialized + * + * \param context Serialization context + * \param sphere Input Sphere + */ + template + bool Serialize(SerializationContext& context, const Sphere& sphere) + { + if (!Serialize(context, sphere.x)) + return false; + + if (!Serialize(context, sphere.y)) + return false; + + if (!Serialize(context, sphere.z)) + return false; + + if (!Serialize(context, sphere.radius)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Sphere + * \return true if successfully unserialized + * + * \param context Serialization context + * \param sphere Output Sphere + */ + template + bool Unserialize(SerializationContext& context, Sphere* sphere) + { + if (!Unserialize(context, &sphere->x)) + return false; + + if (!Unserialize(context, &sphere->y)) + return false; + + if (!Unserialize(context, &sphere->z)) + return false; + + if (!Unserialize(context, &sphere->radius)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Vector2.hpp b/include/Nazara/Math/Vector2.hpp index 3712f0d09..d96420a67 100644 --- a/include/Nazara/Math/Vector2.hpp +++ b/include/Nazara/Math/Vector2.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_VECTOR2_HPP #define NAZARA_VECTOR2_HPP +#include #include namespace Nz @@ -106,6 +107,9 @@ namespace Nz typedef Vector2 Vector2ui; typedef Vector2 Vector2i32; typedef Vector2 Vector2ui32; + + template bool Serialize(SerializationContext& context, const Vector2& vector); + template bool Unserialize(SerializationContext& context, Vector2* vector); } template std::ostream& operator<<(std::ostream& out, const Nz::Vector2& vec); diff --git a/include/Nazara/Math/Vector2.inl b/include/Nazara/Math/Vector2.inl index 6fd58cf38..6ee7af3e0 100644 --- a/include/Nazara/Math/Vector2.inl +++ b/include/Nazara/Math/Vector2.inl @@ -940,7 +940,6 @@ namespace Nz * * \see MakeUnitY */ - template Vector2 Vector2::UnitY() { @@ -956,7 +955,6 @@ namespace Nz * * \see MakeZero */ - template Vector2 Vector2::Zero() { @@ -965,6 +963,44 @@ namespace Nz return vector; } + + /*! + * \brief Serializes a Vector2 + * \return true if successfully serialized + * + * \param context Serialization context + * \param vector Input Vector2 + */ + template + bool Serialize(SerializationContext& context, const Vector2& vector) + { + if (!Serialize(context, vector.x)) + return false; + + if (!Serialize(context, vector.y)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Vector2 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param vector Output Vector2 + */ + template + bool Unserialize(SerializationContext& context, Vector2* vector) + { + if (!Unserialize(context, &vector->x)) + return false; + + if (!Unserialize(context, &vector->y)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Vector3.hpp b/include/Nazara/Math/Vector3.hpp index 0ab620406..c14ab503a 100644 --- a/include/Nazara/Math/Vector3.hpp +++ b/include/Nazara/Math/Vector3.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_VECTOR3_HPP #define NAZARA_VECTOR3_HPP +#include #include namespace Nz @@ -125,6 +126,9 @@ namespace Nz typedef Vector3 Vector3ui; typedef Vector3 Vector3i32; typedef Vector3 Vector3ui32; + + template bool Serialize(SerializationContext& context, const Vector3& vector); + template bool Unserialize(SerializationContext& context, Vector3* vector); } template std::ostream& operator<<(std::ostream& out, const Nz::Vector3& vec); diff --git a/include/Nazara/Math/Vector3.inl b/include/Nazara/Math/Vector3.inl index c386f146d..b6159d867 100644 --- a/include/Nazara/Math/Vector3.inl +++ b/include/Nazara/Math/Vector3.inl @@ -28,7 +28,6 @@ namespace Nz * \param Y Y component * \param Z Z component */ - template Vector3::Vector3(T X, T Y, T Z) { @@ -41,7 +40,6 @@ namespace Nz * \param X X component * \param vec vec.X = Y component and vec.y = Z component */ - template Vector3::Vector3(T X, const Vector2& vec) { @@ -53,7 +51,6 @@ namespace Nz * * \param scale X component = Y component = Z component */ - template Vector3::Vector3(T scale) { @@ -65,7 +62,6 @@ namespace Nz * * \param vec[3] vec[0] is X component, vec[1] is Y component and vec[2] is Z component */ - template Vector3::Vector3(const T vec[3]) { @@ -78,7 +74,6 @@ namespace Nz * \param vec vec.X = X component and vec.y = Y component * \param Z Z component */ - template Vector3::Vector3(const Vector2& vec, T Z) { @@ -90,7 +85,6 @@ namespace Nz * * \param vec Vector of type U to convert to type T */ - template template Vector3::Vector3(const Vector3& vec) @@ -103,7 +97,6 @@ namespace Nz * * \param vec Vector4 where only the first three components are taken */ - template Vector3::Vector3(const Vector4& vec) { @@ -118,7 +111,6 @@ namespace Nz * * \see DotProduct */ - template T Vector3::AbsDotProduct(const Vector3& vec) const { @@ -137,7 +129,6 @@ namespace Nz * * \see NormalizeAngle */ - template T Vector3::AngleBetween(const Vector3& vec) const { @@ -166,7 +157,6 @@ namespace Nz * * \see CrossProduct */ - template Vector3 Vector3::CrossProduct(const Vector3& vec) const { @@ -181,7 +171,6 @@ namespace Nz * * \see SquaredDistance */ - template T Vector3::Distance(const Vector3& vec) const { @@ -194,7 +183,6 @@ namespace Nz * * \param vec The other vector to measure the distance with */ - template float Vector3::Distancef(const Vector3& vec) const { @@ -209,7 +197,6 @@ namespace Nz * * \see AbsDotProduct, DotProduct */ - template T Vector3::DotProduct(const Vector3& vec) const { @@ -222,7 +209,6 @@ namespace Nz * * \see GetSquaredLength */ - template T Vector3::GetLength() const { @@ -233,7 +219,6 @@ namespace Nz * \brief Calculates the length (magnitude) of the vector * \return The length in float of the vector */ - template float Vector3::GetLengthf() const { @@ -250,7 +235,6 @@ namespace Nz * * \see Normalize */ - template Vector3 Vector3::GetNormal(T* length) const { @@ -266,7 +250,6 @@ namespace Nz * * \see GetLength */ - template T Vector3::GetSquaredLength() const { @@ -279,7 +262,6 @@ namespace Nz * * \see Backward */ - template Vector3& Vector3::MakeBackward() { @@ -292,7 +274,6 @@ namespace Nz * * \see Down */ - template Vector3& Vector3::MakeDown() { @@ -305,7 +286,6 @@ namespace Nz * * \see Forward */ - template Vector3& Vector3::MakeForward() { @@ -318,7 +298,6 @@ namespace Nz * * \see Left */ - template Vector3& Vector3::MakeLeft() { @@ -331,7 +310,6 @@ namespace Nz * * \see Right */ - template Vector3& Vector3::MakeRight() { @@ -344,7 +322,6 @@ namespace Nz * * \see Unit */ - template Vector3& Vector3::MakeUnit() { @@ -357,7 +334,6 @@ namespace Nz * * \see UnitX */ - template Vector3& Vector3::MakeUnitX() { @@ -370,7 +346,6 @@ namespace Nz * * \see UnitY */ - template Vector3& Vector3::MakeUnitY() { @@ -383,7 +358,6 @@ namespace Nz * * \see UnitZ */ - template Vector3& Vector3::MakeUnitZ() { @@ -396,7 +370,6 @@ namespace Nz * * \see Up */ - template Vector3& Vector3::MakeUp() { @@ -409,7 +382,6 @@ namespace Nz * * \see Zero */ - template Vector3& Vector3::MakeZero() { @@ -424,7 +396,6 @@ namespace Nz * * \see Minimize */ - template Vector3& Vector3::Maximize(const Vector3& vec) { @@ -448,7 +419,6 @@ namespace Nz * * \see Maximize */ - template Vector3& Vector3::Minimize(const Vector3& vec) { @@ -474,7 +444,6 @@ namespace Nz * * \see GetNormal */ - template Vector3& Vector3::Normalize(T* length) { @@ -501,7 +470,6 @@ namespace Nz * \param Y Y component * \param Z Z component */ - template Vector3& Vector3::Set(T X, T Y, T Z) { @@ -518,7 +486,6 @@ namespace Nz * \param X X component * \param vec vec.X = Y component and vec.y = Z component */ - template Vector3& Vector3::Set(T X, const Vector2& vec) { @@ -535,7 +502,6 @@ namespace Nz * * \param scale X component = Y component = Z component */ - template Vector3& Vector3::Set(T scale) { @@ -552,7 +518,6 @@ namespace Nz * * \param vec[3] vec[0] is X component, vec[1] is Y component and vec[2] is Z component */ - template Vector3& Vector3::Set(const T vec[3]) { @@ -567,7 +532,6 @@ namespace Nz * \param vec vec.X = X component and vec.y = Y component * \param Z Z component */ - template Vector3& Vector3::Set(const Vector2& vec, T Z) { @@ -584,7 +548,6 @@ namespace Nz * * \param vec The other vector */ - template Vector3& Vector3::Set(const Vector3& vec) { @@ -599,7 +562,6 @@ namespace Nz * * \param vec Vector of type U to convert its components */ - template template Vector3& Vector3::Set(const Vector3& vec) @@ -617,7 +579,6 @@ namespace Nz * * \param vec Vector4 where only the first three components are taken */ - template Vector3& Vector3::Set(const Vector4& vec) { @@ -636,7 +597,6 @@ namespace Nz * * \see Distance */ - template T Vector3::SquaredDistance(const Vector3& vec) const { @@ -647,7 +607,6 @@ namespace Nz * \brief Gives a string representation * \return A string representation of the object: "Vector3(x, y, z)" */ - template String Vector3::ToString() const { @@ -662,7 +621,6 @@ namespace Nz * * \remark Access to index greather than 2 is undefined behavior */ - template Vector3::operator T* () { @@ -675,7 +633,6 @@ namespace Nz * * \remark Access to index greather than 2 is undefined behavior */ - template Vector3::operator const T* () const { @@ -686,7 +643,6 @@ namespace Nz * \brief Helps to represent the sign of the vector * \return A constant reference to this vector */ - template const Vector3& Vector3::operator+() const { @@ -697,7 +653,6 @@ namespace Nz * \brief Negates the components of the vector * \return A constant reference to this vector with negate components */ - template Vector3 Vector3::operator-() const { @@ -710,7 +665,6 @@ namespace Nz * * \param vec The other vector to add components with */ - template Vector3 Vector3::operator+(const Vector3& vec) const { @@ -723,7 +677,6 @@ namespace Nz * * \param vec The other vector to substract components with */ - template Vector3 Vector3::operator-(const Vector3& vec) const { @@ -736,7 +689,6 @@ namespace Nz * * \param vec The other vector to multiply components with */ - template Vector3 Vector3::operator*(const Vector3& vec) const { @@ -749,7 +701,6 @@ namespace Nz * * \param scale The scalar to multiply components with */ - template Vector3 Vector3::operator*(T scale) const { @@ -765,7 +716,6 @@ namespace Nz * \remark Produce a NazaraError if one of the vec components is null with NAZARA_MATH_SAFE defined * \throw std::domain_error if NAZARA_MATH_SAFE is defined and one of the vec components is null */ - template Vector3 Vector3::operator/(const Vector3& vec) const { @@ -791,7 +741,6 @@ namespace Nz * \remark Produce a NazaraError if scale is null with NAZARA_MATH_SAFE defined * \throw std::domain_error if NAZARA_MATH_SAFE is defined and scale is null */ - template Vector3 Vector3::operator/(T scale) const { @@ -814,7 +763,6 @@ namespace Nz * * \param vec The other vector to add components with */ - template Vector3& Vector3::operator+=(const Vector3& vec) { @@ -831,7 +779,6 @@ namespace Nz * * \param vec The other vector to substract components with */ - template Vector3& Vector3::operator-=(const Vector3& vec) { @@ -848,7 +795,6 @@ namespace Nz * * \param vec The other vector to multiply components with */ - template Vector3& Vector3::operator*=(const Vector3& vec) { @@ -865,7 +811,6 @@ namespace Nz * * \param vec The other vector to multiply components with */ - template Vector3& Vector3::operator*=(T scale) { @@ -885,7 +830,6 @@ namespace Nz * \remark Produce a NazaraError if one of the vec components is null with NAZARA_MATH_SAFE defined * \throw std::domain_error if NAZARA_MATH_SAFE is defined and one of the vec components is null */ - template Vector3& Vector3::operator/=(const Vector3& vec) { @@ -913,7 +857,6 @@ namespace Nz * \remark Produce a NazaraError if scale is null with NAZARA_MATH_SAFE defined * \throw std::domain_error if NAZARA_MATH_SAFE is defined and scale is null */ - template Vector3& Vector3::operator/=(T scale) { @@ -938,7 +881,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator==(const Vector3& vec) const { @@ -953,7 +895,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator!=(const Vector3& vec) const { @@ -966,7 +907,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator<(const Vector3& vec) const { @@ -987,7 +927,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator<=(const Vector3& vec) const { @@ -1008,7 +947,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator>(const Vector3& vec) const { @@ -1021,7 +959,6 @@ namespace Nz * * \param vec Other vector to compare with */ - template bool Vector3::operator>=(const Vector3& vec) const { @@ -1037,7 +974,6 @@ namespace Nz * * \see CrossProduct */ - template Vector3 Vector3::CrossProduct(const Vector3& vec1, const Vector3& vec2) { @@ -1053,7 +989,6 @@ namespace Nz * * \see AbsDotProduct, DotProduct */ - template T Vector3::DotProduct(const Vector3& vec1, const Vector3& vec2) { @@ -1066,7 +1001,6 @@ namespace Nz * * \see MakeBackward */ - template Vector3 Vector3::Backward() { @@ -1082,7 +1016,6 @@ namespace Nz * * \see MakeDown */ - template Vector3 Vector3::Down() { @@ -1098,7 +1031,6 @@ namespace Nz * * \see Forward */ - template Vector3 Vector3::Forward() { @@ -1114,7 +1046,6 @@ namespace Nz * * \see MakeLeft */ - template Vector3 Vector3::Left() { @@ -1136,7 +1067,6 @@ namespace Nz * * \see Lerp */ - template Vector3 Vector3::Lerp(const Vector3& from, const Vector3& to, T interpolation) { @@ -1158,7 +1088,6 @@ namespace Nz * * \see GetNormal */ - template Vector3 Vector3::Normalize(const Vector3& vec) { @@ -1171,7 +1100,6 @@ namespace Nz * * \see MakeRight */ - template Vector3 Vector3::Right() { @@ -1187,7 +1115,6 @@ namespace Nz * * \see MakeUnit */ - template Vector3 Vector3::Unit() { @@ -1203,7 +1130,6 @@ namespace Nz * * \see MakeUnitX */ - template Vector3 Vector3::UnitX() { @@ -1219,7 +1145,6 @@ namespace Nz * * \see MakeUnitY */ - template Vector3 Vector3::UnitY() { @@ -1235,7 +1160,6 @@ namespace Nz * * \see MakeUnitZ */ - template Vector3 Vector3::UnitZ() { @@ -1251,7 +1175,6 @@ namespace Nz * * \see MakeUp */ - template Vector3 Vector3::Up() { @@ -1267,7 +1190,6 @@ namespace Nz * * \see MakeZero */ - template Vector3 Vector3::Zero() { @@ -1276,6 +1198,50 @@ namespace Nz return vector; } + + /*! + * \brief Serializes a Vector3 + * \return true if successfully serialized + * + * \param context Serialization context + * \param vector Input Vector3 + */ + template + bool Serialize(SerializationContext& context, const Vector3& vector) + { + if (!Serialize(context, vector.x)) + return false; + + if (!Serialize(context, vector.y)) + return false; + + if (!Serialize(context, vector.z)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Vector3 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param vector Output Vector3 + */ + template + bool Unserialize(SerializationContext& context, Vector3* vector) + { + if (!Unserialize(context, &vector->x)) + return false; + + if (!Unserialize(context, &vector->y)) + return false; + + if (!Unserialize(context, &vector->z)) + return false; + + return true; + } } /*! diff --git a/include/Nazara/Math/Vector4.hpp b/include/Nazara/Math/Vector4.hpp index 319f18e3b..f8e6d6895 100644 --- a/include/Nazara/Math/Vector4.hpp +++ b/include/Nazara/Math/Vector4.hpp @@ -7,6 +7,7 @@ #ifndef NAZARA_VECTOR4_HPP #define NAZARA_VECTOR4_HPP +#include #include namespace Nz @@ -104,6 +105,9 @@ namespace Nz typedef Vector4 Vector4ui; typedef Vector4 Vector4i32; typedef Vector4 Vector4ui32; + + template bool Serialize(SerializationContext& context, const Vector4& vector); + template bool Unserialize(SerializationContext& context, Vector4* vector); } template std::ostream& operator<<(std::ostream& out, const Nz::Vector4& vec); diff --git a/include/Nazara/Math/Vector4.inl b/include/Nazara/Math/Vector4.inl index 01c771f8f..9c5fd92c7 100644 --- a/include/Nazara/Math/Vector4.inl +++ b/include/Nazara/Math/Vector4.inl @@ -1013,6 +1013,56 @@ namespace Nz return vector; } + + /*! + * \brief Serializes a Vector4 + * \return true if successfully serialized + * + * \param context Serialization context + * \param vector Input Vector3 + */ + template + bool Serialize(SerializationContext& context, const Vector4& vector) + { + if (!Serialize(context, vector.x)) + return false; + + if (!Serialize(context, vector.y)) + return false; + + if (!Serialize(context, vector.z)) + return false; + + if (!Serialize(context, vector.w)) + return false; + + return true; + } + + /*! + * \brief Unserializes a Vector4 + * \return true if successfully unserialized + * + * \param context Serialization context + * \param vector Output Vector3 + */ + template + bool Unserialize(SerializationContext& context, Vector4* vector) + { + if (!Unserialize(context, &vector->x)) + return false; + + if (!Unserialize(context, &vector->y)) + return false; + + if (!Unserialize(context, &vector->z)) + return false; + + if (!Unserialize(context, &vector->w)) + return false; + + return true; + } } /*!