#include #include #include #include #include FILE *Rfile; // The Read File with lookup table FILE *Wfile; // The output Write File with the mapping done FILE *WVRMLfile; // This is the output file for the VRML output int position = -1; // Records the different positions for the agents int nest[10][10][10]; // The actual nest to be created with bricks of type 0, 1 and 2. int agent[10][5000]; // Contains the 10 agents with their positions int low1[9], mid1[9], high1[9], low21[9], mid21[9], high21[9], low22[9], mid22[9], high22[9]; int low23[9], mid23[9], high23[9], low24[9], mid24[9], high24[9], low25[9], mid25[9], high25[9]; int low26[9], mid26[9], high26[9], low27[9], mid27[9], high27[9], low28[9], mid28[9], high28[9]; //These arrays keep the look up table //low indicates (z-1), mid indicates (z), high indicates (z+1) while low21 indicates rule 2.1 and similarly for others void generate_vrml () { int i, j, k; float translate_x, translate_y, translate_z; // This is kept to manipulate the translation on the 3-axes from the origin fprintf (WVRMLfile, "#VRML V2.0 utf8 Group { children [ Transform { translation 0.0 0.0 0.0 #rotation 1.0 1.0 1.0 1.0 children Shape { appearance DEF Cube1 Appearance { material Material { diffuseColor 0.4 0.6 0.8 specularColor 0.2 0.3 0.4 transparency 0.1 } } geometry DEF Brick1 Box { size 0.2 0.2 0.2 } } } Transform { translation 0.0 0.0 0.0 children Shape { appearance DEF Cube2 Appearance { material Material { diffuseColor 0.7 0.6 0.2 specularColor 0.7 0.7 0.9 transparency 0.1 } } geometry DEF Brick2 Box { size 0.2 0.2 0.2 } } }"); for (i=0; i<10; i++) { for (j=0; j<10; j++) { for (k=0; k<10; k++) { translate_x = 0.0 + i * 0.125 - 1.125; translate_y = 0.0 + j * 0.125 - 1.125; translate_z = 0.0 + k * 0.125 - 1.125; if (nest[i][j][k] == 1) { fprintf (WVRMLfile, "Transform { translation %f %f %f #rotation 1.0 1.0 1.0 1.0 children Shape { appearance USE Cube1 geometry USE Brick1 } }", translate_x, translate_y, translate_z); // End of Transform and fprintf } // end if if (nest[i][j][k] == 2) { fprintf (WVRMLfile, "Transform { translation %f %f %f #rotation 1.0 1.0 1.0 1.0 children Shape { appearance USE Cube2 geometry USE Brick2 } }", translate_x, translate_y, translate_z); // End of Transform and fprintf } // end if } // end for loop of k } // end for loop of j }// end for loop of i fprintf (WVRMLfile, "\n ] }"); // concluding section of VRMLfile } void print_nest () { int i, j, k; for (i=0; i<10; i++) { for (j=0; j<10; j++) { for (k=0; k<10; k++) { fprintf (Wfile, "%d", nest[i][j][k]); } fprintf (Wfile, "\n"); } } } void compare1 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low1[i]) && (level[i] == mid1[i]) && (top[i] == high1[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 1; } void compare21 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a = 0; for (i=0; i<9; i++) { if ((bottom[i] == low21[i]) && (level[i] == mid21[i]) && (top[i] == high21[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare22 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low22[i]) && (level[i] == mid22[i]) && (top[i] == high22[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare23 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low23[i]) && (level[i] == mid23[i]) && (top[i] == high23[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare24 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low24[i]) && (level[i] == mid24[i]) && (top[i] == high24[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare25 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp [9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low25[i]) && (level[i] == mid25[i]) && (top[i] == high25[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare26 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp[9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low26[i]) && (level[i] == mid26[i]) && (top[i] == high26[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare27 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp[9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low27[i]) && (level[i] == mid27[i]) && (top[i] == high27[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void compare28 (int top[], int level[], int bottom[], int pos_x, int pos_y, int pos_z) { int i = 0; int comp[9]; int a; for (i=0; i<9; i++) { if ((bottom[i] == low28[i]) && (level[i] == mid28[i]) && (top[i] == high28[i])) { comp [i] = 1; } else comp [i] = 0; } if ((comp[0] == 0) || (comp[1] == 0) || (comp[2] == 0) || (comp[3] == 0) || (comp[4] == 0) || (comp[5] == 0) || (comp[6] == 0) || (comp[7] == 0) || (comp[8] == 0)) a = 0; else nest[pos_x][pos_y][pos_z] = 2; } void neighbor_bricks (int pos_x, int pos_y, int pos_z) { int top[9], level[9], bottom[9]; if ((pos_x == 0) || (pos_x == 9) || (pos_y == 0) || (pos_y == 9) || (pos_z == 0) || (pos_z == 9)) { fprintf (Wfile, "We would not consider this case\n"); } else { // Stores the elements at the neighboring 26 points for future comparison bottom [0] = nest[pos_x-1][pos_y-1][pos_z+1]; bottom [1] = nest[pos_x][pos_y-1][pos_z+1]; bottom [2] = nest[pos_x+1][pos_y-1][pos_z+1]; bottom [3] = nest[pos_x-1][pos_y-1][pos_z]; bottom [4] = nest[pos_x][pos_y-1][pos_z]; bottom [5] = nest[pos_x+1][pos_y-1][pos_z]; bottom [6] = nest[pos_x-1][pos_y-1][pos_z-1]; bottom [7] = nest[pos_x][pos_y-1][pos_z-1]; bottom [8] = nest[pos_x+1][pos_y-1][pos_z-1]; level [0] = nest[pos_x-1][pos_y][pos_z+1]; level [1] = nest[pos_x][pos_y][pos_z+1]; level [2] = nest[pos_x+1][pos_y][pos_z+1]; level [3] = nest[pos_x-1][pos_y][pos_z]; level [4] = 5; //Initialized with 5 as thats what is in the lookup table (data.txt) for the base position level [5] = nest[pos_x+1][pos_y][pos_z]; level [6] = nest[pos_x-1][pos_y][pos_z-1]; level [7] = nest[pos_x][pos_y][pos_z-1]; level [8] = nest[pos_x+1][pos_y][pos_z-1]; top [0] = nest[pos_x-1][pos_y+1][pos_z+1]; top [1] = nest[pos_x][pos_y+1][pos_z+1]; top [2] = nest[pos_x+1][pos_y+1][pos_z+1]; top [3] = nest[pos_x-1][pos_y+1][pos_z]; top [4] = nest[pos_x][pos_y+1][pos_z]; top [5] = nest[pos_x+1][pos_y+1][pos_z]; top [6] = nest[pos_x-1][pos_y+1][pos_z-1]; top [7] = nest[pos_x][pos_y+1][pos_z-1]; top [8] = nest[pos_x+1][pos_y+1][pos_z-1]; compare1 (top, level, bottom, pos_x, pos_y, pos_z); compare21 (top, level, bottom, pos_x, pos_y, pos_z); compare22 (top, level, bottom, pos_x, pos_y, pos_z); compare23 (top, level, bottom, pos_x, pos_y, pos_z); compare24 (top, level, bottom, pos_x, pos_y, pos_z); compare25 (top, level, bottom, pos_x, pos_y, pos_z); compare26 (top, level, bottom, pos_x, pos_y, pos_z); compare27 (top, level, bottom, pos_x, pos_y, pos_z); compare28 (top, level, bottom, pos_x, pos_y, pos_z); } } void exact_pos (int pos) { int pos_x = 0; int pos_y = 0; int pos_z = 0; int rem = 0; // Generate the exact x, y, z position of the agent in the 3-D nest pos_y = pos/100; rem = pos%100; pos_z = rem/10; pos_x = rem%10; fprintf (Wfile, "%d %d %d\n", pos_x, pos_y, pos_z); neighbor_bricks (pos_x, pos_y, pos_z); } void generate_agent_pos () { int pos=0; int r=0; int i=0; position++; // Records the different positions incrementally for future reference for (i=0; i<10; i++) { r=rand(); // Generate a random position for the agent if ((r>=0) && (r<999)) { // Position should lie b/w 0 and 999 pos = r; agent[i][position] = pos; exact_pos (pos); // Calculate the exact x, y, z position of the agent in the 3-D nest } else { i--; } } // end for } void init_nest () { int i=0; int j=0; int k=0; // Initialise the nest array for (i=0; i<10; i++) for (j=0; j<10; j++) for (k=0; k<10; k++) nest[i][j][k] = 0; // Assigned with 0 indicating no element present nest[5][5][5] = 1; // Just by convention, we chose initial position in the nest to have a brick of type 1. } void set_table (int i, int map[]) { //This function sets the look up table int j=0; int k=0; while (j=9) && (j<=17)) { mid1[k] = map[j]; k++; } if ((j>=18) && (j<=26)){ high1[k] = map[j]; k++; } if ((j>=27) && (j<=35)) { low21[k] = map[j]; k++; } if ((j>=36) && (j<=44)) { mid21[k] = map[j]; k++; } if ((j>=45) && (j<=53)) { high21[k] = map[j]; k++; } if ((j>=54) && (j<=62)) { low22[k] = map[j]; k++; } if ((j>=63) && (j<=71)) { mid22[k] = map[j]; k++; } if ((j>=72) && (j<=80)) { high22[k] = map[j]; k++; } if ((j>=81) && (j<=89)) { low23[k] = map[j]; k++; } if ((j>=90) && (j<=98)) { mid23[k] = map[j]; k++; } if ((j>=99) && (j<=107)) { high23[k] = map[j]; k++; } if ((j>=108) && (j<=116)) { low24[k] = map[j]; k++; } if ((j>=117) && (j<=125)) { mid24[k] = map[j]; k++; } if ((j>=126) && (j<=134)) { high24[k] = map[j]; k++; } if ((j>=135) && (j<=143)) { low25[k] = map[j]; k++; } if ((j>=144) && (j<=152)) { mid25[k] = map[j]; k++; } if ((j>=153) && (j<=161)) { high25[k] = map[j]; k++; } if ((j>=162) && (j<=170)) { low26[k] = map[j]; k++; } if ((j>=171) && (j<=179)) { mid26[k] = map[j]; k++; } if ((j>=180) && (j<=188)) { high26[k] = map[j]; k++; } if ((j>=189) && (j<=197)) { low27[k] = map[j]; k++; } if ((j>=198) && (j<=206)) { mid27[k] = map[j]; k++; } if ((j>=207) && (j<=215)) { high27[k] = map[j]; k++; } if ((j>=216) && (j<=224)) { low28[k] = map[j]; k++; } if ((j>=225) && (j<=233)) { mid28[k] = map[j]; k++; } if ((j>=234) && (j<=242)) { high28[k] = map[j]; k++; } j++; } } // End of setting look-up table int main() { int map[250]; // Contains the lookup table int i=0; int j=0; int k=0; int x=0; Rfile = fopen("data_prac.txt", "r"); Wfile = fopen("output_prac.txt", "w"); WVRMLfile = fopen("vrmloutput_prac.wrl", "w"); if(Rfile==NULL) { printf("Error: can't open read file.\n"); return 1; } else { printf("File opened successfully.\n"); while(!feof(Rfile)) { fscanf (Rfile, "%d", &x); if ((x != ' ') && (x != '\n')) { map[i] = x; i++; } } set_table (i, map); // Setting the look-up table fprintf (Wfile, "We chose the Number of agents (insects) to be 10\n"); init_nest(); // Initialise the nest array fprintf (Wfile, "We genarate the random initial positions of the 10 agents in the 3-D nest (0=