Added OrientedCube class

Former-commit-id: ca42e08a8bf73b5801bea1536bd8a8af5e386574
This commit is contained in:
Lynix 2013-02-21 18:27:08 +01:00
parent 283b551d56
commit 2e6b864cad
2 changed files with 307 additions and 0 deletions

View File

@ -0,0 +1,73 @@
// 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_ORIENTEDCUBE_HPP
#define NAZARA_ORIENTEDCUBE_HPP
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Cube.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector3.hpp>
template<typename T>
class NzOrientedCube
{
public:
NzOrientedCube() = default;
NzOrientedCube(T X, T Y, T Z, T Width, T Height, T Depth);
NzOrientedCube(const NzCube<T>& cube);
NzOrientedCube(const NzVector3<T>& vec1, const NzVector3<T>& vec2);
template<typename U> explicit NzOrientedCube(const NzOrientedCube<U>& orientedCube);
NzOrientedCube(const NzOrientedCube& orientedCube) = default;
~NzOrientedCube() = default;
const NzVector3<T>& GetCorner(nzCorner corner) const;
bool IsValid() const;
NzOrientedCube& MakeZero();
NzOrientedCube& Set(T X, T Y, T Z, T Width, T Height, T Depth);
NzOrientedCube& Set(const NzCube<T>& cube);
NzOrientedCube& Set(const NzOrientedCube& orientedCube);
NzOrientedCube& Set(const NzVector3<T>& vec1, const NzVector3<T>& vec2);
template<typename U> NzOrientedCube& Set(const NzOrientedCube<U>& orientedCube);
NzString ToString() const;
void Update(const NzMatrix4<T>& transformMatrix);
operator NzVector3<T>*();
operator const NzVector3<T>*() const;
NzVector3<T>& operator()(unsigned int i);
NzVector3<T> operator()(unsigned int i) const;
NzOrientedCube operator*(T scalar) const;
NzOrientedCube& operator*=(T scalar);
bool operator==(const NzOrientedCube& cube) const;
bool operator!=(const NzOrientedCube& cube) const;
static NzOrientedCube Lerp(const NzOrientedCube& from, const NzOrientedCube& to, T interpolation);
static NzOrientedCube Zero();
NzCube<T> localCube;
private:
NzVector3<T> m_corners[nzCorner_Max+1]; // Ne peuvent pas être modifiés directement
};
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzOrientedCube<T>& orientedCube);
typedef NzOrientedCube<double> NzOrientedCubed;
typedef NzOrientedCube<float> NzOrientedCubef;
#include <Nazara/Math/OrientedCube.inl>
#endif // NAZARA_ORIENTEDCUBE_HPP

View File

