123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <stdarg.h>
- #include <math.h>
- #include "FEVtypes.h"
- #include "FEV.h"
- #define ARRSIZE 50
- vertex_t v_array[ARRSIZE];
- pr_edge_t e_array[ARRSIZE];
- pr_face_t f_array[ARRSIZE];
- int verts = 0;
- int edges = 0;
- int faces = 0;
- pr_face_t get_f_arr(){
- return f_array[0];
- }
- pr_edge_t get_e_arr(){
- return e_array[0];
- }
- vertex_t get_v_arr(){
- return v_array[0];
- }
- int reset(char which){
- int i;
- vertex_t v;
- pr_edge_t e;
- pr_face_t f;
- switch(which){
- case('v'):
- for(i=0; i<verts; i++){
- if(v_array[i] != NULL){
- v = v_array[i];
- vert_fr(v);
- }
- }
- break;
- case('e'):
- for(i=0; i<edges; i++){
- if(e_array[i] != NULL){
- e = e_array[i];
- pr_edge_fr(e, 0);
- }
- }
- break;
- case('f'):
- for(i=0; i<faces; i++){
- if(f_array[i] != NULL){
- f = f_array[i];
- pr_face_fr(f, 0, 0);
- }
- }
- break;
- default:
- reset('v');
- reset('e');
- reset('f');
- break;
- }
- return OK;
- }
- /* New-struct functions for pointer-based vertices, edges, faces */
- vertex_t new_vert(double x, double y, double z)
- {
- vertex_t newvert;
- newvert = (vertex_t) malloc( sizeof(struct vertex));
- newvert->fev_type = VERTEX;
- newvert->id = verts++;
- newvert->vertex_coords = mat_new(3,1);
- e(newvert->vertex_coords,0,0) = x;
- e(newvert->vertex_coords,1,0) = y;
- e(newvert->vertex_coords,2,0) = z;
- v_array[newvert->id] = newvert;
- return(newvert);
- }
- edge_t new_edge(vertex_t efrom, vertex_t eto)
- {
- edge_t newedge;
- newedge = (edge_t) malloc( sizeof(struct edge));
- newedge->fev_type = EDGE;
- newedge->id = edges++;
- newedge->from_vertex = efrom;
- newedge -> next_edge = NULL; /* for face circular edge list*/
- newedge->to_vertex = eto;
- newedge->direction = mat_add(efrom->vertex_coords, eto->vertex_coords, -1);
- return(newedge);
- }
- face_t new_face (edge_t edges[], int n) /* a little klunky, could use list*/
- {
- face_t newface;
- int i;// n;
- //n = sizeof(edges)/sizeof(edges[0]);
- newface = (face_t) malloc( sizeof(struct face));
- newface->fev_type = FACE;
- newface->id = faces++;
- newface->norm_vec = mat_new(1,3); /*col vector, notice!*/
- newface->next_edge = edges[0]; /* we're copying a pointer */
- /* printf("\n set face nextedge to %s", edges[0]->name);*/
- for (i =0; i<(n-1); i++)
- { edges[i]->next_edge = edges[i+1];
- /* printf("\n set edge %d nextedge to %s", i, edges[i+1]->name);*/
- }
- edges[n-1]->next_edge = edges[0];
- newface->next_face = NULL; /*filled in at higher level*/
- return(newface);
- }
- /* Freeing functions for pointer-based vertices, edges, faces */
- int vert_fr(vertex_t v){
- if(v == NULL) return OK;
- mat_free(v->vertex_coords);
- v_array[v->id] = NULL;
- free(v);
- return OK;
- }
- int edge_fr(edge_t e, int all){
- if(e == NULL) return OK;
- vertex_t v;
- mat_free(e->direction);
- v = e->from_vertex;
- if(v != NULL) vert_fr(v);
- v = e->to_vertex;
- if(v != NULL) vert_fr(v);
- if(all==1 && e->next_edge != NULL) edge_fr(e->next_edge, 1);
- free((char*) e);
- return OK;
- }
- int face_fr(face_t f, int all){
- if(f == NULL) return OK;
- mat_free(f->norm_vec);
- if(f->next_edge != NULL)
- edge_fr(f->next_edge, all);
- if(all==1 && f->next_face != NULL) face_fr(f->next_face, 1);
- free((char*) f);
- return OK;
- }
- /* Verbose print functions for pointer-based vertices, edges, faces */
- int vert_pr(char *msg, vertex_t v, FILE *fout) {
- if (msg == NULL) { cberror("null message arg in vert_pr"); return NILARG1;}
- if (v == NULL) { cberror("null vertex arg in vert_pr"); return NILARG2;}
- if(fout == NULL){
- printf("VERTEX: %s Type: %d , ID: %d\n", msg, v->fev_type, v->id);
- mat_pr("Vertex Coords", v->vertex_coords, NULL);
- printf("~~\n");
- return OK;
- } else {
- fprintf(fout, "VERTEX: %s Type: %d , ID: %d\n", msg, v->fev_type, v->id);
- mat_pr("Vertex Coords", v->vertex_coords, fout);
- fprintf(fout, "~~\n");
- return OK;
- }
- }
- int edge_pr(char *msg, edge_t edg) {
- if (msg == NULL) { cberror("null message arg in edge_pr"); return NILARG1;}
- if (edg == NULL) { cberror("null edge arg in edge_pr"); return NILARG2;}
- printf("EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
- mat_pr("Edge Direction", edg->direction, NULL);
- vert_pr("From Vertex ", edg->from_vertex, NULL);
- vert_pr("To Vertex ", edg->to_vertex, NULL);
- if (edg->next_edge == NULL) printf("Empty Next Edge.\n");
- else printf("Next Edge Exists.\n"); /* don't go recursive yet! */
- printf("~~~\n");
- return OK;
- }
- int face_pr(char *msg, face_t f) {
- if (msg == NULL) { cberror("null message arg in face_pr"); return NILARG1;}
- if (f == NULL) { cberror("null face arg in face_pr"); return NILARG2;}
- printf("FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
- mat_pr("Normal Direction ", f->norm_vec, NULL);
- if (f->next_edge == NULL) printf("Empty Next Edge.\n");
- else printf("Next Edge Exists.\n"); /* don't go recursive yet! */
- if(f->next_face == NULL) printf("Empty Next Face. \n");
- else printf("Next Face Exists. \n");
- printf("~~~~\n");
- return OK;
- }
- /* Short print functions for pointer-based vertices, edges, faces, polygon */
- int short_vert_pr (vertex_t v) {
- printf("\n %d", v->id);
- return OK;
- }
- int short_edge_pr (edge_t edg) {
- printf("\n %d", edg->id);
- short_vert_pr (edg->from_vertex);
- short_vert_pr (edg->to_vertex);
- return OK;
- }
- int short_face_pr (face_t f) {
- edge_t cur_edge;
- printf("\n %d", f->id);
- cur_edge = f->next_edge;
- do {
- short_edge_pr(cur_edge);
- cur_edge = cur_edge->next_edge;
- }
- while (cur_edge != f->next_edge);
- return OK;
- }
- int short_poly_pr(face_t f) {
- face_t cur_face;
- cur_face = f;
- do
- {
- short_face_pr(cur_face);
- cur_face = cur_face->next_face;
- }
- while (cur_face != f);
- return OK;
- }
- /************************PRINTABLE********************/
- /*** Vertices only come in one variety ***/
- /* New-struct functions for printable edges, faces */
- /*
- * fully-recursing function to make printable structs out of
- * non-printable edges
- */
- pr_edge_t new_print_edge(edge_t e){
- pr_edge_t pr_e;
- pr_e = (pr_edge_t) malloc(sizeof(struct printable_e));
- pr_e->direction = e->direction;
- pr_e->fev_type = e->fev_type;
- pr_e->from_v = e->from_vertex->id;
- // if(v_array[pr_e->from_v] == NULL)
- // v_array[pr_e->from_v] = e->from_vertex;
- pr_e->to_v = e->to_vertex->id;
- // if(v_array[pr_e->to_v] == NULL)
- // v_array[pr_e->to_v] = e->to_vertex;
- pr_e->id = e->id;
- e_array[pr_e->id] = pr_e;
- if(e->next_edge == NULL){
- pr_e->next_edge = -1;
- } else {
- pr_e->next_edge = e->next_edge->id;
- if(e_array[pr_e->next_edge] == NULL){
- new_print_edge(e->next_edge);
- }
- }
- return pr_e;
- }
- /* fully-recursing function to make printable structs out of
- * non-printable faces
- */
- pr_face_t new_print_face(face_t f){
- pr_face_t pr_f;
- pr_f = (pr_face_t) malloc(sizeof(struct printable_f));
- pr_f->fev_type = f->fev_type;
- pr_f->id = f->id;
- f_array[pr_f->id] = pr_f;
- pr_f->norm_vec = f->norm_vec;
- if(f->next_edge == NULL){
- pr_f->next_edge = -1;
- } else {
- pr_f->next_edge = f->next_edge->id;
- if(e_array[f->next_edge->id] == NULL){
- new_print_edge(f->next_edge);
- }
- }
- if(f->next_face == NULL){
- pr_f->next_face = -1;
- } else {
- pr_f->next_face = f->next_face->id;
- if(f_array[f->next_face->id] == NULL){
- new_print_face(f->next_face);
- }
- }
- return pr_f;
- }
- /* Freeing functions for printable edges, faces */
- int pr_edge_fr(pr_edge_t e, int fr_vert){
- if(e == NULL) return OK;
- mat_free(e->direction);
- /* Since I'm going to use this iteratively, it doesn't recurse */
- if(fr_vert == 1) {
- if(v_array[e->from_v] != NULL) vert_fr(v_array[e->from_v]);
- if(v_array[e->to_v] != NULL) vert_fr(v_array[e->to_v]);
- }
- e_array[e->id] = NULL;
- free((char*) e);
- return OK;
- }
- int pr_face_fr(pr_face_t f, int fr_edge, int fr_vert){
- if(f == NULL) return OK;
- mat_free(f->norm_vec);
- if(fr_edge == 1) {
- if(e_array[f->next_edge] != NULL)
- pr_edge_fr(e_array[f->next_edge], fr_vert);
- }
- f_array[f->id] = NULL;
- free((char*) f);
- return OK;
- }
- /* Verbose print functions for printable edges, faces
- * - prints to a file optionally
- * - NOT recursive - must be used iteratively
- */
- int pr_edge_pr(char *msg, pr_edge_t edg, FILE *fout) {
- if (msg == NULL) { cberror("null message arg in edge_pr"); return NILARG1;}
- if (edg == NULL) { cberror("null edge arg in edge_pr"); return NILARG2;}
- if(fout == NULL){
- printf("EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
- mat_pr("Edge Direction", edg->direction, NULL);
- vert_pr("From Vertex ", v_array[edg->from_v], NULL);
- vert_pr("To Vertex ", v_array[edg->to_v], NULL);
- printf("Next Edge: %d. \n", edg->next_edge);
- printf("~~~\n");
- return OK;
- } else {
- fprintf(fout, "EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
- mat_pr("Edge Direction", edg->direction, fout);
- vert_pr("From Vertex ", v_array[edg->from_v], fout);
- vert_pr("To Vertex ", v_array[edg->to_v], fout);
- fprintf(fout, "Next Edge: %d. \n", edg->next_edge);
- fprintf(fout, "~~~\n");
- return OK;
- }
- }
- int pr_face_pr(char *msg, pr_face_t f, FILE *fout) {
- if (msg == NULL) { cberror("null message arg in face_pr"); return NILARG1;}
- if (f == NULL) { cberror("null face arg in face_pr"); return NILARG2;}
- if(fout == NULL){
- printf("FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
- mat_pr("Normal Direction ", f->norm_vec, NULL);
- printf("Next Edge: %d. \n", f->next_edge);
- printf("Next Face: %d. \n", f->next_face);
- printf("~~~~\n");
- return OK;
- } else {
- fprintf(fout, "FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
- mat_pr("Normal Direction ", f->norm_vec, fout);
- fprintf(fout, "Next Edge: %d. \n", f->next_edge);
- fprintf(fout, "Next Face: %d. \n", f->next_face);
- fprintf(fout, "~~~~\n");
- return OK;
- }
- }
- /* Short print functions for printable edges, faces */
- int short_pr_edge_pr (pr_edge_t edg) {
- printf("\n %d", edg->id);
- short_vert_pr (v_array[edg->from_v]);
- short_vert_pr (v_array[edg->to_v]);
- return OK;
- }
- int short_pr_face_pr (pr_face_t f) {
- int cur_edge;
- printf("\n %d", f->id);
- cur_edge = f->next_edge;
- do {
- short_pr_edge_pr(e_array[cur_edge]);
- cur_edge = e_array[cur_edge]->next_edge;
- }
- while (cur_edge != f->next_edge);
- return OK;
- }
- vertex_t vert_read(FILE *fin){
- int t, id, assgn, i, j, r, c;
- float n;
- vertex_t v;
- assgn = fscanf(fin, "VERTEX: %*s Type: %d , ID: %d\n", &t, &id);
- if(assgn != 2){
- cberror("Nope, can't do that either");
- return NULL;
- }
- v = (vertex_t) malloc(sizeof(struct vertex));
- v->fev_type = t;
- v->id = id;
- fscanf(fin, "MATRIX: %*s Type: %*d , Dims %d, %d\n", &r, &c);
- v->vertex_coords = mat_new(r, c);
- for (i = 0; i < r; i++) {
- for (j=0; j<c; j++) {
- assgn = fscanf(fin, "%f ", &n);
- if(assgn != 1) {
- cberror("Formatting error (matrix, vertex)");
- return NULL;
- }
- e(v->vertex_coords, i, j) = n;
- }
- fscanf(fin, "\n"); //end of each row
- }
- fscanf(fin, "~\n");
- fscanf(fin, "~~\n");
- v_array[id] = v;
- return v;
- }
- /* Print-a-polyhedron function - takes a pointeriffic polyhedron and
- * makes it all human-readable and not pointery */
- int printificate(face_t head, char* fname){
- FILE *fout;
- int i;
- new_print_face(head);
- fout = fopen(fname, "w+");
- fprintf(fout, "VERTICES: %d \nEDGES: %d \nFACES: %d \n", verts, edges, faces);
- for(i = 0; i < faces; i++){
- pr_face_pr(" ", f_array[i], fout);
- }
- for(i = 0; i < edges; i++){
- pr_edge_pr(" ", e_array[i], fout);
- }
- fclose(fout);
- reset('a'); /* empty my arrays */
- return OK;
- }
- /*
- * Read in a file full of faces, edges, and pointers and turn it into a
- * pointeriffic data structure
- */
- /* returns the head face */
- face_t readificate(char* fname){
- int v, e, f, assgn, i, j, t, id, next, r, c;
- float n;
- int nexte[ARRSIZE];
- int nextfe[ARRSIZE];
- int nextff[ARRSIZE];
- edge_t edge;
- face_t face;
- edge_t edgen[ARRSIZE];
- face_t facen[ARRSIZE];
- FILE *fin;
- char* test;
- fin = fopen(fname, "r");
- assgn = fscanf(fin, "VERTICES: %d \nEDGES: %d \nFACES: %d \n", &v, &e, &f);
- if(assgn != 3) {
- cberror("Incorrectly formatted input file");
- return (face_t) NULL;
- }
- reset('a'); /* Going to put new data into the arrays */
- for(i = 0; i < f; i++){
- fscanf(fin, "FACE: %*s Type: %d, ID: %d \n", &t, &id);
- face = (face_t) malloc(sizeof(struct face));
- face->fev_type = t;
- face->id = id;
- fscanf(fin, "MATRIX: %s Type: %*d , Dims %d, %d\n", test, &r, &c);
- face->norm_vec = mat_new(r, c);
- for (i = 0; i < r; i++) {
- for (j=0; j<c; j++) {
- assgn = fscanf(fin, "%f ", &n);
- if(assgn != 1) {
- cberror("Formatting error (matrix, face)");
- return NULL;
- }
- e(face->norm_vec, i, j) = n;
- }
- fscanf(fin, "\n"); //end of each row
- }
- fscanf(fin, "~\n");
- fscanf(fin, "Next Edge: %d. \n", &nextfe[id]);
- fscanf(fin, "Next Face: %d. \n", &nextff[id]);
- fscanf(fin, "~~~~\n");
- facen[id] = face;
- }
- for(i = 0; i < e; i++){
- fscanf(fin, "EDGE: %*s Type: %d, ID: %d\n", &t, &id);
- edge = (edge_t) malloc(sizeof(struct edge));
- fscanf(fin, "MATRIX: %*s Type: %*d , Dims %d, %d\n", &r, &c);
- edge->direction = mat_new(r, c);
- for (i = 0; i < r; i++) {
- for (j=0; j<c; j++) {
- assgn = fscanf(fin, "%f ", &n);
- if(assgn != 1) {
- cberror("Formatting error (matrix, edge)");
- return NULL;
- }
- e(edge->direction, i, j) = n;
- }
- fscanf(fin, "\n"); //end of each row
- }
- fscanf(fin, "~\n");
- edge->from_vertex = vert_read(fin);
- edge->to_vertex = vert_read(fin);
- edge->fev_type = t;
- edge->id = id;
- fscanf(fin, "Next Edge: %d. \n", &nexte[id]);
- fscanf(fin, "~~~\n");
- edgen[id] = edge;
- }
- for(i = 0; i < e; i++){
- next = nexte[i];
- if(next != -1)
- edgen[i]->next_edge = edgen[next];
- }
- for(i = 0; i < f; i++){
- next = nextfe[i];
- if(next != -1)
- facen[i]->next_edge = edgen[next];
- next = nextff[i];
- if(next != -1)
- facen[i]->next_face = facen[next];
- }
- return facen[0];
- }
|