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#include <algorithm>
30
31#include "constant_defaults.h"
32
33#include "core/array.h"
34#include "core/geometry3D.h"
35
36namespace hemo {
37
38
39template <typename _Tp, std::size_t _Nm>
40struct Array : public std::array<_Tp, _Nm> {
42 Array() {}
45 Array(const std::initializer_list<_Tp> & il) {
46 std::copy_n(il.begin(), il.size() < _Nm ? il.size() : _Nm, this->begin());
47 }
48 Array(const plb::Array<_Tp, _Nm> & ext) {
49 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] = ext[i]; }
50 }
51
53 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] += __two[i]; }
54 return *this;
55 }
56
58 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] -= __two[i]; }
59 return *this;
60 }
61
62 inline Array<_Tp, _Nm> & operator+=(const plb::Array<_Tp, _Nm> & __two) {
63 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] += __two[i]; }
64 return *this;
65 }
66
67 inline Array<_Tp, _Nm> & operator+=(const plb::Dot3D & __two) {
68 static_assert(_Nm == 3, "Adding a plb::Dot3D requires a 3-element array");
69 (*this)[0] += __two.x;
70 (*this)[1] += __two.y;
71 (*this)[2] += __two.z;
72 return *this;
73 }
74 inline Array<_Tp, _Nm> & operator*=(const _Tp & mul) {
75 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] *= mul; }
76 return *this;
77 }
78
79 inline Array<_Tp, _Nm> & operator/=(const _Tp & div) {
80 for (std::size_t i = 0; i < _Nm; i++) { (*this)[i] /= div; }
81 return *this;
82 }
83
84
85 inline Array<_Tp, _Nm> & operator=(const std::initializer_list<_Tp> & c) {
86 // Assigns min(c.size(), _Nm) elements; the others keep their value.
87 std::copy_n(c.begin(), c.size() < _Nm ? c.size() : _Nm, this->begin());
88 return *this;
89 }
90 inline void resetToZero() { this->fill(0); }
91};
92
93template <typename _Tp, std::size_t _Nm>
94inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const Array<_Tp, _Nm> & two) {
96 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] + two[i]; }
97 return ret;
98}
99
100template <typename _Tp, std::size_t _Nm>
101inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const plb::Array<_Tp, _Nm> & two) {
102 Array<_Tp, _Nm> ret;
103 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] + two[i]; }
104 return ret;
105}
106template <typename _Tp, std::size_t _Nm>
107inline Array<_Tp, _Nm> operator+(const Array<_Tp, _Nm> & one, const _Tp & two) {
108 Array<_Tp, _Nm> ret;
109 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] + two; }
110 return ret;
111}
112template <typename _Tp, std::size_t _Nm>
114 Array<_Tp, _Nm> ret;
115 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] - two[i]; }
116 return ret;
117}
118template <typename _Tp, typename _Tp2, std::size_t _Nm>
121 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] - two[i]; }
122 return ret;
123}
124template <typename _Tp, std::size_t _Nm>
126 Array<_Tp, _Nm> ret;
127 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = -one[i]; }
128 return ret;
129}
130
131template <typename _Tp, std::size_t _Nm, typename _Tp2>
132inline Array<_Tp, _Nm> operator/(const Array<_Tp, _Nm> & one, const _Tp2 div) {
133 Array<_Tp, _Nm> ret;
134 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] / div; }
135 return ret;
136}
137
138template <typename _Tp, std::size_t _Nm, typename _Tp2>
139inline Array<_Tp, _Nm> operator*(const Array<_Tp, _Nm> & one, const _Tp2 mul) {
140 Array<_Tp, _Nm> ret;
141 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] * mul; }
142 return ret;
143}
144
145template <typename _Tp, std::size_t _Nm, typename _Tp2>
146inline Array<_Tp, _Nm> operator*(const _Tp2 mul, const Array<_Tp, _Nm> & one) {
147 Array<_Tp, _Nm> ret;
148 for (std::size_t i = 0; i < _Nm; i++) { ret[i] = one[i] * mul; }
149 return ret;
150}
151
152template <typename _Tp>
153inline Array<_Tp, 3> operator-(const Array<_Tp, 3> & one, plb::Dot3D two) {
154 Array<_Tp, 3> ret;
155 ret[0] = one[0] - two.x;
156 ret[1] = one[1] - two.y;
157 ret[2] = one[2] - two.z;
158 return ret;
159}
160
161template <typename _Tp, typename _Tp2, typename _Tp3>
162inline void crossProduct(const Array<_Tp, 3> & one, const Array<_Tp2, 3> & two, Array<_Tp3, 3> & result) {
163 result[0] = one[1] * two[2] - one[2] * two[1];
164 result[1] = one[2] * two[0] - one[0] * two[2];
165 result[2] = one[0] * two[1] - one[1] * two[0];
166};
167
168template <typename _Tp>
169inline Array<_Tp, 3> crossProduct(const Array<_Tp, 3> & one, const Array<_Tp, 3> & two) {
170 Array<_Tp, 3> result;
171 crossProduct(one, two, result);
172 return result;
173};
174
175template <typename _Tp, typename _Tp2>
176inline Array<T, 3> crossProduct(const Array<_Tp, 3> & one, const Array<_Tp2, 3> & two) {
177 Array<T, 3> result;
178 crossProduct(one, two, result);
179 return result;
180};
181
182template <typename _Tp, std::size_t _Nm>
183inline _Tp dot(const Array<_Tp, _Nm> & one, const Array<_Tp, _Nm> & two) {
184 _Tp ret = 0;
185 for (std::size_t i = 0; i < _Nm; i++) { ret += one[i] * two[i]; }
186 return ret;
187};
188
189template <typename _Tp, typename _Tp2, std::size_t _Nm>
190inline T dot(const Array<_Tp, _Nm> & one, const Array<_Tp2, _Nm> & two) {
191 T ret = 0;
192 for (std::size_t i = 0; i < _Nm; i++) { ret += one[i] * two[i]; }
193 return ret;
194};
195
196template <typename _Tp, std::size_t _Nm>
197inline _Tp norm(const Array<_Tp, _Nm> & one) {
198 _Tp ret = 0;
199 for (std::size_t i = 0; i < _Nm; i++) { ret += (one[i]) * (one[i]); }
200 return std::sqrt(ret);
201};
202
203template <typename _Tp>
204_Tp angleBetweenVectors(const Array<_Tp, 3> & one, const Array<_Tp, 3> & two) {
205 Array<_Tp, 3> cross;
206 crossProduct(one, two, cross);
207 return std::atan2(norm(cross), dot(one, two));
208}
209
210template <typename _Tp>
211_Tp computeTriangleArea(const Array<_Tp, 3> & v0, const Array<_Tp, 3> & v1, const Array<_Tp, 3> & v2) {
212 Array<_Tp, 3> e1 = v1 - v0;
213 Array<_Tp, 3> e2 = v2 - v0;
214 Array<_Tp, 3> cross;
215 crossProduct(e1, e2, cross);
216
217 return (_Tp)0.5 * norm(cross);
218}
219
220template <typename _Tp>
222 return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
223}
224
225template <typename _Tp>
227 _Tp & area, Array<_Tp, 3> & unitNormal) {
228 Array<_Tp, 3> e01 = v1 - v0;
229 Array<_Tp, 3> e02 = v2 - v0;
230
231 crossProduct(e01, e02, unitNormal);
232 _Tp normN = norm(unitNormal);
233 if (normN != 0.0) {
234 area = (_Tp)0.5 * normN;
235 unitNormal /= normN;
236 } else {
237 area = (_Tp)0;
238 unitNormal.resetToZero();
239 }
240}
241
242template <typename _Tp>
244 bool isAreaWeighted) {
245 Array<_Tp, 3> e01 = v1 - v0;
246 Array<_Tp, 3> e02 = v2 - v0;
247
249 crossProduct(e01, e02, n);
250 if (!isAreaWeighted) {
251 _Tp normN = norm(n);
252 if (normN != 0) {
253 n /= normN;
254 } else {
255 n.resetToZero();
256 }
257 }
258 return n;
259}
260
261template <typename _Tp>
263 const _Tp adj_norm = dot(a, b) / norm(b);
264 //Pythagoras to get opp side length
265 const _Tp hyp_norm = norm(a);
266 const _Tp opp_norm = std::sqrt(std::pow(hyp_norm, 2) - std::pow(adj_norm, 2));
267 return adj_norm / opp_norm;
268}
269
270template <typename _Tp>
271void computeLengthsPythagoras(const Array<_Tp, 3> & a, const Array<_Tp, 3> & b, _Tp & a_l, _Tp & b_l, _Tp & c_l) {
272 a_l = dot(a, b) / norm(b);
273 //Pythagoras to get opp side length
274 c_l = norm(a);
275 b_l = std::sqrt(std::pow(c_l, 2) - std::pow(a_l, 2));
276}
277}
278
279#endif
double T
Definition constant_defaults.h:137
Definition config.cpp:37
void crossProduct(const Array< _Tp, 3 > &one, const Array< _Tp2, 3 > &two, Array< _Tp3, 3 > &result)
Definition array.h:162
_Tp angleBetweenVectors(const Array< _Tp, 3 > &one, const Array< _Tp, 3 > &two)
Definition array.h:204
Array< _Tp, _Nm > operator/(const Array< _Tp, _Nm > &one, const _Tp2 div)
Definition array.h:132
Array< _Tp, _Nm > operator-(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:113
_Tp computeCotangentFromVectors(const Array< _Tp, 3 > &a, const Array< _Tp, 3 > &b)
Definition array.h:262
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:226
Array< _Tp, _Nm > operator*(const Array< _Tp, _Nm > &one, const _Tp2 mul)
Definition array.h:139
_Tp computeLength(const Array< _Tp, 3 > &v)
Definition array.h:221
_Tp norm(const Array< _Tp, _Nm > &one)
Definition array.h:197
Array< _Tp, 3 > computeTriangleNormal(const Array< _Tp, 3 > &v0, const Array< _Tp, 3 > &v1, const Array< _Tp, 3 > &v2, bool isAreaWeighted)
Definition array.h:243
Array< _Tp, _Nm > operator+(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:94
_Tp dot(const Array< _Tp, _Nm > &one, const Array< _Tp, _Nm > &two)
Definition array.h:183
void computeLengthsPythagoras(const Array< _Tp, 3 > &a, const Array< _Tp, 3 > &b, _Tp &a_l, _Tp &b_l, _Tp &c_l)
Definition array.h:271
_Tp computeTriangleArea(const Array< _Tp, 3 > &v0, const Array< _Tp, 3 > &v1, const Array< _Tp, 3 > &v2)
Definition array.h:211
Definition array.h:40
Array()
Elements are left uninitialised (deliberately, for speed).
Definition array.h:42
Array< _Tp, _Nm > & operator/=(const _Tp &div)
Definition array.h:79
Array< _Tp, _Nm > & operator*=(const _Tp &mul)
Definition array.h:74
Array(const std::initializer_list< _Tp > &il)
Definition array.h:45
Array< _Tp, _Nm > & operator+=(const plb::Dot3D &__two)
Definition array.h:67
Array< _Tp, _Nm > & operator+=(const plb::Array< _Tp, _Nm > &__two)
Definition array.h:62
Array(const plb::Array< _Tp, _Nm > &ext)
Definition array.h:48
void resetToZero()
Definition array.h:90
Array< _Tp, _Nm > & operator=(const std::initializer_list< _Tp > &c)
Definition array.h:85
Array< _Tp, _Nm > & operator+=(const hemo::Array< _Tp, _Nm > &__two)
Definition array.h:52
Array< _Tp, _Nm > & operator-=(const hemo::Array< _Tp, _Nm > &__two)
Definition array.h:57