source: GTP/trunk/Lib/Vis/shared/EvalStats/EvalStats.cpp @ 827

Revision 827, 8.0 KB checked in by mattausch, 18 years ago (diff)

created library from the preprocessor

RevLine 
[656]1// EvalStats.cpp : Defines the entry point for the console application.
2//
3#include "stdafx.h"
4
5#include <string>
6#include <vector>
7#include <istream>
8#include <iostream>
9#include <fstream>
[744]10#include <math.h>
11#include <algorithm>
[656]12
13using namespace std;
14
[667]15/** This is a small function which takes two log files and computes the
16        difference in percent of the weighted render cost.
17*/
[668]18
19
20struct RenderStats
[656]21{
[668]22        float mRenderCost;
23        float mAvgRenderCost;
24};
25
26
27typedef vector<RenderStats> StatsContainer;
28
29
30enum {NO_TAG, RENDER_COST, AVG_RENDER_COST};
31
32
33bool extractRenderStats(ifstream &file, StatsContainer &renderStats)
34{
[656]35        if (!file.is_open())
36                return false;
37               
38        string buf;
39
[668]40        int currentTag = NO_TAG;
[675]41
[656]42        while (!(getline(file, buf)).eof())
43        {
[668]44                //cout << "buf: " << buf << endl;
45
[656]46                if (buf[0] == '#')
47                {
48                        char entry[50];
49
50                        sscanf(buf.c_str(), "#%s", entry);
51                       
[668]52                        // new entry
53                        if (strcmp(entry, "Pass") == 0)
54                        {
55                                //cout << "\nnew render stat " << endl;
56                                renderStats.push_back(RenderStats());
57                        }
58                        else if (strcmp(entry, "TotalRenderCost") == 0)
59                        {
60                                currentTag = RENDER_COST;
61                        }
62                        else if (strcmp(entry, "AvgRenderCost") == 0)
63                        {
64                                currentTag = AVG_RENDER_COST;
65                        }
[656]66                }
[668]67                else
[656]68                {
[668]69                        float val;
70                        sscanf(buf.c_str(), "%f", &val);
[656]71                       
[668]72                        switch (currentTag)
73                        {
74                        case RENDER_COST:
75                                //cout << "render cost: " << val << endl;
76                renderStats.back().mRenderCost = val;
77                                break;
78                        case AVG_RENDER_COST:
79                                //cout << "avg render cost: " << val << endl;
80                                renderStats.back().mAvgRenderCost = val;
81                                break;
82                        default:
83                                break;
84                        }
85                       
86                        currentTag = NO_TAG;
[656]87                }
88        }
89
90        return true;
91}
92
[668]93
94
95void ComputeStats(ofstream &statsOut,
96                                  const RenderStats &firstStats,
97                                  const RenderStats &currentStats,
98                                  const int index)
99{
[670]100        float denom = firstStats.mRenderCost ? firstStats.mRenderCost : 0.000001f;
101        float costRatio = currentStats.mRenderCost / denom;
[668]102                       
103        denom = firstStats.mAvgRenderCost ? firstStats.mAvgRenderCost : 0.000001f;
[670]104        float avgCostRatio = currentStats.mAvgRenderCost / denom;
[668]105
106        statsOut << "#ViewCells\n" << index + 1 << endl
[670]107                         //<< "#TotalRenderCostGain\n" << 100.0f - costRatio * 100.0f << endl
108                         //<< "#AvgRenderCostGain\n" << 100.0f - avgCostRatio * 100.0f << endl << endl;
[675]109                         << "#TotalRenderCostRatio\n" << costRatio << endl
110                         << "#AvgRenderCostRatio\n" << avgCostRatio << endl << endl;
[668]111}
112
[736]113
[744]114inline bool vlt(const RenderStats &c1, const RenderStats &c2)
115{
116        return c1.mRenderCost > c2.mRenderCost;
117}
118
119
120// evaluate number of view cells needed for same rendercost
[733]121void EvalNumViewCells(ofstream &outstream,
[725]122                                          const StatsContainer &firstStats,
[736]123                                          const StatsContainer &currentStats)
[675]124{
[733]125       
[737]126        const int n = min((int)firstStats.size(), (int)currentStats.size());
127        //const int n = (int)firstStats.size();
128       
[725]129        StatsContainer::const_iterator it, it_end = currentStats.end();
[675]130
131        int i = 0;
[725]132
[737]133        // currentStats would be the other (necessarily worse?) method, firstStats would be our method
134        // loop through view cells of currentStats
135        // Compare render cost with render cost of other view cell
136        // compute ratio of view cells
[736]137        for (it = currentStats.begin(); it != it_end; ++ it, ++ i)
[725]138        {
[737]139               
[725]140                const float renderCost = (*it).mRenderCost;
141   
[733]142                // find equivalent render cost in the other stats container.
143                // don't stop until cursor is placed one element behind
144                // or end of vector is reached
[744]145                StatsContainer::const_iterator equalCostIt = std::upper_bound(firstStats.begin(), firstStats.end(), *it, vlt);
[737]146
[744]147                int j = (int)(equalCostIt - firstStats.begin());
148
[737]149                float val;
150
151                // soecial cases
152                if (j == 0)
153                {
154                        val = (float)j;
155                }
[744]156                else if (j >= n)
[737]157                {
158                        val = (float)(j - 1);
159                }
[744]160                else
161                // interpolate linearly. NOTE: probably big error because of steep curve
162                // => intepolate logarithmically
[737]163                {
[744]164#if 1
165                        const float rcu = log(firstStats[j - 1].mRenderCost);
166                        const float rcl = log(firstStats[j].mRenderCost);
167                        const float rc = log(renderCost);
168#else
[737]169                        const float rcu = firstStats[j - 1].mRenderCost;
170                        const float rcl = firstStats[j].mRenderCost;
[744]171                        const float rc = renderCost;
172#endif
[737]173                        const float factor = (rcu - rcl != 0) ?
[744]174                                (rcu - rc) / (rcu - rcl) : 1;
[737]175
[744]176                        // view cells needed for same render cost
[737]177                        val = (float)j - 1 + factor;
178                }
179
[725]180                // lower bound
[733]181                //int j = max (0, i - 1);
[744]182                //cout << "i: " << i << " j: " << j << endl;
[737]183                float ratio = (i && val) ? (float)i / val : 1;
[733]184
[744]185                //cout << "ratio: " << ratio << endl;
[736]186                outstream << "#Pass\n" << i << endl;
187                outstream << "#RenderCost\n" << renderCost << endl;
188                outstream << "#ViewCellsRatio\n" << ratio << endl << endl;     
[725]189        }
[675]190}
191
[733]192
[827]193
194
195
196// evaluate number of view cells needed for same rendercost
197int EvalSingleNumViewCells(const StatsContainer &currentStats,
198                                                   const float cost)
199{
200       
201        StatsContainer::const_iterator it, it_end = currentStats.end();
202
203        RenderStats dummy; dummy.mRenderCost = cost;
204       
205        // find equivalent render cost in the other stats container.
206        // don't stop until cursor is placed one element behind
207        // or end of vector is reached
208        StatsContainer::const_iterator equalCostIt = std::upper_bound(currentStats.begin(),currentStats.end(), dummy, vlt);
209
210        int j = (int)(equalCostIt - currentStats.begin());
211
212        return j;
213}
214
215
216
[656]217int _tmain(int argc, _TCHAR* argv[])
218{
[668]219        vector<StatsContainer> renderStats;
[656]220       
[668]221        if (argc < 3)
[667]222        {
[668]223                cerr << "arguments missing. Usage: input1 input2 ... intputn" << endl;
[667]224                exit(1);
225        }
226
[668]227        vector<string> outFilenames;
[736]228        vector<string> outFilenames2;
[827]229        vector<string> methodnames;
[668]230        // read input files from command line
231        for (int i = 1; i < argc; ++ i)
232        {
233                StatsContainer currentStats;
234     
[675]235                ifstream file;
236                file.open(argv[i]);
[667]237
[668]238                // extract the render cost
239                cout << "extracting render cost of file " << argv[i] << endl;
[675]240       
241                if (extractRenderStats(file, currentStats))
242                {
243                        renderStats.push_back(currentStats);
[656]244
[675]245                        // create output file name
246                        string fn = argv[i];
[668]247               
[675]248                        string::size_type pos = fn.find(".log", 0);
249                        fn.erase(pos, 4);
250                        //sscanf(argv[i], "%s.log", fn);
251                        string outFilename = string(fn) + string("-ratio.log");
[736]252                        string outFilename2 = string(fn) + string("-reverse.log");
[827]253                       
[675]254                        outFilenames.push_back(outFilename);
[736]255                        outFilenames2.push_back(outFilename2);
[827]256                        methodnames.push_back(fn);
257
[736]258                        cout << "new filen: " << outFilename2 << endl;
[675]259                }
260                else
261                {
262                        cout << "could not open file!" << endl;
263                }
[668]264        }
[667]265
[668]266        //statsOut << "rc1 size: " << (int)renderCosts1.size() << " " << (int)renderCosts2.size() << endl << endl;
267        vector<StatsContainer>::const_iterator it = renderStats.begin(), it_end = renderStats.end();
[736]268
[668]269        vector<string>::const_iterator sit, sit_end = outFilenames.end();
[736]270        vector<string>::const_iterator sit2, sit2_end = outFilenames2.end();
[827]271        vector<string>::const_iterator sit3, sit3_end = methodnames.end();
[667]272
[827]273
[668]274        sit = outFilenames.begin();
[736]275        sit2 = outFilenames2.begin();
[827]276        sit3 = methodnames.begin();
[736]277
[668]278        // compare all values with this vector
279        StatsContainer &firstStats = renderStats[0];
[656]280
[668]281        // don't compare with itself
[675]282        //++ it;
283        //++ sit;
[827]284        ofstream statsOut3;
285        statsOut3.open("numviewcells.log");
[668]286
[827]287        for (it; it != it_end; ++ it, ++ sit, ++ sit2, ++sit3)
[656]288        {
[675]289                cout << "now writing output to file " << *sit << endl;
[827]290
[675]291                ofstream statsOut;
292                statsOut.open((*sit).c_str());
[827]293       
[736]294                ofstream statsOut2;
295                statsOut2.open((*sit2).c_str());
[668]296
[736]297                cout << "opening new file: " << (*sit2).c_str() << endl;
[668]298                // compute size of output vector
299                const int n = min((int)firstStats.size(), (int)(*it).size());
300       
301                for (int i = 0; i < n; ++ i)
302                {
[744]303                        //cout << "rc: " << (*it)[i].mRenderCost << endl;
[668]304                        ComputeStats(statsOut, firstStats[i], (*it)[i], i);
305                }
[744]306
307                // evaluate number of view cells needed for same rendercost
[736]308                EvalNumViewCells(statsOut2, firstStats, (*it));
309
[827]310                const float rendercost = 100;
311                const int j = EvalSingleNumViewCells(*it, rendercost);
312
313                statsOut3 << (*sit3).c_str() << " " << j << endl;
314
[668]315                statsOut.close();
[736]316                statsOut2.close();
[656]317        }
318
[827]319        statsOut3.close();
320
321
322
[656]323        return 0;
324}
325
Note: See TracBrowser for help on using the repository browser.