/* Reads a model from file 'fname' and returns the model read. If an error * occurs a message is printed and the program exists. */ static struct model *read_model_file(const char *fname) { int rcode; struct model *m; const char *errstr; rcode = read_fmodel(&m,fname,MESH_FF_AUTO,1); if (rcode <= 0) { switch (rcode) { case 0: errstr = "no models in file"; break; case MESH_NO_MEM: errstr = "no memory"; break; case MESH_CORRUPTED: errstr = "corrupted file or I/O error"; break; case MESH_MODEL_ERR: errstr = "model error"; break; case MESH_NOT_TRIAG: errstr = "not a triangular mesh model"; break; case MESH_BAD_FF: errstr = "unrecognized file format"; break; case MESH_BAD_FNAME: errstr = strerror(errno); break; default: errstr = "unknown error"; } fprintf(stderr,"ERROR: %s: %s\n",fname,errstr); exit(1); } else if (m->num_faces == 0) { fprintf(stderr,"ERROR: %s: empty model (no faces)\n",fname); exit(1); } return m; }
int main(int argc, char **argv) { char *infile, *outfile; struct model *or_model, *sub_model=NULL; int lev, nlev=1, rcode, nonopt_argc=1; int sub_method=-1, use_binary=0; struct subdiv_methods sm={BUTTERFLY_SUBDIV_FUNCTIONS, LOOP_SUBDIV_FUNCTIONS, SPHERICAL_OR_SUBDIV_FUNCTIONS, SPHERICAL_ALT_SUBDIV_FUNCTIONS, KOBBELTSQRT3_SUBDIV_FUNCTIONS}; struct subdiv_functions *tmp_func=NULL; if (argc < 4 || argc > 6) { fprintf(stderr, "Usage: subdiv [-sph_or, -sph_alt, -but, -loop, -ksqrt3][-bin]" " infile outfile n_lev\n"); exit(1); } for (nonopt_argc=1; nonopt_argc<=2; nonopt_argc++) { #ifdef DEBUG printf("argv[%d] = %s\n", nonopt_argc, argv[nonopt_argc]); #endif if (strcmp(argv[nonopt_argc], "-sph_or") == 0) { tmp_func = &(sm.spherical_or); sub_method = SUBDIV_SPH_OR; } else if (strcmp(argv[nonopt_argc], "-sph_alt") == 0) { tmp_func = &(sm.spherical_alt); sub_method = SUBDIV_SPH_ALT; } else if (strcmp(argv[nonopt_argc], "-but") == 0) { tmp_func = &(sm.butterfly); sub_method = SUBDIV_BUTTERFLY; } else if (strcmp(argv[nonopt_argc], "-loop") == 0) { tmp_func = &(sm.loop); sub_method = SUBDIV_LOOP; } else if (strcmp(argv[nonopt_argc], "-ksqrt3") == 0) sub_method = SUBDIV_KOB_SQRT3; else if (strcmp(argv[nonopt_argc], "-bin") == 0) use_binary = 1; else break; } if (sub_method == -1) { fprintf(stderr, "Invalid subdivision method\n"); fprintf(stderr, "Usage: subdiv [-sph_or, -sph_alt, -but, -loop, -ksqrt3][-bin]" " infile outfile n_lev\n"); exit(1); } infile = argv[nonopt_argc]; outfile = argv[++nonopt_argc]; if (argc == nonopt_argc+2) nlev = atoi(argv[++nonopt_argc]); if (nlev < 1) nlev = 1; rcode = read_fmodel(&or_model, infile, MESH_FF_AUTO, 0); if (rcode < 0) { fprintf(stderr, "Unable to read model - error code %d\n", rcode); exit(-1); } for (lev=0; lev<nlev; lev++) { /* performs the subdivision */ switch (sub_method) { case SUBDIV_KOB_SQRT3: /* handle sqrt3 stuff separately */ sub_model = subdiv_sqrt3(or_model, &(sm.kob_sqrt3)); break; default: /* 4-to-1 split */ sub_model = subdiv(or_model, tmp_func); break; } __free_raw_model(or_model); or_model = sub_model; } write_raw_model(sub_model, outfile, use_binary); __free_raw_model(sub_model); return 0; }
int main(int argc, char **argv) { char *in_fname, *out_fname; struct model *raw_model; vertex_t *new_vert, tmp; struct ring_info *ring; int i, j, rcode, n_lev=1, lev; int use_binary=0; if (argc < 3 || argc > 5) { fprintf(stderr, "Usage: lapl [-bin] infile outfile [n_lev]\n"); exit(-1); } if (strcmp(argv[1],"-bin") == 0) { use_binary=1; in_fname = argv[2]; out_fname = argv[3]; if (argc == 5) { i = atoi(argv[4]); n_lev = (i < 1)?1:i; } } else { in_fname = argv[1]; out_fname = argv[2]; if (argc == 4) { i = atoi(argv[3]); n_lev = (i < 1)?1:i; } } rcode = read_fmodel(&raw_model, in_fname, MESH_FF_AUTO, 0); if (rcode < 0) { fprintf(stderr, "Unable to read model, error code = %d\n", rcode); exit(rcode); } ring = (struct ring_info*)malloc(raw_model->num_vert*sizeof(struct ring_info)); new_vert = (vertex_t*)malloc(raw_model->num_vert*sizeof(vertex_t)); for (lev=0; lev<n_lev; lev++) { build_star_global(raw_model, ring); for (i=0; i<raw_model->num_vert; i++) { tmp.x = tmp.y = tmp.z = 0.0; for (j=0; j<ring[i].size; j++) __add_v(raw_model->vertices[ring[i].ord_vert[j]], tmp, tmp); __prod_v(1.0/(float)ring[i].size, tmp, tmp); __add_v(raw_model->vertices[i], tmp, new_vert[i]); } memcpy(raw_model->vertices, new_vert, raw_model->num_vert*sizeof(vertex_t)); for (j=0; j<raw_model->num_vert; j++) { free(ring[j].ord_vert); free(ring[j].ord_face); } } write_raw_model(raw_model, out_fname, use_binary); free(new_vert); free(ring); __free_raw_model(raw_model); return 0; }