Removed dependency on boost::operators

This commit is contained in:
David Williams 2008-05-26 19:37:37 +00:00
parent 22fd38b255
commit 2745d52dc5
3 changed files with 96 additions and 85 deletions

View File

@ -25,21 +25,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <iostream> #include <iostream>
#include "boost/cstdint.hpp" #include "boost/cstdint.hpp"
#include <boost/operators.hpp>
namespace PolyVox namespace PolyVox
{ {
//template <boost::uint32_t Size, typename Type> class Matrix; //Forward declaration
///Represents a vector in space. ///Represents a vector in space.
template <boost::uint32_t Size, typename Type> template <boost::uint32_t Size, typename Type>
class Vector class Vector
: boost::addable< Vector<Size,Type> // Vector + Vector
, boost::subtractable< Vector<Size,Type> // Vector - Vector
, boost::dividable2< Vector<Size,Type>, Type // Vector / Type
, boost::multipliable2< Vector<Size,Type>, Type // Vector * Type, Type * Vector
, boost::equality_comparable1< Vector<Size,Type> // Vector != Vector
> > > > >
{ {
public: public:
///Constructor. ///Constructor.
@ -113,24 +104,38 @@ namespace PolyVox
}; };
//Non-member overloaded operators. //Non-member overloaded operators.
//!Multiplication operator. ///Addition operator.
//template <boost::uint32_t Size,typename Type> template <boost::uint32_t Size,typename Type>
//Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Matrix<Size,Type>& rhs) throw(); Vector<Size,Type> operator+(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw();
//!Multiplication operator. ///Subtraction operator.
//template <boost::uint32_t Size,typename Type> template <boost::uint32_t Size,typename Type>
//Vector<Size,Type> operator*(const Matrix<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw(); Vector<Size,Type> operator-(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw();
///Multiplication operator.
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Type& rhs) throw();
///Division operator.
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator/(const Vector<Size,Type>& lhs, const Type& rhs) throw();
///Stream insertion operator. ///Stream insertion operator.
template <boost::uint32_t Size, typename Type> template <boost::uint32_t Size, typename Type>
std::ostream& operator<<(std::ostream& os, const Vector<Size,Type>& vector) throw(); std::ostream& operator<<(std::ostream& os, const Vector<Size,Type>& vector) throw();
//Some handy typedefs //Some handy typedefs
///A 3D Vector of floats.
typedef Vector<3,float> Vector3DFloat; typedef Vector<3,float> Vector3DFloat;
///A 3D Vector of doubles.
typedef Vector<3,double> Vector3DDouble; typedef Vector<3,double> Vector3DDouble;
///A 3D Vector of signed 8-bit values.
typedef Vector<3,boost::int8_t> Vector3DInt8; typedef Vector<3,boost::int8_t> Vector3DInt8;
///A 3D Vector of unsigned 8-bit values.
typedef Vector<3,boost::uint8_t> Vector3DUint8; typedef Vector<3,boost::uint8_t> Vector3DUint8;
///A 3D Vector of signed 16-bit values.
typedef Vector<3,boost::int16_t> Vector3DInt16; typedef Vector<3,boost::int16_t> Vector3DInt16;
///A 3D Vector of unsigned 16-bit values.
typedef Vector<3,boost::uint16_t> Vector3DUint16; typedef Vector<3,boost::uint16_t> Vector3DUint16;
///A 3D Vector of signed 32-bit values.
typedef Vector<3,boost::int32_t> Vector3DInt32; typedef Vector<3,boost::int32_t> Vector3DInt32;
///A 3D Vector of unsigned 32-bit values.
typedef Vector<3,boost::uint32_t> Vector3DUint32; typedef Vector<3,boost::uint32_t> Vector3DUint32;

View File

@ -196,21 +196,6 @@ namespace PolyVox
return false; return false;
} }
/**
Subtraction operator subtracts corresponding elements of one Vector from the other.
\param rhs Vector to subtract
\return The resulting Vector.
*/
template <boost::uint32_t Size, typename Type>
inline Vector<Size, Type>& Vector<Size, Type>::operator-=(const Vector<Size, Type>& rhs) throw()
{
for(boost::uint32_t ct = 0; ct < Size; ++ct)
{
m_tElements[ct] -= rhs.m_tElements[ct];
}
return *this;
}
/** /**
Addition operator adds corresponding elements of the two Vectors. Addition operator adds corresponding elements of the two Vectors.
\param rhs Vector to add \param rhs Vector to add
@ -226,6 +211,21 @@ namespace PolyVox
return *this; return *this;
} }
/**
Subtraction operator subtracts corresponding elements of one Vector from the other.
\param rhs Vector to subtract
\return The resulting Vector.
*/
template <boost::uint32_t Size, typename Type>
inline Vector<Size, Type>& Vector<Size, Type>::operator-=(const Vector<Size, Type>& rhs) throw()
{
for(boost::uint32_t ct = 0; ct < Size; ++ct)
{
m_tElements[ct] -= rhs.m_tElements[ct];
}
return *this;
}
/** /**
Multiplication operator multiplies each element of the Vector by a number. Multiplication operator multiplies each element of the Vector by a number.
\param rhs the number the Vector is multiplied by. \param rhs the number the Vector is multiplied by.
@ -256,56 +256,6 @@ namespace PolyVox
return *this; return *this;
} }
/**
Multiplies a Vector and a Matrix.
NOTE: Although the Vector class does not distinguish between row and column vectors, the Vector
must <i>conceptually</i> be in <i>row</i> form for this operation to make sense.
\param lhs The Vector to be multiplied
\param rhs The Matrix to multiply by.
\return The resulting Vector
*/
/*template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Matrix<Size,Type>& rhs) throw()
{
Type tZero = static_cast<Type>(0);
Vector<Size,Type> result(tZero,tZero,tZero);
for(boost::uint32_t colCt = 0; colCt < Size; ++colCt)
{
for(boost::uint32_t rowCt = 0; rowCt < Size; ++rowCt)
{
//result(colCt) += lhs(rowCt) * rhs(rowCt, colCt);
result.set(colCt, result(colCt) + (lhs(rowCt) * rhs(rowCt, colCt)));
}
}
return result;
}*/
/**
Multiplies a Matrix and a Vector.
NOTE: Although the Vector class does not distinguish between row and column vectors, the Vector
must <i>conceptually</i> be in <i>column</i> form for this operation to make sense.
\param lhs The Matrix to be multiplied
\param rhs The Vector to multiply by.
\return The resulting Vector
*/
/*template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator*(const Matrix<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
{
Type tZero = static_cast<Type>(0);
Vector<Size,Type> result(tZero,tZero,tZero);
for(boost::uint32_t rowCt = 0; rowCt < Size; ++rowCt)
{
for(boost::uint32_t colCt = 0; colCt < Size; ++colCt)
{
//result(rowCt) += lhs(rowCt,colCt) * rhs(colCt);
result.set(rowCt, result(rowCt) + (lhs(rowCt,colCt) * rhs(colCt)));
}
}
return result;
}*/
/** /**
Enables the Vector to be used intuitively with output streams such as cout. Enables the Vector to be used intuitively with output streams such as cout.
\param os The output stream to write to. \param os The output stream to write to.
@ -525,4 +475,60 @@ namespace PolyVox
m_tElements[ct] /= static_cast<Type>(length); m_tElements[ct] /= static_cast<Type>(length);
} }
} }
/**
Addition operator adds corresponding elements of the two Vectors.
\param lhs Vector to add to.
\param rhs Vector to add.
\return The resulting Vector.
*/
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator+(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
{
Vector<Size,Type> result = lhs;
result += rhs;
return result;
}
/**
Subtraction operator subtracts corresponding elements of one Vector from the other.
\param lhs Vector to subtract from.
\param rhs Vector to subtract.
\return The resulting Vector.
*/
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator-(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
{
Vector<Size,Type> result = lhs;
result -= rhs;
return result;
}
/**
Multiplication operator multiplies each element of the Vector by a number.
\param lhs the Vector to multiply.
\param rhs the number the Vector is multiplied by.
\return The resulting Vector.
*/
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Type& rhs) throw()
{
Vector<Size,Type> result = lhs;
result *= rhs;
return result;
}
/**
Division operator divides each element of the Vector by a number.
\param lhs the Vector to divide.
\param rhs the number the Vector is divided by.
\return The resulting Vector.
*/
template <boost::uint32_t Size,typename Type>
Vector<Size,Type> operator/(const Vector<Size,Type>& lhs, const Type& rhs) throw()
{
Vector<Size,Type> result = lhs;
result /= rhs;
return result;
}
}//namespace Thermite }//namespace Thermite

