updated to modern VTK
[engrid-github.git] / src / math / mathvector_methods.h
blob2b5f59ecca4a80ce3414d7264215c5f1fcd02b93
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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 // MathVector<V>(const MathVector<V>&)
23 // -----------------------------------
24 template <class V>
25 inline MathVector<V>::MathVector(const MathVector<V>& vec) : V()
27 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = vec[i];
30 // MathVector<V>(const ParseNode<L,O,R>&)
31 // --------------------------------------
32 template <class V>
33 template <class L, class O, class R>
34 inline MathVector<V>::MathVector(const ParseNode<L,O,R> &expr) : V()
36 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = expr[i];
39 // MathVector<V>(const value_type *v)
40 // ----------------------------------
41 template <class V>
42 inline MathVector<V>::MathVector(const value_type *v) : V()
44 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = v[i];
47 // MathVector<V>(const value_type v1, const value_type v2, const value_type v3, const value_type v4)
48 // ----------------------------------------------------------------------------
49 template <class V>
50 inline MathVector<V>::MathVector(const value_type v1,
51 const value_type v2,
52 const value_type v3,
53 const value_type v4) : V()
55 (*this)[0] = v1;
56 (*this)[1] = v2;
57 (*this)[2] = v3;
58 (*this)[3] = v4;
61 // MathVector<V>(const value_type v1, const value_type v2, const value_type v3)
62 // ----------------------------------------------------------------------------
63 template <class V>
64 inline MathVector<V>::MathVector(const value_type v1,
65 const value_type v2,
66 const value_type v3) : V()
68 (*this)[0] = v1;
69 (*this)[1] = v2;
70 (*this)[2] = v3;
73 // MathVector<V>(const value_type v1, const value_type v2)
74 // ----------------------------------------------------------------------------
75 template <class V>
76 inline MathVector<V>::MathVector(const value_type v1,
77 const value_type v2) : V()
79 (*this)[0] = v1;
80 (*this)[1] = v2;
83 // cross product
84 // -------------
85 template <class V>
86 inline MathVector<V> MathVector<V>::cross(const MathVector<V> &vec) const
88 MathVector<V> new_vec;
89 new_vec[0] = (*this)[1]*vec[2] - (*this)[2]*vec[1];
90 new_vec[1] = (*this)[2]*vec[0] - (*this)[0]*vec[2];
91 new_vec[2] = (*this)[0]*vec[1] - (*this)[1]*vec[0];
92 return new_vec;
95 // absolute value (||v||_2 in this case)
96 // -------------------------------------
97 template <class V>
98 inline typename MathVector<V>::scalar_t MathVector<V>::abs() const
100 scalar_t l = 0;
101 for (uint_t i = 0; i < this->size(); ++i) l += (*this)[i]*(*this)[i];
102 return sqrt(l);
105 // absolute value squared ((||v||_2)^2 in this case)
106 // -------------------------------------------------
107 template <class V>
108 inline typename MathVector<V>::scalar_t MathVector<V>::abs2() const
110 scalar_t l = 0;
111 for (uint_t i = 0; i < this->size(); ++i) l += (*this)[i]*(*this)[i];
112 return l;
115 // absolute value (||v||_2 in this case)
116 // -------------------------------------
117 template <class L, class O, class R>
118 inline typename ParseNode<L,O,R>::value_type ParseNode<L,O,R>::abs() const
120 value_type abs_value = 0;
121 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
122 return sqrt(abs_value);
125 template <class O, class R>
126 inline typename ParseNode<double,O,R>::value_type ParseNode<double,O,R>::abs() const
128 value_type abs_value = 0;
129 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
130 return sqrt(abs_value);
133 // absolute value squared ((||v||_2)^2 in this case)
134 // -------------------------------------------------
135 template <class L, class O, class R>
136 inline typename ParseNode<L,O,R>::value_type ParseNode<L,O,R>::abs2() const
138 value_type abs_value = 0;
139 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
140 return abs_value;
143 template <class O, class R>
144 inline typename ParseNode<double,O,R>::value_type ParseNode<double,O,R>::abs2() const
146 value_type abs_value = 0;
147 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
148 return abs_value;
151 // norm the vector to a length of 1 (according to ||v||_2)
152 // -------------------------------------------------------
153 template <class V>
154 inline MathVector<V> MathVector<V>::normalise()
156 scalar_t l = abs();
157 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] /= l;
158 return(*this);
161 // create a C-style array
162 // ----------------------
163 template <class V>
164 typename MathVector<V>::scalar_t* MathVector<V>::c_array() const
166 scalar_t *t = new scalar_t[this->size()];
167 for (uint_t i = 0; i < this->size(); ++i) t[i] = (*this)[i];
168 return t;