| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "packing.h"
- Node *load_file(char *filename, int *numnodes, int *numboxes){
- // open file for reading
- FILE *fptr = NULL;
- fptr = fopen(filename, "r");
- if (fptr == NULL){ // check for error
- printf("File name error.\n");
- return NULL;
- }
-
- // read number of boxes
- if(fscanf(fptr, "%d", numboxes) == EOF){
- printf("Error reading mumber of boxes.\n");
- return NULL;
- }
-
- // read number of nodes
- if(fscanf(fptr, "%d", numnodes) == EOF){
- printf("Error reading mumber of nodes.\n");
- return NULL;
- }
-
- // allocate memory for all nodes
- Node *array = malloc(sizeof(Node) * (*numnodes));
- // read values for each node
- int i;
- for(i = 0; i < (*numnodes); i++){
- if(fscanf(fptr, "%d", &array[i].thisnode) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, "%d", &array[i].parnode) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, "%d", &array[i].lcnode) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, "%d", &array[i].rcnode) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, " %c ", &array[i].cutline) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, "%lf", &array[i].width) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- if(fscanf(fptr, "%lf", &array[i].height) == EOF){
- printf("Error reading value.\n");
- return NULL;
- }
- // initialize values to be calculated later
- array[i].xcord = 0;
- array[i].ycord = 0;
- array[i].left = NULL;
- array[i].right = NULL;
- array[i].parent = NULL;
- }
-
- // clean up
- if(!fclose(fptr) == 0){
- printf("File close error.");
- return NULL;
- }
- return array;
- }
- // create binary tree structure in array of nodes
- Node *set_pointers(Node *array, int length){
- // root is last value
- int cursor = length - 1; // start at root
- int i;
- for(i = length - 1; i >= 0; i--){
- // update left pointers
- if(array[i].lcnode != -1){
- cursor = i - 1;
- while(array[cursor].thisnode != array[i].lcnode){
- cursor--;
- }
- array[i].left = &array[cursor];
- array[cursor].parent = &array[i];
- }
- else{
- array[i].left = NULL;
- }
- // update right pointers
- if(array[i].rcnode != -1){
- cursor = i - 1;
- while(array[cursor].thisnode != array[i].rcnode){
- cursor--;
- }
- array[i].right = &array[cursor];
- array[cursor].parent = &array[i];
- }
- else{
- array[i].right = NULL;
- }
- }
-
- // return root
- return &array[length - 1];
- }
- void set_dimensions(Node *root){
- // end recursion
- if(root->left == NULL && root->right == NULL){
- return;
- }
-
- // recurse
- set_dimensions(root->left);
- set_dimensions(root->right);
-
- if(root->cutline == 'V'){
- // add widths for vertzical cut
- root->width = root->left->width + root->right->width;
- if(root->left->height >= root->right->height){
- root->height = root->left->height;
- }
- else{
- root->height = root->right->height;
- }
- }
- if(root->cutline == 'H'){
- // add heights for horizontal cut
- root->height = root->left->height + root->right->height;
- if(root->left->width >= root->right->width){
- root->width = root->left->width;
- }
- else{
- root->width = root->right->width;
- }
- }
- }
- void set_coordinates(Node *root){
- if(root->cutline == 'V'){
- // left stays, right moves
- root->left->xcord = root->xcord;
- root->left->ycord = root->ycord;
- root->right->xcord = root->xcord + root->left->width;
- root->right->ycord = root->ycord;
- }
- if(root->cutline == 'H'){
- // right stays, left moves
- root->left->xcord = root->xcord;
- root->left->ycord = root->ycord + root->right->height;;
- root->right->xcord = root->xcord;
- root->right->ycord = root->ycord;
- }
- }
- void print_node_numbers_preorder(Node *root){
- if(root == NULL){
- return;
- }
- printf(" %d", root->thisnode);
- print_node_numbers_preorder(root->left);
- print_node_numbers_preorder(root->right);
- }
- void print_node_numbers_inorder(Node *root){
- if(root == NULL){
- return;
- }
- print_node_numbers_inorder(root->left);
- printf(" %d", root->thisnode);
- print_node_numbers_inorder(root->right);
- }
- void print_node_numbers_postorder(Node *root){
- if(root == NULL){
- return;
- }
- print_node_numbers_postorder(root->left);
- print_node_numbers_postorder(root->right);
- printf(" %d", root->thisnode);
- }
- void save_boxes(char *filename, Node *array, int numboxes){
- // write to file
- FILE *fptr = NULL;
- fptr = fopen(filename, "w");
-
- fprintf(fptr, "%d\n", numboxes);
- int i;
- for(i = 0; i < numboxes; i++){
- fprintf(fptr, "%d %le %le %le %le\n",
- array[i].thisnode,
- array[i].width,
- array[i].height,
- array[i].xcord,
- array[i].ycord);
- }
-
- // close file
- if(!fclose(fptr) == 0){
- printf("File close error.");
- return;
- }
- }
|