hemocell
Loading...
Searching...
No Matches
meshGeneratingFunctions.hh
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 MESH_GENERATING_FUNCTIONS_HH
25#define MESH_GENERATING_FUNCTIONS_HH
26
28
29namespace plb {
30
31template <typename T>
32TriangleSet<T> constructSphereIcosahedron(plb::Array<T, 3> const & center, T radius, plint minNumOfTriangles) {
33 std::vector<typename TriangleSet<T>::Triangle> triangles;
34#ifdef PLB_DEBUG
35 static const T eps = std::numeric_limits<T>::epsilon();
36#endif
37 PLB_ASSERT(radius > (T)0.0 && !util::fpequal(radius, (T)0.0, eps) && minNumOfTriangles >= 8);
38
39 // Create a triangularized unit sphere
40 // Twelve vertices of icosahedron on unit sphere
41 T tau = -0.8506508084; // t=(1+sqrt(5))/2, tau=t/sqrt(1+t^2)
42 T one = -0.5257311121; // one=1/sqrt(1+t^2) , unit sphere
43
44 // Initial 6 vertices
45 plb::Array<T, 3> v1(tau, one, 0); //ZA
46 plb::Array<T, 3> v2(-tau, one, 0); //ZB
47 plb::Array<T, 3> v3(-tau, -one, 0); //ZC
48 plb::Array<T, 3> v4(tau, -one, 0); //ZD
49 plb::Array<T, 3> v5(one, 0, tau); //YA
50 plb::Array<T, 3> v6(one, 0, -tau); //YB
51 plb::Array<T, 3> v7(-one, 0, -tau); //YC
52 plb::Array<T, 3> v8(-one, 0, tau); //YD
53 plb::Array<T, 3> v9(0, tau, one); //XA
54 plb::Array<T, 3> v10(0, -tau, one); //XB
55 plb::Array<T, 3> v11(0, -tau, -one); //XC
56 plb::Array<T, 3> v12(0, tau, -one); //XD
57
58 // Structure for unit icosahedron
59 typename TriangleSet<T>::Triangle tmp;
60
61 tmp[0] = v5;
62 tmp[1] = v8;
63 tmp[2] = v9;
64 triangles.push_back(tmp);
65 tmp[0] = v5;
66 tmp[1] = v10;
67 tmp[2] = v8;
68 triangles.push_back(tmp);
69 tmp[0] = v6;
70 tmp[1] = v12;
71 tmp[2] = v7;
72 triangles.push_back(tmp);
73 tmp[0] = v6;
74 tmp[1] = v7;
75 tmp[2] = v11;
76 triangles.push_back(tmp);
77 tmp[0] = v1;
78 tmp[1] = v4;
79 tmp[2] = v5;
80 triangles.push_back(tmp);
81 tmp[0] = v1;
82 tmp[1] = v6;
83 tmp[2] = v4;
84 triangles.push_back(tmp);
85 tmp[0] = v3;
86 tmp[1] = v2;
87 tmp[2] = v8;
88 triangles.push_back(tmp);
89 tmp[0] = v3;
90 tmp[1] = v7;
91 tmp[2] = v2;
92 triangles.push_back(tmp);
93 tmp[0] = v9;
94 tmp[1] = v12;
95 tmp[2] = v1;
96 triangles.push_back(tmp);
97 tmp[0] = v9;
98 tmp[1] = v2;
99 tmp[2] = v12;
100 triangles.push_back(tmp);
101 tmp[0] = v10;
102 tmp[1] = v4;
103 tmp[2] = v11;
104 triangles.push_back(tmp);
105 tmp[0] = v10;
106 tmp[1] = v11;
107 tmp[2] = v3;
108 triangles.push_back(tmp);
109 tmp[0] = v9;
110 tmp[1] = v1;
111 tmp[2] = v5;
112 triangles.push_back(tmp);
113 tmp[0] = v12;
114 tmp[1] = v6;
115 tmp[2] = v1;
116 triangles.push_back(tmp);
117 tmp[0] = v5;
118 tmp[1] = v4;
119 tmp[2] = v10;
120 triangles.push_back(tmp);
121 tmp[0] = v6;
122 tmp[1] = v11;
123 tmp[2] = v4;
124 triangles.push_back(tmp);
125 tmp[0] = v8;
126 tmp[1] = v2;
127 tmp[2] = v9;
128 triangles.push_back(tmp);
129 tmp[0] = v7;
130 tmp[1] = v12;
131 tmp[2] = v2;
132 triangles.push_back(tmp);
133 tmp[0] = v8;
134 tmp[1] = v10;
135 tmp[2] = v3;
136 triangles.push_back(tmp);
137 tmp[0] = v7;
138 tmp[1] = v3;
139 tmp[2] = v11;
140 triangles.push_back(tmp);
141
142 // Perform refinement iterations
143
144 plint size;
145 plb::Array<T, 3> va, vb, vc, vd, ve, vf;
146 while ((size = triangles.size()) < minNumOfTriangles) {
147 for (plint i = 0; i < size; i++) {
148 va = triangles[i][0];
149 vb = triangles[i][1];
150 vc = triangles[i][2];
151
152 vd = (T)0.5 * (va + vb);
153 ve = (T)0.5 * (vb + vc);
154 vf = (T)0.5 * (vc + va);
155
156 vd /= norm(vd);
157 ve /= norm(ve);
158 vf /= norm(vf);
159
160 triangles[i][0] = vd;
161 triangles[i][1] = ve;
162 triangles[i][2] = vf;
163
164 tmp[0] = va;
165 tmp[1] = vd;
166 tmp[2] = vf;
167 triangles.push_back(tmp);
168
169 tmp[0] = vd;
170 tmp[1] = vb;
171 tmp[2] = ve;
172 triangles.push_back(tmp);
173
174 tmp[0] = vf;
175 tmp[1] = ve;
176 tmp[2] = vc;
177 triangles.push_back(tmp);
178 }
179 }
180
181 // Scale and translate the mesh
182
183 TriangleSet<T> triangleSet(triangles);
184
185 triangleSet.scale(radius);
186 triangleSet.translate(center);
187
188 return triangleSet;
189}
190
191template <typename T>
192plb::Array<T, 3> spherePointToRBCPoint(const plb::Array<T, 3> point, T R) {
193 plb::Array<T, 3> rbcPoint(point);
194 T r2 = rbcPoint[0] * rbcPoint[0] + rbcPoint[1] * rbcPoint[1];
195 T val = rbcPoint[2];
196 plint sign = (T(0) < val) - (val < T(0));
197 rbcPoint[0] *= R;
198 rbcPoint[1] *= R;
199 if (1 - r2 < 0) { r2 = 1; }
200 //T C0 = 0.0135805, C2 = 1.001279, C4 = -0.561381;
201 T C0 = 0.054322, C2 = 1.001279, C4 = -0.561381;
202 rbcPoint[2] = sign * R * sqrt(1 - r2) * (C0 + C2 * r2 + C4 * r2 * r2);
203 return rbcPoint;
204}
205
206template <typename T>
207plb::Array<T, 3> spherePointToEllipsoidPoint(const plb::Array<T, 3> point, T R, T aspectRatio) {
208 plb::Array<T, 3> elPoint(point);
209 T r2 = elPoint[0] * elPoint[0] + elPoint[1] * elPoint[1];
210 T val = elPoint[2];
211 plint sign = (T(0) < val) - (val < T(0));
212 if (1 - r2 < 0) { r2 = 1; }
213 elPoint[0] *= R;
214 elPoint[1] *= R;
215 elPoint[2] = sign * aspectRatio * R * sqrt(1 - r2);
216 return elPoint;
217}
218
219template <typename T>
220plb::Array<T, 3> mapMeshAsRBC(const plb::Array<T, 3> point, const plb::Array<T, 3> center, T R) {
221 plb::Array<T, 3> rbcPoint(point - center);
222 rbcPoint[0] = rbcPoint[0] > R ? R : rbcPoint[0];
223 rbcPoint[1] = rbcPoint[1] > R ? R : rbcPoint[1];
224 T r2 = rbcPoint[0] * rbcPoint[0] + rbcPoint[1] * rbcPoint[1];
225 //T C0 = 0.02716, C2 = 2.0024, C4 = -1.123;
226 T C0 = 0.02716, C2 = 2.0024, C4 = -1.123;
227 T val = rbcPoint[2];
228 plint sign = (T(0) < val) - (val < T(0));
229 // rbcPoint[0] *= R;
230 // rbcPoint[1] *= R;
231 r2 = r2 / (R * R);
232 if (1 - r2 < 0) { r2 = 1; }
233 rbcPoint[2] = sign * 0.5 * R * sqrt(1 - r2) * (C0 + C2 * r2 + C4 * r2 * r2);
234 rbcPoint = (rbcPoint + center);
235 return rbcPoint;
236}
237
238
239template <typename T>
240TriangleSet<T> constructRBC(plb::Array<T, 3> const & center, T radius, plint minNumOfTriangles,
241 plb::Array<T, 3> const & eulerAngles) {
242 return constructCell(center, radius, "./lib/RBC.stl", eulerAngles);
243}
244
245
246template <typename T>
247TriangleSet<T> constructRBCFromSphere(plb::Array<T, 3> const & center, T radius, plint minNumOfTriangles,
248 plb::Array<T, 3> const & eulerAngles, pluint initialSphereShape) {
249 TriangleSet<T> sphere;
250 if (initialSphereShape == 1) {
251 sphere = constructSphereIcosahedron<T>(plb::Array<T, 3>(0, 0, 0), 1.0, minNumOfTriangles);
252 } else if (initialSphereShape == 0) {
253 sphere = constructSphere<T>(plb::Array<T, 3>(0, 0, 0), 1.0, minNumOfTriangles);
254 }
255 sphere.rotate(PI / 2.0 + eulerAngles[0], PI / 2.0 + eulerAngles[1], 0. + eulerAngles[2]);
256 std::vector<typename TriangleSet<T>::Triangle> rbcTriangles = sphere.getTriangles();
257 for (pluint var = 0; var < rbcTriangles.size(); ++var) {
258 rbcTriangles[var][0] = spherePointToRBCPoint(rbcTriangles[var][0]);
259 rbcTriangles[var][1] = spherePointToRBCPoint(rbcTriangles[var][1]);
260 rbcTriangles[var][2] = spherePointToRBCPoint(rbcTriangles[var][2]);
261 }
262 TriangleSet<T> rbc(rbcTriangles);
263 rbc.scale(radius);
264 rbc.rotate(PI / 2.0 + eulerAngles[0], PI / 2.0 + eulerAngles[1], 0. + eulerAngles[2]);
265 rbc.translate(center);
266 return rbc;
267}
268
269
270template <typename T>
271TriangleSet<T> constructEllipsoidFromSphere(plb::Array<T, 3> const & center, T radius, T aspectRatio,
272 plint minNumOfTriangles, plb::Array<T, 3> const & eulerAngles,
273 pluint initialSphereShape) {
274 TriangleSet<T> sphere;
275 if (initialSphereShape == 1) {
276 sphere = constructSphereIcosahedron<T>(plb::Array<T, 3>(0, 0, 0), 1.0, minNumOfTriangles);
277 } else if (initialSphereShape == 0) {
278 sphere = constructSphere<T>(plb::Array<T, 3>(0, 0, 0), 1.0, minNumOfTriangles);
279 }
280 sphere.rotate(PI / 2.0 + eulerAngles[0], PI / 2.0 + eulerAngles[1], 0. + eulerAngles[2]);
281 std::vector<typename TriangleSet<T>::Triangle> ellipsoidTriangles = sphere.getTriangles();
282 for (pluint var = 0; var < ellipsoidTriangles.size(); ++var) {
283 ellipsoidTriangles[var][0] = spherePointToEllipsoidPoint(ellipsoidTriangles[var][0], radius, aspectRatio);
284 ellipsoidTriangles[var][1] = spherePointToEllipsoidPoint(ellipsoidTriangles[var][1], radius, aspectRatio);
285 ellipsoidTriangles[var][2] = spherePointToEllipsoidPoint(ellipsoidTriangles[var][2], radius, aspectRatio);
286 }
287 TriangleSet<T> ellipsoid(ellipsoidTriangles);
288 ellipsoid.rotate(PI / 2.0 + eulerAngles[0], PI / 2.0 + eulerAngles[1], 0. + eulerAngles[2]);
289 ellipsoid.translate(center);
290 return ellipsoid;
291}
292
293
294template <typename T>
295TriangleSet<T> constructCell(plb::Array<T, 3> const & center, T radius, std::string cellFilename,
296 plb::Array<T, 3> const & eulerAngles) {
297 // Cuboid<T> boundingCuboid;
298 TriangleSet<T> Cell(cellFilename);
299 Cuboid<T> cb = Cell.getBoundingCuboid();
300 plb::Array<T, 3> dr = (cb.upperRightCorner - cb.lowerLeftCorner);
301 T scaleFactor = std::max(dr[0], std::max(dr[1], dr[2]));
302 Cell.scale(radius * 2.0 / scaleFactor);
303 Cell.rotate(PI / 2.0 + eulerAngles[0], PI / 2.0 + eulerAngles[1], 0. + eulerAngles[2]);
304 Cell.translate(center);
305 return Cell;
306}
307
308} // namespace plb
309
310#endif // MESH_GENERATING_FUNCTIONS_HH
#define PI
Definition constant_defaults.h:144
double T
Definition constant_defaults.h:137
long int plint
Definition constant_defaults.h:149
long unsigned int pluint
Definition constant_defaults.h:152
Definition meshGeneratingFunctions.cpp:28
plb::Array< T, 3 > spherePointToEllipsoidPoint(const plb::Array< T, 3 > point, T R, T aspectRatio)
Definition meshGeneratingFunctions.hh:207
plb::Array< T, 3 > mapMeshAsRBC(const plb::Array< T, 3 > point, const plb::Array< T, 3 > center, T R)
Definition meshGeneratingFunctions.hh:220
TriangleSet< T > constructCell(plb::Array< T, 3 > const &center, T radius, std::string cellFilename, plb::Array< T, 3 > const &eulerAngles)
Definition meshGeneratingFunctions.hh:295
plb::Array< T, 3 > spherePointToRBCPoint(const plb::Array< T, 3 > point, T R=1.0)
Definition meshGeneratingFunctions.hh:192
TriangleSet< T > constructRBCFromSphere(plb::Array< T, 3 > const &center, T radius, plint minNumOfTriangles, plb::Array< T, 3 > const &eulerAngles, pluint initialSphereShape=0)
Definition meshGeneratingFunctions.hh:247
TriangleSet< T > constructRBC(plb::Array< T, 3 > const &center, T radius, plint minNumOfTriangles, plb::Array< T, 3 > const &eulerAngles)
Definition meshGeneratingFunctions.hh:240
TriangleSet< T > constructEllipsoidFromSphere(plb::Array< T, 3 > const &center, T radius, T aspectRatio, plint minNumOfTriangles, plb::Array< T, 3 > const &eulerAngles, pluint initialSphereShape)
Definition meshGeneratingFunctions.hh:271
TriangleSet< T > constructSphereIcosahedron(plb::Array< T, 3 > const &center, T radius, plint minNumOfTriangles)
Definition meshGeneratingFunctions.hh:32