| 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 |
#include <gamer/biom.h> |
| 23 |
|
| 24 |
/* |
| 25 |
* *************************************************************************** |
| 26 |
* Routine: SurfaceMesh_correctNormals |
| 27 |
* |
| 28 |
* Author: Zeyun Yu ([email protected]) |
| 29 |
* |
| 30 |
* Purpose: Function to correct normals so all facet normals point outwards |
| 31 |
* *************************************************************************** |
| 32 |
*/ |
| 33 |
void SurfaceMesh_correctNormals(SurfaceMesh* surfmesh) |
| 34 |
{ |
| 35 |
int n, m; |
| 36 |
int i, j; |
| 37 |
int a, b, c, d; |
| 38 |
int a0, b0, c0; |
| 39 |
int *stack; |
| 40 |
unsigned char *visited; |
| 41 |
int start, end; |
| 42 |
float x, y, z; |
| 43 |
float distance, max_dist; |
| 44 |
int *face_list; |
| 45 |
int progress; |
| 46 |
float max_x, max_y, max_z; |
| 47 |
float ax, ay, az; |
| 48 |
float bx, by, bz; |
| 49 |
float cx, cy, cz; |
| 50 |
|
| 51 |
// get the max coordinates of the nodes |
| 52 |
max_x = -999999.0f; |
| 53 |
max_y = -999999.0f; |
| 54 |
max_z = -999999.0f; |
| 55 |
for (n = 0; n < surfmesh->nv; n++) { |
| 56 |
x = surfmesh->vertex[n].x; |
| 57 |
y = surfmesh->vertex[n].y; |
| 58 |
z = surfmesh->vertex[n].z; |
| 59 |
if (x > max_x) |
| 60 |
max_x = x; |
| 61 |
if (y > max_y) |
| 62 |
max_y = y; |
| 63 |
if (z > max_z) |
| 64 |
max_z = z; |
| 65 |
} |
| 66 |
|
| 67 |
//printf("max coordinates: %f %f %f\n",max_x,max_y,max_z); |
| 68 |
max_x += 10.0f; |
| 69 |
max_y += 10.0f; |
| 70 |
max_z += 10.0f; |
| 71 |
|
| 72 |
// update the normal directions |
| 73 |
stack = (int*)malloc(sizeof(int)*surfmesh->nf); |
| 74 |
visited = (unsigned char*)malloc(sizeof(unsigned char)*surfmesh->nf); |
| 75 |
for (i = 0; i < surfmesh->nf; i++) |
| 76 |
visited[i] = 0; |
| 77 |
start = 0; |
| 78 |
progress = 0; |
| 79 |
while (1) { |
| 80 |
// find the unvisited face that is closest to (max_x,max_y,max_z) |
| 81 |
max_dist = 999999.0f; |
| 82 |
for (i = 0; i < surfmesh->nf; i++) { |
| 83 |
if (visited[i] == 0) { |
| 84 |
a = surfmesh->face[i].a; |
| 85 |
b = surfmesh->face[i].b; |
| 86 |
c = surfmesh->face[i].c; |
| 87 |
x = (surfmesh->vertex[a].x+surfmesh->vertex[b].x+surfmesh->vertex[c].x)/3.0f; |
| 88 |
y = (surfmesh->vertex[a].y+surfmesh->vertex[b].y+surfmesh->vertex[c].y)/3.0f; |
| 89 |
z = (surfmesh->vertex[a].z+surfmesh->vertex[b].z+surfmesh->vertex[c].z)/3.0f; |
| 90 |
distance = sqrt((x-max_x)*(x-max_x)+(y-max_y)*(y-max_y)+(z-max_z)*(z-max_z)); |
| 91 |
if (distance < max_dist) { |
| 92 |
j = i; |
| 93 |
max_dist = distance; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
if (max_dist == 999999.0f) |
| 98 |
break; |
| 99 |
|
| 100 |
a = surfmesh->face[j].a; |
| 101 |
b = surfmesh->face[j].b; |
| 102 |
c = surfmesh->face[j].c; |
| 103 |
ax = surfmesh->vertex[a].x; |
| 104 |
ay = surfmesh->vertex[a].y; |
| 105 |
az = surfmesh->vertex[a].z; |
| 106 |
bx = surfmesh->vertex[b].x; |
| 107 |
by = surfmesh->vertex[b].y; |
| 108 |
bz = surfmesh->vertex[b].z; |
| 109 |
cx = surfmesh->vertex[c].x; |
| 110 |
cy = surfmesh->vertex[c].y; |
| 111 |
cz = surfmesh->vertex[c].z; |
| 112 |
x = (ax+bx+cx)/3.0f; |
| 113 |
y = (ay+by+cy)/3.0f; |
| 114 |
z = (az+bz+cz)/3.0f; |
| 115 |
bx -= ax; |
| 116 |
by -= ay; |
| 117 |
bz -= az; |
| 118 |
cx -= ax; |
| 119 |
cy -= ay; |
| 120 |
cz -= az; |
| 121 |
ax = by*cz-bz*cy; |
| 122 |
ay = bz*cx-bx*cz; |
| 123 |
az = bx*cy-by*cx; |
| 124 |
if (ax*(max_x-x)+ay*(max_y-y)+az*(max_z-z) < 0) { |
| 125 |
// switch the order of the last two vertices |
| 126 |
b = surfmesh->face[j].b; |
| 127 |
surfmesh->face[j].b = surfmesh->face[j].c; |
| 128 |
surfmesh->face[j].c = b; |
| 129 |
} |
| 130 |
|
| 131 |
progress += start; |
| 132 |
stack[0] = j; |
| 133 |
start = 0; |
| 134 |
end = 1; |
| 135 |
while (start < end) { |
| 136 |
if (((start) % 500) == 0) { |
| 137 |
printf("%2.2f%% done (%08d)\r", 100.0*(float)(start+progress)/(float)(surfmesh->nf), start+progress); |
| 138 |
fflush(stdout); |
| 139 |
} |
| 140 |
|
| 141 |
i = stack[start]; |
| 142 |
visited[i] = 1; |
| 143 |
start++; |
| 144 |
a0 = surfmesh->face[i].a; |
| 145 |
b0 = surfmesh->face[i].b; |
| 146 |
c0 = surfmesh->face[i].c; |
| 147 |
|
| 148 |
for (n = 0; n < surfmesh->nf; n++) { |
| 149 |
if (visited[n] != 1) { |
| 150 |
a = surfmesh->face[n].a; |
| 151 |
b = surfmesh->face[n].b; |
| 152 |
c = surfmesh->face[n].c; |
| 153 |
|
| 154 |
if (visited[n] == 0) { |
| 155 |
if (a == a0 && b == b0) { |
| 156 |
j = surfmesh->face[n].a; |
| 157 |
surfmesh->face[n].a = surfmesh->face[n].b; |
| 158 |
surfmesh->face[n].b = j; |
| 159 |
visited[n] = 1; |
| 160 |
stack[end] = n; |
| 161 |
end++; |
| 162 |
} |
| 163 |
else if (b == a0 && a == b0) { |
| 164 |
visited[n] = 1; |
| 165 |
stack[end] = n; |
| 166 |
end++; |
| 167 |
} |
| 168 |
else if (c == a0 && a == b0) { |
| 169 |
j = surfmesh->face[n].a; |
| 170 |
surfmesh->face[n].a = surfmesh->face[n].c; |
| 171 |
surfmesh->face[n].c = j; |
| 172 |
visited[n] = 1; |
| 173 |
stack[end] = n; |
| 174 |
end++; |
| 175 |
} |
| 176 |
else if (a == a0 && c == b0) { |
| 177 |
visited[n] = 1; |
| 178 |
stack[end] = n; |
| 179 |
end++; |
| 180 |
} |
| 181 |
else if (b == a0 && c == b0) { |
| 182 |
j = surfmesh->face[n].b; |
| 183 |
surfmesh->face[n].b = surfmesh->face[n].c; |
| 184 |
surfmesh->face[n].c = j; |
| 185 |
visited[n] = 1; |
| 186 |
stack[end] = n; |
| 187 |
end++; |
| 188 |
} |
| 189 |
else if (c == a0 && b == b0) { |
| 190 |
visited[n] = 1; |
| 191 |
stack[end] = n; |
| 192 |
end++; |
| 193 |
} |
| 194 |
|
| 195 |
else if (b == a0 && a == c0) { |
| 196 |
j = surfmesh->face[n].a; |
| 197 |
surfmesh->face[n].a = surfmesh->face[n].b; |
| 198 |
surfmesh->face[n].b = j; |
| 199 |
visited[n] = 1; |
| 200 |
stack[end] = n; |
| 201 |
end++; |
| 202 |
} |
| 203 |
else if (a == a0 && b == c0) { |
| 204 |
visited[n] = 1; |
| 205 |
stack[end] = n; |
| 206 |
end++; |
| 207 |
} |
| 208 |
else if (a == a0 && c == c0) { |
| 209 |
j = surfmesh->face[n].a; |
| 210 |
surfmesh->face[n].a = surfmesh->face[n].c; |
| 211 |
surfmesh->face[n].c = j; |
| 212 |
visited[n] = 1; |
| 213 |
stack[end] = n; |
| 214 |
end++; |
| 215 |
} |
| 216 |
else if (c == a0 && a == c0) { |
| 217 |
visited[n] = 1; |
| 218 |
stack[end] = n; |
| 219 |
end++; |
| 220 |
} |
| 221 |
else if (c == a0 && b == c0) { |
| 222 |
j = surfmesh->face[n].b; |
| 223 |
surfmesh->face[n].b = surfmesh->face[n].c; |
| 224 |
surfmesh->face[n].c = j; |
| 225 |
visited[n] = 1; |
| 226 |
stack[end] = n; |
| 227 |
end++; |
| 228 |
} |
| 229 |
else if (b == a0 && c == c0) { |
| 230 |
visited[n] = 1; |
| 231 |
stack[end] = n; |
| 232 |
end++; |
| 233 |
} |
| 234 |
|
| 235 |
else if (a == b0 && b == c0) { |
| 236 |
j = surfmesh->face[n].a; |
| 237 |
surfmesh->face[n].a = surfmesh->face[n].b; |
| 238 |
surfmesh->face[n].b = j; |
| 239 |
visited[n] = 1; |
| 240 |
stack[end] = n; |
| 241 |
end++; |
| 242 |
} |
| 243 |
else if (b == b0 && a == c0) { |
| 244 |
visited[n] = 1; |
| 245 |
stack[end] = n; |
| 246 |
end++; |
| 247 |
} |
| 248 |
else if (c == b0 && a == c0) { |
| 249 |
j = surfmesh->face[n].a; |
| 250 |
surfmesh->face[n].a = surfmesh->face[n].c; |
| 251 |
surfmesh->face[n].c = j; |
| 252 |
visited[n] = 1; |
| 253 |
stack[end] = n; |
| 254 |
end++; |
| 255 |
} |
| 256 |
else if (a == b0 && c == c0) { |
| 257 |
visited[n] = 1; |
| 258 |
stack[end] = n; |
| 259 |
end++; |
| 260 |
} |
| 261 |
else if (b == b0 && c == c0) { |
| 262 |
j = surfmesh->face[n].b; |
| 263 |
surfmesh->face[n].b = surfmesh->face[n].c; |
| 264 |
surfmesh->face[n].c = j; |
| 265 |
visited[n] = 1; |
| 266 |
stack[end] = n; |
| 267 |
end++; |
| 268 |
} |
| 269 |
else if (c == b0 && b == c0) { |
| 270 |
visited[n] = 1; |
| 271 |
stack[end] = n; |
| 272 |
end++; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|