From 28fcb06025c423cbd7edf6d50cd63330f8c1b9a2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 19 Feb 2013 01:20:15 +0100 Subject: [PATCH] Added Plane class Former-commit-id: fbbb1f230b4a9f85de0b56b4924ab65f74b4b654 --- include/Nazara/Math/Plane.hpp | 54 +++++++++++ include/Nazara/Math/Plane.inl | 163 ++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 include/Nazara/Math/Plane.hpp create mode 100644 include/Nazara/Math/Plane.inl diff --git a/include/Nazara/Math/Plane.hpp b/include/Nazara/Math/Plane.hpp new file mode 100644 index 000000000..598f77c24 --- /dev/null +++ b/include/Nazara/Math/Plane.hpp @@ -0,0 +1,54 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Mathematics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_PLANE_HPP +#define NAZARA_PLANE_HPP + +#include +#include + +template +class NzPlane +{ + public: + NzPlane() = default; + NzPlane(T normalX, T normalY, T normalZ, T Distance); + NzPlane(const T plane[4]); + NzPlane(const NzVector3& Normal, T Distance); + NzPlane(const NzVector3& Normal, const NzVector3& point); + NzPlane(const NzVector3& point1, const NzVector3& point2, const NzVector3& point3); + template explicit NzPlane(const NzPlane& plane); + NzPlane(const NzPlane& plane) = default; + ~NzPlane() = default; + + T Distance(const NzVector3& point); + T Distance(T x, T y, T z); + + NzPlane& Set(T normalX, T normalY, T normalZ, T Distance); + NzPlane& Set(const T plane[4]); + NzPlane& Set(const NzPlane& plane); + NzPlane& Set(const NzVector3& Normal, T Distance); + NzPlane& Set(const NzVector3& Normal, const NzVector3& point); + NzPlane& Set(const NzVector3& point1, const NzVector3& point2, const NzVector3& point3); + template NzPlane& Set(const NzPlane& plane); + + NzString ToString() const; + + static NzPlane Lerp(const NzPlane& from, const NzPlane& to, T interpolation); + + NzVector3 normal; + T distance; +}; + +template +std::ostream& operator<<(std::ostream& out, const NzPlane& plane); + +typedef NzPlane NzPlaned; +typedef NzPlane NzPlanef; + +#include + +#endif // NAZARA_PLANE_HPP diff --git a/include/Nazara/Math/Plane.inl b/include/Nazara/Math/Plane.inl new file mode 100644 index 000000000..5cfd7e5c6 --- /dev/null +++ b/include/Nazara/Math/Plane.inl @@ -0,0 +1,163 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Mathematics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +#define F(a) static_cast(a) + +template +NzPlane::NzPlane(T normalX, T normalY, T normalZ, T D) +{ + Set(normalX, normalY, normalZ, D); +} + +template +NzPlane::NzPlane(const T plane[4]) +{ + Set(plane); +} + +template +NzPlane::NzPlane(const NzVector3& Normal, T D) +{ + Set(Normal, D); +} + +template +NzPlane::NzPlane(const NzVector3& Normal, const NzVector3& point) +{ + Set(Normal, point); +} + +template +NzPlane::NzPlane(const NzVector3& point1, const NzVector3& point2, const NzVector3& point3) +{ + Set(point1, point2, point3); +} + +template +template +NzPlane::NzPlane(const NzPlane& plane) +{ + Set(plane); +} + +template +T NzPlane::Distance(const NzVector3& point) +{ + return normal.DotProduct(point) + distance; +} + +template +T NzPlane::Distance(T x, T y, T z) +{ + return Distance(NzVector3(x, y, z)); +} + +template +NzPlane& NzPlane::Set(T normalX, T normalY, T normalZ, T D) +{ + distance = D; + normal.Set(normalX, normalY, normalZ); + + return *this; +} + +template +NzPlane& NzPlane::Set(const T plane[4]) +{ + normal.Set(plane[0], plane[1], plane[2]); + distance = plane[3]; + + return *this; +} + +template +NzPlane& NzPlane::Set(const NzPlane& plane) +{ + std::memcpy(this, &plane, sizeof(NzPlane)); + + return *this; +} + +template +NzPlane& NzPlane::Set(const NzVector3& Normal, T D) +{ + distance = D; + normal = Normal; + + return *this; +} + +template +NzPlane& NzPlane::Set(const NzVector3& Normal, const NzVector3& point) +{ + normal = Normal; + distance = -normal.DotProduct(point); + + return *this; +} + +template +NzPlane& NzPlane::Set(const NzVector3& point1, const NzVector3& point2, const NzVector3& point3) +{ + NzVector3 edge1 = point2 - point1; + NzVector3 edge2 = point3 - point1; + normal = edge1.CrossProduct(edge2); + normal.Normalize(); + + distance = -normal.DotProduct(point3); + + return *this; +} + +template +template +NzPlane& NzPlane::Set(const NzPlane& plane) +{ + normal.Set(plane.normal); + distance = F(plane.distance); + + return *this; +} + +template +NzString NzPlane::ToString() const +{ + NzStringStream ss; + + return ss << "Plane(Normal: " << normal << "; Distance: " << distance << ')'; +} + +template +NzPlane NzPlane::Lerp(const NzPlane& from, const NzPlane& to, T interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return NzPlane(); + } + #endif + + NzPlane plane; + plane.distance = NzLerp(from.distance, to.distance, interpolation); + plane.normal = NzVector3::Lerp(from.normal, to.normal, interpolation); + plane.normal.Normalize(); + + return plane; +} + +template +std::ostream& operator<<(std::ostream& out, const NzPlane& plane) +{ + return out << plane.ToString(); +} + +#undef F + +#include