1
0

packing.c~ 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "packing.h"
  5. Node *load_file(char *filename, int *numnodes, int *numboxes){
  6. // open file for reading
  7. FILE *fptr = NULL;
  8. fptr = fopen(filename, "r");
  9. if (fptr == NULL){ // check for error
  10. printf("File name error.\n");
  11. return NULL;
  12. }
  13. // read number of boxes
  14. if(fscanf(fptr, "%d", numboxes) == EOF){
  15. printf("Error reading mumber of boxes.\n");
  16. return NULL;
  17. }
  18. // read number of nodes
  19. if(fscanf(fptr, "%d", numnodes) == EOF){
  20. printf("Error reading mumber of nodes.\n");
  21. return NULL;
  22. }
  23. // allocate memory for all nodes
  24. Node *array = malloc(sizeof(Node) * (*numnodes));
  25. // read values for each node
  26. int i;
  27. for(i = 0; i < (*numnodes); i++){
  28. if(fscanf(fptr, "%d", &array[i].thisnode) == EOF){
  29. printf("Error reading value.\n");
  30. return NULL;
  31. }
  32. if(fscanf(fptr, "%d", &array[i].parnode) == EOF){
  33. printf("Error reading value.\n");
  34. return NULL;
  35. }
  36. if(fscanf(fptr, "%d", &array[i].lcnode) == EOF){
  37. printf("Error reading value.\n");
  38. return NULL;
  39. }
  40. if(fscanf(fptr, "%d", &array[i].rcnode) == EOF){
  41. printf("Error reading value.\n");
  42. return NULL;
  43. }
  44. if(fscanf(fptr, " %c ", &array[i].cutline) == EOF){
  45. printf("Error reading value.\n");
  46. return NULL;
  47. }
  48. if(fscanf(fptr, "%lf", &array[i].width) == EOF){
  49. printf("Error reading value.\n");
  50. return NULL;
  51. }
  52. if(fscanf(fptr, "%lf", &array[i].height) == EOF){
  53. printf("Error reading value.\n");
  54. return NULL;
  55. }
  56. // initialize values to be calculated later
  57. array[i].xcord = 0;
  58. array[i].ycord = 0;
  59. array[i].left = NULL;
  60. array[i].right = NULL;
  61. array[i].parent = NULL;
  62. }
  63. // clean up
  64. if(!fclose(fptr) == 0){
  65. printf("File close error.");
  66. return NULL;
  67. }
  68. return array;
  69. }
  70. // create binary tree structure in array of nodes
  71. Node *set_pointers(Node *array, int length){
  72. // root is last value
  73. int cursor = length - 1; // start at root
  74. int i;
  75. for(i = length - 1; i >= 0; i--){
  76. // update left pointers
  77. if(array[i].lcnode != -1){
  78. cursor = i - 1;
  79. while(array[cursor].thisnode != array[i].lcnode){
  80. cursor--;
  81. }
  82. array[i].left = &array[cursor];
  83. array[cursor].parent = &array[i];
  84. }
  85. else{
  86. array[i].left = NULL;
  87. }
  88. // update right pointers
  89. if(array[i].rcnode != -1){
  90. cursor = i - 1;
  91. while(array[cursor].thisnode != array[i].rcnode){
  92. cursor--;
  93. }
  94. array[i].right = &array[cursor];
  95. array[cursor].parent = &array[i];
  96. }
  97. else{
  98. array[i].right = NULL;
  99. }
  100. }
  101. // return root
  102. return &array[length - 1];
  103. }
  104. void set_dimensions(Node *root){
  105. // end recursion
  106. if(root->left == NULL && root->right == NULL){
  107. return;
  108. }
  109. // recurse
  110. set_dimensions(root->left);
  111. set_dimensions(root->right);
  112. if(root->cutline == 'V'){
  113. // add widths for vertzical cut
  114. root->width = root->left->width + root->right->width;
  115. if(root->left->height >= root->right->height){
  116. root->height = root->left->height;
  117. }
  118. else{
  119. root->height = root->right->height;
  120. }
  121. }
  122. if(root->cutline == 'H'){
  123. // add heights for horizontal cut
  124. root->height = root->left->height + root->right->height;
  125. if(root->left->width >= root->right->width){
  126. root->width = root->left->width;
  127. }
  128. else{
  129. root->width = root->right->width;
  130. }
  131. }
  132. }
  133. void set_coordinates(Node *root){
  134. if(root->cutline == 'V'){
  135. // left stays, right moves
  136. root->left->xcord = root->xcord;
  137. root->left->ycord = root->ycord;
  138. root->right->xcord = root->xcord + root->left->width;
  139. root->right->ycord = root->ycord;
  140. }
  141. if(root->cutline == 'H'){
  142. // right stays, left moves
  143. root->left->xcord = root->xcord;
  144. root->left->ycord = root->ycord + root->right->height;;
  145. root->right->xcord = root->xcord;
  146. root->right->ycord = root->ycord;
  147. }
  148. }
  149. void print_node_numbers_preorder(Node *root){
  150. if(root == NULL){
  151. return;
  152. }
  153. printf(" %d", root->thisnode);
  154. print_node_numbers_preorder(root->left);
  155. print_node_numbers_preorder(root->right);
  156. }
  157. void print_node_numbers_inorder(Node *root){
  158. if(root == NULL){
  159. return;
  160. }
  161. print_node_numbers_inorder(root->left);
  162. printf(" %d", root->thisnode);
  163. print_node_numbers_inorder(root->right);
  164. }
  165. void print_node_numbers_postorder(Node *root){
  166. if(root == NULL){
  167. return;
  168. }
  169. print_node_numbers_postorder(root->left);
  170. print_node_numbers_postorder(root->right);
  171. printf(" %d", root->thisnode);
  172. }
  173. void save_boxes(char *filename, Node *array, int numboxes){
  174. // write to file
  175. FILE *fptr = NULL;
  176. fptr = fopen(filename, "w");
  177. fprintf(fptr, "%d\n", numboxes);
  178. int i;
  179. for(i = 0; i < numboxes; i++){
  180. fprintf(fptr, "%d %le %le %le %le\n",
  181. array[i].thisnode,
  182. array[i].width,
  183. array[i].height,
  184. array[i].xcord,
  185. array[i].ycord);
  186. }
  187. // close file
  188. if(!fclose(fptr) == 0){
  189. printf("File close error.");
  190. return;
  191. }
  192. }