Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 #include <string.h>
00038 #include <stdio.h>
00039 #include <math.h>
00040 
00041 #include "PSSources.h"
00042 
00043 
00044 
00045 plumesim::PSLog::PSLog()
00046 {
00047         this->maxPointsPerCell = 10;
00048         this->logFilePath = "log.txt";
00049 }
00050 
00051 
00052 
00053 plumesim::PSLog::~PSLog()
00054 {
00055         
00056 }
00057 
00058 
00059 
00060 int plumesim::PSLog::setup()
00061 {
00062         if(!plumePoints.empty()) plumePoints.clear();
00063         if(!frame.empty()) frame.clear();
00064         
00065         
00066         this->logFile = fopen(logFilePath.c_str(), "r");
00067         
00068         
00069         if( fscanf(this->logFile, "%d %d %lf\n", &this->period, &this->numOfFrames, &this->cellSize) == 0)
00070         {
00071                 return(-1);
00072         }
00073         
00074         if(this->numOfFrames > 1) this->changesOverTime = true;
00075         else this->changesOverTime = false;
00076         this->isPlaying = true;
00077         
00078         return(0);
00079 }
00080 
00081 
00082 
00083 int plumesim::PSLog::cleanup()
00084 {
00085         
00086         fclose(logFile);
00087         
00088         plumePoints.clear();
00089         frame.clear();
00090         return 0;
00091 }
00092 
00093 
00094 
00095 int plumesim::PSLog::generatePoints()
00096 {
00097         int i, j;
00098         int pointsCount;
00099         int pointsInFrame;
00100         
00101         if( !feof(logFile) )
00102         {
00103                 if(!plumePoints.empty()) plumePoints.clear();
00104                 if(!frame.empty()) frame.clear();
00105 
00106                 
00107                 fscanf(logFile, "+%d %d\n", ¤tFrame, &pointsInFrame);
00108                 
00109                 for(i=0 ; i<pointsInFrame ; i++)
00110                 {
00111                         PSFramePoint pointFromFrame;
00112                         
00113                         
00114                         fscanf(logFile, "%lf %lf %lf\n", &pointFromFrame.point.px, &pointFromFrame.point.py, &pointFromFrame.odor.chemical);
00115                         
00116                         this->frame.push_back(pointFromFrame);
00117                         
00118                         pointsCount = round(maxPointsPerCell * pointFromFrame.odor.chemical/100.0);
00119                         for(j=0 ; j<pointsCount ; j++)
00120                         {
00121                                 PSPoint3d point;
00122                                 point.px = pointFromFrame.point.px + cellSize * randomNormal();
00123                                 point.py = pointFromFrame.point.py + cellSize * randomNormal();
00124                                 
00125                                 point.pz = 0.0;
00126                                 plumePoints.push_back(point);
00127                         }
00128                 }
00129         }
00130         else
00131         {
00132                 isPlaying = false;
00133         }
00134         return(0);
00135 }
00136 
00137 
00138 
00139 int plumesim::PSLog::getChemicalReading(PSPoint3d * point, PSOdorData * odor_data)
00140 {       
00141         double distance;
00142         double minDistance = cellSize;
00143         
00144         odor_data->chemical = 0.0;
00145         odor_data->windSpeed = 0.0;
00146         odor_data->windDirection = 0.0;
00147         
00148         PSFramePoint * framePoint;
00149         for(int i=0 ; i<this->frame.size() ; i++)
00150         {
00151                 framePoint = &this->frame[i];
00152                 
00153                 distance = sqrt((point->px - framePoint->point.px)*(point->px - framePoint->point.px) + (point->py - framePoint->point.py)*(point->py - framePoint->point.py) + (point->pz - framePoint->point.pz)*(point->pz - framePoint->point.pz));
00154                 if(distance < minDistance)
00155                 {
00156                         odor_data->chemical = framePoint->odor.chemical;
00157                         odor_data->windSpeed = framePoint->odor.windSpeed;
00158                         odor_data->windDirection = framePoint->odor.windDirection;
00159                         
00160                         minDistance = distance;
00161                 }
00162         }
00163         return(0);
00164 }
00165 
00166 
00167