| 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: SurfExtract.C < ... > |
| 25 |
* |
| 26 |
* Author: Zeyun Yu ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: Extract the surface mesh from a given tetrahedral mesh |
| 29 |
* *************************************************************************** |
| 30 |
*/ |
| 31 |
|
| 32 |
|
| 33 |
#include <gamer/biom.h> |
| 34 |
#include "gamercf.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/* |
| 39 |
* *************************************************************************** |
| 40 |
* Routine: SurfaceExtract < ... > |
| 41 |
* |
| 42 |
* Author: Zeyun Yu ([email protected]) |
| 43 |
* |
| 44 |
* Purpose: Extract the surface mesh from a given tetrahedral mesh |
| 45 |
* FIXME: It should be possible to speed up this... |
| 46 |
* *************************************************************************** |
| 47 |
*/ |
| 48 |
void SurfaceExtract(TeTraMesh* volmesh, SurfaceMesh* surfmesh) |
| 49 |
{ |
| 50 |
int m,n,l; |
| 51 |
int start_index, face_num, num_vertices, vertex_index; |
| 52 |
int *vertex_indices; |
| 53 |
int a0, b0, c0, d0; |
| 54 |
int a, b, c, d; |
| 55 |
float x, y, z; |
| 56 |
char *boundary_flag; |
| 57 |
char surf_flag; |
| 58 |
unsigned int num_surfaces_vertices; |
| 59 |
INT3VECT* surfmesh_face; |
| 60 |
|
| 61 |
// Initalize enough memory |
| 62 |
surfmesh_face = (INT3VECT*)malloc(sizeof(INT3VECT)*volmesh->nf); |
| 63 |
|
| 64 |
/* Classify the vertices into boundary or interior */ |
| 65 |
boundary_flag = (char *)malloc(sizeof(char)*volmesh->nv); |
| 66 |
for (n = 0; n < volmesh->nv; n++) |
| 67 |
boundary_flag[n] = 0; |
| 68 |
|
| 69 |
// Walk over all tetrahedrons of the Volume mesh |
| 70 |
face_num = 0; |
| 71 |
num_vertices = 0; |
| 72 |
for (n = 0; n < volmesh->nf; n++) { |
| 73 |
printf("%2.1f%% done \r", 100.0*(double)(n)/(double)(volmesh->nf-1)); |
| 74 |
fflush(stdout); |
| 75 |
|
| 76 |
// Get the vertices of the tetrahedron |
| 77 |
a = volmesh->face[n].a; |
| 78 |
b = volmesh->face[n].b; |
| 79 |
c = volmesh->face[n].c; |
| 80 |
d = volmesh->face[n].d; |
| 81 |
|
| 82 |
// Walk over all tetrahedrons and check if the (a, b, c) face is shared by another tet |
| 83 |
surf_flag = 1; |
| 84 |
for (m = 0; m < volmesh->nf; m++) { |
| 85 |
if (m != n) { |
| 86 |
a0 = volmesh->face[m].a; |
| 87 |
b0 = volmesh->face[m].b; |
| 88 |
c0 = volmesh->face[m].c; |
| 89 |
d0 = volmesh->face[m].d; |
| 90 |
l = 0; |
| 91 |
if (a==a0 || a==b0 || a==c0 || a==d0) |
| 92 |
l++; |
| 93 |
if (b==a0 || b==b0 || b==c0 || b==d0) |
| 94 |
l++; |
| 95 |
if (c==a0 || c==b0 || c==c0 || c==d0) |
| 96 |
l++; |
| 97 |
|
| 98 |
// m and n share a face |
| 99 |
if (l == 3) { |
| 100 |
surf_flag = 0; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
if (surf_flag == 1) { |
| 106 |
num_vertices += (boundary_flag[a] == 0) ? 1 : 0; |
| 107 |
num_vertices += (boundary_flag[b] == 0) ? 1 : 0; |
| 108 |
num_vertices += (boundary_flag[c] == 0) ? 1 : 0; |
| 109 |
boundary_flag[a] = 1; |
| 110 |
boundary_flag[b] = 1; |
| 111 |
boundary_flag[c] = 1; |
| 112 |
surfmesh_face[face_num].a = a; |
| 113 |
surfmesh_face[face_num].b = c; |
| 114 |
surfmesh_face[face_num].c = b; |
| 115 |
face_num++; |
| 116 |
} |
| 117 |
|
| 118 |
// Walk over all tetrahedrons and check if the (a, b, d) face is shared by another tet |
| 119 |
surf_flag = 1; |
| 120 |
for (m = 0; m < volmesh->nf; m++) { |
| 121 |
if (m != n) { |
| 122 |
a0 = volmesh->face[m].a; |
| 123 |
b0 = volmesh->face[m].b; |
| 124 |
c0 = volmesh->face[m].c; |
| 125 |
d0 = volmesh->face[m].d; |
| 126 |
l = 0; |
| 127 |
if (a==a0 || a==b0 || a==c0 || a==d0) |
| 128 |
l++; |
| 129 |
if (b==a0 || b==b0 || b==c0 || b==d0) |
| 130 |
l++; |
| 131 |
if (d==a0 || d==b0 || d==c0 || d==d0) |
| 132 |
l++; |
| 133 |
if (l == 3) { |
| 134 |
surf_flag = 0; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// m and n share a face |
| 141 |
if (surf_flag == 1) { |
| 142 |
num_vertices += (boundary_flag[a] == 0) ? 1 : 0; |
| 143 |
num_vertices += (boundary_flag[b] == 0) ? 1 : 0; |
| 144 |
num_vertices += (boundary_flag[d] == 0) ? 1 : 0; |
| 145 |
boundary_flag[a] = 1; |
| 146 |
boundary_flag[b] = 1; |
| 147 |
boundary_flag[d] = 1; |
| 148 |
surfmesh_face[face_num].a = a; |
| 149 |
surfmesh_face[face_num].b = b; |
| 150 |
surfmesh_face[face_num].c = d; |
| 151 |
face_num++; |
| 152 |
} |
| 153 |
|
| 154 |
// Walk over all tetrahedrons and check if the (a, c, d) face is shared by another tet |
| 155 |
surf_flag = 1; |
| 156 |
for (m = 0; m < volmesh->nf; m++) { |
| 157 |
if (m != n) { |
| 158 |
a0 = volmesh->face[m].a; |
| 159 |
b0 = volmesh->face[m].b; |
| 160 |
c0 = volmesh->face[m].c; |
| 161 |
d0 = volmesh->face[m].d; |
| 162 |
l = 0; |
| 163 |
if (a==a0 || a==b0 || a==c0 || a==d0) |
| 164 |
l++; |
| 165 |
if (c==a0 || c==b0 || c==c0 || c==d0) |
| 166 |
l++; |
| 167 |
if (d==a0 || d==b0 || d==c0 || d==d0) |
| 168 |
l++; |
| 169 |
if (l == 3) { |
| 170 |
surf_flag = 0; |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// m and n share a face |
| 177 |
if (surf_flag == 1) { |
| 178 |
num_vertices += (boundary_flag[a] == 0) ? 1 : 0; |
| 179 |
num_vertices += (boundary_flag[c] == 0) ? 1 : 0; |
| 180 |
num_vertices += (boundary_flag[d] == 0) ? 1 : 0; |
| 181 |
boundary_flag[a] = 1; |
| 182 |
boundary_flag[c] = 1; |
| 183 |
boundary_flag[d] = 1; |
| 184 |
surfmesh_face[face_num].a = a; |
| 185 |
surfmesh_face[face_num].b = d; |
| 186 |
surfmesh_face[face_num].c = c; |
| 187 |
face_num++; |
| 188 |
} |
| 189 |
|
| 190 |
// Walk over all tetrahedrons and check if the (b, c, d) face is shared by another tet |
| 191 |
surf_flag = 1; |
| 192 |
for (m = 0; m < volmesh->nf; m++) { |
| 193 |
if (m != n) { |
| 194 |
a0 = volmesh->face[m].a; |
| 195 |
b0 = volmesh->face[m].b; |
| 196 |
c0 = volmesh->face[m].c; |
| 197 |
d0 = volmesh->face[m].d; |
| 198 |
l = 0; |
| 199 |
if (b==a0 || b==b0 || b==c0 || b==d0) |
| 200 |
l++; |
| 201 |
if (c==a0 || c==b0 || c==c0 || c==d0) |
| 202 |
l++; |
| 203 |
if (d==a0 || d==b0 || d==c0 || d==d0) |
| 204 |
l++; |
| 205 |
if (l == 3) { |
| 206 |
surf_flag = 0; |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// m and n share a face |
| 213 |
if (surf_flag == 1) { |
| 214 |
num_vertices += (boundary_flag[b] == 0) ? 1 : 0; |
| 215 |
num_vertices += (boundary_flag[c] == 0) ? 1 : 0; |
| 216 |
num_vertices += (boundary_flag[d] == 0) ? 1 : 0; |
| 217 |
boundary_flag[b] = 1; |
| 218 |
boundary_flag[c] = 1; |
| 219 |
boundary_flag[d] = 1; |
| 220 |
surfmesh_face[face_num].a = b; |
| 221 |
surfmesh_face[face_num].b = c; |
| 222 |
surfmesh_face[face_num].c = d; |
| 223 |
face_num++; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// Assign new memory to the surface mesh |
| 228 |
surfmesh->nv = num_vertices; |
| 229 |
surfmesh->nf = face_num; |
| 230 |
surfmesh->vertex = (FLTVECT*)malloc(sizeof(FLTVECT)*num_vertices); |
| 231 |
surfmesh->face = (INT3VECT*)malloc(sizeof(INT3VECT)*face_num); |
| 232 |
|
| 233 |
// Assign vertex coordinates to surface mesh |
| 234 |
vertex_index = 0; |
| 235 |
vertex_indices = (int*)malloc(sizeof(int)*volmesh->nv); |
| 236 |
for (n = 0; n < volmesh->nv; n++) { |
| 237 |
if (boundary_flag[n]) { |
| 238 |
surfmesh->vertex[vertex_index].x = volmesh->vertex[n].x; |
| 239 |
surfmesh->vertex[vertex_index].y = volmesh->vertex[n].y; |
| 240 |
surfmesh->vertex[vertex_index].z = volmesh->vertex[n].z; |
| 241 |
vertex_indices[n] = vertex_index; |
| 242 |
vertex_index++; |
| 243 |
} |
| 244 |
else { |
| 245 |
vertex_indices[n] = -1; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Assign the correct face number to surface mesh |
| 250 |
for (n = 0; n < surfmesh->nf; n++) { |
| 251 |
surfmesh->face[n].a = vertex_indices[surfmesh_face[n].a]; |
| 252 |
surfmesh->face[n].b = vertex_indices[surfmesh_face[n].b]; |
| 253 |
surfmesh->face[n].c = vertex_indices[surfmesh_face[n].c]; |
| 254 |
} |
| 255 |
|
| 256 |
printf("Surface Mesh Extracted: Nodes = %d, Faces = %d\n\n", surfmesh->nv, surfmesh->nf); |
| 257 |
|
| 258 |
free(vertex_indices); |
| 259 |
free(boundary_flag); |
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
/* |
| 267 |
* *************************************************************************** |
| 268 |
* Routine: SurfaceMesh_merge |
| 269 |
* |
| 270 |
* Author: Zeyun Yu ([email protected]) |
| 271 |
* |
| 272 |
* Purpose: Merge two surface meshes into a single one |
| 273 |
* *************************************************************************** |
| 274 |
*/ |
| 275 |
SurfaceMesh * SurfaceMesh_merge(SurfaceMesh *surfmesh_in0, SurfaceMesh *surfmesh_in1) |
| 276 |
{ |
| 277 |
SurfaceMesh *surfmesh; |
| 278 |
int i; |
| 279 |
|
| 280 |
surfmesh = SurfaceMesh_ctor(surfmesh_in0->nv + surfmesh_in1->nv, |
| 281 |
surfmesh_in0->nf + surfmesh_in1->nf); |
| 282 |
|
| 283 |
|
| 284 |
for (i = 0; i < surfmesh_in0->nv; i++) { |
| 285 |
surfmesh->vertex[i].x = surfmesh_in0->vertex[i].x; |
| 286 |
surfmesh->vertex[i].y = surfmesh_in0->vertex[i].y; |
| 287 |
surfmesh->vertex[i].z = surfmesh_in0->vertex[i].z; |
| 288 |
} |
| 289 |
for (i = 0; i < surfmesh_in1->nv; i++) { |
| 290 |
surfmesh->vertex[i+surfmesh_in0->nv].x = surfmesh_in1->vertex[i].x; |
| 291 |
surfmesh->vertex[i+surfmesh_in0->nv].y = surfmesh_in1->vertex[i].y; |
| 292 |
surfmesh->vertex[i+surfmesh_in0->nv].z = surfmesh_in1->vertex[i].z; |
| 293 |
} |
| 294 |
|
| 295 |
for (i = 0; i < surfmesh_in0->nf; i++) { |
| 296 |
surfmesh->face[i].a = surfmesh_in0->face[i].a; |
| 297 |
surfmesh->face[i].b = surfmesh_in0->face[i].b; |
| 298 |
surfmesh->face[i].c = surfmesh_in0->face[i].c; |
| 299 |
} |
| 300 |
for (i = 0; i < surfmesh_in1->nf; i++) { |
| 301 |
surfmesh->face[i+surfmesh_in0->nf].a = surfmesh_in1->face[i].a+surfmesh_in0->nv; |
| 302 |
surfmesh->face[i+surfmesh_in0->nf].b = surfmesh_in1->face[i].b+surfmesh_in0->nv; |
| 303 |
surfmesh->face[i+surfmesh_in0->nf].c = surfmesh_in1->face[i].c+surfmesh_in0->nv; |
| 304 |
} |
| 305 |
|
| 306 |
return surfmesh; |
| 307 |
|
| 308 |
} |
| 309 |
|