| 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: ImproveSurfMesh.C |
| 25 |
* |
| 26 |
* Author: Johan Hake ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: The 'main' function of ImproveSurfMesh |
| 29 |
* *************************************************************************** |
| 30 |
*/ |
| 31 |
|
| 32 |
|
| 33 |
#include <gamer/gamer.h> |
| 34 |
|
| 35 |
// Constants and default values |
| 36 |
const char OUT_SUFFIX[] = "_improved_"; |
| 37 |
const unsigned char OUT_SUFFIX_LENGTH = 10; |
| 38 |
const unsigned int MAX_ITER = 6; |
| 39 |
const unsigned int NUM_ITER = 1; |
| 40 |
const unsigned int MAX_MIN_ANGLE = 15; |
| 41 |
const unsigned int MIN_MAX_ANGLE = 150; |
| 42 |
const unsigned int FLIP_EDGES = 1; |
| 43 |
const float FLAT_RATE = 0.016; |
| 44 |
const float DENSE_RATE = 1.5; |
| 45 |
const float MAX_MAX_NORMAL_ANGLE = -1.0; |
| 46 |
|
| 47 |
// flag for changed mesh |
| 48 |
bool mesh_changed = false; |
| 49 |
|
| 50 |
// Action enums |
| 51 |
enum ImproveAction {no_action, refine, smooth, normal_smooth, coarse_dense, |
| 52 |
coarse_flat, correct_normals, stats}; |
| 53 |
|
| 54 |
// Action struct |
| 55 |
struct ActionItem { |
| 56 |
ImproveAction action; |
| 57 |
unsigned int max_iter; |
| 58 |
unsigned int num_iter; |
| 59 |
unsigned int max_min_angle; |
| 60 |
unsigned int min_max_angle; |
| 61 |
unsigned int flip_edges; |
| 62 |
float flat_rate, dense_rate; |
| 63 |
float max_max_normal_angle; |
| 64 |
ActionItem* next; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
// Constructor of ActionItem |
| 69 |
ActionItem* new_ActionItem() |
| 70 |
{ |
| 71 |
ActionItem* action_item; |
| 72 |
action_item = (ActionItem*)malloc(sizeof(ActionItem)); |
| 73 |
|
| 74 |
// Assign default values |
| 75 |
action_item->action = no_action; |
| 76 |
action_item->max_iter = MAX_ITER; |
| 77 |
action_item->num_iter = NUM_ITER; |
| 78 |
action_item->max_min_angle = MAX_MIN_ANGLE; |
| 79 |
action_item->min_max_angle = MIN_MAX_ANGLE; |
| 80 |
action_item->flip_edges = FLIP_EDGES; |
| 81 |
action_item->dense_rate = DENSE_RATE; |
| 82 |
action_item->flat_rate = FLAT_RATE; |
| 83 |
action_item->max_max_normal_angle = MAX_MAX_NORMAL_ANGLE; |
| 84 |
action_item->next = NULL; |
| 85 |
return action_item; |
| 86 |
} |
| 87 |
|
| 88 |
// Destructor of ActionItem |
| 89 |
void delete_ActionItem(ActionItem* action_list) |
| 90 |
{ |
| 91 |
ActionItem* tmp_list; |
| 92 |
while (action_list != NULL) { |
| 93 |
tmp_list = action_list->next; |
| 94 |
free(action_list); |
| 95 |
action_list = tmp_list; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Print usage of program |
| 100 |
void usage() |
| 101 |
{ |
| 102 |
printf(""); |
| 103 |
printf("Usage: ImproveSurfMesh [options...] file\n" |
| 104 |
"\n" |
| 105 |
" file - A surface mesh given in .off format\n" |
| 106 |
"\n" |
| 107 |
"Several options can be combined. The order of the options will deside \n" |
| 108 |
"in what order the mesh improvements are applied.\n" |
| 109 |
"\n"); |
| 110 |
printf("Options:\n"); |
| 111 |
printf(" --smooth [MAX_ITER [FLIP_EDGES [MAX_MIN_ANGLE MIN_MAX_ANGLE]]]\n" |
| 112 |
" Improve the quality of the surface mesh using a angle-based\n" |
| 113 |
" method. Geometrical features are preserved using a local structure\n" |
| 114 |
" tensor. The algorithm is run until MAX_ITER is reached or the\n" |
| 115 |
" angle conditions are saticefied.\n" |
| 116 |
"\n" |
| 117 |
" MAX_ITER[%d]: Maximal iteration of smooth cycles.\n" |
| 118 |
"\n" |
| 119 |
" FLIP_EDGES[1]: Flip edges between neigboring bad triangles during\n"\ |
| 120 |
" smooth\n" |
| 121 |
"\n" |
| 122 |
" MAX_MIN_ANGLE[%d]: The maximal value of the minimal angle.\n" |
| 123 |
"\n" |
| 124 |
" MIN_MAX_ANGLE[%d]: The minimal value of the maximal angle.\n" |
| 125 |
"\n", |
| 126 |
MAX_ITER, MAX_MIN_ANGLE, MIN_MAX_ANGLE); |
| 127 |
printf(" --normal-smooth\n" |
| 128 |
" Improve the quality of the surface mesh by smoothing the vertex\n" |
| 129 |
" normals. Adviceable to do after a coarse - smooth cycle.\n" |
| 130 |
"\n"); |
| 131 |
printf(" --coarse-flat [RATE]\n" |
| 132 |
" Remove vertices in flat areas\n" |
| 133 |
"\n" |
| 134 |
" RATE[%0.3f]: A cutoff value for deciding if a vertex will be removed.\n" |
| 135 |
" A larger number will remove more vertices.\n" |
| 136 |
"\n", |
| 137 |
FLAT_RATE); |
| 138 |
printf(" --coarse-dense [NUM_ITER [RATE]]\n" |
| 139 |
" Remove vertices in dense areas.\n" |
| 140 |
"\n" |
| 141 |
" NUM_ITER[%d]: Number of coarsening cycles.\n" |
| 142 |
"\n" |
| 143 |
" RATE[%0.1f]: A cutoff value for deciding if a vertex will be removed.\n" |
| 144 |
" A larger number will remove more vertices.\n" |
| 145 |
"\n" |
| 146 |
"\n", |
| 147 |
NUM_ITER, DENSE_RATE); |
| 148 |
printf(" --refine\n" |
| 149 |
" Uniformly refine mesh\n" |
| 150 |
"\n"); |
| 151 |
printf(" --correct-normals\n" |
| 152 |
" Correct the face normals so they all point outward.\n" |
| 153 |
"\n"); |
| 154 |
printf(" --active-sites FILE \n" |
| 155 |
" A file with active sites which will not be coarsed.\n" |
| 156 |
" See README for specifications.\n" |
| 157 |
"\n"); |
| 158 |
printf(" --stats [MAX_MIN_ANGLE MIN_MAX_ANGLE]\n" |
| 159 |
" Print quality statistics of a surface mesh.\n" |
| 160 |
"\n" |
| 161 |
" MAX_MIN_ANGLE[%d]: The maximal value of the minimal angle.\n" |
| 162 |
"\n" |
| 163 |
" MIN_MAX_ANGLE[%d]: The minimal value of the maximal angle.\n" |
| 164 |
"\n", |
| 165 |
MAX_MIN_ANGLE, MIN_MAX_ANGLE); |
| 166 |
printf(" --help\n" |
| 167 |
" Print this help.\n"); |
| 168 |
} |
| 169 |
|
| 170 |
void option_error() |
| 171 |
{ |
| 172 |
usage(); |
| 173 |
exit(1); |
| 174 |
} |
| 175 |
|
| 176 |
// Small helper function to increase the arg item variable |
| 177 |
void increase_arg_item(int& arg_item, int argc) |
| 178 |
{ |
| 179 |
arg_item++; |
| 180 |
if (arg_item == argc) |
| 181 |
option_error(); |
| 182 |
} |
| 183 |
|
| 184 |
// Parse the command line arguments |
| 185 |
void parse_comand_line(int argc, char *argv[], char*& input_filename, |
| 186 |
char*& input_sites, ActionItem*& action_list) |
| 187 |
{ |
| 188 |
// Init local variables |
| 189 |
ActionItem* action_item = NULL; |
| 190 |
int arg_item = 1; |
| 191 |
|
| 192 |
// If not options given |
| 193 |
if (argc == arg_item) |
| 194 |
option_error(); |
| 195 |
|
| 196 |
// Parse command line |
| 197 |
while (arg_item < argc){ |
| 198 |
|
| 199 |
// Parse --active-sites option |
| 200 |
// ------------------------------------------------------------------ |
| 201 |
if (strcmp (argv[arg_item], "--active-sites") == 0){ |
| 202 |
increase_arg_item(arg_item, argc); |
| 203 |
|
| 204 |
// Get active site file name |
| 205 |
input_sites = argv[arg_item]; |
| 206 |
increase_arg_item(arg_item, argc); |
| 207 |
|
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
// If the next argument is an action option |
| 212 |
if (strncmp(argv[arg_item], "--", 2) == 0){ |
| 213 |
|
| 214 |
// If it is the first action item created |
| 215 |
if (action_item == NULL){ |
| 216 |
action_item = new_ActionItem(); |
| 217 |
action_list = action_item; |
| 218 |
} |
| 219 |
else { |
| 220 |
action_item->next = new_ActionItem(); |
| 221 |
action_item = action_item->next; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// If the next argument is a letter assume input file name |
| 226 |
else if ( isalpha(argv[arg_item][0])){ |
| 227 |
input_filename = argv[arg_item]; |
| 228 |
arg_item++; |
| 229 |
if (arg_item != argc) |
| 230 |
option_error(); |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
// Parse --help |
| 235 |
// ------------------------------------------------------------------ |
| 236 |
if (strcmp (argv[arg_item], "--help") == 0) |
| 237 |
option_error(); |
| 238 |
|
| 239 |
// Parse --refine option |
| 240 |
// ------------------------------------------------------------------ |
| 241 |
if (strcmp (argv[arg_item], "--refine") == 0){ |
| 242 |
action_item->action = refine; |
| 243 |
increase_arg_item(arg_item, argc); |
| 244 |
continue; |
| 245 |
} |
| 246 |
|
| 247 |
// Parse --normal-smooth option |
| 248 |
// ------------------------------------------------------------------ |
| 249 |
if (strcmp (argv[arg_item], "--normal-smooth") == 0){ |
| 250 |
action_item->action = normal_smooth; |
| 251 |
increase_arg_item(arg_item, argc); |
| 252 |
continue; |
| 253 |
} |
| 254 |
|
| 255 |
// Parse --correct-normals option |
| 256 |
// ------------------------------------------------------------------ |
| 257 |
if (strcmp (argv[arg_item], "--correct-normals") == 0){ |
| 258 |
action_item->action = correct_normals; |
| 259 |
increase_arg_item(arg_item, argc); |
| 260 |
continue; |
| 261 |
} |
| 262 |
|
| 263 |
// Parse --smooth option |
| 264 |
// ------------------------------------------------------------------ |
| 265 |
if (strcmp (argv[arg_item], "--smooth") == 0){ |
| 266 |
|
| 267 |
action_item->action = smooth; |
| 268 |
increase_arg_item(arg_item, argc); |
| 269 |
|
| 270 |
// If no values passed |
| 271 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 272 |
continue; |
| 273 |
|
| 274 |
// Parse max iter argument |
| 275 |
action_item->max_iter = atoi(argv[arg_item]); |
| 276 |
increase_arg_item(arg_item, argc); |
| 277 |
|
| 278 |
// Check value |
| 279 |
if (action_item->max_iter < 1){ |
| 280 |
printf("*** Argument error: Expected a positive value for the \'MAX_ITER\' argument.\n\n"); |
| 281 |
exit(1); |
| 282 |
} |
| 283 |
|
| 284 |
// If no other values passed |
| 285 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 286 |
continue; |
| 287 |
|
| 288 |
// Parse flip edges argument |
| 289 |
action_item->flip_edges = atoi(argv[arg_item]); |
| 290 |
increase_arg_item(arg_item, argc); |
| 291 |
|
| 292 |
// Check value |
| 293 |
if (action_item->flip_edges != 1 && action_item->flip_edges != 0){ |
| 294 |
printf("*** Argument error: Expected \'0\' or \'1\' for the \'FLIP_EDGES\' argument.\n\n"); |
| 295 |
exit(1); |
| 296 |
} |
| 297 |
|
| 298 |
// If no other values passed |
| 299 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 300 |
continue; |
| 301 |
|
| 302 |
// Parse max min angle |
| 303 |
action_item->max_min_angle = atoi(argv[arg_item]); |
| 304 |
increase_arg_item(arg_item, argc); |
| 305 |
|
| 306 |
// Check value |
| 307 |
if (action_item->max_min_angle < 1 || action_item->max_min_angle > 50){ |
| 308 |
printf("*** Argument error: Expected a value between 1 and 50 for the \'MAX_MIN_ANGLE\' argument.\n\n"); |
| 309 |
exit(1); |
| 310 |
} |
| 311 |
|
| 312 |
// Check that we get another value for MIN_MAX_ANGLE |
| 313 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])){ |
| 314 |
printf("*** Argument error: Expected a value for the \'MIN_MAX_ANGLE\' argument.\n\n"); |
| 315 |
exit(1); |
| 316 |
} |
| 317 |
|
| 318 |
// Parse min max angle |
| 319 |
action_item->min_max_angle = atoi(argv[arg_item]); |
| 320 |
increase_arg_item(arg_item, argc); |
| 321 |
|
| 322 |
// Check value |
| 323 |
if (action_item->min_max_angle < 70 || action_item->min_max_angle > 179){ |
| 324 |
printf("*** Argument error: Expected a value between 70 and 179 for the \'MIN_MAX_ANGLE\' argument.\n\n"); |
| 325 |
exit(1); |
| 326 |
} |
| 327 |
|
| 328 |
continue; |
| 329 |
} |
| 330 |
|
| 331 |
// Parse --coarse-dense option |
| 332 |
// ------------------------------------------------------------------ |
| 333 |
if (strcmp (argv[arg_item], "--coarse-dense") == 0){ |
| 334 |
action_item->action = coarse_dense; |
| 335 |
increase_arg_item(arg_item, argc); |
| 336 |
|
| 337 |
// If no values passed |
| 338 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 339 |
continue; |
| 340 |
|
| 341 |
// Parse num_iter argument |
| 342 |
action_item->num_iter = atoi(argv[arg_item]); |
| 343 |
increase_arg_item(arg_item, argc); |
| 344 |
|
| 345 |
// Check value |
| 346 |
if (action_item->num_iter <= 0){ |
| 347 |
printf("*** Argument error: Expected a positive integer for the \'NUM_ITER\' argument.\n\n"); |
| 348 |
exit(1); |
| 349 |
} |
| 350 |
|
| 351 |
// Check if we are finnished parsing coarsning parameters |
| 352 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 353 |
continue; |
| 354 |
|
| 355 |
// Parse dense_rate |
| 356 |
action_item->dense_rate = atof(argv[arg_item]); |
| 357 |
increase_arg_item(arg_item, argc); |
| 358 |
|
| 359 |
// Check value |
| 360 |
if (action_item->dense_rate < 0){ |
| 361 |
printf("*** Argument error: Expected a positive value for the \'RATE\' argument.\n\n"); |
| 362 |
exit(1); |
| 363 |
} |
| 364 |
|
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
// Parse --coarse-flat option |
| 369 |
// ------------------------------------------------------------------ |
| 370 |
if (strcmp (argv[arg_item], "--coarse-flat") == 0){ |
| 371 |
action_item->action = coarse_flat; |
| 372 |
increase_arg_item(arg_item, argc); |
| 373 |
|
| 374 |
// If no values passed |
| 375 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 376 |
continue; |
| 377 |
|
| 378 |
// Parse flat_rate |
| 379 |
action_item->flat_rate = atof(argv[arg_item]); |
| 380 |
increase_arg_item(arg_item, argc); |
| 381 |
|
| 382 |
// Check value |
| 383 |
if (action_item->flat_rate < 0){ |
| 384 |
printf("*** Argument error: Expected a positive value for the \'RATE\' argument.\n\n"); |
| 385 |
exit(1); |
| 386 |
} |
| 387 |
|
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
// Parse --stats option |
| 392 |
// ------------------------------------------------------------------ |
| 393 |
if (strcmp (argv[arg_item], "--stats") == 0){ |
| 394 |
|
| 395 |
action_item->action = stats; |
| 396 |
increase_arg_item(arg_item, argc); |
| 397 |
|
| 398 |
// If no values passed |
| 399 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])) |
| 400 |
continue; |
| 401 |
|
| 402 |
// Parse max min angle |
| 403 |
action_item->max_min_angle = atoi(argv[arg_item]); |
| 404 |
increase_arg_item(arg_item, argc); |
| 405 |
|
| 406 |
// Check value |
| 407 |
if (action_item->max_min_angle < 1 || action_item->max_min_angle > 50){ |
| 408 |
printf("*** Argument error: Expected a value between 1 and 50 for the \'MAX_MIN_ANGLE\' argument.\n\n"); |
| 409 |
exit(1); |
| 410 |
} |
| 411 |
|
| 412 |
// Check that we get another value for MIN_MAX_ANGLE |
| 413 |
if (strncmp(argv[arg_item], "--", 2) == 0 || isalpha(argv[arg_item][0])){ |
| 414 |
printf("*** Argument error: Expected a value for the \'MIN_MAX_ANGLE\' argument.\n\n"); |
| 415 |
exit(1); |
| 416 |
} |
| 417 |
|
| 418 |
// Parse min max angle |
| 419 |
action_item->min_max_angle = atoi(argv[arg_item]); |
| 420 |
increase_arg_item(arg_item, argc); |
| 421 |
|
| 422 |
// Check value |
| 423 |
if (action_item->min_max_angle < 70 || action_item->min_max_angle > 179){ |
| 424 |
printf("*** Argument error: Expected a value between 70 and 179 for the \'MIN_MAX_ANGLE\' argument.\n\n"); |
| 425 |
exit(1); |
| 426 |
} |
| 427 |
|
| 428 |
continue; |
| 429 |
} |
| 430 |
|
| 431 |
printf("Unknown argument: \'%s\'\n", argv[arg_item]); |
| 432 |
exit(1); |
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
// Check for no action options |
| 437 |
if (action_item == NULL) { |
| 438 |
printf("No action option choosen. Nothing to be done.\n"); |
| 439 |
exit(1); |
| 440 |
} |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
/* |
| 445 |
* *************************************************************************** |
| 446 |
* Routine: ReadSurfMeshFile |
| 447 |
* |
| 448 |
* Author: Johan Hake ([email protected]), Zeyun Yu ([email protected]) |
| 449 |
* |
| 450 |
* Purpose: Read a surface mesh file stored in OFF format |
| 451 |
* *************************************************************************** |
| 452 |
*/ |
| 453 |
SurfaceMesh* ReadSurfMeshFile(char* input_name, bool read_suffix_count, |
| 454 |
char* basename, int& suffix_count) |
| 455 |
{ |
| 456 |
char filename[256]; |
| 457 |
|
| 458 |
char* basename_end_pointer; |
| 459 |
char* suffix_end_pointer; |
| 460 |
unsigned int suffix_index; |
| 461 |
unsigned int basename_index; |
| 462 |
int suffix_offset; |
| 463 |
SurfaceMesh* surfmesh; |
| 464 |
|
| 465 |
strcpy(filename, input_name); |
| 466 |
|
| 467 |
// Check if input is a .off file |
| 468 |
basename_end_pointer = strstr(filename, ".off"); |
| 469 |
if (basename_end_pointer == NULL){ |
| 470 |
printf("Provide a mesh in \'.off\' format.\n"); |
| 471 |
exit(1); |
| 472 |
} |
| 473 |
|
| 474 |
// Get the basename |
| 475 |
basename_index = basename_end_pointer-filename; |
| 476 |
strncpy (basename, filename, basename_index); |
| 477 |
basename[basename_index] = '\0'; |
| 478 |
|
| 479 |
// Check if input has _suffix_XX in its name |
| 480 |
if (read_suffix_count){ |
| 481 |
suffix_end_pointer = strstr(filename, OUT_SUFFIX); |
| 482 |
if (suffix_end_pointer != NULL){ |
| 483 |
// If so update basename and get suffix_count |
| 484 |
suffix_index = suffix_end_pointer-filename; |
| 485 |
strncpy (basename, filename, suffix_index); |
| 486 |
basename[suffix_index] = '\0'; |
| 487 |
suffix_offset = (basename_index - (suffix_index + OUT_SUFFIX_LENGTH)); |
| 488 |
|
| 489 |
if (suffix_offset == 1 || suffix_offset == 2) |
| 490 |
sscanf(basename_end_pointer-suffix_offset, "%d", &suffix_count); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
// Load surface meshes in OFF format |
| 495 |
return SurfaceMesh_readOFF(input_name); |
| 496 |
} |
| 497 |
|
| 498 |
/* |
| 499 |
* *************************************************************************** |
| 500 |
* Routine: ImproveSurfaceMesh |
| 501 |
* |
| 502 |
* Author: Johan Hake ([email protected]), Zeyun Yu ([email protected]) |
| 503 |
* |
| 504 |
* Purpose: Improve a surface mesh |
| 505 |
* *************************************************************************** |
| 506 |
*/ |
| 507 |
int ImproveSurfaceMesh(char* input_name, char* input_site, ActionItem* action_list) |
| 508 |
{ |
| 509 |
FILE *fout; |
| 510 |
time_t t0, t1, t2; |
| 511 |
SurfaceMesh* surfmesh = NULL; |
| 512 |
char filename[256], basename[256]; |
| 513 |
unsigned int num_spheres = 0; |
| 514 |
ATOM *sphere_list = NULL; |
| 515 |
unsigned int* sphere_markers = NULL; |
| 516 |
//float* area_constraint_list = NULL; // Not used |
| 517 |
int suffix_count = -1; |
| 518 |
ActionItem* action = action_list; |
| 519 |
float acos_max_max_normal_angle; |
| 520 |
unsigned int i, j; |
| 521 |
|
| 522 |
(void)time(&t0); |
| 523 |
|
| 524 |
// Read any active sites from file |
| 525 |
if (input_site != NULL) |
| 526 |
ReadActiveSiteFile(input_site, num_spheres, |
| 527 |
// sphere_list, sphere_markers, area_constraint_list); |
| 528 |
sphere_list, sphere_markers); |
| 529 |
// Read surface mesh file together with basename and suffix count |
| 530 |
surfmesh = ReadSurfMeshFile(input_name, true, basename, suffix_count); |
| 531 |
|
| 532 |
// Create neighborhood information |
| 533 |
SurfaceMesh_createNeighborlist(surfmesh); |
| 534 |
|
| 535 |
// Main action loop |
| 536 |
while (action != NULL){ |
| 537 |
|
| 538 |
switch(action->action){ |
| 539 |
|
| 540 |
// Refine mesh |
| 541 |
// ------------------------------------------------------------------ |
| 542 |
case refine: { |
| 543 |
|
| 544 |
(void)time(&t1); |
| 545 |
|
| 546 |
printf("\n\n"); |
| 547 |
printf("Refine mesh:\n"); |
| 548 |
|
| 549 |
SurfaceMesh_refine(surfmesh); |
| 550 |
|
| 551 |
printf("After refinement: \n" |
| 552 |
" vertices: %d --- simplices: %d\n", surfmesh->nv, surfmesh->nf); |
| 553 |
|
| 554 |
(void)time(&t2); |
| 555 |
printf("------------------------------------------\n"); |
| 556 |
printf("Time to refine surface mesh: %4.1f seconds. \n", fabs(t2-t1)); |
| 557 |
|
| 558 |
// Set changed flag |
| 559 |
mesh_changed = true; |
| 560 |
|
| 561 |
break; |
| 562 |
} |
| 563 |
|
| 564 |
// Smooth mesh |
| 565 |
// ------------------------------------------------------------------ |
| 566 |
case smooth: { |
| 567 |
(void)time(&t1); |
| 568 |
printf("\n\n"); |
| 569 |
printf("Smooth mesh:\n"); |
| 570 |
SurfaceMesh_smooth(surfmesh, action->max_min_angle, |
| 571 |
action->min_max_angle, action->max_iter, |
| 572 |
action->flip_edges == 1); |
| 573 |
(void)time(&t2); |
| 574 |
printf("------------------------------------------\n"); |
| 575 |
printf("Time to smooth surface mesh: %4.1f seconds. \n", fabs(t2-t1)); |
| 576 |
|
| 577 |
// Set changed flag |
| 578 |
mesh_changed = true; |
| 579 |
|
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
// Normal smooth mesh |
| 584 |
// ------------------------------------------------------------------ |
| 585 |
case normal_smooth: { |
| 586 |
(void)time(&t1); |
| 587 |
printf("\n\n"); |
| 588 |
printf("Normal smooth mesh:\n"); |
| 589 |
SurfaceMesh_normalSmooth(surfmesh); |
| 590 |
(void)time(&t2); |
| 591 |
printf("-------------------------------------------------\n"); |
| 592 |
printf("Time to normal smooth surface mesh: %4.1f seconds.\n", fabs(t2-t1)); |
| 593 |
|
| 594 |
// Set changed flag |
| 595 |
mesh_changed = true; |
| 596 |
|
| 597 |
break; |
| 598 |
} |
| 599 |
|
| 600 |
// Correct normals of surface mesh |
| 601 |
// ------------------------------------------------------------------ |
| 602 |
case correct_normals: { |
| 603 |
(void)time(&t1); |
| 604 |
printf("\n\n"); |
| 605 |
printf("Correct normals:\n"); |
| 606 |
SurfaceMesh_correctNormals(surfmesh); |
| 607 |
(void)time(&t2); |
| 608 |
printf("---------------------------------------------------\n"); |
| 609 |
printf("Time to correct surface mesh normals: %4.1f seconds.\n", fabs(t2-t1)); |
| 610 |
|
| 611 |
// Set changed flag |
| 612 |
mesh_changed = true; |
| 613 |
|
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
// Coarse mesh |
| 618 |
// ------------------------------------------------------------------ |
| 619 |
case coarse_dense: { |
| 620 |
(void)time(&t1); |
| 621 |
|
| 622 |
printf("\n\n"); |
| 623 |
printf("Coarse dense part of mesh:\n"); |
| 624 |
|
| 625 |
// Do the actuall coarsening |
| 626 |
for (i=0; i<action->num_iter; i++){ |
| 627 |
|
| 628 |
// Assign active sites |
| 629 |
SurfaceMesh_assignActiveSites(surfmesh, sphere_list, num_spheres, |
| 630 |
sphere_markers); |
| 631 |
|
| 632 |
// Do the actuall coarsening |
| 633 |
SurfaceMesh_coarse(surfmesh, action->dense_rate, 0, 10, -1); |
| 634 |
if (action->num_iter==1) |
| 635 |
printf("After coarsening: \n vertices: %d --- simplices: %d\n", surfmesh->nv, surfmesh->nf); |
| 636 |
else |
| 637 |
printf("%d After coarsening: \n vertices: %d --- simplices: %d\n", i, surfmesh->nv, surfmesh->nf); |
| 638 |
} |
| 639 |
|
| 640 |
(void)time(&t2); |
| 641 |
printf("------------------------------------------\n"); |
| 642 |
printf("Time to coarse surface mesh: %4.1f seconds.\n", fabs(t2-t1)); |
| 643 |
|
| 644 |
// Set changed flag |
| 645 |
mesh_changed = true; |
| 646 |
|
| 647 |
break; |
| 648 |
} |
| 649 |
|
| 650 |
// Coarse mesh |
| 651 |
// ------------------------------------------------------------------ |
| 652 |
case coarse_flat: { |
| 653 |
(void)time(&t1); |
| 654 |
|
| 655 |
printf("\n\n"); |
| 656 |
printf("Coarse flat part of mesh:\n"); |
| 657 |
|
| 658 |
// Assign active sites |
| 659 |
SurfaceMesh_assignActiveSites(surfmesh, sphere_list, num_spheres, |
| 660 |
sphere_markers); |
| 661 |
|
| 662 |
// Do the actuall coarsening |
| 663 |
SurfaceMesh_coarse(surfmesh, action->flat_rate, 1, 0, -1); |
| 664 |
|
| 665 |
printf("After coarsening: \n vertices: %d --- simplices: %d\n", surfmesh->nv, surfmesh->nf); |
| 666 |
|
| 667 |
(void)time(&t2); |
| 668 |
printf("------------------------------------------\n"); |
| 669 |
printf("Time to coarse surface mesh: %4.1f seconds.\n", fabs(t2-t1)); |
| 670 |
|
| 671 |
// Set changed flag |
| 672 |
mesh_changed = true; |
| 673 |
|
| 674 |
break; |
| 675 |
} |
| 676 |
|
| 677 |
// Get stats |
| 678 |
// ------------------------------------------------------------------ |
| 679 |
case stats: { |
| 680 |
float min_angle, max_angle; |
| 681 |
int num_small, num_large; |
| 682 |
|
| 683 |
printf("\n\n"); |
| 684 |
printf("Surface mesh statistics:\n"); |
| 685 |
SurfaceMesh_getMinMaxAngles(surfmesh ,&min_angle, &max_angle, &num_small, |
| 686 |
&num_large, action->max_min_angle, |
| 687 |
action->min_max_angle); |
| 688 |
|
| 689 |
printf(" min_angle: %f - max_angle: %f - " |
| 690 |
"smaller-than-%d: %d - larger-than-%d: %d\n", |
| 691 |
min_angle, max_angle, action->max_min_angle, num_small, |
| 692 |
action->min_max_angle, num_large); |
| 693 |
|
| 694 |
break; |
| 695 |
} |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
// The next action |
| 700 |
action = action->next; |
| 701 |
|
| 702 |
} |
| 703 |
|
| 704 |
// Ouput mesh if it has changed |
| 705 |
if (mesh_changed) |
| 706 |
{ |
| 707 |
|
| 708 |
// Output surface mesh |
| 709 |
sprintf(filename, "%s%s%d.off", basename, OUT_SUFFIX, ++suffix_count); |
| 710 |
|
| 711 |
printf("\n\n"); |
| 712 |
printf("Writing results to: %s%s%d.off\n", basename, OUT_SUFFIX, suffix_count); |
| 713 |
|
| 714 |
// Write surface mesh to file |
| 715 |
SurfaceMesh_writeOFF(surfmesh, filename); |
| 716 |
|
| 717 |
(void)time(&t2); |
| 718 |
|
| 719 |
printf("\n"); |
| 720 |
printf("----------------------------------------\n"); |
| 721 |
printf("Total time improving mesh: %4.1f seconds. \n\n", fabs(t2-t0)); |
| 722 |
} |
| 723 |
|
| 724 |
// Release memory |
| 725 |
SurfaceMesh_dtor(surfmesh); |
| 726 |
|
| 727 |
// If any active sites have been read |
| 728 |
if (num_spheres){ |
| 729 |
free(sphere_list); |
| 730 |
free(sphere_markers); |
| 731 |
} |
| 732 |
|
| 733 |
return(0); |
| 734 |
} |
| 735 |
|
| 736 |
// The main function |
| 737 |
// ------------------------------------------------------------------ |
| 738 |
int main(int argc, char *argv[]) |
| 739 |
{ |
| 740 |
char* input_sites = NULL; |
| 741 |
char* input_filename = NULL; |
| 742 |
ActionItem* action_list; |
| 743 |
|
| 744 |
// Parse command line |
| 745 |
parse_comand_line(argc, argv, input_filename, input_sites, action_list); |
| 746 |
|
| 747 |
// Call ImproveSurfaceMesh routine |
| 748 |
ImproveSurfaceMesh(input_filename, input_sites, action_list); |
| 749 |
|
| 750 |
// Clean action list |
| 751 |
delete_ActionItem(action_list); |
| 752 |
|
| 753 |
return(0); |
| 754 |
} |