summaryrefslogtreecommitdiffstats
path: root/engine/include/symbasic
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-12 23:24:05 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-12 23:24:05 +0100
commitc35dfbd0bb8a1abfef876bb6dbe20945867a5270 (patch)
tree895fbe6a09c24c45f09d33da7bf46a2e19e0155c /engine/include/symbasic
parentinitialization (diff)
downloadflatland-c35dfbd0bb8a1abfef876bb6dbe20945867a5270.tar.gz
flatland-c35dfbd0bb8a1abfef876bb6dbe20945867a5270.zip
edas
Diffstat (limited to 'engine/include/symbasic')
-rw-r--r--engine/include/symbasic/.test.cpp.swpbin0 -> 12288 bytes
-rw-r--r--engine/include/symbasic/dvector.h80
-rw-r--r--engine/include/symbasic/dvector.tpp147
-rw-r--r--engine/include/symbasic/svector.h78
-rw-r--r--engine/include/symbasic/svector.tpp162
-rw-r--r--engine/include/symbasic/vector.h133
-rw-r--r--engine/include/symbasic/vector.tpp213
7 files changed, 813 insertions, 0 deletions
diff --git a/engine/include/symbasic/.test.cpp.swp b/engine/include/symbasic/.test.cpp.swp
new file mode 100644
index 0000000..b30055c
--- /dev/null
+++ b/engine/include/symbasic/.test.cpp.swp
Binary files differ
diff --git a/engine/include/symbasic/dvector.h b/engine/include/symbasic/dvector.h
new file mode 100644
index 0000000..99dadee
--- /dev/null
+++ b/engine/include/symbasic/dvector.h
@@ -0,0 +1,80 @@
+#ifndef __DVECTOR_H__
+#define __DVECTOR_H__
+
+#include <vector>
+#include <initializer_list>
+
+#include "vector.h"
+
+template<class T>
+class DVector : public Vector<T>
+{
+
+public :
+
+ DVector(size_t dim = 0);
+ DVector(const std::vector<T>&);
+ DVector(const std::initializer_list<T> &init);
+
+ virtual const size_t size() const override;
+
+ virtual T& operator[](size_t i) override;
+ virtual const T& operator[](size_t i) const override;
+
+ DVector& operator<<(const T&);
+
+ /* Plus / minus */
+ DVector<T>& operator+=(const Vector<T>&);
+ DVector<T>& operator-=(const Vector<T>&);
+
+ /* Scalar multiplication / division */
+ DVector<T>& operator*=(double k);
+ DVector<T>& operator/=(double k);
+
+ /* Reverse operator */
+ DVector<T>& operator~();
+
+ /* Unit vector */
+ DVector<T>& unit();
+
+private:
+
+ std::vector<T> components;
+};
+
+template<class T>
+const DVector<T> operator+(DVector<T> , const Vector<T>& );
+
+template<class T>
+const DVector<T> operator-(DVector<T> , const Vector<T>& );
+
+template<class T>
+const DVector<T> operator*(DVector<T> , double);
+
+template<class T>
+const DVector<T> operator/(DVector<T> , double);
+
+/*template<class T>
+const DVector<T> operator+(const Vector<T>&, DVector<T>);
+
+template<class T>
+const DVector<T> operator-(const Vector<T>&, DVector<T>);*/
+
+template<class T>
+const DVector<T> operator*(double, DVector<T>);
+
+template<class T>
+const DVector<T> operator/(double, DVector<T>);
+
+
+template<class T>
+const DVector<T> operator-(DVector<T>);
+
+inline const DVector<double> operator^(const DVector<double>& v, const Vector<double>&);
+
+/*
+ * Include definitions file
+ */
+#include "dvector.tpp"
+
+#endif
diff --git a/engine/include/symbasic/dvector.tpp b/engine/include/symbasic/dvector.tpp
new file mode 100644
index 0000000..3aa7923
--- /dev/null
+++ b/engine/include/symbasic/dvector.tpp
@@ -0,0 +1,147 @@
+#ifndef __DVECTOR_TPP__
+#define __DVECTOR_TPP__
+
+template<class T>
+DVector<T>::DVector(size_t dim)
+ : components(dim)
+{
+
+}
+
+template<class T>
+DVector<T>::DVector(const std::vector<T>& init)
+ : components(init)
+{
+
+}
+
+template<class T>
+DVector<T>::DVector(const std::initializer_list<T> &init)
+ : components(init.begin(), init.end())
+{
+
+}
+
+template<class T>
+const size_t DVector<T>::size() const
+{
+ return components.size();
+}
+
+template<class T>
+T& DVector<T>::operator[](size_t i)
+{
+ return components[i];
+}
+
+template<class T>
+const T& DVector<T>::operator[](size_t i) const
+{
+ return components[i];
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator<<(const T &value)
+{
+ components.push_back(value);
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator+=(const Vector<T> &w)
+{
+ return static_cast<DVector<T>&>(this->add(w));
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator-=(const Vector<T> &w)
+{
+ return static_cast<DVector<T>&>(this->sub(w));
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator*=(double k)
+{
+ return static_cast<DVector<T>&>(this->mult(k));
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator/=(double k)
+{
+ return static_cast<DVector<T>&>(this->divide(k));
+}
+
+template<class T>
+DVector<T>& DVector<T>::operator~()
+{
+ return static_cast<DVector<T>&>(this->reverse());
+}
+
+template<class T>
+DVector<T>& DVector<T>::unit()
+{
+ return static_cast<DVector<T>&>(this->_unit());
+}
+
+template<class T>
+const DVector<T> operator-(DVector<T> v)
+{
+ return ~v;
+}
+
+template<class T>
+const DVector<T> operator+(DVector<T> v, const Vector<T>& w)
+{
+ return v += w;
+}
+
+template<class T>
+const DVector<T> operator-(DVector<T> v, const Vector<T>& w)
+{
+ return v -= w;
+}
+
+template<class T>
+const DVector<T> operator*(DVector<T> v, double k)
+{
+ return v *= k;
+}
+
+template<class T>
+const DVector<T> operator/(DVector<T> v, double k)
+{
+ return v /= k;
+}
+
+template<class T>
+const DVector<T> operator*(double k, DVector<T> v)
+{
+ return v *= k;
+}
+
+template<class T>
+const DVector<T> operator/(double k, DVector<T> v)
+{
+ return v /= k;
+}
+
+inline const DVector<double> operator^(const DVector<double>& v, const Vector<double>& w)
+{
+ DVector<double> u;
+
+ size_t n = (v.size() <= w.size()) ? v.size() : w.size();
+
+ if (n > 2)
+ {
+ u << v[1] * w[2] - v[2] * w[1];
+ u << v[2] * w[0] - v[0] * w[2];
+ u << v[0] * w[1] - v[1] * w[0];
+
+ } else if (n == 2)
+ u << v[0] * w[1] - v[1] * w[0];
+ else if (n == 1)
+ u << v[0] * w[0];
+
+ return u;
+}
+
+#endif
diff --git a/engine/include/symbasic/svector.h b/engine/include/symbasic/svector.h
new file mode 100644
index 0000000..c53ae0f
--- /dev/null
+++ b/engine/include/symbasic/svector.h
@@ -0,0 +1,78 @@
+#ifndef __STATIC_VECTOR_H__
+#define __STATIC_VECTOR_H__
+
+#include <array>
+#include "vector.h"
+
+template<class T, std::size_t N>
+class SVector : public Vector<T>
+{
+
+public:
+
+ /* Constructors */
+
+ SVector(const T &value = T());
+ SVector(const std::initializer_list<T> &init);
+ SVector(const SVector<T, N>&);
+
+ SVector& operator=(const SVector<T, N>&);
+
+ virtual const size_t size() const override;
+
+ virtual T& operator[](size_t i) override;
+ virtual const T& operator[](size_t i) const override;
+
+ /* Plus / minus */
+ SVector<T, N>& operator+=(const Vector<T>&);
+ SVector<T, N>& operator-=(const Vector<T>&);
+
+ /* Scalar multiplication / division */
+ SVector<T, N>& operator*=(double k);
+ SVector<T, N>& operator/=(double k);
+
+ /* Reverse operator */
+ SVector<T, N>& operator~();
+
+ /* Unit vector */
+ SVector<T, N>& unit();
+
+private:
+
+ std::array<T, N> components;
+};
+
+template<class T, std::size_t N>
+const SVector<T, N> operator+(SVector<T, N> , const Vector<T>& );
+
+template<class T, std::size_t N>
+const SVector<T, N> operator-(SVector<T, N> , const Vector<T>& );
+
+template<class T, std::size_t N>
+const SVector<T, N> operator*(SVector<T, N> , double);
+
+template<class T, std::size_t N>
+const SVector<T, N> operator/(SVector<T, N> , double);
+
+template<class T, std::size_t N>
+const SVector<T, N> operator*(double, SVector<T, N>);
+
+template<class T, std::size_t N>
+const SVector<T, N> operator/(double, SVector<T, N>);
+
+template<class T, std::size_t N>
+const SVector<T, N> operator-(SVector<T, N>);
+
+/*
+ * Prototipe for cross product
+ */
+inline const SVector<double, 3> operator^(const SVector<double, 3>& v, const SVector<double, 3>& w);
+inline double operator^(const SVector<double, 2>& v, const SVector<double, 2>& w);
+inline double operator^(const SVector<double, 1>& v, const SVector<double, 1>& w);
+
+/*
+ * Include definitions file
+ */
+#include "svector.tpp"
+
+#endif // __SVECTOR_H__
diff --git a/engine/include/symbasic/svector.tpp b/engine/include/symbasic/svector.tpp
new file mode 100644
index 0000000..e054049
--- /dev/null
+++ b/engine/include/symbasic/svector.tpp
@@ -0,0 +1,162 @@
+#ifndef __SVECTOR_TPP__
+#define __SVECTOR_TPP__
+
+template<class T, std::size_t N>
+SVector<T, N>::SVector(const T &value)
+{
+ components.fill(value);
+}
+
+template<class T, std::size_t N>
+SVector<T, N>::SVector(const std::initializer_list<T> &init)
+{
+ size_t n = (init.size() < N) ? init.size() : N;
+
+ /* copy init list without include <algorithm> */
+ size_t index = 0;
+
+ /* copy the initializer list until its size */
+ for (auto c : init)
+ {
+ if (index >= n)
+ break;
+
+ components[index++] = c;
+ }
+
+ /* fill the remaining components with zero */
+ while(index < N)
+ components[index++] = 0;
+}
+
+template<class T, std::size_t N>
+SVector<T, N>::SVector(const SVector<T, N> &cpy)
+{
+ this->components = cpy.components;
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator=(const SVector<T, N> &v)
+{
+ this->components = v.components;
+}
+
+template<class T, std::size_t N>
+const size_t SVector<T, N>::size() const
+{
+ return N;
+}
+
+/* Access operators */
+template<class T, std::size_t N>
+T& SVector<T, N>::operator[](size_t i)
+{
+ return components[i];
+}
+
+template<class T, std::size_t N>
+const T& SVector<T, N>::operator[](size_t i) const
+{
+ return components[i];
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator+=(const Vector<T> &w)
+{
+ return static_cast<SVector<T, N>&>(this->add(w));
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator-=(const Vector<T> &w)
+{
+ return static_cast<SVector<T, N>&>(this->sub(w));
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator*=(double k)
+{
+ return static_cast<SVector<T, N>&>(this->mult(k));
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator/=(double k)
+{
+ return static_cast<SVector<T, N>&>(this->divide(k));
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::operator~()
+{
+ return static_cast<SVector<T, N>&>(this->reverse());
+}
+
+template<class T, std::size_t N>
+SVector<T, N>& SVector<T, N>::unit()
+{
+ return static_cast<SVector<T, N>&>(this->_unit());
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator+(SVector<T, N> v, const Vector<T>& w)
+{
+ return v += w;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator-(SVector<T, N> v, const Vector<T>& w)
+{
+ return v -= w;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator*(SVector<T, N> v, double k)
+{
+ return v *= k;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator/(SVector<T, N> v, double k)
+{
+ return v /= k;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator*(double k, SVector<T, N> v)
+{
+ return v *= k;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator/(double k, SVector<T, N> v)
+{
+ return v /= k;
+}
+
+template<class T, std::size_t N>
+const SVector<T, N> operator-(SVector<T, N> v)
+{
+ return ~v;
+}
+
+inline const SVector<double, 3> operator^(const SVector<double, 3>& v, const SVector<double, 3>& w)
+{
+ SVector<double, 3> u = {0, 0, 0};
+
+ u[0] = v[1] * w[2] - v[2] * w[1];
+ u[1] = v[2] * w[0] - v[0] * w[2];
+ u[2] = v[0] * w[1] - v[1] * w[0];
+
+ return u;
+}
+
+inline double operator^(const SVector<double, 2>& v, const SVector<double, 2>& w)
+{
+ return v[0] * w[1] - v[1] * w[0];
+}
+
+inline double operator^(const SVector<double, 1>& v, const SVector<double, 1>& w)
+{
+ return v[0] * w[0];
+}
+
+#endif
diff --git a/engine/include/symbasic/vector.h b/engine/include/symbasic/vector.h
new file mode 100644
index 0000000..f8574dc
--- /dev/null
+++ b/engine/include/symbasic/vector.h
@@ -0,0 +1,133 @@
+#ifndef __VECTOR_H__
+#define __VECTOR_H__
+
+#ifdef _USE_VECTOR_OSTREAM
+#include <iosfwd>
+#endif
+
+#include <cstddef>
+
+template <class T>
+class Vector {
+
+public:
+
+ typedef std::size_t size_t;
+
+ class iterator
+ {
+ size_t i;
+ Vector<T> *v;
+
+ public:
+
+ iterator(Vector<T> *v, size_t i = 0)
+ : v(v), i(i)
+ {
+ }
+
+ iterator& operator++()
+ {
+ ++i;
+ }
+
+ iterator& operator--()
+ {
+ --i;
+ }
+
+ bool operator!=(const iterator& it)
+ {
+ return this->i != it.i;
+ }
+
+ bool operator==(const iterator& it)
+ {
+ return !(*this != it);
+ }
+
+ T& operator*()
+ {
+ return (*v)[i];
+ }
+ };
+
+ typedef const iterator const_iterator;
+
+ /* For auto implementation */
+ /* it allows to loop through components
+ *
+ * for (auto comp : v)
+ * {
+ * // loop content
+ * }
+ */
+
+ iterator begin();
+ iterator end();
+
+ const_iterator begin() const;
+ const_iterator end() const;
+
+ //Virtual accesseurs
+
+ virtual const size_t size() const = 0;
+
+ /* Accède à la i-ème valeur */
+ virtual T& operator[](size_t i) = 0;
+ virtual const T& operator[](size_t i) const = 0;
+
+ /* Return the module */
+ double module() const;
+ double sq_module() const; // squared module
+
+ //Operateurs
+
+ /* Operateurs de comparaison */
+ bool operator==(const Vector<T>&) const;
+ bool operator!=(const Vector<T>&) const;
+
+ bool operator>=(const Vector<T>&) const;
+ bool operator<=(const Vector<T>&) const;
+
+ bool operator>=(double) const;
+ bool operator<=(double) const;
+
+ bool operator>(const Vector<T>&) const;
+ bool operator<(const Vector<T>&) const;
+
+ bool operator>(double) const;
+ bool operator<(double) const;
+
+ /* Dot product */
+ double operator*(const Vector<T>&) const;
+
+ /* For printing a Vector on a ostream */
+#ifdef _USE_VECTOR_OSTREAM
+ void affiche(std::ostream&) const;
+#endif
+
+protected: // not stable operations
+
+ /* Plus / minus */
+ Vector<T>& add(const Vector<T>&);
+ Vector<T>& sub(const Vector<T>&);
+
+ /* Scalar multiplication / division */
+ Vector<T>& mult(double k);
+ Vector<T>& divide(double k);
+
+ Vector<T>& reverse();
+ Vector<T>& _unit() const;
+};
+
+#ifdef _USE_VECTOR_OSTREAM
+// forward declaration of ostream
+
+template<class T>
+std::ostream& operator<<(std::ostream&, const Vector<T>&);
+#endif
+
+#include "vector.tpp"
+
+#endif
diff --git a/engine/include/symbasic/vector.tpp b/engine/include/symbasic/vector.tpp
new file mode 100644
index 0000000..d622940
--- /dev/null
+++ b/engine/include/symbasic/vector.tpp
@@ -0,0 +1,213 @@
+#ifndef __VECTOR_TPP__
+#define __VECTOR_TPP__
+
+/* for auto loop */
+
+template<class T>
+typename Vector<T>::iterator Vector<T>::begin()
+{
+ return iterator(this);
+}
+
+template<class T>
+typename Vector<T>::iterator Vector<T>::end()
+{
+ return iterator(this, this->size() - 1);
+}
+
+template<class T>
+typename Vector<T>::const_iterator Vector<T>::begin() const
+{
+ return iterator(this);
+}
+
+template<class T>
+typename Vector<T>::const_iterator Vector<T>::end() const
+{
+ return iterator(this, this->size() - 1);
+}
+
+template<class T>
+bool Vector<T>::operator==(const Vector<T>& v) const {
+
+ if(this->size() != v.size())
+ return false;
+
+ //Retourne déjà faux si les deux vecteurs n'ont pas la même dimension
+ for(size_t i(0); i < v.size(); ++i){
+
+ if ((*this)[i] != v[i])
+ return false;
+ //Lors que le programme rencontre deux coordonnées différenentes respectivement pour la même dimension des deux vecteurs
+ //la fonction retourne false
+ }
+ return true;
+}
+
+template<class T>
+bool Vector<T>::operator>=(const Vector<T>& v) const
+{
+ return sq_module() >= v.sq_module();
+}
+
+template<class T>
+bool Vector<T>::operator<=(const Vector<T>& v) const
+{
+ return sq_module() <= v.sq_module();
+}
+
+template<class T>
+bool Vector<T>::operator>=(double m) const
+{
+ return sq_module() >= m * m;
+}
+
+template<class T>
+bool Vector<T>::operator<=(double m) const
+{
+ return sq_module() <= m * m;
+}
+
+template<class T>
+bool Vector<T>::operator>(const Vector<T>& v) const
+{
+ return sq_module() > v.sq_module();
+}
+
+template<class T>
+bool Vector<T>::operator<(const Vector<T>& v) const
+{
+ return sq_module() < v.sq_module();
+}
+
+template<class T>
+bool Vector<T>::operator>(double m) const
+{
+ return sq_module() > m * m;
+}
+
+template<class T>
+bool Vector<T>::operator<(double m) const
+{
+ return sq_module() < m * m;
+}
+
+template<class T>
+bool Vector<T>::operator!=(const Vector<T>& w) const
+{
+ return !(*this == w);
+}
+
+//Operateurs somme et produit
+
+template<class T>
+Vector<T>& Vector<T>::add(const Vector<T>& w)
+{
+ size_t n = (this->size() <= w.size()) ? this->size() : w.size();
+
+ for (size_t i= 0; i < n; ++i)
+ (*this)[i] += w[i];
+
+ return *this;
+}
+
+
+template<class T>
+Vector<T>& Vector<T>::sub(const Vector<T>& w)
+{
+ size_t n = (this->size() <= w.size()) ? this->size() : w.size();
+
+ for (size_t i = 0; i < n; ++i)
+ (*this)[i] -= w[i];
+
+ return *this;
+}
+
+template<class T>
+Vector<T>& Vector<T>::mult(double k)
+{
+ for (size_t i = 0; i < this->size(); ++i)
+ (*this)[i] *= k;
+
+ return *this;
+}
+
+template<class T>
+Vector<T>& Vector<T>::divide(double k)
+{
+ for (size_t i = 0; i < this->size(); ++i)
+ (*this)[i] /= k;
+
+ return *this;
+}
+
+template<class T>
+Vector<T>& Vector<T>::_unit() const
+{
+ return (*this) /= this->module();
+}
+
+/* Dot product */
+template<class T>
+double Vector<T>::operator*(const Vector<T>& w) const
+{
+ double x = 0;
+ size_t n = (this->size() <= w.size()) ? this->size() : w.size();
+
+ for (size_t i = 0; i < n; ++i)
+ x += (*this)[i] * w[i];
+
+ return x;
+}
+
+template<class T>
+Vector<T>& Vector<T>::reverse()
+{
+ return (*this)*=-1.0;
+}
+
+template<class T>
+double Vector<T>::sq_module() const
+{
+ return (*this) * (*this);
+}
+
+template<class T>
+double Vector<T>::module() const
+{
+ return sqrt(this->sq_module());
+}
+
+/* Implementation of ostream operator overloading */
+
+/* Le prototype je le mets dans un header séparé parce que
+ * il ne fait pas partie de l'implementation d'un vecteur
+ * Une classe l'utilisant n'as pas besoin de l'imprimer
+ */
+
+#ifdef _USE_VECTOR_OSTREAM
+#include <ostream>
+
+template<class T>
+void Vector<T>::affiche(std::ostream& os) const
+{
+ os << "(";
+
+ for (size_t i(0); i < (size() - 1); ++i)
+ {
+ os << (*this)[i] << ", ";
+ }
+
+ os << (*this)[size()-1] << ")";
+}
+
+// Flux de sortie permettant d'afficher les coordonnées du vecteur
+template<class T>
+std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
+{
+ v.affiche(os);
+ return os;
+}
+#endif
+
+#endif //__VECTOR_TPP__