@ -0,0 +1,234 @@
// 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 <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Basic.hpp>
#include <cstring>
#include <Nazara/Core/Debug.hpp>
#define F(a) static_cast<T>(a)
template<typename T>
NzOrientedCube<T>::NzOrientedCube(T X, T Y, T Z, T Width, T Height, T Depth)
{
Set(X, Y, Z, Width, Height, Depth);
}
template<typename T>
NzOrientedCube<T>::NzOrientedCube(const NzCube<T>& cube)
{
Set(cube);
}
template<typename T>
NzOrientedCube<T>::NzOrientedCube(const NzVector3<T>& vec1, const NzVector3<T>& vec2)
{
Set(vec1, vec2);
}
template<typename T>
template<typename U>
NzOrientedCube<T>::NzOrientedCube(const NzOrientedCube<U>& orientedCube)
{
Set(orientedCube);
}
template<typename T>
const NzVector3<T>& NzOrientedCube<T>::GetCorner(nzCorner corner) const
{
#ifdef NAZARA_DEBUG
if (corner > nzCorner_Max)
{
NazaraError("Corner not handled (0x" + NzString::Number(corner, 16) + ')');
static NzVector3<T> dummy;
return dummy;
}
#endif
return m_corners[corner];
}
template<typename T>
bool NzOrientedCube<T>::IsValid() const
{
return localCube.IsValid();
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::MakeZero()
{
localCube.MakeZero();
return *this;
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::Set(T X, T Y, T Z, T Width, T Height, T Depth)
{
localCube.Set(X, Y, Z, Width, Height, Depth);
return *this;
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::Set(const NzCube<T>& cube)
{
localCube.Set(cube);
return *this;
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::Set(const NzOrientedCube& orientedCube)
{
std::memcpy(this, &orientedCube, sizeof(NzOrientedCube));
return *this;
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::Set(const NzVector3<T>& vec1, const NzVector3<T>& vec2)
{
localCube.Set(vec1, vec2);
return *this;
}
template<typename T>
template<typename U>
NzOrientedCube<T>& NzOrientedCube<T>::Set(const NzOrientedCube<U>& orientedCube)
{
for (unsigned int i = 0; i <= nzCorner_Max; ++i)
m_corners[i].Set(orientedCube.m_corners[i]);
localCube = orientedCube.localCube;
return *this;
}
template<typename T>
NzString NzOrientedCube<T>::ToString() const
{
NzStringStream ss;
return ss << "OrientedCube(FLB: " << m_corners[nzCorner_FarLeftBottom].ToString() << "\n"
<< " FLT: " << m_corners[nzCorner_FarLeftTop].ToString() << "\n"
<< " FRB: " << m_corners[nzCorner_FarRightBottom].ToString() << "\n"
<< " FRT: " << m_corners[nzCorner_FarRightTop].ToString() << "\n"
<< " NLB: " << m_corners[nzCorner_NearLeftBottom].ToString() << "\n"
<< " NLT: " << m_corners[nzCorner_NearLeftTop].ToString() << "\n"
<< " NRB: " << m_corners[nzCorner_NearRightBottom].ToString() << "\n"
<< " NRT: " << m_corners[nzCorner_NearRightTop].ToString() << ")\n";
}
template<typename T>
void NzOrientedCube<T>::Update(const NzMatrix4<T>& transformMatrix)
{
for (unsigned int i = 0; i <= nzCorner_Max; ++i)
m_corners[i] = transformMatrix.Transform(localCube.GetCorner(static_cast<nzCorner>(i)));
}
template<typename T>
NzOrientedCube<T>::operator NzVector3<T>*()
{
return &m_corners[0];
}
template<typename T>
NzOrientedCube<T>::operator const NzVector3<T>*() const
{
return &m_corners[0];
}
template<typename T>
NzVector3<T>& NzOrientedCube<T>::operator()(unsigned int i)
{
#if NAZARA_MATH_SAFE
if (i > nzCorner_Max)
{
NzStringStream ss;
ss << "Index out of range: (" << i << " >= 3)";
NazaraError(ss);
throw std::out_of_range(ss.ToString());
}
#endif
return &m_corners[i];
}
template<typename T>
NzVector3<T> NzOrientedCube<T>::operator()(unsigned int i) const
{
#if NAZARA_MATH_SAFE
if (i > nzCorner_Max)
{
NzStringStream ss;
ss << "Index out of range: (" << i << " >= 3)";
NazaraError(ss);
throw std::out_of_range(ss.ToString());
}
#endif
return &m_corners[i];
}
template<typename T>
NzOrientedCube<T> NzOrientedCube<T>::operator*(T scalar) const
{
NzOrientedCube box(*this);
box *= scalar;
return box;
}
template<typename T>
NzOrientedCube<T>& NzOrientedCube<T>::operator*=(T scalar)
{
localCube *= scalar;
return *this;
}
template<typename T>
bool NzOrientedCube<T>::operator==(const NzOrientedCube& box) const
{
return localCube == box.localCube;
}
template<typename T>
bool NzOrientedCube<T>::operator!=(const NzOrientedCube& box) const
{
return !operator==(box);
}
template<typename T>
NzOrientedCube<T> NzOrientedCube<T>::Lerp(const NzOrientedCube& from, const NzOrientedCube& to, T interpolation)
{
NzOrientedCube orientedCube;
orientedCube.Set(NzCube<T>::Lerp(from.localCube, to.localCube, interpolation));
return orientedCube;
}
template<typename T>
NzOrientedCube<T> NzOrientedCube<T>::Zero()
{
NzOrientedCube orientedCube;
orientedCube.MakeZero();
return orientedCube;
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzOrientedCube<T>& orientedCube)
{
return out << orientedCube.ToString();
}
#undef F
#include <Nazara/Core/DebugOff.hpp>