| 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: SurfRefine.C |
| 25 |
* |
| 26 |
* Author: Johan Hake ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: Refine the surface mesh by subdividing each triangle into four |
| 29 |
* *************************************************************************** |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <gamer/biom.h> |
| 33 |
#include "gamercf.h" |
| 34 |
|
| 35 |
|
| 36 |
/* |
| 37 |
* *************************************************************************** |
| 38 |
* Routine: SurfaceMesh_refine |
| 39 |
* |
| 40 |
* Author: Johan Hake ([email protected]) |
| 41 |
* |
| 42 |
* Purpose: Refine a surface mesh and recalculate the neighbors |
| 43 |
* *************************************************************************** |
| 44 |
*/ |
| 45 |
void SurfaceMesh_refine(SurfaceMesh* surfmesh) |
| 46 |
{ |
| 47 |
unsigned int local_num_edges = 0, total_num_edges = 0, edge_num = 0; |
| 48 |
unsigned int min_vertex_num, max_vertex_num, face_num; |
| 49 |
unsigned int k, m, n, nv; |
| 50 |
unsigned int a, b, c; |
| 51 |
unsigned int* num_edges; |
| 52 |
unsigned int* offsets; |
| 53 |
unsigned int* vertex2edge; |
| 54 |
unsigned int local_vertices[3], local_additional_vertices[3]; |
| 55 |
NPNT3* ngr; |
| 56 |
SurfaceMesh* surfmesh_refine; |
| 57 |
float ax, ay, az; |
| 58 |
float nx, ny, nz; |
| 59 |
|
| 60 |
// Check if neighborlist is created |
| 61 |
if (surfmesh->neighbor_list == NULL) |
| 62 |
SurfaceMesh_createNeighborlist(surfmesh); |
| 63 |
|
| 64 |
NPNT3** neighbor_list = surfmesh->neighbor_list; |
| 65 |
|
| 66 |
// Assign the number of vertices in the original mesh |
| 67 |
nv = surfmesh->nv; |
| 68 |
|
| 69 |
// Create an array with the number of edges associated with each vertex |
| 70 |
num_edges = (unsigned int*)malloc(sizeof(unsigned int)*nv); |
| 71 |
|
| 72 |
// Create an array with the offsets into the vertex2edge array for each vertex |
| 73 |
offsets = (unsigned int*)malloc(sizeof(unsigned int)*nv); |
| 74 |
|
| 75 |
// Iterate over all vertices and collect edges |
| 76 |
for (n = 0; n < nv; n++){ |
| 77 |
offsets[n] = total_num_edges; |
| 78 |
local_num_edges = 0; |
| 79 |
ngr = neighbor_list[n]; |
| 80 |
while (ngr != NULL) { |
| 81 |
// If n is smaller than ngr->a we have an edge |
| 82 |
if (n < ngr->a){ |
| 83 |
total_num_edges++; |
| 84 |
local_num_edges++; |
| 85 |
} |
| 86 |
ngr = ngr->next; |
| 87 |
} |
| 88 |
num_edges[n] = local_num_edges; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
// Create memory for the refined mesh |
| 93 |
surfmesh_refine = SurfaceMesh_ctor(nv + total_num_edges, surfmesh->nf*4); |
| 94 |
surfmesh_refine->nv = nv; |
| 95 |
surfmesh_refine->nf = surfmesh->nf; |
| 96 |
|
| 97 |
// Copy the original mesh to the new mesh |
| 98 |
for (n = 0; n < nv; n++) { |
| 99 |
surfmesh_refine->vertex[n].x = surfmesh->vertex[n].x; |
| 100 |
surfmesh_refine->vertex[n].y = surfmesh->vertex[n].y; |
| 101 |
surfmesh_refine->vertex[n].z = surfmesh->vertex[n].z; |
| 102 |
} |
| 103 |
|
| 104 |
for (n = 0; n < surfmesh->nf; n++) { |
| 105 |
surfmesh_refine->face[n].a = surfmesh->face[n].a; |
| 106 |
surfmesh_refine->face[n].b = surfmesh->face[n].b; |
| 107 |
surfmesh_refine->face[n].c = surfmesh->face[n].c; |
| 108 |
} |
| 109 |
|
| 110 |
// Create the map from vertices to edges |
| 111 |
vertex2edge = (unsigned int*)malloc(sizeof(unsigned int)*total_num_edges); |
| 112 |
|
| 113 |
// Iterate over all vertices and split edges |
| 114 |
for (n = 0; n < nv; n++){ |
| 115 |
|
| 116 |
// Get the coordinates of vertex n |
| 117 |
nx = surfmesh_refine->vertex[n].x; |
| 118 |
ny = surfmesh_refine->vertex[n].y; |
| 119 |
nz = surfmesh_refine->vertex[n].z; |
| 120 |
|
| 121 |
ngr = neighbor_list[n]; |
| 122 |
while (ngr != NULL) { |
| 123 |
|
| 124 |
// If n is smaller than ngr->a we have an edge |
| 125 |
if (n < ngr->a){ |
| 126 |
|
| 127 |
// Add the value of the opposite vertex to the map |
| 128 |
vertex2edge[edge_num] = ngr->a; |
| 129 |
|
| 130 |
// Get the coordinates of vertex ngr->a |
| 131 |
ax = surfmesh_refine->vertex[ngr->a].x; |
| 132 |
ay = surfmesh_refine->vertex[ngr->a].y; |
| 133 |
az = surfmesh_refine->vertex[ngr->a].z; |
| 134 |
|
| 135 |
// Add the new vertex coordinates of the splitted edge |
| 136 |
surfmesh_refine->vertex[nv + edge_num].x = 0.5*(ax + nx); |
| 137 |
surfmesh_refine->vertex[nv + edge_num].y = 0.5*(ay + ny); |
| 138 |
surfmesh_refine->vertex[nv + edge_num].z = 0.5*(az + nz); |
| 139 |
|
| 140 |
// Increase the edge number |
| 141 |
edge_num++; |
| 142 |
} |
| 143 |
ngr = ngr->next; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// A counter for adding new faces |
| 148 |
face_num = surfmesh_refine->nf; |
| 149 |
|
| 150 |
// Iterate over faces and add information of the refined face |
| 151 |
for (n = 0; n < surfmesh_refine->nf; n++) { |
| 152 |
local_vertices[0] = surfmesh_refine->face[n].a; |
| 153 |
local_vertices[1] = surfmesh_refine->face[n].b; |
| 154 |
local_vertices[2] = surfmesh_refine->face[n].c; |
| 155 |
|
| 156 |
// Iterate over the vertices and find the edges |
| 157 |
for (m = 0; m < 3; m++){ |
| 158 |
min_vertex_num = min(local_vertices[m], local_vertices[(m+1)%3]); |
| 159 |
max_vertex_num = max(local_vertices[m], local_vertices[(m+1)%3]); |
| 160 |
|
| 161 |
// Find the edge number that fit the pair of vertices |
| 162 |
for (k = 0; k < num_edges[min_vertex_num]; k++) |
| 163 |
if (vertex2edge[offsets[min_vertex_num] + k] == max_vertex_num) |
| 164 |
break; |
| 165 |
|
| 166 |
// The edge number represents the number of the added vertex plus the |
| 167 |
// number of original vertices |
| 168 |
local_additional_vertices[m] = nv + offsets[min_vertex_num] + k; |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
// Add information of the four new faces |
| 173 |
|
| 174 |
// First the mid face |
| 175 |
surfmesh_refine->face[n].a = local_additional_vertices[0]; |
| 176 |
surfmesh_refine->face[n].b = local_additional_vertices[1]; |
| 177 |
surfmesh_refine->face[n].c = local_additional_vertices[2]; |
| 178 |
|
| 179 |
// Then the three corner faces |
| 180 |
for (m = 0; m < 3; m++){ |
| 181 |
surfmesh_refine->face[face_num].a = local_vertices[m]; |
| 182 |
surfmesh_refine->face[face_num].b = local_additional_vertices[m]; |
| 183 |
surfmesh_refine->face[face_num].c = local_additional_vertices[(m+2)%3]; |
| 184 |
face_num++; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// Release memory |
| 189 |
free(num_edges); |
| 190 |
free(offsets); |
| 191 |
free(vertex2edge); |
| 192 |
|
| 193 |
// Update number information |
| 194 |
surfmesh_refine->nv += total_num_edges; |
| 195 |
surfmesh_refine->nf *= 4; |
| 196 |
|
| 197 |
// Release old data |
| 198 |
SurfaceMesh_releaseData(surfmesh); |
| 199 |
|
| 200 |
// Assign the refined mesh to the passed |
| 201 |
surfmesh->nv = surfmesh_refine->nv; |
| 202 |
surfmesh->nf = surfmesh_refine->nf; |
| 203 |
surfmesh->vertex = surfmesh_refine->vertex; |
| 204 |
surfmesh->face = surfmesh_refine->face; |
| 205 |
|
| 206 |
// Free memory of refined surface mesh struct |
| 207 |
free(surfmesh_refine); |
| 208 |
|
| 209 |
// Recreate the neigborlist |
| 210 |
SurfaceMesh_createNeighborlist(surfmesh); |
| 211 |
|
| 212 |
} |