Advertisement
hoefsdavid9701

Untitled

Dec 14th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. This is the function where I fill the array data from a file.
  2. void read_egm(string fname, string &ftype, Header &egm, short * &data_e4, float * &data_e1, double * &dnew)
  3. {
  4.     ifstream ifs;           //opening the input filestream
  5.     ifs.open(fname);        // open the file
  6.     if (!ifs)                // checks to make sure the file was successfully opened
  7.     {
  8.         cerr << "cannot open file" << endl;
  9.     }
  10.  
  11.     char* type = new char[2];       // declaring a char pointer to determine if the file is e1 or e4
  12.     ifs.read(type, 2);
  13.     ftype = type[1];                // assigns the variable ftype to the value of type (e1 or e4)
  14.     cout << ftype << endl;
  15.     delete[] type;                 // cleaning up when Im finished with the char pointer.
  16.     type = nullptr;                 // setting type to null
  17.  
  18.     if (ftype == "1")        // if the file was e1
  19.     {
  20.         ifs >> egm.nr >> egm.nc >> egm.min >> egm.max >> egm.Xscale >> egm.Yscale;   // read in the header information using >> operator
  21.         int size = egm.nc*egm.nr;                         // declaring the variable size to be equal to total number of values in file
  22.         data_e1 = new float[size];      // creating a pointer array to hold the data in the file.
  23.         cout << egm.nc << " " << egm.nr << endl;;
  24.         for (int i = 0; i<size; i++)         // for loop to fill the dynamically allocated array with values.
  25.         {
  26.  
  27.             ifs >> data_e1[i];
  28.  
  29.         }
  30.         cout << data_e1[0] << endl;
  31.     }
  32.     else if (ftype == "4")
  33.     {
  34.         ifs.close();
  35.         ifs.open(fname, ios::binary);
  36.         type = new char[8];
  37.         ifs.read(type, 8);
  38.         cout << type << endl;
  39.         int * header = new int[6];
  40.         ifs.read(reinterpret_cast<char*>(header), 6 * sizeof(int));
  41.  
  42.         egm.nc = header[0];
  43.         egm.nr = header[1];
  44.         egm.min = header[2];
  45.         egm.max = header[3];
  46.         egm.Xscale = header[4];
  47.         egm.Yscale = header[5];
  48.         cout << egm.nc << " " << egm.nr << " " << egm.min << " " << egm.max << " " << egm.Xscale << " " << egm.Yscale << endl;
  49.         int size = egm.nc*egm.nr;
  50.         data_e4 = new short[size];
  51.         ifs.read(reinterpret_cast<char*>(data_e4), size * sizeof(short));
  52.         dnew = new double[size];
  53.         for (int i = 0; i<size; i++)
  54.         {
  55.             dnew[i] = data_e4[i];
  56.         }
  57.  
  58.         ifs.close();
  59.     }
  60. }
  61.  
  62. Then i do calculations on the data and put the new values in another array
  63.  
  64. void illuminate_PGM_from_EGM(string ftype, string outname, Header egm, short * &data_e4, Vec3d v, float* &data_e1, Vec3d sun, int* &newdat, double* &dnew)
  65. {
  66.     Vec3d a, b;
  67.     sun = get_sun_angle();   // prompts user for sun azimuth and elevation
  68.  
  69.     int* prev = new int[egm.nc*egm.nr];
  70.  
  71.     newdat = new int[egm.nc*egm.nr];
  72.     for (int r = 0; r<egm.nr; r++)      // for each row
  73.     {
  74.         for (int c = 0; c<egm.nc; c++)// for each column
  75.         {
  76.  
  77.             a.x = egm.Xscale;       // setting x in vec a to be the xscaling
  78.             a.y = 1;              // setting y in vec a to be 1;
  79.             int pointa = data_e1[r*egm.nc + c + 1] - data_e1[r*egm.nc + c];    // finding the difference of a value and the value to its right
  80.             a.z = pointa;        // setting z in vec a to the difference of the points
  81.             b.x = 1;              // setting x in vec b to 1
  82.             b.y = egm.Yscale;         // setting y in vec b to the yscaling
  83.             int pointb = data_e1[r + 1 * egm.nc + c] - data_e1[r*egm.nc + c]; // finding the change in elevation from the original elevation and the elevation directly beneath it
  84.             b.z = pointb;  // setting z in vec b to that value that was found above
  85.  
  86.             Vec3d anorm = unit_vector(a);   // finding the unit vector of vec a
  87.             Vec3d bnorm = unit_vector(b);   // finding the unit vector of vec b
  88.             Vec3d snorm = cross(bnorm, anorm); // finding the cross product of normalized vectors b and a
  89.             double brightness = dot_prod(snorm, sun, brightness); // finding the brightness at each point by calculating the dot product of the surface normal and the normalized vector of the sun
  90.  
  91.             int bness = ((.9*brightness) + .1) * 255;    // bness(brightness value) is the dot product times 255 (to get a value between 0 and 255 for pgm) .9 and +.1 added to represent the sky brighntess
  92.  
  93.                                                            // if the brighness is negative (cosine of the angle was negative, meaning the brighness of that point is in a shadow) set the brightness to a positive number
  94.             if (bness <0)
  95.             {
  96.                 bness = 0; //- abs(bness) ;
  97.  
  98.             }
  99.  
  100.  
  101.             newdat[r*egm.nc + c] = bness;
  102.            
  103.             prev[r*egm.nc + c] = bness;
  104.  
  105.         }
  106.  
  107.     }
  108.    
  109.  
  110.     delete[] prev;
  111.     prev = nullptr;
  112. }
  113. Then I write out the new values to a file
  114.  
  115. void write_pgm(string outname, Header egm, float * &data_e1, int* newdat, double* &dnew)
  116. {
  117.  
  118.  
  119.     ofstream opfs;
  120.     opfs.open(outname + ".pgm");
  121.     opfs << "P2" << endl;
  122.     opfs << egm.nr << endl;
  123.     opfs << egm.nc << endl;
  124.     opfs << 255 << endl;
  125.  
  126.     for (int r = 0; r<egm.nr; r++)
  127.     {
  128.         for (int c = 0; c<egm.nc; c++)
  129.         {
  130.  
  131.  
  132.             opfs << newdat[r*egm.nc + c];
  133.  
  134.             opfs << "\t";
  135.         }opfs << endl;
  136.     }
  137.     opfs.close();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement