Specified copy constructor for Vec2 and Vec3 to change the x,y and z references and not keep references to the coordinates of the original object.

Fixed the SFINAE not triggering on normalize() because there was no template substitution.
This commit is contained in:
trotFunky 2019-10-19 00:56:55 +02:00
parent 4da6167836
commit 8cdcc9750b

View file

@ -29,10 +29,12 @@ struct CoordinatesVector
return sqrt(sum_of_squares); return sqrt(sum_of_squares);
} }
// Use SFINAE to allow normalization of floating point // Use SFINAE to allow normalization of floating point.
// The template argument is used to force substitution and thus checking the enable_if.
/// Normalizes the vector using its euclidean norm. /// Normalizes the vector using its euclidean norm.
/// \return Nothing, the vector is normalized in-place. /// \return Nothing, the vector is normalized in-place.
typename std::enable_if<std::is_floating_point<T>::value,void>::type template <typename U = T>
typename std::enable_if<std::is_floating_point<U>::value,void>::type
normalize() normalize()
{ {
double norm = magnitude(); double norm = magnitude();
@ -104,6 +106,11 @@ struct Vec2 : public CoordinatesVector<T,2>
Vec2() = default; Vec2() = default;
Vec2(const Vec2<T>& original): x(CoordinatesVector<T,2>::coordinates[0]), y(CoordinatesVector<T,2>::coordinates[1])
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
}
// Cast-copy constructor in order to use the operators of the mother class. // Cast-copy constructor in order to use the operators of the mother class.
Vec2(const CoordinatesVector<T,2>& origin) Vec2(const CoordinatesVector<T,2>& origin)
{ {
@ -133,12 +140,17 @@ struct Vec3 : CoordinatesVector<T,3>
Vec3() = default; Vec3() = default;
// Cast-copy constructor in order to use the operators of the mother class. Vec3(const Vec3<T>& original): x(CoordinatesVector<T,3>::coordinates[0]), y(CoordinatesVector<T,3>::coordinates[1]), z(CoordinatesVector<T,3>::coordinates[2])
Vec3(const CoordinatesVector<T,3>& origin)
{ {
if (this != &origin) std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
}
// Cast-copy constructor in order to use the operators of the mother class.
Vec3(const CoordinatesVector<T,3>& original)
{
if (this != &original)
{ {
std::copy(std::begin(origin.coordinates),std::end(origin.coordinates),std::begin(CoordinatesVector<T,2>::coordinates)); std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
} }
} }