hemocell
Loading...
Searching...
No Matches
meshMetrics.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_METRICS_HH
25#define MESH_METRICS_HH
26
27#include "meshMetrics.h"
28
29namespace plb {
30
31/*
32 * Helper function, calculates the angle between -pi and pi.
33 * The edge is iVertex-jVertex.
34 */
35template<typename T>
36T calculateSignedAngle(TriangularSurfaceMesh<T> const& mesh, plint iVertex, plint jVertex, plint & kVertex, plint & lVertex) {
37 hemo::Array<T,3> x1 = mesh.getVertex(iVertex), x2({0.,0.,0.}), x3({0.,0.,0.}), x4({0.,0.,0.});
38
39 std::vector<plint> adjacentTriangles = mesh.getAdjacentTriangleIds(iVertex, jVertex);
40 plint iTriangle=adjacentTriangles[0], jTriangle=adjacentTriangles[1];
41 x3 = mesh.getVertex(jVertex);
42 T foundVertices=0;
43 for (pluint id = 0; id < 3; ++id) {
44 kVertex = mesh.getVertexId(iTriangle,id);
45 if ( (kVertex != iVertex) && (kVertex != jVertex) ) {
46 x2 = mesh.getVertex(kVertex);
47 foundVertices += 1;
48 break;
49 }
50 }
51 for (pluint id = 0; id < 3; ++id) {
52 lVertex = mesh.getVertexId(jTriangle,id);
53 if ( (lVertex != iVertex) && (lVertex != jVertex) ) {
54 x4 = mesh.getVertex(lVertex);
55 foundVertices += 1;
56 break;
57 }
58 }
59 PLB_ASSERT(foundVertices == 2); //Assert if some particles are outside of the domain
60
61 hemo::Array<T,3> V1 = mesh.computeTriangleNormal(iTriangle);
62 hemo::Array<T,3> V2 = mesh.computeTriangleNormal(jTriangle);
63 T angle = angleBetweenVectors(V1, V2);
64 plint sign = dot(x2-x1, V2) >= 0?1:-1;
65 if (sign <= 0) {
66 angle = 2*PI-angle;
67 }
68 angle = (angle > PI)?angle-2*PI:angle;
69 return angle;
70}
71
72
73/*
74 * * Helper function, calculates the angle between -pi and pi
75 * */
76template<typename T>
77T calculateSignedAngle(TriangularSurfaceMesh<T> const& mesh, plint iVertex, plint jVertex) {
78 plint kVertex, lVertex;
79 return calculateSignedAngle(mesh, iVertex, jVertex, kVertex, lVertex);
80}
81
82template<typename T>
83void writeSurfaceMeshAsciiSTL(TriangularSurfaceMesh<T> const& mesh, std::string fname)
84{
85 T dx = 1;
86 // Output only from one MPI process.
87
88 FILE *fp = fopen(fname.c_str(), "w");
89 PLB_ASSERT(fp != NULL);
90
91 char fmt1[64] = " facet normal ";
92 char fmt2[64] = " vertex ";
93 if (sizeof(T) == sizeof(long double)) {
94 strcat(fmt1, "% Le % Le % Le\n");
95 strcat(fmt2, "% Le % Le % Le\n");
96 }
97 else if (sizeof(T) == sizeof(float) ||
98 sizeof(T) == sizeof(double)) {
99 strcat(fmt1, "% e % e % e\n");
100 strcat(fmt2, "% e % e % e\n");
101 }
102 else {
103 PLB_ASSERT(false);
104 }
105
106 fprintf(fp, "solid surface\n");
107 for (plint i = 0; i < mesh.getNumTriangles(); i++) {
108 hemo::Array<T,3> n = mesh.computeTriangleNormal(i);
110 fprintf(fp, fmt1, n[0], n[1], n[2]);
111 fprintf(fp, " outer loop\n");
112 v = dx * mesh.getVertex(i, 0);
113 fprintf(fp, fmt2, v[0], v[1], v[2]);
114 v = dx * mesh.getVertex(i, 1);
115 fprintf(fp, fmt2, v[0], v[1], v[2]);
116 v = dx * mesh.getVertex(i, 2);
117 fprintf(fp, fmt2, v[0], v[1], v[2]);
118 fprintf(fp, " endloop\n");
119 fprintf(fp, " endfacet\n");
120 }
121 fprintf(fp, "endsolid surface\n");
122
123 fclose(fp);
124}
125
126}
127#endif // MESH_METRICS_HH
#define PI
Definition constant_defaults.h:122
double T
Definition constant_defaults.h:118
long int plint
Definition constant_defaults.h:127
long unsigned int pluint
Definition constant_defaults.h:130
Definition meshGeneratingFunctions.cpp:28
void writeSurfaceMeshAsciiSTL(TriangularSurfaceMesh< T > const &mesh, std::string fname)
Definition meshMetrics.hh:83
T calculateSignedAngle(TriangularSurfaceMesh< T > const &mesh, plint iVertex, plint jVertex, plint &kVertex, plint &lVertex)
Definition meshMetrics.hh:36
Definition array.h:39