Blame view

FileIO.cpp 4.07 KB
0c9bf8ae   dmayerich   Case-sensitive er...
1
2
3
4
5
6
7
8
  #include "globals.h"

  #include <sstream>

  #include <fstream>

  #include <iostream>

  using namespace std;

  

  vector<SpecPair> LoadSpectrum(string filename)

  {

8ffb8373   dmayerich   Improved material...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
      //load a spectrum from a file and resample to 2wn intervals

  

      //create the spectrum

      vector<SpecPair> S;

  

      //open the file

      ifstream inFile(filename.c_str());
      if(!inFile)
      {
          cout<<"Error loading spectrum: "<<inFile<<endl;
          return S;
      }

  

      //read all elements of the file

      SpecPair temp;

      while(!inFile.eof()) {

          inFile>>temp.nu;

          inFile>>temp.A;

          S.push_back(temp);

      }

  

      //compute the minimum and maximum input wavenumbers

      double inMin = S.front().nu;

      double inMax = S.back().nu;

  

      int nuMin = (int)ceil(inMin);

      int nuMax = (int)floor(inMax);

  

      //make sure both are either even or odd

      if(nuMin % 2 != nuMax % 2)

          nuMax--;

  

      //compute the number of values in the resampled spectrum

      int nVals = (nuMax - nuMin)/2 + 1;

  

      //allocate space for the spectrum

      vector<SpecPair> outSpec;

  

      double nu, highVal, lowVal, a;

      int j=1;

      for(int i=0; i<nVals; i++)

      {

          nu = nuMin + i * 2;

          temp.nu = nu;

  

          //handle the boundary cases

          if(nu < inMin || nu > inMax)

              temp.A = 0.0;

          else

          {

              //move to the correct position in the input array

              while(j < (int)S.size()-1 && S[j].nu <= nu)

                  j++;

  

  

              lowVal = S[j-1].nu;

              highVal = S[j].nu;

              a = (nu - lowVal)/(highVal - lowVal);

              temp.A = S[j-1].A * (1.0 - a) + S[j].A * a;

          }

          outSpec.push_back(temp);

      }

  

  

  

      return outSpec;

0c9bf8ae   dmayerich   Case-sensitive er...
75
76
77
78
  }

  

  vector<SpecPair> SetReferenceSpectrum(char* text)

  {

8ffb8373   dmayerich   Improved material...
79
      stringstream inString(text);

0c9bf8ae   dmayerich   Case-sensitive er...
80
  

8ffb8373   dmayerich   Improved material...
81
82
      //create the spectrum

      vector<SpecPair> S;

0c9bf8ae   dmayerich   Case-sensitive er...
83
  

8ffb8373   dmayerich   Improved material...
84
85
86
87
88
89
      SpecPair temp;

      while(!inString.eof()) {

          inString>>temp.nu;

          inString>>temp.A;

          S.push_back(temp);

      }

0c9bf8ae   dmayerich   Case-sensitive er...
90
  

8ffb8373   dmayerich   Improved material...
91
      return S;

0c9bf8ae   dmayerich   Case-sensitive er...
92
93
  }

  

8ffb8373   dmayerich   Improved material...
94
  /*void SaveK(string fileName)

0c9bf8ae   dmayerich   Case-sensitive er...
95
  {

8ffb8373   dmayerich   Improved material...
96
97
98
99
100
101
102
103
104
105
      ofstream outFile(fileName.c_str());

      for(unsigned int i=0; i<EtaK.size(); i++)

      {

          outFile<<EtaK[i].nu<<"          ";

          outFile<<EtaK[i].A<<endl;

      }

      outFile.close();

  }*/

  

  void SaveMaterial(string fileName)

0c9bf8ae   dmayerich   Case-sensitive er...
106
  {

8ffb8373   dmayerich   Improved material...
107
108
109
110
111
112
113
114
115
      ofstream outFile(fileName.c_str());
      outFile<<"nu"<<'\t'<<"n"<<'\t'<<"k"<<endl;

      for(unsigned int i=0; i<EtaN.size(); i++)

      {

          outFile<<EtaN[i].nu<<'\t';

          outFile<<EtaN[i].A<<'\t';
          outFile<<EtaK[i].A<<endl;

      }

      outFile.close();

0c9bf8ae   dmayerich   Case-sensitive er...
116
117
118
119
  }

  

  void SaveSimulation(string fileName)

  {

8ffb8373   dmayerich   Improved material...
120
121
122
123
124
125
126
127
      ofstream outFile(fileName.c_str());

      outFile.precision(10);

      for(unsigned int i=0; i<SimSpectrum.size(); i++)

      {

          outFile<<SimSpectrum[i].nu<<"          ";

          outFile<<SimSpectrum[i].A<<endl;

      }

      outFile.close();

0c9bf8ae   dmayerich   Case-sensitive er...
128
129
130
131
  }

  

  void SaveState()

  {

8ffb8373   dmayerich   Improved material...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      ofstream outFile("main.prj");

      //Window Parameters

      outFile<<nuMin<<endl;

      outFile<<nuMax<<endl;

      outFile<<aMin<<endl;

      outFile<<aMax<<endl;

      outFile<<dNu<<endl;

  

      //material parameters

      outFile<<radius<<endl;

      outFile<<baseIR<<endl;

      outFile<<cA<<endl;

  

      //optical parameters

      outFile<<cNAi<<endl;

      outFile<<cNAo<<endl;

      outFile<<oNAi<<endl;

      outFile<<oNAo<<endl;

  

      outFile.close();

0c9bf8ae   dmayerich   Case-sensitive er...
152
153
154
155
156
  

  }

  

  void LoadState()

  {

8ffb8373   dmayerich   Improved material...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
      ifstream inFile("main.prj");

      //Window Parameters

      inFile>>nuMin;

      inFile>>nuMax;

      inFile>>aMin;

      inFile>>aMax;

      inFile>>dNu;

  

      //material parameters

      inFile>>radius;

      inFile>>baseIR;

      inFile>>cA;

  

      //optical parameters

      inFile>>cNAi;

      inFile>>cNAo;

      inFile>>oNAi;

      inFile>>oNAo;

  

      inFile.close();

0c9bf8ae   dmayerich   Case-sensitive er...
177
178
179
180
181
182
  

  }

  

  void SetDefaults()

  {

  

8ffb8373   dmayerich   Improved material...
183
184
185
      nuMin = 800;

      nuMax = 4000;

      dNu = 2;

0c9bf8ae   dmayerich   Case-sensitive er...
186
  

8ffb8373   dmayerich   Improved material...
187
188
      aMin = 0;

      aMax = 1;

0c9bf8ae   dmayerich   Case-sensitive er...
189
190
  

  

8ffb8373   dmayerich   Improved material...
191
192
193
194
195
196
      //material parameters

      radius = 4.0f;

      baseIR = 1.49f;

      cA = 1.0;

      vector<SpecPair> KMaterial;

      vector<SpecPair> NMaterial;

0c9bf8ae   dmayerich   Case-sensitive er...
197
  

8ffb8373   dmayerich   Improved material...
198
199
200
201
202
203
      //optical parameters

      cNAi = 0.0;

      cNAo = 0.6;

      oNAi = 0.0;

      oNAo = 0.6;

  }