00001 #include "Batch.h"
00002 #include <iostream>
00003
00004
00005 #include <cstdlib>
00006 #include <log.h>
00007 #include <time.h>
00008
00009 using namespace std;
00010
00011 int main(int argc,char *argv[]) {
00012 bool debug = false;
00013 if(argc==2) {
00014 if(string(argv[1]) == "debug") debug = true;
00015 }
00016 Batch b = Batch(debug);
00017 b.Init();
00018 return 0;
00019 }
00020
00021 Batch::Batch() {
00022 Batch(false);
00023 }
00024
00025 Batch::Batch(bool debug) {
00026 m_mainErr = "";
00027 m_subErr = "";
00028 log::clear();
00029 log::setDebug(debug);
00030 log::write("Batch started");
00031 }
00032
00033 void Batch::Init() {
00034 m_mainRun = true;
00035 m_subRun = true;
00036
00037
00038 while(m_mainRun) {
00039 vector<string> options;
00040 options.push_back("Import models");
00041 char* optname = "Analyze models";
00042 int count = m_inputModels.size();
00043 if(count>0) {
00044 char tmp[100];
00045 #ifndef __WINDOWS__
00046 sprintf(tmp,"%s (%d)",optname,count);
00047 #else
00048 sprintf_s(tmp,"%s (%d)",optname,count);
00049 #endif
00050 optname = tmp;
00051 }
00052 string s = string(optname);
00053 options.push_back(s);
00054 optname = "Export models";
00055 count = m_inputModels.size() + m_refModels.size() + m_generatedModels.size();
00056 if(count>0) {
00057 char tmp[100];
00058 #ifndef __WINDOWS__
00059 sprintf(tmp,"%s (%d)",optname,count);
00060 #else
00061 sprintf_s(tmp,"%s (%d)",optname,count);
00062 #endif
00063 optname = tmp;
00064 }
00065 s = string(optname);
00066 options.push_back(s);
00067 options.push_back("Test");
00068
00069 renderOptions("PCG",options);
00070 int o = handleInput();
00071 switch(o) {
00072 case 1: import();break;
00073 case 2: analyze(); break;
00074 case 3: Export(); break;
00075 case 4: Test(); break;
00076 default: m_mainErr = "Not a valid option!";break;
00077 }
00078 }
00079 }
00080
00081 void Batch::import() {
00082 m_subRun=true;
00083 vector<string> options;
00084 options.push_back("Import a model");
00085 options.push_back("Import models");
00086 options.push_back("Import data models");
00087
00088 while(m_subRun) {
00089 renderOptions("Import models", options, true);
00090 int o = handleInput();
00091 switch(o) {
00092 case 1: importModel();break;
00093 case 2: importDir(); break;
00094 case 3: importData(); break;
00095 default: m_subErr = "Not a valid option!";break;
00096 }
00097 }
00098 }
00099
00100 void Batch::importModel() {
00101 ExampleModel* em = new ExampleModel();
00102 functions::Importer im = functions::Importer();
00103 string file = "";
00104 bool run = true;
00105 renderTitle("Import a model",true);
00106 while(run) {
00107 cout << "Enter path to the model file: ";
00108 cin >> file;
00109 char temp[100];
00110 cin.getline(temp, 100);
00111 run = !im.importModel(file.c_str(),em);
00112 if(!run) {
00113 m_subErr = "Model loaded";
00114 m_inputModels.push_back(em);
00115 }
00116 }
00117 }
00118
00119 void Batch::importDir() {
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 }
00142
00143 void Batch::importData() {
00144 functions::Importer im = functions::Importer();
00145 string path = "";
00146 string name = "";
00147 string ending = "";
00148 int rangeStart = 0;
00149 int rangeEnd = 1;
00150 char temp[100];
00151 bool run = true;
00152 renderTitle("Import data models",true);
00153 while(run) {
00154 cout << "Enter path to the data models (../data/): ";
00155 cin.getline(temp, 100);
00156 path.assign(temp);
00157 cout << "Enter start file name (exportFile): ";
00158 cin.getline(temp, 100);
00159 name.assign(temp);
00160 cout << "Enter file ending (.x3d): ";
00161 cin.getline(temp, 100);
00162 ending.assign(temp);
00163 cout << "Enter start range (1): ";
00164 cin.getline(temp, 100);
00165 rangeStart=atoi(temp);
00166 cout << "Enter end range (99): ";
00167 cin.getline(temp, 100);
00168 rangeEnd=atoi(temp);
00169 for(int i=rangeStart;i<=rangeEnd;i++) {
00170 std::cout << "Importing model " << i << " of " << rangeEnd << " \r" << std::flush;
00171 ExampleModel* em = new ExampleModel();
00172 string filename = path + name;
00173 #ifndef __WINDOWS__
00174 sprintf(temp,"%d",i);
00175 #else
00176 sprintf_s(temp,"%d",i);
00177 #endif
00178 filename.append(string(temp));
00179 filename.append(ending);
00180 run = !im.importModel(filename.c_str(),em);
00181 if(!run) {
00182 m_subErr = "Data Models loaded";
00183 m_inputModels.push_back(em);
00184 }
00185 }
00186 }
00187 }
00188
00189 void Batch::analyze() {
00190 m_subRun=true;
00191 vector<string> options;
00192 char tmp[100];
00193 #ifndef __WINDOWS__
00194 sprintf(tmp,"Analyze %d models",m_inputModels.size());
00195 #else
00196 sprintf_s(tmp,"Analyze %d models",m_inputModels.size());
00197 #endif
00198 string s = string(tmp);
00199 options.push_back(s);
00200 options.push_back("Get a Model");
00201 options.push_back("Get random Model");
00202
00203 while(m_subRun) {
00204 renderOptions("Analyze models", options, true);
00205 int o = handleInput();
00206 switch(o) {
00207 case 1: analyzeModel();break;
00208 case 2: getModel();break;
00209 case 3: getRandomModels();break;
00210 default: m_subErr = "Not a valid option!";break;
00211 }
00212 }
00213 }
00214
00215 void Batch::analyzeModel() {
00216
00217 ReferenceModel refModel = ReferenceModel();
00218 refModel.setVertexs(m_inputModels[0]->getVertexs());
00219 refModel.setFaces(m_inputModels[0]->getFaces());
00220 log::write("*************Starting to Analyze*************");
00221 printf("start\n");
00222 pca.mergeData(refModel,m_inputModels);
00223 printf("mergeData done\n");
00224 log::write("Calculating mean");
00225 pca.mean();
00226 printf("mean done\n");
00227 log::write("Adjusting data");
00228 pca.adjust();
00229 printf("adjust done\n");
00230 log::write("Finding Covariance");
00231 pca.covariance();
00232 printf("covariance done\n");
00233 log::write("Begin EigenVector calculations");
00234 log::write("Timer Started");
00235 clock_t begin = clock();
00236 pca.findComponents();
00237 clock_t end = clock();
00238 log::write("Timer Stopped");
00239 log::write("Time elapsed in seconds:", difftime(end, begin));
00240 log::write("Time elapsed in clock cycles:",(int)(end-begin));
00241 log::write("End EigenVector calculations");
00242 printf("finding done\n");
00243 pca.computeEnergy();
00244 printf("compute energy done\n");
00245 pca.selectComponents();
00246 printf("select components done\n");
00247 pca.transformation();
00248 m_subErr = "Models analyzed";
00249 log::write("*************ANALYSIS ENDED*************");
00250 }
00251
00252 void Batch::getModel() {
00253 renderTitle("Get a model");
00254
00255 }
00256
00257 void Batch::getRandomModels() {
00258 renderTitle("Generates 10 random models",true);
00259 int dim = pca.getInputDimension();
00260 Matrix input(1,dim);
00261 int lowest=-10, highest=10;
00262 int range=(highest-lowest)+1;
00263 for(int i=0; i<10;i++) {
00264 if(dim=1) {
00265 input(1,1) = i-5;
00266 } else {
00267 for(int n=0;n<dim;n++) {
00268 input(1,n+1) = lowest+int(range*rand()/(RAND_MAX + 1.0));
00269 }
00270 }
00271 log::write("Batch-Input",input);
00272 ReferenceModel refModel = pca.getModel(input);
00273 log::write("ReferenceModel",refModel);
00274 m_generatedModels.push_back(refModel);
00275 }
00276 }
00277
00278 void Batch::Export() {
00279 m_subRun=true;
00280 vector<string> options;
00281 options.push_back("Export a model");
00282 char* optname = "Export all models";
00283 int count = m_inputModels.size() + m_refModels.size() + m_generatedModels.size();
00284 if(count>0) {
00285 char tmp[100];
00286 #ifndef __WINDOWS__
00287 sprintf(tmp,"%s (%d)",optname,count);
00288 #else
00289 sprintf_s(tmp,"%s (%d)",optname,count);
00290 #endif
00291 optname = tmp;
00292 }
00293 string s = string(optname);
00294 options.push_back(s);
00295
00296 while(m_subRun) {
00297 renderOptions("Export models", options, true);
00298 int o = handleInput();
00299 switch(o) {
00300 case 1: ExportModel();break;
00301 case 2: ExportModels(); break;
00302 default: m_subErr = "Not a valid option!";break;
00303 }
00304 }
00305 }
00306
00307 void Batch::ExportModel() {
00308
00309 }
00310
00311 void Batch::ExportModels() {
00312 log::write("Batch::ExportModels");
00313 functions::Exporter ex = functions::Exporter();
00314 char tmp[100];
00315 log::write("Export Inputmodels");
00316 for(int i=0; i<m_inputModels.size(); i++) {
00317 #ifndef __WINDOWS__
00318 sprintf(tmp,"inputModel%d.x3d",i);
00319 #else
00320 sprintf_s(tmp,"inputModel%d.x3d",i);
00321 #endif
00322 ex.exportModel(m_inputModels[i],tmp);
00323 }
00324 log::write("Export refModels");
00325 for(int i=0; i<m_refModels.size(); i++) {
00326 #ifndef __WINDOWS__
00327 sprintf(tmp,"referenceModel%d.x3d",i);
00328 #else
00329 sprintf_s(tmp,"referenceModel%d.x3d",i);
00330 #endif
00331
00332 ex.exportModel(&m_refModels[i],tmp);
00333 }
00334 log::write("Export generatedModels");
00335 for(int i=0; i<m_generatedModels.size(); i++) {
00336 #ifndef __WINDOWS__
00337 sprintf(tmp,"generatedModel%d.x3d",i);
00338 #else
00339 sprintf_s(tmp,"generatedModel%d.x3d",i);
00340 #endif
00341 ex.exportModel(&m_generatedModels[i],tmp);
00342 }
00343 m_subErr = "Models exported";
00344 }
00345
00346
00347
00348 void Batch::Test() {
00349 m_subRun=true;
00350 vector<string> options;
00351 options.push_back("ALL Methods");
00352 options.push_back("JACOBI Method");
00353 options.push_back("HOUSEHOLDER Method");
00354 options.push_back("Time stability");
00355 options.push_back("All tests");
00356
00357 while(m_subRun) {
00358 renderOptions("Test", options, true);
00359 int o = handleInput();
00360 switch(o) {
00361 case 1: TestSystem(PCA,Analyzer::ALL);break;
00362 case 2: TestSystem(PCA,Analyzer::JACOBI); break;
00363 case 3: TestSystem(PCA,Analyzer::HOUSEHOLDER); break;
00364 case 4: TestSystem(STABILITY); break;
00365 case 5: TestSystem(ALLTEST); break;
00366 default: m_subErr = "Not a valid option!";break;
00367 }
00368 }
00369 }
00370
00371 void Batch::TestSystem(e_TESTMODE mode) {
00372 TestSystem(mode,Analyzer::ALL);
00373 }
00374
00375 void Batch::TestSystem(e_TESTMODE mode, Analyzer::e_PCAMode method) {
00376 #ifdef __WINDOWS__
00377 system("cls");
00378 #else
00379 system("clear");
00380 #endif
00381 log::clear();
00382 log::write("TESTING STARTED");
00383 if(mode == PCA || mode == ALLTEST) {
00384 log::write("PCA TEST");
00385 int testRunNumbers[4] = {5,10,50,100};
00386 if(method == Analyzer::ALL) {
00387 for(int i=0; i<4; i++) {
00388 TestAnalyzer(Analyzer::JACOBI, testRunNumbers[i]);
00389 }
00390 for(int i=0; i<4; i++) {
00391 TestAnalyzer(Analyzer::HOUSEHOLDER,testRunNumbers[i]);
00392 }
00393 } else {
00394 for(int i=0; i<4; i++) {
00395 TestAnalyzer(method,testRunNumbers[i]);
00396 }
00397 }
00398 } else if(mode == STABILITY || mode == ALLTEST) {
00399 log::write("TIME STABILITY TEST");
00400 int testRunNumbers[4] = {2,3,4,5};
00401 for(int i=0;i<4;i++) {
00402 for(int n=0;n<100;n++) {
00403 log::write("Run nr:",n+1);
00404 TestAnalyzer(Analyzer::HOUSEHOLDER,testRunNumbers[i]);
00405 }
00406 }
00407 }
00408 }
00409
00410 void Batch::TestAnalyzer(Analyzer::e_PCAMode mode, int size) {
00411 if(mode == Analyzer::JACOBI) {
00412 printf("Running JACOBI with %d models",size);
00413 } else if(mode == Analyzer::HOUSEHOLDER) {
00414 printf("Running HOUSEHOLDER with %d models",size);
00415 }
00416 ReferenceModel refModel = ReferenceModel();
00417 refModel.setVertexs(m_inputModels[0]->getVertexs());
00418 refModel.setFaces(m_inputModels[0]->getFaces());
00419 log::write("*************Starting to Analyze*************");
00420 pca.mergeData(refModel,m_inputModels,size);
00421 log::write("Calculating mean");
00422 pca.mean();
00423 log::write("Adjusting data");
00424 pca.adjust();
00425 log::write("Finding Covariance");
00426 pca.covariance();
00427 log::write("Begin EigenVector calculations");
00428 log::write("Timer Started");
00429 clock_t begin = clock();
00430 pca.findComponents(mode);
00431 clock_t end = clock();
00432 float time = (float)(end - begin)/CLOCKS_PER_SEC;
00433 printf(" - It took %f seconds\n",time);
00434 log::write("Timer Stopped");
00435 log::write("Time elapsed in seconds:", time);
00436 log::write("Time elapsed in clock cycles:",(int)(end-begin));
00437 log::write("End EigenVector calculations");
00438 pca.computeEnergy();
00439 pca.selectComponents();
00440 pca.transformation();
00441 log::write("*************ANALYSIS ENDED*************");
00442 }
00443
00444
00445
00446 void Batch::renderTitle(string title, bool sub) {
00447 #ifdef __WINDOWS__
00448 system("cls");
00449 #else
00450 system("clear");
00451 #endif
00452 string err = m_mainErr;
00453 if(sub) {
00454 err = m_subErr;
00455 }
00456 cout << err << "\n\n\n";
00457 cout << " " << title << "\n\n";
00458 m_mainErr = "";
00459 m_subErr = "";
00460 }
00461
00462 void Batch::renderOptions(string title, vector<string> options, bool back) {
00463 renderTitle(title,back);
00464 for(int i=0; i<options.size();i++) {
00465 cout << " " << i+1 << ") " << options[i] << "\n";
00466 }
00467 cout << "\n\n";
00468 if(back) {
00469 cout << " 8) Back\n";
00470 }
00471 cout << " 9) Quit\n\n";
00472 cout << " Select a option: ";
00473
00474 }
00475
00476 int Batch::handleInput() {
00477
00478 int opt;
00479 char temp[100];
00480 cin.getline(temp, 100);
00481
00482 opt=atoi(temp);
00483 if(opt==9) {
00484 m_mainRun = false;
00485 m_subRun = false;
00486 } else if(opt == 8) {
00487 m_subRun = false;
00488 }
00489
00490 return opt;
00491 }
00492