FEV.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <stdarg.h>
  5. #include <math.h>
  6. #include "FEVtypes.h"
  7. #include "FEV.h"
  8. #define ARRSIZE 50
  9. vertex_t v_array[ARRSIZE];
  10. pr_edge_t e_array[ARRSIZE];
  11. pr_face_t f_array[ARRSIZE];
  12. int verts = 0;
  13. int edges = 0;
  14. int faces = 0;
  15. pr_face_t get_f_arr(){
  16. return f_array[0];
  17. }
  18. pr_edge_t get_e_arr(){
  19. return e_array[0];
  20. }
  21. vertex_t get_v_arr(){
  22. return v_array[0];
  23. }
  24. int reset(char which){
  25. int i;
  26. vertex_t v;
  27. pr_edge_t e;
  28. pr_face_t f;
  29. switch(which){
  30. case('v'):
  31. for(i=0; i<verts; i++){
  32. if(v_array[i] != NULL){
  33. v = v_array[i];
  34. vert_fr(v);
  35. }
  36. }
  37. break;
  38. case('e'):
  39. for(i=0; i<edges; i++){
  40. if(e_array[i] != NULL){
  41. e = e_array[i];
  42. pr_edge_fr(e, 0);
  43. }
  44. }
  45. break;
  46. case('f'):
  47. for(i=0; i<faces; i++){
  48. if(f_array[i] != NULL){
  49. f = f_array[i];
  50. pr_face_fr(f, 0, 0);
  51. }
  52. }
  53. break;
  54. default:
  55. reset('v');
  56. reset('e');
  57. reset('f');
  58. break;
  59. }
  60. return OK;
  61. }
  62. /* New-struct functions for pointer-based vertices, edges, faces */
  63. vertex_t new_vert(double x, double y, double z)
  64. {
  65. vertex_t newvert;
  66. newvert = (vertex_t) malloc( sizeof(struct vertex));
  67. newvert->fev_type = VERTEX;
  68. newvert->id = verts++;
  69. newvert->vertex_coords = mat_new(3,1);
  70. e(newvert->vertex_coords,0,0) = x;
  71. e(newvert->vertex_coords,1,0) = y;
  72. e(newvert->vertex_coords,2,0) = z;
  73. v_array[newvert->id] = newvert;
  74. return(newvert);
  75. }
  76. edge_t new_edge(vertex_t efrom, vertex_t eto)
  77. {
  78. edge_t newedge;
  79. newedge = (edge_t) malloc( sizeof(struct edge));
  80. newedge->fev_type = EDGE;
  81. newedge->id = edges++;
  82. newedge->from_vertex = efrom;
  83. newedge -> next_edge = NULL; /* for face circular edge list*/
  84. newedge->to_vertex = eto;
  85. newedge->direction = mat_add(efrom->vertex_coords, eto->vertex_coords, -1);
  86. return(newedge);
  87. }
  88. face_t new_face (edge_t edges[], int n) /* a little klunky, could use list*/
  89. {
  90. face_t newface;
  91. int i;// n;
  92. //n = sizeof(edges)/sizeof(edges[0]);
  93. newface = (face_t) malloc( sizeof(struct face));
  94. newface->fev_type = FACE;
  95. newface->id = faces++;
  96. newface->norm_vec = mat_new(1,3); /*col vector, notice!*/
  97. newface->next_edge = edges[0]; /* we're copying a pointer */
  98. /* printf("\n set face nextedge to %s", edges[0]->name);*/
  99. for (i =0; i<(n-1); i++)
  100. { edges[i]->next_edge = edges[i+1];
  101. /* printf("\n set edge %d nextedge to %s", i, edges[i+1]->name);*/
  102. }
  103. edges[n-1]->next_edge = edges[0];
  104. newface->next_face = NULL; /*filled in at higher level*/
  105. return(newface);
  106. }
  107. /* Freeing functions for pointer-based vertices, edges, faces */
  108. int vert_fr(vertex_t v){
  109. if(v == NULL) return OK;
  110. mat_free(v->vertex_coords);
  111. v_array[v->id] = NULL;
  112. free(v);
  113. return OK;
  114. }
  115. int edge_fr(edge_t e, int all){
  116. if(e == NULL) return OK;
  117. vertex_t v;
  118. mat_free(e->direction);
  119. v = e->from_vertex;
  120. if(v != NULL) vert_fr(v);
  121. v = e->to_vertex;
  122. if(v != NULL) vert_fr(v);
  123. if(all==1 && e->next_edge != NULL) edge_fr(e->next_edge, 1);
  124. free((char*) e);
  125. return OK;
  126. }
  127. int face_fr(face_t f, int all){
  128. if(f == NULL) return OK;
  129. mat_free(f->norm_vec);
  130. if(f->next_edge != NULL)
  131. edge_fr(f->next_edge, all);
  132. if(all==1 && f->next_face != NULL) face_fr(f->next_face, 1);
  133. free((char*) f);
  134. return OK;
  135. }
  136. /* Verbose print functions for pointer-based vertices, edges, faces */
  137. int vert_pr(char *msg, vertex_t v, FILE *fout) {
  138. if (msg == NULL) { cberror("null message arg in vert_pr"); return NILARG1;}
  139. if (v == NULL) { cberror("null vertex arg in vert_pr"); return NILARG2;}
  140. if(fout == NULL){
  141. printf("VERTEX: %s Type: %d , ID: %d\n", msg, v->fev_type, v->id);
  142. mat_pr("Vertex Coords", v->vertex_coords, NULL);
  143. printf("~~\n");
  144. return OK;
  145. } else {
  146. fprintf(fout, "VERTEX: %s Type: %d , ID: %d\n", msg, v->fev_type, v->id);
  147. mat_pr("Vertex Coords", v->vertex_coords, fout);
  148. fprintf(fout, "~~\n");
  149. return OK;
  150. }
  151. }
  152. int edge_pr(char *msg, edge_t edg) {
  153. if (msg == NULL) { cberror("null message arg in edge_pr"); return NILARG1;}
  154. if (edg == NULL) { cberror("null edge arg in edge_pr"); return NILARG2;}
  155. printf("EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
  156. mat_pr("Edge Direction", edg->direction, NULL);
  157. vert_pr("From Vertex ", edg->from_vertex, NULL);
  158. vert_pr("To Vertex ", edg->to_vertex, NULL);
  159. if (edg->next_edge == NULL) printf("Empty Next Edge.\n");
  160. else printf("Next Edge Exists.\n"); /* don't go recursive yet! */
  161. printf("~~~\n");
  162. return OK;
  163. }
  164. int face_pr(char *msg, face_t f) {
  165. if (msg == NULL) { cberror("null message arg in face_pr"); return NILARG1;}
  166. if (f == NULL) { cberror("null face arg in face_pr"); return NILARG2;}
  167. printf("FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
  168. mat_pr("Normal Direction ", f->norm_vec, NULL);
  169. if (f->next_edge == NULL) printf("Empty Next Edge.\n");
  170. else printf("Next Edge Exists.\n"); /* don't go recursive yet! */
  171. if(f->next_face == NULL) printf("Empty Next Face. \n");
  172. else printf("Next Face Exists. \n");
  173. printf("~~~~\n");
  174. return OK;
  175. }
  176. /* Short print functions for pointer-based vertices, edges, faces, polygon */
  177. int short_vert_pr (vertex_t v) {
  178. printf("\n %d", v->id);
  179. return OK;
  180. }
  181. int short_edge_pr (edge_t edg) {
  182. printf("\n %d", edg->id);
  183. short_vert_pr (edg->from_vertex);
  184. short_vert_pr (edg->to_vertex);
  185. return OK;
  186. }
  187. int short_face_pr (face_t f) {
  188. edge_t cur_edge;
  189. printf("\n %d", f->id);
  190. cur_edge = f->next_edge;
  191. do {
  192. short_edge_pr(cur_edge);
  193. cur_edge = cur_edge->next_edge;
  194. }
  195. while (cur_edge != f->next_edge);
  196. return OK;
  197. }
  198. int short_poly_pr(face_t f) {
  199. face_t cur_face;
  200. cur_face = f;
  201. do
  202. {
  203. short_face_pr(cur_face);
  204. cur_face = cur_face->next_face;
  205. }
  206. while (cur_face != f);
  207. return OK;
  208. }
  209. /************************PRINTABLE********************/
  210. /*** Vertices only come in one variety ***/
  211. /* New-struct functions for printable edges, faces */
  212. /*
  213. * fully-recursing function to make printable structs out of
  214. * non-printable edges
  215. */
  216. pr_edge_t new_print_edge(edge_t e){
  217. pr_edge_t pr_e;
  218. pr_e = (pr_edge_t) malloc(sizeof(struct printable_e));
  219. pr_e->direction = e->direction;
  220. pr_e->fev_type = e->fev_type;
  221. pr_e->from_v = e->from_vertex->id;
  222. // if(v_array[pr_e->from_v] == NULL)
  223. // v_array[pr_e->from_v] = e->from_vertex;
  224. pr_e->to_v = e->to_vertex->id;
  225. // if(v_array[pr_e->to_v] == NULL)
  226. // v_array[pr_e->to_v] = e->to_vertex;
  227. pr_e->id = e->id;
  228. e_array[pr_e->id] = pr_e;
  229. if(e->next_edge == NULL){
  230. pr_e->next_edge = -1;
  231. } else {
  232. pr_e->next_edge = e->next_edge->id;
  233. if(e_array[pr_e->next_edge] == NULL){
  234. new_print_edge(e->next_edge);
  235. }
  236. }
  237. return pr_e;
  238. }
  239. /* fully-recursing function to make printable structs out of
  240. * non-printable faces
  241. */
  242. pr_face_t new_print_face(face_t f){
  243. pr_face_t pr_f;
  244. pr_f = (pr_face_t) malloc(sizeof(struct printable_f));
  245. pr_f->fev_type = f->fev_type;
  246. pr_f->id = f->id;
  247. f_array[pr_f->id] = pr_f;
  248. pr_f->norm_vec = f->norm_vec;
  249. if(f->next_edge == NULL){
  250. pr_f->next_edge = -1;
  251. } else {
  252. pr_f->next_edge = f->next_edge->id;
  253. if(e_array[f->next_edge->id] == NULL){
  254. new_print_edge(f->next_edge);
  255. }
  256. }
  257. if(f->next_face == NULL){
  258. pr_f->next_face = -1;
  259. } else {
  260. pr_f->next_face = f->next_face->id;
  261. if(f_array[f->next_face->id] == NULL){
  262. new_print_face(f->next_face);
  263. }
  264. }
  265. return pr_f;
  266. }
  267. /* Freeing functions for printable edges, faces */
  268. int pr_edge_fr(pr_edge_t e, int fr_vert){
  269. if(e == NULL) return OK;
  270. mat_free(e->direction);
  271. /* Since I'm going to use this iteratively, it doesn't recurse */
  272. if(fr_vert == 1) {
  273. if(v_array[e->from_v] != NULL) vert_fr(v_array[e->from_v]);
  274. if(v_array[e->to_v] != NULL) vert_fr(v_array[e->to_v]);
  275. }
  276. e_array[e->id] = NULL;
  277. free((char*) e);
  278. return OK;
  279. }
  280. int pr_face_fr(pr_face_t f, int fr_edge, int fr_vert){
  281. if(f == NULL) return OK;
  282. mat_free(f->norm_vec);
  283. if(fr_edge == 1) {
  284. if(e_array[f->next_edge] != NULL)
  285. pr_edge_fr(e_array[f->next_edge], fr_vert);
  286. }
  287. f_array[f->id] = NULL;
  288. free((char*) f);
  289. return OK;
  290. }
  291. /* Verbose print functions for printable edges, faces
  292. * - prints to a file optionally
  293. * - NOT recursive - must be used iteratively
  294. */
  295. int pr_edge_pr(char *msg, pr_edge_t edg, FILE *fout) {
  296. if (msg == NULL) { cberror("null message arg in edge_pr"); return NILARG1;}
  297. if (edg == NULL) { cberror("null edge arg in edge_pr"); return NILARG2;}
  298. if(fout == NULL){
  299. printf("EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
  300. mat_pr("Edge Direction", edg->direction, NULL);
  301. vert_pr("From Vertex ", v_array[edg->from_v], NULL);
  302. vert_pr("To Vertex ", v_array[edg->to_v], NULL);
  303. printf("Next Edge: %d. \n", edg->next_edge);
  304. printf("~~~\n");
  305. return OK;
  306. } else {
  307. fprintf(fout, "EDGE: %s Type: %d, ID: %d\n", msg, edg->fev_type,edg->id);
  308. mat_pr("Edge Direction", edg->direction, fout);
  309. vert_pr("From Vertex ", v_array[edg->from_v], fout);
  310. vert_pr("To Vertex ", v_array[edg->to_v], fout);
  311. fprintf(fout, "Next Edge: %d. \n", edg->next_edge);
  312. fprintf(fout, "~~~\n");
  313. return OK;
  314. }
  315. }
  316. int pr_face_pr(char *msg, pr_face_t f, FILE *fout) {
  317. if (msg == NULL) { cberror("null message arg in face_pr"); return NILARG1;}
  318. if (f == NULL) { cberror("null face arg in face_pr"); return NILARG2;}
  319. if(fout == NULL){
  320. printf("FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
  321. mat_pr("Normal Direction ", f->norm_vec, NULL);
  322. printf("Next Edge: %d. \n", f->next_edge);
  323. printf("Next Face: %d. \n", f->next_face);
  324. printf("~~~~\n");
  325. return OK;
  326. } else {
  327. fprintf(fout, "FACE: %s Type: %d, ID: %d \n", msg, f->fev_type, f->id);
  328. mat_pr("Normal Direction ", f->norm_vec, fout);
  329. fprintf(fout, "Next Edge: %d. \n", f->next_edge);
  330. fprintf(fout, "Next Face: %d. \n", f->next_face);
  331. fprintf(fout, "~~~~\n");
  332. return OK;
  333. }
  334. }
  335. /* Short print functions for printable edges, faces */
  336. int short_pr_edge_pr (pr_edge_t edg) {
  337. printf("\n %d", edg->id);
  338. short_vert_pr (v_array[edg->from_v]);
  339. short_vert_pr (v_array[edg->to_v]);
  340. return OK;
  341. }
  342. int short_pr_face_pr (pr_face_t f) {
  343. int cur_edge;
  344. printf("\n %d", f->id);
  345. cur_edge = f->next_edge;
  346. do {
  347. short_pr_edge_pr(e_array[cur_edge]);
  348. cur_edge = e_array[cur_edge]->next_edge;
  349. }
  350. while (cur_edge != f->next_edge);
  351. return OK;
  352. }
  353. vertex_t vert_read(FILE *fin){
  354. int t, id, assgn, i, j, r, c;
  355. float n;
  356. vertex_t v;
  357. assgn = fscanf(fin, "VERTEX: %*s Type: %d , ID: %d\n", &t, &id);
  358. if(assgn != 2){
  359. cberror("Nope, can't do that either");
  360. return NULL;
  361. }
  362. v = (vertex_t) malloc(sizeof(struct vertex));
  363. v->fev_type = t;
  364. v->id = id;
  365. fscanf(fin, "MATRIX: %*s Type: %*d , Dims %d, %d\n", &r, &c);
  366. v->vertex_coords = mat_new(r, c);
  367. for (i = 0; i < r; i++) {
  368. for (j=0; j<c; j++) {
  369. assgn = fscanf(fin, "%f ", &n);
  370. if(assgn != 1) {
  371. cberror("Formatting error (matrix, vertex)");
  372. return NULL;
  373. }
  374. e(v->vertex_coords, i, j) = n;
  375. }
  376. fscanf(fin, "\n"); //end of each row
  377. }
  378. fscanf(fin, "~\n");
  379. fscanf(fin, "~~\n");
  380. v_array[id] = v;
  381. return v;
  382. }
  383. /* Print-a-polyhedron function - takes a pointeriffic polyhedron and
  384. * makes it all human-readable and not pointery */
  385. int printificate(face_t head, char* fname){
  386. FILE *fout;
  387. int i;
  388. new_print_face(head);
  389. fout = fopen(fname, "w+");
  390. fprintf(fout, "VERTICES: %d \nEDGES: %d \nFACES: %d \n", verts, edges, faces);
  391. for(i = 0; i < faces; i++){
  392. pr_face_pr(" ", f_array[i], fout);
  393. }
  394. for(i = 0; i < edges; i++){
  395. pr_edge_pr(" ", e_array[i], fout);
  396. }
  397. fclose(fout);
  398. reset('a'); /* empty my arrays */
  399. return OK;
  400. }
  401. /*
  402. * Read in a file full of faces, edges, and pointers and turn it into a
  403. * pointeriffic data structure
  404. */
  405. /* returns the head face */
  406. face_t readificate(char* fname){
  407. int v, e, f, assgn, i, j, t, id, next, r, c;
  408. float n;
  409. int nexte[ARRSIZE];
  410. int nextfe[ARRSIZE];
  411. int nextff[ARRSIZE];
  412. edge_t edge;
  413. face_t face;
  414. edge_t edgen[ARRSIZE];
  415. face_t facen[ARRSIZE];
  416. FILE *fin;
  417. char* test;
  418. fin = fopen(fname, "r");
  419. assgn = fscanf(fin, "VERTICES: %d \nEDGES: %d \nFACES: %d \n", &v, &e, &f);
  420. if(assgn != 3) {
  421. cberror("Incorrectly formatted input file");
  422. return (face_t) NULL;
  423. }
  424. reset('a'); /* Going to put new data into the arrays */
  425. for(i = 0; i < f; i++){
  426. fscanf(fin, "FACE: %*s Type: %d, ID: %d \n", &t, &id);
  427. face = (face_t) malloc(sizeof(struct face));
  428. face->fev_type = t;
  429. face->id = id;
  430. fscanf(fin, "MATRIX: %s Type: %*d , Dims %d, %d\n", test, &r, &c);
  431. face->norm_vec = mat_new(r, c);
  432. for (i = 0; i < r; i++) {
  433. for (j=0; j<c; j++) {
  434. assgn = fscanf(fin, "%f ", &n);
  435. if(assgn != 1) {
  436. cberror("Formatting error (matrix, face)");
  437. return NULL;
  438. }
  439. e(face->norm_vec, i, j) = n;
  440. }
  441. fscanf(fin, "\n"); //end of each row
  442. }
  443. fscanf(fin, "~\n");
  444. fscanf(fin, "Next Edge: %d. \n", &nextfe[id]);
  445. fscanf(fin, "Next Face: %d. \n", &nextff[id]);
  446. fscanf(fin, "~~~~\n");
  447. facen[id] = face;
  448. }
  449. for(i = 0; i < e; i++){
  450. fscanf(fin, "EDGE: %*s Type: %d, ID: %d\n", &t, &id);
  451. edge = (edge_t) malloc(sizeof(struct edge));
  452. fscanf(fin, "MATRIX: %*s Type: %*d , Dims %d, %d\n", &r, &c);
  453. edge->direction = mat_new(r, c);
  454. for (i = 0; i < r; i++) {
  455. for (j=0; j<c; j++) {
  456. assgn = fscanf(fin, "%f ", &n);
  457. if(assgn != 1) {
  458. cberror("Formatting error (matrix, edge)");
  459. return NULL;
  460. }
  461. e(edge->direction, i, j) = n;
  462. }
  463. fscanf(fin, "\n"); //end of each row
  464. }
  465. fscanf(fin, "~\n");
  466. edge->from_vertex = vert_read(fin);
  467. edge->to_vertex = vert_read(fin);
  468. edge->fev_type = t;
  469. edge->id = id;
  470. fscanf(fin, "Next Edge: %d. \n", &nexte[id]);
  471. fscanf(fin, "~~~\n");
  472. edgen[id] = edge;
  473. }
  474. for(i = 0; i < e; i++){
  475. next = nexte[i];
  476. if(next != -1)
  477. edgen[i]->next_edge = edgen[next];
  478. }
  479. for(i = 0; i < f; i++){
  480. next = nextfe[i];
  481. if(next != -1)
  482. facen[i]->next_edge = edgen[next];
  483. next = nextff[i];
  484. if(next != -1)
  485. facen[i]->next_face = facen[next];
  486. }
  487. return facen[0];
  488. }