updated to modern VTK
[engrid-github.git] / src / math / mathvector.h
bloba822f19a7d561ceb5cecf6922d8ffe4b466a11c7
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef mathvector_h
22 #define mathvector_h
24 template <class V> struct MathVector;
26 #include <iostream>
27 #include <vector>
28 #include <cmath>
30 using namespace std;
32 typedef unsigned int uint_t;
34 #include "mathvector_structs.h"
37 // wrapper class for arrays to provide mathematical functionality
38 // =======================================================================================
40 template <class V>
41 struct MathVector : public V
44 // types
45 // -----
46 typedef typename V::value_type scalar_t;
47 typedef typename V::value_type value_type;
50 // constructors
51 // ------------
53 MathVector() : V() {};
54 MathVector(const MathVector<V>& vec);
55 MathVector(const value_type *v);
56 MathVector(const value_type v1, const value_type v2, const value_type v3, const value_type v4);
57 MathVector(const value_type v1, const value_type v2, const value_type v3);
58 MathVector(const value_type v1, const value_type v2);
60 template <class L, class O, class R>
61 MathVector(const ParseNode<L,O,R> &expr);
64 // operators
65 // ---------
66 void operator= (const MathVector<V> &vec);
67 //void operator= (MathVector<V> &vec);
68 void operator= (const vector<typename V::value_type> &vec);
69 void operator-= (const MathVector<V> &vec);
70 void operator+= (const MathVector<V> &vec);
71 void operator*= (const scalar_t s);
73 // assignment to an expression
74 template <class L, class O, class R> void operator= (const ParseNode<L,O,R> &expr);
77 // other things
78 // ------------
79 MathVector<V> cross(const MathVector<V> &vec) const;
80 scalar_t abs() const;
81 scalar_t abs2() const;
82 MathVector<V> normalise();
83 scalar_t* c_array() const;
84 uint_t dim() { return this->size(); };
87 // STL
88 // ---
89 class iterator;
90 class const_iterator;
92 class iterator
94 friend class const_iterator;
95 MathVector<V> *vec;
96 uint_t i;
98 public:
99 typedef random_access_iterator_tag iterator_category;
100 typedef typename V::value_type value_type;
101 typedef uint_t difference_type;
102 typedef value_type* pointer;
103 typedef value_type& reference;
105 iterator(MathVector<V> *a_vec = NULL, uint_t an_i = 0) : vec(a_vec), i(an_i) {};
106 bool operator==(const iterator &iter) const { return (iter.vec == vec) && (iter.i == i); };
107 bool operator==(const const_iterator &iter) const;
108 bool operator!=(const const_iterator &iter) const { return !operator==(iter); };
109 iterator operator++() { ++i; return iterator(vec,i); };
110 iterator operator++(int) { uint_t j = i; ++i; return iterator(vec,j); };
111 iterator operator--() { --i; return iterator(vec,i); };
112 iterator operator--(int) { uint_t j = i; --i; return iterator(vec,j); };
113 void operator+=(uint_t n) { i += n; };
114 void operator-=(uint_t n) { i -= n; };
115 scalar_t& operator*() const { return (*vec)[i]; };
116 scalar_t& operator[](uint_t n) const { return (*vec)[i+n]; };
117 bool operator<(const iterator &iter) const { return i < iter.i; };
118 uint_t operator-(const iterator &iter) const { return i - iter.i; };
121 class const_iterator
123 MathVector<V> *vec;
124 uint_t i;
126 public:
127 typedef random_access_iterator_tag iterator_category;
128 typedef typename V::value_type value_type;
129 typedef uint_t difference_type;
130 typedef value_type* pointer;
131 typedef value_type& reference;
133 const_iterator(MathVector<V> *a_vec = NULL, uint_t an_i = 0) : vec(a_vec), i(an_i) {};
134 const_iterator(const iterator &iter) : vec(iter.vec), i(iter.i) {};
135 void operator=(const iterator &iter) { vec = iter.vec; i = iter.i; };
136 bool operator==(const const_iterator &iter) const { return (iter.vec == vec) && (iter.i == i); };
137 bool operator==(const iterator &iter) const;
138 bool operator!=(const iterator &iter) const { return !operator==(iter); };
139 const_iterator operator++() { ++i; return iterator(vec,i); };
140 const_iterator operator++(int) { uint_t j = i; ++i; return iterator(vec,j); };
141 const_iterator operator--() { --i; return iterator(vec,i); };
142 const_iterator operator--(int) { uint_t j = i; --i; return iterator(vec,j); };
143 void operator+=(uint_t n) { i += n; };
144 void operator-=(uint_t n) { i -= n; };
145 scalar_t operator*() const { return (*vec)[i]; };
146 scalar_t operator[](uint_t n) const { return (*vec)[i+n]; };
147 bool operator<(const iterator &iter) const { return i < iter.i; };
148 uint_t operator-(const iterator &iter) const { return i - iter.i; };
151 iterator begin() { return iterator(this); };
152 iterator end() { return iterator(this,this->size()); };
153 const_iterator begin() const { return iterator(this); };
154 const_iterator end() const { return iterator(this,this->size()); };
160 // static array class for small vectors
161 // ====================================
163 template <class T, uint_t DIM>
164 class StaticVector
166 public:
168 typedef T value_type;
170 protected:
172 T VALUE[DIM];
174 public:
175 StaticVector() { };
176 T& operator[](const uint_t &i) { return VALUE[i]; };
177 T operator[](const uint_t &i) const { return VALUE[i]; };
178 uint_t size() const { return DIM; };
179 T* data() { return VALUE; };
183 typedef MathVector<StaticVector<double,2> > vec2_t;
184 typedef MathVector<StaticVector<double,3> > vec3_t;
185 typedef MathVector<StaticVector<double,4> > vec4_t;
186 typedef MathVector<StaticVector<double,5> > vec5_t;
187 typedef MathVector<StaticVector<double,6> > vec6_t;
189 #include "mathvector_operators.h"
190 #include "mathvector_methods.h"
192 #endif