| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* GAMER = < Geometry-preserving Adaptive MeshER > |
| 4 |
* Copyright (C) 1994-- Michael Holst and Zeyun Yu |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* *************************************************************************** |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
/* *************************************************************************** |
| 24 |
* File: GenerateSphereMesh.C < ... > |
| 25 |
* |
| 26 |
* Author: Zeyun Yu ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: Generate sphere mesh used as a bounding mesh for molecules |
| 29 |
* |
| 30 |
* Notes: Starting from an octahedron (6 vertices, 8 faces, and 12 edges) |
| 31 |
* Consider level n (original level = 1): |
| 32 |
* We can see that f_n = 8 * 4^(n-1), |
| 33 |
* By recursive scheme: v_n = 2v_{n-1} + 8*4^{n-2} - 2, we can get: |
| 34 |
* Number of vertices = 12*2^{n-2}+8*2^{2n-3}-8*2^{n-2}-2^n+2. |
| 35 |
* |
| 36 |
* The following are the calculated v_n and f_n for n = 1, ... 9: |
| 37 |
* level 1: v_n = 6, f_n = 8 |
| 38 |
* level 2: v_n = 18, f_n = 32 |
| 39 |
* level 3: v_n = 66, f_n = 128 |
| 40 |
* level 4: v_n = 258, f_n = 512 |
| 41 |
* level 5: v_n = 1026, f_n = 2048 |
| 42 |
* level 6: v_n = 4098, f_n = 8192 |
| 43 |
* level 7: v_n = 16386, f_n = 32768 |
| 44 |
* level 8: v_n = 65538, f_n = 131072 |
| 45 |
* level 9: v_n = 262146, f_n = 524288 |
| 46 |
* In most cases, n=4 should be a good choice. try not to use over 7 |
| 47 |
* **************************************************************************** |
| 48 |
*/ |
| 49 |
|
| 50 |
|
| 51 |
#include <gamer/biom.h> |
| 52 |
#include "gamercf.h" |
| 53 |
|
| 54 |
SurfaceMesh* SurfaceMesh_sphere(int level) |
| 55 |
{ |
| 56 |
int n,m,l,k; |
| 57 |
int a,b,c,e,f,g; |
| 58 |
float ax,ay,az; |
| 59 |
float bx,by,bz; |
| 60 |
float cx,cy,cz; |
| 61 |
float ex,ey,ez; |
| 62 |
float fx,fy,fz; |
| 63 |
float gx,gy,gz; |
| 64 |
SurfaceMesh *surfmesh; |
| 65 |
int n_v, n_f; |
| 66 |
float length,dist; |
| 67 |
|
| 68 |
// Allocate memory |
| 69 |
if (level == 1) |
| 70 |
surfmesh = SurfaceMesh_ctor(6, 8*(int)pow(4,(level-1))); |
| 71 |
else if (level > 1) |
| 72 |
surfmesh = SurfaceMesh_ctor(12*(int)pow(2,(level-2)) + 8*(int)pow(2,(2*level-3)) - |
| 73 |
8*(int)pow(2,(level-2)) - (int)pow(2,level) + 2, |
| 74 |
8*(int)pow(4,(level-1))); |
| 75 |
else { |
| 76 |
printf(" The level of subdivision must be greater than 0 (usually 4 is good) ....\n"); |
| 77 |
printf(" Try not to use over 7, otherwise, you may have to wait for minutes/hours !!!! .... \n"); |
| 78 |
exit(0); |
| 79 |
} |
| 80 |
|
| 81 |
printf("vertices: %d --- triangles: %d \n",surfmesh->nv,surfmesh->nf); |
| 82 |
|
| 83 |
// initialization |
| 84 |
n_v = 6; |
| 85 |
n_f = 8; |
| 86 |
surfmesh->vertex[0].x = 0; |
| 87 |
surfmesh->vertex[0].y = 0; |
| 88 |
surfmesh->vertex[0].z = 1; |
| 89 |
surfmesh->vertex[1].x = 0; |
| 90 |
surfmesh->vertex[1].y = 0; |
| 91 |
surfmesh->vertex[1].z = -1; |
| 92 |
surfmesh->vertex[2].x = 0; |
| 93 |
surfmesh->vertex[2].y = 1; |
| 94 |
surfmesh->vertex[2].z = 0; |
| 95 |
surfmesh->vertex[3].x = 0; |
| 96 |
surfmesh->vertex[3].y = -1; |
| 97 |
surfmesh->vertex[3].z = 0; |
| 98 |
surfmesh->vertex[4].x = 1; |
| 99 |
surfmesh->vertex[4].y = 0; |
| 100 |
surfmesh->vertex[4].z = 0; |
| 101 |
surfmesh->vertex[5].x = -1; |
| 102 |
surfmesh->vertex[5].y = 0; |
| 103 |
surfmesh->vertex[5].z = 0; |
| 104 |
surfmesh->face[0].a = 0; |
| 105 |
surfmesh->face[0].b = 4; |
| 106 |
surfmesh->face[0].c = 2; |
| 107 |
surfmesh->face[1].a = 0; |
| 108 |
surfmesh->face[1].b = 2; |
| 109 |
surfmesh->face[1].c = 5; |
| 110 |
surfmesh->face[2].a = 0; |
| 111 |
surfmesh->face[2].b = 5; |
| 112 |
surfmesh->face[2].c = 3; |
| 113 |
surfmesh->face[3].a = 0; |
| 114 |
surfmesh->face[3].b = 3; |
| 115 |
surfmesh->face[3].c = 4; |
| 116 |
surfmesh->face[4].a = 1; |
| 117 |
surfmesh->face[4].b = 2; |
| 118 |
surfmesh->face[4].c = 4; |
| 119 |
surfmesh->face[5].a = 1; |
| 120 |
surfmesh->face[5].b = 5; |
| 121 |
surfmesh->face[5].c = 2; |
| 122 |
surfmesh->face[6].a = 1; |
| 123 |
surfmesh->face[6].b = 3; |
| 124 |
surfmesh->face[6].c = 5; |
| 125 |
surfmesh->face[7].a = 1; |
| 126 |
surfmesh->face[7].b = 4; |
| 127 |
surfmesh->face[7].c = 3; |
| 128 |
|
| 129 |
// subdivision |
| 130 |
m = 0; |
| 131 |
while (m < level-1) { |
| 132 |
surfmesh->nv = n_v; |
| 133 |
surfmesh->nf = n_f; |
| 134 |
for (n = 0; n < surfmesh->nf; n++) { |
| 135 |
a = surfmesh->face[n].a; |
| 136 |
b = surfmesh->face[n].b; |
| 137 |
c = surfmesh->face[n].c; |
| 138 |
ax = surfmesh->vertex[a].x; |
| 139 |
ay = surfmesh->vertex[a].y; |
| 140 |
az = surfmesh->vertex[a].z; |
| 141 |
bx = surfmesh->vertex[b].x; |
| 142 |
by = surfmesh->vertex[b].y; |
| 143 |
bz = surfmesh->vertex[b].z; |
| 144 |
cx = surfmesh->vertex[c].x; |
| 145 |
cy = surfmesh->vertex[c].y; |
| 146 |
cz = surfmesh->vertex[c].z; |
| 147 |
|
| 148 |
ex = 0.5*(ax+bx); |
| 149 |
ey = 0.5*(ay+by); |
| 150 |
ez = 0.5*(az+bz); |
| 151 |
length = sqrt(ex*ex+ey*ey+ez*ez); |
| 152 |
ex /= length; |
| 153 |
ey /= length; |
| 154 |
ez /= length; |
| 155 |
k = -1; |
| 156 |
for (l = 0; l < n_v; l++) { |
| 157 |
dist = sqrt((ex-surfmesh->vertex[l].x)*(ex-surfmesh->vertex[l].x)+ |
| 158 |
(ey-surfmesh->vertex[l].y)*(ey-surfmesh->vertex[l].y)+ |
| 159 |
(ez-surfmesh->vertex[l].z)*(ez-surfmesh->vertex[l].z)); |
| 160 |
if (dist < 0.0001) { |
| 161 |
k = l; |
| 162 |
break; |
| 163 |
} |
| 164 |
}//check if the vertex has been created or not |
| 165 |
if (k == -1) { |
| 166 |
e = n_v; |
| 167 |
n_v++; |
| 168 |
} |
| 169 |
else |
| 170 |
e = k; |
| 171 |
surfmesh->vertex[e].x = ex; |
| 172 |
surfmesh->vertex[e].y = ey; |
| 173 |
surfmesh->vertex[e].z = ez; |
| 174 |
|
| 175 |
fx = 0.5*(cx+bx); |
| 176 |
fy = 0.5*(cy+by); |
| 177 |
fz = 0.5*(cz+bz); |
| 178 |
length = sqrt(fx*fx+fy*fy+fz*fz); |
| 179 |
fx /= length; |
| 180 |
fy /= length; |
| 181 |
fz /= length; |
| 182 |
k = -1; |
| 183 |
for (l = 0; l < n_v; l++) { |
| 184 |
dist = sqrt((fx-surfmesh->vertex[l].x)*(fx-surfmesh->vertex[l].x)+ |
| 185 |
(fy-surfmesh->vertex[l].y)*(fy-surfmesh->vertex[l].y)+ |
| 186 |
(fz-surfmesh->vertex[l].z)*(fz-surfmesh->vertex[l].z)); |
| 187 |
if (dist < 0.0001) { |
| 188 |
k = l; |
| 189 |
break; |
| 190 |
} |
| 191 |
}//check if the vertex has been created or not |
| 192 |
if (k == -1) { |
| 193 |
f = n_v; |
| 194 |
n_v++; |
| 195 |
} |
| 196 |
else |
| 197 |
f = k; |
| 198 |
surfmesh->vertex[f].x = fx; |
| 199 |
surfmesh->vertex[f].y = fy; |
| 200 |
surfmesh->vertex[f].z = fz; |
| 201 |
|
| 202 |
gx = 0.5*(cx+ax); |
| 203 |
gy = 0.5*(cy+ay); |
| 204 |
gz = 0.5*(cz+az); |
| 205 |
length = sqrt(gx*gx+gy*gy+gz*gz); |
| 206 |
gx /= length; |
| 207 |
gy /= length; |
| 208 |
gz /= length; |
| 209 |
k = -1; |
| 210 |
for (l = 0; l < n_v; l++) { |
| 211 |
dist = sqrt((gx-surfmesh->vertex[l].x)*(gx-surfmesh->vertex[l].x)+ |
| 212 |
(gy-surfmesh->vertex[l].y)*(gy-surfmesh->vertex[l].y)+ |
| 213 |
(gz-surfmesh->vertex[l].z)*(gz-surfmesh->vertex[l].z)); |
| 214 |
if (dist < 0.0001) { |
| 215 |
k = l; |
| 216 |
break; |
| 217 |
} |
| 218 |
}//check if the vertex has been created or not |
| 219 |
if (k == -1) { |
| 220 |
g = n_v; |
| 221 |
n_v++; |
| 222 |
} |
| 223 |
else |
| 224 |
g = k; |
| 225 |
surfmesh->vertex[g].x = gx; |
| 226 |
surfmesh->vertex[g].y = gy; |
| 227 |
surfmesh->vertex[g].z = gz; |
| 228 |
|
| 229 |
// create new triangles |
| 230 |
surfmesh->face[n].a = e; |
| 231 |
surfmesh->face[n].b = f; |
| 232 |
surfmesh->face[n].c = g; |
| 233 |
surfmesh->face[n_f].a = a; |
| 234 |
surfmesh->face[n_f].b = e; |
| 235 |
surfmesh->face[n_f].c = g; |
| 236 |
n_f++; |
| 237 |
surfmesh->face[n_f].a = e; |
| 238 |
surfmesh->face[n_f].b = b; |
| 239 |
surfmesh->face[n_f].c = f; |
| 240 |
n_f++; |
| 241 |
surfmesh->face[n_f].a = g; |
| 242 |
surfmesh->face[n_f].b = f; |
| 243 |
surfmesh->face[n_f].c = c; |
| 244 |
n_f++; |
| 245 |
} |
| 246 |
m++; |
| 247 |
} |
| 248 |
surfmesh->nv = n_v; |
| 249 |
surfmesh->nf = n_f; |
| 250 |
|
| 251 |
return surfmesh; |
| 252 |
} |