Added magnitude and normalization

Refactored  `types.h` to `Vectors.h`
This commit is contained in:
trotFunky 2019-10-14 22:36:08 +02:00
parent 52acb4044c
commit 4da6167836
4 changed files with 34 additions and 6 deletions

View file

@ -18,7 +18,7 @@ add_executable(tests_opengl
src/DataHandling/Texture.h
src/DataHandling/Model3D.cpp
src/DataHandling/Model3D.h
src/types.h
src/Vectors.h
src/KeyStateManager.cpp
src/KeyStateManager.h)

View file

@ -10,7 +10,7 @@
#include <iostream>
#include "GL/glut.h"
#include "types.h"
#include "Vectors.h"
#include "Texture.h"
class Model3D {

View file

@ -8,7 +8,7 @@
#include <map>
#include <GL/glut.h>
#include "types.h"
#include "Vectors.h"
/// Handles the key and mouse events from glut and keep their status up to date.

View file

@ -2,10 +2,11 @@
// Created by trotfunky on 30/09/2019.
//
#ifndef TESTS_OPENGL_TYPES_H
#define TESTS_OPENGL_TYPES_H
#ifndef TESTS_OPENGL_VECTORS_H
#define TESTS_OPENGL_VECTORS_H
#include <fstream>
#include <cmath>
/// Group of coordinates with the input operator overloaded.
/// \tparam T Content of the vector
@ -15,6 +16,32 @@ struct CoordinatesVector
{
T coordinates[N];
/// Computes the euclidean norm of the vector.
/// \return The magnitude.
double magnitude()
{
// TODO : Save magnitude if the vector has not been modified.
double sum_of_squares = 0;
for (unsigned int i = 0;i<N;i++)
{
sum_of_squares += coordinates[i]*coordinates[i];
}
return sqrt(sum_of_squares);
}
// Use SFINAE to allow normalization of floating point
/// Normalizes the vector using its euclidean norm.
/// \return Nothing, the vector is normalized in-place.
typename std::enable_if<std::is_floating_point<T>::value,void>::type
normalize()
{
double norm = magnitude();
for (unsigned int i = 0;i<N;i++)
{
coordinates[i] /= norm;
}
}
// Use SFINAE to declare the function if and only if the type is arithmetic
template <typename Scalar>
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,N>>::type
@ -67,6 +94,7 @@ struct CoordinatesVector
}
};
// Define some specializations to add x,y,z references to the coordinates.
template <typename T>
struct Vec2 : public CoordinatesVector<T,2>
@ -133,4 +161,4 @@ typedef Vec3<int> Vec3i;
typedef Vec3<float> Vec3f;
#endif //TESTS_OPENGL_TYPES_H
#endif //TESTS_OPENGL_VECTORS_H