hemocell
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1/*
2This file is part of the HemoCell library
3
4HemoCell is developed and maintained by the Computational Science Lab
5in the University of Amsterdam. Any questions or remarks regarding this library
6can be sent to: info@hemocell.eu
7
8When using the HemoCell library in scientific work please cite the
9corresponding paper: https://doi.org/10.3389/fphys.2017.00563
10
11The HemoCell library is free software: you can redistribute it and/or
12modify it under the terms of the GNU Affero General Public License as
13published by the Free Software Foundation, either version 3 of the
14License, or (at your option) any later version.
15
16The library is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU Affero General Public License for more details.
20
21You should have received a copy of the GNU Affero General Public License
22along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24#ifndef HEMO_ARRAY_H
25#define HEMO_ARRAY_H
26
27#include <cstddef>
28#include <array>
29
30#include "constant_defaults.h"
31
32#include "core/array.h"
33#include "core/geometry3D.h"
34
35namespace hemo {
36
37
38 template<typename _Tp, std::size_t _Nm>
39 struct Array : public std::array<_Tp,_Nm> {
40 Array() {}
41 Array(const std::initializer_list<_Tp> & il){
42 std::copy(il.begin(),il.end(), this->_M_elems);
43 }
44 Array(const plb::Array<_Tp,_Nm> & ext) {
45 for (std::size_t i = 0 ; i < _Nm ; i++) {
46 (*this)[i] = ext[i];
47 }
48 }
49
50 inline Array<_Tp, _Nm> &
52 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
53 (*this)[i] += __two[i];
54 }
55 return *this;
56 }
57
58 inline Array<_Tp, _Nm> &
60 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
61 (*this)[i] -= __two[i];
62 }
63 return *this;
64 }
65
66 inline Array<_Tp, _Nm> &
67 operator+=(const plb::Array<_Tp, _Nm>& __two) {
68 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
69 (*this)[i] += __two[i];
70 }
71 return *this;
72 }
73
74 inline Array<_Tp, 3> &
75 operator+=(const plb::Dot3D & __two) {
76 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
77 (*this)[0] += __two.x;
78 (*this)[1] += __two.y;
79 (*this)[2] += __two.z;
80
81 }
82 return *this;
83 }
84 inline Array<_Tp, _Nm> &
85 operator*=(const _Tp & mul) {
86 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
87 (*this)[i] *= mul;
88 }
89 return *this;
90 }
91
92 inline Array<_Tp, _Nm> &
93 operator/=(const _Tp & div) {
94 for(std::size_t i = 0 ; i < _Nm ; i++ ) {
95 (*this)[i] /= div;
96 }
97 return *this;
98 }
99
100
101 inline Array<_Tp, _Nm> &
102 operator=(const std::initializer_list<_Tp> & c) {
103 std::copy(c.begin(),c.end(), this->_M_elems);
104 return *this;
105 }
106 inline void resetToZero() {
107 this->fill(0);
108 }
109
110 };
111
112 template<typename _Tp, std::size_t _Nm>
113 inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const Array<_Tp, _Nm> & two) {
114 Array<_Tp, _Nm> ret;
115 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
116 ret[i] = one[i]+two[i];
117 }
118 return ret;
119 }
120
121 template<typename _Tp, std::size_t _Nm>
122 inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const plb::Array<_Tp, _Nm> & two) {
123 Array<_Tp, _Nm> ret;
124 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
125 ret[i] = one[i]+two[i];
126 }
127 return ret;
128 }
129 template<typename _Tp, std::size_t _Nm>
130 inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const _Tp & two) {
131 Array<_Tp, _Nm> ret;
132 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
133 ret[i] = one[i]+two;
134 }
135 return ret;
136 }
137 template<typename _Tp, std::size_t _Nm>
138 inline Array<_Tp, _Nm> operator-(const Array<_Tp, _Nm> & one, const Array<_Tp, _Nm> & two) {
139 Array<_Tp, _Nm> ret;
140 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
141 ret[i] = one[i]-two[i];
142 }
143 return ret;
144 }
145 template<typename _Tp, typename _Tp2, std::size_t _Nm>
148 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
149 ret[i] = one[i]-two[i];
150 }
151 return ret;
152 }
153 template<typename _Tp, std::size_t _Nm>
155 Array<_Tp, _Nm> ret;
156 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
157 ret[i] = -one[i];
158 }
159 return ret;
160 }
161
162 template<typename _Tp, std::size_t _Nm, typename _Tp2>
163 inline Array<_Tp, _Nm> operator/(const Array<_Tp, _Nm> & one, const _Tp2 div) {
164 Array<_Tp, _Nm> ret;
165 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
166 ret[i] = one[i]/div;
167 }
168 return ret;
169 }
170
171 template<typename _Tp, std::size_t _Nm, typename _Tp2>
172 inline Array<_Tp, _Nm> operator*(const Array<_Tp, _Nm> & one, const _Tp2 mul) {
173 Array<_Tp, _Nm> ret;
174 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
175 ret[i] = one[i]*mul;
176 }
177 return ret;
178 }
179
180 template<typename _Tp, std::size_t _Nm, typename _Tp2>
181 inline Array<_Tp, _Nm> operator*(const _Tp2 mul, const Array<_Tp, _Nm> & one) {
182 Array<_Tp, _Nm> ret;
183 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
184 ret[i] = one[i]*mul;
185 }
186 return ret;
187 }
188
189 template<typename _Tp>
190 inline Array<_Tp, 3> operator-(const Array<_Tp,3> & one, plb::Dot3D two) {
191 Array<_Tp, 3> ret;
192 ret[0] = one[0]-two.x;
193 ret[1] = one[1]-two.y;
194 ret[2] = one[2]-two.z;
195 return ret;
196 }
197
198 template<typename _Tp,typename _Tp2, typename _Tp3>
199 inline void crossProduct (const Array<_Tp, 3>& one, const Array<_Tp2,3> & two, Array<_Tp3,3> & result) {
200 result[0] = one[1]*two[2] - one[2]*two[1];
201 result[1] = one[2]*two[0] - one[0]*two[2];
202 result[2] = one[0]*two[1] - one[1]*two[0];
203 };
204
205 template<typename _Tp>
206 inline Array<_Tp,3> crossProduct (const Array<_Tp, 3>& one, const Array<_Tp,3> & two) {
207 Array<_Tp,3> result;
208 crossProduct(one, two, result);
209 return result;
210 };
211
212 template<typename _Tp, typename _Tp2>
213 inline Array<T,3> crossProduct (const Array<_Tp, 3>& one, const Array<_Tp2,3> & two) {
214 Array<T,3> result;
215 crossProduct(one, two, result);
216 return result;
217 };
218
219 template<typename _Tp, std::size_t _Nm>
220 inline _Tp dot(const Array<_Tp, _Nm> & one, const Array<_Tp, _Nm> & two) {
221 _Tp ret = 0;
222 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
223 ret += one[i]*two[i];
224 }
225 return ret;
226 };
227
228 template<typename _Tp, typename _Tp2, std::size_t _Nm>
229 inline T dot(const Array<_Tp, _Nm> & one, const Array<_Tp2, _Nm> & two) {
230 T ret = 0;
231 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
232 ret += one[i]*two[i];
233 }
234 return ret;
235 };
236
237 template<typename _Tp, std::size_t _Nm>
238 inline _Tp norm(const Array<_Tp, _Nm> & one) {
239 _Tp ret = 0;
240 for (std::size_t i = 0 ; i < _Nm ; i++ ) {
241 ret += (one[i])*(one[i]);
242 }
243 return std::sqrt(ret);
244 };
245
246 template<typename _Tp>
247 _Tp angleBetweenVectors(const Array<_Tp,3> & one, const Array<_Tp,3> & two)
248 {
249 Array<_Tp,3> cross;
250 crossProduct(one, two, cross);
251 return std::atan2(norm(cross), dot(one,two));
252 }
253
254 template<typename _Tp>
255 _Tp computeTriangleArea(const Array<_Tp,3> & v0, const Array<_Tp,3> & v1, const Array<_Tp,3> & v2)
256 {
257 Array<_Tp,3> e1 = v1 - v0;
258 Array<_Tp,3> e2 = v2 - v0;
259 Array<_Tp,3> cross;
260 crossProduct(e1, e2, cross);
261
262 return (_Tp) 0.5 * norm(cross);
263 }
264
265 template<typename _Tp>
267 return sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
268 }
269
270 template<typename _Tp>
271 void computeTriangleAreaAndUnitNormal(const Array<_Tp,3> & v0, const Array<_Tp,3> & v1, const Array<_Tp,3> & v2, _Tp & area, Array<_Tp,3>& unitNormal)
272 {
273 Array<_Tp,3> e01 = v1 - v0;
274 Array<_Tp,3> e02 = v2 - v0;
275
276 crossProduct(e01, e02, unitNormal);
277 _Tp normN = norm(unitNormal);
278 if (normN != 0.0) {
279 area = (_Tp) 0.5 * normN;
280 unitNormal /= normN;
281 } else {
282 area = (_Tp) 0;
283 unitNormal.resetToZero();
284 }
285 }
286
287 template<typename _Tp>
288 Array<_Tp,3> computeTriangleNormal(const Array<_Tp,3> & v0, const Array<_Tp,3> & v1, const Array<_Tp,3> & v2, bool isAreaWeighted)
289 {
290 Array<_Tp,3> e01 = v1 - v0;
291 Array<_Tp,3> e02 = v2 - v0;
292
293 Array<_Tp,3> n;
294 crossProduct(e01, e02, n);
295 if (!isAreaWeighted) {
296 _Tp normN = norm(n);
297 if (normN != 0) {
298 n /= normN;
299 } else {
300 n.resetToZero();
301 }
302 }
303 return n;
304 }
305
306 template<typename _Tp>
308 const _Tp adj_norm = dot(a,b)/norm(b);
309 //Pythagoras to get opp side length
310 const _Tp hyp_norm = norm(a);
311 const _Tp opp_norm = std::sqrt(std::pow(hyp_norm,2)-std::pow(adj_norm,2));
312 return adj_norm/opp_norm;
313 }
314
315 template<typename _Tp>
316 void computeLengthsPythagoras (const Array<_Tp,3> & a, const Array<_Tp,3> & b, _Tp & a_l, _Tp & b_l, _Tp & c_l) {
317 a_l = dot(a,b)/norm(b);
318 //Pythagoras to get opp side length
319 c_l = norm(a);
320 b_l = std::sqrt(std::pow(c_l,2)-std::pow(a_l,2));
321 }
322}
323
324#endif
double T
Definition constant_defaults.h:118
Definition config.cpp:34
void crossProduct(const Array< _Tp, 3 > &one, const Array< _Tp2, 3 > &two, Array< _Tp3, 3 > &result)
Definition array.h:199
_Tp angleBetweenVectors(const Array< _Tp, 3 > &one, const Array< _Tp, 3 > &two)
Definition array.h:247
Array< _Tp, _Nm > operator/(const Array< _Tp, _Nm > &one, const _Tp2 div)
Definition array.h:163
Array< _Tp, _Nm > operator-(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:138
_Tp computeCotangentFromVectors(const Array< _Tp, 3 > &a, const Array< _Tp, 3 > &b)
Definition array.h:307
void computeTriangleAreaAndUnitNormal(const Array< _Tp, 3 > &v0, const Array< _Tp, 3 > &v1, const Array< _Tp, 3 > &v2, _Tp &area, Array< _Tp, 3 > &unitNormal)
Definition array.h:271
Array< _Tp, _Nm > operator*(const Array< _Tp, _Nm > &one, const _Tp2 mul)
Definition array.h:172
_Tp computeLength(const Array< _Tp, 3 > &v)
Definition array.h:266
_Tp norm(const Array< _Tp, _Nm > &one)
Definition array.h:238
Array< _Tp, 3 > computeTriangleNormal(const Array< _Tp, 3 > &v0, const Array< _Tp, 3 > &v1, const Array< _Tp, 3 > &v2, bool isAreaWeighted)
Definition array.h:288
Array< _Tp, _Nm > operator+(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:113
_Tp dot(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:220
void computeLengthsPythagoras(const Array< _Tp, 3 > &a, const Array< _Tp, 3 > &b, _Tp &a_l, _Tp &b_l, _Tp &c_l)
Definition array.h:316
_Tp computeTriangleArea(const Array< _Tp, 3 > &v0, const Array< _Tp, 3 > &v1, const Array< _Tp, 3 > &v2)
Definition array.h:255
Definition array.h:39
Array()
Definition array.h:40
Array< _Tp, _Nm > & operator/=(const _Tp &div)
Definition array.h:93
Array< _Tp, _Nm > & operator*=(const _Tp &mul)
Definition array.h:85
Array(const std::initializer_list< _Tp > &il)
Definition array.h:41
Array< _Tp, _Nm > & operator+=(const plb::Array< _Tp, _Nm > &__two)
Definition array.h:67
Array(const plb::Array< _Tp, _Nm > &ext)
Definition array.h:44
void resetToZero()
Definition array.h:106
Array< _Tp, _Nm > & operator=(const std::initializer_list< _Tp > &c)
Definition array.h:102
Array< _Tp, _Nm > & operator+=(const hemo::Array< _Tp, _Nm > &__two)
Definition array.h:51
Array< _Tp, 3 > & operator+=(const plb::Dot3D &__two)
Definition array.h:75
Array< _Tp, _Nm > & operator-=(const hemo::Array< _Tp, _Nm > &__two)
Definition array.h:59