fixed a runtime divide by zero crash for integral type template instanciations and made the class trivial copyable to increase performance

This commit is contained in:
Marco Langer 2021-05-02 19:38:39 +02:00 committed by Seth Hillbrand
parent fb7bc263de
commit 1606bfc9f8
1 changed files with 20 additions and 27 deletions

View File

@ -20,6 +20,8 @@
#ifndef VECTOR3_H_ #ifndef VECTOR3_H_
#define VECTOR3_H_ #define VECTOR3_H_
#include <limits>
/** /**
* Traits class for VECTOR2. * Traits class for VECTOR2.
*/ */
@ -55,31 +57,20 @@ public:
static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max(); static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min(); static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
T x, y, z; T x{};
T y{};
T z{};
/// Construct a 3D-vector with x, y = 0 /// Construct a 3D-vector with x, y, z = 0
VECTOR3(); VECTOR3() = default;
/// Construct a vector with given components x, y /// Construct a vector with given components x, y, z
VECTOR3( T x, T y, T z ); VECTOR3( T x, T y, T z );
/// Initializes a vector from another specialization. Beware of rounding /// Initializes a vector from another specialization. Beware of rounding
/// issues. /// issues.
template <typename CastingType> template <typename CastingType>
VECTOR3( const VECTOR3<CastingType>& aVec ) VECTOR3( const VECTOR3<CastingType>& aVec );
{
x = (T) aVec.x;
y = (T) aVec.y;
z = (T) aVec.z;
}
/// Copy a vector
VECTOR3( const VECTOR3<T>& aVec )
{
x = aVec.x;
y = aVec.y;
z = aVec.z;
}
/** /**
* Compute cross product of self with \a aVector * Compute cross product of self with \a aVector
@ -114,18 +105,17 @@ public:
template <class T> template <class T>
VECTOR3<T>::VECTOR3() VECTOR3<T>::VECTOR3( T aX, T aY, T aZ ) :
x( aX ), y( aY ), z( aZ )
{ {
x = y = z = 0.0;
} }
template <class T> template <class T>
VECTOR3<T>::VECTOR3( T aX, T aY, T aZ ) template <typename CastingType>
VECTOR3<T>::VECTOR3( const VECTOR3<CastingType>& aVec ) :
x( aVec.x ), y( aVec.y ), z( aVec.z )
{ {
x = aX;
y = aY;
z = aZ;
} }
@ -159,9 +149,12 @@ template <class T>
VECTOR3<T> VECTOR3<T>::Normalize() VECTOR3<T> VECTOR3<T>::Normalize()
{ {
T norm = EuclideanNorm(); T norm = EuclideanNorm();
x /= norm; if( norm > T( 0 ) )
y /= norm; {
z /= norm; x /= norm;
y /= norm;
z /= norm;
}
return *this; return *this;
} }