View File

@ -383,7 +383,7 @@ namespace PolyVox
volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0)); volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));
} }
const Vector3DFloat gradCeil = computeSobelGradient(volIter); const Vector3DFloat gradCeil = computeSobelGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001) if(result.lengthSquared() < 0.0001)
{ {
//Operation failed - fall back on simple gradient estimation //Operation failed - fall back on simple gradient estimation
@ -407,7 +407,7 @@ namespace PolyVox
volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0)); volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));
} }
const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001) if(result.lengthSquared() < 0.0001)
{ {
//Operation failed - fall back on simple gradient estimation //Operation failed - fall back on simple gradient estimation
@ -785,7 +785,7 @@ namespace PolyVox
volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0)); volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));
} }
const Vector3DFloat gradCeil = computeSobelGradient(volIter); const Vector3DFloat gradCeil = computeSobelGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001) if(result.lengthSquared() < 0.0001)
{ {
//Operation failed - fall back on simple gradient estimation //Operation failed - fall back on simple gradient estimation
@ -809,7 +809,7 @@ namespace PolyVox
volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0)); volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));
} }
const Vector3DFloat gradCeil = computeSmoothCentralDifferenceGradient(volIter); const Vector3DFloat gradCeil = computeSmoothCentralDifferenceGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001) if(result.lengthSquared() < 0.0001)
{ {
//Operation failed - fall back on simple gradient estimation //Operation failed - fall back on simple gradient estimation