feature angle defaults to 20deg now
[engrid.git] / src / math / mathvector_methods.h
blobf98be4090fd716546f85c670153f546d2b4ac86f
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 enGits GmbH +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
24 // MathVector<V>(const MathVector<V>&)
25 // -----------------------------------
26 template <class V>
27 inline MathVector<V>::MathVector(const MathVector<V>& vec) : V()
29 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = vec[i];
32 // MathVector<V>(const ParseNode<L,O,R>&)
33 // --------------------------------------
34 template <class V>
35 template <class L, class O, class R>
36 inline MathVector<V>::MathVector(const ParseNode<L,O,R> &expr) : V()
38 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = expr[i];
41 // MathVector<V>(const value_type *v)
42 // ----------------------------------
43 template <class V>
44 inline MathVector<V>::MathVector(const value_type *v) : V()
46 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] = v[i];
49 // MathVector<V>(const value_type v1, const value_type v2, const value_type v3, const value_type v4)
50 // ----------------------------------------------------------------------------
51 template <class V>
52 inline MathVector<V>::MathVector(const value_type v1,
53 const value_type v2,
54 const value_type v3,
55 const value_type v4) : V()
57 (*this)[0] = v1;
58 (*this)[1] = v2;
59 (*this)[2] = v3;
60 (*this)[3] = v4;
63 // MathVector<V>(const value_type v1, const value_type v2, const value_type v3)
64 // ----------------------------------------------------------------------------
65 template <class V>
66 inline MathVector<V>::MathVector(const value_type v1,
67 const value_type v2,
68 const value_type v3) : V()
70 (*this)[0] = v1;
71 (*this)[1] = v2;
72 (*this)[2] = v3;
75 // MathVector<V>(const value_type v1, const value_type v2)
76 // ----------------------------------------------------------------------------
77 template <class V>
78 inline MathVector<V>::MathVector(const value_type v1,
79 const value_type v2) : V()
81 (*this)[0] = v1;
82 (*this)[1] = v2;
85 // cross product
86 // -------------
87 template <class V>
88 inline MathVector<V> MathVector<V>::cross(const MathVector<V> &vec) const
90 MathVector<V> new_vec;
91 new_vec[0] = (*this)[1]*vec[2] - (*this)[2]*vec[1];
92 new_vec[1] = (*this)[2]*vec[0] - (*this)[0]*vec[2];
93 new_vec[2] = (*this)[0]*vec[1] - (*this)[1]*vec[0];
94 return new_vec;
97 // absolute value (||v||_2 in this case)
98 // -------------------------------------
99 template <class V>
100 inline typename MathVector<V>::scalar_t MathVector<V>::abs() const
102 scalar_t l = 0;
103 for (uint_t i = 0; i < this->size(); ++i) l += (*this)[i]*(*this)[i];
104 return sqrt(l);
107 // absolute value squared ((||v||_2)^2 in this case)
108 // -------------------------------------------------
109 template <class V>
110 inline typename MathVector<V>::scalar_t MathVector<V>::abs2() const
112 scalar_t l = 0;
113 for (uint_t i = 0; i < this->size(); ++i) l += (*this)[i]*(*this)[i];
114 return l;
117 // absolute value (||v||_2 in this case)
118 // -------------------------------------
119 template <class L, class O, class R>
120 inline typename ParseNode<L,O,R>::value_type ParseNode<L,O,R>::abs() const
122 value_type abs_value = 0;
123 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
124 return sqrt(abs_value);
127 template <class O, class R>
128 inline typename ParseNode<double,O,R>::value_type ParseNode<double,O,R>::abs() const
130 value_type abs_value = 0;
131 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
132 return sqrt(abs_value);
135 // absolute value squared ((||v||_2)^2 in this case)
136 // -------------------------------------------------
137 template <class L, class O, class R>
138 inline typename ParseNode<L,O,R>::value_type ParseNode<L,O,R>::abs2() const
140 value_type abs_value = 0;
141 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
142 return abs_value;
145 template <class O, class R>
146 inline typename ParseNode<double,O,R>::value_type ParseNode<double,O,R>::abs2() const
148 value_type abs_value = 0;
149 for (uint_t i = 0; i < this->size(); ++i) abs_value += (*this)[i]*(*this)[i];
150 return abs_value;
153 // norm the vector to a length of 1 (according to ||v||_2)
154 // -------------------------------------------------------
155 template <class V>
156 inline MathVector<V> MathVector<V>::normalise()
158 scalar_t l = abs();
159 for (uint_t i = 0; i < this->size(); ++i) (*this)[i] /= l;
160 return(*this);
163 // create a C-style array
164 // ----------------------
165 template <class V>
166 typename MathVector<V>::scalar_t* MathVector<V>::c_array() const
168 scalar_t *t = new scalar_t[this->size()];
169 for (uint_t i = 0; i < this->size(); ++i) t[i] = (*this)[i];
170 return t;